blob: b8f2847437fe9e3e1105249992e2c99e5ce9579d [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
Gilles Peskine1819a912025-07-22 21:54:50 +020027#include <limits.h>
Rich Evans36796df2015-02-12 18:27:14 +000028#include <stdio.h>
Rich Evans00ab4702015-02-06 13:43:58 +000029#include <string.h>
30
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020031#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000032#include "mbedtls/pem.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020033#endif
34
Agathiyan Bragadeeshddc720d2023-07-26 15:51:49 +010035#include "mbedtls/asn1write.h"
Agathiyan Bragadeeshddc720d2023-07-26 15:51:49 +010036
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000037#include "mbedtls/platform.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020038
Simon Butcherb5b6af22016-07-13 14:46:18 +010039#if defined(MBEDTLS_HAVE_TIME)
40#include "mbedtls/platform_time.h"
41#endif
Nicholas Wilson512b4ee2017-12-05 12:07:33 +000042#if defined(MBEDTLS_HAVE_TIME_DATE)
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +010043#include "mbedtls/platform_util.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020044#include <time.h>
45#endif
46
Demi Marie Obenour690b8c92022-12-04 04:24:22 -050047#define CHECK(code) \
48 do { \
49 if ((ret = (code)) != 0) { \
50 return ret; \
51 } \
52 } while (0)
53
Hanno Becker1eeca412018-10-15 12:01:35 +010054#define CHECK_RANGE(min, max, val) \
Demi Marie Obenour690b8c92022-12-04 04:24:22 -050055 do { \
56 if ((val) < (min) || (val) > (max)) { \
57 return ret; \
Hanno Becker1eeca412018-10-15 12:01:35 +010058 } \
Gilles Peskine449bd832023-01-11 14:50:10 +010059 } while (0)
Rich Evans7d5a55a2015-02-13 11:48:02 +000060
Paul Bakker7c6b2c32013-09-16 13:49:26 +020061/*
62 * CertificateSerialNumber ::= INTEGER
63 */
Gilles Peskine449bd832023-01-11 14:50:10 +010064int mbedtls_x509_get_serial(unsigned char **p, const unsigned char *end,
65 mbedtls_x509_buf *serial)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020066{
Janos Follath865b3eb2019-12-16 11:46:15 +000067 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020068
Gilles Peskine449bd832023-01-11 14:50:10 +010069 if ((end - *p) < 1) {
70 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_SERIAL,
71 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
72 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +020073
Gilles Peskine449bd832023-01-11 14:50:10 +010074 if (**p != (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_PRIMITIVE | 2) &&
75 **p != MBEDTLS_ASN1_INTEGER) {
76 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_SERIAL,
77 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
78 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +020079
80 serial->tag = *(*p)++;
81
Gilles Peskine449bd832023-01-11 14:50:10 +010082 if ((ret = mbedtls_asn1_get_len(p, end, &serial->len)) != 0) {
83 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_SERIAL, ret);
84 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +020085
86 serial->p = *p;
87 *p += serial->len;
88
Gilles Peskine449bd832023-01-11 14:50:10 +010089 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020090}
91
92/* Get an algorithm identifier without parameters (eg for signatures)
93 *
94 * AlgorithmIdentifier ::= SEQUENCE {
95 * algorithm OBJECT IDENTIFIER,
96 * parameters ANY DEFINED BY algorithm OPTIONAL }
97 */
Gilles Peskine449bd832023-01-11 14:50:10 +010098int mbedtls_x509_get_alg_null(unsigned char **p, const unsigned char *end,
99 mbedtls_x509_buf *alg)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200100{
Janos Follath865b3eb2019-12-16 11:46:15 +0000101 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200102
Gilles Peskine449bd832023-01-11 14:50:10 +0100103 if ((ret = mbedtls_asn1_get_alg_null(p, end, alg)) != 0) {
104 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
105 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200106
Gilles Peskine449bd832023-01-11 14:50:10 +0100107 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200108}
109
110/*
Antonin Décimo36e89b52019-01-23 15:24:37 +0100111 * Parse an algorithm identifier with (optional) parameters
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100112 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100113int mbedtls_x509_get_alg(unsigned char **p, const unsigned char *end,
114 mbedtls_x509_buf *alg, mbedtls_x509_buf *params)
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100115{
Janos Follath865b3eb2019-12-16 11:46:15 +0000116 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100117
Gilles Peskine449bd832023-01-11 14:50:10 +0100118 if ((ret = mbedtls_asn1_get_alg(p, end, alg, params)) != 0) {
119 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
120 }
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100121
Gilles Peskine449bd832023-01-11 14:50:10 +0100122 return 0;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100123}
124
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200125/*
126 * Convert md type to string
127 */
Dave Rodgmanffabb7b2023-06-28 16:22:50 +0100128#if !defined(MBEDTLS_X509_REMOVE_INFO) && defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
129
Gilles Peskine449bd832023-01-11 14:50:10 +0100130static inline const char *md_type_to_string(mbedtls_md_type_t md_alg)
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200131{
Gilles Peskine449bd832023-01-11 14:50:10 +0100132 switch (md_alg) {
Elena Uziunaiteb66a9912024-05-10 14:25:58 +0100133#if defined(PSA_WANT_ALG_MD5)
Gilles Peskine449bd832023-01-11 14:50:10 +0100134 case MBEDTLS_MD_MD5:
135 return "MD5";
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200136#endif
Elena Uziunaite9fc5be02024-09-04 18:12:59 +0100137#if defined(PSA_WANT_ALG_SHA_1)
Gilles Peskine449bd832023-01-11 14:50:10 +0100138 case MBEDTLS_MD_SHA1:
139 return "SHA1";
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200140#endif
Elena Uziunaitefcc9afa2024-05-23 14:43:22 +0100141#if defined(PSA_WANT_ALG_SHA_224)
Gilles Peskine449bd832023-01-11 14:50:10 +0100142 case MBEDTLS_MD_SHA224:
143 return "SHA224";
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200144#endif
Elena Uziunaite0916cd72024-05-23 17:01:07 +0100145#if defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine449bd832023-01-11 14:50:10 +0100146 case MBEDTLS_MD_SHA256:
147 return "SHA256";
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200148#endif
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +0100149#if defined(PSA_WANT_ALG_SHA_384)
Gilles Peskine449bd832023-01-11 14:50:10 +0100150 case MBEDTLS_MD_SHA384:
151 return "SHA384";
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200152#endif
Gabor Mezeic15ef932024-06-13 12:53:54 +0200153#if defined(PSA_WANT_ALG_SHA_512)
Gilles Peskine449bd832023-01-11 14:50:10 +0100154 case MBEDTLS_MD_SHA512:
155 return "SHA512";
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200156#endif
Elena Uziunaite1b6fb212024-05-10 16:17:41 +0100157#if defined(PSA_WANT_ALG_RIPEMD160)
Gilles Peskine449bd832023-01-11 14:50:10 +0100158 case MBEDTLS_MD_RIPEMD160:
159 return "RIPEMD160";
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200160#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100161 case MBEDTLS_MD_NONE:
162 return NULL;
163 default:
164 return NULL;
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200165 }
166}
167
Dave Rodgmanf032c982023-06-29 12:09:27 +0100168#endif /* !defined(MBEDTLS_X509_REMOVE_INFO) && defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) */
Dave Rodgmanffabb7b2023-06-28 16:22:50 +0100169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100171/*
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100172 * HashAlgorithm ::= AlgorithmIdentifier
173 *
174 * AlgorithmIdentifier ::= SEQUENCE {
175 * algorithm OBJECT IDENTIFIER,
176 * parameters ANY DEFINED BY algorithm OPTIONAL }
177 *
178 * For HashAlgorithm, parameters MUST be NULL or absent.
179 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100180static 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 +0100181{
Janos Follath865b3eb2019-12-16 11:46:15 +0000182 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100183 unsigned char *p;
184 const unsigned char *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200185 mbedtls_x509_buf md_oid;
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100186 size_t len;
187
188 /* Make sure we got a SEQUENCE and setup bounds */
Gilles Peskine449bd832023-01-11 14:50:10 +0100189 if (alg->tag != (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) {
190 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
191 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
192 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100193
Andrzej Kurekfeaebc52020-07-16 04:37:41 -0400194 p = alg->p;
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100195 end = p + alg->len;
196
Gilles Peskine449bd832023-01-11 14:50:10 +0100197 if (p >= end) {
198 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
199 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
200 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100201
202 /* Parse md_oid */
203 md_oid.tag = *p;
204
Gilles Peskine449bd832023-01-11 14:50:10 +0100205 if ((ret = mbedtls_asn1_get_tag(&p, end, &md_oid.len, MBEDTLS_ASN1_OID)) != 0) {
206 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
207 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100208
209 md_oid.p = p;
210 p += md_oid.len;
211
212 /* Get md_alg from md_oid */
Gilles Peskine532e3ee2025-05-07 20:37:15 +0200213 if ((ret = mbedtls_x509_oid_get_md_alg(&md_oid, md_alg)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100214 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
215 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100216
217 /* Make sure params is absent of NULL */
Gilles Peskine449bd832023-01-11 14:50:10 +0100218 if (p == end) {
219 return 0;
220 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100221
Gilles Peskine449bd832023-01-11 14:50:10 +0100222 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_NULL)) != 0 || len != 0) {
223 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
224 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100225
Gilles Peskine449bd832023-01-11 14:50:10 +0100226 if (p != end) {
227 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
228 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
229 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100230
Gilles Peskine449bd832023-01-11 14:50:10 +0100231 return 0;
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100232}
233
234/*
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100235 * RSASSA-PSS-params ::= SEQUENCE {
236 * hashAlgorithm [0] HashAlgorithm DEFAULT sha1Identifier,
237 * maskGenAlgorithm [1] MaskGenAlgorithm DEFAULT mgf1SHA1Identifier,
238 * saltLength [2] INTEGER DEFAULT 20,
239 * trailerField [3] INTEGER DEFAULT 1 }
240 * -- Note that the tags in this Sequence are explicit.
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200241 *
242 * RFC 4055 (which defines use of RSASSA-PSS in PKIX) states that the value
243 * of trailerField MUST be 1, and PKCS#1 v2.2 doesn't even define any other
Tom Cosgrove1797b052022-12-04 17:19:59 +0000244 * option. Enforce this at parsing time.
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100245 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100246int mbedtls_x509_get_rsassa_pss_params(const mbedtls_x509_buf *params,
247 mbedtls_md_type_t *md_alg, mbedtls_md_type_t *mgf_md,
248 int *salt_len)
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100249{
Janos Follath865b3eb2019-12-16 11:46:15 +0000250 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100251 unsigned char *p;
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100252 const unsigned char *end, *end2;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100253 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254 mbedtls_x509_buf alg_id, alg_params;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100255
256 /* First set everything to defaults */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257 *md_alg = MBEDTLS_MD_SHA1;
258 *mgf_md = MBEDTLS_MD_SHA1;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100259 *salt_len = 20;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100260
261 /* Make sure params is a SEQUENCE and setup bounds */
Gilles Peskine449bd832023-01-11 14:50:10 +0100262 if (params->tag != (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) {
263 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
264 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
265 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100266
267 p = (unsigned char *) params->p;
268 end = p + params->len;
269
Gilles Peskine449bd832023-01-11 14:50:10 +0100270 if (p == end) {
271 return 0;
272 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100273
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100274 /*
275 * HashAlgorithm
276 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100277 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
278 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
279 0)) == 0) {
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100280 end2 = p + len;
281
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100282 /* HashAlgorithm ::= AlgorithmIdentifier (without parameters) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100283 if ((ret = mbedtls_x509_get_alg_null(&p, end2, &alg_id)) != 0) {
284 return ret;
285 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100286
Gilles Peskine532e3ee2025-05-07 20:37:15 +0200287 if ((ret = mbedtls_x509_oid_get_md_alg(&alg_id, md_alg)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100288 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
289 }
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100290
Gilles Peskine449bd832023-01-11 14:50:10 +0100291 if (p != end2) {
292 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
293 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
294 }
295 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
296 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100297 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100298
Gilles Peskine449bd832023-01-11 14:50:10 +0100299 if (p == end) {
300 return 0;
301 }
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100302
303 /*
304 * MaskGenAlgorithm
305 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100306 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
307 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
308 1)) == 0) {
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100309 end2 = p + len;
310
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100311 /* MaskGenAlgorithm ::= AlgorithmIdentifier (params = HashAlgorithm) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100312 if ((ret = mbedtls_x509_get_alg(&p, end2, &alg_id, &alg_params)) != 0) {
313 return ret;
314 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100315
316 /* Only MFG1 is recognised for now */
Gilles Peskine449bd832023-01-11 14:50:10 +0100317 if (MBEDTLS_OID_CMP(MBEDTLS_OID_MGF1, &alg_id) != 0) {
318 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE,
Gilles Peskine4c832212025-05-07 23:05:12 +0200319 MBEDTLS_ERR_X509_UNKNOWN_OID);
Gilles Peskine449bd832023-01-11 14:50:10 +0100320 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100321
322 /* Parse HashAlgorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +0100323 if ((ret = x509_get_hash_alg(&alg_params, mgf_md)) != 0) {
324 return ret;
325 }
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100326
Gilles Peskine449bd832023-01-11 14:50:10 +0100327 if (p != end2) {
328 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
329 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
330 }
331 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
332 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100333 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100334
Gilles Peskine449bd832023-01-11 14:50:10 +0100335 if (p == end) {
336 return 0;
337 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100338
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100339 /*
340 * salt_len
341 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100342 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
343 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
344 2)) == 0) {
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100345 end2 = p + len;
346
Gilles Peskine449bd832023-01-11 14:50:10 +0100347 if ((ret = mbedtls_asn1_get_int(&p, end2, salt_len)) != 0) {
348 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
349 }
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100350
Gilles Peskine449bd832023-01-11 14:50:10 +0100351 if (p != end2) {
352 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
353 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
354 }
355 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
356 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100357 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100358
Gilles Peskine449bd832023-01-11 14:50:10 +0100359 if (p == end) {
360 return 0;
361 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100362
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100363 /*
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200364 * trailer_field (if present, must be 1)
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100365 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100366 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
367 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
368 3)) == 0) {
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200369 int trailer_field;
370
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100371 end2 = p + len;
372
Gilles Peskine449bd832023-01-11 14:50:10 +0100373 if ((ret = mbedtls_asn1_get_int(&p, end2, &trailer_field)) != 0) {
374 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
375 }
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100376
Gilles Peskine449bd832023-01-11 14:50:10 +0100377 if (p != end2) {
378 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
379 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
380 }
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200381
Gilles Peskine449bd832023-01-11 14:50:10 +0100382 if (trailer_field != 1) {
383 return MBEDTLS_ERR_X509_INVALID_ALG;
384 }
385 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
386 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100387 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100388
Gilles Peskine449bd832023-01-11 14:50:10 +0100389 if (p != end) {
390 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
391 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
392 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100393
Gilles Peskine449bd832023-01-11 14:50:10 +0100394 return 0;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100395}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200396#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100397
398/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200399 * AttributeTypeAndValue ::= SEQUENCE {
400 * type AttributeType,
401 * value AttributeValue }
402 *
403 * AttributeType ::= OBJECT IDENTIFIER
404 *
405 * AttributeValue ::= ANY DEFINED BY AttributeType
406 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100407static int x509_get_attr_type_value(unsigned char **p,
408 const unsigned char *end,
409 mbedtls_x509_name *cur)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200410{
Janos Follath865b3eb2019-12-16 11:46:15 +0000411 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200412 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200413 mbedtls_x509_buf *oid;
414 mbedtls_x509_buf *val;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200415
Gilles Peskine449bd832023-01-11 14:50:10 +0100416 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
417 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
418 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME, ret);
419 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200420
Hanno Becker12f62fb2019-02-12 17:22:36 +0000421 end = *p + len;
422
Gilles Peskine449bd832023-01-11 14:50:10 +0100423 if ((end - *p) < 1) {
424 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME,
425 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
426 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200427
428 oid = &cur->oid;
429 oid->tag = **p;
430
Gilles Peskine449bd832023-01-11 14:50:10 +0100431 if ((ret = mbedtls_asn1_get_tag(p, end, &oid->len, MBEDTLS_ASN1_OID)) != 0) {
432 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME, ret);
433 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200434
435 oid->p = *p;
436 *p += oid->len;
437
Gilles Peskine449bd832023-01-11 14:50:10 +0100438 if ((end - *p) < 1) {
439 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME,
440 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
441 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200442
Gilles Peskine449bd832023-01-11 14:50:10 +0100443 if (**p != MBEDTLS_ASN1_BMP_STRING && **p != MBEDTLS_ASN1_UTF8_STRING &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200444 **p != MBEDTLS_ASN1_T61_STRING && **p != MBEDTLS_ASN1_PRINTABLE_STRING &&
445 **p != MBEDTLS_ASN1_IA5_STRING && **p != MBEDTLS_ASN1_UNIVERSAL_STRING &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100446 **p != MBEDTLS_ASN1_BIT_STRING) {
447 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME,
448 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
449 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200450
451 val = &cur->val;
452 val->tag = *(*p)++;
453
Gilles Peskine449bd832023-01-11 14:50:10 +0100454 if ((ret = mbedtls_asn1_get_len(p, end, &val->len)) != 0) {
455 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME, ret);
456 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200457
458 val->p = *p;
459 *p += val->len;
460
Gilles Peskine449bd832023-01-11 14:50:10 +0100461 if (*p != end) {
462 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME,
463 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Hanno Becker12f62fb2019-02-12 17:22:36 +0000464 }
465
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200466 cur->next = NULL;
467
Gilles Peskine449bd832023-01-11 14:50:10 +0100468 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200469}
470
471/*
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000472 * Name ::= CHOICE { -- only one possibility for now --
473 * rdnSequence RDNSequence }
474 *
475 * RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
476 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200477 * RelativeDistinguishedName ::=
478 * SET OF AttributeTypeAndValue
479 *
480 * AttributeTypeAndValue ::= SEQUENCE {
481 * type AttributeType,
482 * value AttributeValue }
483 *
484 * AttributeType ::= OBJECT IDENTIFIER
485 *
486 * AttributeValue ::= ANY DEFINED BY AttributeType
Manuel Pégourié-Gonnard5d861852014-10-17 12:41:41 +0200487 *
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000488 * The data structure is optimized for the common case where each RDN has only
489 * one element, which is represented as a list of AttributeTypeAndValue.
490 * For the general case we still use a flat list, but we mark elements of the
491 * same set so that they are "merged" together in the functions that consume
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200492 * this list, eg mbedtls_x509_dn_gets().
David Horstmanned794832022-10-04 18:12:06 +0100493 *
David Horstmann11307a12022-10-17 18:10:23 +0100494 * On success, this function may allocate a linked list starting at cur->next
David Horstmanned794832022-10-04 18:12:06 +0100495 * that must later be free'd by the caller using mbedtls_free(). In error
496 * cases, this function frees all allocated memory internally and the caller
497 * has no freeing responsibilities.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200498 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100499int mbedtls_x509_get_name(unsigned char **p, const unsigned char *end,
500 mbedtls_x509_name *cur)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200501{
Janos Follath865b3eb2019-12-16 11:46:15 +0000502 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard5d861852014-10-17 12:41:41 +0200503 size_t set_len;
504 const unsigned char *end_set;
David Horstmanned794832022-10-04 18:12:06 +0100505 mbedtls_x509_name *head = cur;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200506
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100507 /* don't use recursion, we'd risk stack overflow if not optimized */
Gilles Peskine449bd832023-01-11 14:50:10 +0100508 while (1) {
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100509 /*
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000510 * parse SET
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100511 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100512 if ((ret = mbedtls_asn1_get_tag(p, end, &set_len,
513 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SET)) != 0) {
514 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME, ret);
David Horstmanned794832022-10-04 18:12:06 +0100515 goto error;
516 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200517
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100518 end_set = *p + set_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200519
Gilles Peskine449bd832023-01-11 14:50:10 +0100520 while (1) {
521 if ((ret = x509_get_attr_type_value(p, end_set, cur)) != 0) {
David Horstmanned794832022-10-04 18:12:06 +0100522 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +0100523 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200524
Gilles Peskine449bd832023-01-11 14:50:10 +0100525 if (*p == end_set) {
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000526 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100527 }
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000528
Manuel Pégourié-Gonnardeecb43c2015-05-12 12:56:41 +0200529 /* Mark this item as being no the only one in a set */
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000530 cur->next_merged = 1;
531
Gilles Peskine449bd832023-01-11 14:50:10 +0100532 cur->next = mbedtls_calloc(1, sizeof(mbedtls_x509_name));
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000533
Gilles Peskine449bd832023-01-11 14:50:10 +0100534 if (cur->next == NULL) {
David Horstmanned794832022-10-04 18:12:06 +0100535 ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
536 goto error;
537 }
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000538
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000539 cur = cur->next;
540 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200541
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100542 /*
543 * continue until end of SEQUENCE is reached
544 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100545 if (*p == end) {
546 return 0;
547 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200548
Gilles Peskine449bd832023-01-11 14:50:10 +0100549 cur->next = mbedtls_calloc(1, sizeof(mbedtls_x509_name));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200550
Gilles Peskine449bd832023-01-11 14:50:10 +0100551 if (cur->next == NULL) {
David Horstmanned794832022-10-04 18:12:06 +0100552 ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
553 goto error;
554 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200555
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100556 cur = cur->next;
557 }
David Horstmanned794832022-10-04 18:12:06 +0100558
559error:
David Horstmanned794832022-10-04 18:12:06 +0100560 /* Skip the first element as we did not allocate it */
Gilles Peskine449bd832023-01-11 14:50:10 +0100561 mbedtls_asn1_free_named_data_list_shallow(head->next);
Glenn Straussa4b40412022-06-26 19:32:09 -0400562 head->next = NULL;
David Horstmanned794832022-10-04 18:12:06 +0100563
Gilles Peskine449bd832023-01-11 14:50:10 +0100564 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200565}
566
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400567static int x509_date_is_valid(const mbedtls_x509_time *t)
Janos Follath87c98072017-02-03 12:36:59 +0000568{
David Horstmanncdf52832023-07-05 09:58:03 +0100569 unsigned int month_days;
570 unsigned int year;
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400571 switch (t->mon) {
572 case 1: case 3: case 5: case 7: case 8: case 10: case 12:
David Horstmanncdf52832023-07-05 09:58:03 +0100573 month_days = 31;
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400574 break;
575 case 4: case 6: case 9: case 11:
David Horstmanncdf52832023-07-05 09:58:03 +0100576 month_days = 30;
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400577 break;
578 case 2:
David Horstmanncdf52832023-07-05 09:58:03 +0100579 year = (unsigned int) t->year;
580 month_days = ((year & 3) || (!(year % 100)
581 && (year % 400)))
582 ? 28 : 29;
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400583 break;
584 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100585 return MBEDTLS_ERR_X509_INVALID_DATE;
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400586 }
Janos Follath87c98072017-02-03 12:36:59 +0000587
David Horstmannb1d27bc2023-07-05 10:00:31 +0100588 if ((unsigned int) (t->day - 1) >= month_days || /* (1 - days in month) */
David Horstmann3ae1c4c2023-07-05 11:15:08 +0100589 /* (unsigned int) (t->mon - 1) >= 12 || */ /* (1 - 12) checked above */
David Horstmannb1d27bc2023-07-05 10:00:31 +0100590 (unsigned int) t->year > 9999 || /* (0 - 9999) */
591 (unsigned int) t->hour > 23 || /* (0 - 23) */
592 (unsigned int) t->min > 59 || /* (0 - 59) */
593 (unsigned int) t->sec > 59) { /* (0 - 59) */
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400594 return MBEDTLS_ERR_X509_INVALID_DATE;
Rich Evans7d5a55a2015-02-13 11:48:02 +0000595 }
Janos Follath87c98072017-02-03 12:36:59 +0000596
Gilles Peskine449bd832023-01-11 14:50:10 +0100597 return 0;
Rich Evans7d5a55a2015-02-13 11:48:02 +0000598}
599
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400600static int x509_parse2_int(const unsigned char *p)
Andres AG4b76aec2016-09-23 13:16:02 +0100601{
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400602 uint32_t d1 = p[0] - '0';
603 uint32_t d2 = p[1] - '0';
604 return (d1 < 10 && d2 < 10) ? (int) (d1 * 10 + d2) : -1;
Andres AG4b76aec2016-09-23 13:16:02 +0100605}
606
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200607/*
Janos Follath87c98072017-02-03 12:36:59 +0000608 * Parse an ASN1_UTC_TIME (yearlen=2) or ASN1_GENERALIZED_TIME (yearlen=4)
609 * field.
610 */
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400611static int x509_parse_time(const unsigned char *p, mbedtls_x509_time *tm,
612 size_t yearlen)
Janos Follath87c98072017-02-03 12:36:59 +0000613{
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400614 int x;
Janos Follath87c98072017-02-03 12:36:59 +0000615
616 /*
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400617 * Parse year, month, day, hour, minute, second
Janos Follath87c98072017-02-03 12:36:59 +0000618 */
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400619 tm->year = x509_parse2_int(p);
620 if (tm->year < 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100621 return MBEDTLS_ERR_X509_INVALID_DATE;
622 }
Janos Follath87c98072017-02-03 12:36:59 +0000623
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400624 if (4 == yearlen) {
625 x = tm->year * 100;
626 p += 2;
627 tm->year = x509_parse2_int(p);
628 if (tm->year < 0) {
629 return MBEDTLS_ERR_X509_INVALID_DATE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100630 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100631 } else {
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400632 x = (tm->year < 50) ? 2000 : 1900;
Janos Follath87c98072017-02-03 12:36:59 +0000633 }
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400634 tm->year += x;
Janos Follath87c98072017-02-03 12:36:59 +0000635
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400636 tm->mon = x509_parse2_int(p + 2);
637 tm->day = x509_parse2_int(p + 4);
638 tm->hour = x509_parse2_int(p + 6);
639 tm->min = x509_parse2_int(p + 8);
640 tm->sec = x509_parse2_int(p + 10);
Janos Follath87c98072017-02-03 12:36:59 +0000641
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400642 return x509_date_is_valid(tm);
Janos Follath87c98072017-02-03 12:36:59 +0000643}
644
645/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200646 * Time ::= CHOICE {
647 * utcTime UTCTime,
648 * generalTime GeneralizedTime }
649 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100650int mbedtls_x509_get_time(unsigned char **p, const unsigned char *end,
651 mbedtls_x509_time *tm)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200652{
Janos Follath865b3eb2019-12-16 11:46:15 +0000653 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Janos Follath87c98072017-02-03 12:36:59 +0000654 size_t len, year_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200655 unsigned char tag;
656
Gilles Peskine449bd832023-01-11 14:50:10 +0100657 if ((end - *p) < 1) {
658 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE,
659 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
660 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200661
662 tag = **p;
663
Gilles Peskine449bd832023-01-11 14:50:10 +0100664 if (tag == MBEDTLS_ASN1_UTC_TIME) {
Janos Follath87c98072017-02-03 12:36:59 +0000665 year_len = 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100666 } else if (tag == MBEDTLS_ASN1_GENERALIZED_TIME) {
Janos Follath87c98072017-02-03 12:36:59 +0000667 year_len = 4;
Gilles Peskine449bd832023-01-11 14:50:10 +0100668 } else {
669 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE,
670 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
671 }
Janos Follath87c98072017-02-03 12:36:59 +0000672
673 (*p)++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100674 ret = mbedtls_asn1_get_len(p, end, &len);
Janos Follath87c98072017-02-03 12:36:59 +0000675
Gilles Peskine449bd832023-01-11 14:50:10 +0100676 if (ret != 0) {
677 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE, ret);
678 }
Janos Follath87c98072017-02-03 12:36:59 +0000679
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400680 /* len is 12 or 14 depending on year_len, plus optional trailing 'Z' */
681 if (len != year_len + 10 &&
682 !(len == year_len + 11 && (*p)[(len - 1)] == 'Z')) {
683 return MBEDTLS_ERR_X509_INVALID_DATE;
684 }
685
686 (*p) += len;
687 return x509_parse_time(*p - len, tm, year_len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200688}
689
Gilles Peskine449bd832023-01-11 14:50:10 +0100690int mbedtls_x509_get_sig(unsigned char **p, const unsigned char *end, mbedtls_x509_buf *sig)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200691{
Janos Follath865b3eb2019-12-16 11:46:15 +0000692 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200693 size_t len;
Andres AG4bdbe092016-09-19 16:58:45 +0100694 int tag_type;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200695
Gilles Peskine449bd832023-01-11 14:50:10 +0100696 if ((end - *p) < 1) {
697 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_SIGNATURE,
698 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
699 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200700
Andres AG4bdbe092016-09-19 16:58:45 +0100701 tag_type = **p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200702
Gilles Peskine449bd832023-01-11 14:50:10 +0100703 if ((ret = mbedtls_asn1_get_bitstring_null(p, end, &len)) != 0) {
704 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_SIGNATURE, ret);
705 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200706
Andres AG4bdbe092016-09-19 16:58:45 +0100707 sig->tag = tag_type;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200708 sig->len = len;
709 sig->p = *p;
710
711 *p += len;
712
Gilles Peskine449bd832023-01-11 14:50:10 +0100713 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200714}
715
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100716/*
717 * Get signature algorithm from alg OID and optional parameters
718 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100719int mbedtls_x509_get_sig_alg(const mbedtls_x509_buf *sig_oid, const mbedtls_x509_buf *sig_params,
Ben Taylorb2eecc62025-07-07 14:18:37 +0100720 mbedtls_md_type_t *md_alg, mbedtls_pk_sigalg_t *pk_alg)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200721{
Janos Follath865b3eb2019-12-16 11:46:15 +0000722 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200723
Ben Taylor75733212025-08-05 14:14:18 +0100724 if ((ret = mbedtls_x509_oid_get_sig_alg(sig_oid, md_alg, (mbedtls_pk_type_t *) pk_alg)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100725 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG, ret);
726 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200727
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200728#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Ben Taylorb2eecc62025-07-07 14:18:37 +0100729 if (*pk_alg == MBEDTLS_PK_SIGALG_RSA_PSS) {
Valerio Setti68878cc2025-04-10 23:30:26 +0200730 mbedtls_md_type_t mgf1_hash_id;
731 int expected_salt_len;
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200732
Gilles Peskine449bd832023-01-11 14:50:10 +0100733 ret = mbedtls_x509_get_rsassa_pss_params(sig_params,
734 md_alg,
Valerio Setti68878cc2025-04-10 23:30:26 +0200735 &mgf1_hash_id,
736 &expected_salt_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100737 if (ret != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100738 return ret;
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200739 }
Valerio Setti68878cc2025-04-10 23:30:26 +0200740 /* Ensure MGF1 hash alg is the same as the one used to hash the message. */
741 if (mgf1_hash_id != *md_alg) {
742 return MBEDTLS_ERR_X509_INVALID_ALG;
743 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100744 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200745#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100746 {
747 /* Make sure parameters are absent or NULL */
Gilles Peskine449bd832023-01-11 14:50:10 +0100748 if ((sig_params->tag != MBEDTLS_ASN1_NULL && sig_params->tag != 0) ||
749 sig_params->len != 0) {
750 return MBEDTLS_ERR_X509_INVALID_ALG;
751 }
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100752 }
753
Gilles Peskine449bd832023-01-11 14:50:10 +0100754 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200755}
756
757/*
758 * X.509 Extensions (No parsing of extensions, pointer should
Brian J Murray1903fb32016-11-06 04:45:15 -0800759 * be either manually updated or extensions should be parsed!)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200760 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100761int mbedtls_x509_get_ext(unsigned char **p, const unsigned char *end,
762 mbedtls_x509_buf *ext, int tag)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200763{
Janos Follath865b3eb2019-12-16 11:46:15 +0000764 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200765 size_t len;
766
Hanno Becker3cddba82019-02-11 14:33:36 +0000767 /* Extension structure use EXPLICIT tagging. That is, the actual
768 * `Extensions` structure is wrapped by a tag-length pair using
769 * the respective context-specific tag. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100770 ret = mbedtls_asn1_get_tag(p, end, &ext->len,
771 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | tag);
772 if (ret != 0) {
773 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
774 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200775
Hanno Becker6ccfb182019-02-12 11:52:10 +0000776 ext->tag = MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | tag;
777 ext->p = *p;
778 end = *p + ext->len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200779
780 /*
781 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200782 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100783 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
784 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
785 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
786 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200787
Gilles Peskine449bd832023-01-11 14:50:10 +0100788 if (end != *p + len) {
789 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
790 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
791 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200792
Gilles Peskine449bd832023-01-11 14:50:10 +0100793 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200794}
795
Agathiyan Bragadeesh9d2507c2023-07-24 16:35:57 +0100796static char nibble_to_hex_digit(int i)
Agathiyan Bragadeeshef2decb2023-07-21 15:47:47 +0100797{
Agathiyan Bragadeeshf0e1ac52023-07-24 16:43:36 +0100798 return (i < 10) ? (i + '0') : (i - 10 + 'A');
Agathiyan Bragadeeshef2decb2023-07-21 15:47:47 +0100799}
800
Sam Berry4f761942024-07-19 14:56:30 +0100801/* Return the x.y.z.... style numeric string for the given OID */
802int mbedtls_oid_get_numeric_string(char *buf, size_t size,
803 const mbedtls_asn1_buf *oid)
804{
805 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
806 char *p = buf;
807 size_t n = size;
808 unsigned int value = 0;
809
810 if (size > INT_MAX) {
811 /* Avoid overflow computing return value */
812 return MBEDTLS_ERR_ASN1_INVALID_LENGTH;
813 }
814
815 if (oid->len <= 0) {
816 /* OID must not be empty */
817 return MBEDTLS_ERR_ASN1_OUT_OF_DATA;
818 }
819
820 for (size_t i = 0; i < oid->len; i++) {
821 /* Prevent overflow in value. */
822 if (value > (UINT_MAX >> 7)) {
823 return MBEDTLS_ERR_ASN1_INVALID_DATA;
824 }
825 if ((value == 0) && ((oid->p[i]) == 0x80)) {
826 /* Overlong encoding is not allowed */
827 return MBEDTLS_ERR_ASN1_INVALID_DATA;
828 }
829
830 value <<= 7;
831 value |= oid->p[i] & 0x7F;
832
833 if (!(oid->p[i] & 0x80)) {
834 /* Last byte */
835 if (n == size) {
836 int component1;
837 unsigned int component2;
838 /* First subidentifier contains first two OID components */
839 if (value >= 80) {
840 component1 = '2';
841 component2 = value - 80;
842 } else if (value >= 40) {
843 component1 = '1';
844 component2 = value - 40;
845 } else {
846 component1 = '0';
847 component2 = value;
848 }
849 ret = mbedtls_snprintf(p, n, "%c.%u", component1, component2);
850 } else {
851 ret = mbedtls_snprintf(p, n, ".%u", value);
852 }
853 if (ret < 2 || (size_t) ret >= n) {
Gilles Peskine47f1d7b2025-05-07 21:04:51 +0200854 return PSA_ERROR_BUFFER_TOO_SMALL;
Sam Berry4f761942024-07-19 14:56:30 +0100855 }
856 n -= (size_t) ret;
857 p += ret;
858 value = 0;
859 }
860 }
861
862 if (value != 0) {
863 /* Unterminated subidentifier */
864 return MBEDTLS_ERR_ASN1_OUT_OF_DATA;
865 }
866
867 return (int) (size - n);
868}
869
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200870/*
871 * Store the name in printable form into buf; no more
872 * than size characters will be written
873 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100874int mbedtls_x509_dn_gets(char *buf, size_t size, const mbedtls_x509_name *dn)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200875{
Janos Follath865b3eb2019-12-16 11:46:15 +0000876 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh5adffb22023-08-10 15:50:57 +0100877 size_t i, j, n, asn1_len_size, asn1_tag_size, asn1_tag_len_buf_start;
Agathiyan Bragadeeshc7959b22023-09-12 17:54:43 +0100878 /* 6 is enough as our asn1 write functions only write one byte for the tag and at most five bytes for the length*/
879 unsigned char asn1_tag_len_buf[6];
Agathiyan Bragadeeshddc720d2023-07-26 15:51:49 +0100880 unsigned char *asn1_len_p;
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000881 unsigned char c, merge = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200882 const mbedtls_x509_name *name;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200883 const char *short_name = NULL;
Agathiyan Bragadeesha1f5c2d2023-08-02 17:08:52 +0100884 char lowbits, highbits;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200885 char s[MBEDTLS_X509_MAX_DN_NAME_SIZE], *p;
Agathiyan Bragadeeshc9d74f32023-07-31 17:25:44 +0100886 int print_hexstring;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200887
Gilles Peskine449bd832023-01-11 14:50:10 +0100888 memset(s, 0, sizeof(s));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200889
890 name = dn;
891 p = buf;
892 n = size;
893
Gilles Peskine449bd832023-01-11 14:50:10 +0100894 while (name != NULL) {
895 if (!name->oid.p) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200896 name = name->next;
897 continue;
898 }
899
Gilles Peskine449bd832023-01-11 14:50:10 +0100900 if (name != dn) {
901 ret = mbedtls_snprintf(p, n, merge ? " + " : ", ");
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200902 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200903 }
904
Agathiyan Bragadeesh0a4b6d82023-08-02 15:05:57 +0100905 print_hexstring = (name->val.tag != MBEDTLS_ASN1_UTF8_STRING) &&
906 (name->val.tag != MBEDTLS_ASN1_PRINTABLE_STRING) &&
907 (name->val.tag != MBEDTLS_ASN1_IA5_STRING);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200908
Gilles Peskine532e3ee2025-05-07 20:37:15 +0200909 if ((ret = mbedtls_x509_oid_get_attr_short_name(&name->oid, &short_name)) == 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100910 ret = mbedtls_snprintf(p, n, "%s=", short_name);
911 } else {
Agathiyan Bragadeeshaf70c7d2023-08-10 16:39:23 +0100912 if ((ret = mbedtls_oid_get_numeric_string(p, n, &name->oid)) > 0) {
Agathiyan Bragadeeshf3b97242023-08-22 16:37:11 +0100913 n -= ret;
914 p += ret;
Agathiyan Bragadeeshaf70c7d2023-08-10 16:39:23 +0100915 ret = mbedtls_snprintf(p, n, "=");
Agathiyan Bragadeeshc9d74f32023-07-31 17:25:44 +0100916 print_hexstring = 1;
Gilles Peskine47f1d7b2025-05-07 21:04:51 +0200917 } else if (ret == PSA_ERROR_BUFFER_TOO_SMALL) {
Agathiyan Bragadeesh8aa74ab2023-08-22 16:42:27 +0100918 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
Agathiyan Bragadeesh15df0122023-08-22 17:50:00 +0100919 } else {
Agathiyan Bragadeeshddc720d2023-07-26 15:51:49 +0100920 ret = mbedtls_snprintf(p, n, "\?\?=");
921 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100922 }
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200923 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200924
Agathiyan Bragadeesh4987c8f2023-08-01 11:10:52 +0100925 if (print_hexstring) {
Agathiyan Bragadeeshddc720d2023-07-26 15:51:49 +0100926 s[0] = '#';
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200927
Agathiyan Bragadeeshc7959b22023-09-12 17:54:43 +0100928 asn1_len_p = asn1_tag_len_buf + sizeof(asn1_tag_len_buf);
Agathiyan Bragadeesh15df0122023-08-22 17:50:00 +0100929 if ((ret = mbedtls_asn1_write_len(&asn1_len_p, asn1_tag_len_buf, name->val.len)) < 0) {
Agathiyan Bragadeesh07f472a2023-08-22 16:29:39 +0100930 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
931 }
932 asn1_len_size = ret;
Agathiyan Bragadeesh15df0122023-08-22 17:50:00 +0100933 if ((ret = mbedtls_asn1_write_tag(&asn1_len_p, asn1_tag_len_buf, name->val.tag)) < 0) {
Agathiyan Bragadeesh07f472a2023-08-22 16:29:39 +0100934 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
935 }
936 asn1_tag_size = ret;
Agathiyan Bragadeeshc7959b22023-09-12 17:54:43 +0100937 asn1_tag_len_buf_start = sizeof(asn1_tag_len_buf) - asn1_len_size - asn1_tag_size;
Agathiyan Bragadeesh5adffb22023-08-10 15:50:57 +0100938 for (i = 0, j = 1; i < asn1_len_size + asn1_tag_size; i++) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100939 if (j + 1 >= sizeof(s) - 1) {
940 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
941 }
Agathiyan Bragadeesh5adffb22023-08-10 15:50:57 +0100942 c = asn1_tag_len_buf[asn1_tag_len_buf_start+i];
Agathiyan Bragadeesha1f5c2d2023-08-02 17:08:52 +0100943 lowbits = (c & 0x0F);
Agathiyan Bragadeesh2bf09a62023-08-10 14:37:00 +0100944 highbits = c >> 4;
Agathiyan Bragadeesh9d2507c2023-07-24 16:35:57 +0100945 s[j++] = nibble_to_hex_digit(highbits);
Agathiyan Bragadeesh5adffb22023-08-10 15:50:57 +0100946 s[j++] = nibble_to_hex_digit(lowbits);
Werner Lewisb33dacd2022-05-20 12:48:46 +0100947 }
Agathiyan Bragadeesh5adffb22023-08-10 15:50:57 +0100948 for (i = 0; i < name->val.len; i++) {
949 if (j + 1 >= sizeof(s) - 1) {
950 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
951 }
952 c = name->val.p[i];
953 lowbits = (c & 0x0F);
954 highbits = c >> 4;
955 s[j++] = nibble_to_hex_digit(highbits);
956 s[j++] = nibble_to_hex_digit(lowbits);
Agathiyan Bragadeeshddc720d2023-07-26 15:51:49 +0100957 }
958 } else {
959 for (i = 0, j = 0; i < name->val.len; i++, j++) {
960 if (j >= sizeof(s) - 1) {
961 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
962 }
963
964 c = name->val.p[i];
965 // Special characters requiring escaping, RFC 4514 Section 2.4
Agathiyan Bragadeesh022f86f2023-08-22 16:56:04 +0100966 if (c == '\0') {
967 return MBEDTLS_ERR_X509_INVALID_NAME;
Agathiyan Bragadeesh15df0122023-08-22 17:50:00 +0100968 } else {
Agathiyan Bragadeesha7f96302023-08-10 16:03:27 +0100969 if (strchr(",=+<>;\"\\", c) ||
Agathiyan Bragadeeshddc720d2023-07-26 15:51:49 +0100970 ((i == 0) && strchr("# ", c)) ||
971 ((i == name->val.len-1) && (c == ' '))) {
972 if (j + 1 >= sizeof(s) - 1) {
973 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
974 }
975 s[j++] = '\\';
976 }
977 }
978 if (c < 32 || c >= 127) {
979 if (j + 3 >= sizeof(s) - 1) {
980 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
981 }
982 s[j++] = '\\';
Agathiyan Bragadeesha1f5c2d2023-08-02 17:08:52 +0100983 lowbits = (c & 0x0F);
Agathiyan Bragadeesh2bf09a62023-08-10 14:37:00 +0100984 highbits = c >> 4;
Agathiyan Bragadeeshddc720d2023-07-26 15:51:49 +0100985 s[j++] = nibble_to_hex_digit(highbits);
986 s[j] = nibble_to_hex_digit(lowbits);
987 } else {
988 s[j] = c;
989 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100990 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200991 }
Werner Lewisb33dacd2022-05-20 12:48:46 +0100992 s[j] = '\0';
Gilles Peskine449bd832023-01-11 14:50:10 +0100993 ret = mbedtls_snprintf(p, n, "%s", s);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200994 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000995
996 merge = name->next_merged;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200997 name = name->next;
998 }
999
Gilles Peskine449bd832023-01-11 14:50:10 +01001000 return (int) (size - n);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001001}
1002
1003/*
1004 * Store the serial in printable form into buf; no more
1005 * than size characters will be written
1006 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001007int mbedtls_x509_serial_gets(char *buf, size_t size, const mbedtls_x509_buf *serial)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001008{
Janos Follath865b3eb2019-12-16 11:46:15 +00001009 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001010 size_t i, n, nr;
1011 char *p;
1012
1013 p = buf;
1014 n = size;
1015
Gilles Peskine449bd832023-01-11 14:50:10 +01001016 nr = (serial->len <= 32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001017 ? serial->len : 28;
1018
Gilles Peskine449bd832023-01-11 14:50:10 +01001019 for (i = 0; i < nr; i++) {
1020 if (i == 0 && nr > 1 && serial->p[i] == 0x0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001021 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001022 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001023
Gilles Peskine449bd832023-01-11 14:50:10 +01001024 ret = mbedtls_snprintf(p, n, "%02X%s",
1025 serial->p[i], (i < nr - 1) ? ":" : "");
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001026 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001027 }
1028
Gilles Peskine449bd832023-01-11 14:50:10 +01001029 if (nr != serial->len) {
1030 ret = mbedtls_snprintf(p, n, "....");
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001031 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001032 }
1033
Gilles Peskine449bd832023-01-11 14:50:10 +01001034 return (int) (size - n);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001035}
1036
Hanno Becker612a2f12020-10-09 09:19:39 +01001037#if !defined(MBEDTLS_X509_REMOVE_INFO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001038/*
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +02001039 * Helper for writing signature algorithms
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +01001040 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001041int mbedtls_x509_sig_alg_gets(char *buf, size_t size, const mbedtls_x509_buf *sig_oid,
Ben Taylorb2eecc62025-07-07 14:18:37 +01001042 mbedtls_pk_sigalg_t pk_alg, mbedtls_md_type_t md_alg)
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +01001043{
Janos Follath865b3eb2019-12-16 11:46:15 +00001044 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +01001045 char *p = buf;
1046 size_t n = size;
1047 const char *desc = NULL;
1048
Gilles Peskine532e3ee2025-05-07 20:37:15 +02001049 ret = mbedtls_x509_oid_get_sig_alg_desc(sig_oid, &desc);
Gilles Peskine449bd832023-01-11 14:50:10 +01001050 if (ret != 0) {
1051 ret = mbedtls_snprintf(p, n, "???");
1052 } else {
1053 ret = mbedtls_snprintf(p, n, "%s", desc);
1054 }
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001055 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +01001056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001057#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Ben Taylorb2eecc62025-07-07 14:18:37 +01001058 if (pk_alg == MBEDTLS_PK_SIGALG_RSA_PSS) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001059 const char *name = md_type_to_string(md_alg);
Valerio Settid24dfad2025-04-23 11:13:02 +02001060 if (name != NULL) {
1061 ret = mbedtls_snprintf(p, n, " (%s)", name);
1062 } else {
1063 ret = mbedtls_snprintf(p, n, " (?)");
1064 }
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001065 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +01001066 }
1067#else
1068 ((void) pk_alg);
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +02001069 ((void) md_alg);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001070#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +01001071
Gilles Peskine449bd832023-01-11 14:50:10 +01001072 return (int) (size - n);
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +01001073}
Hanno Becker612a2f12020-10-09 09:19:39 +01001074#endif /* MBEDTLS_X509_REMOVE_INFO */
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +01001075
1076/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001077 * Helper for writing "RSA key size", "EC key size", etc
1078 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001079int mbedtls_x509_key_size_helper(char *buf, size_t buf_size, const char *name)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001080{
1081 char *p = buf;
Manuel Pégourié-Gonnardfb317c52015-06-18 16:25:56 +02001082 size_t n = buf_size;
Janos Follath865b3eb2019-12-16 11:46:15 +00001083 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001084
Gilles Peskine449bd832023-01-11 14:50:10 +01001085 ret = mbedtls_snprintf(p, n, "%s key size", name);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001086 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001087
Gilles Peskine449bd832023-01-11 14:50:10 +01001088 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001089}
1090
Glenn Strauss416dc032022-06-30 00:38:53 -04001091int mbedtls_x509_time_cmp(const mbedtls_x509_time *t1,
1092 const mbedtls_x509_time *t2)
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001093{
Glenn Strauss5aef2972022-06-30 04:38:02 -04001094 int x;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001095
Glenn Strauss5aef2972022-06-30 04:38:02 -04001096 x = (((t1->year << 9) | (t1->mon << 5) | (t1->day)) -
1097 ((t2->year << 9) | (t2->mon << 5) | (t2->day)));
1098 if (x != 0) {
1099 return x;
Manuel Pégourié-Gonnard57e10d72015-06-22 18:59:21 +02001100 }
Manuel Pégourié-Gonnard864108d2015-05-29 10:11:03 +02001101
Glenn Strauss5aef2972022-06-30 04:38:02 -04001102 x = (((t1->hour << 12) | (t1->min << 6) | (t1->sec)) -
1103 ((t2->hour << 12) | (t2->min << 6) | (t2->sec)));
1104 return x;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001105}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001106
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001107#if defined(MBEDTLS_HAVE_TIME_DATE)
Glenn Strauss61d99302022-06-30 05:25:56 -04001108int mbedtls_x509_time_gmtime(mbedtls_time_t tt, mbedtls_x509_time *now)
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001109{
Glenn Strauss811eeb22022-06-30 05:28:50 -04001110 struct tm tm;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001111
Glenn Strauss811eeb22022-06-30 05:28:50 -04001112 if (mbedtls_platform_gmtime_r(&tt, &tm) == NULL) {
1113 return -1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001114 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001115
Glenn Strauss811eeb22022-06-30 05:28:50 -04001116 now->year = tm.tm_year + 1900;
1117 now->mon = tm.tm_mon + 1;
1118 now->day = tm.tm_mday;
1119 now->hour = tm.tm_hour;
1120 now->min = tm.tm_min;
1121 now->sec = tm.tm_sec;
Gilles Peskine449bd832023-01-11 14:50:10 +01001122 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001123}
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001124
Glenn Strauss61d99302022-06-30 05:25:56 -04001125static int x509_get_current_time(mbedtls_x509_time *now)
1126{
1127 return mbedtls_x509_time_gmtime(mbedtls_time(NULL), now);
1128}
1129
Gilles Peskine449bd832023-01-11 14:50:10 +01001130int mbedtls_x509_time_is_past(const mbedtls_x509_time *to)
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001131{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001132 mbedtls_x509_time now;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001133
Gilles Peskine449bd832023-01-11 14:50:10 +01001134 if (x509_get_current_time(&now) != 0) {
1135 return 1;
1136 }
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001137
Glenn Strauss416dc032022-06-30 00:38:53 -04001138 return mbedtls_x509_time_cmp(to, &now) < 0;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001139}
1140
Gilles Peskine449bd832023-01-11 14:50:10 +01001141int mbedtls_x509_time_is_future(const mbedtls_x509_time *from)
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001142{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001143 mbedtls_x509_time now;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001144
Gilles Peskine449bd832023-01-11 14:50:10 +01001145 if (x509_get_current_time(&now) != 0) {
1146 return 1;
1147 }
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001148
Glenn Strauss416dc032022-06-30 00:38:53 -04001149 return mbedtls_x509_time_cmp(from, &now) > 0;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001150}
1151
Manuel Pégourié-Gonnard60c793b2015-06-18 20:52:58 +02001152#else /* MBEDTLS_HAVE_TIME_DATE */
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001153
Gilles Peskine449bd832023-01-11 14:50:10 +01001154int mbedtls_x509_time_is_past(const mbedtls_x509_time *to)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001155{
1156 ((void) to);
Gilles Peskine449bd832023-01-11 14:50:10 +01001157 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001158}
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001159
Gilles Peskine449bd832023-01-11 14:50:10 +01001160int mbedtls_x509_time_is_future(const mbedtls_x509_time *from)
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001161{
1162 ((void) from);
Gilles Peskine449bd832023-01-11 14:50:10 +01001163 return 0;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001164}
Manuel Pégourié-Gonnard60c793b2015-06-18 20:52:58 +02001165#endif /* MBEDTLS_HAVE_TIME_DATE */
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001166
1167/* Common functions for parsing CRT and CSR. */
1168#if defined(MBEDTLS_X509_CRT_PARSE_C) || defined(MBEDTLS_X509_CSR_PARSE_C)
1169/*
1170 * OtherName ::= SEQUENCE {
1171 * type-id OBJECT IDENTIFIER,
1172 * value [0] EXPLICIT ANY DEFINED BY type-id }
1173 *
1174 * HardwareModuleName ::= SEQUENCE {
1175 * hwType OBJECT IDENTIFIER,
1176 * hwSerialNum OCTET STRING }
1177 *
1178 * NOTE: we currently only parse and use otherName of type HwModuleName,
1179 * as defined in RFC 4108.
1180 */
1181static int x509_get_other_name(const mbedtls_x509_buf *subject_alt_name,
1182 mbedtls_x509_san_other_name *other_name)
1183{
1184 int ret = 0;
1185 size_t len;
1186 unsigned char *p = subject_alt_name->p;
1187 const unsigned char *end = p + subject_alt_name->len;
1188 mbedtls_x509_buf cur_oid;
1189
1190 if ((subject_alt_name->tag &
1191 (MBEDTLS_ASN1_TAG_CLASS_MASK | MBEDTLS_ASN1_TAG_VALUE_MASK)) !=
1192 (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME)) {
1193 /*
1194 * The given subject alternative name is not of type "othername".
1195 */
1196 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1197 }
1198
1199 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1200 MBEDTLS_ASN1_OID)) != 0) {
1201 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1202 }
1203
1204 cur_oid.tag = MBEDTLS_ASN1_OID;
1205 cur_oid.p = p;
1206 cur_oid.len = len;
1207
1208 /*
1209 * Only HwModuleName is currently supported.
1210 */
1211 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ON_HW_MODULE_NAME, &cur_oid) != 0) {
1212 return MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
1213 }
David Horstmann2ea44d22023-08-18 18:36:02 +01001214 other_name->type_id = cur_oid;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001215
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001216 p += len;
1217 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1218 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC)) !=
1219 0) {
1220 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1221 }
1222
Hanno Beckerdae916b2019-09-13 14:21:13 +01001223 if (end != p + len) {
1224 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1225 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1226 }
1227
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001228 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1229 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1230 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1231 }
1232
Hanno Beckerdae916b2019-09-13 14:21:13 +01001233 if (end != p + len) {
1234 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1235 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1236 }
1237
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001238 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OID)) != 0) {
1239 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1240 }
1241
1242 other_name->value.hardware_module_name.oid.tag = MBEDTLS_ASN1_OID;
1243 other_name->value.hardware_module_name.oid.p = p;
1244 other_name->value.hardware_module_name.oid.len = len;
1245
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001246 p += len;
1247 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1248 MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1249 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1250 }
1251
1252 other_name->value.hardware_module_name.val.tag = MBEDTLS_ASN1_OCTET_STRING;
1253 other_name->value.hardware_module_name.val.p = p;
1254 other_name->value.hardware_module_name.val.len = len;
1255 p += len;
1256 if (p != end) {
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001257 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1258 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1259 }
1260 return 0;
1261}
1262
Przemek Stekiel21903ec2023-02-21 08:32:37 +01001263/* Check mbedtls_x509_get_subject_alt_name for detailed description.
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001264 *
Przemek Stekiel21903ec2023-02-21 08:32:37 +01001265 * In some cases while parsing subject alternative names the sequence tag is optional
1266 * (e.g. CertSerialNumber). This function is designed to handle such case.
Przemek Stekiel725688b2023-04-04 22:49:44 +02001267 */
Przemek Stekiel21903ec2023-02-21 08:32:37 +01001268int mbedtls_x509_get_subject_alt_name_ext(unsigned char **p,
1269 const unsigned char *end,
1270 mbedtls_x509_sequence *subject_alt_name)
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001271{
1272 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel21903ec2023-02-21 08:32:37 +01001273 size_t tag_len;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001274 mbedtls_asn1_sequence *cur = subject_alt_name;
1275
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001276 while (*p < end) {
Andrzej Kurek154a6052023-04-30 14:11:49 -04001277 mbedtls_x509_subject_alternative_name tmp_san_name;
Hanno Beckerae8f8c42019-09-13 12:24:56 +01001278 mbedtls_x509_buf tmp_san_buf;
Andrzej Kurek154a6052023-04-30 14:11:49 -04001279 memset(&tmp_san_name, 0, sizeof(tmp_san_name));
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001280
Hanno Beckerae8f8c42019-09-13 12:24:56 +01001281 tmp_san_buf.tag = **p;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001282 (*p)++;
Hanno Beckerae8f8c42019-09-13 12:24:56 +01001283
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001284 if ((ret = mbedtls_asn1_get_len(p, end, &tag_len)) != 0) {
1285 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1286 }
1287
Hanno Beckerae8f8c42019-09-13 12:24:56 +01001288 tmp_san_buf.p = *p;
1289 tmp_san_buf.len = tag_len;
1290
1291 if ((tmp_san_buf.tag & MBEDTLS_ASN1_TAG_CLASS_MASK) !=
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001292 MBEDTLS_ASN1_CONTEXT_SPECIFIC) {
1293 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1294 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
1295 }
1296
1297 /*
Andrzej Kurek154a6052023-04-30 14:11:49 -04001298 * Check that the SAN is structured correctly by parsing it.
1299 * The SAN structure is discarded afterwards.
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001300 */
Andrzej Kurek154a6052023-04-30 14:11:49 -04001301 ret = mbedtls_x509_parse_subject_alt_name(&tmp_san_buf, &tmp_san_name);
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001302 /*
1303 * In case the extension is malformed, return an error,
1304 * and clear the allocated sequences.
1305 */
1306 if (ret != 0 && ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE) {
1307 mbedtls_asn1_sequence_free(subject_alt_name->next);
1308 subject_alt_name->next = NULL;
1309 return ret;
1310 }
1311
Andrzej Kurek154a6052023-04-30 14:11:49 -04001312 mbedtls_x509_free_subject_alt_name(&tmp_san_name);
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001313 /* Allocate and assign next pointer */
1314 if (cur->buf.p != NULL) {
1315 if (cur->next != NULL) {
1316 return MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
1317 }
1318
1319 cur->next = mbedtls_calloc(1, sizeof(mbedtls_asn1_sequence));
1320
1321 if (cur->next == NULL) {
1322 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1323 MBEDTLS_ERR_ASN1_ALLOC_FAILED);
1324 }
1325
1326 cur = cur->next;
1327 }
1328
Hanno Beckerae8f8c42019-09-13 12:24:56 +01001329 cur->buf = tmp_san_buf;
1330 *p += tmp_san_buf.len;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001331 }
1332
1333 /* Set final sequence entry's next pointer to NULL */
1334 cur->next = NULL;
1335
1336 if (*p != end) {
1337 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1338 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1339 }
1340
1341 return 0;
1342}
1343
Przemek Stekiel21903ec2023-02-21 08:32:37 +01001344/*
1345 * SubjectAltName ::= GeneralNames
1346 *
1347 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
1348 *
1349 * GeneralName ::= CHOICE {
1350 * otherName [0] OtherName,
1351 * rfc822Name [1] IA5String,
1352 * dNSName [2] IA5String,
1353 * x400Address [3] ORAddress,
1354 * directoryName [4] Name,
1355 * ediPartyName [5] EDIPartyName,
1356 * uniformResourceIdentifier [6] IA5String,
1357 * iPAddress [7] OCTET STRING,
1358 * registeredID [8] OBJECT IDENTIFIER }
1359 *
1360 * OtherName ::= SEQUENCE {
1361 * type-id OBJECT IDENTIFIER,
1362 * value [0] EXPLICIT ANY DEFINED BY type-id }
1363 *
1364 * EDIPartyName ::= SEQUENCE {
1365 * nameAssigner [0] DirectoryString OPTIONAL,
1366 * partyName [1] DirectoryString }
1367 *
1368 * We list all types, but use the following GeneralName types from RFC 5280:
1369 * "dnsName", "uniformResourceIdentifier" and "hardware_module_name"
1370 * of type "otherName", as defined in RFC 4108.
1371 */
1372int mbedtls_x509_get_subject_alt_name(unsigned char **p,
1373 const unsigned char *end,
1374 mbedtls_x509_sequence *subject_alt_name)
1375{
1376 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1377 size_t len;
1378
1379 /* Get main sequence tag */
1380 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
1381 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1382 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1383 }
1384
1385 if (*p + len != end) {
1386 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1387 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1388 }
1389
1390 return mbedtls_x509_get_subject_alt_name_ext(p, end, subject_alt_name);
1391}
1392
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001393int mbedtls_x509_get_ns_cert_type(unsigned char **p,
1394 const unsigned char *end,
1395 unsigned char *ns_cert_type)
1396{
1397 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1398 mbedtls_x509_bitstring bs = { 0, 0, NULL };
1399
1400 if ((ret = mbedtls_asn1_get_bitstring(p, end, &bs)) != 0) {
1401 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1402 }
1403
Przemek Stekiel32e20912023-01-25 14:26:15 +01001404 /* A bitstring with no flags set is still technically valid, as it will mean
1405 that the certificate has no designated purpose at the time of creation. */
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001406 if (bs.len == 0) {
1407 *ns_cert_type = 0;
1408 return 0;
1409 }
1410
1411 if (bs.len != 1) {
1412 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1413 MBEDTLS_ERR_ASN1_INVALID_LENGTH);
1414 }
1415
1416 /* Get actual bitstring */
1417 *ns_cert_type = *bs.p;
1418 return 0;
1419}
1420
1421int mbedtls_x509_get_key_usage(unsigned char **p,
1422 const unsigned char *end,
1423 unsigned int *key_usage)
1424{
1425 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1426 size_t i;
1427 mbedtls_x509_bitstring bs = { 0, 0, NULL };
1428
1429 if ((ret = mbedtls_asn1_get_bitstring(p, end, &bs)) != 0) {
1430 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1431 }
1432
Przemek Stekiel32e20912023-01-25 14:26:15 +01001433 /* A bitstring with no flags set is still technically valid, as it will mean
1434 that the certificate has no designated purpose at the time of creation. */
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001435 if (bs.len == 0) {
1436 *key_usage = 0;
1437 return 0;
1438 }
1439
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001440 /* Get actual bitstring */
1441 *key_usage = 0;
1442 for (i = 0; i < bs.len && i < sizeof(unsigned int); i++) {
1443 *key_usage |= (unsigned int) bs.p[i] << (8*i);
1444 }
1445
1446 return 0;
1447}
1448
1449int mbedtls_x509_parse_subject_alt_name(const mbedtls_x509_buf *san_buf,
1450 mbedtls_x509_subject_alternative_name *san)
1451{
1452 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1453 switch (san_buf->tag &
1454 (MBEDTLS_ASN1_TAG_CLASS_MASK |
1455 MBEDTLS_ASN1_TAG_VALUE_MASK)) {
1456 /*
1457 * otherName
1458 */
1459 case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME):
1460 {
1461 mbedtls_x509_san_other_name other_name;
1462
1463 ret = x509_get_other_name(san_buf, &other_name);
1464 if (ret != 0) {
1465 return ret;
1466 }
1467
1468 memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name));
1469 san->type = MBEDTLS_X509_SAN_OTHER_NAME;
1470 memcpy(&san->san.other_name,
1471 &other_name, sizeof(other_name));
1472
1473 }
1474 break;
Andrzej Kurek7a05fab2023-02-13 10:03:07 -05001475 /*
1476 * uniformResourceIdentifier
1477 */
1478 case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER):
1479 {
1480 memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name));
1481 san->type = MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001482
Andrzej Kurek7a05fab2023-02-13 10:03:07 -05001483 memcpy(&san->san.unstructured_name,
1484 san_buf, sizeof(*san_buf));
1485
1486 }
1487 break;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001488 /*
1489 * dNSName
1490 */
1491 case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_DNS_NAME):
1492 {
1493 memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name));
1494 san->type = MBEDTLS_X509_SAN_DNS_NAME;
1495
1496 memcpy(&san->san.unstructured_name,
1497 san_buf, sizeof(*san_buf));
Przemek Stekielecee12f2023-02-09 14:43:49 +01001498 }
1499 break;
Przemek Stekiel0ab5b932023-05-29 16:30:50 +02001500 /*
1501 * IP address
1502 */
1503 case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_IP_ADDRESS):
1504 {
1505 memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name));
1506 san->type = MBEDTLS_X509_SAN_IP_ADDRESS;
Przemek Stekiel01cb6eb2023-06-05 16:38:13 +02001507 // Only IPv6 (16 bytes) and IPv4 (4 bytes) types are supported
1508 if (san_buf->len == 4 || san_buf->len == 16) {
1509 memcpy(&san->san.unstructured_name,
1510 san_buf, sizeof(*san_buf));
1511 } else {
1512 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1513 }
Przemek Stekiel0ab5b932023-05-29 16:30:50 +02001514 }
1515 break;
Przemek Stekielecee12f2023-02-09 14:43:49 +01001516 /*
Andrzej Kurek154a6052023-04-30 14:11:49 -04001517 * rfc822Name
Przemek Stekielecee12f2023-02-09 14:43:49 +01001518 */
1519 case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_RFC822_NAME):
1520 {
1521 memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name));
1522 san->type = MBEDTLS_X509_SAN_RFC822_NAME;
1523 memcpy(&san->san.unstructured_name, san_buf, sizeof(*san_buf));
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001524 }
1525 break;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001526 /*
Andrzej Kureke12b01d2023-01-10 06:47:38 -05001527 * directoryName
1528 */
1529 case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_DIRECTORY_NAME):
1530 {
1531 size_t name_len;
1532 unsigned char *p = san_buf->p;
1533 memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name));
1534 san->type = MBEDTLS_X509_SAN_DIRECTORY_NAME;
1535
1536 ret = mbedtls_asn1_get_tag(&p, p + san_buf->len, &name_len,
1537 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE);
1538
1539 if (ret != 0) {
Andrzej Kurekbf8ccd82023-02-13 07:13:30 -05001540 return ret;
Andrzej Kureke12b01d2023-01-10 06:47:38 -05001541 }
1542
1543 if ((ret = mbedtls_x509_get_name(&p, p + name_len,
1544 &san->san.directory_name)) != 0) {
Andrzej Kurekbf8ccd82023-02-13 07:13:30 -05001545 return ret;
Andrzej Kureke12b01d2023-01-10 06:47:38 -05001546 }
1547 }
1548 break;
1549 /*
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001550 * Type not supported
1551 */
1552 default:
1553 return MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
1554 }
1555 return 0;
1556}
1557
Andrzej Kurekd40c2b62023-02-13 07:01:59 -05001558void mbedtls_x509_free_subject_alt_name(mbedtls_x509_subject_alternative_name *san)
1559{
1560 if (san->type == MBEDTLS_X509_SAN_DIRECTORY_NAME) {
1561 mbedtls_asn1_free_named_data_list_shallow(san->san.directory_name.next);
1562 }
1563}
1564
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001565#if !defined(MBEDTLS_X509_REMOVE_INFO)
1566int mbedtls_x509_info_subject_alt_name(char **buf, size_t *size,
1567 const mbedtls_x509_sequence
1568 *subject_alt_name,
1569 const char *prefix)
1570{
1571 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1572 size_t i;
1573 size_t n = *size;
1574 char *p = *buf;
1575 const mbedtls_x509_sequence *cur = subject_alt_name;
1576 mbedtls_x509_subject_alternative_name san;
1577 int parse_ret;
1578
1579 while (cur != NULL) {
1580 memset(&san, 0, sizeof(san));
1581 parse_ret = mbedtls_x509_parse_subject_alt_name(&cur->buf, &san);
1582 if (parse_ret != 0) {
1583 if (parse_ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE) {
1584 ret = mbedtls_snprintf(p, n, "\n%s <unsupported>", prefix);
1585 MBEDTLS_X509_SAFE_SNPRINTF;
1586 } else {
1587 ret = mbedtls_snprintf(p, n, "\n%s <malformed>", prefix);
1588 MBEDTLS_X509_SAFE_SNPRINTF;
1589 }
1590 cur = cur->next;
1591 continue;
1592 }
1593
1594 switch (san.type) {
1595 /*
1596 * otherName
1597 */
1598 case MBEDTLS_X509_SAN_OTHER_NAME:
1599 {
1600 mbedtls_x509_san_other_name *other_name = &san.san.other_name;
1601
1602 ret = mbedtls_snprintf(p, n, "\n%s otherName :", prefix);
1603 MBEDTLS_X509_SAFE_SNPRINTF;
1604
1605 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ON_HW_MODULE_NAME,
David Horstmanncfae6a12023-08-18 19:12:59 +01001606 &other_name->type_id) == 0) {
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001607 ret = mbedtls_snprintf(p, n, "\n%s hardware module name :", prefix);
1608 MBEDTLS_X509_SAFE_SNPRINTF;
1609 ret =
1610 mbedtls_snprintf(p, n, "\n%s hardware type : ", prefix);
1611 MBEDTLS_X509_SAFE_SNPRINTF;
1612
1613 ret = mbedtls_oid_get_numeric_string(p,
1614 n,
1615 &other_name->value.hardware_module_name.oid);
1616 MBEDTLS_X509_SAFE_SNPRINTF;
1617
1618 ret =
1619 mbedtls_snprintf(p, n, "\n%s hardware serial number : ", prefix);
1620 MBEDTLS_X509_SAFE_SNPRINTF;
1621
1622 for (i = 0; i < other_name->value.hardware_module_name.val.len; i++) {
1623 ret = mbedtls_snprintf(p,
1624 n,
1625 "%02X",
1626 other_name->value.hardware_module_name.val.p[i]);
1627 MBEDTLS_X509_SAFE_SNPRINTF;
1628 }
1629 }/* MBEDTLS_OID_ON_HW_MODULE_NAME */
1630 }
1631 break;
Andrzej Kurek7a05fab2023-02-13 10:03:07 -05001632 /*
1633 * uniformResourceIdentifier
1634 */
1635 case MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER:
1636 {
1637 ret = mbedtls_snprintf(p, n, "\n%s uniformResourceIdentifier : ", prefix);
1638 MBEDTLS_X509_SAFE_SNPRINTF;
1639 if (san.san.unstructured_name.len >= n) {
Przemek Stekiel01cb6eb2023-06-05 16:38:13 +02001640 if (n > 0) {
1641 *p = '\0';
1642 }
Andrzej Kurek7a05fab2023-02-13 10:03:07 -05001643 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1644 }
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001645
Andrzej Kurek7a05fab2023-02-13 10:03:07 -05001646 memcpy(p, san.san.unstructured_name.p, san.san.unstructured_name.len);
1647 p += san.san.unstructured_name.len;
1648 n -= san.san.unstructured_name.len;
1649 }
1650 break;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001651 /*
1652 * dNSName
Przemek Stekiel5b9e4162023-02-15 12:56:37 +01001653 * RFC822 Name
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001654 */
1655 case MBEDTLS_X509_SAN_DNS_NAME:
Przemek Stekiel5b9e4162023-02-15 12:56:37 +01001656 case MBEDTLS_X509_SAN_RFC822_NAME:
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001657 {
Przemek Stekiel82d250d2023-02-15 15:00:50 +01001658 const char *dns_name = "dNSName";
1659 const char *rfc822_name = "rfc822Name";
Przemek Stekiel5b9e4162023-02-15 12:56:37 +01001660
Przemek Stekiel82d250d2023-02-15 15:00:50 +01001661 ret = mbedtls_snprintf(p, n,
1662 "\n%s %s : ",
1663 prefix,
Przemek Stekiel093c97d2023-06-02 10:11:32 +02001664 san.type ==
1665 MBEDTLS_X509_SAN_DNS_NAME ? dns_name : rfc822_name);
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001666 MBEDTLS_X509_SAFE_SNPRINTF;
1667 if (san.san.unstructured_name.len >= n) {
Przemek Stekiel01cb6eb2023-06-05 16:38:13 +02001668 if (n > 0) {
1669 *p = '\0';
1670 }
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001671 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1672 }
1673
Przemek Stekiel093c97d2023-06-02 10:11:32 +02001674 memcpy(p, san.san.unstructured_name.p, san.san.unstructured_name.len);
1675 p += san.san.unstructured_name.len;
1676 n -= san.san.unstructured_name.len;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001677 }
1678 break;
Przemek Stekiel093c97d2023-06-02 10:11:32 +02001679 /*
1680 * iPAddress
1681 */
1682 case MBEDTLS_X509_SAN_IP_ADDRESS:
1683 {
1684 ret = mbedtls_snprintf(p, n, "\n%s %s : ",
1685 prefix, "iPAddress");
1686 MBEDTLS_X509_SAFE_SNPRINTF;
1687 if (san.san.unstructured_name.len >= n) {
Przemek Stekiel01cb6eb2023-06-05 16:38:13 +02001688 if (n > 0) {
1689 *p = '\0';
1690 }
Przemek Stekiel093c97d2023-06-02 10:11:32 +02001691 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1692 }
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001693
Przemek Stekiel093c97d2023-06-02 10:11:32 +02001694 unsigned char *ip = san.san.unstructured_name.p;
1695 // Only IPv6 (16 bytes) and IPv4 (4 bytes) types are supported
1696 if (san.san.unstructured_name.len == 4) {
Przemek Stekiel4d3fc212023-06-06 11:40:32 +02001697 ret = mbedtls_snprintf(p, n, "%u.%u.%u.%u", ip[0], ip[1], ip[2], ip[3]);
1698 MBEDTLS_X509_SAFE_SNPRINTF;
Przemek Stekiel093c97d2023-06-02 10:11:32 +02001699 } else if (san.san.unstructured_name.len == 16) {
Przemek Stekiel4d3fc212023-06-06 11:40:32 +02001700 ret = mbedtls_snprintf(p, n,
Przemek Stekiel01cb6eb2023-06-05 16:38:13 +02001701 "%X%X:%X%X:%X%X:%X%X:%X%X:%X%X:%X%X:%X%X",
1702 ip[0], ip[1], ip[2], ip[3], ip[4], ip[5], ip[6],
Przemek Stekiel4d3fc212023-06-06 11:40:32 +02001703 ip[7], ip[8], ip[9], ip[10], ip[11], ip[12], ip[13],
Przemek Stekiel01cb6eb2023-06-05 16:38:13 +02001704 ip[14], ip[15]);
Przemek Stekiel4d3fc212023-06-06 11:40:32 +02001705 MBEDTLS_X509_SAFE_SNPRINTF;
Przemek Stekiel093c97d2023-06-02 10:11:32 +02001706 } else {
Przemek Stekiel01cb6eb2023-06-05 16:38:13 +02001707 if (n > 0) {
1708 *p = '\0';
1709 }
Przemek Stekiel093c97d2023-06-02 10:11:32 +02001710 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1711 }
Przemek Stekiel093c97d2023-06-02 10:11:32 +02001712 }
1713 break;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001714 /*
Andrzej Kureke12b01d2023-01-10 06:47:38 -05001715 * directoryName
1716 */
1717 case MBEDTLS_X509_SAN_DIRECTORY_NAME:
1718 {
1719 ret = mbedtls_snprintf(p, n, "\n%s directoryName : ", prefix);
Andrzej Kurek5f0c6e82023-02-27 16:03:41 -05001720 if (ret < 0 || (size_t) ret >= n) {
1721 mbedtls_x509_free_subject_alt_name(&san);
1722 }
1723
Andrzej Kureke12b01d2023-01-10 06:47:38 -05001724 MBEDTLS_X509_SAFE_SNPRINTF;
1725 ret = mbedtls_x509_dn_gets(p, n, &san.san.directory_name);
Andrzej Kurekd40c2b62023-02-13 07:01:59 -05001726
Andrzej Kureke12b01d2023-01-10 06:47:38 -05001727 if (ret < 0) {
Andrzej Kurek5f0c6e82023-02-27 16:03:41 -05001728 mbedtls_x509_free_subject_alt_name(&san);
Przemek Stekiel01cb6eb2023-06-05 16:38:13 +02001729 if (n > 0) {
1730 *p = '\0';
1731 }
Andrzej Kureke12b01d2023-01-10 06:47:38 -05001732 return ret;
1733 }
1734
1735 p += ret;
1736 n -= ret;
1737 }
1738 break;
1739 /*
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001740 * Type not supported, skip item.
1741 */
1742 default:
1743 ret = mbedtls_snprintf(p, n, "\n%s <unsupported>", prefix);
1744 MBEDTLS_X509_SAFE_SNPRINTF;
1745 break;
1746 }
1747
Andrzej Kurekd40c2b62023-02-13 07:01:59 -05001748 /* So far memory is freed only in the case of directoryName
Andrzej Kurek5f0c6e82023-02-27 16:03:41 -05001749 * parsing succeeding, as mbedtls_x509_get_name allocates memory. */
Andrzej Kurekd40c2b62023-02-13 07:01:59 -05001750 mbedtls_x509_free_subject_alt_name(&san);
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001751 cur = cur->next;
1752 }
1753
1754 *p = '\0';
1755
1756 *size = n;
1757 *buf = p;
1758
1759 return 0;
1760}
1761
Demi Marie Obenour690b8c92022-12-04 04:24:22 -05001762#define PRINT_ITEM(i) \
1763 do { \
1764 ret = mbedtls_snprintf(p, n, "%s" i, sep); \
1765 MBEDTLS_X509_SAFE_SNPRINTF; \
1766 sep = ", "; \
1767 } while (0)
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001768
Demi Marie Obenour690b8c92022-12-04 04:24:22 -05001769#define CERT_TYPE(type, name) \
1770 do { \
1771 if (ns_cert_type & (type)) { \
1772 PRINT_ITEM(name); \
1773 } \
1774 } while (0)
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001775
1776int mbedtls_x509_info_cert_type(char **buf, size_t *size,
1777 unsigned char ns_cert_type)
1778{
1779 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1780 size_t n = *size;
1781 char *p = *buf;
1782 const char *sep = "";
1783
1784 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT, "SSL Client");
1785 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER, "SSL Server");
1786 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_EMAIL, "Email");
1787 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing");
1788 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_RESERVED, "Reserved");
1789 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_SSL_CA, "SSL CA");
1790 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA, "Email CA");
1791 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA");
1792
1793 *size = n;
1794 *buf = p;
1795
1796 return 0;
1797}
1798
Demi Marie Obenour690b8c92022-12-04 04:24:22 -05001799#define KEY_USAGE(code, name) \
1800 do { \
1801 if ((key_usage) & (code)) { \
1802 PRINT_ITEM(name); \
1803 } \
1804 } while (0)
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001805
1806int mbedtls_x509_info_key_usage(char **buf, size_t *size,
1807 unsigned int key_usage)
1808{
1809 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1810 size_t n = *size;
1811 char *p = *buf;
1812 const char *sep = "";
1813
1814 KEY_USAGE(MBEDTLS_X509_KU_DIGITAL_SIGNATURE, "Digital Signature");
1815 KEY_USAGE(MBEDTLS_X509_KU_NON_REPUDIATION, "Non Repudiation");
1816 KEY_USAGE(MBEDTLS_X509_KU_KEY_ENCIPHERMENT, "Key Encipherment");
1817 KEY_USAGE(MBEDTLS_X509_KU_DATA_ENCIPHERMENT, "Data Encipherment");
1818 KEY_USAGE(MBEDTLS_X509_KU_KEY_AGREEMENT, "Key Agreement");
1819 KEY_USAGE(MBEDTLS_X509_KU_KEY_CERT_SIGN, "Key Cert Sign");
1820 KEY_USAGE(MBEDTLS_X509_KU_CRL_SIGN, "CRL Sign");
1821 KEY_USAGE(MBEDTLS_X509_KU_ENCIPHER_ONLY, "Encipher Only");
1822 KEY_USAGE(MBEDTLS_X509_KU_DECIPHER_ONLY, "Decipher Only");
1823
1824 *size = n;
1825 *buf = p;
1826
1827 return 0;
1828}
1829#endif /* MBEDTLS_X509_REMOVE_INFO */
1830#endif /* MBEDTLS_X509_CRT_PARSE_C || MBEDTLS_X509_CSR_PARSE_C */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001831#endif /* MBEDTLS_X509_USE_C */