blob: 54275ebce0c46284ce01215630a9b781212ae838 [file] [log] [blame]
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001/*
Manuel Pégourié-Gonnard1c082f32014-06-12 22:34:55 +02002 * X.509 common functions for parsing and verification
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker7c6b2c32013-09-16 13:49:26 +02006 */
7/*
8 * The ITU-T X.509 standard defines a certificate format for PKI.
9 *
Manuel Pégourié-Gonnard1c082f32014-06-12 22:34:55 +020010 * http://www.ietf.org/rfc/rfc5280.txt (Certificates and CRLs)
11 * http://www.ietf.org/rfc/rfc3279.txt (Alg IDs for CRLs)
12 * http://www.ietf.org/rfc/rfc2986.txt (CSRs, aka PKCS#10)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020013 *
14 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
15 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
16 */
17
Harry Ramsey0f6bc412024-10-04 10:36:54 +010018#include "x509_internal.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020019
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020020#if defined(MBEDTLS_X509_USE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020021
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000022#include "mbedtls/asn1.h"
Janos Follath73c616b2019-12-18 15:07:04 +000023#include "mbedtls/error.h"
Gilles Peskine86a47f82025-05-07 20:20:12 +020024#include "x509_oid.h"
Rich Evans00ab4702015-02-06 13:43:58 +000025
Rich Evans36796df2015-02-12 18:27:14 +000026#include <stdio.h>
Rich Evans00ab4702015-02-06 13:43:58 +000027#include <string.h>
28
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000030#include "mbedtls/pem.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020031#endif
32
Agathiyan Bragadeeshddc720d2023-07-26 15:51:49 +010033#include "mbedtls/asn1write.h"
Agathiyan Bragadeeshddc720d2023-07-26 15:51:49 +010034
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000035#include "mbedtls/platform.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020036
Simon Butcherb5b6af22016-07-13 14:46:18 +010037#if defined(MBEDTLS_HAVE_TIME)
38#include "mbedtls/platform_time.h"
39#endif
Nicholas Wilson512b4ee2017-12-05 12:07:33 +000040#if defined(MBEDTLS_HAVE_TIME_DATE)
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +010041#include "mbedtls/platform_util.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020042#include <time.h>
43#endif
44
Demi Marie Obenour690b8c92022-12-04 04:24:22 -050045#define CHECK(code) \
46 do { \
47 if ((ret = (code)) != 0) { \
48 return ret; \
49 } \
50 } while (0)
51
Hanno Becker1eeca412018-10-15 12:01:35 +010052#define CHECK_RANGE(min, max, val) \
Demi Marie Obenour690b8c92022-12-04 04:24:22 -050053 do { \
54 if ((val) < (min) || (val) > (max)) { \
55 return ret; \
Hanno Becker1eeca412018-10-15 12:01:35 +010056 } \
Gilles Peskine449bd832023-01-11 14:50:10 +010057 } while (0)
Rich Evans7d5a55a2015-02-13 11:48:02 +000058
Paul Bakker7c6b2c32013-09-16 13:49:26 +020059/*
60 * CertificateSerialNumber ::= INTEGER
61 */
Gilles Peskine449bd832023-01-11 14:50:10 +010062int mbedtls_x509_get_serial(unsigned char **p, const unsigned char *end,
63 mbedtls_x509_buf *serial)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020064{
Janos Follath865b3eb2019-12-16 11:46:15 +000065 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020066
Gilles Peskine449bd832023-01-11 14:50:10 +010067 if ((end - *p) < 1) {
68 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_SERIAL,
69 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
70 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +020071
Gilles Peskine449bd832023-01-11 14:50:10 +010072 if (**p != (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_PRIMITIVE | 2) &&
73 **p != MBEDTLS_ASN1_INTEGER) {
74 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_SERIAL,
75 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
76 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +020077
78 serial->tag = *(*p)++;
79
Gilles Peskine449bd832023-01-11 14:50:10 +010080 if ((ret = mbedtls_asn1_get_len(p, end, &serial->len)) != 0) {
81 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_SERIAL, ret);
82 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +020083
84 serial->p = *p;
85 *p += serial->len;
86
Gilles Peskine449bd832023-01-11 14:50:10 +010087 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020088}
89
90/* Get an algorithm identifier without parameters (eg for signatures)
91 *
92 * AlgorithmIdentifier ::= SEQUENCE {
93 * algorithm OBJECT IDENTIFIER,
94 * parameters ANY DEFINED BY algorithm OPTIONAL }
95 */
Gilles Peskine449bd832023-01-11 14:50:10 +010096int mbedtls_x509_get_alg_null(unsigned char **p, const unsigned char *end,
97 mbedtls_x509_buf *alg)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020098{
Janos Follath865b3eb2019-12-16 11:46:15 +000099 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200100
Gilles Peskine449bd832023-01-11 14:50:10 +0100101 if ((ret = mbedtls_asn1_get_alg_null(p, end, alg)) != 0) {
102 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
103 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200104
Gilles Peskine449bd832023-01-11 14:50:10 +0100105 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200106}
107
108/*
Antonin Décimo36e89b52019-01-23 15:24:37 +0100109 * Parse an algorithm identifier with (optional) parameters
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100110 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100111int mbedtls_x509_get_alg(unsigned char **p, const unsigned char *end,
112 mbedtls_x509_buf *alg, mbedtls_x509_buf *params)
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100113{
Janos Follath865b3eb2019-12-16 11:46:15 +0000114 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100115
Gilles Peskine449bd832023-01-11 14:50:10 +0100116 if ((ret = mbedtls_asn1_get_alg(p, end, alg, params)) != 0) {
117 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
118 }
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100119
Gilles Peskine449bd832023-01-11 14:50:10 +0100120 return 0;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100121}
122
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200123/*
124 * Convert md type to string
125 */
Dave Rodgmanffabb7b2023-06-28 16:22:50 +0100126#if !defined(MBEDTLS_X509_REMOVE_INFO) && defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
127
Gilles Peskine449bd832023-01-11 14:50:10 +0100128static inline const char *md_type_to_string(mbedtls_md_type_t md_alg)
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200129{
Gilles Peskine449bd832023-01-11 14:50:10 +0100130 switch (md_alg) {
Elena Uziunaiteb66a9912024-05-10 14:25:58 +0100131#if defined(PSA_WANT_ALG_MD5)
Gilles Peskine449bd832023-01-11 14:50:10 +0100132 case MBEDTLS_MD_MD5:
133 return "MD5";
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200134#endif
Elena Uziunaite9fc5be02024-09-04 18:12:59 +0100135#if defined(PSA_WANT_ALG_SHA_1)
Gilles Peskine449bd832023-01-11 14:50:10 +0100136 case MBEDTLS_MD_SHA1:
137 return "SHA1";
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200138#endif
Elena Uziunaitefcc9afa2024-05-23 14:43:22 +0100139#if defined(PSA_WANT_ALG_SHA_224)
Gilles Peskine449bd832023-01-11 14:50:10 +0100140 case MBEDTLS_MD_SHA224:
141 return "SHA224";
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200142#endif
Elena Uziunaite0916cd72024-05-23 17:01:07 +0100143#if defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine449bd832023-01-11 14:50:10 +0100144 case MBEDTLS_MD_SHA256:
145 return "SHA256";
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200146#endif
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +0100147#if defined(PSA_WANT_ALG_SHA_384)
Gilles Peskine449bd832023-01-11 14:50:10 +0100148 case MBEDTLS_MD_SHA384:
149 return "SHA384";
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200150#endif
Gabor Mezeic15ef932024-06-13 12:53:54 +0200151#if defined(PSA_WANT_ALG_SHA_512)
Gilles Peskine449bd832023-01-11 14:50:10 +0100152 case MBEDTLS_MD_SHA512:
153 return "SHA512";
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200154#endif
Elena Uziunaite1b6fb212024-05-10 16:17:41 +0100155#if defined(PSA_WANT_ALG_RIPEMD160)
Gilles Peskine449bd832023-01-11 14:50:10 +0100156 case MBEDTLS_MD_RIPEMD160:
157 return "RIPEMD160";
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200158#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100159 case MBEDTLS_MD_NONE:
160 return NULL;
161 default:
162 return NULL;
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200163 }
164}
165
Dave Rodgmanf032c982023-06-29 12:09:27 +0100166#endif /* !defined(MBEDTLS_X509_REMOVE_INFO) && defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) */
Dave Rodgmanffabb7b2023-06-28 16:22:50 +0100167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100169/*
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100170 * HashAlgorithm ::= AlgorithmIdentifier
171 *
172 * AlgorithmIdentifier ::= SEQUENCE {
173 * algorithm OBJECT IDENTIFIER,
174 * parameters ANY DEFINED BY algorithm OPTIONAL }
175 *
176 * For HashAlgorithm, parameters MUST be NULL or absent.
177 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100178static 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 +0100179{
Janos Follath865b3eb2019-12-16 11:46:15 +0000180 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100181 unsigned char *p;
182 const unsigned char *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183 mbedtls_x509_buf md_oid;
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100184 size_t len;
185
186 /* Make sure we got a SEQUENCE and setup bounds */
Gilles Peskine449bd832023-01-11 14:50:10 +0100187 if (alg->tag != (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) {
188 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
189 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
190 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100191
Andrzej Kurekfeaebc52020-07-16 04:37:41 -0400192 p = alg->p;
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100193 end = p + alg->len;
194
Gilles Peskine449bd832023-01-11 14:50:10 +0100195 if (p >= end) {
196 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
197 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
198 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100199
200 /* Parse md_oid */
201 md_oid.tag = *p;
202
Gilles Peskine449bd832023-01-11 14:50:10 +0100203 if ((ret = mbedtls_asn1_get_tag(&p, end, &md_oid.len, MBEDTLS_ASN1_OID)) != 0) {
204 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
205 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100206
207 md_oid.p = p;
208 p += md_oid.len;
209
210 /* Get md_alg from md_oid */
Gilles Peskine532e3ee2025-05-07 20:37:15 +0200211 if ((ret = mbedtls_x509_oid_get_md_alg(&md_oid, md_alg)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100212 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
213 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100214
215 /* Make sure params is absent of NULL */
Gilles Peskine449bd832023-01-11 14:50:10 +0100216 if (p == end) {
217 return 0;
218 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100219
Gilles Peskine449bd832023-01-11 14:50:10 +0100220 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_NULL)) != 0 || len != 0) {
221 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
222 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100223
Gilles Peskine449bd832023-01-11 14:50:10 +0100224 if (p != end) {
225 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
226 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
227 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100228
Gilles Peskine449bd832023-01-11 14:50:10 +0100229 return 0;
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100230}
231
232/*
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100233 * RSASSA-PSS-params ::= SEQUENCE {
234 * hashAlgorithm [0] HashAlgorithm DEFAULT sha1Identifier,
235 * maskGenAlgorithm [1] MaskGenAlgorithm DEFAULT mgf1SHA1Identifier,
236 * saltLength [2] INTEGER DEFAULT 20,
237 * trailerField [3] INTEGER DEFAULT 1 }
238 * -- Note that the tags in this Sequence are explicit.
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200239 *
240 * RFC 4055 (which defines use of RSASSA-PSS in PKIX) states that the value
241 * of trailerField MUST be 1, and PKCS#1 v2.2 doesn't even define any other
Tom Cosgrove1797b052022-12-04 17:19:59 +0000242 * option. Enforce this at parsing time.
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100243 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100244int mbedtls_x509_get_rsassa_pss_params(const mbedtls_x509_buf *params,
245 mbedtls_md_type_t *md_alg, mbedtls_md_type_t *mgf_md,
246 int *salt_len)
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100247{
Janos Follath865b3eb2019-12-16 11:46:15 +0000248 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100249 unsigned char *p;
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100250 const unsigned char *end, *end2;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100251 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200252 mbedtls_x509_buf alg_id, alg_params;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100253
254 /* First set everything to defaults */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200255 *md_alg = MBEDTLS_MD_SHA1;
256 *mgf_md = MBEDTLS_MD_SHA1;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100257 *salt_len = 20;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100258
259 /* Make sure params is a SEQUENCE and setup bounds */
Gilles Peskine449bd832023-01-11 14:50:10 +0100260 if (params->tag != (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) {
261 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
262 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
263 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100264
265 p = (unsigned char *) params->p;
266 end = p + params->len;
267
Gilles Peskine449bd832023-01-11 14:50:10 +0100268 if (p == end) {
269 return 0;
270 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100271
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100272 /*
273 * HashAlgorithm
274 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100275 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
276 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
277 0)) == 0) {
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100278 end2 = p + len;
279
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100280 /* HashAlgorithm ::= AlgorithmIdentifier (without parameters) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100281 if ((ret = mbedtls_x509_get_alg_null(&p, end2, &alg_id)) != 0) {
282 return ret;
283 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100284
Gilles Peskine532e3ee2025-05-07 20:37:15 +0200285 if ((ret = mbedtls_x509_oid_get_md_alg(&alg_id, md_alg)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100286 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
287 }
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100288
Gilles Peskine449bd832023-01-11 14:50:10 +0100289 if (p != end2) {
290 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
291 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
292 }
293 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
294 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100295 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100296
Gilles Peskine449bd832023-01-11 14:50:10 +0100297 if (p == end) {
298 return 0;
299 }
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100300
301 /*
302 * MaskGenAlgorithm
303 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100304 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
305 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
306 1)) == 0) {
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100307 end2 = p + len;
308
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100309 /* MaskGenAlgorithm ::= AlgorithmIdentifier (params = HashAlgorithm) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100310 if ((ret = mbedtls_x509_get_alg(&p, end2, &alg_id, &alg_params)) != 0) {
311 return ret;
312 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100313
314 /* Only MFG1 is recognised for now */
Gilles Peskine449bd832023-01-11 14:50:10 +0100315 if (MBEDTLS_OID_CMP(MBEDTLS_OID_MGF1, &alg_id) != 0) {
316 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE,
Gilles Peskine4c832212025-05-07 23:05:12 +0200317 MBEDTLS_ERR_X509_UNKNOWN_OID);
Gilles Peskine449bd832023-01-11 14:50:10 +0100318 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100319
320 /* Parse HashAlgorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +0100321 if ((ret = x509_get_hash_alg(&alg_params, mgf_md)) != 0) {
322 return ret;
323 }
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100324
Gilles Peskine449bd832023-01-11 14:50:10 +0100325 if (p != end2) {
326 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
327 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
328 }
329 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
330 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100331 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100332
Gilles Peskine449bd832023-01-11 14:50:10 +0100333 if (p == end) {
334 return 0;
335 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100336
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100337 /*
338 * salt_len
339 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100340 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
341 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
342 2)) == 0) {
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100343 end2 = p + len;
344
Gilles Peskine449bd832023-01-11 14:50:10 +0100345 if ((ret = mbedtls_asn1_get_int(&p, end2, salt_len)) != 0) {
346 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
347 }
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100348
Gilles Peskine449bd832023-01-11 14:50:10 +0100349 if (p != end2) {
350 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
351 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
352 }
353 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
354 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100355 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100356
Gilles Peskine449bd832023-01-11 14:50:10 +0100357 if (p == end) {
358 return 0;
359 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100360
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100361 /*
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200362 * trailer_field (if present, must be 1)
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100363 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100364 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
365 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
366 3)) == 0) {
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200367 int trailer_field;
368
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100369 end2 = p + len;
370
Gilles Peskine449bd832023-01-11 14:50:10 +0100371 if ((ret = mbedtls_asn1_get_int(&p, end2, &trailer_field)) != 0) {
372 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
373 }
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100374
Gilles Peskine449bd832023-01-11 14:50:10 +0100375 if (p != end2) {
376 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
377 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
378 }
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200379
Gilles Peskine449bd832023-01-11 14:50:10 +0100380 if (trailer_field != 1) {
381 return MBEDTLS_ERR_X509_INVALID_ALG;
382 }
383 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
384 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100385 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100386
Gilles Peskine449bd832023-01-11 14:50:10 +0100387 if (p != end) {
388 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
389 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
390 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100391
Gilles Peskine449bd832023-01-11 14:50:10 +0100392 return 0;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100393}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200394#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100395
396/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200397 * AttributeTypeAndValue ::= SEQUENCE {
398 * type AttributeType,
399 * value AttributeValue }
400 *
401 * AttributeType ::= OBJECT IDENTIFIER
402 *
403 * AttributeValue ::= ANY DEFINED BY AttributeType
404 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100405static int x509_get_attr_type_value(unsigned char **p,
406 const unsigned char *end,
407 mbedtls_x509_name *cur)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200408{
Janos Follath865b3eb2019-12-16 11:46:15 +0000409 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200410 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200411 mbedtls_x509_buf *oid;
412 mbedtls_x509_buf *val;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200413
Gilles Peskine449bd832023-01-11 14:50:10 +0100414 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
415 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
416 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME, ret);
417 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200418
Hanno Becker12f62fb2019-02-12 17:22:36 +0000419 end = *p + len;
420
Gilles Peskine449bd832023-01-11 14:50:10 +0100421 if ((end - *p) < 1) {
422 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME,
423 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
424 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200425
426 oid = &cur->oid;
427 oid->tag = **p;
428
Gilles Peskine449bd832023-01-11 14:50:10 +0100429 if ((ret = mbedtls_asn1_get_tag(p, end, &oid->len, MBEDTLS_ASN1_OID)) != 0) {
430 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME, ret);
431 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200432
433 oid->p = *p;
434 *p += oid->len;
435
Gilles Peskine449bd832023-01-11 14:50:10 +0100436 if ((end - *p) < 1) {
437 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME,
438 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
439 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200440
Gilles Peskine449bd832023-01-11 14:50:10 +0100441 if (**p != MBEDTLS_ASN1_BMP_STRING && **p != MBEDTLS_ASN1_UTF8_STRING &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200442 **p != MBEDTLS_ASN1_T61_STRING && **p != MBEDTLS_ASN1_PRINTABLE_STRING &&
443 **p != MBEDTLS_ASN1_IA5_STRING && **p != MBEDTLS_ASN1_UNIVERSAL_STRING &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100444 **p != MBEDTLS_ASN1_BIT_STRING) {
445 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME,
446 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
447 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200448
449 val = &cur->val;
450 val->tag = *(*p)++;
451
Gilles Peskine449bd832023-01-11 14:50:10 +0100452 if ((ret = mbedtls_asn1_get_len(p, end, &val->len)) != 0) {
453 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME, ret);
454 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200455
456 val->p = *p;
457 *p += val->len;
458
Gilles Peskine449bd832023-01-11 14:50:10 +0100459 if (*p != end) {
460 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME,
461 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Hanno Becker12f62fb2019-02-12 17:22:36 +0000462 }
463
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200464 cur->next = NULL;
465
Gilles Peskine449bd832023-01-11 14:50:10 +0100466 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200467}
468
469/*
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000470 * Name ::= CHOICE { -- only one possibility for now --
471 * rdnSequence RDNSequence }
472 *
473 * RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
474 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200475 * RelativeDistinguishedName ::=
476 * SET OF AttributeTypeAndValue
477 *
478 * AttributeTypeAndValue ::= SEQUENCE {
479 * type AttributeType,
480 * value AttributeValue }
481 *
482 * AttributeType ::= OBJECT IDENTIFIER
483 *
484 * AttributeValue ::= ANY DEFINED BY AttributeType
Manuel Pégourié-Gonnard5d861852014-10-17 12:41:41 +0200485 *
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000486 * The data structure is optimized for the common case where each RDN has only
487 * one element, which is represented as a list of AttributeTypeAndValue.
488 * For the general case we still use a flat list, but we mark elements of the
489 * same set so that they are "merged" together in the functions that consume
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200490 * this list, eg mbedtls_x509_dn_gets().
David Horstmanned794832022-10-04 18:12:06 +0100491 *
David Horstmann11307a12022-10-17 18:10:23 +0100492 * On success, this function may allocate a linked list starting at cur->next
David Horstmanned794832022-10-04 18:12:06 +0100493 * that must later be free'd by the caller using mbedtls_free(). In error
494 * cases, this function frees all allocated memory internally and the caller
495 * has no freeing responsibilities.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200496 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100497int mbedtls_x509_get_name(unsigned char **p, const unsigned char *end,
498 mbedtls_x509_name *cur)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200499{
Janos Follath865b3eb2019-12-16 11:46:15 +0000500 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard5d861852014-10-17 12:41:41 +0200501 size_t set_len;
502 const unsigned char *end_set;
David Horstmanned794832022-10-04 18:12:06 +0100503 mbedtls_x509_name *head = cur;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200504
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100505 /* don't use recursion, we'd risk stack overflow if not optimized */
Gilles Peskine449bd832023-01-11 14:50:10 +0100506 while (1) {
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100507 /*
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000508 * parse SET
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100509 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100510 if ((ret = mbedtls_asn1_get_tag(p, end, &set_len,
511 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SET)) != 0) {
512 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME, ret);
David Horstmanned794832022-10-04 18:12:06 +0100513 goto error;
514 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200515
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100516 end_set = *p + set_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200517
Gilles Peskine449bd832023-01-11 14:50:10 +0100518 while (1) {
519 if ((ret = x509_get_attr_type_value(p, end_set, cur)) != 0) {
David Horstmanned794832022-10-04 18:12:06 +0100520 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +0100521 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200522
Gilles Peskine449bd832023-01-11 14:50:10 +0100523 if (*p == end_set) {
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000524 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100525 }
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000526
Manuel Pégourié-Gonnardeecb43c2015-05-12 12:56:41 +0200527 /* Mark this item as being no the only one in a set */
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000528 cur->next_merged = 1;
529
Gilles Peskine449bd832023-01-11 14:50:10 +0100530 cur->next = mbedtls_calloc(1, sizeof(mbedtls_x509_name));
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000531
Gilles Peskine449bd832023-01-11 14:50:10 +0100532 if (cur->next == NULL) {
David Horstmanned794832022-10-04 18:12:06 +0100533 ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
534 goto error;
535 }
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000536
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000537 cur = cur->next;
538 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200539
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100540 /*
541 * continue until end of SEQUENCE is reached
542 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100543 if (*p == end) {
544 return 0;
545 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200546
Gilles Peskine449bd832023-01-11 14:50:10 +0100547 cur->next = mbedtls_calloc(1, sizeof(mbedtls_x509_name));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200548
Gilles Peskine449bd832023-01-11 14:50:10 +0100549 if (cur->next == NULL) {
David Horstmanned794832022-10-04 18:12:06 +0100550 ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
551 goto error;
552 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200553
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100554 cur = cur->next;
555 }
David Horstmanned794832022-10-04 18:12:06 +0100556
557error:
David Horstmanned794832022-10-04 18:12:06 +0100558 /* Skip the first element as we did not allocate it */
Gilles Peskine449bd832023-01-11 14:50:10 +0100559 mbedtls_asn1_free_named_data_list_shallow(head->next);
Glenn Straussa4b40412022-06-26 19:32:09 -0400560 head->next = NULL;
David Horstmanned794832022-10-04 18:12:06 +0100561
Gilles Peskine449bd832023-01-11 14:50:10 +0100562 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200563}
564
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400565static int x509_date_is_valid(const mbedtls_x509_time *t)
Janos Follath87c98072017-02-03 12:36:59 +0000566{
David Horstmanncdf52832023-07-05 09:58:03 +0100567 unsigned int month_days;
568 unsigned int year;
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400569 switch (t->mon) {
570 case 1: case 3: case 5: case 7: case 8: case 10: case 12:
David Horstmanncdf52832023-07-05 09:58:03 +0100571 month_days = 31;
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400572 break;
573 case 4: case 6: case 9: case 11:
David Horstmanncdf52832023-07-05 09:58:03 +0100574 month_days = 30;
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400575 break;
576 case 2:
David Horstmanncdf52832023-07-05 09:58:03 +0100577 year = (unsigned int) t->year;
578 month_days = ((year & 3) || (!(year % 100)
579 && (year % 400)))
580 ? 28 : 29;
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400581 break;
582 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100583 return MBEDTLS_ERR_X509_INVALID_DATE;
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400584 }
Janos Follath87c98072017-02-03 12:36:59 +0000585
David Horstmannb1d27bc2023-07-05 10:00:31 +0100586 if ((unsigned int) (t->day - 1) >= month_days || /* (1 - days in month) */
David Horstmann3ae1c4c2023-07-05 11:15:08 +0100587 /* (unsigned int) (t->mon - 1) >= 12 || */ /* (1 - 12) checked above */
David Horstmannb1d27bc2023-07-05 10:00:31 +0100588 (unsigned int) t->year > 9999 || /* (0 - 9999) */
589 (unsigned int) t->hour > 23 || /* (0 - 23) */
590 (unsigned int) t->min > 59 || /* (0 - 59) */
591 (unsigned int) t->sec > 59) { /* (0 - 59) */
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400592 return MBEDTLS_ERR_X509_INVALID_DATE;
Rich Evans7d5a55a2015-02-13 11:48:02 +0000593 }
Janos Follath87c98072017-02-03 12:36:59 +0000594
Gilles Peskine449bd832023-01-11 14:50:10 +0100595 return 0;
Rich Evans7d5a55a2015-02-13 11:48:02 +0000596}
597
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400598static int x509_parse2_int(const unsigned char *p)
Andres AG4b76aec2016-09-23 13:16:02 +0100599{
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400600 uint32_t d1 = p[0] - '0';
601 uint32_t d2 = p[1] - '0';
602 return (d1 < 10 && d2 < 10) ? (int) (d1 * 10 + d2) : -1;
Andres AG4b76aec2016-09-23 13:16:02 +0100603}
604
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200605/*
Janos Follath87c98072017-02-03 12:36:59 +0000606 * Parse an ASN1_UTC_TIME (yearlen=2) or ASN1_GENERALIZED_TIME (yearlen=4)
607 * field.
608 */
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400609static int x509_parse_time(const unsigned char *p, mbedtls_x509_time *tm,
610 size_t yearlen)
Janos Follath87c98072017-02-03 12:36:59 +0000611{
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400612 int x;
Janos Follath87c98072017-02-03 12:36:59 +0000613
614 /*
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400615 * Parse year, month, day, hour, minute, second
Janos Follath87c98072017-02-03 12:36:59 +0000616 */
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400617 tm->year = x509_parse2_int(p);
618 if (tm->year < 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100619 return MBEDTLS_ERR_X509_INVALID_DATE;
620 }
Janos Follath87c98072017-02-03 12:36:59 +0000621
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400622 if (4 == yearlen) {
623 x = tm->year * 100;
624 p += 2;
625 tm->year = x509_parse2_int(p);
626 if (tm->year < 0) {
627 return MBEDTLS_ERR_X509_INVALID_DATE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100628 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100629 } else {
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400630 x = (tm->year < 50) ? 2000 : 1900;
Janos Follath87c98072017-02-03 12:36:59 +0000631 }
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400632 tm->year += x;
Janos Follath87c98072017-02-03 12:36:59 +0000633
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400634 tm->mon = x509_parse2_int(p + 2);
635 tm->day = x509_parse2_int(p + 4);
636 tm->hour = x509_parse2_int(p + 6);
637 tm->min = x509_parse2_int(p + 8);
638 tm->sec = x509_parse2_int(p + 10);
Janos Follath87c98072017-02-03 12:36:59 +0000639
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400640 return x509_date_is_valid(tm);
Janos Follath87c98072017-02-03 12:36:59 +0000641}
642
643/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200644 * Time ::= CHOICE {
645 * utcTime UTCTime,
646 * generalTime GeneralizedTime }
647 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100648int mbedtls_x509_get_time(unsigned char **p, const unsigned char *end,
649 mbedtls_x509_time *tm)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200650{
Janos Follath865b3eb2019-12-16 11:46:15 +0000651 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Janos Follath87c98072017-02-03 12:36:59 +0000652 size_t len, year_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200653 unsigned char tag;
654
Gilles Peskine449bd832023-01-11 14:50:10 +0100655 if ((end - *p) < 1) {
656 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE,
657 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
658 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200659
660 tag = **p;
661
Gilles Peskine449bd832023-01-11 14:50:10 +0100662 if (tag == MBEDTLS_ASN1_UTC_TIME) {
Janos Follath87c98072017-02-03 12:36:59 +0000663 year_len = 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100664 } else if (tag == MBEDTLS_ASN1_GENERALIZED_TIME) {
Janos Follath87c98072017-02-03 12:36:59 +0000665 year_len = 4;
Gilles Peskine449bd832023-01-11 14:50:10 +0100666 } else {
667 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE,
668 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
669 }
Janos Follath87c98072017-02-03 12:36:59 +0000670
671 (*p)++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100672 ret = mbedtls_asn1_get_len(p, end, &len);
Janos Follath87c98072017-02-03 12:36:59 +0000673
Gilles Peskine449bd832023-01-11 14:50:10 +0100674 if (ret != 0) {
675 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE, ret);
676 }
Janos Follath87c98072017-02-03 12:36:59 +0000677
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400678 /* len is 12 or 14 depending on year_len, plus optional trailing 'Z' */
679 if (len != year_len + 10 &&
680 !(len == year_len + 11 && (*p)[(len - 1)] == 'Z')) {
681 return MBEDTLS_ERR_X509_INVALID_DATE;
682 }
683
684 (*p) += len;
685 return x509_parse_time(*p - len, tm, year_len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200686}
687
Gilles Peskine449bd832023-01-11 14:50:10 +0100688int mbedtls_x509_get_sig(unsigned char **p, const unsigned char *end, mbedtls_x509_buf *sig)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200689{
Janos Follath865b3eb2019-12-16 11:46:15 +0000690 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200691 size_t len;
Andres AG4bdbe092016-09-19 16:58:45 +0100692 int tag_type;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200693
Gilles Peskine449bd832023-01-11 14:50:10 +0100694 if ((end - *p) < 1) {
695 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_SIGNATURE,
696 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
697 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200698
Andres AG4bdbe092016-09-19 16:58:45 +0100699 tag_type = **p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200700
Gilles Peskine449bd832023-01-11 14:50:10 +0100701 if ((ret = mbedtls_asn1_get_bitstring_null(p, end, &len)) != 0) {
702 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_SIGNATURE, ret);
703 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200704
Andres AG4bdbe092016-09-19 16:58:45 +0100705 sig->tag = tag_type;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200706 sig->len = len;
707 sig->p = *p;
708
709 *p += len;
710
Gilles Peskine449bd832023-01-11 14:50:10 +0100711 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200712}
713
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100714/*
715 * Get signature algorithm from alg OID and optional parameters
716 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100717int mbedtls_x509_get_sig_alg(const mbedtls_x509_buf *sig_oid, const mbedtls_x509_buf *sig_params,
Valerio Setti68878cc2025-04-10 23:30:26 +0200718 mbedtls_md_type_t *md_alg, mbedtls_pk_type_t *pk_alg)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200719{
Janos Follath865b3eb2019-12-16 11:46:15 +0000720 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200721
Gilles Peskine532e3ee2025-05-07 20:37:15 +0200722 if ((ret = mbedtls_x509_oid_get_sig_alg(sig_oid, md_alg, pk_alg)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100723 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG, ret);
724 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200725
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200726#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100727 if (*pk_alg == MBEDTLS_PK_RSASSA_PSS) {
Valerio Setti68878cc2025-04-10 23:30:26 +0200728 mbedtls_md_type_t mgf1_hash_id;
729 int expected_salt_len;
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200730
Gilles Peskine449bd832023-01-11 14:50:10 +0100731 ret = mbedtls_x509_get_rsassa_pss_params(sig_params,
732 md_alg,
Valerio Setti68878cc2025-04-10 23:30:26 +0200733 &mgf1_hash_id,
734 &expected_salt_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100735 if (ret != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100736 return ret;
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200737 }
Valerio Setti68878cc2025-04-10 23:30:26 +0200738 /* Ensure MGF1 hash alg is the same as the one used to hash the message. */
739 if (mgf1_hash_id != *md_alg) {
740 return MBEDTLS_ERR_X509_INVALID_ALG;
741 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100742 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200743#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100744 {
745 /* Make sure parameters are absent or NULL */
Gilles Peskine449bd832023-01-11 14:50:10 +0100746 if ((sig_params->tag != MBEDTLS_ASN1_NULL && sig_params->tag != 0) ||
747 sig_params->len != 0) {
748 return MBEDTLS_ERR_X509_INVALID_ALG;
749 }
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100750 }
751
Gilles Peskine449bd832023-01-11 14:50:10 +0100752 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200753}
754
755/*
756 * X.509 Extensions (No parsing of extensions, pointer should
Brian J Murray1903fb32016-11-06 04:45:15 -0800757 * be either manually updated or extensions should be parsed!)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200758 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100759int mbedtls_x509_get_ext(unsigned char **p, const unsigned char *end,
760 mbedtls_x509_buf *ext, int tag)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200761{
Janos Follath865b3eb2019-12-16 11:46:15 +0000762 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200763 size_t len;
764
Hanno Becker3cddba82019-02-11 14:33:36 +0000765 /* Extension structure use EXPLICIT tagging. That is, the actual
766 * `Extensions` structure is wrapped by a tag-length pair using
767 * the respective context-specific tag. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100768 ret = mbedtls_asn1_get_tag(p, end, &ext->len,
769 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | tag);
770 if (ret != 0) {
771 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
772 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200773
Hanno Becker6ccfb182019-02-12 11:52:10 +0000774 ext->tag = MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | tag;
775 ext->p = *p;
776 end = *p + ext->len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200777
778 /*
779 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200780 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100781 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
782 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
783 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
784 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200785
Gilles Peskine449bd832023-01-11 14:50:10 +0100786 if (end != *p + len) {
787 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
788 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
789 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200790
Gilles Peskine449bd832023-01-11 14:50:10 +0100791 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200792}
793
Agathiyan Bragadeesh9d2507c2023-07-24 16:35:57 +0100794static char nibble_to_hex_digit(int i)
Agathiyan Bragadeeshef2decb2023-07-21 15:47:47 +0100795{
Agathiyan Bragadeeshf0e1ac52023-07-24 16:43:36 +0100796 return (i < 10) ? (i + '0') : (i - 10 + 'A');
Agathiyan Bragadeeshef2decb2023-07-21 15:47:47 +0100797}
798
Sam Berry4f761942024-07-19 14:56:30 +0100799/* Return the x.y.z.... style numeric string for the given OID */
800int mbedtls_oid_get_numeric_string(char *buf, size_t size,
801 const mbedtls_asn1_buf *oid)
802{
803 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
804 char *p = buf;
805 size_t n = size;
806 unsigned int value = 0;
807
808 if (size > INT_MAX) {
809 /* Avoid overflow computing return value */
810 return MBEDTLS_ERR_ASN1_INVALID_LENGTH;
811 }
812
813 if (oid->len <= 0) {
814 /* OID must not be empty */
815 return MBEDTLS_ERR_ASN1_OUT_OF_DATA;
816 }
817
818 for (size_t i = 0; i < oid->len; i++) {
819 /* Prevent overflow in value. */
820 if (value > (UINT_MAX >> 7)) {
821 return MBEDTLS_ERR_ASN1_INVALID_DATA;
822 }
823 if ((value == 0) && ((oid->p[i]) == 0x80)) {
824 /* Overlong encoding is not allowed */
825 return MBEDTLS_ERR_ASN1_INVALID_DATA;
826 }
827
828 value <<= 7;
829 value |= oid->p[i] & 0x7F;
830
831 if (!(oid->p[i] & 0x80)) {
832 /* Last byte */
833 if (n == size) {
834 int component1;
835 unsigned int component2;
836 /* First subidentifier contains first two OID components */
837 if (value >= 80) {
838 component1 = '2';
839 component2 = value - 80;
840 } else if (value >= 40) {
841 component1 = '1';
842 component2 = value - 40;
843 } else {
844 component1 = '0';
845 component2 = value;
846 }
847 ret = mbedtls_snprintf(p, n, "%c.%u", component1, component2);
848 } else {
849 ret = mbedtls_snprintf(p, n, ".%u", value);
850 }
851 if (ret < 2 || (size_t) ret >= n) {
Gilles Peskine47f1d7b2025-05-07 21:04:51 +0200852 return PSA_ERROR_BUFFER_TOO_SMALL;
Sam Berry4f761942024-07-19 14:56:30 +0100853 }
854 n -= (size_t) ret;
855 p += ret;
856 value = 0;
857 }
858 }
859
860 if (value != 0) {
861 /* Unterminated subidentifier */
862 return MBEDTLS_ERR_ASN1_OUT_OF_DATA;
863 }
864
865 return (int) (size - n);
866}
867
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200868/*
869 * Store the name in printable form into buf; no more
870 * than size characters will be written
871 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100872int mbedtls_x509_dn_gets(char *buf, size_t size, const mbedtls_x509_name *dn)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200873{
Janos Follath865b3eb2019-12-16 11:46:15 +0000874 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh5adffb22023-08-10 15:50:57 +0100875 size_t i, j, n, asn1_len_size, asn1_tag_size, asn1_tag_len_buf_start;
Agathiyan Bragadeeshc7959b22023-09-12 17:54:43 +0100876 /* 6 is enough as our asn1 write functions only write one byte for the tag and at most five bytes for the length*/
877 unsigned char asn1_tag_len_buf[6];
Agathiyan Bragadeeshddc720d2023-07-26 15:51:49 +0100878 unsigned char *asn1_len_p;
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000879 unsigned char c, merge = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200880 const mbedtls_x509_name *name;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200881 const char *short_name = NULL;
Agathiyan Bragadeesha1f5c2d2023-08-02 17:08:52 +0100882 char lowbits, highbits;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200883 char s[MBEDTLS_X509_MAX_DN_NAME_SIZE], *p;
Agathiyan Bragadeeshc9d74f32023-07-31 17:25:44 +0100884 int print_hexstring;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200885
Gilles Peskine449bd832023-01-11 14:50:10 +0100886 memset(s, 0, sizeof(s));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200887
888 name = dn;
889 p = buf;
890 n = size;
891
Gilles Peskine449bd832023-01-11 14:50:10 +0100892 while (name != NULL) {
893 if (!name->oid.p) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200894 name = name->next;
895 continue;
896 }
897
Gilles Peskine449bd832023-01-11 14:50:10 +0100898 if (name != dn) {
899 ret = mbedtls_snprintf(p, n, merge ? " + " : ", ");
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200900 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200901 }
902
Agathiyan Bragadeesh0a4b6d82023-08-02 15:05:57 +0100903 print_hexstring = (name->val.tag != MBEDTLS_ASN1_UTF8_STRING) &&
904 (name->val.tag != MBEDTLS_ASN1_PRINTABLE_STRING) &&
905 (name->val.tag != MBEDTLS_ASN1_IA5_STRING);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200906
Gilles Peskine532e3ee2025-05-07 20:37:15 +0200907 if ((ret = mbedtls_x509_oid_get_attr_short_name(&name->oid, &short_name)) == 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100908 ret = mbedtls_snprintf(p, n, "%s=", short_name);
909 } else {
Agathiyan Bragadeeshaf70c7d2023-08-10 16:39:23 +0100910 if ((ret = mbedtls_oid_get_numeric_string(p, n, &name->oid)) > 0) {
Agathiyan Bragadeeshf3b97242023-08-22 16:37:11 +0100911 n -= ret;
912 p += ret;
Agathiyan Bragadeeshaf70c7d2023-08-10 16:39:23 +0100913 ret = mbedtls_snprintf(p, n, "=");
Agathiyan Bragadeeshc9d74f32023-07-31 17:25:44 +0100914 print_hexstring = 1;
Gilles Peskine47f1d7b2025-05-07 21:04:51 +0200915 } else if (ret == PSA_ERROR_BUFFER_TOO_SMALL) {
Agathiyan Bragadeesh8aa74ab2023-08-22 16:42:27 +0100916 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
Agathiyan Bragadeesh15df0122023-08-22 17:50:00 +0100917 } else {
Agathiyan Bragadeeshddc720d2023-07-26 15:51:49 +0100918 ret = mbedtls_snprintf(p, n, "\?\?=");
919 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100920 }
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200921 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200922
Agathiyan Bragadeesh4987c8f2023-08-01 11:10:52 +0100923 if (print_hexstring) {
Agathiyan Bragadeeshddc720d2023-07-26 15:51:49 +0100924 s[0] = '#';
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200925
Agathiyan Bragadeeshc7959b22023-09-12 17:54:43 +0100926 asn1_len_p = asn1_tag_len_buf + sizeof(asn1_tag_len_buf);
Agathiyan Bragadeesh15df0122023-08-22 17:50:00 +0100927 if ((ret = mbedtls_asn1_write_len(&asn1_len_p, asn1_tag_len_buf, name->val.len)) < 0) {
Agathiyan Bragadeesh07f472a2023-08-22 16:29:39 +0100928 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
929 }
930 asn1_len_size = ret;
Agathiyan Bragadeesh15df0122023-08-22 17:50:00 +0100931 if ((ret = mbedtls_asn1_write_tag(&asn1_len_p, asn1_tag_len_buf, name->val.tag)) < 0) {
Agathiyan Bragadeesh07f472a2023-08-22 16:29:39 +0100932 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
933 }
934 asn1_tag_size = ret;
Agathiyan Bragadeeshc7959b22023-09-12 17:54:43 +0100935 asn1_tag_len_buf_start = sizeof(asn1_tag_len_buf) - asn1_len_size - asn1_tag_size;
Agathiyan Bragadeesh5adffb22023-08-10 15:50:57 +0100936 for (i = 0, j = 1; i < asn1_len_size + asn1_tag_size; i++) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100937 if (j + 1 >= sizeof(s) - 1) {
938 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
939 }
Agathiyan Bragadeesh5adffb22023-08-10 15:50:57 +0100940 c = asn1_tag_len_buf[asn1_tag_len_buf_start+i];
Agathiyan Bragadeesha1f5c2d2023-08-02 17:08:52 +0100941 lowbits = (c & 0x0F);
Agathiyan Bragadeesh2bf09a62023-08-10 14:37:00 +0100942 highbits = c >> 4;
Agathiyan Bragadeesh9d2507c2023-07-24 16:35:57 +0100943 s[j++] = nibble_to_hex_digit(highbits);
Agathiyan Bragadeesh5adffb22023-08-10 15:50:57 +0100944 s[j++] = nibble_to_hex_digit(lowbits);
Werner Lewisb33dacd2022-05-20 12:48:46 +0100945 }
Agathiyan Bragadeesh5adffb22023-08-10 15:50:57 +0100946 for (i = 0; i < name->val.len; i++) {
947 if (j + 1 >= sizeof(s) - 1) {
948 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
949 }
950 c = name->val.p[i];
951 lowbits = (c & 0x0F);
952 highbits = c >> 4;
953 s[j++] = nibble_to_hex_digit(highbits);
954 s[j++] = nibble_to_hex_digit(lowbits);
Agathiyan Bragadeeshddc720d2023-07-26 15:51:49 +0100955 }
956 } else {
957 for (i = 0, j = 0; i < name->val.len; i++, j++) {
958 if (j >= sizeof(s) - 1) {
959 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
960 }
961
962 c = name->val.p[i];
963 // Special characters requiring escaping, RFC 4514 Section 2.4
Agathiyan Bragadeesh022f86f2023-08-22 16:56:04 +0100964 if (c == '\0') {
965 return MBEDTLS_ERR_X509_INVALID_NAME;
Agathiyan Bragadeesh15df0122023-08-22 17:50:00 +0100966 } else {
Agathiyan Bragadeesha7f96302023-08-10 16:03:27 +0100967 if (strchr(",=+<>;\"\\", c) ||
Agathiyan Bragadeeshddc720d2023-07-26 15:51:49 +0100968 ((i == 0) && strchr("# ", c)) ||
969 ((i == name->val.len-1) && (c == ' '))) {
970 if (j + 1 >= sizeof(s) - 1) {
971 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
972 }
973 s[j++] = '\\';
974 }
975 }
976 if (c < 32 || c >= 127) {
977 if (j + 3 >= sizeof(s) - 1) {
978 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
979 }
980 s[j++] = '\\';
Agathiyan Bragadeesha1f5c2d2023-08-02 17:08:52 +0100981 lowbits = (c & 0x0F);
Agathiyan Bragadeesh2bf09a62023-08-10 14:37:00 +0100982 highbits = c >> 4;
Agathiyan Bragadeeshddc720d2023-07-26 15:51:49 +0100983 s[j++] = nibble_to_hex_digit(highbits);
984 s[j] = nibble_to_hex_digit(lowbits);
985 } else {
986 s[j] = c;
987 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100988 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200989 }
Werner Lewisb33dacd2022-05-20 12:48:46 +0100990 s[j] = '\0';
Gilles Peskine449bd832023-01-11 14:50:10 +0100991 ret = mbedtls_snprintf(p, n, "%s", s);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200992 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000993
994 merge = name->next_merged;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200995 name = name->next;
996 }
997
Gilles Peskine449bd832023-01-11 14:50:10 +0100998 return (int) (size - n);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200999}
1000
1001/*
1002 * Store the serial in printable form into buf; no more
1003 * than size characters will be written
1004 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001005int mbedtls_x509_serial_gets(char *buf, size_t size, const mbedtls_x509_buf *serial)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001006{
Janos Follath865b3eb2019-12-16 11:46:15 +00001007 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001008 size_t i, n, nr;
1009 char *p;
1010
1011 p = buf;
1012 n = size;
1013
Gilles Peskine449bd832023-01-11 14:50:10 +01001014 nr = (serial->len <= 32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001015 ? serial->len : 28;
1016
Gilles Peskine449bd832023-01-11 14:50:10 +01001017 for (i = 0; i < nr; i++) {
1018 if (i == 0 && nr > 1 && serial->p[i] == 0x0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001019 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001020 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001021
Gilles Peskine449bd832023-01-11 14:50:10 +01001022 ret = mbedtls_snprintf(p, n, "%02X%s",
1023 serial->p[i], (i < nr - 1) ? ":" : "");
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001024 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001025 }
1026
Gilles Peskine449bd832023-01-11 14:50:10 +01001027 if (nr != serial->len) {
1028 ret = mbedtls_snprintf(p, n, "....");
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001029 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001030 }
1031
Gilles Peskine449bd832023-01-11 14:50:10 +01001032 return (int) (size - n);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001033}
1034
Hanno Becker612a2f12020-10-09 09:19:39 +01001035#if !defined(MBEDTLS_X509_REMOVE_INFO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001036/*
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +02001037 * Helper for writing signature algorithms
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +01001038 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001039int mbedtls_x509_sig_alg_gets(char *buf, size_t size, const mbedtls_x509_buf *sig_oid,
Valerio Settid24dfad2025-04-23 11:13:02 +02001040 mbedtls_pk_type_t pk_alg, mbedtls_md_type_t md_alg)
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +01001041{
Janos Follath865b3eb2019-12-16 11:46:15 +00001042 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +01001043 char *p = buf;
1044 size_t n = size;
1045 const char *desc = NULL;
1046
Gilles Peskine532e3ee2025-05-07 20:37:15 +02001047 ret = mbedtls_x509_oid_get_sig_alg_desc(sig_oid, &desc);
Gilles Peskine449bd832023-01-11 14:50:10 +01001048 if (ret != 0) {
1049 ret = mbedtls_snprintf(p, n, "???");
1050 } else {
1051 ret = mbedtls_snprintf(p, n, "%s", desc);
1052 }
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001053 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +01001054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001055#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001056 if (pk_alg == MBEDTLS_PK_RSASSA_PSS) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001057 const char *name = md_type_to_string(md_alg);
Valerio Settid24dfad2025-04-23 11:13:02 +02001058 if (name != NULL) {
1059 ret = mbedtls_snprintf(p, n, " (%s)", name);
1060 } else {
1061 ret = mbedtls_snprintf(p, n, " (?)");
1062 }
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001063 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +01001064 }
1065#else
1066 ((void) pk_alg);
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +02001067 ((void) md_alg);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001068#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +01001069
Gilles Peskine449bd832023-01-11 14:50:10 +01001070 return (int) (size - n);
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +01001071}
Hanno Becker612a2f12020-10-09 09:19:39 +01001072#endif /* MBEDTLS_X509_REMOVE_INFO */
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +01001073
1074/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001075 * Helper for writing "RSA key size", "EC key size", etc
1076 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001077int mbedtls_x509_key_size_helper(char *buf, size_t buf_size, const char *name)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001078{
1079 char *p = buf;
Manuel Pégourié-Gonnardfb317c52015-06-18 16:25:56 +02001080 size_t n = buf_size;
Janos Follath865b3eb2019-12-16 11:46:15 +00001081 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001082
Gilles Peskine449bd832023-01-11 14:50:10 +01001083 ret = mbedtls_snprintf(p, n, "%s key size", name);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001084 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001085
Gilles Peskine449bd832023-01-11 14:50:10 +01001086 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001087}
1088
Glenn Strauss416dc032022-06-30 00:38:53 -04001089int mbedtls_x509_time_cmp(const mbedtls_x509_time *t1,
1090 const mbedtls_x509_time *t2)
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001091{
Glenn Strauss5aef2972022-06-30 04:38:02 -04001092 int x;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001093
Glenn Strauss5aef2972022-06-30 04:38:02 -04001094 x = (((t1->year << 9) | (t1->mon << 5) | (t1->day)) -
1095 ((t2->year << 9) | (t2->mon << 5) | (t2->day)));
1096 if (x != 0) {
1097 return x;
Manuel Pégourié-Gonnard57e10d72015-06-22 18:59:21 +02001098 }
Manuel Pégourié-Gonnard864108d2015-05-29 10:11:03 +02001099
Glenn Strauss5aef2972022-06-30 04:38:02 -04001100 x = (((t1->hour << 12) | (t1->min << 6) | (t1->sec)) -
1101 ((t2->hour << 12) | (t2->min << 6) | (t2->sec)));
1102 return x;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001103}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001104
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001105#if defined(MBEDTLS_HAVE_TIME_DATE)
Glenn Strauss61d99302022-06-30 05:25:56 -04001106int mbedtls_x509_time_gmtime(mbedtls_time_t tt, mbedtls_x509_time *now)
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001107{
Glenn Strauss811eeb22022-06-30 05:28:50 -04001108 struct tm tm;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001109
Glenn Strauss811eeb22022-06-30 05:28:50 -04001110 if (mbedtls_platform_gmtime_r(&tt, &tm) == NULL) {
1111 return -1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001112 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001113
Glenn Strauss811eeb22022-06-30 05:28:50 -04001114 now->year = tm.tm_year + 1900;
1115 now->mon = tm.tm_mon + 1;
1116 now->day = tm.tm_mday;
1117 now->hour = tm.tm_hour;
1118 now->min = tm.tm_min;
1119 now->sec = tm.tm_sec;
Gilles Peskine449bd832023-01-11 14:50:10 +01001120 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001121}
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001122
Glenn Strauss61d99302022-06-30 05:25:56 -04001123static int x509_get_current_time(mbedtls_x509_time *now)
1124{
1125 return mbedtls_x509_time_gmtime(mbedtls_time(NULL), now);
1126}
1127
Gilles Peskine449bd832023-01-11 14:50:10 +01001128int mbedtls_x509_time_is_past(const mbedtls_x509_time *to)
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001129{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001130 mbedtls_x509_time now;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001131
Gilles Peskine449bd832023-01-11 14:50:10 +01001132 if (x509_get_current_time(&now) != 0) {
1133 return 1;
1134 }
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001135
Glenn Strauss416dc032022-06-30 00:38:53 -04001136 return mbedtls_x509_time_cmp(to, &now) < 0;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001137}
1138
Gilles Peskine449bd832023-01-11 14:50:10 +01001139int mbedtls_x509_time_is_future(const mbedtls_x509_time *from)
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001140{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001141 mbedtls_x509_time now;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001142
Gilles Peskine449bd832023-01-11 14:50:10 +01001143 if (x509_get_current_time(&now) != 0) {
1144 return 1;
1145 }
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001146
Glenn Strauss416dc032022-06-30 00:38:53 -04001147 return mbedtls_x509_time_cmp(from, &now) > 0;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001148}
1149
Manuel Pégourié-Gonnard60c793b2015-06-18 20:52:58 +02001150#else /* MBEDTLS_HAVE_TIME_DATE */
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001151
Gilles Peskine449bd832023-01-11 14:50:10 +01001152int mbedtls_x509_time_is_past(const mbedtls_x509_time *to)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001153{
1154 ((void) to);
Gilles Peskine449bd832023-01-11 14:50:10 +01001155 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001156}
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001157
Gilles Peskine449bd832023-01-11 14:50:10 +01001158int mbedtls_x509_time_is_future(const mbedtls_x509_time *from)
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001159{
1160 ((void) from);
Gilles Peskine449bd832023-01-11 14:50:10 +01001161 return 0;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001162}
Manuel Pégourié-Gonnard60c793b2015-06-18 20:52:58 +02001163#endif /* MBEDTLS_HAVE_TIME_DATE */
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001164
1165/* Common functions for parsing CRT and CSR. */
1166#if defined(MBEDTLS_X509_CRT_PARSE_C) || defined(MBEDTLS_X509_CSR_PARSE_C)
1167/*
1168 * OtherName ::= SEQUENCE {
1169 * type-id OBJECT IDENTIFIER,
1170 * value [0] EXPLICIT ANY DEFINED BY type-id }
1171 *
1172 * HardwareModuleName ::= SEQUENCE {
1173 * hwType OBJECT IDENTIFIER,
1174 * hwSerialNum OCTET STRING }
1175 *
1176 * NOTE: we currently only parse and use otherName of type HwModuleName,
1177 * as defined in RFC 4108.
1178 */
1179static int x509_get_other_name(const mbedtls_x509_buf *subject_alt_name,
1180 mbedtls_x509_san_other_name *other_name)
1181{
1182 int ret = 0;
1183 size_t len;
1184 unsigned char *p = subject_alt_name->p;
1185 const unsigned char *end = p + subject_alt_name->len;
1186 mbedtls_x509_buf cur_oid;
1187
1188 if ((subject_alt_name->tag &
1189 (MBEDTLS_ASN1_TAG_CLASS_MASK | MBEDTLS_ASN1_TAG_VALUE_MASK)) !=
1190 (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME)) {
1191 /*
1192 * The given subject alternative name is not of type "othername".
1193 */
1194 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1195 }
1196
1197 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1198 MBEDTLS_ASN1_OID)) != 0) {
1199 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1200 }
1201
1202 cur_oid.tag = MBEDTLS_ASN1_OID;
1203 cur_oid.p = p;
1204 cur_oid.len = len;
1205
1206 /*
1207 * Only HwModuleName is currently supported.
1208 */
1209 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ON_HW_MODULE_NAME, &cur_oid) != 0) {
1210 return MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
1211 }
David Horstmann2ea44d22023-08-18 18:36:02 +01001212 other_name->type_id = cur_oid;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001213
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001214 p += len;
1215 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1216 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC)) !=
1217 0) {
1218 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1219 }
1220
Hanno Beckerdae916b2019-09-13 14:21:13 +01001221 if (end != p + len) {
1222 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1223 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1224 }
1225
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001226 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1227 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1228 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1229 }
1230
Hanno Beckerdae916b2019-09-13 14:21:13 +01001231 if (end != p + len) {
1232 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1233 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1234 }
1235
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001236 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OID)) != 0) {
1237 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1238 }
1239
1240 other_name->value.hardware_module_name.oid.tag = MBEDTLS_ASN1_OID;
1241 other_name->value.hardware_module_name.oid.p = p;
1242 other_name->value.hardware_module_name.oid.len = len;
1243
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001244 p += len;
1245 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1246 MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1247 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1248 }
1249
1250 other_name->value.hardware_module_name.val.tag = MBEDTLS_ASN1_OCTET_STRING;
1251 other_name->value.hardware_module_name.val.p = p;
1252 other_name->value.hardware_module_name.val.len = len;
1253 p += len;
1254 if (p != end) {
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001255 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1256 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1257 }
1258 return 0;
1259}
1260
Przemek Stekiel21903ec2023-02-21 08:32:37 +01001261/* Check mbedtls_x509_get_subject_alt_name for detailed description.
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001262 *
Przemek Stekiel21903ec2023-02-21 08:32:37 +01001263 * In some cases while parsing subject alternative names the sequence tag is optional
1264 * (e.g. CertSerialNumber). This function is designed to handle such case.
Przemek Stekiel725688b2023-04-04 22:49:44 +02001265 */
Przemek Stekiel21903ec2023-02-21 08:32:37 +01001266int mbedtls_x509_get_subject_alt_name_ext(unsigned char **p,
1267 const unsigned char *end,
1268 mbedtls_x509_sequence *subject_alt_name)
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001269{
1270 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel21903ec2023-02-21 08:32:37 +01001271 size_t tag_len;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001272 mbedtls_asn1_sequence *cur = subject_alt_name;
1273
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001274 while (*p < end) {
Andrzej Kurek154a6052023-04-30 14:11:49 -04001275 mbedtls_x509_subject_alternative_name tmp_san_name;
Hanno Beckerae8f8c42019-09-13 12:24:56 +01001276 mbedtls_x509_buf tmp_san_buf;
Andrzej Kurek154a6052023-04-30 14:11:49 -04001277 memset(&tmp_san_name, 0, sizeof(tmp_san_name));
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001278
Hanno Beckerae8f8c42019-09-13 12:24:56 +01001279 tmp_san_buf.tag = **p;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001280 (*p)++;
Hanno Beckerae8f8c42019-09-13 12:24:56 +01001281
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001282 if ((ret = mbedtls_asn1_get_len(p, end, &tag_len)) != 0) {
1283 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1284 }
1285
Hanno Beckerae8f8c42019-09-13 12:24:56 +01001286 tmp_san_buf.p = *p;
1287 tmp_san_buf.len = tag_len;
1288
1289 if ((tmp_san_buf.tag & MBEDTLS_ASN1_TAG_CLASS_MASK) !=
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001290 MBEDTLS_ASN1_CONTEXT_SPECIFIC) {
1291 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1292 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
1293 }
1294
1295 /*
Andrzej Kurek154a6052023-04-30 14:11:49 -04001296 * Check that the SAN is structured correctly by parsing it.
1297 * The SAN structure is discarded afterwards.
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001298 */
Andrzej Kurek154a6052023-04-30 14:11:49 -04001299 ret = mbedtls_x509_parse_subject_alt_name(&tmp_san_buf, &tmp_san_name);
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001300 /*
1301 * In case the extension is malformed, return an error,
1302 * and clear the allocated sequences.
1303 */
1304 if (ret != 0 && ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE) {
1305 mbedtls_asn1_sequence_free(subject_alt_name->next);
1306 subject_alt_name->next = NULL;
1307 return ret;
1308 }
1309
Andrzej Kurek154a6052023-04-30 14:11:49 -04001310 mbedtls_x509_free_subject_alt_name(&tmp_san_name);
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001311 /* Allocate and assign next pointer */
1312 if (cur->buf.p != NULL) {
1313 if (cur->next != NULL) {
1314 return MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
1315 }
1316
1317 cur->next = mbedtls_calloc(1, sizeof(mbedtls_asn1_sequence));
1318
1319 if (cur->next == NULL) {
1320 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1321 MBEDTLS_ERR_ASN1_ALLOC_FAILED);
1322 }
1323
1324 cur = cur->next;
1325 }
1326
Hanno Beckerae8f8c42019-09-13 12:24:56 +01001327 cur->buf = tmp_san_buf;
1328 *p += tmp_san_buf.len;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001329 }
1330
1331 /* Set final sequence entry's next pointer to NULL */
1332 cur->next = NULL;
1333
1334 if (*p != end) {
1335 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1336 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1337 }
1338
1339 return 0;
1340}
1341
Przemek Stekiel21903ec2023-02-21 08:32:37 +01001342/*
1343 * SubjectAltName ::= GeneralNames
1344 *
1345 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
1346 *
1347 * GeneralName ::= CHOICE {
1348 * otherName [0] OtherName,
1349 * rfc822Name [1] IA5String,
1350 * dNSName [2] IA5String,
1351 * x400Address [3] ORAddress,
1352 * directoryName [4] Name,
1353 * ediPartyName [5] EDIPartyName,
1354 * uniformResourceIdentifier [6] IA5String,
1355 * iPAddress [7] OCTET STRING,
1356 * registeredID [8] OBJECT IDENTIFIER }
1357 *
1358 * OtherName ::= SEQUENCE {
1359 * type-id OBJECT IDENTIFIER,
1360 * value [0] EXPLICIT ANY DEFINED BY type-id }
1361 *
1362 * EDIPartyName ::= SEQUENCE {
1363 * nameAssigner [0] DirectoryString OPTIONAL,
1364 * partyName [1] DirectoryString }
1365 *
1366 * We list all types, but use the following GeneralName types from RFC 5280:
1367 * "dnsName", "uniformResourceIdentifier" and "hardware_module_name"
1368 * of type "otherName", as defined in RFC 4108.
1369 */
1370int mbedtls_x509_get_subject_alt_name(unsigned char **p,
1371 const unsigned char *end,
1372 mbedtls_x509_sequence *subject_alt_name)
1373{
1374 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1375 size_t len;
1376
1377 /* Get main sequence tag */
1378 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
1379 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1380 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1381 }
1382
1383 if (*p + len != end) {
1384 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1385 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1386 }
1387
1388 return mbedtls_x509_get_subject_alt_name_ext(p, end, subject_alt_name);
1389}
1390
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001391int mbedtls_x509_get_ns_cert_type(unsigned char **p,
1392 const unsigned char *end,
1393 unsigned char *ns_cert_type)
1394{
1395 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1396 mbedtls_x509_bitstring bs = { 0, 0, NULL };
1397
1398 if ((ret = mbedtls_asn1_get_bitstring(p, end, &bs)) != 0) {
1399 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1400 }
1401
Przemek Stekiel32e20912023-01-25 14:26:15 +01001402 /* A bitstring with no flags set is still technically valid, as it will mean
1403 that the certificate has no designated purpose at the time of creation. */
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001404 if (bs.len == 0) {
1405 *ns_cert_type = 0;
1406 return 0;
1407 }
1408
1409 if (bs.len != 1) {
1410 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1411 MBEDTLS_ERR_ASN1_INVALID_LENGTH);
1412 }
1413
1414 /* Get actual bitstring */
1415 *ns_cert_type = *bs.p;
1416 return 0;
1417}
1418
1419int mbedtls_x509_get_key_usage(unsigned char **p,
1420 const unsigned char *end,
1421 unsigned int *key_usage)
1422{
1423 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1424 size_t i;
1425 mbedtls_x509_bitstring bs = { 0, 0, NULL };
1426
1427 if ((ret = mbedtls_asn1_get_bitstring(p, end, &bs)) != 0) {
1428 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1429 }
1430
Przemek Stekiel32e20912023-01-25 14:26:15 +01001431 /* A bitstring with no flags set is still technically valid, as it will mean
1432 that the certificate has no designated purpose at the time of creation. */
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001433 if (bs.len == 0) {
1434 *key_usage = 0;
1435 return 0;
1436 }
1437
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001438 /* Get actual bitstring */
1439 *key_usage = 0;
1440 for (i = 0; i < bs.len && i < sizeof(unsigned int); i++) {
1441 *key_usage |= (unsigned int) bs.p[i] << (8*i);
1442 }
1443
1444 return 0;
1445}
1446
1447int mbedtls_x509_parse_subject_alt_name(const mbedtls_x509_buf *san_buf,
1448 mbedtls_x509_subject_alternative_name *san)
1449{
1450 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1451 switch (san_buf->tag &
1452 (MBEDTLS_ASN1_TAG_CLASS_MASK |
1453 MBEDTLS_ASN1_TAG_VALUE_MASK)) {
1454 /*
1455 * otherName
1456 */
1457 case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME):
1458 {
1459 mbedtls_x509_san_other_name other_name;
1460
1461 ret = x509_get_other_name(san_buf, &other_name);
1462 if (ret != 0) {
1463 return ret;
1464 }
1465
1466 memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name));
1467 san->type = MBEDTLS_X509_SAN_OTHER_NAME;
1468 memcpy(&san->san.other_name,
1469 &other_name, sizeof(other_name));
1470
1471 }
1472 break;
Andrzej Kurek7a05fab2023-02-13 10:03:07 -05001473 /*
1474 * uniformResourceIdentifier
1475 */
1476 case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER):
1477 {
1478 memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name));
1479 san->type = MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001480
Andrzej Kurek7a05fab2023-02-13 10:03:07 -05001481 memcpy(&san->san.unstructured_name,
1482 san_buf, sizeof(*san_buf));
1483
1484 }
1485 break;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001486 /*
1487 * dNSName
1488 */
1489 case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_DNS_NAME):
1490 {
1491 memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name));
1492 san->type = MBEDTLS_X509_SAN_DNS_NAME;
1493
1494 memcpy(&san->san.unstructured_name,
1495 san_buf, sizeof(*san_buf));
Przemek Stekielecee12f2023-02-09 14:43:49 +01001496 }
1497 break;
Przemek Stekiel0ab5b932023-05-29 16:30:50 +02001498 /*
1499 * IP address
1500 */
1501 case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_IP_ADDRESS):
1502 {
1503 memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name));
1504 san->type = MBEDTLS_X509_SAN_IP_ADDRESS;
Przemek Stekiel01cb6eb2023-06-05 16:38:13 +02001505 // Only IPv6 (16 bytes) and IPv4 (4 bytes) types are supported
1506 if (san_buf->len == 4 || san_buf->len == 16) {
1507 memcpy(&san->san.unstructured_name,
1508 san_buf, sizeof(*san_buf));
1509 } else {
1510 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1511 }
Przemek Stekiel0ab5b932023-05-29 16:30:50 +02001512 }
1513 break;
Przemek Stekielecee12f2023-02-09 14:43:49 +01001514 /*
Andrzej Kurek154a6052023-04-30 14:11:49 -04001515 * rfc822Name
Przemek Stekielecee12f2023-02-09 14:43:49 +01001516 */
1517 case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_RFC822_NAME):
1518 {
1519 memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name));
1520 san->type = MBEDTLS_X509_SAN_RFC822_NAME;
1521 memcpy(&san->san.unstructured_name, san_buf, sizeof(*san_buf));
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001522 }
1523 break;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001524 /*
Andrzej Kureke12b01d2023-01-10 06:47:38 -05001525 * directoryName
1526 */
1527 case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_DIRECTORY_NAME):
1528 {
1529 size_t name_len;
1530 unsigned char *p = san_buf->p;
1531 memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name));
1532 san->type = MBEDTLS_X509_SAN_DIRECTORY_NAME;
1533
1534 ret = mbedtls_asn1_get_tag(&p, p + san_buf->len, &name_len,
1535 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE);
1536
1537 if (ret != 0) {
Andrzej Kurekbf8ccd82023-02-13 07:13:30 -05001538 return ret;
Andrzej Kureke12b01d2023-01-10 06:47:38 -05001539 }
1540
1541 if ((ret = mbedtls_x509_get_name(&p, p + name_len,
1542 &san->san.directory_name)) != 0) {
Andrzej Kurekbf8ccd82023-02-13 07:13:30 -05001543 return ret;
Andrzej Kureke12b01d2023-01-10 06:47:38 -05001544 }
1545 }
1546 break;
1547 /*
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001548 * Type not supported
1549 */
1550 default:
1551 return MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
1552 }
1553 return 0;
1554}
1555
Andrzej Kurekd40c2b62023-02-13 07:01:59 -05001556void mbedtls_x509_free_subject_alt_name(mbedtls_x509_subject_alternative_name *san)
1557{
1558 if (san->type == MBEDTLS_X509_SAN_DIRECTORY_NAME) {
1559 mbedtls_asn1_free_named_data_list_shallow(san->san.directory_name.next);
1560 }
1561}
1562
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001563#if !defined(MBEDTLS_X509_REMOVE_INFO)
1564int mbedtls_x509_info_subject_alt_name(char **buf, size_t *size,
1565 const mbedtls_x509_sequence
1566 *subject_alt_name,
1567 const char *prefix)
1568{
1569 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1570 size_t i;
1571 size_t n = *size;
1572 char *p = *buf;
1573 const mbedtls_x509_sequence *cur = subject_alt_name;
1574 mbedtls_x509_subject_alternative_name san;
1575 int parse_ret;
1576
1577 while (cur != NULL) {
1578 memset(&san, 0, sizeof(san));
1579 parse_ret = mbedtls_x509_parse_subject_alt_name(&cur->buf, &san);
1580 if (parse_ret != 0) {
1581 if (parse_ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE) {
1582 ret = mbedtls_snprintf(p, n, "\n%s <unsupported>", prefix);
1583 MBEDTLS_X509_SAFE_SNPRINTF;
1584 } else {
1585 ret = mbedtls_snprintf(p, n, "\n%s <malformed>", prefix);
1586 MBEDTLS_X509_SAFE_SNPRINTF;
1587 }
1588 cur = cur->next;
1589 continue;
1590 }
1591
1592 switch (san.type) {
1593 /*
1594 * otherName
1595 */
1596 case MBEDTLS_X509_SAN_OTHER_NAME:
1597 {
1598 mbedtls_x509_san_other_name *other_name = &san.san.other_name;
1599
1600 ret = mbedtls_snprintf(p, n, "\n%s otherName :", prefix);
1601 MBEDTLS_X509_SAFE_SNPRINTF;
1602
1603 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ON_HW_MODULE_NAME,
David Horstmanncfae6a12023-08-18 19:12:59 +01001604 &other_name->type_id) == 0) {
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001605 ret = mbedtls_snprintf(p, n, "\n%s hardware module name :", prefix);
1606 MBEDTLS_X509_SAFE_SNPRINTF;
1607 ret =
1608 mbedtls_snprintf(p, n, "\n%s hardware type : ", prefix);
1609 MBEDTLS_X509_SAFE_SNPRINTF;
1610
1611 ret = mbedtls_oid_get_numeric_string(p,
1612 n,
1613 &other_name->value.hardware_module_name.oid);
1614 MBEDTLS_X509_SAFE_SNPRINTF;
1615
1616 ret =
1617 mbedtls_snprintf(p, n, "\n%s hardware serial number : ", prefix);
1618 MBEDTLS_X509_SAFE_SNPRINTF;
1619
1620 for (i = 0; i < other_name->value.hardware_module_name.val.len; i++) {
1621 ret = mbedtls_snprintf(p,
1622 n,
1623 "%02X",
1624 other_name->value.hardware_module_name.val.p[i]);
1625 MBEDTLS_X509_SAFE_SNPRINTF;
1626 }
1627 }/* MBEDTLS_OID_ON_HW_MODULE_NAME */
1628 }
1629 break;
Andrzej Kurek7a05fab2023-02-13 10:03:07 -05001630 /*
1631 * uniformResourceIdentifier
1632 */
1633 case MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER:
1634 {
1635 ret = mbedtls_snprintf(p, n, "\n%s uniformResourceIdentifier : ", prefix);
1636 MBEDTLS_X509_SAFE_SNPRINTF;
1637 if (san.san.unstructured_name.len >= n) {
Przemek Stekiel01cb6eb2023-06-05 16:38:13 +02001638 if (n > 0) {
1639 *p = '\0';
1640 }
Andrzej Kurek7a05fab2023-02-13 10:03:07 -05001641 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1642 }
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001643
Andrzej Kurek7a05fab2023-02-13 10:03:07 -05001644 memcpy(p, san.san.unstructured_name.p, san.san.unstructured_name.len);
1645 p += san.san.unstructured_name.len;
1646 n -= san.san.unstructured_name.len;
1647 }
1648 break;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001649 /*
1650 * dNSName
Przemek Stekiel5b9e4162023-02-15 12:56:37 +01001651 * RFC822 Name
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001652 */
1653 case MBEDTLS_X509_SAN_DNS_NAME:
Przemek Stekiel5b9e4162023-02-15 12:56:37 +01001654 case MBEDTLS_X509_SAN_RFC822_NAME:
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001655 {
Przemek Stekiel82d250d2023-02-15 15:00:50 +01001656 const char *dns_name = "dNSName";
1657 const char *rfc822_name = "rfc822Name";
Przemek Stekiel5b9e4162023-02-15 12:56:37 +01001658
Przemek Stekiel82d250d2023-02-15 15:00:50 +01001659 ret = mbedtls_snprintf(p, n,
1660 "\n%s %s : ",
1661 prefix,
Przemek Stekiel093c97d2023-06-02 10:11:32 +02001662 san.type ==
1663 MBEDTLS_X509_SAN_DNS_NAME ? dns_name : rfc822_name);
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001664 MBEDTLS_X509_SAFE_SNPRINTF;
1665 if (san.san.unstructured_name.len >= n) {
Przemek Stekiel01cb6eb2023-06-05 16:38:13 +02001666 if (n > 0) {
1667 *p = '\0';
1668 }
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001669 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1670 }
1671
Przemek Stekiel093c97d2023-06-02 10:11:32 +02001672 memcpy(p, san.san.unstructured_name.p, san.san.unstructured_name.len);
1673 p += san.san.unstructured_name.len;
1674 n -= san.san.unstructured_name.len;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001675 }
1676 break;
Przemek Stekiel093c97d2023-06-02 10:11:32 +02001677 /*
1678 * iPAddress
1679 */
1680 case MBEDTLS_X509_SAN_IP_ADDRESS:
1681 {
1682 ret = mbedtls_snprintf(p, n, "\n%s %s : ",
1683 prefix, "iPAddress");
1684 MBEDTLS_X509_SAFE_SNPRINTF;
1685 if (san.san.unstructured_name.len >= n) {
Przemek Stekiel01cb6eb2023-06-05 16:38:13 +02001686 if (n > 0) {
1687 *p = '\0';
1688 }
Przemek Stekiel093c97d2023-06-02 10:11:32 +02001689 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1690 }
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001691
Przemek Stekiel093c97d2023-06-02 10:11:32 +02001692 unsigned char *ip = san.san.unstructured_name.p;
1693 // Only IPv6 (16 bytes) and IPv4 (4 bytes) types are supported
1694 if (san.san.unstructured_name.len == 4) {
Przemek Stekiel4d3fc212023-06-06 11:40:32 +02001695 ret = mbedtls_snprintf(p, n, "%u.%u.%u.%u", ip[0], ip[1], ip[2], ip[3]);
1696 MBEDTLS_X509_SAFE_SNPRINTF;
Przemek Stekiel093c97d2023-06-02 10:11:32 +02001697 } else if (san.san.unstructured_name.len == 16) {
Przemek Stekiel4d3fc212023-06-06 11:40:32 +02001698 ret = mbedtls_snprintf(p, n,
Przemek Stekiel01cb6eb2023-06-05 16:38:13 +02001699 "%X%X:%X%X:%X%X:%X%X:%X%X:%X%X:%X%X:%X%X",
1700 ip[0], ip[1], ip[2], ip[3], ip[4], ip[5], ip[6],
Przemek Stekiel4d3fc212023-06-06 11:40:32 +02001701 ip[7], ip[8], ip[9], ip[10], ip[11], ip[12], ip[13],
Przemek Stekiel01cb6eb2023-06-05 16:38:13 +02001702 ip[14], ip[15]);
Przemek Stekiel4d3fc212023-06-06 11:40:32 +02001703 MBEDTLS_X509_SAFE_SNPRINTF;
Przemek Stekiel093c97d2023-06-02 10:11:32 +02001704 } else {
Przemek Stekiel01cb6eb2023-06-05 16:38:13 +02001705 if (n > 0) {
1706 *p = '\0';
1707 }
Przemek Stekiel093c97d2023-06-02 10:11:32 +02001708 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1709 }
Przemek Stekiel093c97d2023-06-02 10:11:32 +02001710 }
1711 break;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001712 /*
Andrzej Kureke12b01d2023-01-10 06:47:38 -05001713 * directoryName
1714 */
1715 case MBEDTLS_X509_SAN_DIRECTORY_NAME:
1716 {
1717 ret = mbedtls_snprintf(p, n, "\n%s directoryName : ", prefix);
Andrzej Kurek5f0c6e82023-02-27 16:03:41 -05001718 if (ret < 0 || (size_t) ret >= n) {
1719 mbedtls_x509_free_subject_alt_name(&san);
1720 }
1721
Andrzej Kureke12b01d2023-01-10 06:47:38 -05001722 MBEDTLS_X509_SAFE_SNPRINTF;
1723 ret = mbedtls_x509_dn_gets(p, n, &san.san.directory_name);
Andrzej Kurekd40c2b62023-02-13 07:01:59 -05001724
Andrzej Kureke12b01d2023-01-10 06:47:38 -05001725 if (ret < 0) {
Andrzej Kurek5f0c6e82023-02-27 16:03:41 -05001726 mbedtls_x509_free_subject_alt_name(&san);
Przemek Stekiel01cb6eb2023-06-05 16:38:13 +02001727 if (n > 0) {
1728 *p = '\0';
1729 }
Andrzej Kureke12b01d2023-01-10 06:47:38 -05001730 return ret;
1731 }
1732
1733 p += ret;
1734 n -= ret;
1735 }
1736 break;
1737 /*
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001738 * Type not supported, skip item.
1739 */
1740 default:
1741 ret = mbedtls_snprintf(p, n, "\n%s <unsupported>", prefix);
1742 MBEDTLS_X509_SAFE_SNPRINTF;
1743 break;
1744 }
1745
Andrzej Kurekd40c2b62023-02-13 07:01:59 -05001746 /* So far memory is freed only in the case of directoryName
Andrzej Kurek5f0c6e82023-02-27 16:03:41 -05001747 * parsing succeeding, as mbedtls_x509_get_name allocates memory. */
Andrzej Kurekd40c2b62023-02-13 07:01:59 -05001748 mbedtls_x509_free_subject_alt_name(&san);
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001749 cur = cur->next;
1750 }
1751
1752 *p = '\0';
1753
1754 *size = n;
1755 *buf = p;
1756
1757 return 0;
1758}
1759
Demi Marie Obenour690b8c92022-12-04 04:24:22 -05001760#define PRINT_ITEM(i) \
1761 do { \
1762 ret = mbedtls_snprintf(p, n, "%s" i, sep); \
1763 MBEDTLS_X509_SAFE_SNPRINTF; \
1764 sep = ", "; \
1765 } while (0)
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001766
Demi Marie Obenour690b8c92022-12-04 04:24:22 -05001767#define CERT_TYPE(type, name) \
1768 do { \
1769 if (ns_cert_type & (type)) { \
1770 PRINT_ITEM(name); \
1771 } \
1772 } while (0)
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001773
1774int mbedtls_x509_info_cert_type(char **buf, size_t *size,
1775 unsigned char ns_cert_type)
1776{
1777 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1778 size_t n = *size;
1779 char *p = *buf;
1780 const char *sep = "";
1781
1782 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT, "SSL Client");
1783 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER, "SSL Server");
1784 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_EMAIL, "Email");
1785 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing");
1786 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_RESERVED, "Reserved");
1787 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_SSL_CA, "SSL CA");
1788 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA, "Email CA");
1789 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA");
1790
1791 *size = n;
1792 *buf = p;
1793
1794 return 0;
1795}
1796
Demi Marie Obenour690b8c92022-12-04 04:24:22 -05001797#define KEY_USAGE(code, name) \
1798 do { \
1799 if ((key_usage) & (code)) { \
1800 PRINT_ITEM(name); \
1801 } \
1802 } while (0)
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001803
1804int mbedtls_x509_info_key_usage(char **buf, size_t *size,
1805 unsigned int key_usage)
1806{
1807 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1808 size_t n = *size;
1809 char *p = *buf;
1810 const char *sep = "";
1811
1812 KEY_USAGE(MBEDTLS_X509_KU_DIGITAL_SIGNATURE, "Digital Signature");
1813 KEY_USAGE(MBEDTLS_X509_KU_NON_REPUDIATION, "Non Repudiation");
1814 KEY_USAGE(MBEDTLS_X509_KU_KEY_ENCIPHERMENT, "Key Encipherment");
1815 KEY_USAGE(MBEDTLS_X509_KU_DATA_ENCIPHERMENT, "Data Encipherment");
1816 KEY_USAGE(MBEDTLS_X509_KU_KEY_AGREEMENT, "Key Agreement");
1817 KEY_USAGE(MBEDTLS_X509_KU_KEY_CERT_SIGN, "Key Cert Sign");
1818 KEY_USAGE(MBEDTLS_X509_KU_CRL_SIGN, "CRL Sign");
1819 KEY_USAGE(MBEDTLS_X509_KU_ENCIPHER_ONLY, "Encipher Only");
1820 KEY_USAGE(MBEDTLS_X509_KU_DECIPHER_ONLY, "Decipher Only");
1821
1822 *size = n;
1823 *buf = p;
1824
1825 return 0;
1826}
1827#endif /* MBEDTLS_X509_REMOVE_INFO */
1828#endif /* MBEDTLS_X509_CRT_PARSE_C || MBEDTLS_X509_CSR_PARSE_C */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001829#endif /* MBEDTLS_X509_USE_C */