blob: f315821fdf2b87b8b9b21ebef6062f88be3f41ee [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 Peskinecd4c0d72025-05-07 23:45:12 +020024#include "mbedtls/oid.h"
Gilles Peskine86a47f82025-05-07 20:20:12 +020025#include "x509_oid.h"
Rich Evans00ab4702015-02-06 13:43:58 +000026
Rich Evans36796df2015-02-12 18:27:14 +000027#include <stdio.h>
Rich Evans00ab4702015-02-06 13:43:58 +000028#include <string.h>
29
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000031#include "mbedtls/pem.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020032#endif
33
Agathiyan Bragadeeshddc720d2023-07-26 15:51:49 +010034#include "mbedtls/asn1write.h"
Agathiyan Bragadeeshddc720d2023-07-26 15:51:49 +010035
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000036#include "mbedtls/platform.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020037
Simon Butcherb5b6af22016-07-13 14:46:18 +010038#if defined(MBEDTLS_HAVE_TIME)
39#include "mbedtls/platform_time.h"
40#endif
Nicholas Wilson512b4ee2017-12-05 12:07:33 +000041#if defined(MBEDTLS_HAVE_TIME_DATE)
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +010042#include "mbedtls/platform_util.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020043#include <time.h>
44#endif
45
Demi Marie Obenour690b8c92022-12-04 04:24:22 -050046#define CHECK(code) \
47 do { \
48 if ((ret = (code)) != 0) { \
49 return ret; \
50 } \
51 } while (0)
52
Hanno Becker1eeca412018-10-15 12:01:35 +010053#define CHECK_RANGE(min, max, val) \
Demi Marie Obenour690b8c92022-12-04 04:24:22 -050054 do { \
55 if ((val) < (min) || (val) > (max)) { \
56 return ret; \
Hanno Becker1eeca412018-10-15 12:01:35 +010057 } \
Gilles Peskine449bd832023-01-11 14:50:10 +010058 } while (0)
Rich Evans7d5a55a2015-02-13 11:48:02 +000059
Paul Bakker7c6b2c32013-09-16 13:49:26 +020060/*
61 * CertificateSerialNumber ::= INTEGER
62 */
Gilles Peskine449bd832023-01-11 14:50:10 +010063int mbedtls_x509_get_serial(unsigned char **p, const unsigned char *end,
64 mbedtls_x509_buf *serial)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020065{
Janos Follath865b3eb2019-12-16 11:46:15 +000066 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020067
Gilles Peskine449bd832023-01-11 14:50:10 +010068 if ((end - *p) < 1) {
69 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_SERIAL,
70 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
71 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +020072
Gilles Peskine449bd832023-01-11 14:50:10 +010073 if (**p != (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_PRIMITIVE | 2) &&
74 **p != MBEDTLS_ASN1_INTEGER) {
75 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_SERIAL,
76 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
77 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +020078
79 serial->tag = *(*p)++;
80
Gilles Peskine449bd832023-01-11 14:50:10 +010081 if ((ret = mbedtls_asn1_get_len(p, end, &serial->len)) != 0) {
82 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_SERIAL, ret);
83 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +020084
85 serial->p = *p;
86 *p += serial->len;
87
Gilles Peskine449bd832023-01-11 14:50:10 +010088 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020089}
90
91/* Get an algorithm identifier without parameters (eg for signatures)
92 *
93 * AlgorithmIdentifier ::= SEQUENCE {
94 * algorithm OBJECT IDENTIFIER,
95 * parameters ANY DEFINED BY algorithm OPTIONAL }
96 */
Gilles Peskine449bd832023-01-11 14:50:10 +010097int mbedtls_x509_get_alg_null(unsigned char **p, const unsigned char *end,
98 mbedtls_x509_buf *alg)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020099{
Janos Follath865b3eb2019-12-16 11:46:15 +0000100 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200101
Gilles Peskine449bd832023-01-11 14:50:10 +0100102 if ((ret = mbedtls_asn1_get_alg_null(p, end, alg)) != 0) {
103 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
104 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200105
Gilles Peskine449bd832023-01-11 14:50:10 +0100106 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200107}
108
109/*
Antonin Décimo36e89b52019-01-23 15:24:37 +0100110 * Parse an algorithm identifier with (optional) parameters
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100111 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100112int mbedtls_x509_get_alg(unsigned char **p, const unsigned char *end,
113 mbedtls_x509_buf *alg, mbedtls_x509_buf *params)
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100114{
Janos Follath865b3eb2019-12-16 11:46:15 +0000115 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100116
Gilles Peskine449bd832023-01-11 14:50:10 +0100117 if ((ret = mbedtls_asn1_get_alg(p, end, alg, params)) != 0) {
118 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
119 }
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100120
Gilles Peskine449bd832023-01-11 14:50:10 +0100121 return 0;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100122}
123
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200124/*
125 * Convert md type to string
126 */
Dave Rodgmanffabb7b2023-06-28 16:22:50 +0100127#if !defined(MBEDTLS_X509_REMOVE_INFO) && defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
128
Gilles Peskine449bd832023-01-11 14:50:10 +0100129static inline const char *md_type_to_string(mbedtls_md_type_t md_alg)
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200130{
Gilles Peskine449bd832023-01-11 14:50:10 +0100131 switch (md_alg) {
Elena Uziunaiteb66a9912024-05-10 14:25:58 +0100132#if defined(PSA_WANT_ALG_MD5)
Gilles Peskine449bd832023-01-11 14:50:10 +0100133 case MBEDTLS_MD_MD5:
134 return "MD5";
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200135#endif
Elena Uziunaite9fc5be02024-09-04 18:12:59 +0100136#if defined(PSA_WANT_ALG_SHA_1)
Gilles Peskine449bd832023-01-11 14:50:10 +0100137 case MBEDTLS_MD_SHA1:
138 return "SHA1";
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200139#endif
Elena Uziunaitefcc9afa2024-05-23 14:43:22 +0100140#if defined(PSA_WANT_ALG_SHA_224)
Gilles Peskine449bd832023-01-11 14:50:10 +0100141 case MBEDTLS_MD_SHA224:
142 return "SHA224";
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200143#endif
Elena Uziunaite0916cd72024-05-23 17:01:07 +0100144#if defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine449bd832023-01-11 14:50:10 +0100145 case MBEDTLS_MD_SHA256:
146 return "SHA256";
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200147#endif
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +0100148#if defined(PSA_WANT_ALG_SHA_384)
Gilles Peskine449bd832023-01-11 14:50:10 +0100149 case MBEDTLS_MD_SHA384:
150 return "SHA384";
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200151#endif
Gabor Mezeic15ef932024-06-13 12:53:54 +0200152#if defined(PSA_WANT_ALG_SHA_512)
Gilles Peskine449bd832023-01-11 14:50:10 +0100153 case MBEDTLS_MD_SHA512:
154 return "SHA512";
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200155#endif
Elena Uziunaite1b6fb212024-05-10 16:17:41 +0100156#if defined(PSA_WANT_ALG_RIPEMD160)
Gilles Peskine449bd832023-01-11 14:50:10 +0100157 case MBEDTLS_MD_RIPEMD160:
158 return "RIPEMD160";
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200159#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100160 case MBEDTLS_MD_NONE:
161 return NULL;
162 default:
163 return NULL;
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200164 }
165}
166
Dave Rodgmanf032c982023-06-29 12:09:27 +0100167#endif /* !defined(MBEDTLS_X509_REMOVE_INFO) && defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) */
Dave Rodgmanffabb7b2023-06-28 16:22:50 +0100168
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200169#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100170/*
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100171 * HashAlgorithm ::= AlgorithmIdentifier
172 *
173 * AlgorithmIdentifier ::= SEQUENCE {
174 * algorithm OBJECT IDENTIFIER,
175 * parameters ANY DEFINED BY algorithm OPTIONAL }
176 *
177 * For HashAlgorithm, parameters MUST be NULL or absent.
178 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100179static 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 +0100180{
Janos Follath865b3eb2019-12-16 11:46:15 +0000181 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100182 unsigned char *p;
183 const unsigned char *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200184 mbedtls_x509_buf md_oid;
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100185 size_t len;
186
187 /* Make sure we got a SEQUENCE and setup bounds */
Gilles Peskine449bd832023-01-11 14:50:10 +0100188 if (alg->tag != (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) {
189 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
190 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
191 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100192
Andrzej Kurekfeaebc52020-07-16 04:37:41 -0400193 p = alg->p;
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100194 end = p + alg->len;
195
Gilles Peskine449bd832023-01-11 14:50:10 +0100196 if (p >= end) {
197 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
198 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
199 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100200
201 /* Parse md_oid */
202 md_oid.tag = *p;
203
Gilles Peskine449bd832023-01-11 14:50:10 +0100204 if ((ret = mbedtls_asn1_get_tag(&p, end, &md_oid.len, MBEDTLS_ASN1_OID)) != 0) {
205 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
206 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100207
208 md_oid.p = p;
209 p += md_oid.len;
210
211 /* Get md_alg from md_oid */
Gilles Peskine532e3ee2025-05-07 20:37:15 +0200212 if ((ret = mbedtls_x509_oid_get_md_alg(&md_oid, md_alg)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100213 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
214 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100215
216 /* Make sure params is absent of NULL */
Gilles Peskine449bd832023-01-11 14:50:10 +0100217 if (p == end) {
218 return 0;
219 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100220
Gilles Peskine449bd832023-01-11 14:50:10 +0100221 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_NULL)) != 0 || len != 0) {
222 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
223 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100224
Gilles Peskine449bd832023-01-11 14:50:10 +0100225 if (p != end) {
226 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
227 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
228 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100229
Gilles Peskine449bd832023-01-11 14:50:10 +0100230 return 0;
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100231}
232
233/*
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100234 * RSASSA-PSS-params ::= SEQUENCE {
235 * hashAlgorithm [0] HashAlgorithm DEFAULT sha1Identifier,
236 * maskGenAlgorithm [1] MaskGenAlgorithm DEFAULT mgf1SHA1Identifier,
237 * saltLength [2] INTEGER DEFAULT 20,
238 * trailerField [3] INTEGER DEFAULT 1 }
239 * -- Note that the tags in this Sequence are explicit.
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200240 *
241 * RFC 4055 (which defines use of RSASSA-PSS in PKIX) states that the value
242 * of trailerField MUST be 1, and PKCS#1 v2.2 doesn't even define any other
Tom Cosgrove1797b052022-12-04 17:19:59 +0000243 * option. Enforce this at parsing time.
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100244 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100245int mbedtls_x509_get_rsassa_pss_params(const mbedtls_x509_buf *params,
246 mbedtls_md_type_t *md_alg, mbedtls_md_type_t *mgf_md,
247 int *salt_len)
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100248{
Janos Follath865b3eb2019-12-16 11:46:15 +0000249 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100250 unsigned char *p;
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100251 const unsigned char *end, *end2;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100252 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253 mbedtls_x509_buf alg_id, alg_params;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100254
255 /* First set everything to defaults */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200256 *md_alg = MBEDTLS_MD_SHA1;
257 *mgf_md = MBEDTLS_MD_SHA1;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100258 *salt_len = 20;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100259
260 /* Make sure params is a SEQUENCE and setup bounds */
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 if (params->tag != (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) {
262 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
263 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
264 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100265
266 p = (unsigned char *) params->p;
267 end = p + params->len;
268
Gilles Peskine449bd832023-01-11 14:50:10 +0100269 if (p == end) {
270 return 0;
271 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100272
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100273 /*
274 * HashAlgorithm
275 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100276 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
277 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
278 0)) == 0) {
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100279 end2 = p + len;
280
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100281 /* HashAlgorithm ::= AlgorithmIdentifier (without parameters) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100282 if ((ret = mbedtls_x509_get_alg_null(&p, end2, &alg_id)) != 0) {
283 return ret;
284 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100285
Gilles Peskine532e3ee2025-05-07 20:37:15 +0200286 if ((ret = mbedtls_x509_oid_get_md_alg(&alg_id, md_alg)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100287 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
288 }
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100289
Gilles Peskine449bd832023-01-11 14:50:10 +0100290 if (p != end2) {
291 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
292 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
293 }
294 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
295 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100296 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100297
Gilles Peskine449bd832023-01-11 14:50:10 +0100298 if (p == end) {
299 return 0;
300 }
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100301
302 /*
303 * MaskGenAlgorithm
304 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100305 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
306 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
307 1)) == 0) {
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100308 end2 = p + len;
309
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100310 /* MaskGenAlgorithm ::= AlgorithmIdentifier (params = HashAlgorithm) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100311 if ((ret = mbedtls_x509_get_alg(&p, end2, &alg_id, &alg_params)) != 0) {
312 return ret;
313 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100314
315 /* Only MFG1 is recognised for now */
Gilles Peskine449bd832023-01-11 14:50:10 +0100316 if (MBEDTLS_OID_CMP(MBEDTLS_OID_MGF1, &alg_id) != 0) {
317 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE,
Gilles Peskine4c832212025-05-07 23:05:12 +0200318 MBEDTLS_ERR_X509_UNKNOWN_OID);
Gilles Peskine449bd832023-01-11 14:50:10 +0100319 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100320
321 /* Parse HashAlgorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +0100322 if ((ret = x509_get_hash_alg(&alg_params, mgf_md)) != 0) {
323 return ret;
324 }
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100325
Gilles Peskine449bd832023-01-11 14:50:10 +0100326 if (p != end2) {
327 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
328 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
329 }
330 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
331 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100332 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100333
Gilles Peskine449bd832023-01-11 14:50:10 +0100334 if (p == end) {
335 return 0;
336 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100337
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100338 /*
339 * salt_len
340 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100341 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
342 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
343 2)) == 0) {
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100344 end2 = p + len;
345
Gilles Peskine449bd832023-01-11 14:50:10 +0100346 if ((ret = mbedtls_asn1_get_int(&p, end2, salt_len)) != 0) {
347 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
348 }
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100349
Gilles Peskine449bd832023-01-11 14:50:10 +0100350 if (p != end2) {
351 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
352 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
353 }
354 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
355 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100356 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100357
Gilles Peskine449bd832023-01-11 14:50:10 +0100358 if (p == end) {
359 return 0;
360 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100361
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100362 /*
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200363 * trailer_field (if present, must be 1)
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100364 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100365 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
366 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
367 3)) == 0) {
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200368 int trailer_field;
369
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100370 end2 = p + len;
371
Gilles Peskine449bd832023-01-11 14:50:10 +0100372 if ((ret = mbedtls_asn1_get_int(&p, end2, &trailer_field)) != 0) {
373 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
374 }
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100375
Gilles Peskine449bd832023-01-11 14:50:10 +0100376 if (p != end2) {
377 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
378 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
379 }
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200380
Gilles Peskine449bd832023-01-11 14:50:10 +0100381 if (trailer_field != 1) {
382 return MBEDTLS_ERR_X509_INVALID_ALG;
383 }
384 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
385 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100386 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100387
Gilles Peskine449bd832023-01-11 14:50:10 +0100388 if (p != end) {
389 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
390 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
391 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100392
Gilles Peskine449bd832023-01-11 14:50:10 +0100393 return 0;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100394}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200395#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100396
397/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200398 * AttributeTypeAndValue ::= SEQUENCE {
399 * type AttributeType,
400 * value AttributeValue }
401 *
402 * AttributeType ::= OBJECT IDENTIFIER
403 *
404 * AttributeValue ::= ANY DEFINED BY AttributeType
405 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100406static int x509_get_attr_type_value(unsigned char **p,
407 const unsigned char *end,
408 mbedtls_x509_name *cur)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200409{
Janos Follath865b3eb2019-12-16 11:46:15 +0000410 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200411 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200412 mbedtls_x509_buf *oid;
413 mbedtls_x509_buf *val;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200414
Gilles Peskine449bd832023-01-11 14:50:10 +0100415 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
416 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
417 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME, ret);
418 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200419
Hanno Becker12f62fb2019-02-12 17:22:36 +0000420 end = *p + len;
421
Gilles Peskine449bd832023-01-11 14:50:10 +0100422 if ((end - *p) < 1) {
423 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME,
424 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
425 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200426
427 oid = &cur->oid;
428 oid->tag = **p;
429
Gilles Peskine449bd832023-01-11 14:50:10 +0100430 if ((ret = mbedtls_asn1_get_tag(p, end, &oid->len, MBEDTLS_ASN1_OID)) != 0) {
431 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME, ret);
432 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200433
434 oid->p = *p;
435 *p += oid->len;
436
Gilles Peskine449bd832023-01-11 14:50:10 +0100437 if ((end - *p) < 1) {
438 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME,
439 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
440 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200441
Gilles Peskine449bd832023-01-11 14:50:10 +0100442 if (**p != MBEDTLS_ASN1_BMP_STRING && **p != MBEDTLS_ASN1_UTF8_STRING &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200443 **p != MBEDTLS_ASN1_T61_STRING && **p != MBEDTLS_ASN1_PRINTABLE_STRING &&
444 **p != MBEDTLS_ASN1_IA5_STRING && **p != MBEDTLS_ASN1_UNIVERSAL_STRING &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100445 **p != MBEDTLS_ASN1_BIT_STRING) {
446 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME,
447 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
448 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200449
450 val = &cur->val;
451 val->tag = *(*p)++;
452
Gilles Peskine449bd832023-01-11 14:50:10 +0100453 if ((ret = mbedtls_asn1_get_len(p, end, &val->len)) != 0) {
454 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME, ret);
455 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200456
457 val->p = *p;
458 *p += val->len;
459
Gilles Peskine449bd832023-01-11 14:50:10 +0100460 if (*p != end) {
461 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME,
462 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Hanno Becker12f62fb2019-02-12 17:22:36 +0000463 }
464
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200465 cur->next = NULL;
466
Gilles Peskine449bd832023-01-11 14:50:10 +0100467 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200468}
469
470/*
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000471 * Name ::= CHOICE { -- only one possibility for now --
472 * rdnSequence RDNSequence }
473 *
474 * RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
475 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200476 * RelativeDistinguishedName ::=
477 * SET OF AttributeTypeAndValue
478 *
479 * AttributeTypeAndValue ::= SEQUENCE {
480 * type AttributeType,
481 * value AttributeValue }
482 *
483 * AttributeType ::= OBJECT IDENTIFIER
484 *
485 * AttributeValue ::= ANY DEFINED BY AttributeType
Manuel Pégourié-Gonnard5d861852014-10-17 12:41:41 +0200486 *
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000487 * The data structure is optimized for the common case where each RDN has only
488 * one element, which is represented as a list of AttributeTypeAndValue.
489 * For the general case we still use a flat list, but we mark elements of the
490 * same set so that they are "merged" together in the functions that consume
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200491 * this list, eg mbedtls_x509_dn_gets().
David Horstmanned794832022-10-04 18:12:06 +0100492 *
David Horstmann11307a12022-10-17 18:10:23 +0100493 * On success, this function may allocate a linked list starting at cur->next
David Horstmanned794832022-10-04 18:12:06 +0100494 * that must later be free'd by the caller using mbedtls_free(). In error
495 * cases, this function frees all allocated memory internally and the caller
496 * has no freeing responsibilities.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200497 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100498int mbedtls_x509_get_name(unsigned char **p, const unsigned char *end,
499 mbedtls_x509_name *cur)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200500{
Janos Follath865b3eb2019-12-16 11:46:15 +0000501 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard5d861852014-10-17 12:41:41 +0200502 size_t set_len;
503 const unsigned char *end_set;
David Horstmanned794832022-10-04 18:12:06 +0100504 mbedtls_x509_name *head = cur;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200505
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100506 /* don't use recursion, we'd risk stack overflow if not optimized */
Gilles Peskine449bd832023-01-11 14:50:10 +0100507 while (1) {
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100508 /*
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000509 * parse SET
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100510 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100511 if ((ret = mbedtls_asn1_get_tag(p, end, &set_len,
512 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SET)) != 0) {
513 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME, ret);
David Horstmanned794832022-10-04 18:12:06 +0100514 goto error;
515 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200516
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100517 end_set = *p + set_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200518
Gilles Peskine449bd832023-01-11 14:50:10 +0100519 while (1) {
520 if ((ret = x509_get_attr_type_value(p, end_set, cur)) != 0) {
David Horstmanned794832022-10-04 18:12:06 +0100521 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +0100522 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200523
Gilles Peskine449bd832023-01-11 14:50:10 +0100524 if (*p == end_set) {
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000525 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100526 }
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000527
Manuel Pégourié-Gonnardeecb43c2015-05-12 12:56:41 +0200528 /* Mark this item as being no the only one in a set */
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000529 cur->next_merged = 1;
530
Gilles Peskine449bd832023-01-11 14:50:10 +0100531 cur->next = mbedtls_calloc(1, sizeof(mbedtls_x509_name));
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000532
Gilles Peskine449bd832023-01-11 14:50:10 +0100533 if (cur->next == NULL) {
David Horstmanned794832022-10-04 18:12:06 +0100534 ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
535 goto error;
536 }
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000537
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000538 cur = cur->next;
539 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200540
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100541 /*
542 * continue until end of SEQUENCE is reached
543 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100544 if (*p == end) {
545 return 0;
546 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200547
Gilles Peskine449bd832023-01-11 14:50:10 +0100548 cur->next = mbedtls_calloc(1, sizeof(mbedtls_x509_name));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200549
Gilles Peskine449bd832023-01-11 14:50:10 +0100550 if (cur->next == NULL) {
David Horstmanned794832022-10-04 18:12:06 +0100551 ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
552 goto error;
553 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200554
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100555 cur = cur->next;
556 }
David Horstmanned794832022-10-04 18:12:06 +0100557
558error:
David Horstmanned794832022-10-04 18:12:06 +0100559 /* Skip the first element as we did not allocate it */
Gilles Peskine449bd832023-01-11 14:50:10 +0100560 mbedtls_asn1_free_named_data_list_shallow(head->next);
Glenn Straussa4b40412022-06-26 19:32:09 -0400561 head->next = NULL;
David Horstmanned794832022-10-04 18:12:06 +0100562
Gilles Peskine449bd832023-01-11 14:50:10 +0100563 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200564}
565
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400566static int x509_date_is_valid(const mbedtls_x509_time *t)
Janos Follath87c98072017-02-03 12:36:59 +0000567{
David Horstmanncdf52832023-07-05 09:58:03 +0100568 unsigned int month_days;
569 unsigned int year;
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400570 switch (t->mon) {
571 case 1: case 3: case 5: case 7: case 8: case 10: case 12:
David Horstmanncdf52832023-07-05 09:58:03 +0100572 month_days = 31;
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400573 break;
574 case 4: case 6: case 9: case 11:
David Horstmanncdf52832023-07-05 09:58:03 +0100575 month_days = 30;
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400576 break;
577 case 2:
David Horstmanncdf52832023-07-05 09:58:03 +0100578 year = (unsigned int) t->year;
579 month_days = ((year & 3) || (!(year % 100)
580 && (year % 400)))
581 ? 28 : 29;
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400582 break;
583 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100584 return MBEDTLS_ERR_X509_INVALID_DATE;
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400585 }
Janos Follath87c98072017-02-03 12:36:59 +0000586
David Horstmannb1d27bc2023-07-05 10:00:31 +0100587 if ((unsigned int) (t->day - 1) >= month_days || /* (1 - days in month) */
David Horstmann3ae1c4c2023-07-05 11:15:08 +0100588 /* (unsigned int) (t->mon - 1) >= 12 || */ /* (1 - 12) checked above */
David Horstmannb1d27bc2023-07-05 10:00:31 +0100589 (unsigned int) t->year > 9999 || /* (0 - 9999) */
590 (unsigned int) t->hour > 23 || /* (0 - 23) */
591 (unsigned int) t->min > 59 || /* (0 - 59) */
592 (unsigned int) t->sec > 59) { /* (0 - 59) */
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400593 return MBEDTLS_ERR_X509_INVALID_DATE;
Rich Evans7d5a55a2015-02-13 11:48:02 +0000594 }
Janos Follath87c98072017-02-03 12:36:59 +0000595
Gilles Peskine449bd832023-01-11 14:50:10 +0100596 return 0;
Rich Evans7d5a55a2015-02-13 11:48:02 +0000597}
598
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400599static int x509_parse2_int(const unsigned char *p)
Andres AG4b76aec2016-09-23 13:16:02 +0100600{
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400601 uint32_t d1 = p[0] - '0';
602 uint32_t d2 = p[1] - '0';
603 return (d1 < 10 && d2 < 10) ? (int) (d1 * 10 + d2) : -1;
Andres AG4b76aec2016-09-23 13:16:02 +0100604}
605
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200606/*
Janos Follath87c98072017-02-03 12:36:59 +0000607 * Parse an ASN1_UTC_TIME (yearlen=2) or ASN1_GENERALIZED_TIME (yearlen=4)
608 * field.
609 */
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400610static int x509_parse_time(const unsigned char *p, mbedtls_x509_time *tm,
611 size_t yearlen)
Janos Follath87c98072017-02-03 12:36:59 +0000612{
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400613 int x;
Janos Follath87c98072017-02-03 12:36:59 +0000614
615 /*
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400616 * Parse year, month, day, hour, minute, second
Janos Follath87c98072017-02-03 12:36:59 +0000617 */
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400618 tm->year = x509_parse2_int(p);
619 if (tm->year < 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100620 return MBEDTLS_ERR_X509_INVALID_DATE;
621 }
Janos Follath87c98072017-02-03 12:36:59 +0000622
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400623 if (4 == yearlen) {
624 x = tm->year * 100;
625 p += 2;
626 tm->year = x509_parse2_int(p);
627 if (tm->year < 0) {
628 return MBEDTLS_ERR_X509_INVALID_DATE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100629 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100630 } else {
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400631 x = (tm->year < 50) ? 2000 : 1900;
Janos Follath87c98072017-02-03 12:36:59 +0000632 }
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400633 tm->year += x;
Janos Follath87c98072017-02-03 12:36:59 +0000634
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400635 tm->mon = x509_parse2_int(p + 2);
636 tm->day = x509_parse2_int(p + 4);
637 tm->hour = x509_parse2_int(p + 6);
638 tm->min = x509_parse2_int(p + 8);
639 tm->sec = x509_parse2_int(p + 10);
Janos Follath87c98072017-02-03 12:36:59 +0000640
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400641 return x509_date_is_valid(tm);
Janos Follath87c98072017-02-03 12:36:59 +0000642}
643
644/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200645 * Time ::= CHOICE {
646 * utcTime UTCTime,
647 * generalTime GeneralizedTime }
648 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100649int mbedtls_x509_get_time(unsigned char **p, const unsigned char *end,
650 mbedtls_x509_time *tm)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200651{
Janos Follath865b3eb2019-12-16 11:46:15 +0000652 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Janos Follath87c98072017-02-03 12:36:59 +0000653 size_t len, year_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200654 unsigned char tag;
655
Gilles Peskine449bd832023-01-11 14:50:10 +0100656 if ((end - *p) < 1) {
657 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE,
658 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
659 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200660
661 tag = **p;
662
Gilles Peskine449bd832023-01-11 14:50:10 +0100663 if (tag == MBEDTLS_ASN1_UTC_TIME) {
Janos Follath87c98072017-02-03 12:36:59 +0000664 year_len = 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100665 } else if (tag == MBEDTLS_ASN1_GENERALIZED_TIME) {
Janos Follath87c98072017-02-03 12:36:59 +0000666 year_len = 4;
Gilles Peskine449bd832023-01-11 14:50:10 +0100667 } else {
668 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE,
669 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
670 }
Janos Follath87c98072017-02-03 12:36:59 +0000671
672 (*p)++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100673 ret = mbedtls_asn1_get_len(p, end, &len);
Janos Follath87c98072017-02-03 12:36:59 +0000674
Gilles Peskine449bd832023-01-11 14:50:10 +0100675 if (ret != 0) {
676 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE, ret);
677 }
Janos Follath87c98072017-02-03 12:36:59 +0000678
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400679 /* len is 12 or 14 depending on year_len, plus optional trailing 'Z' */
680 if (len != year_len + 10 &&
681 !(len == year_len + 11 && (*p)[(len - 1)] == 'Z')) {
682 return MBEDTLS_ERR_X509_INVALID_DATE;
683 }
684
685 (*p) += len;
686 return x509_parse_time(*p - len, tm, year_len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200687}
688
Gilles Peskine449bd832023-01-11 14:50:10 +0100689int mbedtls_x509_get_sig(unsigned char **p, const unsigned char *end, mbedtls_x509_buf *sig)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200690{
Janos Follath865b3eb2019-12-16 11:46:15 +0000691 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200692 size_t len;
Andres AG4bdbe092016-09-19 16:58:45 +0100693 int tag_type;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200694
Gilles Peskine449bd832023-01-11 14:50:10 +0100695 if ((end - *p) < 1) {
696 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_SIGNATURE,
697 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
698 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200699
Andres AG4bdbe092016-09-19 16:58:45 +0100700 tag_type = **p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200701
Gilles Peskine449bd832023-01-11 14:50:10 +0100702 if ((ret = mbedtls_asn1_get_bitstring_null(p, end, &len)) != 0) {
703 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_SIGNATURE, ret);
704 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200705
Andres AG4bdbe092016-09-19 16:58:45 +0100706 sig->tag = tag_type;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200707 sig->len = len;
708 sig->p = *p;
709
710 *p += len;
711
Gilles Peskine449bd832023-01-11 14:50:10 +0100712 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200713}
714
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100715/*
716 * Get signature algorithm from alg OID and optional parameters
717 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100718int mbedtls_x509_get_sig_alg(const mbedtls_x509_buf *sig_oid, const mbedtls_x509_buf *sig_params,
Valerio Setti68878cc2025-04-10 23:30:26 +0200719 mbedtls_md_type_t *md_alg, mbedtls_pk_type_t *pk_alg)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200720{
Janos Follath865b3eb2019-12-16 11:46:15 +0000721 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200722
Gilles Peskine532e3ee2025-05-07 20:37:15 +0200723 if ((ret = mbedtls_x509_oid_get_sig_alg(sig_oid, md_alg, pk_alg)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100724 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG, ret);
725 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200727#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100728 if (*pk_alg == MBEDTLS_PK_RSASSA_PSS) {
Valerio Setti68878cc2025-04-10 23:30:26 +0200729 mbedtls_md_type_t mgf1_hash_id;
730 int expected_salt_len;
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200731
Gilles Peskine449bd832023-01-11 14:50:10 +0100732 ret = mbedtls_x509_get_rsassa_pss_params(sig_params,
733 md_alg,
Valerio Setti68878cc2025-04-10 23:30:26 +0200734 &mgf1_hash_id,
735 &expected_salt_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100736 if (ret != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100737 return ret;
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200738 }
Valerio Setti68878cc2025-04-10 23:30:26 +0200739 /* Ensure MGF1 hash alg is the same as the one used to hash the message. */
740 if (mgf1_hash_id != *md_alg) {
741 return MBEDTLS_ERR_X509_INVALID_ALG;
742 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100743 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200744#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100745 {
746 /* Make sure parameters are absent or NULL */
Gilles Peskine449bd832023-01-11 14:50:10 +0100747 if ((sig_params->tag != MBEDTLS_ASN1_NULL && sig_params->tag != 0) ||
748 sig_params->len != 0) {
749 return MBEDTLS_ERR_X509_INVALID_ALG;
750 }
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100751 }
752
Gilles Peskine449bd832023-01-11 14:50:10 +0100753 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200754}
755
756/*
757 * X.509 Extensions (No parsing of extensions, pointer should
Brian J Murray1903fb32016-11-06 04:45:15 -0800758 * be either manually updated or extensions should be parsed!)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200759 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100760int mbedtls_x509_get_ext(unsigned char **p, const unsigned char *end,
761 mbedtls_x509_buf *ext, int tag)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200762{
Janos Follath865b3eb2019-12-16 11:46:15 +0000763 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200764 size_t len;
765
Hanno Becker3cddba82019-02-11 14:33:36 +0000766 /* Extension structure use EXPLICIT tagging. That is, the actual
767 * `Extensions` structure is wrapped by a tag-length pair using
768 * the respective context-specific tag. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100769 ret = mbedtls_asn1_get_tag(p, end, &ext->len,
770 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | tag);
771 if (ret != 0) {
772 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
773 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200774
Hanno Becker6ccfb182019-02-12 11:52:10 +0000775 ext->tag = MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | tag;
776 ext->p = *p;
777 end = *p + ext->len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200778
779 /*
780 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200781 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100782 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
783 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
784 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
785 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200786
Gilles Peskine449bd832023-01-11 14:50:10 +0100787 if (end != *p + len) {
788 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
789 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
790 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200791
Gilles Peskine449bd832023-01-11 14:50:10 +0100792 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200793}
794
Agathiyan Bragadeesh9d2507c2023-07-24 16:35:57 +0100795static char nibble_to_hex_digit(int i)
Agathiyan Bragadeeshef2decb2023-07-21 15:47:47 +0100796{
Agathiyan Bragadeeshf0e1ac52023-07-24 16:43:36 +0100797 return (i < 10) ? (i + '0') : (i - 10 + 'A');
Agathiyan Bragadeeshef2decb2023-07-21 15:47:47 +0100798}
799
Sam Berry4f761942024-07-19 14:56:30 +0100800/* Return the x.y.z.... style numeric string for the given OID */
801int mbedtls_oid_get_numeric_string(char *buf, size_t size,
802 const mbedtls_asn1_buf *oid)
803{
804 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
805 char *p = buf;
806 size_t n = size;
807 unsigned int value = 0;
808
809 if (size > INT_MAX) {
810 /* Avoid overflow computing return value */
811 return MBEDTLS_ERR_ASN1_INVALID_LENGTH;
812 }
813
814 if (oid->len <= 0) {
815 /* OID must not be empty */
816 return MBEDTLS_ERR_ASN1_OUT_OF_DATA;
817 }
818
819 for (size_t i = 0; i < oid->len; i++) {
820 /* Prevent overflow in value. */
821 if (value > (UINT_MAX >> 7)) {
822 return MBEDTLS_ERR_ASN1_INVALID_DATA;
823 }
824 if ((value == 0) && ((oid->p[i]) == 0x80)) {
825 /* Overlong encoding is not allowed */
826 return MBEDTLS_ERR_ASN1_INVALID_DATA;
827 }
828
829 value <<= 7;
830 value |= oid->p[i] & 0x7F;
831
832 if (!(oid->p[i] & 0x80)) {
833 /* Last byte */
834 if (n == size) {
835 int component1;
836 unsigned int component2;
837 /* First subidentifier contains first two OID components */
838 if (value >= 80) {
839 component1 = '2';
840 component2 = value - 80;
841 } else if (value >= 40) {
842 component1 = '1';
843 component2 = value - 40;
844 } else {
845 component1 = '0';
846 component2 = value;
847 }
848 ret = mbedtls_snprintf(p, n, "%c.%u", component1, component2);
849 } else {
850 ret = mbedtls_snprintf(p, n, ".%u", value);
851 }
852 if (ret < 2 || (size_t) ret >= n) {
Gilles Peskine47f1d7b2025-05-07 21:04:51 +0200853 return PSA_ERROR_BUFFER_TOO_SMALL;
Sam Berry4f761942024-07-19 14:56:30 +0100854 }
855 n -= (size_t) ret;
856 p += ret;
857 value = 0;
858 }
859 }
860
861 if (value != 0) {
862 /* Unterminated subidentifier */
863 return MBEDTLS_ERR_ASN1_OUT_OF_DATA;
864 }
865
866 return (int) (size - n);
867}
868
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200869/*
870 * Store the name in printable form into buf; no more
871 * than size characters will be written
872 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100873int mbedtls_x509_dn_gets(char *buf, size_t size, const mbedtls_x509_name *dn)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200874{
Janos Follath865b3eb2019-12-16 11:46:15 +0000875 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh5adffb22023-08-10 15:50:57 +0100876 size_t i, j, n, asn1_len_size, asn1_tag_size, asn1_tag_len_buf_start;
Agathiyan Bragadeeshc7959b22023-09-12 17:54:43 +0100877 /* 6 is enough as our asn1 write functions only write one byte for the tag and at most five bytes for the length*/
878 unsigned char asn1_tag_len_buf[6];
Agathiyan Bragadeeshddc720d2023-07-26 15:51:49 +0100879 unsigned char *asn1_len_p;
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000880 unsigned char c, merge = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200881 const mbedtls_x509_name *name;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200882 const char *short_name = NULL;
Agathiyan Bragadeesha1f5c2d2023-08-02 17:08:52 +0100883 char lowbits, highbits;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200884 char s[MBEDTLS_X509_MAX_DN_NAME_SIZE], *p;
Agathiyan Bragadeeshc9d74f32023-07-31 17:25:44 +0100885 int print_hexstring;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200886
Gilles Peskine449bd832023-01-11 14:50:10 +0100887 memset(s, 0, sizeof(s));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200888
889 name = dn;
890 p = buf;
891 n = size;
892
Gilles Peskine449bd832023-01-11 14:50:10 +0100893 while (name != NULL) {
894 if (!name->oid.p) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200895 name = name->next;
896 continue;
897 }
898
Gilles Peskine449bd832023-01-11 14:50:10 +0100899 if (name != dn) {
900 ret = mbedtls_snprintf(p, n, merge ? " + " : ", ");
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200901 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200902 }
903
Agathiyan Bragadeesh0a4b6d82023-08-02 15:05:57 +0100904 print_hexstring = (name->val.tag != MBEDTLS_ASN1_UTF8_STRING) &&
905 (name->val.tag != MBEDTLS_ASN1_PRINTABLE_STRING) &&
906 (name->val.tag != MBEDTLS_ASN1_IA5_STRING);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200907
Gilles Peskine532e3ee2025-05-07 20:37:15 +0200908 if ((ret = mbedtls_x509_oid_get_attr_short_name(&name->oid, &short_name)) == 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100909 ret = mbedtls_snprintf(p, n, "%s=", short_name);
910 } else {
Agathiyan Bragadeeshaf70c7d2023-08-10 16:39:23 +0100911 if ((ret = mbedtls_oid_get_numeric_string(p, n, &name->oid)) > 0) {
Agathiyan Bragadeeshf3b97242023-08-22 16:37:11 +0100912 n -= ret;
913 p += ret;
Agathiyan Bragadeeshaf70c7d2023-08-10 16:39:23 +0100914 ret = mbedtls_snprintf(p, n, "=");
Agathiyan Bragadeeshc9d74f32023-07-31 17:25:44 +0100915 print_hexstring = 1;
Gilles Peskine47f1d7b2025-05-07 21:04:51 +0200916 } else if (ret == PSA_ERROR_BUFFER_TOO_SMALL) {
Agathiyan Bragadeesh8aa74ab2023-08-22 16:42:27 +0100917 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
Agathiyan Bragadeesh15df0122023-08-22 17:50:00 +0100918 } else {
Agathiyan Bragadeeshddc720d2023-07-26 15:51:49 +0100919 ret = mbedtls_snprintf(p, n, "\?\?=");
920 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100921 }
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200922 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200923
Agathiyan Bragadeesh4987c8f2023-08-01 11:10:52 +0100924 if (print_hexstring) {
Agathiyan Bragadeeshddc720d2023-07-26 15:51:49 +0100925 s[0] = '#';
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200926
Agathiyan Bragadeeshc7959b22023-09-12 17:54:43 +0100927 asn1_len_p = asn1_tag_len_buf + sizeof(asn1_tag_len_buf);
Agathiyan Bragadeesh15df0122023-08-22 17:50:00 +0100928 if ((ret = mbedtls_asn1_write_len(&asn1_len_p, asn1_tag_len_buf, name->val.len)) < 0) {
Agathiyan Bragadeesh07f472a2023-08-22 16:29:39 +0100929 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
930 }
931 asn1_len_size = ret;
Agathiyan Bragadeesh15df0122023-08-22 17:50:00 +0100932 if ((ret = mbedtls_asn1_write_tag(&asn1_len_p, asn1_tag_len_buf, name->val.tag)) < 0) {
Agathiyan Bragadeesh07f472a2023-08-22 16:29:39 +0100933 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
934 }
935 asn1_tag_size = ret;
Agathiyan Bragadeeshc7959b22023-09-12 17:54:43 +0100936 asn1_tag_len_buf_start = sizeof(asn1_tag_len_buf) - asn1_len_size - asn1_tag_size;
Agathiyan Bragadeesh5adffb22023-08-10 15:50:57 +0100937 for (i = 0, j = 1; i < asn1_len_size + asn1_tag_size; i++) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100938 if (j + 1 >= sizeof(s) - 1) {
939 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
940 }
Agathiyan Bragadeesh5adffb22023-08-10 15:50:57 +0100941 c = asn1_tag_len_buf[asn1_tag_len_buf_start+i];
Agathiyan Bragadeesha1f5c2d2023-08-02 17:08:52 +0100942 lowbits = (c & 0x0F);
Agathiyan Bragadeesh2bf09a62023-08-10 14:37:00 +0100943 highbits = c >> 4;
Agathiyan Bragadeesh9d2507c2023-07-24 16:35:57 +0100944 s[j++] = nibble_to_hex_digit(highbits);
Agathiyan Bragadeesh5adffb22023-08-10 15:50:57 +0100945 s[j++] = nibble_to_hex_digit(lowbits);
Werner Lewisb33dacd2022-05-20 12:48:46 +0100946 }
Agathiyan Bragadeesh5adffb22023-08-10 15:50:57 +0100947 for (i = 0; i < name->val.len; i++) {
948 if (j + 1 >= sizeof(s) - 1) {
949 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
950 }
951 c = name->val.p[i];
952 lowbits = (c & 0x0F);
953 highbits = c >> 4;
954 s[j++] = nibble_to_hex_digit(highbits);
955 s[j++] = nibble_to_hex_digit(lowbits);
Agathiyan Bragadeeshddc720d2023-07-26 15:51:49 +0100956 }
957 } else {
958 for (i = 0, j = 0; i < name->val.len; i++, j++) {
959 if (j >= sizeof(s) - 1) {
960 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
961 }
962
963 c = name->val.p[i];
964 // Special characters requiring escaping, RFC 4514 Section 2.4
Agathiyan Bragadeesh022f86f2023-08-22 16:56:04 +0100965 if (c == '\0') {
966 return MBEDTLS_ERR_X509_INVALID_NAME;
Agathiyan Bragadeesh15df0122023-08-22 17:50:00 +0100967 } else {
Agathiyan Bragadeesha7f96302023-08-10 16:03:27 +0100968 if (strchr(",=+<>;\"\\", c) ||
Agathiyan Bragadeeshddc720d2023-07-26 15:51:49 +0100969 ((i == 0) && strchr("# ", c)) ||
970 ((i == name->val.len-1) && (c == ' '))) {
971 if (j + 1 >= sizeof(s) - 1) {
972 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
973 }
974 s[j++] = '\\';
975 }
976 }
977 if (c < 32 || c >= 127) {
978 if (j + 3 >= sizeof(s) - 1) {
979 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
980 }
981 s[j++] = '\\';
Agathiyan Bragadeesha1f5c2d2023-08-02 17:08:52 +0100982 lowbits = (c & 0x0F);
Agathiyan Bragadeesh2bf09a62023-08-10 14:37:00 +0100983 highbits = c >> 4;
Agathiyan Bragadeeshddc720d2023-07-26 15:51:49 +0100984 s[j++] = nibble_to_hex_digit(highbits);
985 s[j] = nibble_to_hex_digit(lowbits);
986 } else {
987 s[j] = c;
988 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100989 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200990 }
Werner Lewisb33dacd2022-05-20 12:48:46 +0100991 s[j] = '\0';
Gilles Peskine449bd832023-01-11 14:50:10 +0100992 ret = mbedtls_snprintf(p, n, "%s", s);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200993 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000994
995 merge = name->next_merged;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200996 name = name->next;
997 }
998
Gilles Peskine449bd832023-01-11 14:50:10 +0100999 return (int) (size - n);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001000}
1001
1002/*
1003 * Store the serial in printable form into buf; no more
1004 * than size characters will be written
1005 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001006int mbedtls_x509_serial_gets(char *buf, size_t size, const mbedtls_x509_buf *serial)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001007{
Janos Follath865b3eb2019-12-16 11:46:15 +00001008 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001009 size_t i, n, nr;
1010 char *p;
1011
1012 p = buf;
1013 n = size;
1014
Gilles Peskine449bd832023-01-11 14:50:10 +01001015 nr = (serial->len <= 32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001016 ? serial->len : 28;
1017
Gilles Peskine449bd832023-01-11 14:50:10 +01001018 for (i = 0; i < nr; i++) {
1019 if (i == 0 && nr > 1 && serial->p[i] == 0x0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001020 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001021 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001022
Gilles Peskine449bd832023-01-11 14:50:10 +01001023 ret = mbedtls_snprintf(p, n, "%02X%s",
1024 serial->p[i], (i < nr - 1) ? ":" : "");
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001025 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001026 }
1027
Gilles Peskine449bd832023-01-11 14:50:10 +01001028 if (nr != serial->len) {
1029 ret = mbedtls_snprintf(p, n, "....");
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001030 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001031 }
1032
Gilles Peskine449bd832023-01-11 14:50:10 +01001033 return (int) (size - n);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001034}
1035
Hanno Becker612a2f12020-10-09 09:19:39 +01001036#if !defined(MBEDTLS_X509_REMOVE_INFO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001037/*
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +02001038 * Helper for writing signature algorithms
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +01001039 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001040int mbedtls_x509_sig_alg_gets(char *buf, size_t size, const mbedtls_x509_buf *sig_oid,
Valerio Settid24dfad2025-04-23 11:13:02 +02001041 mbedtls_pk_type_t pk_alg, mbedtls_md_type_t md_alg)
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +01001042{
Janos Follath865b3eb2019-12-16 11:46:15 +00001043 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +01001044 char *p = buf;
1045 size_t n = size;
1046 const char *desc = NULL;
1047
Gilles Peskine532e3ee2025-05-07 20:37:15 +02001048 ret = mbedtls_x509_oid_get_sig_alg_desc(sig_oid, &desc);
Gilles Peskine449bd832023-01-11 14:50:10 +01001049 if (ret != 0) {
1050 ret = mbedtls_snprintf(p, n, "???");
1051 } else {
1052 ret = mbedtls_snprintf(p, n, "%s", desc);
1053 }
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001054 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +01001055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001056#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001057 if (pk_alg == MBEDTLS_PK_RSASSA_PSS) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001058 const char *name = md_type_to_string(md_alg);
Valerio Settid24dfad2025-04-23 11:13:02 +02001059 if (name != NULL) {
1060 ret = mbedtls_snprintf(p, n, " (%s)", name);
1061 } else {
1062 ret = mbedtls_snprintf(p, n, " (?)");
1063 }
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001064 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +01001065 }
1066#else
1067 ((void) pk_alg);
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +02001068 ((void) md_alg);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001069#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +01001070
Gilles Peskine449bd832023-01-11 14:50:10 +01001071 return (int) (size - n);
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +01001072}
Hanno Becker612a2f12020-10-09 09:19:39 +01001073#endif /* MBEDTLS_X509_REMOVE_INFO */
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +01001074
1075/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001076 * Helper for writing "RSA key size", "EC key size", etc
1077 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001078int mbedtls_x509_key_size_helper(char *buf, size_t buf_size, const char *name)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001079{
1080 char *p = buf;
Manuel Pégourié-Gonnardfb317c52015-06-18 16:25:56 +02001081 size_t n = buf_size;
Janos Follath865b3eb2019-12-16 11:46:15 +00001082 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001083
Gilles Peskine449bd832023-01-11 14:50:10 +01001084 ret = mbedtls_snprintf(p, n, "%s key size", name);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001085 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001086
Gilles Peskine449bd832023-01-11 14:50:10 +01001087 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001088}
1089
Glenn Strauss416dc032022-06-30 00:38:53 -04001090int mbedtls_x509_time_cmp(const mbedtls_x509_time *t1,
1091 const mbedtls_x509_time *t2)
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001092{
Glenn Strauss5aef2972022-06-30 04:38:02 -04001093 int x;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001094
Glenn Strauss5aef2972022-06-30 04:38:02 -04001095 x = (((t1->year << 9) | (t1->mon << 5) | (t1->day)) -
1096 ((t2->year << 9) | (t2->mon << 5) | (t2->day)));
1097 if (x != 0) {
1098 return x;
Manuel Pégourié-Gonnard57e10d72015-06-22 18:59:21 +02001099 }
Manuel Pégourié-Gonnard864108d2015-05-29 10:11:03 +02001100
Glenn Strauss5aef2972022-06-30 04:38:02 -04001101 x = (((t1->hour << 12) | (t1->min << 6) | (t1->sec)) -
1102 ((t2->hour << 12) | (t2->min << 6) | (t2->sec)));
1103 return x;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001104}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001105
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001106#if defined(MBEDTLS_HAVE_TIME_DATE)
Glenn Strauss61d99302022-06-30 05:25:56 -04001107int mbedtls_x509_time_gmtime(mbedtls_time_t tt, mbedtls_x509_time *now)
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001108{
Glenn Strauss811eeb22022-06-30 05:28:50 -04001109 struct tm tm;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001110
Glenn Strauss811eeb22022-06-30 05:28:50 -04001111 if (mbedtls_platform_gmtime_r(&tt, &tm) == NULL) {
1112 return -1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001113 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001114
Glenn Strauss811eeb22022-06-30 05:28:50 -04001115 now->year = tm.tm_year + 1900;
1116 now->mon = tm.tm_mon + 1;
1117 now->day = tm.tm_mday;
1118 now->hour = tm.tm_hour;
1119 now->min = tm.tm_min;
1120 now->sec = tm.tm_sec;
Gilles Peskine449bd832023-01-11 14:50:10 +01001121 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001122}
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001123
Glenn Strauss61d99302022-06-30 05:25:56 -04001124static int x509_get_current_time(mbedtls_x509_time *now)
1125{
1126 return mbedtls_x509_time_gmtime(mbedtls_time(NULL), now);
1127}
1128
Gilles Peskine449bd832023-01-11 14:50:10 +01001129int mbedtls_x509_time_is_past(const mbedtls_x509_time *to)
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001130{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001131 mbedtls_x509_time now;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001132
Gilles Peskine449bd832023-01-11 14:50:10 +01001133 if (x509_get_current_time(&now) != 0) {
1134 return 1;
1135 }
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001136
Glenn Strauss416dc032022-06-30 00:38:53 -04001137 return mbedtls_x509_time_cmp(to, &now) < 0;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001138}
1139
Gilles Peskine449bd832023-01-11 14:50:10 +01001140int mbedtls_x509_time_is_future(const mbedtls_x509_time *from)
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001141{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001142 mbedtls_x509_time now;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001143
Gilles Peskine449bd832023-01-11 14:50:10 +01001144 if (x509_get_current_time(&now) != 0) {
1145 return 1;
1146 }
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001147
Glenn Strauss416dc032022-06-30 00:38:53 -04001148 return mbedtls_x509_time_cmp(from, &now) > 0;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001149}
1150
Manuel Pégourié-Gonnard60c793b2015-06-18 20:52:58 +02001151#else /* MBEDTLS_HAVE_TIME_DATE */
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001152
Gilles Peskine449bd832023-01-11 14:50:10 +01001153int mbedtls_x509_time_is_past(const mbedtls_x509_time *to)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001154{
1155 ((void) to);
Gilles Peskine449bd832023-01-11 14:50:10 +01001156 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001157}
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001158
Gilles Peskine449bd832023-01-11 14:50:10 +01001159int mbedtls_x509_time_is_future(const mbedtls_x509_time *from)
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001160{
1161 ((void) from);
Gilles Peskine449bd832023-01-11 14:50:10 +01001162 return 0;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001163}
Manuel Pégourié-Gonnard60c793b2015-06-18 20:52:58 +02001164#endif /* MBEDTLS_HAVE_TIME_DATE */
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001165
1166/* Common functions for parsing CRT and CSR. */
1167#if defined(MBEDTLS_X509_CRT_PARSE_C) || defined(MBEDTLS_X509_CSR_PARSE_C)
1168/*
1169 * OtherName ::= SEQUENCE {
1170 * type-id OBJECT IDENTIFIER,
1171 * value [0] EXPLICIT ANY DEFINED BY type-id }
1172 *
1173 * HardwareModuleName ::= SEQUENCE {
1174 * hwType OBJECT IDENTIFIER,
1175 * hwSerialNum OCTET STRING }
1176 *
1177 * NOTE: we currently only parse and use otherName of type HwModuleName,
1178 * as defined in RFC 4108.
1179 */
1180static int x509_get_other_name(const mbedtls_x509_buf *subject_alt_name,
1181 mbedtls_x509_san_other_name *other_name)
1182{
1183 int ret = 0;
1184 size_t len;
1185 unsigned char *p = subject_alt_name->p;
1186 const unsigned char *end = p + subject_alt_name->len;
1187 mbedtls_x509_buf cur_oid;
1188
1189 if ((subject_alt_name->tag &
1190 (MBEDTLS_ASN1_TAG_CLASS_MASK | MBEDTLS_ASN1_TAG_VALUE_MASK)) !=
1191 (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME)) {
1192 /*
1193 * The given subject alternative name is not of type "othername".
1194 */
1195 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1196 }
1197
1198 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1199 MBEDTLS_ASN1_OID)) != 0) {
1200 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1201 }
1202
1203 cur_oid.tag = MBEDTLS_ASN1_OID;
1204 cur_oid.p = p;
1205 cur_oid.len = len;
1206
1207 /*
1208 * Only HwModuleName is currently supported.
1209 */
1210 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ON_HW_MODULE_NAME, &cur_oid) != 0) {
1211 return MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
1212 }
David Horstmann2ea44d22023-08-18 18:36:02 +01001213 other_name->type_id = cur_oid;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001214
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001215 p += len;
1216 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1217 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC)) !=
1218 0) {
1219 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1220 }
1221
Hanno Beckerdae916b2019-09-13 14:21:13 +01001222 if (end != p + len) {
1223 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1224 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1225 }
1226
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001227 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1228 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1229 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1230 }
1231
Hanno Beckerdae916b2019-09-13 14:21:13 +01001232 if (end != p + len) {
1233 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1234 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1235 }
1236
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001237 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OID)) != 0) {
1238 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1239 }
1240
1241 other_name->value.hardware_module_name.oid.tag = MBEDTLS_ASN1_OID;
1242 other_name->value.hardware_module_name.oid.p = p;
1243 other_name->value.hardware_module_name.oid.len = len;
1244
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001245 p += len;
1246 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1247 MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1248 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1249 }
1250
1251 other_name->value.hardware_module_name.val.tag = MBEDTLS_ASN1_OCTET_STRING;
1252 other_name->value.hardware_module_name.val.p = p;
1253 other_name->value.hardware_module_name.val.len = len;
1254 p += len;
1255 if (p != end) {
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001256 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1257 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1258 }
1259 return 0;
1260}
1261
Przemek Stekiel21903ec2023-02-21 08:32:37 +01001262/* Check mbedtls_x509_get_subject_alt_name for detailed description.
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001263 *
Przemek Stekiel21903ec2023-02-21 08:32:37 +01001264 * In some cases while parsing subject alternative names the sequence tag is optional
1265 * (e.g. CertSerialNumber). This function is designed to handle such case.
Przemek Stekiel725688b2023-04-04 22:49:44 +02001266 */
Przemek Stekiel21903ec2023-02-21 08:32:37 +01001267int mbedtls_x509_get_subject_alt_name_ext(unsigned char **p,
1268 const unsigned char *end,
1269 mbedtls_x509_sequence *subject_alt_name)
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001270{
1271 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel21903ec2023-02-21 08:32:37 +01001272 size_t tag_len;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001273 mbedtls_asn1_sequence *cur = subject_alt_name;
1274
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001275 while (*p < end) {
Andrzej Kurek154a6052023-04-30 14:11:49 -04001276 mbedtls_x509_subject_alternative_name tmp_san_name;
Hanno Beckerae8f8c42019-09-13 12:24:56 +01001277 mbedtls_x509_buf tmp_san_buf;
Andrzej Kurek154a6052023-04-30 14:11:49 -04001278 memset(&tmp_san_name, 0, sizeof(tmp_san_name));
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001279
Hanno Beckerae8f8c42019-09-13 12:24:56 +01001280 tmp_san_buf.tag = **p;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001281 (*p)++;
Hanno Beckerae8f8c42019-09-13 12:24:56 +01001282
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001283 if ((ret = mbedtls_asn1_get_len(p, end, &tag_len)) != 0) {
1284 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1285 }
1286
Hanno Beckerae8f8c42019-09-13 12:24:56 +01001287 tmp_san_buf.p = *p;
1288 tmp_san_buf.len = tag_len;
1289
1290 if ((tmp_san_buf.tag & MBEDTLS_ASN1_TAG_CLASS_MASK) !=
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001291 MBEDTLS_ASN1_CONTEXT_SPECIFIC) {
1292 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1293 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
1294 }
1295
1296 /*
Andrzej Kurek154a6052023-04-30 14:11:49 -04001297 * Check that the SAN is structured correctly by parsing it.
1298 * The SAN structure is discarded afterwards.
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001299 */
Andrzej Kurek154a6052023-04-30 14:11:49 -04001300 ret = mbedtls_x509_parse_subject_alt_name(&tmp_san_buf, &tmp_san_name);
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001301 /*
1302 * In case the extension is malformed, return an error,
1303 * and clear the allocated sequences.
1304 */
1305 if (ret != 0 && ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE) {
1306 mbedtls_asn1_sequence_free(subject_alt_name->next);
1307 subject_alt_name->next = NULL;
1308 return ret;
1309 }
1310
Andrzej Kurek154a6052023-04-30 14:11:49 -04001311 mbedtls_x509_free_subject_alt_name(&tmp_san_name);
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001312 /* Allocate and assign next pointer */
1313 if (cur->buf.p != NULL) {
1314 if (cur->next != NULL) {
1315 return MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
1316 }
1317
1318 cur->next = mbedtls_calloc(1, sizeof(mbedtls_asn1_sequence));
1319
1320 if (cur->next == NULL) {
1321 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1322 MBEDTLS_ERR_ASN1_ALLOC_FAILED);
1323 }
1324
1325 cur = cur->next;
1326 }
1327
Hanno Beckerae8f8c42019-09-13 12:24:56 +01001328 cur->buf = tmp_san_buf;
1329 *p += tmp_san_buf.len;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001330 }
1331
1332 /* Set final sequence entry's next pointer to NULL */
1333 cur->next = NULL;
1334
1335 if (*p != end) {
1336 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1337 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1338 }
1339
1340 return 0;
1341}
1342
Przemek Stekiel21903ec2023-02-21 08:32:37 +01001343/*
1344 * SubjectAltName ::= GeneralNames
1345 *
1346 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
1347 *
1348 * GeneralName ::= CHOICE {
1349 * otherName [0] OtherName,
1350 * rfc822Name [1] IA5String,
1351 * dNSName [2] IA5String,
1352 * x400Address [3] ORAddress,
1353 * directoryName [4] Name,
1354 * ediPartyName [5] EDIPartyName,
1355 * uniformResourceIdentifier [6] IA5String,
1356 * iPAddress [7] OCTET STRING,
1357 * registeredID [8] OBJECT IDENTIFIER }
1358 *
1359 * OtherName ::= SEQUENCE {
1360 * type-id OBJECT IDENTIFIER,
1361 * value [0] EXPLICIT ANY DEFINED BY type-id }
1362 *
1363 * EDIPartyName ::= SEQUENCE {
1364 * nameAssigner [0] DirectoryString OPTIONAL,
1365 * partyName [1] DirectoryString }
1366 *
1367 * We list all types, but use the following GeneralName types from RFC 5280:
1368 * "dnsName", "uniformResourceIdentifier" and "hardware_module_name"
1369 * of type "otherName", as defined in RFC 4108.
1370 */
1371int mbedtls_x509_get_subject_alt_name(unsigned char **p,
1372 const unsigned char *end,
1373 mbedtls_x509_sequence *subject_alt_name)
1374{
1375 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1376 size_t len;
1377
1378 /* Get main sequence tag */
1379 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
1380 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1381 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1382 }
1383
1384 if (*p + len != end) {
1385 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1386 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1387 }
1388
1389 return mbedtls_x509_get_subject_alt_name_ext(p, end, subject_alt_name);
1390}
1391
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001392int mbedtls_x509_get_ns_cert_type(unsigned char **p,
1393 const unsigned char *end,
1394 unsigned char *ns_cert_type)
1395{
1396 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1397 mbedtls_x509_bitstring bs = { 0, 0, NULL };
1398
1399 if ((ret = mbedtls_asn1_get_bitstring(p, end, &bs)) != 0) {
1400 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1401 }
1402
Przemek Stekiel32e20912023-01-25 14:26:15 +01001403 /* A bitstring with no flags set is still technically valid, as it will mean
1404 that the certificate has no designated purpose at the time of creation. */
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001405 if (bs.len == 0) {
1406 *ns_cert_type = 0;
1407 return 0;
1408 }
1409
1410 if (bs.len != 1) {
1411 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1412 MBEDTLS_ERR_ASN1_INVALID_LENGTH);
1413 }
1414
1415 /* Get actual bitstring */
1416 *ns_cert_type = *bs.p;
1417 return 0;
1418}
1419
1420int mbedtls_x509_get_key_usage(unsigned char **p,
1421 const unsigned char *end,
1422 unsigned int *key_usage)
1423{
1424 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1425 size_t i;
1426 mbedtls_x509_bitstring bs = { 0, 0, NULL };
1427
1428 if ((ret = mbedtls_asn1_get_bitstring(p, end, &bs)) != 0) {
1429 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1430 }
1431
Przemek Stekiel32e20912023-01-25 14:26:15 +01001432 /* A bitstring with no flags set is still technically valid, as it will mean
1433 that the certificate has no designated purpose at the time of creation. */
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001434 if (bs.len == 0) {
1435 *key_usage = 0;
1436 return 0;
1437 }
1438
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001439 /* Get actual bitstring */
1440 *key_usage = 0;
1441 for (i = 0; i < bs.len && i < sizeof(unsigned int); i++) {
1442 *key_usage |= (unsigned int) bs.p[i] << (8*i);
1443 }
1444
1445 return 0;
1446}
1447
1448int mbedtls_x509_parse_subject_alt_name(const mbedtls_x509_buf *san_buf,
1449 mbedtls_x509_subject_alternative_name *san)
1450{
1451 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1452 switch (san_buf->tag &
1453 (MBEDTLS_ASN1_TAG_CLASS_MASK |
1454 MBEDTLS_ASN1_TAG_VALUE_MASK)) {
1455 /*
1456 * otherName
1457 */
1458 case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME):
1459 {
1460 mbedtls_x509_san_other_name other_name;
1461
1462 ret = x509_get_other_name(san_buf, &other_name);
1463 if (ret != 0) {
1464 return ret;
1465 }
1466
1467 memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name));
1468 san->type = MBEDTLS_X509_SAN_OTHER_NAME;
1469 memcpy(&san->san.other_name,
1470 &other_name, sizeof(other_name));
1471
1472 }
1473 break;
Andrzej Kurek7a05fab2023-02-13 10:03:07 -05001474 /*
1475 * uniformResourceIdentifier
1476 */
1477 case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER):
1478 {
1479 memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name));
1480 san->type = MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001481
Andrzej Kurek7a05fab2023-02-13 10:03:07 -05001482 memcpy(&san->san.unstructured_name,
1483 san_buf, sizeof(*san_buf));
1484
1485 }
1486 break;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001487 /*
1488 * dNSName
1489 */
1490 case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_DNS_NAME):
1491 {
1492 memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name));
1493 san->type = MBEDTLS_X509_SAN_DNS_NAME;
1494
1495 memcpy(&san->san.unstructured_name,
1496 san_buf, sizeof(*san_buf));
Przemek Stekielecee12f2023-02-09 14:43:49 +01001497 }
1498 break;
Przemek Stekiel0ab5b932023-05-29 16:30:50 +02001499 /*
1500 * IP address
1501 */
1502 case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_IP_ADDRESS):
1503 {
1504 memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name));
1505 san->type = MBEDTLS_X509_SAN_IP_ADDRESS;
Przemek Stekiel01cb6eb2023-06-05 16:38:13 +02001506 // Only IPv6 (16 bytes) and IPv4 (4 bytes) types are supported
1507 if (san_buf->len == 4 || san_buf->len == 16) {
1508 memcpy(&san->san.unstructured_name,
1509 san_buf, sizeof(*san_buf));
1510 } else {
1511 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1512 }
Przemek Stekiel0ab5b932023-05-29 16:30:50 +02001513 }
1514 break;
Przemek Stekielecee12f2023-02-09 14:43:49 +01001515 /*
Andrzej Kurek154a6052023-04-30 14:11:49 -04001516 * rfc822Name
Przemek Stekielecee12f2023-02-09 14:43:49 +01001517 */
1518 case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_RFC822_NAME):
1519 {
1520 memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name));
1521 san->type = MBEDTLS_X509_SAN_RFC822_NAME;
1522 memcpy(&san->san.unstructured_name, san_buf, sizeof(*san_buf));
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001523 }
1524 break;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001525 /*
Andrzej Kureke12b01d2023-01-10 06:47:38 -05001526 * directoryName
1527 */
1528 case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_DIRECTORY_NAME):
1529 {
1530 size_t name_len;
1531 unsigned char *p = san_buf->p;
1532 memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name));
1533 san->type = MBEDTLS_X509_SAN_DIRECTORY_NAME;
1534
1535 ret = mbedtls_asn1_get_tag(&p, p + san_buf->len, &name_len,
1536 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE);
1537
1538 if (ret != 0) {
Andrzej Kurekbf8ccd82023-02-13 07:13:30 -05001539 return ret;
Andrzej Kureke12b01d2023-01-10 06:47:38 -05001540 }
1541
1542 if ((ret = mbedtls_x509_get_name(&p, p + name_len,
1543 &san->san.directory_name)) != 0) {
Andrzej Kurekbf8ccd82023-02-13 07:13:30 -05001544 return ret;
Andrzej Kureke12b01d2023-01-10 06:47:38 -05001545 }
1546 }
1547 break;
1548 /*
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001549 * Type not supported
1550 */
1551 default:
1552 return MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
1553 }
1554 return 0;
1555}
1556
Andrzej Kurekd40c2b62023-02-13 07:01:59 -05001557void mbedtls_x509_free_subject_alt_name(mbedtls_x509_subject_alternative_name *san)
1558{
1559 if (san->type == MBEDTLS_X509_SAN_DIRECTORY_NAME) {
1560 mbedtls_asn1_free_named_data_list_shallow(san->san.directory_name.next);
1561 }
1562}
1563
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001564#if !defined(MBEDTLS_X509_REMOVE_INFO)
1565int mbedtls_x509_info_subject_alt_name(char **buf, size_t *size,
1566 const mbedtls_x509_sequence
1567 *subject_alt_name,
1568 const char *prefix)
1569{
1570 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1571 size_t i;
1572 size_t n = *size;
1573 char *p = *buf;
1574 const mbedtls_x509_sequence *cur = subject_alt_name;
1575 mbedtls_x509_subject_alternative_name san;
1576 int parse_ret;
1577
1578 while (cur != NULL) {
1579 memset(&san, 0, sizeof(san));
1580 parse_ret = mbedtls_x509_parse_subject_alt_name(&cur->buf, &san);
1581 if (parse_ret != 0) {
1582 if (parse_ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE) {
1583 ret = mbedtls_snprintf(p, n, "\n%s <unsupported>", prefix);
1584 MBEDTLS_X509_SAFE_SNPRINTF;
1585 } else {
1586 ret = mbedtls_snprintf(p, n, "\n%s <malformed>", prefix);
1587 MBEDTLS_X509_SAFE_SNPRINTF;
1588 }
1589 cur = cur->next;
1590 continue;
1591 }
1592
1593 switch (san.type) {
1594 /*
1595 * otherName
1596 */
1597 case MBEDTLS_X509_SAN_OTHER_NAME:
1598 {
1599 mbedtls_x509_san_other_name *other_name = &san.san.other_name;
1600
1601 ret = mbedtls_snprintf(p, n, "\n%s otherName :", prefix);
1602 MBEDTLS_X509_SAFE_SNPRINTF;
1603
1604 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ON_HW_MODULE_NAME,
David Horstmanncfae6a12023-08-18 19:12:59 +01001605 &other_name->type_id) == 0) {
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001606 ret = mbedtls_snprintf(p, n, "\n%s hardware module name :", prefix);
1607 MBEDTLS_X509_SAFE_SNPRINTF;
1608 ret =
1609 mbedtls_snprintf(p, n, "\n%s hardware type : ", prefix);
1610 MBEDTLS_X509_SAFE_SNPRINTF;
1611
1612 ret = mbedtls_oid_get_numeric_string(p,
1613 n,
1614 &other_name->value.hardware_module_name.oid);
1615 MBEDTLS_X509_SAFE_SNPRINTF;
1616
1617 ret =
1618 mbedtls_snprintf(p, n, "\n%s hardware serial number : ", prefix);
1619 MBEDTLS_X509_SAFE_SNPRINTF;
1620
1621 for (i = 0; i < other_name->value.hardware_module_name.val.len; i++) {
1622 ret = mbedtls_snprintf(p,
1623 n,
1624 "%02X",
1625 other_name->value.hardware_module_name.val.p[i]);
1626 MBEDTLS_X509_SAFE_SNPRINTF;
1627 }
1628 }/* MBEDTLS_OID_ON_HW_MODULE_NAME */
1629 }
1630 break;
Andrzej Kurek7a05fab2023-02-13 10:03:07 -05001631 /*
1632 * uniformResourceIdentifier
1633 */
1634 case MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER:
1635 {
1636 ret = mbedtls_snprintf(p, n, "\n%s uniformResourceIdentifier : ", prefix);
1637 MBEDTLS_X509_SAFE_SNPRINTF;
1638 if (san.san.unstructured_name.len >= n) {
Przemek Stekiel01cb6eb2023-06-05 16:38:13 +02001639 if (n > 0) {
1640 *p = '\0';
1641 }
Andrzej Kurek7a05fab2023-02-13 10:03:07 -05001642 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1643 }
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001644
Andrzej Kurek7a05fab2023-02-13 10:03:07 -05001645 memcpy(p, san.san.unstructured_name.p, san.san.unstructured_name.len);
1646 p += san.san.unstructured_name.len;
1647 n -= san.san.unstructured_name.len;
1648 }
1649 break;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001650 /*
1651 * dNSName
Przemek Stekiel5b9e4162023-02-15 12:56:37 +01001652 * RFC822 Name
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001653 */
1654 case MBEDTLS_X509_SAN_DNS_NAME:
Przemek Stekiel5b9e4162023-02-15 12:56:37 +01001655 case MBEDTLS_X509_SAN_RFC822_NAME:
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001656 {
Przemek Stekiel82d250d2023-02-15 15:00:50 +01001657 const char *dns_name = "dNSName";
1658 const char *rfc822_name = "rfc822Name";
Przemek Stekiel5b9e4162023-02-15 12:56:37 +01001659
Przemek Stekiel82d250d2023-02-15 15:00:50 +01001660 ret = mbedtls_snprintf(p, n,
1661 "\n%s %s : ",
1662 prefix,
Przemek Stekiel093c97d2023-06-02 10:11:32 +02001663 san.type ==
1664 MBEDTLS_X509_SAN_DNS_NAME ? dns_name : rfc822_name);
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001665 MBEDTLS_X509_SAFE_SNPRINTF;
1666 if (san.san.unstructured_name.len >= n) {
Przemek Stekiel01cb6eb2023-06-05 16:38:13 +02001667 if (n > 0) {
1668 *p = '\0';
1669 }
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001670 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1671 }
1672
Przemek Stekiel093c97d2023-06-02 10:11:32 +02001673 memcpy(p, san.san.unstructured_name.p, san.san.unstructured_name.len);
1674 p += san.san.unstructured_name.len;
1675 n -= san.san.unstructured_name.len;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001676 }
1677 break;
Przemek Stekiel093c97d2023-06-02 10:11:32 +02001678 /*
1679 * iPAddress
1680 */
1681 case MBEDTLS_X509_SAN_IP_ADDRESS:
1682 {
1683 ret = mbedtls_snprintf(p, n, "\n%s %s : ",
1684 prefix, "iPAddress");
1685 MBEDTLS_X509_SAFE_SNPRINTF;
1686 if (san.san.unstructured_name.len >= n) {
Przemek Stekiel01cb6eb2023-06-05 16:38:13 +02001687 if (n > 0) {
1688 *p = '\0';
1689 }
Przemek Stekiel093c97d2023-06-02 10:11:32 +02001690 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1691 }
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001692
Przemek Stekiel093c97d2023-06-02 10:11:32 +02001693 unsigned char *ip = san.san.unstructured_name.p;
1694 // Only IPv6 (16 bytes) and IPv4 (4 bytes) types are supported
1695 if (san.san.unstructured_name.len == 4) {
Przemek Stekiel4d3fc212023-06-06 11:40:32 +02001696 ret = mbedtls_snprintf(p, n, "%u.%u.%u.%u", ip[0], ip[1], ip[2], ip[3]);
1697 MBEDTLS_X509_SAFE_SNPRINTF;
Przemek Stekiel093c97d2023-06-02 10:11:32 +02001698 } else if (san.san.unstructured_name.len == 16) {
Przemek Stekiel4d3fc212023-06-06 11:40:32 +02001699 ret = mbedtls_snprintf(p, n,
Przemek Stekiel01cb6eb2023-06-05 16:38:13 +02001700 "%X%X:%X%X:%X%X:%X%X:%X%X:%X%X:%X%X:%X%X",
1701 ip[0], ip[1], ip[2], ip[3], ip[4], ip[5], ip[6],
Przemek Stekiel4d3fc212023-06-06 11:40:32 +02001702 ip[7], ip[8], ip[9], ip[10], ip[11], ip[12], ip[13],
Przemek Stekiel01cb6eb2023-06-05 16:38:13 +02001703 ip[14], ip[15]);
Przemek Stekiel4d3fc212023-06-06 11:40:32 +02001704 MBEDTLS_X509_SAFE_SNPRINTF;
Przemek Stekiel093c97d2023-06-02 10:11:32 +02001705 } else {
Przemek Stekiel01cb6eb2023-06-05 16:38:13 +02001706 if (n > 0) {
1707 *p = '\0';
1708 }
Przemek Stekiel093c97d2023-06-02 10:11:32 +02001709 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1710 }
Przemek Stekiel093c97d2023-06-02 10:11:32 +02001711 }
1712 break;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001713 /*
Andrzej Kureke12b01d2023-01-10 06:47:38 -05001714 * directoryName
1715 */
1716 case MBEDTLS_X509_SAN_DIRECTORY_NAME:
1717 {
1718 ret = mbedtls_snprintf(p, n, "\n%s directoryName : ", prefix);
Andrzej Kurek5f0c6e82023-02-27 16:03:41 -05001719 if (ret < 0 || (size_t) ret >= n) {
1720 mbedtls_x509_free_subject_alt_name(&san);
1721 }
1722
Andrzej Kureke12b01d2023-01-10 06:47:38 -05001723 MBEDTLS_X509_SAFE_SNPRINTF;
1724 ret = mbedtls_x509_dn_gets(p, n, &san.san.directory_name);
Andrzej Kurekd40c2b62023-02-13 07:01:59 -05001725
Andrzej Kureke12b01d2023-01-10 06:47:38 -05001726 if (ret < 0) {
Andrzej Kurek5f0c6e82023-02-27 16:03:41 -05001727 mbedtls_x509_free_subject_alt_name(&san);
Przemek Stekiel01cb6eb2023-06-05 16:38:13 +02001728 if (n > 0) {
1729 *p = '\0';
1730 }
Andrzej Kureke12b01d2023-01-10 06:47:38 -05001731 return ret;
1732 }
1733
1734 p += ret;
1735 n -= ret;
1736 }
1737 break;
1738 /*
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001739 * Type not supported, skip item.
1740 */
1741 default:
1742 ret = mbedtls_snprintf(p, n, "\n%s <unsupported>", prefix);
1743 MBEDTLS_X509_SAFE_SNPRINTF;
1744 break;
1745 }
1746
Andrzej Kurekd40c2b62023-02-13 07:01:59 -05001747 /* So far memory is freed only in the case of directoryName
Andrzej Kurek5f0c6e82023-02-27 16:03:41 -05001748 * parsing succeeding, as mbedtls_x509_get_name allocates memory. */
Andrzej Kurekd40c2b62023-02-13 07:01:59 -05001749 mbedtls_x509_free_subject_alt_name(&san);
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001750 cur = cur->next;
1751 }
1752
1753 *p = '\0';
1754
1755 *size = n;
1756 *buf = p;
1757
1758 return 0;
1759}
1760
Demi Marie Obenour690b8c92022-12-04 04:24:22 -05001761#define PRINT_ITEM(i) \
1762 do { \
1763 ret = mbedtls_snprintf(p, n, "%s" i, sep); \
1764 MBEDTLS_X509_SAFE_SNPRINTF; \
1765 sep = ", "; \
1766 } while (0)
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001767
Demi Marie Obenour690b8c92022-12-04 04:24:22 -05001768#define CERT_TYPE(type, name) \
1769 do { \
1770 if (ns_cert_type & (type)) { \
1771 PRINT_ITEM(name); \
1772 } \
1773 } while (0)
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001774
1775int mbedtls_x509_info_cert_type(char **buf, size_t *size,
1776 unsigned char ns_cert_type)
1777{
1778 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1779 size_t n = *size;
1780 char *p = *buf;
1781 const char *sep = "";
1782
1783 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT, "SSL Client");
1784 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER, "SSL Server");
1785 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_EMAIL, "Email");
1786 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing");
1787 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_RESERVED, "Reserved");
1788 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_SSL_CA, "SSL CA");
1789 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA, "Email CA");
1790 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA");
1791
1792 *size = n;
1793 *buf = p;
1794
1795 return 0;
1796}
1797
Demi Marie Obenour690b8c92022-12-04 04:24:22 -05001798#define KEY_USAGE(code, name) \
1799 do { \
1800 if ((key_usage) & (code)) { \
1801 PRINT_ITEM(name); \
1802 } \
1803 } while (0)
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001804
1805int mbedtls_x509_info_key_usage(char **buf, size_t *size,
1806 unsigned int key_usage)
1807{
1808 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1809 size_t n = *size;
1810 char *p = *buf;
1811 const char *sep = "";
1812
1813 KEY_USAGE(MBEDTLS_X509_KU_DIGITAL_SIGNATURE, "Digital Signature");
1814 KEY_USAGE(MBEDTLS_X509_KU_NON_REPUDIATION, "Non Repudiation");
1815 KEY_USAGE(MBEDTLS_X509_KU_KEY_ENCIPHERMENT, "Key Encipherment");
1816 KEY_USAGE(MBEDTLS_X509_KU_DATA_ENCIPHERMENT, "Data Encipherment");
1817 KEY_USAGE(MBEDTLS_X509_KU_KEY_AGREEMENT, "Key Agreement");
1818 KEY_USAGE(MBEDTLS_X509_KU_KEY_CERT_SIGN, "Key Cert Sign");
1819 KEY_USAGE(MBEDTLS_X509_KU_CRL_SIGN, "CRL Sign");
1820 KEY_USAGE(MBEDTLS_X509_KU_ENCIPHER_ONLY, "Encipher Only");
1821 KEY_USAGE(MBEDTLS_X509_KU_DECIPHER_ONLY, "Decipher Only");
1822
1823 *size = n;
1824 *buf = p;
1825
1826 return 0;
1827}
1828#endif /* MBEDTLS_X509_REMOVE_INFO */
1829#endif /* MBEDTLS_X509_CRT_PARSE_C || MBEDTLS_X509_CSR_PARSE_C */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001830#endif /* MBEDTLS_X509_USE_C */