blob: d72a0c5c64ae28a2d10e6114d0943d4cfccdc0cc [file] [log] [blame]
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001/*
Manuel Pégourié-Gonnard1c082f32014-06-12 22:34:55 +02002 * X.509 common functions for parsing and verification
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker7c6b2c32013-09-16 13:49:26 +020018 */
19/*
20 * The ITU-T X.509 standard defines a certificate format for PKI.
21 *
Manuel Pégourié-Gonnard1c082f32014-06-12 22:34:55 +020022 * http://www.ietf.org/rfc/rfc5280.txt (Certificates and CRLs)
23 * http://www.ietf.org/rfc/rfc3279.txt (Alg IDs for CRLs)
24 * http://www.ietf.org/rfc/rfc2986.txt (CSRs, aka PKCS#10)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020025 *
26 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
27 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
28 */
29
Gilles Peskinedb09ef62020-06-03 01:43:33 +020030#include "common.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032#if defined(MBEDTLS_X509_USE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020033
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000034#include "mbedtls/x509.h"
35#include "mbedtls/asn1.h"
Janos Follath73c616b2019-12-18 15:07:04 +000036#include "mbedtls/error.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000037#include "mbedtls/oid.h"
Rich Evans00ab4702015-02-06 13:43:58 +000038
Rich Evans36796df2015-02-12 18:27:14 +000039#include <stdio.h>
Rich Evans00ab4702015-02-06 13:43:58 +000040#include <string.h>
41
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000043#include "mbedtls/pem.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020044#endif
45
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/platform.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020047
Simon Butcherb5b6af22016-07-13 14:46:18 +010048#if defined(MBEDTLS_HAVE_TIME)
49#include "mbedtls/platform_time.h"
50#endif
Nicholas Wilson512b4ee2017-12-05 12:07:33 +000051#if defined(MBEDTLS_HAVE_TIME_DATE)
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +010052#include "mbedtls/platform_util.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020053#include <time.h>
54#endif
55
Gilles Peskine449bd832023-01-11 14:50:10 +010056#define CHECK(code) if ((ret = (code)) != 0) { return ret; }
Hanno Becker1eeca412018-10-15 12:01:35 +010057#define CHECK_RANGE(min, max, val) \
58 do \
59 { \
Gilles Peskine449bd832023-01-11 14:50:10 +010060 if ((val) < (min) || (val) > (max)) \
Hanno Becker1eeca412018-10-15 12:01:35 +010061 { \
Gilles Peskine449bd832023-01-11 14:50:10 +010062 return ret; \
Hanno Becker1eeca412018-10-15 12:01:35 +010063 } \
Gilles Peskine449bd832023-01-11 14:50:10 +010064 } while (0)
Rich Evans7d5a55a2015-02-13 11:48:02 +000065
Paul Bakker7c6b2c32013-09-16 13:49:26 +020066/*
67 * CertificateSerialNumber ::= INTEGER
68 */
Gilles Peskine449bd832023-01-11 14:50:10 +010069int mbedtls_x509_get_serial(unsigned char **p, const unsigned char *end,
70 mbedtls_x509_buf *serial)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020071{
Janos Follath865b3eb2019-12-16 11:46:15 +000072 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020073
Gilles Peskine449bd832023-01-11 14:50:10 +010074 if ((end - *p) < 1) {
75 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_SERIAL,
76 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
77 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +020078
Gilles Peskine449bd832023-01-11 14:50:10 +010079 if (**p != (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_PRIMITIVE | 2) &&
80 **p != MBEDTLS_ASN1_INTEGER) {
81 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_SERIAL,
82 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
83 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +020084
85 serial->tag = *(*p)++;
86
Gilles Peskine449bd832023-01-11 14:50:10 +010087 if ((ret = mbedtls_asn1_get_len(p, end, &serial->len)) != 0) {
88 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_SERIAL, ret);
89 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +020090
91 serial->p = *p;
92 *p += serial->len;
93
Gilles Peskine449bd832023-01-11 14:50:10 +010094 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020095}
96
97/* Get an algorithm identifier without parameters (eg for signatures)
98 *
99 * AlgorithmIdentifier ::= SEQUENCE {
100 * algorithm OBJECT IDENTIFIER,
101 * parameters ANY DEFINED BY algorithm OPTIONAL }
102 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100103int mbedtls_x509_get_alg_null(unsigned char **p, const unsigned char *end,
104 mbedtls_x509_buf *alg)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200105{
Janos Follath865b3eb2019-12-16 11:46:15 +0000106 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200107
Gilles Peskine449bd832023-01-11 14:50:10 +0100108 if ((ret = mbedtls_asn1_get_alg_null(p, end, alg)) != 0) {
109 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
110 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200111
Gilles Peskine449bd832023-01-11 14:50:10 +0100112 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200113}
114
115/*
Antonin Décimo36e89b52019-01-23 15:24:37 +0100116 * Parse an algorithm identifier with (optional) parameters
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100117 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100118int mbedtls_x509_get_alg(unsigned char **p, const unsigned char *end,
119 mbedtls_x509_buf *alg, mbedtls_x509_buf *params)
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100120{
Janos Follath865b3eb2019-12-16 11:46:15 +0000121 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100122
Gilles Peskine449bd832023-01-11 14:50:10 +0100123 if ((ret = mbedtls_asn1_get_alg(p, end, alg, params)) != 0) {
124 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
125 }
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100126
Gilles Peskine449bd832023-01-11 14:50:10 +0100127 return 0;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100128}
129
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200130/*
131 * Convert md type to string
132 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100133static inline const char *md_type_to_string(mbedtls_md_type_t md_alg)
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200134{
Gilles Peskine449bd832023-01-11 14:50:10 +0100135 switch (md_alg) {
Manuel Pégourié-Gonnard49e67f82023-03-16 11:39:20 +0100136#if defined(MBEDTLS_MD_CAN_MD5)
Gilles Peskine449bd832023-01-11 14:50:10 +0100137 case MBEDTLS_MD_MD5:
138 return "MD5";
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200139#endif
Manuel Pégourié-Gonnard49e67f82023-03-16 11:39:20 +0100140#if defined(MBEDTLS_MD_CAN_SHA1)
Gilles Peskine449bd832023-01-11 14:50:10 +0100141 case MBEDTLS_MD_SHA1:
142 return "SHA1";
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200143#endif
Manuel Pégourié-Gonnard49e67f82023-03-16 11:39:20 +0100144#if defined(MBEDTLS_MD_CAN_SHA224)
Gilles Peskine449bd832023-01-11 14:50:10 +0100145 case MBEDTLS_MD_SHA224:
146 return "SHA224";
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200147#endif
Manuel Pégourié-Gonnard49e67f82023-03-16 11:39:20 +0100148#if defined(MBEDTLS_MD_CAN_SHA256)
Gilles Peskine449bd832023-01-11 14:50:10 +0100149 case MBEDTLS_MD_SHA256:
150 return "SHA256";
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200151#endif
Manuel Pégourié-Gonnard49e67f82023-03-16 11:39:20 +0100152#if defined(MBEDTLS_MD_CAN_SHA384)
Gilles Peskine449bd832023-01-11 14:50:10 +0100153 case MBEDTLS_MD_SHA384:
154 return "SHA384";
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200155#endif
Manuel Pégourié-Gonnard49e67f82023-03-16 11:39:20 +0100156#if defined(MBEDTLS_MD_CAN_SHA512)
Gilles Peskine449bd832023-01-11 14:50:10 +0100157 case MBEDTLS_MD_SHA512:
158 return "SHA512";
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200159#endif
Manuel Pégourié-Gonnard49e67f82023-03-16 11:39:20 +0100160#if defined(MBEDTLS_MD_CAN_RIPEMD160)
Gilles Peskine449bd832023-01-11 14:50:10 +0100161 case MBEDTLS_MD_RIPEMD160:
162 return "RIPEMD160";
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200163#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100164 case MBEDTLS_MD_NONE:
165 return NULL;
166 default:
167 return NULL;
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200168 }
169}
170
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200171#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100172/*
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100173 * HashAlgorithm ::= AlgorithmIdentifier
174 *
175 * AlgorithmIdentifier ::= SEQUENCE {
176 * algorithm OBJECT IDENTIFIER,
177 * parameters ANY DEFINED BY algorithm OPTIONAL }
178 *
179 * For HashAlgorithm, parameters MUST be NULL or absent.
180 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100181static 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 +0100182{
Janos Follath865b3eb2019-12-16 11:46:15 +0000183 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100184 unsigned char *p;
185 const unsigned char *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186 mbedtls_x509_buf md_oid;
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100187 size_t len;
188
189 /* Make sure we got a SEQUENCE and setup bounds */
Gilles Peskine449bd832023-01-11 14:50:10 +0100190 if (alg->tag != (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) {
191 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
192 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
193 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100194
Andrzej Kurekfeaebc52020-07-16 04:37:41 -0400195 p = alg->p;
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100196 end = p + alg->len;
197
Gilles Peskine449bd832023-01-11 14:50:10 +0100198 if (p >= end) {
199 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
200 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
201 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100202
203 /* Parse md_oid */
204 md_oid.tag = *p;
205
Gilles Peskine449bd832023-01-11 14:50:10 +0100206 if ((ret = mbedtls_asn1_get_tag(&p, end, &md_oid.len, MBEDTLS_ASN1_OID)) != 0) {
207 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
208 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100209
210 md_oid.p = p;
211 p += md_oid.len;
212
213 /* Get md_alg from md_oid */
Gilles Peskine449bd832023-01-11 14:50:10 +0100214 if ((ret = mbedtls_oid_get_md_alg(&md_oid, md_alg)) != 0) {
215 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
216 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100217
218 /* Make sure params is absent of NULL */
Gilles Peskine449bd832023-01-11 14:50:10 +0100219 if (p == end) {
220 return 0;
221 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100222
Gilles Peskine449bd832023-01-11 14:50:10 +0100223 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_NULL)) != 0 || len != 0) {
224 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
225 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100226
Gilles Peskine449bd832023-01-11 14:50:10 +0100227 if (p != end) {
228 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
229 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
230 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100231
Gilles Peskine449bd832023-01-11 14:50:10 +0100232 return 0;
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100233}
234
235/*
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100236 * RSASSA-PSS-params ::= SEQUENCE {
237 * hashAlgorithm [0] HashAlgorithm DEFAULT sha1Identifier,
238 * maskGenAlgorithm [1] MaskGenAlgorithm DEFAULT mgf1SHA1Identifier,
239 * saltLength [2] INTEGER DEFAULT 20,
240 * trailerField [3] INTEGER DEFAULT 1 }
241 * -- Note that the tags in this Sequence are explicit.
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200242 *
243 * RFC 4055 (which defines use of RSASSA-PSS in PKIX) states that the value
244 * of trailerField MUST be 1, and PKCS#1 v2.2 doesn't even define any other
Tom Cosgrove1797b052022-12-04 17:19:59 +0000245 * option. Enforce this at parsing time.
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100246 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100247int mbedtls_x509_get_rsassa_pss_params(const mbedtls_x509_buf *params,
248 mbedtls_md_type_t *md_alg, mbedtls_md_type_t *mgf_md,
249 int *salt_len)
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100250{
Janos Follath865b3eb2019-12-16 11:46:15 +0000251 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100252 unsigned char *p;
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100253 const unsigned char *end, *end2;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100254 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200255 mbedtls_x509_buf alg_id, alg_params;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100256
257 /* First set everything to defaults */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200258 *md_alg = MBEDTLS_MD_SHA1;
259 *mgf_md = MBEDTLS_MD_SHA1;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100260 *salt_len = 20;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100261
262 /* Make sure params is a SEQUENCE and setup bounds */
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 if (params->tag != (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) {
264 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
265 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
266 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100267
268 p = (unsigned char *) params->p;
269 end = p + params->len;
270
Gilles Peskine449bd832023-01-11 14:50:10 +0100271 if (p == end) {
272 return 0;
273 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100274
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100275 /*
276 * HashAlgorithm
277 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100278 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
279 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
280 0)) == 0) {
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100281 end2 = p + len;
282
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100283 /* HashAlgorithm ::= AlgorithmIdentifier (without parameters) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100284 if ((ret = mbedtls_x509_get_alg_null(&p, end2, &alg_id)) != 0) {
285 return ret;
286 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100287
Gilles Peskine449bd832023-01-11 14:50:10 +0100288 if ((ret = mbedtls_oid_get_md_alg(&alg_id, md_alg)) != 0) {
289 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
290 }
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100291
Gilles Peskine449bd832023-01-11 14:50:10 +0100292 if (p != end2) {
293 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
294 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
295 }
296 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
297 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100298 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100299
Gilles Peskine449bd832023-01-11 14:50:10 +0100300 if (p == end) {
301 return 0;
302 }
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100303
304 /*
305 * MaskGenAlgorithm
306 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100307 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
308 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
309 1)) == 0) {
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100310 end2 = p + len;
311
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100312 /* MaskGenAlgorithm ::= AlgorithmIdentifier (params = HashAlgorithm) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100313 if ((ret = mbedtls_x509_get_alg(&p, end2, &alg_id, &alg_params)) != 0) {
314 return ret;
315 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100316
317 /* Only MFG1 is recognised for now */
Gilles Peskine449bd832023-01-11 14:50:10 +0100318 if (MBEDTLS_OID_CMP(MBEDTLS_OID_MGF1, &alg_id) != 0) {
319 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE,
320 MBEDTLS_ERR_OID_NOT_FOUND);
321 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100322
323 /* Parse HashAlgorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +0100324 if ((ret = x509_get_hash_alg(&alg_params, mgf_md)) != 0) {
325 return ret;
326 }
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100327
Gilles Peskine449bd832023-01-11 14:50:10 +0100328 if (p != end2) {
329 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
330 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
331 }
332 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
333 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100334 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100335
Gilles Peskine449bd832023-01-11 14:50:10 +0100336 if (p == end) {
337 return 0;
338 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100339
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100340 /*
341 * salt_len
342 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100343 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
344 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
345 2)) == 0) {
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100346 end2 = p + len;
347
Gilles Peskine449bd832023-01-11 14:50:10 +0100348 if ((ret = mbedtls_asn1_get_int(&p, end2, salt_len)) != 0) {
349 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
350 }
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100351
Gilles Peskine449bd832023-01-11 14:50:10 +0100352 if (p != end2) {
353 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
354 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
355 }
356 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
357 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100358 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100359
Gilles Peskine449bd832023-01-11 14:50:10 +0100360 if (p == end) {
361 return 0;
362 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100363
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100364 /*
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200365 * trailer_field (if present, must be 1)
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100366 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100367 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
368 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
369 3)) == 0) {
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200370 int trailer_field;
371
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100372 end2 = p + len;
373
Gilles Peskine449bd832023-01-11 14:50:10 +0100374 if ((ret = mbedtls_asn1_get_int(&p, end2, &trailer_field)) != 0) {
375 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
376 }
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100377
Gilles Peskine449bd832023-01-11 14:50:10 +0100378 if (p != end2) {
379 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
380 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
381 }
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200382
Gilles Peskine449bd832023-01-11 14:50:10 +0100383 if (trailer_field != 1) {
384 return MBEDTLS_ERR_X509_INVALID_ALG;
385 }
386 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
387 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100388 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100389
Gilles Peskine449bd832023-01-11 14:50:10 +0100390 if (p != end) {
391 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
392 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
393 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100394
Gilles Peskine449bd832023-01-11 14:50:10 +0100395 return 0;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100396}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200397#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100398
399/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200400 * AttributeTypeAndValue ::= SEQUENCE {
401 * type AttributeType,
402 * value AttributeValue }
403 *
404 * AttributeType ::= OBJECT IDENTIFIER
405 *
406 * AttributeValue ::= ANY DEFINED BY AttributeType
407 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100408static int x509_get_attr_type_value(unsigned char **p,
409 const unsigned char *end,
410 mbedtls_x509_name *cur)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200411{
Janos Follath865b3eb2019-12-16 11:46:15 +0000412 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200413 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200414 mbedtls_x509_buf *oid;
415 mbedtls_x509_buf *val;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200416
Gilles Peskine449bd832023-01-11 14:50:10 +0100417 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
418 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
419 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME, ret);
420 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200421
Hanno Becker12f62fb2019-02-12 17:22:36 +0000422 end = *p + len;
423
Gilles Peskine449bd832023-01-11 14:50:10 +0100424 if ((end - *p) < 1) {
425 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME,
426 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
427 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200428
429 oid = &cur->oid;
430 oid->tag = **p;
431
Gilles Peskine449bd832023-01-11 14:50:10 +0100432 if ((ret = mbedtls_asn1_get_tag(p, end, &oid->len, MBEDTLS_ASN1_OID)) != 0) {
433 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME, ret);
434 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200435
436 oid->p = *p;
437 *p += oid->len;
438
Gilles Peskine449bd832023-01-11 14:50:10 +0100439 if ((end - *p) < 1) {
440 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME,
441 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
442 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200443
Gilles Peskine449bd832023-01-11 14:50:10 +0100444 if (**p != MBEDTLS_ASN1_BMP_STRING && **p != MBEDTLS_ASN1_UTF8_STRING &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200445 **p != MBEDTLS_ASN1_T61_STRING && **p != MBEDTLS_ASN1_PRINTABLE_STRING &&
446 **p != MBEDTLS_ASN1_IA5_STRING && **p != MBEDTLS_ASN1_UNIVERSAL_STRING &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100447 **p != MBEDTLS_ASN1_BIT_STRING) {
448 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME,
449 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
450 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200451
452 val = &cur->val;
453 val->tag = *(*p)++;
454
Gilles Peskine449bd832023-01-11 14:50:10 +0100455 if ((ret = mbedtls_asn1_get_len(p, end, &val->len)) != 0) {
456 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME, ret);
457 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200458
459 val->p = *p;
460 *p += val->len;
461
Gilles Peskine449bd832023-01-11 14:50:10 +0100462 if (*p != end) {
463 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME,
464 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Hanno Becker12f62fb2019-02-12 17:22:36 +0000465 }
466
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200467 cur->next = NULL;
468
Gilles Peskine449bd832023-01-11 14:50:10 +0100469 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200470}
471
472/*
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000473 * Name ::= CHOICE { -- only one possibility for now --
474 * rdnSequence RDNSequence }
475 *
476 * RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
477 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200478 * RelativeDistinguishedName ::=
479 * SET OF AttributeTypeAndValue
480 *
481 * AttributeTypeAndValue ::= SEQUENCE {
482 * type AttributeType,
483 * value AttributeValue }
484 *
485 * AttributeType ::= OBJECT IDENTIFIER
486 *
487 * AttributeValue ::= ANY DEFINED BY AttributeType
Manuel Pégourié-Gonnard5d861852014-10-17 12:41:41 +0200488 *
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000489 * The data structure is optimized for the common case where each RDN has only
490 * one element, which is represented as a list of AttributeTypeAndValue.
491 * For the general case we still use a flat list, but we mark elements of the
492 * same set so that they are "merged" together in the functions that consume
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200493 * this list, eg mbedtls_x509_dn_gets().
David Horstmanned794832022-10-04 18:12:06 +0100494 *
David Horstmann11307a12022-10-17 18:10:23 +0100495 * On success, this function may allocate a linked list starting at cur->next
David Horstmanned794832022-10-04 18:12:06 +0100496 * that must later be free'd by the caller using mbedtls_free(). In error
497 * cases, this function frees all allocated memory internally and the caller
498 * has no freeing responsibilities.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200499 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100500int mbedtls_x509_get_name(unsigned char **p, const unsigned char *end,
501 mbedtls_x509_name *cur)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200502{
Janos Follath865b3eb2019-12-16 11:46:15 +0000503 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard5d861852014-10-17 12:41:41 +0200504 size_t set_len;
505 const unsigned char *end_set;
David Horstmanned794832022-10-04 18:12:06 +0100506 mbedtls_x509_name *head = cur;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200507
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100508 /* don't use recursion, we'd risk stack overflow if not optimized */
Gilles Peskine449bd832023-01-11 14:50:10 +0100509 while (1) {
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100510 /*
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000511 * parse SET
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100512 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100513 if ((ret = mbedtls_asn1_get_tag(p, end, &set_len,
514 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SET)) != 0) {
515 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME, ret);
David Horstmanned794832022-10-04 18:12:06 +0100516 goto error;
517 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200518
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100519 end_set = *p + set_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200520
Gilles Peskine449bd832023-01-11 14:50:10 +0100521 while (1) {
522 if ((ret = x509_get_attr_type_value(p, end_set, cur)) != 0) {
David Horstmanned794832022-10-04 18:12:06 +0100523 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +0100524 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200525
Gilles Peskine449bd832023-01-11 14:50:10 +0100526 if (*p == end_set) {
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000527 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100528 }
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000529
Manuel Pégourié-Gonnardeecb43c2015-05-12 12:56:41 +0200530 /* Mark this item as being no the only one in a set */
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000531 cur->next_merged = 1;
532
Gilles Peskine449bd832023-01-11 14:50:10 +0100533 cur->next = mbedtls_calloc(1, sizeof(mbedtls_x509_name));
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000534
Gilles Peskine449bd832023-01-11 14:50:10 +0100535 if (cur->next == NULL) {
David Horstmanned794832022-10-04 18:12:06 +0100536 ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
537 goto error;
538 }
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000539
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000540 cur = cur->next;
541 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200542
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100543 /*
544 * continue until end of SEQUENCE is reached
545 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100546 if (*p == end) {
547 return 0;
548 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200549
Gilles Peskine449bd832023-01-11 14:50:10 +0100550 cur->next = mbedtls_calloc(1, sizeof(mbedtls_x509_name));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200551
Gilles Peskine449bd832023-01-11 14:50:10 +0100552 if (cur->next == NULL) {
David Horstmanned794832022-10-04 18:12:06 +0100553 ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
554 goto error;
555 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200556
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100557 cur = cur->next;
558 }
David Horstmanned794832022-10-04 18:12:06 +0100559
560error:
David Horstmanned794832022-10-04 18:12:06 +0100561 /* Skip the first element as we did not allocate it */
Gilles Peskine449bd832023-01-11 14:50:10 +0100562 mbedtls_asn1_free_named_data_list_shallow(head->next);
Glenn Straussa4b40412022-06-26 19:32:09 -0400563 head->next = NULL;
David Horstmanned794832022-10-04 18:12:06 +0100564
Gilles Peskine449bd832023-01-11 14:50:10 +0100565 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200566}
567
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400568static int x509_date_is_valid(const mbedtls_x509_time *t)
Janos Follath87c98072017-02-03 12:36:59 +0000569{
David Horstmanncdf52832023-07-05 09:58:03 +0100570 unsigned int month_days;
571 unsigned int year;
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400572 switch (t->mon) {
573 case 1: case 3: case 5: case 7: case 8: case 10: case 12:
David Horstmanncdf52832023-07-05 09:58:03 +0100574 month_days = 31;
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400575 break;
576 case 4: case 6: case 9: case 11:
David Horstmanncdf52832023-07-05 09:58:03 +0100577 month_days = 30;
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400578 break;
579 case 2:
David Horstmanncdf52832023-07-05 09:58:03 +0100580 year = (unsigned int) t->year;
581 month_days = ((year & 3) || (!(year % 100)
582 && (year % 400)))
583 ? 28 : 29;
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400584 break;
585 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100586 return MBEDTLS_ERR_X509_INVALID_DATE;
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400587 }
Janos Follath87c98072017-02-03 12:36:59 +0000588
David Horstmanncdf52832023-07-05 09:58:03 +0100589 if ((unsigned int) (t->day - 1) >= month_days || /*(1 - days in month)*/
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400590 /*(unsigned int)( t->mon - 1 ) >= 12 ||*//*(1 - 12) checked above*/
591 (unsigned int) t->year > 9999 || /*(0 - 9999)*/
592 (unsigned int) t->hour > 23 || /*(0 - 23)*/
593 (unsigned int) t->min > 59 || /*(0 - 59)*/
594 (unsigned int) t->sec > 59) { /*(0 - 59)*/
595 return MBEDTLS_ERR_X509_INVALID_DATE;
Rich Evans7d5a55a2015-02-13 11:48:02 +0000596 }
Janos Follath87c98072017-02-03 12:36:59 +0000597
Gilles Peskine449bd832023-01-11 14:50:10 +0100598 return 0;
Rich Evans7d5a55a2015-02-13 11:48:02 +0000599}
600
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400601static int x509_parse2_int(const unsigned char *p)
Andres AG4b76aec2016-09-23 13:16:02 +0100602{
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400603 uint32_t d1 = p[0] - '0';
604 uint32_t d2 = p[1] - '0';
605 return (d1 < 10 && d2 < 10) ? (int) (d1 * 10 + d2) : -1;
Andres AG4b76aec2016-09-23 13:16:02 +0100606}
607
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200608/*
Janos Follath87c98072017-02-03 12:36:59 +0000609 * Parse an ASN1_UTC_TIME (yearlen=2) or ASN1_GENERALIZED_TIME (yearlen=4)
610 * field.
611 */
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400612static int x509_parse_time(const unsigned char *p, mbedtls_x509_time *tm,
613 size_t yearlen)
Janos Follath87c98072017-02-03 12:36:59 +0000614{
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400615 int x;
Janos Follath87c98072017-02-03 12:36:59 +0000616
617 /*
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400618 * Parse year, month, day, hour, minute, second
Janos Follath87c98072017-02-03 12:36:59 +0000619 */
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400620 tm->year = x509_parse2_int(p);
621 if (tm->year < 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100622 return MBEDTLS_ERR_X509_INVALID_DATE;
623 }
Janos Follath87c98072017-02-03 12:36:59 +0000624
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400625 if (4 == yearlen) {
626 x = tm->year * 100;
627 p += 2;
628 tm->year = x509_parse2_int(p);
629 if (tm->year < 0) {
630 return MBEDTLS_ERR_X509_INVALID_DATE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100631 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100632 } else {
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400633 x = (tm->year < 50) ? 2000 : 1900;
Janos Follath87c98072017-02-03 12:36:59 +0000634 }
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400635 tm->year += x;
Janos Follath87c98072017-02-03 12:36:59 +0000636
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400637 tm->mon = x509_parse2_int(p + 2);
638 tm->day = x509_parse2_int(p + 4);
639 tm->hour = x509_parse2_int(p + 6);
640 tm->min = x509_parse2_int(p + 8);
641 tm->sec = x509_parse2_int(p + 10);
Janos Follath87c98072017-02-03 12:36:59 +0000642
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400643 return x509_date_is_valid(tm);
Janos Follath87c98072017-02-03 12:36:59 +0000644}
645
646/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200647 * Time ::= CHOICE {
648 * utcTime UTCTime,
649 * generalTime GeneralizedTime }
650 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100651int mbedtls_x509_get_time(unsigned char **p, const unsigned char *end,
652 mbedtls_x509_time *tm)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200653{
Janos Follath865b3eb2019-12-16 11:46:15 +0000654 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Janos Follath87c98072017-02-03 12:36:59 +0000655 size_t len, year_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200656 unsigned char tag;
657
Gilles Peskine449bd832023-01-11 14:50:10 +0100658 if ((end - *p) < 1) {
659 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE,
660 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
661 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200662
663 tag = **p;
664
Gilles Peskine449bd832023-01-11 14:50:10 +0100665 if (tag == MBEDTLS_ASN1_UTC_TIME) {
Janos Follath87c98072017-02-03 12:36:59 +0000666 year_len = 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100667 } else if (tag == MBEDTLS_ASN1_GENERALIZED_TIME) {
Janos Follath87c98072017-02-03 12:36:59 +0000668 year_len = 4;
Gilles Peskine449bd832023-01-11 14:50:10 +0100669 } else {
670 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE,
671 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
672 }
Janos Follath87c98072017-02-03 12:36:59 +0000673
674 (*p)++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100675 ret = mbedtls_asn1_get_len(p, end, &len);
Janos Follath87c98072017-02-03 12:36:59 +0000676
Gilles Peskine449bd832023-01-11 14:50:10 +0100677 if (ret != 0) {
678 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE, ret);
679 }
Janos Follath87c98072017-02-03 12:36:59 +0000680
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400681 /* len is 12 or 14 depending on year_len, plus optional trailing 'Z' */
682 if (len != year_len + 10 &&
683 !(len == year_len + 11 && (*p)[(len - 1)] == 'Z')) {
684 return MBEDTLS_ERR_X509_INVALID_DATE;
685 }
686
687 (*p) += len;
688 return x509_parse_time(*p - len, tm, year_len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200689}
690
Gilles Peskine449bd832023-01-11 14:50:10 +0100691int mbedtls_x509_get_sig(unsigned char **p, const unsigned char *end, mbedtls_x509_buf *sig)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200692{
Janos Follath865b3eb2019-12-16 11:46:15 +0000693 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200694 size_t len;
Andres AG4bdbe092016-09-19 16:58:45 +0100695 int tag_type;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200696
Gilles Peskine449bd832023-01-11 14:50:10 +0100697 if ((end - *p) < 1) {
698 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_SIGNATURE,
699 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
700 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200701
Andres AG4bdbe092016-09-19 16:58:45 +0100702 tag_type = **p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200703
Gilles Peskine449bd832023-01-11 14:50:10 +0100704 if ((ret = mbedtls_asn1_get_bitstring_null(p, end, &len)) != 0) {
705 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_SIGNATURE, ret);
706 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200707
Andres AG4bdbe092016-09-19 16:58:45 +0100708 sig->tag = tag_type;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200709 sig->len = len;
710 sig->p = *p;
711
712 *p += len;
713
Gilles Peskine449bd832023-01-11 14:50:10 +0100714 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200715}
716
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100717/*
718 * Get signature algorithm from alg OID and optional parameters
719 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100720int mbedtls_x509_get_sig_alg(const mbedtls_x509_buf *sig_oid, const mbedtls_x509_buf *sig_params,
721 mbedtls_md_type_t *md_alg, mbedtls_pk_type_t *pk_alg,
722 void **sig_opts)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200723{
Janos Follath865b3eb2019-12-16 11:46:15 +0000724 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200725
Gilles Peskine449bd832023-01-11 14:50:10 +0100726 if (*sig_opts != NULL) {
727 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
728 }
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200729
Gilles Peskine449bd832023-01-11 14:50:10 +0100730 if ((ret = mbedtls_oid_get_sig_alg(sig_oid, md_alg, pk_alg)) != 0) {
731 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG, ret);
732 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200734#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100735 if (*pk_alg == MBEDTLS_PK_RSASSA_PSS) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200736 mbedtls_pk_rsassa_pss_options *pss_opts;
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100737
Gilles Peskine449bd832023-01-11 14:50:10 +0100738 pss_opts = mbedtls_calloc(1, sizeof(mbedtls_pk_rsassa_pss_options));
739 if (pss_opts == NULL) {
740 return MBEDTLS_ERR_X509_ALLOC_FAILED;
741 }
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200742
Gilles Peskine449bd832023-01-11 14:50:10 +0100743 ret = mbedtls_x509_get_rsassa_pss_params(sig_params,
744 md_alg,
745 &pss_opts->mgf1_hash_id,
746 &pss_opts->expected_salt_len);
747 if (ret != 0) {
748 mbedtls_free(pss_opts);
749 return ret;
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200750 }
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100751
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200752 *sig_opts = (void *) pss_opts;
Gilles Peskine449bd832023-01-11 14:50:10 +0100753 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200754#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100755 {
756 /* Make sure parameters are absent or NULL */
Gilles Peskine449bd832023-01-11 14:50:10 +0100757 if ((sig_params->tag != MBEDTLS_ASN1_NULL && sig_params->tag != 0) ||
758 sig_params->len != 0) {
759 return MBEDTLS_ERR_X509_INVALID_ALG;
760 }
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100761 }
762
Gilles Peskine449bd832023-01-11 14:50:10 +0100763 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200764}
765
766/*
767 * X.509 Extensions (No parsing of extensions, pointer should
Brian J Murray1903fb32016-11-06 04:45:15 -0800768 * be either manually updated or extensions should be parsed!)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200769 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100770int mbedtls_x509_get_ext(unsigned char **p, const unsigned char *end,
771 mbedtls_x509_buf *ext, int tag)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200772{
Janos Follath865b3eb2019-12-16 11:46:15 +0000773 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200774 size_t len;
775
Hanno Becker3cddba82019-02-11 14:33:36 +0000776 /* Extension structure use EXPLICIT tagging. That is, the actual
777 * `Extensions` structure is wrapped by a tag-length pair using
778 * the respective context-specific tag. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100779 ret = mbedtls_asn1_get_tag(p, end, &ext->len,
780 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | tag);
781 if (ret != 0) {
782 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
783 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200784
Hanno Becker6ccfb182019-02-12 11:52:10 +0000785 ext->tag = MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | tag;
786 ext->p = *p;
787 end = *p + ext->len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200788
789 /*
790 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200791 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100792 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
793 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
794 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
795 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200796
Gilles Peskine449bd832023-01-11 14:50:10 +0100797 if (end != *p + len) {
798 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
799 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
800 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200801
Gilles Peskine449bd832023-01-11 14:50:10 +0100802 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200803}
804
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200805/*
806 * Store the name in printable form into buf; no more
807 * than size characters will be written
808 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100809int mbedtls_x509_dn_gets(char *buf, size_t size, const mbedtls_x509_name *dn)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200810{
Janos Follath865b3eb2019-12-16 11:46:15 +0000811 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Werner Lewisb33dacd2022-05-20 12:48:46 +0100812 size_t i, j, n;
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000813 unsigned char c, merge = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200814 const mbedtls_x509_name *name;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200815 const char *short_name = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200816 char s[MBEDTLS_X509_MAX_DN_NAME_SIZE], *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200817
Gilles Peskine449bd832023-01-11 14:50:10 +0100818 memset(s, 0, sizeof(s));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200819
820 name = dn;
821 p = buf;
822 n = size;
823
Gilles Peskine449bd832023-01-11 14:50:10 +0100824 while (name != NULL) {
825 if (!name->oid.p) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200826 name = name->next;
827 continue;
828 }
829
Gilles Peskine449bd832023-01-11 14:50:10 +0100830 if (name != dn) {
831 ret = mbedtls_snprintf(p, n, merge ? " + " : ", ");
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200832 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200833 }
834
Gilles Peskine449bd832023-01-11 14:50:10 +0100835 ret = mbedtls_oid_get_attr_short_name(&name->oid, &short_name);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200836
Gilles Peskine449bd832023-01-11 14:50:10 +0100837 if (ret == 0) {
838 ret = mbedtls_snprintf(p, n, "%s=", short_name);
839 } else {
840 ret = mbedtls_snprintf(p, n, "\?\?=");
841 }
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200842 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200843
Gilles Peskine449bd832023-01-11 14:50:10 +0100844 for (i = 0, j = 0; i < name->val.len; i++, j++) {
845 if (j >= sizeof(s) - 1) {
846 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
847 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200848
849 c = name->val.p[i];
Werner Lewisb33dacd2022-05-20 12:48:46 +0100850 // Special characters requiring escaping, RFC 1779
Gilles Peskine449bd832023-01-11 14:50:10 +0100851 if (c && strchr(",=+<>#;\"\\", c)) {
852 if (j + 1 >= sizeof(s) - 1) {
853 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
854 }
Werner Lewisb33dacd2022-05-20 12:48:46 +0100855 s[j++] = '\\';
856 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100857 if (c < 32 || c >= 127) {
858 s[j] = '?';
859 } else {
860 s[j] = c;
861 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200862 }
Werner Lewisb33dacd2022-05-20 12:48:46 +0100863 s[j] = '\0';
Gilles Peskine449bd832023-01-11 14:50:10 +0100864 ret = mbedtls_snprintf(p, n, "%s", s);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200865 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000866
867 merge = name->next_merged;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200868 name = name->next;
869 }
870
Gilles Peskine449bd832023-01-11 14:50:10 +0100871 return (int) (size - n);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200872}
873
874/*
875 * Store the serial in printable form into buf; no more
876 * than size characters will be written
877 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100878int mbedtls_x509_serial_gets(char *buf, size_t size, const mbedtls_x509_buf *serial)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200879{
Janos Follath865b3eb2019-12-16 11:46:15 +0000880 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200881 size_t i, n, nr;
882 char *p;
883
884 p = buf;
885 n = size;
886
Gilles Peskine449bd832023-01-11 14:50:10 +0100887 nr = (serial->len <= 32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200888 ? serial->len : 28;
889
Gilles Peskine449bd832023-01-11 14:50:10 +0100890 for (i = 0; i < nr; i++) {
891 if (i == 0 && nr > 1 && serial->p[i] == 0x0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200892 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100893 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200894
Gilles Peskine449bd832023-01-11 14:50:10 +0100895 ret = mbedtls_snprintf(p, n, "%02X%s",
896 serial->p[i], (i < nr - 1) ? ":" : "");
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200897 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200898 }
899
Gilles Peskine449bd832023-01-11 14:50:10 +0100900 if (nr != serial->len) {
901 ret = mbedtls_snprintf(p, n, "....");
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200902 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200903 }
904
Gilles Peskine449bd832023-01-11 14:50:10 +0100905 return (int) (size - n);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200906}
907
Hanno Becker612a2f12020-10-09 09:19:39 +0100908#if !defined(MBEDTLS_X509_REMOVE_INFO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200909/*
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200910 * Helper for writing signature algorithms
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100911 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100912int mbedtls_x509_sig_alg_gets(char *buf, size_t size, const mbedtls_x509_buf *sig_oid,
913 mbedtls_pk_type_t pk_alg, mbedtls_md_type_t md_alg,
914 const void *sig_opts)
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100915{
Janos Follath865b3eb2019-12-16 11:46:15 +0000916 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100917 char *p = buf;
918 size_t n = size;
919 const char *desc = NULL;
920
Gilles Peskine449bd832023-01-11 14:50:10 +0100921 ret = mbedtls_oid_get_sig_alg_desc(sig_oid, &desc);
922 if (ret != 0) {
923 ret = mbedtls_snprintf(p, n, "???");
924 } else {
925 ret = mbedtls_snprintf(p, n, "%s", desc);
926 }
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200927 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100928
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200929#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100930 if (pk_alg == MBEDTLS_PK_RSASSA_PSS) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200931 const mbedtls_pk_rsassa_pss_options *pss_opts;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100932
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200933 pss_opts = (const mbedtls_pk_rsassa_pss_options *) sig_opts;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100934
Gilles Peskine449bd832023-01-11 14:50:10 +0100935 const char *name = md_type_to_string(md_alg);
936 const char *mgf_name = md_type_to_string(pss_opts->mgf1_hash_id);
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100937
Gilles Peskine449bd832023-01-11 14:50:10 +0100938 ret = mbedtls_snprintf(p, n, " (%s, MGF1-%s, 0x%02X)",
939 name ? name : "???",
940 mgf_name ? mgf_name : "???",
941 (unsigned int) pss_opts->expected_salt_len);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200942 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100943 }
944#else
945 ((void) pk_alg);
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200946 ((void) md_alg);
947 ((void) sig_opts);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200948#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100949
Gilles Peskine449bd832023-01-11 14:50:10 +0100950 return (int) (size - n);
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100951}
Hanno Becker612a2f12020-10-09 09:19:39 +0100952#endif /* MBEDTLS_X509_REMOVE_INFO */
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100953
954/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200955 * Helper for writing "RSA key size", "EC key size", etc
956 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100957int mbedtls_x509_key_size_helper(char *buf, size_t buf_size, const char *name)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200958{
959 char *p = buf;
Manuel Pégourié-Gonnardfb317c52015-06-18 16:25:56 +0200960 size_t n = buf_size;
Janos Follath865b3eb2019-12-16 11:46:15 +0000961 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200962
Gilles Peskine449bd832023-01-11 14:50:10 +0100963 ret = mbedtls_snprintf(p, n, "%s key size", name);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200964 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200965
Gilles Peskine449bd832023-01-11 14:50:10 +0100966 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200967}
968
Glenn Strauss416dc032022-06-30 00:38:53 -0400969int mbedtls_x509_time_cmp(const mbedtls_x509_time *t1,
970 const mbedtls_x509_time *t2)
971{
Glenn Strauss5aef2972022-06-30 04:38:02 -0400972 int x;
973
974 x = (((t1->year << 9) | (t1->mon << 5) | (t1->day)) -
975 ((t2->year << 9) | (t2->mon << 5) | (t2->day)));
976 if (x != 0) {
977 return x;
Glenn Strauss416dc032022-06-30 00:38:53 -0400978 }
979
Glenn Strauss5aef2972022-06-30 04:38:02 -0400980 x = (((t1->hour << 12) | (t1->min << 6) | (t1->sec)) -
981 ((t2->hour << 12) | (t2->min << 6) | (t2->sec)));
982 return x;
Glenn Strauss416dc032022-06-30 00:38:53 -0400983}
984
Manuel Pégourié-Gonnard60c793b2015-06-18 20:52:58 +0200985#if defined(MBEDTLS_HAVE_TIME_DATE)
Glenn Strauss61d99302022-06-30 05:25:56 -0400986int mbedtls_x509_time_gmtime(mbedtls_time_t tt, mbedtls_x509_time *now)
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100987{
Glenn Strauss811eeb22022-06-30 05:28:50 -0400988 struct tm tm;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200989
Glenn Strauss811eeb22022-06-30 05:28:50 -0400990 if (mbedtls_platform_gmtime_r(&tt, &tm) == NULL) {
991 return -1;
Manuel Pégourié-Gonnard57e10d72015-06-22 18:59:21 +0200992 }
Manuel Pégourié-Gonnard864108d2015-05-29 10:11:03 +0200993
Glenn Strauss811eeb22022-06-30 05:28:50 -0400994 now->year = tm.tm_year + 1900;
995 now->mon = tm.tm_mon + 1;
996 now->day = tm.tm_mday;
997 now->hour = tm.tm_hour;
998 now->min = tm.tm_min;
999 now->sec = tm.tm_sec;
1000 return 0;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001001}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001002
Glenn Strauss61d99302022-06-30 05:25:56 -04001003static int x509_get_current_time(mbedtls_x509_time *now)
1004{
1005 return mbedtls_x509_time_gmtime(mbedtls_time(NULL), now);
1006}
1007
Gilles Peskine449bd832023-01-11 14:50:10 +01001008int mbedtls_x509_time_is_past(const mbedtls_x509_time *to)
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001009{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001010 mbedtls_x509_time now;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001011
Gilles Peskine449bd832023-01-11 14:50:10 +01001012 if (x509_get_current_time(&now) != 0) {
1013 return 1;
1014 }
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001015
Glenn Strauss416dc032022-06-30 00:38:53 -04001016 return mbedtls_x509_time_cmp(to, &now) < 0;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001017}
1018
Gilles Peskine449bd832023-01-11 14:50:10 +01001019int mbedtls_x509_time_is_future(const mbedtls_x509_time *from)
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001020{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001021 mbedtls_x509_time now;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001022
Gilles Peskine449bd832023-01-11 14:50:10 +01001023 if (x509_get_current_time(&now) != 0) {
1024 return 1;
1025 }
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001026
Glenn Strauss416dc032022-06-30 00:38:53 -04001027 return mbedtls_x509_time_cmp(from, &now) > 0;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001028}
1029
Manuel Pégourié-Gonnard60c793b2015-06-18 20:52:58 +02001030#else /* MBEDTLS_HAVE_TIME_DATE */
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001031
Gilles Peskine449bd832023-01-11 14:50:10 +01001032int mbedtls_x509_time_is_past(const mbedtls_x509_time *to)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001033{
1034 ((void) to);
Gilles Peskine449bd832023-01-11 14:50:10 +01001035 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001036}
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001037
Gilles Peskine449bd832023-01-11 14:50:10 +01001038int mbedtls_x509_time_is_future(const mbedtls_x509_time *from)
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001039{
1040 ((void) from);
Gilles Peskine449bd832023-01-11 14:50:10 +01001041 return 0;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001042}
Manuel Pégourié-Gonnard60c793b2015-06-18 20:52:58 +02001043#endif /* MBEDTLS_HAVE_TIME_DATE */
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001044
1045/* Common functions for parsing CRT and CSR. */
1046#if defined(MBEDTLS_X509_CRT_PARSE_C) || defined(MBEDTLS_X509_CSR_PARSE_C)
1047/*
1048 * OtherName ::= SEQUENCE {
1049 * type-id OBJECT IDENTIFIER,
1050 * value [0] EXPLICIT ANY DEFINED BY type-id }
1051 *
1052 * HardwareModuleName ::= SEQUENCE {
1053 * hwType OBJECT IDENTIFIER,
1054 * hwSerialNum OCTET STRING }
1055 *
1056 * NOTE: we currently only parse and use otherName of type HwModuleName,
1057 * as defined in RFC 4108.
1058 */
1059static int x509_get_other_name(const mbedtls_x509_buf *subject_alt_name,
1060 mbedtls_x509_san_other_name *other_name)
1061{
1062 int ret = 0;
1063 size_t len;
1064 unsigned char *p = subject_alt_name->p;
1065 const unsigned char *end = p + subject_alt_name->len;
1066 mbedtls_x509_buf cur_oid;
1067
1068 if ((subject_alt_name->tag &
1069 (MBEDTLS_ASN1_TAG_CLASS_MASK | MBEDTLS_ASN1_TAG_VALUE_MASK)) !=
1070 (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME)) {
1071 /*
1072 * The given subject alternative name is not of type "othername".
1073 */
1074 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1075 }
1076
1077 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1078 MBEDTLS_ASN1_OID)) != 0) {
1079 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1080 }
1081
1082 cur_oid.tag = MBEDTLS_ASN1_OID;
1083 cur_oid.p = p;
1084 cur_oid.len = len;
1085
1086 /*
1087 * Only HwModuleName is currently supported.
1088 */
1089 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ON_HW_MODULE_NAME, &cur_oid) != 0) {
1090 return MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
1091 }
1092
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001093 p += len;
1094 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1095 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC)) !=
1096 0) {
1097 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1098 }
1099
Hanno Beckerdae916b2019-09-13 14:21:13 +01001100 if (end != p + len) {
1101 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1102 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1103 }
1104
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001105 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1106 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1107 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1108 }
1109
Hanno Beckerdae916b2019-09-13 14:21:13 +01001110 if (end != p + len) {
1111 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1112 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1113 }
1114
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001115 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OID)) != 0) {
1116 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1117 }
1118
1119 other_name->value.hardware_module_name.oid.tag = MBEDTLS_ASN1_OID;
1120 other_name->value.hardware_module_name.oid.p = p;
1121 other_name->value.hardware_module_name.oid.len = len;
1122
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001123 p += len;
1124 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1125 MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1126 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1127 }
1128
1129 other_name->value.hardware_module_name.val.tag = MBEDTLS_ASN1_OCTET_STRING;
1130 other_name->value.hardware_module_name.val.p = p;
1131 other_name->value.hardware_module_name.val.len = len;
1132 p += len;
1133 if (p != end) {
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001134 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1135 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1136 }
1137 return 0;
1138}
1139
Przemek Stekiel21903ec2023-02-21 08:32:37 +01001140/* Check mbedtls_x509_get_subject_alt_name for detailed description.
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001141 *
Przemek Stekiel21903ec2023-02-21 08:32:37 +01001142 * In some cases while parsing subject alternative names the sequence tag is optional
1143 * (e.g. CertSerialNumber). This function is designed to handle such case.
Przemek Stekiel725688b2023-04-04 22:49:44 +02001144 */
Przemek Stekiel21903ec2023-02-21 08:32:37 +01001145int mbedtls_x509_get_subject_alt_name_ext(unsigned char **p,
1146 const unsigned char *end,
1147 mbedtls_x509_sequence *subject_alt_name)
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001148{
1149 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel21903ec2023-02-21 08:32:37 +01001150 size_t tag_len;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001151 mbedtls_asn1_sequence *cur = subject_alt_name;
1152
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001153 while (*p < end) {
Andrzej Kurek154a6052023-04-30 14:11:49 -04001154 mbedtls_x509_subject_alternative_name tmp_san_name;
Hanno Beckerae8f8c42019-09-13 12:24:56 +01001155 mbedtls_x509_buf tmp_san_buf;
Andrzej Kurek154a6052023-04-30 14:11:49 -04001156 memset(&tmp_san_name, 0, sizeof(tmp_san_name));
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001157
Hanno Beckerae8f8c42019-09-13 12:24:56 +01001158 tmp_san_buf.tag = **p;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001159 (*p)++;
Hanno Beckerae8f8c42019-09-13 12:24:56 +01001160
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001161 if ((ret = mbedtls_asn1_get_len(p, end, &tag_len)) != 0) {
1162 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1163 }
1164
Hanno Beckerae8f8c42019-09-13 12:24:56 +01001165 tmp_san_buf.p = *p;
1166 tmp_san_buf.len = tag_len;
1167
1168 if ((tmp_san_buf.tag & MBEDTLS_ASN1_TAG_CLASS_MASK) !=
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001169 MBEDTLS_ASN1_CONTEXT_SPECIFIC) {
1170 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1171 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
1172 }
1173
1174 /*
Andrzej Kurek154a6052023-04-30 14:11:49 -04001175 * Check that the SAN is structured correctly by parsing it.
1176 * The SAN structure is discarded afterwards.
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001177 */
Andrzej Kurek154a6052023-04-30 14:11:49 -04001178 ret = mbedtls_x509_parse_subject_alt_name(&tmp_san_buf, &tmp_san_name);
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001179 /*
1180 * In case the extension is malformed, return an error,
1181 * and clear the allocated sequences.
1182 */
1183 if (ret != 0 && ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE) {
1184 mbedtls_asn1_sequence_free(subject_alt_name->next);
1185 subject_alt_name->next = NULL;
1186 return ret;
1187 }
1188
Andrzej Kurek154a6052023-04-30 14:11:49 -04001189 mbedtls_x509_free_subject_alt_name(&tmp_san_name);
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001190 /* Allocate and assign next pointer */
1191 if (cur->buf.p != NULL) {
1192 if (cur->next != NULL) {
1193 return MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
1194 }
1195
1196 cur->next = mbedtls_calloc(1, sizeof(mbedtls_asn1_sequence));
1197
1198 if (cur->next == NULL) {
1199 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1200 MBEDTLS_ERR_ASN1_ALLOC_FAILED);
1201 }
1202
1203 cur = cur->next;
1204 }
1205
Hanno Beckerae8f8c42019-09-13 12:24:56 +01001206 cur->buf = tmp_san_buf;
1207 *p += tmp_san_buf.len;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001208 }
1209
1210 /* Set final sequence entry's next pointer to NULL */
1211 cur->next = NULL;
1212
1213 if (*p != end) {
1214 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1215 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1216 }
1217
1218 return 0;
1219}
1220
Przemek Stekiel21903ec2023-02-21 08:32:37 +01001221/*
1222 * SubjectAltName ::= GeneralNames
1223 *
1224 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
1225 *
1226 * GeneralName ::= CHOICE {
1227 * otherName [0] OtherName,
1228 * rfc822Name [1] IA5String,
1229 * dNSName [2] IA5String,
1230 * x400Address [3] ORAddress,
1231 * directoryName [4] Name,
1232 * ediPartyName [5] EDIPartyName,
1233 * uniformResourceIdentifier [6] IA5String,
1234 * iPAddress [7] OCTET STRING,
1235 * registeredID [8] OBJECT IDENTIFIER }
1236 *
1237 * OtherName ::= SEQUENCE {
1238 * type-id OBJECT IDENTIFIER,
1239 * value [0] EXPLICIT ANY DEFINED BY type-id }
1240 *
1241 * EDIPartyName ::= SEQUENCE {
1242 * nameAssigner [0] DirectoryString OPTIONAL,
1243 * partyName [1] DirectoryString }
1244 *
1245 * We list all types, but use the following GeneralName types from RFC 5280:
1246 * "dnsName", "uniformResourceIdentifier" and "hardware_module_name"
1247 * of type "otherName", as defined in RFC 4108.
1248 */
1249int mbedtls_x509_get_subject_alt_name(unsigned char **p,
1250 const unsigned char *end,
1251 mbedtls_x509_sequence *subject_alt_name)
1252{
1253 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1254 size_t len;
1255
1256 /* Get main sequence tag */
1257 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
1258 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1259 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1260 }
1261
1262 if (*p + len != end) {
1263 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1264 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1265 }
1266
1267 return mbedtls_x509_get_subject_alt_name_ext(p, end, subject_alt_name);
1268}
1269
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001270int mbedtls_x509_get_ns_cert_type(unsigned char **p,
1271 const unsigned char *end,
1272 unsigned char *ns_cert_type)
1273{
1274 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1275 mbedtls_x509_bitstring bs = { 0, 0, NULL };
1276
1277 if ((ret = mbedtls_asn1_get_bitstring(p, end, &bs)) != 0) {
1278 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1279 }
1280
Przemek Stekiel32e20912023-01-25 14:26:15 +01001281 /* A bitstring with no flags set is still technically valid, as it will mean
1282 that the certificate has no designated purpose at the time of creation. */
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001283 if (bs.len == 0) {
1284 *ns_cert_type = 0;
1285 return 0;
1286 }
1287
1288 if (bs.len != 1) {
1289 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1290 MBEDTLS_ERR_ASN1_INVALID_LENGTH);
1291 }
1292
1293 /* Get actual bitstring */
1294 *ns_cert_type = *bs.p;
1295 return 0;
1296}
1297
1298int mbedtls_x509_get_key_usage(unsigned char **p,
1299 const unsigned char *end,
1300 unsigned int *key_usage)
1301{
1302 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1303 size_t i;
1304 mbedtls_x509_bitstring bs = { 0, 0, NULL };
1305
1306 if ((ret = mbedtls_asn1_get_bitstring(p, end, &bs)) != 0) {
1307 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1308 }
1309
Przemek Stekiel32e20912023-01-25 14:26:15 +01001310 /* A bitstring with no flags set is still technically valid, as it will mean
1311 that the certificate has no designated purpose at the time of creation. */
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001312 if (bs.len == 0) {
1313 *key_usage = 0;
1314 return 0;
1315 }
1316
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001317 /* Get actual bitstring */
1318 *key_usage = 0;
1319 for (i = 0; i < bs.len && i < sizeof(unsigned int); i++) {
1320 *key_usage |= (unsigned int) bs.p[i] << (8*i);
1321 }
1322
1323 return 0;
1324}
1325
1326int mbedtls_x509_parse_subject_alt_name(const mbedtls_x509_buf *san_buf,
1327 mbedtls_x509_subject_alternative_name *san)
1328{
1329 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1330 switch (san_buf->tag &
1331 (MBEDTLS_ASN1_TAG_CLASS_MASK |
1332 MBEDTLS_ASN1_TAG_VALUE_MASK)) {
1333 /*
1334 * otherName
1335 */
1336 case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME):
1337 {
1338 mbedtls_x509_san_other_name other_name;
1339
1340 ret = x509_get_other_name(san_buf, &other_name);
1341 if (ret != 0) {
1342 return ret;
1343 }
1344
1345 memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name));
1346 san->type = MBEDTLS_X509_SAN_OTHER_NAME;
1347 memcpy(&san->san.other_name,
1348 &other_name, sizeof(other_name));
1349
1350 }
1351 break;
Andrzej Kurek7a05fab2023-02-13 10:03:07 -05001352 /*
1353 * uniformResourceIdentifier
1354 */
1355 case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER):
1356 {
1357 memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name));
1358 san->type = MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001359
Andrzej Kurek7a05fab2023-02-13 10:03:07 -05001360 memcpy(&san->san.unstructured_name,
1361 san_buf, sizeof(*san_buf));
1362
1363 }
1364 break;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001365 /*
1366 * dNSName
1367 */
1368 case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_DNS_NAME):
1369 {
1370 memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name));
1371 san->type = MBEDTLS_X509_SAN_DNS_NAME;
1372
1373 memcpy(&san->san.unstructured_name,
1374 san_buf, sizeof(*san_buf));
Przemek Stekielecee12f2023-02-09 14:43:49 +01001375 }
1376 break;
Przemek Stekiel0ab5b932023-05-29 16:30:50 +02001377 /*
1378 * IP address
1379 */
1380 case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_IP_ADDRESS):
1381 {
1382 memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name));
1383 san->type = MBEDTLS_X509_SAN_IP_ADDRESS;
Przemek Stekiel01cb6eb2023-06-05 16:38:13 +02001384 // Only IPv6 (16 bytes) and IPv4 (4 bytes) types are supported
1385 if (san_buf->len == 4 || san_buf->len == 16) {
1386 memcpy(&san->san.unstructured_name,
1387 san_buf, sizeof(*san_buf));
1388 } else {
1389 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1390 }
Przemek Stekiel0ab5b932023-05-29 16:30:50 +02001391 }
1392 break;
Przemek Stekielecee12f2023-02-09 14:43:49 +01001393 /*
Andrzej Kurek154a6052023-04-30 14:11:49 -04001394 * rfc822Name
Przemek Stekielecee12f2023-02-09 14:43:49 +01001395 */
1396 case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_RFC822_NAME):
1397 {
1398 memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name));
1399 san->type = MBEDTLS_X509_SAN_RFC822_NAME;
1400 memcpy(&san->san.unstructured_name, san_buf, sizeof(*san_buf));
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001401 }
1402 break;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001403 /*
Andrzej Kureke12b01d2023-01-10 06:47:38 -05001404 * directoryName
1405 */
1406 case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_DIRECTORY_NAME):
1407 {
1408 size_t name_len;
1409 unsigned char *p = san_buf->p;
1410 memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name));
1411 san->type = MBEDTLS_X509_SAN_DIRECTORY_NAME;
1412
1413 ret = mbedtls_asn1_get_tag(&p, p + san_buf->len, &name_len,
1414 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE);
1415
1416 if (ret != 0) {
Andrzej Kurekbf8ccd82023-02-13 07:13:30 -05001417 return ret;
Andrzej Kureke12b01d2023-01-10 06:47:38 -05001418 }
1419
1420 if ((ret = mbedtls_x509_get_name(&p, p + name_len,
1421 &san->san.directory_name)) != 0) {
Andrzej Kurekbf8ccd82023-02-13 07:13:30 -05001422 return ret;
Andrzej Kureke12b01d2023-01-10 06:47:38 -05001423 }
1424 }
1425 break;
1426 /*
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001427 * Type not supported
1428 */
1429 default:
1430 return MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
1431 }
1432 return 0;
1433}
1434
Andrzej Kurekd40c2b62023-02-13 07:01:59 -05001435void mbedtls_x509_free_subject_alt_name(mbedtls_x509_subject_alternative_name *san)
1436{
1437 if (san->type == MBEDTLS_X509_SAN_DIRECTORY_NAME) {
1438 mbedtls_asn1_free_named_data_list_shallow(san->san.directory_name.next);
1439 }
1440}
1441
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001442#if !defined(MBEDTLS_X509_REMOVE_INFO)
1443int mbedtls_x509_info_subject_alt_name(char **buf, size_t *size,
1444 const mbedtls_x509_sequence
1445 *subject_alt_name,
1446 const char *prefix)
1447{
1448 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1449 size_t i;
1450 size_t n = *size;
1451 char *p = *buf;
1452 const mbedtls_x509_sequence *cur = subject_alt_name;
1453 mbedtls_x509_subject_alternative_name san;
1454 int parse_ret;
1455
1456 while (cur != NULL) {
1457 memset(&san, 0, sizeof(san));
1458 parse_ret = mbedtls_x509_parse_subject_alt_name(&cur->buf, &san);
1459 if (parse_ret != 0) {
1460 if (parse_ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE) {
1461 ret = mbedtls_snprintf(p, n, "\n%s <unsupported>", prefix);
1462 MBEDTLS_X509_SAFE_SNPRINTF;
1463 } else {
1464 ret = mbedtls_snprintf(p, n, "\n%s <malformed>", prefix);
1465 MBEDTLS_X509_SAFE_SNPRINTF;
1466 }
1467 cur = cur->next;
1468 continue;
1469 }
1470
1471 switch (san.type) {
1472 /*
1473 * otherName
1474 */
1475 case MBEDTLS_X509_SAN_OTHER_NAME:
1476 {
1477 mbedtls_x509_san_other_name *other_name = &san.san.other_name;
1478
1479 ret = mbedtls_snprintf(p, n, "\n%s otherName :", prefix);
1480 MBEDTLS_X509_SAFE_SNPRINTF;
1481
1482 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ON_HW_MODULE_NAME,
1483 &other_name->value.hardware_module_name.oid) != 0) {
1484 ret = mbedtls_snprintf(p, n, "\n%s hardware module name :", prefix);
1485 MBEDTLS_X509_SAFE_SNPRINTF;
1486 ret =
1487 mbedtls_snprintf(p, n, "\n%s hardware type : ", prefix);
1488 MBEDTLS_X509_SAFE_SNPRINTF;
1489
1490 ret = mbedtls_oid_get_numeric_string(p,
1491 n,
1492 &other_name->value.hardware_module_name.oid);
1493 MBEDTLS_X509_SAFE_SNPRINTF;
1494
1495 ret =
1496 mbedtls_snprintf(p, n, "\n%s hardware serial number : ", prefix);
1497 MBEDTLS_X509_SAFE_SNPRINTF;
1498
1499 for (i = 0; i < other_name->value.hardware_module_name.val.len; i++) {
1500 ret = mbedtls_snprintf(p,
1501 n,
1502 "%02X",
1503 other_name->value.hardware_module_name.val.p[i]);
1504 MBEDTLS_X509_SAFE_SNPRINTF;
1505 }
1506 }/* MBEDTLS_OID_ON_HW_MODULE_NAME */
1507 }
1508 break;
Andrzej Kurek7a05fab2023-02-13 10:03:07 -05001509 /*
1510 * uniformResourceIdentifier
1511 */
1512 case MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER:
1513 {
1514 ret = mbedtls_snprintf(p, n, "\n%s uniformResourceIdentifier : ", prefix);
1515 MBEDTLS_X509_SAFE_SNPRINTF;
1516 if (san.san.unstructured_name.len >= n) {
Przemek Stekiel01cb6eb2023-06-05 16:38:13 +02001517 if (n > 0) {
1518 *p = '\0';
1519 }
Andrzej Kurek7a05fab2023-02-13 10:03:07 -05001520 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1521 }
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001522
Andrzej Kurek7a05fab2023-02-13 10:03:07 -05001523 memcpy(p, san.san.unstructured_name.p, san.san.unstructured_name.len);
1524 p += san.san.unstructured_name.len;
1525 n -= san.san.unstructured_name.len;
1526 }
1527 break;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001528 /*
1529 * dNSName
Przemek Stekiel5b9e4162023-02-15 12:56:37 +01001530 * RFC822 Name
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001531 */
1532 case MBEDTLS_X509_SAN_DNS_NAME:
Przemek Stekiel5b9e4162023-02-15 12:56:37 +01001533 case MBEDTLS_X509_SAN_RFC822_NAME:
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001534 {
Przemek Stekiel82d250d2023-02-15 15:00:50 +01001535 const char *dns_name = "dNSName";
1536 const char *rfc822_name = "rfc822Name";
Przemek Stekiel5b9e4162023-02-15 12:56:37 +01001537
Przemek Stekiel82d250d2023-02-15 15:00:50 +01001538 ret = mbedtls_snprintf(p, n,
1539 "\n%s %s : ",
1540 prefix,
Przemek Stekiel093c97d2023-06-02 10:11:32 +02001541 san.type ==
1542 MBEDTLS_X509_SAN_DNS_NAME ? dns_name : rfc822_name);
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001543 MBEDTLS_X509_SAFE_SNPRINTF;
1544 if (san.san.unstructured_name.len >= n) {
Przemek Stekiel01cb6eb2023-06-05 16:38:13 +02001545 if (n > 0) {
1546 *p = '\0';
1547 }
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001548 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1549 }
1550
Przemek Stekiel093c97d2023-06-02 10:11:32 +02001551 memcpy(p, san.san.unstructured_name.p, san.san.unstructured_name.len);
1552 p += san.san.unstructured_name.len;
1553 n -= san.san.unstructured_name.len;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001554 }
1555 break;
Przemek Stekiel093c97d2023-06-02 10:11:32 +02001556 /*
1557 * iPAddress
1558 */
1559 case MBEDTLS_X509_SAN_IP_ADDRESS:
1560 {
1561 ret = mbedtls_snprintf(p, n, "\n%s %s : ",
1562 prefix, "iPAddress");
1563 MBEDTLS_X509_SAFE_SNPRINTF;
1564 if (san.san.unstructured_name.len >= n) {
Przemek Stekiel01cb6eb2023-06-05 16:38:13 +02001565 if (n > 0) {
1566 *p = '\0';
1567 }
Przemek Stekiel093c97d2023-06-02 10:11:32 +02001568 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1569 }
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001570
Przemek Stekiel093c97d2023-06-02 10:11:32 +02001571 unsigned char *ip = san.san.unstructured_name.p;
1572 // Only IPv6 (16 bytes) and IPv4 (4 bytes) types are supported
1573 if (san.san.unstructured_name.len == 4) {
Przemek Stekiel4d3fc212023-06-06 11:40:32 +02001574 ret = mbedtls_snprintf(p, n, "%u.%u.%u.%u", ip[0], ip[1], ip[2], ip[3]);
1575 MBEDTLS_X509_SAFE_SNPRINTF;
Przemek Stekiel093c97d2023-06-02 10:11:32 +02001576 } else if (san.san.unstructured_name.len == 16) {
Przemek Stekiel4d3fc212023-06-06 11:40:32 +02001577 ret = mbedtls_snprintf(p, n,
Przemek Stekiel01cb6eb2023-06-05 16:38:13 +02001578 "%X%X:%X%X:%X%X:%X%X:%X%X:%X%X:%X%X:%X%X",
1579 ip[0], ip[1], ip[2], ip[3], ip[4], ip[5], ip[6],
Przemek Stekiel4d3fc212023-06-06 11:40:32 +02001580 ip[7], ip[8], ip[9], ip[10], ip[11], ip[12], ip[13],
Przemek Stekiel01cb6eb2023-06-05 16:38:13 +02001581 ip[14], ip[15]);
Przemek Stekiel4d3fc212023-06-06 11:40:32 +02001582 MBEDTLS_X509_SAFE_SNPRINTF;
Przemek Stekiel093c97d2023-06-02 10:11:32 +02001583 } else {
Przemek Stekiel01cb6eb2023-06-05 16:38:13 +02001584 if (n > 0) {
1585 *p = '\0';
1586 }
Przemek Stekiel093c97d2023-06-02 10:11:32 +02001587 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1588 }
Przemek Stekiel093c97d2023-06-02 10:11:32 +02001589 }
1590 break;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001591 /*
Andrzej Kureke12b01d2023-01-10 06:47:38 -05001592 * directoryName
1593 */
1594 case MBEDTLS_X509_SAN_DIRECTORY_NAME:
1595 {
1596 ret = mbedtls_snprintf(p, n, "\n%s directoryName : ", prefix);
Andrzej Kurek5f0c6e82023-02-27 16:03:41 -05001597 if (ret < 0 || (size_t) ret >= n) {
1598 mbedtls_x509_free_subject_alt_name(&san);
1599 }
1600
Andrzej Kureke12b01d2023-01-10 06:47:38 -05001601 MBEDTLS_X509_SAFE_SNPRINTF;
1602 ret = mbedtls_x509_dn_gets(p, n, &san.san.directory_name);
Andrzej Kurekd40c2b62023-02-13 07:01:59 -05001603
Andrzej Kureke12b01d2023-01-10 06:47:38 -05001604 if (ret < 0) {
Andrzej Kurek5f0c6e82023-02-27 16:03:41 -05001605 mbedtls_x509_free_subject_alt_name(&san);
Przemek Stekiel01cb6eb2023-06-05 16:38:13 +02001606 if (n > 0) {
1607 *p = '\0';
1608 }
Andrzej Kureke12b01d2023-01-10 06:47:38 -05001609 return ret;
1610 }
1611
1612 p += ret;
1613 n -= ret;
1614 }
1615 break;
1616 /*
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001617 * Type not supported, skip item.
1618 */
1619 default:
1620 ret = mbedtls_snprintf(p, n, "\n%s <unsupported>", prefix);
1621 MBEDTLS_X509_SAFE_SNPRINTF;
1622 break;
1623 }
1624
Andrzej Kurekd40c2b62023-02-13 07:01:59 -05001625 /* So far memory is freed only in the case of directoryName
Andrzej Kurek5f0c6e82023-02-27 16:03:41 -05001626 * parsing succeeding, as mbedtls_x509_get_name allocates memory. */
Andrzej Kurekd40c2b62023-02-13 07:01:59 -05001627 mbedtls_x509_free_subject_alt_name(&san);
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001628 cur = cur->next;
1629 }
1630
1631 *p = '\0';
1632
1633 *size = n;
1634 *buf = p;
1635
1636 return 0;
1637}
1638
1639#define PRINT_ITEM(i) \
1640 { \
1641 ret = mbedtls_snprintf(p, n, "%s" i, sep); \
1642 MBEDTLS_X509_SAFE_SNPRINTF; \
1643 sep = ", "; \
1644 }
1645
1646#define CERT_TYPE(type, name) \
1647 if (ns_cert_type & (type)) \
1648 PRINT_ITEM(name);
1649
1650int mbedtls_x509_info_cert_type(char **buf, size_t *size,
1651 unsigned char ns_cert_type)
1652{
1653 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1654 size_t n = *size;
1655 char *p = *buf;
1656 const char *sep = "";
1657
1658 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT, "SSL Client");
1659 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER, "SSL Server");
1660 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_EMAIL, "Email");
1661 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing");
1662 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_RESERVED, "Reserved");
1663 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_SSL_CA, "SSL CA");
1664 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA, "Email CA");
1665 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA");
1666
1667 *size = n;
1668 *buf = p;
1669
1670 return 0;
1671}
1672
1673#define KEY_USAGE(code, name) \
1674 if (key_usage & (code)) \
1675 PRINT_ITEM(name);
1676
1677int mbedtls_x509_info_key_usage(char **buf, size_t *size,
1678 unsigned int key_usage)
1679{
1680 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1681 size_t n = *size;
1682 char *p = *buf;
1683 const char *sep = "";
1684
1685 KEY_USAGE(MBEDTLS_X509_KU_DIGITAL_SIGNATURE, "Digital Signature");
1686 KEY_USAGE(MBEDTLS_X509_KU_NON_REPUDIATION, "Non Repudiation");
1687 KEY_USAGE(MBEDTLS_X509_KU_KEY_ENCIPHERMENT, "Key Encipherment");
1688 KEY_USAGE(MBEDTLS_X509_KU_DATA_ENCIPHERMENT, "Data Encipherment");
1689 KEY_USAGE(MBEDTLS_X509_KU_KEY_AGREEMENT, "Key Agreement");
1690 KEY_USAGE(MBEDTLS_X509_KU_KEY_CERT_SIGN, "Key Cert Sign");
1691 KEY_USAGE(MBEDTLS_X509_KU_CRL_SIGN, "CRL Sign");
1692 KEY_USAGE(MBEDTLS_X509_KU_ENCIPHER_ONLY, "Encipher Only");
1693 KEY_USAGE(MBEDTLS_X509_KU_DECIPHER_ONLY, "Decipher Only");
1694
1695 *size = n;
1696 *buf = p;
1697
1698 return 0;
1699}
1700#endif /* MBEDTLS_X509_REMOVE_INFO */
1701#endif /* MBEDTLS_X509_CRT_PARSE_C || MBEDTLS_X509_CSR_PARSE_C */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001702#endif /* MBEDTLS_X509_USE_C */