blob: 8ca7dde62412df7c16b2acf5173e1ceefb46131c [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"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000024#include "mbedtls/oid.h"
Rich Evans00ab4702015-02-06 13:43:58 +000025
Rich Evans36796df2015-02-12 18:27:14 +000026#include <stdio.h>
Rich Evans00ab4702015-02-06 13:43:58 +000027#include <string.h>
28
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000030#include "mbedtls/pem.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020031#endif
32
Agathiyan Bragadeeshddc720d2023-07-26 15:51:49 +010033#include "mbedtls/asn1write.h"
Agathiyan Bragadeeshddc720d2023-07-26 15:51:49 +010034
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000035#include "mbedtls/platform.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020036
Simon Butcherb5b6af22016-07-13 14:46:18 +010037#if defined(MBEDTLS_HAVE_TIME)
38#include "mbedtls/platform_time.h"
39#endif
Nicholas Wilson512b4ee2017-12-05 12:07:33 +000040#if defined(MBEDTLS_HAVE_TIME_DATE)
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +010041#include "mbedtls/platform_util.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020042#include <time.h>
43#endif
44
Demi Marie Obenour690b8c92022-12-04 04:24:22 -050045#define CHECK(code) \
46 do { \
47 if ((ret = (code)) != 0) { \
48 return ret; \
49 } \
50 } while (0)
51
Hanno Becker1eeca412018-10-15 12:01:35 +010052#define CHECK_RANGE(min, max, val) \
Demi Marie Obenour690b8c92022-12-04 04:24:22 -050053 do { \
54 if ((val) < (min) || (val) > (max)) { \
55 return ret; \
Hanno Becker1eeca412018-10-15 12:01:35 +010056 } \
Gilles Peskine449bd832023-01-11 14:50:10 +010057 } while (0)
Rich Evans7d5a55a2015-02-13 11:48:02 +000058
Paul Bakker7c6b2c32013-09-16 13:49:26 +020059/*
60 * CertificateSerialNumber ::= INTEGER
61 */
Gilles Peskine449bd832023-01-11 14:50:10 +010062int mbedtls_x509_get_serial(unsigned char **p, const unsigned char *end,
63 mbedtls_x509_buf *serial)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020064{
Janos Follath865b3eb2019-12-16 11:46:15 +000065 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020066
Gilles Peskine449bd832023-01-11 14:50:10 +010067 if ((end - *p) < 1) {
68 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_SERIAL,
69 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
70 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +020071
Gilles Peskine449bd832023-01-11 14:50:10 +010072 if (**p != (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_PRIMITIVE | 2) &&
73 **p != MBEDTLS_ASN1_INTEGER) {
74 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_SERIAL,
75 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
76 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +020077
78 serial->tag = *(*p)++;
79
Gilles Peskine449bd832023-01-11 14:50:10 +010080 if ((ret = mbedtls_asn1_get_len(p, end, &serial->len)) != 0) {
81 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_SERIAL, ret);
82 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +020083
84 serial->p = *p;
85 *p += serial->len;
86
Gilles Peskine449bd832023-01-11 14:50:10 +010087 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020088}
89
90/* Get an algorithm identifier without parameters (eg for signatures)
91 *
92 * AlgorithmIdentifier ::= SEQUENCE {
93 * algorithm OBJECT IDENTIFIER,
94 * parameters ANY DEFINED BY algorithm OPTIONAL }
95 */
Gilles Peskine449bd832023-01-11 14:50:10 +010096int mbedtls_x509_get_alg_null(unsigned char **p, const unsigned char *end,
97 mbedtls_x509_buf *alg)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020098{
Janos Follath865b3eb2019-12-16 11:46:15 +000099 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200100
Gilles Peskine449bd832023-01-11 14:50:10 +0100101 if ((ret = mbedtls_asn1_get_alg_null(p, end, alg)) != 0) {
102 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
103 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200104
Gilles Peskine449bd832023-01-11 14:50:10 +0100105 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200106}
107
108/*
Antonin Décimo36e89b52019-01-23 15:24:37 +0100109 * Parse an algorithm identifier with (optional) parameters
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100110 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100111int mbedtls_x509_get_alg(unsigned char **p, const unsigned char *end,
112 mbedtls_x509_buf *alg, mbedtls_x509_buf *params)
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100113{
Janos Follath865b3eb2019-12-16 11:46:15 +0000114 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100115
Gilles Peskine449bd832023-01-11 14:50:10 +0100116 if ((ret = mbedtls_asn1_get_alg(p, end, alg, params)) != 0) {
117 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
118 }
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100119
Gilles Peskine449bd832023-01-11 14:50:10 +0100120 return 0;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100121}
122
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200123/*
124 * Convert md type to string
125 */
Dave Rodgmanffabb7b2023-06-28 16:22:50 +0100126#if !defined(MBEDTLS_X509_REMOVE_INFO) && defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
127
Gilles Peskine449bd832023-01-11 14:50:10 +0100128static inline const char *md_type_to_string(mbedtls_md_type_t md_alg)
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200129{
Gilles Peskine449bd832023-01-11 14:50:10 +0100130 switch (md_alg) {
Elena Uziunaiteb66a9912024-05-10 14:25:58 +0100131#if defined(PSA_WANT_ALG_MD5)
Gilles Peskine449bd832023-01-11 14:50:10 +0100132 case MBEDTLS_MD_MD5:
133 return "MD5";
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200134#endif
Elena Uziunaite9fc5be02024-09-04 18:12:59 +0100135#if defined(PSA_WANT_ALG_SHA_1)
Gilles Peskine449bd832023-01-11 14:50:10 +0100136 case MBEDTLS_MD_SHA1:
137 return "SHA1";
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200138#endif
Elena Uziunaitefcc9afa2024-05-23 14:43:22 +0100139#if defined(PSA_WANT_ALG_SHA_224)
Gilles Peskine449bd832023-01-11 14:50:10 +0100140 case MBEDTLS_MD_SHA224:
141 return "SHA224";
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200142#endif
Elena Uziunaite0916cd72024-05-23 17:01:07 +0100143#if defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine449bd832023-01-11 14:50:10 +0100144 case MBEDTLS_MD_SHA256:
145 return "SHA256";
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200146#endif
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +0100147#if defined(PSA_WANT_ALG_SHA_384)
Gilles Peskine449bd832023-01-11 14:50:10 +0100148 case MBEDTLS_MD_SHA384:
149 return "SHA384";
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200150#endif
Gabor Mezeic15ef932024-06-13 12:53:54 +0200151#if defined(PSA_WANT_ALG_SHA_512)
Gilles Peskine449bd832023-01-11 14:50:10 +0100152 case MBEDTLS_MD_SHA512:
153 return "SHA512";
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200154#endif
Elena Uziunaite1b6fb212024-05-10 16:17:41 +0100155#if defined(PSA_WANT_ALG_RIPEMD160)
Gilles Peskine449bd832023-01-11 14:50:10 +0100156 case MBEDTLS_MD_RIPEMD160:
157 return "RIPEMD160";
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200158#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100159 case MBEDTLS_MD_NONE:
160 return NULL;
161 default:
162 return NULL;
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200163 }
164}
165
Dave Rodgmanf032c982023-06-29 12:09:27 +0100166#endif /* !defined(MBEDTLS_X509_REMOVE_INFO) && defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) */
Dave Rodgmanffabb7b2023-06-28 16:22:50 +0100167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100169/*
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100170 * HashAlgorithm ::= AlgorithmIdentifier
171 *
172 * AlgorithmIdentifier ::= SEQUENCE {
173 * algorithm OBJECT IDENTIFIER,
174 * parameters ANY DEFINED BY algorithm OPTIONAL }
175 *
176 * For HashAlgorithm, parameters MUST be NULL or absent.
177 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100178static int x509_get_hash_alg(const mbedtls_x509_buf *alg, mbedtls_md_type_t *md_alg)
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100179{
Janos Follath865b3eb2019-12-16 11:46:15 +0000180 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100181 unsigned char *p;
182 const unsigned char *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183 mbedtls_x509_buf md_oid;
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100184 size_t len;
185
186 /* Make sure we got a SEQUENCE and setup bounds */
Gilles Peskine449bd832023-01-11 14:50:10 +0100187 if (alg->tag != (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) {
188 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
189 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
190 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100191
Andrzej Kurekfeaebc52020-07-16 04:37:41 -0400192 p = alg->p;
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100193 end = p + alg->len;
194
Gilles Peskine449bd832023-01-11 14:50:10 +0100195 if (p >= end) {
196 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
197 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
198 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100199
200 /* Parse md_oid */
201 md_oid.tag = *p;
202
Gilles Peskine449bd832023-01-11 14:50:10 +0100203 if ((ret = mbedtls_asn1_get_tag(&p, end, &md_oid.len, MBEDTLS_ASN1_OID)) != 0) {
204 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
205 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100206
207 md_oid.p = p;
208 p += md_oid.len;
209
210 /* Get md_alg from md_oid */
Gilles Peskine449bd832023-01-11 14:50:10 +0100211 if ((ret = mbedtls_oid_get_md_alg(&md_oid, md_alg)) != 0) {
212 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
213 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100214
215 /* Make sure params is absent of NULL */
Gilles Peskine449bd832023-01-11 14:50:10 +0100216 if (p == end) {
217 return 0;
218 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100219
Gilles Peskine449bd832023-01-11 14:50:10 +0100220 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_NULL)) != 0 || len != 0) {
221 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
222 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100223
Gilles Peskine449bd832023-01-11 14:50:10 +0100224 if (p != end) {
225 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
226 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
227 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100228
Gilles Peskine449bd832023-01-11 14:50:10 +0100229 return 0;
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100230}
231
232/*
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100233 * RSASSA-PSS-params ::= SEQUENCE {
234 * hashAlgorithm [0] HashAlgorithm DEFAULT sha1Identifier,
235 * maskGenAlgorithm [1] MaskGenAlgorithm DEFAULT mgf1SHA1Identifier,
236 * saltLength [2] INTEGER DEFAULT 20,
237 * trailerField [3] INTEGER DEFAULT 1 }
238 * -- Note that the tags in this Sequence are explicit.
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200239 *
240 * RFC 4055 (which defines use of RSASSA-PSS in PKIX) states that the value
241 * of trailerField MUST be 1, and PKCS#1 v2.2 doesn't even define any other
Tom Cosgrove1797b052022-12-04 17:19:59 +0000242 * option. Enforce this at parsing time.
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100243 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100244int mbedtls_x509_get_rsassa_pss_params(const mbedtls_x509_buf *params,
245 mbedtls_md_type_t *md_alg, mbedtls_md_type_t *mgf_md,
246 int *salt_len)
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100247{
Janos Follath865b3eb2019-12-16 11:46:15 +0000248 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100249 unsigned char *p;
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100250 const unsigned char *end, *end2;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100251 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200252 mbedtls_x509_buf alg_id, alg_params;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100253
254 /* First set everything to defaults */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200255 *md_alg = MBEDTLS_MD_SHA1;
256 *mgf_md = MBEDTLS_MD_SHA1;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100257 *salt_len = 20;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100258
259 /* Make sure params is a SEQUENCE and setup bounds */
Gilles Peskine449bd832023-01-11 14:50:10 +0100260 if (params->tag != (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) {
261 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
262 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
263 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100264
265 p = (unsigned char *) params->p;
266 end = p + params->len;
267
Gilles Peskine449bd832023-01-11 14:50:10 +0100268 if (p == end) {
269 return 0;
270 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100271
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100272 /*
273 * HashAlgorithm
274 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100275 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
276 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
277 0)) == 0) {
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100278 end2 = p + len;
279
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100280 /* HashAlgorithm ::= AlgorithmIdentifier (without parameters) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100281 if ((ret = mbedtls_x509_get_alg_null(&p, end2, &alg_id)) != 0) {
282 return ret;
283 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100284
Gilles Peskine449bd832023-01-11 14:50:10 +0100285 if ((ret = mbedtls_oid_get_md_alg(&alg_id, md_alg)) != 0) {
286 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
287 }
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100288
Gilles Peskine449bd832023-01-11 14:50:10 +0100289 if (p != end2) {
290 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
291 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
292 }
293 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
294 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100295 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100296
Gilles Peskine449bd832023-01-11 14:50:10 +0100297 if (p == end) {
298 return 0;
299 }
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100300
301 /*
302 * MaskGenAlgorithm
303 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100304 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
305 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
306 1)) == 0) {
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100307 end2 = p + len;
308
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100309 /* MaskGenAlgorithm ::= AlgorithmIdentifier (params = HashAlgorithm) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100310 if ((ret = mbedtls_x509_get_alg(&p, end2, &alg_id, &alg_params)) != 0) {
311 return ret;
312 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100313
314 /* Only MFG1 is recognised for now */
Gilles Peskine449bd832023-01-11 14:50:10 +0100315 if (MBEDTLS_OID_CMP(MBEDTLS_OID_MGF1, &alg_id) != 0) {
316 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE,
317 MBEDTLS_ERR_OID_NOT_FOUND);
318 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100319
320 /* Parse HashAlgorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +0100321 if ((ret = x509_get_hash_alg(&alg_params, mgf_md)) != 0) {
322 return ret;
323 }
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100324
Gilles Peskine449bd832023-01-11 14:50:10 +0100325 if (p != end2) {
326 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
327 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
328 }
329 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
330 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100331 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100332
Gilles Peskine449bd832023-01-11 14:50:10 +0100333 if (p == end) {
334 return 0;
335 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100336
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100337 /*
338 * salt_len
339 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100340 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
341 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
342 2)) == 0) {
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100343 end2 = p + len;
344
Gilles Peskine449bd832023-01-11 14:50:10 +0100345 if ((ret = mbedtls_asn1_get_int(&p, end2, salt_len)) != 0) {
346 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
347 }
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100348
Gilles Peskine449bd832023-01-11 14:50:10 +0100349 if (p != end2) {
350 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
351 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
352 }
353 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
354 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100355 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100356
Gilles Peskine449bd832023-01-11 14:50:10 +0100357 if (p == end) {
358 return 0;
359 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100360
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100361 /*
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200362 * trailer_field (if present, must be 1)
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100363 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100364 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
365 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
366 3)) == 0) {
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200367 int trailer_field;
368
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100369 end2 = p + len;
370
Gilles Peskine449bd832023-01-11 14:50:10 +0100371 if ((ret = mbedtls_asn1_get_int(&p, end2, &trailer_field)) != 0) {
372 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
373 }
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100374
Gilles Peskine449bd832023-01-11 14:50:10 +0100375 if (p != end2) {
376 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
377 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
378 }
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200379
Gilles Peskine449bd832023-01-11 14:50:10 +0100380 if (trailer_field != 1) {
381 return MBEDTLS_ERR_X509_INVALID_ALG;
382 }
383 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
384 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100385 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100386
Gilles Peskine449bd832023-01-11 14:50:10 +0100387 if (p != end) {
388 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
389 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
390 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100391
Gilles Peskine449bd832023-01-11 14:50:10 +0100392 return 0;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100393}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200394#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100395
396/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200397 * AttributeTypeAndValue ::= SEQUENCE {
398 * type AttributeType,
399 * value AttributeValue }
400 *
401 * AttributeType ::= OBJECT IDENTIFIER
402 *
403 * AttributeValue ::= ANY DEFINED BY AttributeType
404 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100405static int x509_get_attr_type_value(unsigned char **p,
406 const unsigned char *end,
407 mbedtls_x509_name *cur)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200408{
Janos Follath865b3eb2019-12-16 11:46:15 +0000409 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200410 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200411 mbedtls_x509_buf *oid;
412 mbedtls_x509_buf *val;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200413
Gilles Peskine449bd832023-01-11 14:50:10 +0100414 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
415 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
416 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME, ret);
417 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200418
Hanno Becker12f62fb2019-02-12 17:22:36 +0000419 end = *p + len;
420
Gilles Peskine449bd832023-01-11 14:50:10 +0100421 if ((end - *p) < 1) {
422 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME,
423 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
424 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200425
426 oid = &cur->oid;
427 oid->tag = **p;
428
Gilles Peskine449bd832023-01-11 14:50:10 +0100429 if ((ret = mbedtls_asn1_get_tag(p, end, &oid->len, MBEDTLS_ASN1_OID)) != 0) {
430 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME, ret);
431 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200432
433 oid->p = *p;
434 *p += oid->len;
435
Gilles Peskine449bd832023-01-11 14:50:10 +0100436 if ((end - *p) < 1) {
437 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME,
438 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
439 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200440
Gilles Peskine449bd832023-01-11 14:50:10 +0100441 if (**p != MBEDTLS_ASN1_BMP_STRING && **p != MBEDTLS_ASN1_UTF8_STRING &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200442 **p != MBEDTLS_ASN1_T61_STRING && **p != MBEDTLS_ASN1_PRINTABLE_STRING &&
443 **p != MBEDTLS_ASN1_IA5_STRING && **p != MBEDTLS_ASN1_UNIVERSAL_STRING &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100444 **p != MBEDTLS_ASN1_BIT_STRING) {
445 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME,
446 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
447 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200448
449 val = &cur->val;
450 val->tag = *(*p)++;
451
Gilles Peskine449bd832023-01-11 14:50:10 +0100452 if ((ret = mbedtls_asn1_get_len(p, end, &val->len)) != 0) {
453 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME, ret);
454 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200455
456 val->p = *p;
457 *p += val->len;
458
Gilles Peskine449bd832023-01-11 14:50:10 +0100459 if (*p != end) {
460 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME,
461 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Hanno Becker12f62fb2019-02-12 17:22:36 +0000462 }
463
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200464 cur->next = NULL;
465
Gilles Peskine449bd832023-01-11 14:50:10 +0100466 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200467}
468
469/*
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000470 * Name ::= CHOICE { -- only one possibility for now --
471 * rdnSequence RDNSequence }
472 *
473 * RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
474 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200475 * RelativeDistinguishedName ::=
476 * SET OF AttributeTypeAndValue
477 *
478 * AttributeTypeAndValue ::= SEQUENCE {
479 * type AttributeType,
480 * value AttributeValue }
481 *
482 * AttributeType ::= OBJECT IDENTIFIER
483 *
484 * AttributeValue ::= ANY DEFINED BY AttributeType
Manuel Pégourié-Gonnard5d861852014-10-17 12:41:41 +0200485 *
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000486 * The data structure is optimized for the common case where each RDN has only
487 * one element, which is represented as a list of AttributeTypeAndValue.
488 * For the general case we still use a flat list, but we mark elements of the
489 * same set so that they are "merged" together in the functions that consume
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200490 * this list, eg mbedtls_x509_dn_gets().
David Horstmanned794832022-10-04 18:12:06 +0100491 *
David Horstmann11307a12022-10-17 18:10:23 +0100492 * On success, this function may allocate a linked list starting at cur->next
David Horstmanned794832022-10-04 18:12:06 +0100493 * that must later be free'd by the caller using mbedtls_free(). In error
494 * cases, this function frees all allocated memory internally and the caller
495 * has no freeing responsibilities.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200496 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100497int mbedtls_x509_get_name(unsigned char **p, const unsigned char *end,
498 mbedtls_x509_name *cur)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200499{
Janos Follath865b3eb2019-12-16 11:46:15 +0000500 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard5d861852014-10-17 12:41:41 +0200501 size_t set_len;
502 const unsigned char *end_set;
David Horstmanned794832022-10-04 18:12:06 +0100503 mbedtls_x509_name *head = cur;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200504
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100505 /* don't use recursion, we'd risk stack overflow if not optimized */
Gilles Peskine449bd832023-01-11 14:50:10 +0100506 while (1) {
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100507 /*
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000508 * parse SET
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100509 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100510 if ((ret = mbedtls_asn1_get_tag(p, end, &set_len,
511 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SET)) != 0) {
512 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME, ret);
David Horstmanned794832022-10-04 18:12:06 +0100513 goto error;
514 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200515
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100516 end_set = *p + set_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200517
Gilles Peskine449bd832023-01-11 14:50:10 +0100518 while (1) {
519 if ((ret = x509_get_attr_type_value(p, end_set, cur)) != 0) {
David Horstmanned794832022-10-04 18:12:06 +0100520 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +0100521 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200522
Gilles Peskine449bd832023-01-11 14:50:10 +0100523 if (*p == end_set) {
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000524 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100525 }
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000526
Manuel Pégourié-Gonnardeecb43c2015-05-12 12:56:41 +0200527 /* Mark this item as being no the only one in a set */
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000528 cur->next_merged = 1;
529
Gilles Peskine449bd832023-01-11 14:50:10 +0100530 cur->next = mbedtls_calloc(1, sizeof(mbedtls_x509_name));
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000531
Gilles Peskine449bd832023-01-11 14:50:10 +0100532 if (cur->next == NULL) {
David Horstmanned794832022-10-04 18:12:06 +0100533 ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
534 goto error;
535 }
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000536
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000537 cur = cur->next;
538 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200539
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100540 /*
541 * continue until end of SEQUENCE is reached
542 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100543 if (*p == end) {
544 return 0;
545 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200546
Gilles Peskine449bd832023-01-11 14:50:10 +0100547 cur->next = mbedtls_calloc(1, sizeof(mbedtls_x509_name));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200548
Gilles Peskine449bd832023-01-11 14:50:10 +0100549 if (cur->next == NULL) {
David Horstmanned794832022-10-04 18:12:06 +0100550 ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
551 goto error;
552 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200553
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100554 cur = cur->next;
555 }
David Horstmanned794832022-10-04 18:12:06 +0100556
557error:
David Horstmanned794832022-10-04 18:12:06 +0100558 /* Skip the first element as we did not allocate it */
Gilles Peskine449bd832023-01-11 14:50:10 +0100559 mbedtls_asn1_free_named_data_list_shallow(head->next);
Glenn Straussa4b40412022-06-26 19:32:09 -0400560 head->next = NULL;
David Horstmanned794832022-10-04 18:12:06 +0100561
Gilles Peskine449bd832023-01-11 14:50:10 +0100562 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200563}
564
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400565static int x509_date_is_valid(const mbedtls_x509_time *t)
Janos Follath87c98072017-02-03 12:36:59 +0000566{
David Horstmanncdf52832023-07-05 09:58:03 +0100567 unsigned int month_days;
568 unsigned int year;
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400569 switch (t->mon) {
570 case 1: case 3: case 5: case 7: case 8: case 10: case 12:
David Horstmanncdf52832023-07-05 09:58:03 +0100571 month_days = 31;
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400572 break;
573 case 4: case 6: case 9: case 11:
David Horstmanncdf52832023-07-05 09:58:03 +0100574 month_days = 30;
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400575 break;
576 case 2:
David Horstmanncdf52832023-07-05 09:58:03 +0100577 year = (unsigned int) t->year;
578 month_days = ((year & 3) || (!(year % 100)
579 && (year % 400)))
580 ? 28 : 29;
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400581 break;
582 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100583 return MBEDTLS_ERR_X509_INVALID_DATE;
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400584 }
Janos Follath87c98072017-02-03 12:36:59 +0000585
David Horstmannb1d27bc2023-07-05 10:00:31 +0100586 if ((unsigned int) (t->day - 1) >= month_days || /* (1 - days in month) */
David Horstmann3ae1c4c2023-07-05 11:15:08 +0100587 /* (unsigned int) (t->mon - 1) >= 12 || */ /* (1 - 12) checked above */
David Horstmannb1d27bc2023-07-05 10:00:31 +0100588 (unsigned int) t->year > 9999 || /* (0 - 9999) */
589 (unsigned int) t->hour > 23 || /* (0 - 23) */
590 (unsigned int) t->min > 59 || /* (0 - 59) */
591 (unsigned int) t->sec > 59) { /* (0 - 59) */
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400592 return MBEDTLS_ERR_X509_INVALID_DATE;
Rich Evans7d5a55a2015-02-13 11:48:02 +0000593 }
Janos Follath87c98072017-02-03 12:36:59 +0000594
Gilles Peskine449bd832023-01-11 14:50:10 +0100595 return 0;
Rich Evans7d5a55a2015-02-13 11:48:02 +0000596}
597
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400598static int x509_parse2_int(const unsigned char *p)
Andres AG4b76aec2016-09-23 13:16:02 +0100599{
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400600 uint32_t d1 = p[0] - '0';
601 uint32_t d2 = p[1] - '0';
602 return (d1 < 10 && d2 < 10) ? (int) (d1 * 10 + d2) : -1;
Andres AG4b76aec2016-09-23 13:16:02 +0100603}
604
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200605/*
Janos Follath87c98072017-02-03 12:36:59 +0000606 * Parse an ASN1_UTC_TIME (yearlen=2) or ASN1_GENERALIZED_TIME (yearlen=4)
607 * field.
608 */
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400609static int x509_parse_time(const unsigned char *p, mbedtls_x509_time *tm,
610 size_t yearlen)
Janos Follath87c98072017-02-03 12:36:59 +0000611{
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400612 int x;
Janos Follath87c98072017-02-03 12:36:59 +0000613
614 /*
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400615 * Parse year, month, day, hour, minute, second
Janos Follath87c98072017-02-03 12:36:59 +0000616 */
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400617 tm->year = x509_parse2_int(p);
618 if (tm->year < 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100619 return MBEDTLS_ERR_X509_INVALID_DATE;
620 }
Janos Follath87c98072017-02-03 12:36:59 +0000621
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400622 if (4 == yearlen) {
623 x = tm->year * 100;
624 p += 2;
625 tm->year = x509_parse2_int(p);
626 if (tm->year < 0) {
627 return MBEDTLS_ERR_X509_INVALID_DATE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100628 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100629 } else {
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400630 x = (tm->year < 50) ? 2000 : 1900;
Janos Follath87c98072017-02-03 12:36:59 +0000631 }
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400632 tm->year += x;
Janos Follath87c98072017-02-03 12:36:59 +0000633
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400634 tm->mon = x509_parse2_int(p + 2);
635 tm->day = x509_parse2_int(p + 4);
636 tm->hour = x509_parse2_int(p + 6);
637 tm->min = x509_parse2_int(p + 8);
638 tm->sec = x509_parse2_int(p + 10);
Janos Follath87c98072017-02-03 12:36:59 +0000639
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400640 return x509_date_is_valid(tm);
Janos Follath87c98072017-02-03 12:36:59 +0000641}
642
643/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200644 * Time ::= CHOICE {
645 * utcTime UTCTime,
646 * generalTime GeneralizedTime }
647 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100648int mbedtls_x509_get_time(unsigned char **p, const unsigned char *end,
649 mbedtls_x509_time *tm)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200650{
Janos Follath865b3eb2019-12-16 11:46:15 +0000651 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Janos Follath87c98072017-02-03 12:36:59 +0000652 size_t len, year_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200653 unsigned char tag;
654
Gilles Peskine449bd832023-01-11 14:50:10 +0100655 if ((end - *p) < 1) {
656 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE,
657 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
658 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200659
660 tag = **p;
661
Gilles Peskine449bd832023-01-11 14:50:10 +0100662 if (tag == MBEDTLS_ASN1_UTC_TIME) {
Janos Follath87c98072017-02-03 12:36:59 +0000663 year_len = 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100664 } else if (tag == MBEDTLS_ASN1_GENERALIZED_TIME) {
Janos Follath87c98072017-02-03 12:36:59 +0000665 year_len = 4;
Gilles Peskine449bd832023-01-11 14:50:10 +0100666 } else {
667 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE,
668 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
669 }
Janos Follath87c98072017-02-03 12:36:59 +0000670
671 (*p)++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100672 ret = mbedtls_asn1_get_len(p, end, &len);
Janos Follath87c98072017-02-03 12:36:59 +0000673
Gilles Peskine449bd832023-01-11 14:50:10 +0100674 if (ret != 0) {
675 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE, ret);
676 }
Janos Follath87c98072017-02-03 12:36:59 +0000677
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400678 /* len is 12 or 14 depending on year_len, plus optional trailing 'Z' */
679 if (len != year_len + 10 &&
680 !(len == year_len + 11 && (*p)[(len - 1)] == 'Z')) {
681 return MBEDTLS_ERR_X509_INVALID_DATE;
682 }
683
684 (*p) += len;
685 return x509_parse_time(*p - len, tm, year_len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200686}
687
Gilles Peskine449bd832023-01-11 14:50:10 +0100688int mbedtls_x509_get_sig(unsigned char **p, const unsigned char *end, mbedtls_x509_buf *sig)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200689{
Janos Follath865b3eb2019-12-16 11:46:15 +0000690 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200691 size_t len;
Andres AG4bdbe092016-09-19 16:58:45 +0100692 int tag_type;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200693
Gilles Peskine449bd832023-01-11 14:50:10 +0100694 if ((end - *p) < 1) {
695 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_SIGNATURE,
696 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
697 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200698
Andres AG4bdbe092016-09-19 16:58:45 +0100699 tag_type = **p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200700
Gilles Peskine449bd832023-01-11 14:50:10 +0100701 if ((ret = mbedtls_asn1_get_bitstring_null(p, end, &len)) != 0) {
702 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_SIGNATURE, ret);
703 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200704
Andres AG4bdbe092016-09-19 16:58:45 +0100705 sig->tag = tag_type;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200706 sig->len = len;
707 sig->p = *p;
708
709 *p += len;
710
Gilles Peskine449bd832023-01-11 14:50:10 +0100711 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200712}
713
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100714/*
715 * Get signature algorithm from alg OID and optional parameters
716 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100717int mbedtls_x509_get_sig_alg(const mbedtls_x509_buf *sig_oid, const mbedtls_x509_buf *sig_params,
Valerio Setti68878cc2025-04-10 23:30:26 +0200718 mbedtls_md_type_t *md_alg, mbedtls_pk_type_t *pk_alg)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200719{
Janos Follath865b3eb2019-12-16 11:46:15 +0000720 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200721
Gilles Peskine449bd832023-01-11 14:50:10 +0100722 if ((ret = mbedtls_oid_get_sig_alg(sig_oid, md_alg, pk_alg)) != 0) {
723 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG, ret);
724 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200725
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200726#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100727 if (*pk_alg == MBEDTLS_PK_RSASSA_PSS) {
Valerio Setti68878cc2025-04-10 23:30:26 +0200728 mbedtls_md_type_t mgf1_hash_id;
729 int expected_salt_len;
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200730
Gilles Peskine449bd832023-01-11 14:50:10 +0100731 ret = mbedtls_x509_get_rsassa_pss_params(sig_params,
732 md_alg,
Valerio Setti68878cc2025-04-10 23:30:26 +0200733 &mgf1_hash_id,
734 &expected_salt_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100735 if (ret != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100736 return ret;
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200737 }
Valerio Setti68878cc2025-04-10 23:30:26 +0200738 /* Ensure MGF1 hash alg is the same as the one used to hash the message. */
739 if (mgf1_hash_id != *md_alg) {
740 return MBEDTLS_ERR_X509_INVALID_ALG;
741 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100742 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200743#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100744 {
745 /* Make sure parameters are absent or NULL */
Gilles Peskine449bd832023-01-11 14:50:10 +0100746 if ((sig_params->tag != MBEDTLS_ASN1_NULL && sig_params->tag != 0) ||
747 sig_params->len != 0) {
748 return MBEDTLS_ERR_X509_INVALID_ALG;
749 }
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100750 }
751
Gilles Peskine449bd832023-01-11 14:50:10 +0100752 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200753}
754
755/*
756 * X.509 Extensions (No parsing of extensions, pointer should
Brian J Murray1903fb32016-11-06 04:45:15 -0800757 * be either manually updated or extensions should be parsed!)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200758 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100759int mbedtls_x509_get_ext(unsigned char **p, const unsigned char *end,
760 mbedtls_x509_buf *ext, int tag)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200761{
Janos Follath865b3eb2019-12-16 11:46:15 +0000762 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200763 size_t len;
764
Hanno Becker3cddba82019-02-11 14:33:36 +0000765 /* Extension structure use EXPLICIT tagging. That is, the actual
766 * `Extensions` structure is wrapped by a tag-length pair using
767 * the respective context-specific tag. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100768 ret = mbedtls_asn1_get_tag(p, end, &ext->len,
769 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | tag);
770 if (ret != 0) {
771 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
772 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200773
Hanno Becker6ccfb182019-02-12 11:52:10 +0000774 ext->tag = MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | tag;
775 ext->p = *p;
776 end = *p + ext->len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200777
778 /*
779 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200780 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100781 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
782 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
783 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
784 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200785
Gilles Peskine449bd832023-01-11 14:50:10 +0100786 if (end != *p + len) {
787 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
788 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
789 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200790
Gilles Peskine449bd832023-01-11 14:50:10 +0100791 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200792}
793
Agathiyan Bragadeesh9d2507c2023-07-24 16:35:57 +0100794static char nibble_to_hex_digit(int i)
Agathiyan Bragadeeshef2decb2023-07-21 15:47:47 +0100795{
Agathiyan Bragadeeshf0e1ac52023-07-24 16:43:36 +0100796 return (i < 10) ? (i + '0') : (i - 10 + 'A');
Agathiyan Bragadeeshef2decb2023-07-21 15:47:47 +0100797}
798
Sam Berry4f761942024-07-19 14:56:30 +0100799/* Return the x.y.z.... style numeric string for the given OID */
800int mbedtls_oid_get_numeric_string(char *buf, size_t size,
801 const mbedtls_asn1_buf *oid)
802{
803 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
804 char *p = buf;
805 size_t n = size;
806 unsigned int value = 0;
807
808 if (size > INT_MAX) {
809 /* Avoid overflow computing return value */
810 return MBEDTLS_ERR_ASN1_INVALID_LENGTH;
811 }
812
813 if (oid->len <= 0) {
814 /* OID must not be empty */
815 return MBEDTLS_ERR_ASN1_OUT_OF_DATA;
816 }
817
818 for (size_t i = 0; i < oid->len; i++) {
819 /* Prevent overflow in value. */
820 if (value > (UINT_MAX >> 7)) {
821 return MBEDTLS_ERR_ASN1_INVALID_DATA;
822 }
823 if ((value == 0) && ((oid->p[i]) == 0x80)) {
824 /* Overlong encoding is not allowed */
825 return MBEDTLS_ERR_ASN1_INVALID_DATA;
826 }
827
828 value <<= 7;
829 value |= oid->p[i] & 0x7F;
830
831 if (!(oid->p[i] & 0x80)) {
832 /* Last byte */
833 if (n == size) {
834 int component1;
835 unsigned int component2;
836 /* First subidentifier contains first two OID components */
837 if (value >= 80) {
838 component1 = '2';
839 component2 = value - 80;
840 } else if (value >= 40) {
841 component1 = '1';
842 component2 = value - 40;
843 } else {
844 component1 = '0';
845 component2 = value;
846 }
847 ret = mbedtls_snprintf(p, n, "%c.%u", component1, component2);
848 } else {
849 ret = mbedtls_snprintf(p, n, ".%u", value);
850 }
851 if (ret < 2 || (size_t) ret >= n) {
852 return MBEDTLS_ERR_OID_BUF_TOO_SMALL;
853 }
854 n -= (size_t) ret;
855 p += ret;
856 value = 0;
857 }
858 }
859
860 if (value != 0) {
861 /* Unterminated subidentifier */
862 return MBEDTLS_ERR_ASN1_OUT_OF_DATA;
863 }
864
865 return (int) (size - n);
866}
867
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200868/*
869 * Store the name in printable form into buf; no more
870 * than size characters will be written
871 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100872int mbedtls_x509_dn_gets(char *buf, size_t size, const mbedtls_x509_name *dn)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200873{
Janos Follath865b3eb2019-12-16 11:46:15 +0000874 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh5adffb22023-08-10 15:50:57 +0100875 size_t i, j, n, asn1_len_size, asn1_tag_size, asn1_tag_len_buf_start;
Agathiyan Bragadeeshc7959b22023-09-12 17:54:43 +0100876 /* 6 is enough as our asn1 write functions only write one byte for the tag and at most five bytes for the length*/
877 unsigned char asn1_tag_len_buf[6];
Agathiyan Bragadeeshddc720d2023-07-26 15:51:49 +0100878 unsigned char *asn1_len_p;
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000879 unsigned char c, merge = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200880 const mbedtls_x509_name *name;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200881 const char *short_name = NULL;
Agathiyan Bragadeesha1f5c2d2023-08-02 17:08:52 +0100882 char lowbits, highbits;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200883 char s[MBEDTLS_X509_MAX_DN_NAME_SIZE], *p;
Agathiyan Bragadeeshc9d74f32023-07-31 17:25:44 +0100884 int print_hexstring;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200885
Gilles Peskine449bd832023-01-11 14:50:10 +0100886 memset(s, 0, sizeof(s));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200887
888 name = dn;
889 p = buf;
890 n = size;
891
Gilles Peskine449bd832023-01-11 14:50:10 +0100892 while (name != NULL) {
893 if (!name->oid.p) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200894 name = name->next;
895 continue;
896 }
897
Gilles Peskine449bd832023-01-11 14:50:10 +0100898 if (name != dn) {
899 ret = mbedtls_snprintf(p, n, merge ? " + " : ", ");
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200900 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200901 }
902
Agathiyan Bragadeesh0a4b6d82023-08-02 15:05:57 +0100903 print_hexstring = (name->val.tag != MBEDTLS_ASN1_UTF8_STRING) &&
904 (name->val.tag != MBEDTLS_ASN1_PRINTABLE_STRING) &&
905 (name->val.tag != MBEDTLS_ASN1_IA5_STRING);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200906
Agathiyan Bragadeeshc9d74f32023-07-31 17:25:44 +0100907 if ((ret = mbedtls_oid_get_attr_short_name(&name->oid, &short_name)) == 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100908 ret = mbedtls_snprintf(p, n, "%s=", short_name);
909 } else {
Agathiyan Bragadeeshaf70c7d2023-08-10 16:39:23 +0100910 if ((ret = mbedtls_oid_get_numeric_string(p, n, &name->oid)) > 0) {
Agathiyan Bragadeeshf3b97242023-08-22 16:37:11 +0100911 n -= ret;
912 p += ret;
Agathiyan Bragadeeshaf70c7d2023-08-10 16:39:23 +0100913 ret = mbedtls_snprintf(p, n, "=");
Agathiyan Bragadeeshc9d74f32023-07-31 17:25:44 +0100914 print_hexstring = 1;
Agathiyan Bragadeesh8aa74ab2023-08-22 16:42:27 +0100915 } else if (ret == MBEDTLS_ERR_OID_BUF_TOO_SMALL) {
916 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
Agathiyan Bragadeesh15df0122023-08-22 17:50:00 +0100917 } else {
Agathiyan Bragadeeshddc720d2023-07-26 15:51:49 +0100918 ret = mbedtls_snprintf(p, n, "\?\?=");
919 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100920 }
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200921 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200922
Agathiyan Bragadeesh4987c8f2023-08-01 11:10:52 +0100923 if (print_hexstring) {
Agathiyan Bragadeeshddc720d2023-07-26 15:51:49 +0100924 s[0] = '#';
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200925
Agathiyan Bragadeeshc7959b22023-09-12 17:54:43 +0100926 asn1_len_p = asn1_tag_len_buf + sizeof(asn1_tag_len_buf);
Agathiyan Bragadeesh15df0122023-08-22 17:50:00 +0100927 if ((ret = mbedtls_asn1_write_len(&asn1_len_p, asn1_tag_len_buf, name->val.len)) < 0) {
Agathiyan Bragadeesh07f472a2023-08-22 16:29:39 +0100928 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
929 }
930 asn1_len_size = ret;
Agathiyan Bragadeesh15df0122023-08-22 17:50:00 +0100931 if ((ret = mbedtls_asn1_write_tag(&asn1_len_p, asn1_tag_len_buf, name->val.tag)) < 0) {
Agathiyan Bragadeesh07f472a2023-08-22 16:29:39 +0100932 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
933 }
934 asn1_tag_size = ret;
Agathiyan Bragadeeshc7959b22023-09-12 17:54:43 +0100935 asn1_tag_len_buf_start = sizeof(asn1_tag_len_buf) - asn1_len_size - asn1_tag_size;
Agathiyan Bragadeesh5adffb22023-08-10 15:50:57 +0100936 for (i = 0, j = 1; i < asn1_len_size + asn1_tag_size; i++) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100937 if (j + 1 >= sizeof(s) - 1) {
938 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
939 }
Agathiyan Bragadeesh5adffb22023-08-10 15:50:57 +0100940 c = asn1_tag_len_buf[asn1_tag_len_buf_start+i];
Agathiyan Bragadeesha1f5c2d2023-08-02 17:08:52 +0100941 lowbits = (c & 0x0F);
Agathiyan Bragadeesh2bf09a62023-08-10 14:37:00 +0100942 highbits = c >> 4;
Agathiyan Bragadeesh9d2507c2023-07-24 16:35:57 +0100943 s[j++] = nibble_to_hex_digit(highbits);
Agathiyan Bragadeesh5adffb22023-08-10 15:50:57 +0100944 s[j++] = nibble_to_hex_digit(lowbits);
Werner Lewisb33dacd2022-05-20 12:48:46 +0100945 }
Agathiyan Bragadeesh5adffb22023-08-10 15:50:57 +0100946 for (i = 0; i < name->val.len; i++) {
947 if (j + 1 >= sizeof(s) - 1) {
948 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
949 }
950 c = name->val.p[i];
951 lowbits = (c & 0x0F);
952 highbits = c >> 4;
953 s[j++] = nibble_to_hex_digit(highbits);
954 s[j++] = nibble_to_hex_digit(lowbits);
Agathiyan Bragadeeshddc720d2023-07-26 15:51:49 +0100955 }
956 } else {
957 for (i = 0, j = 0; i < name->val.len; i++, j++) {
958 if (j >= sizeof(s) - 1) {
959 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
960 }
961
962 c = name->val.p[i];
963 // Special characters requiring escaping, RFC 4514 Section 2.4
Agathiyan Bragadeesh022f86f2023-08-22 16:56:04 +0100964 if (c == '\0') {
965 return MBEDTLS_ERR_X509_INVALID_NAME;
Agathiyan Bragadeesh15df0122023-08-22 17:50:00 +0100966 } else {
Agathiyan Bragadeesha7f96302023-08-10 16:03:27 +0100967 if (strchr(",=+<>;\"\\", c) ||
Agathiyan Bragadeeshddc720d2023-07-26 15:51:49 +0100968 ((i == 0) && strchr("# ", c)) ||
969 ((i == name->val.len-1) && (c == ' '))) {
970 if (j + 1 >= sizeof(s) - 1) {
971 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
972 }
973 s[j++] = '\\';
974 }
975 }
976 if (c < 32 || c >= 127) {
977 if (j + 3 >= sizeof(s) - 1) {
978 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
979 }
980 s[j++] = '\\';
Agathiyan Bragadeesha1f5c2d2023-08-02 17:08:52 +0100981 lowbits = (c & 0x0F);
Agathiyan Bragadeesh2bf09a62023-08-10 14:37:00 +0100982 highbits = c >> 4;
Agathiyan Bragadeeshddc720d2023-07-26 15:51:49 +0100983 s[j++] = nibble_to_hex_digit(highbits);
984 s[j] = nibble_to_hex_digit(lowbits);
985 } else {
986 s[j] = c;
987 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100988 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200989 }
Werner Lewisb33dacd2022-05-20 12:48:46 +0100990 s[j] = '\0';
Gilles Peskine449bd832023-01-11 14:50:10 +0100991 ret = mbedtls_snprintf(p, n, "%s", s);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200992 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000993
994 merge = name->next_merged;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200995 name = name->next;
996 }
997
Gilles Peskine449bd832023-01-11 14:50:10 +0100998 return (int) (size - n);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200999}
1000
1001/*
1002 * Store the serial in printable form into buf; no more
1003 * than size characters will be written
1004 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001005int mbedtls_x509_serial_gets(char *buf, size_t size, const mbedtls_x509_buf *serial)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001006{
Janos Follath865b3eb2019-12-16 11:46:15 +00001007 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001008 size_t i, n, nr;
1009 char *p;
1010
1011 p = buf;
1012 n = size;
1013
Gilles Peskine449bd832023-01-11 14:50:10 +01001014 nr = (serial->len <= 32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001015 ? serial->len : 28;
1016
Gilles Peskine449bd832023-01-11 14:50:10 +01001017 for (i = 0; i < nr; i++) {
1018 if (i == 0 && nr > 1 && serial->p[i] == 0x0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001019 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001020 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001021
Gilles Peskine449bd832023-01-11 14:50:10 +01001022 ret = mbedtls_snprintf(p, n, "%02X%s",
1023 serial->p[i], (i < nr - 1) ? ":" : "");
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001024 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001025 }
1026
Gilles Peskine449bd832023-01-11 14:50:10 +01001027 if (nr != serial->len) {
1028 ret = mbedtls_snprintf(p, n, "....");
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001029 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001030 }
1031
Gilles Peskine449bd832023-01-11 14:50:10 +01001032 return (int) (size - n);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001033}
1034
Hanno Becker612a2f12020-10-09 09:19:39 +01001035#if !defined(MBEDTLS_X509_REMOVE_INFO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001036/*
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +02001037 * Helper for writing signature algorithms
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +01001038 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001039int mbedtls_x509_sig_alg_gets(char *buf, size_t size, const mbedtls_x509_buf *sig_oid,
1040 mbedtls_pk_type_t pk_alg, mbedtls_md_type_t md_alg,
1041 const void *sig_opts)
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 Peskine449bd832023-01-11 14:50:10 +01001048 ret = mbedtls_oid_get_sig_alg_desc(sig_oid, &desc);
1049 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) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001058 const mbedtls_pk_rsassa_pss_options *pss_opts;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +01001059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001060 pss_opts = (const mbedtls_pk_rsassa_pss_options *) sig_opts;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +01001061
Gilles Peskine449bd832023-01-11 14:50:10 +01001062 const char *name = md_type_to_string(md_alg);
1063 const char *mgf_name = md_type_to_string(pss_opts->mgf1_hash_id);
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +01001064
Gilles Peskine449bd832023-01-11 14:50:10 +01001065 ret = mbedtls_snprintf(p, n, " (%s, MGF1-%s, 0x%02X)",
1066 name ? name : "???",
1067 mgf_name ? mgf_name : "???",
1068 (unsigned int) pss_opts->expected_salt_len);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001069 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +01001070 }
1071#else
1072 ((void) pk_alg);
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +02001073 ((void) md_alg);
1074 ((void) sig_opts);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001075#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +01001076
Gilles Peskine449bd832023-01-11 14:50:10 +01001077 return (int) (size - n);
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +01001078}
Hanno Becker612a2f12020-10-09 09:19:39 +01001079#endif /* MBEDTLS_X509_REMOVE_INFO */
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +01001080
1081/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001082 * Helper for writing "RSA key size", "EC key size", etc
1083 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001084int mbedtls_x509_key_size_helper(char *buf, size_t buf_size, const char *name)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001085{
1086 char *p = buf;
Manuel Pégourié-Gonnardfb317c52015-06-18 16:25:56 +02001087 size_t n = buf_size;
Janos Follath865b3eb2019-12-16 11:46:15 +00001088 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001089
Gilles Peskine449bd832023-01-11 14:50:10 +01001090 ret = mbedtls_snprintf(p, n, "%s key size", name);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001091 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001092
Gilles Peskine449bd832023-01-11 14:50:10 +01001093 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001094}
1095
Glenn Strauss416dc032022-06-30 00:38:53 -04001096int mbedtls_x509_time_cmp(const mbedtls_x509_time *t1,
1097 const mbedtls_x509_time *t2)
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001098{
Glenn Strauss5aef2972022-06-30 04:38:02 -04001099 int x;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001100
Glenn Strauss5aef2972022-06-30 04:38:02 -04001101 x = (((t1->year << 9) | (t1->mon << 5) | (t1->day)) -
1102 ((t2->year << 9) | (t2->mon << 5) | (t2->day)));
1103 if (x != 0) {
1104 return x;
Manuel Pégourié-Gonnard57e10d72015-06-22 18:59:21 +02001105 }
Manuel Pégourié-Gonnard864108d2015-05-29 10:11:03 +02001106
Glenn Strauss5aef2972022-06-30 04:38:02 -04001107 x = (((t1->hour << 12) | (t1->min << 6) | (t1->sec)) -
1108 ((t2->hour << 12) | (t2->min << 6) | (t2->sec)));
1109 return x;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001110}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001111
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001112#if defined(MBEDTLS_HAVE_TIME_DATE)
Glenn Strauss61d99302022-06-30 05:25:56 -04001113int mbedtls_x509_time_gmtime(mbedtls_time_t tt, mbedtls_x509_time *now)
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001114{
Glenn Strauss811eeb22022-06-30 05:28:50 -04001115 struct tm tm;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001116
Glenn Strauss811eeb22022-06-30 05:28:50 -04001117 if (mbedtls_platform_gmtime_r(&tt, &tm) == NULL) {
1118 return -1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001119 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001120
Glenn Strauss811eeb22022-06-30 05:28:50 -04001121 now->year = tm.tm_year + 1900;
1122 now->mon = tm.tm_mon + 1;
1123 now->day = tm.tm_mday;
1124 now->hour = tm.tm_hour;
1125 now->min = tm.tm_min;
1126 now->sec = tm.tm_sec;
Gilles Peskine449bd832023-01-11 14:50:10 +01001127 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001128}
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001129
Glenn Strauss61d99302022-06-30 05:25:56 -04001130static int x509_get_current_time(mbedtls_x509_time *now)
1131{
1132 return mbedtls_x509_time_gmtime(mbedtls_time(NULL), now);
1133}
1134
Gilles Peskine449bd832023-01-11 14:50:10 +01001135int mbedtls_x509_time_is_past(const mbedtls_x509_time *to)
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001136{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001137 mbedtls_x509_time now;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001138
Gilles Peskine449bd832023-01-11 14:50:10 +01001139 if (x509_get_current_time(&now) != 0) {
1140 return 1;
1141 }
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001142
Glenn Strauss416dc032022-06-30 00:38:53 -04001143 return mbedtls_x509_time_cmp(to, &now) < 0;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001144}
1145
Gilles Peskine449bd832023-01-11 14:50:10 +01001146int mbedtls_x509_time_is_future(const mbedtls_x509_time *from)
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001147{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001148 mbedtls_x509_time now;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001149
Gilles Peskine449bd832023-01-11 14:50:10 +01001150 if (x509_get_current_time(&now) != 0) {
1151 return 1;
1152 }
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001153
Glenn Strauss416dc032022-06-30 00:38:53 -04001154 return mbedtls_x509_time_cmp(from, &now) > 0;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001155}
1156
Manuel Pégourié-Gonnard60c793b2015-06-18 20:52:58 +02001157#else /* MBEDTLS_HAVE_TIME_DATE */
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001158
Gilles Peskine449bd832023-01-11 14:50:10 +01001159int mbedtls_x509_time_is_past(const mbedtls_x509_time *to)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001160{
1161 ((void) to);
Gilles Peskine449bd832023-01-11 14:50:10 +01001162 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001163}
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001164
Gilles Peskine449bd832023-01-11 14:50:10 +01001165int mbedtls_x509_time_is_future(const mbedtls_x509_time *from)
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001166{
1167 ((void) from);
Gilles Peskine449bd832023-01-11 14:50:10 +01001168 return 0;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001169}
Manuel Pégourié-Gonnard60c793b2015-06-18 20:52:58 +02001170#endif /* MBEDTLS_HAVE_TIME_DATE */
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001171
1172/* Common functions for parsing CRT and CSR. */
1173#if defined(MBEDTLS_X509_CRT_PARSE_C) || defined(MBEDTLS_X509_CSR_PARSE_C)
1174/*
1175 * OtherName ::= SEQUENCE {
1176 * type-id OBJECT IDENTIFIER,
1177 * value [0] EXPLICIT ANY DEFINED BY type-id }
1178 *
1179 * HardwareModuleName ::= SEQUENCE {
1180 * hwType OBJECT IDENTIFIER,
1181 * hwSerialNum OCTET STRING }
1182 *
1183 * NOTE: we currently only parse and use otherName of type HwModuleName,
1184 * as defined in RFC 4108.
1185 */
1186static int x509_get_other_name(const mbedtls_x509_buf *subject_alt_name,
1187 mbedtls_x509_san_other_name *other_name)
1188{
1189 int ret = 0;
1190 size_t len;
1191 unsigned char *p = subject_alt_name->p;
1192 const unsigned char *end = p + subject_alt_name->len;
1193 mbedtls_x509_buf cur_oid;
1194
1195 if ((subject_alt_name->tag &
1196 (MBEDTLS_ASN1_TAG_CLASS_MASK | MBEDTLS_ASN1_TAG_VALUE_MASK)) !=
1197 (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME)) {
1198 /*
1199 * The given subject alternative name is not of type "othername".
1200 */
1201 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1202 }
1203
1204 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1205 MBEDTLS_ASN1_OID)) != 0) {
1206 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1207 }
1208
1209 cur_oid.tag = MBEDTLS_ASN1_OID;
1210 cur_oid.p = p;
1211 cur_oid.len = len;
1212
1213 /*
1214 * Only HwModuleName is currently supported.
1215 */
1216 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ON_HW_MODULE_NAME, &cur_oid) != 0) {
1217 return MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
1218 }
David Horstmann2ea44d22023-08-18 18:36:02 +01001219 other_name->type_id = cur_oid;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001220
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001221 p += len;
1222 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1223 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC)) !=
1224 0) {
1225 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1226 }
1227
Hanno Beckerdae916b2019-09-13 14:21:13 +01001228 if (end != p + len) {
1229 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1230 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1231 }
1232
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001233 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1234 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1235 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1236 }
1237
Hanno Beckerdae916b2019-09-13 14:21:13 +01001238 if (end != p + len) {
1239 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1240 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1241 }
1242
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001243 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OID)) != 0) {
1244 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1245 }
1246
1247 other_name->value.hardware_module_name.oid.tag = MBEDTLS_ASN1_OID;
1248 other_name->value.hardware_module_name.oid.p = p;
1249 other_name->value.hardware_module_name.oid.len = len;
1250
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001251 p += len;
1252 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1253 MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1254 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1255 }
1256
1257 other_name->value.hardware_module_name.val.tag = MBEDTLS_ASN1_OCTET_STRING;
1258 other_name->value.hardware_module_name.val.p = p;
1259 other_name->value.hardware_module_name.val.len = len;
1260 p += len;
1261 if (p != end) {
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001262 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1263 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1264 }
1265 return 0;
1266}
1267
Przemek Stekiel21903ec2023-02-21 08:32:37 +01001268/* Check mbedtls_x509_get_subject_alt_name for detailed description.
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001269 *
Przemek Stekiel21903ec2023-02-21 08:32:37 +01001270 * In some cases while parsing subject alternative names the sequence tag is optional
1271 * (e.g. CertSerialNumber). This function is designed to handle such case.
Przemek Stekiel725688b2023-04-04 22:49:44 +02001272 */
Przemek Stekiel21903ec2023-02-21 08:32:37 +01001273int mbedtls_x509_get_subject_alt_name_ext(unsigned char **p,
1274 const unsigned char *end,
1275 mbedtls_x509_sequence *subject_alt_name)
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001276{
1277 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel21903ec2023-02-21 08:32:37 +01001278 size_t tag_len;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001279 mbedtls_asn1_sequence *cur = subject_alt_name;
1280
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001281 while (*p < end) {
Andrzej Kurek154a6052023-04-30 14:11:49 -04001282 mbedtls_x509_subject_alternative_name tmp_san_name;
Hanno Beckerae8f8c42019-09-13 12:24:56 +01001283 mbedtls_x509_buf tmp_san_buf;
Andrzej Kurek154a6052023-04-30 14:11:49 -04001284 memset(&tmp_san_name, 0, sizeof(tmp_san_name));
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001285
Hanno Beckerae8f8c42019-09-13 12:24:56 +01001286 tmp_san_buf.tag = **p;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001287 (*p)++;
Hanno Beckerae8f8c42019-09-13 12:24:56 +01001288
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001289 if ((ret = mbedtls_asn1_get_len(p, end, &tag_len)) != 0) {
1290 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1291 }
1292
Hanno Beckerae8f8c42019-09-13 12:24:56 +01001293 tmp_san_buf.p = *p;
1294 tmp_san_buf.len = tag_len;
1295
1296 if ((tmp_san_buf.tag & MBEDTLS_ASN1_TAG_CLASS_MASK) !=
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001297 MBEDTLS_ASN1_CONTEXT_SPECIFIC) {
1298 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1299 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
1300 }
1301
1302 /*
Andrzej Kurek154a6052023-04-30 14:11:49 -04001303 * Check that the SAN is structured correctly by parsing it.
1304 * The SAN structure is discarded afterwards.
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001305 */
Andrzej Kurek154a6052023-04-30 14:11:49 -04001306 ret = mbedtls_x509_parse_subject_alt_name(&tmp_san_buf, &tmp_san_name);
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001307 /*
1308 * In case the extension is malformed, return an error,
1309 * and clear the allocated sequences.
1310 */
1311 if (ret != 0 && ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE) {
1312 mbedtls_asn1_sequence_free(subject_alt_name->next);
1313 subject_alt_name->next = NULL;
1314 return ret;
1315 }
1316
Andrzej Kurek154a6052023-04-30 14:11:49 -04001317 mbedtls_x509_free_subject_alt_name(&tmp_san_name);
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001318 /* Allocate and assign next pointer */
1319 if (cur->buf.p != NULL) {
1320 if (cur->next != NULL) {
1321 return MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
1322 }
1323
1324 cur->next = mbedtls_calloc(1, sizeof(mbedtls_asn1_sequence));
1325
1326 if (cur->next == NULL) {
1327 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1328 MBEDTLS_ERR_ASN1_ALLOC_FAILED);
1329 }
1330
1331 cur = cur->next;
1332 }
1333
Hanno Beckerae8f8c42019-09-13 12:24:56 +01001334 cur->buf = tmp_san_buf;
1335 *p += tmp_san_buf.len;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001336 }
1337
1338 /* Set final sequence entry's next pointer to NULL */
1339 cur->next = NULL;
1340
1341 if (*p != end) {
1342 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1343 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1344 }
1345
1346 return 0;
1347}
1348
Przemek Stekiel21903ec2023-02-21 08:32:37 +01001349/*
1350 * SubjectAltName ::= GeneralNames
1351 *
1352 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
1353 *
1354 * GeneralName ::= CHOICE {
1355 * otherName [0] OtherName,
1356 * rfc822Name [1] IA5String,
1357 * dNSName [2] IA5String,
1358 * x400Address [3] ORAddress,
1359 * directoryName [4] Name,
1360 * ediPartyName [5] EDIPartyName,
1361 * uniformResourceIdentifier [6] IA5String,
1362 * iPAddress [7] OCTET STRING,
1363 * registeredID [8] OBJECT IDENTIFIER }
1364 *
1365 * OtherName ::= SEQUENCE {
1366 * type-id OBJECT IDENTIFIER,
1367 * value [0] EXPLICIT ANY DEFINED BY type-id }
1368 *
1369 * EDIPartyName ::= SEQUENCE {
1370 * nameAssigner [0] DirectoryString OPTIONAL,
1371 * partyName [1] DirectoryString }
1372 *
1373 * We list all types, but use the following GeneralName types from RFC 5280:
1374 * "dnsName", "uniformResourceIdentifier" and "hardware_module_name"
1375 * of type "otherName", as defined in RFC 4108.
1376 */
1377int mbedtls_x509_get_subject_alt_name(unsigned char **p,
1378 const unsigned char *end,
1379 mbedtls_x509_sequence *subject_alt_name)
1380{
1381 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1382 size_t len;
1383
1384 /* Get main sequence tag */
1385 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
1386 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1387 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1388 }
1389
1390 if (*p + len != end) {
1391 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1392 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1393 }
1394
1395 return mbedtls_x509_get_subject_alt_name_ext(p, end, subject_alt_name);
1396}
1397
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001398int mbedtls_x509_get_ns_cert_type(unsigned char **p,
1399 const unsigned char *end,
1400 unsigned char *ns_cert_type)
1401{
1402 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1403 mbedtls_x509_bitstring bs = { 0, 0, NULL };
1404
1405 if ((ret = mbedtls_asn1_get_bitstring(p, end, &bs)) != 0) {
1406 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1407 }
1408
Przemek Stekiel32e20912023-01-25 14:26:15 +01001409 /* A bitstring with no flags set is still technically valid, as it will mean
1410 that the certificate has no designated purpose at the time of creation. */
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001411 if (bs.len == 0) {
1412 *ns_cert_type = 0;
1413 return 0;
1414 }
1415
1416 if (bs.len != 1) {
1417 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1418 MBEDTLS_ERR_ASN1_INVALID_LENGTH);
1419 }
1420
1421 /* Get actual bitstring */
1422 *ns_cert_type = *bs.p;
1423 return 0;
1424}
1425
1426int mbedtls_x509_get_key_usage(unsigned char **p,
1427 const unsigned char *end,
1428 unsigned int *key_usage)
1429{
1430 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1431 size_t i;
1432 mbedtls_x509_bitstring bs = { 0, 0, NULL };
1433
1434 if ((ret = mbedtls_asn1_get_bitstring(p, end, &bs)) != 0) {
1435 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1436 }
1437
Przemek Stekiel32e20912023-01-25 14:26:15 +01001438 /* A bitstring with no flags set is still technically valid, as it will mean
1439 that the certificate has no designated purpose at the time of creation. */
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001440 if (bs.len == 0) {
1441 *key_usage = 0;
1442 return 0;
1443 }
1444
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001445 /* Get actual bitstring */
1446 *key_usage = 0;
1447 for (i = 0; i < bs.len && i < sizeof(unsigned int); i++) {
1448 *key_usage |= (unsigned int) bs.p[i] << (8*i);
1449 }
1450
1451 return 0;
1452}
1453
1454int mbedtls_x509_parse_subject_alt_name(const mbedtls_x509_buf *san_buf,
1455 mbedtls_x509_subject_alternative_name *san)
1456{
1457 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1458 switch (san_buf->tag &
1459 (MBEDTLS_ASN1_TAG_CLASS_MASK |
1460 MBEDTLS_ASN1_TAG_VALUE_MASK)) {
1461 /*
1462 * otherName
1463 */
1464 case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME):
1465 {
1466 mbedtls_x509_san_other_name other_name;
1467
1468 ret = x509_get_other_name(san_buf, &other_name);
1469 if (ret != 0) {
1470 return ret;
1471 }
1472
1473 memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name));
1474 san->type = MBEDTLS_X509_SAN_OTHER_NAME;
1475 memcpy(&san->san.other_name,
1476 &other_name, sizeof(other_name));
1477
1478 }
1479 break;
Andrzej Kurek7a05fab2023-02-13 10:03:07 -05001480 /*
1481 * uniformResourceIdentifier
1482 */
1483 case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER):
1484 {
1485 memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name));
1486 san->type = MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001487
Andrzej Kurek7a05fab2023-02-13 10:03:07 -05001488 memcpy(&san->san.unstructured_name,
1489 san_buf, sizeof(*san_buf));
1490
1491 }
1492 break;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001493 /*
1494 * dNSName
1495 */
1496 case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_DNS_NAME):
1497 {
1498 memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name));
1499 san->type = MBEDTLS_X509_SAN_DNS_NAME;
1500
1501 memcpy(&san->san.unstructured_name,
1502 san_buf, sizeof(*san_buf));
Przemek Stekielecee12f2023-02-09 14:43:49 +01001503 }
1504 break;
Przemek Stekiel0ab5b932023-05-29 16:30:50 +02001505 /*
1506 * IP address
1507 */
1508 case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_IP_ADDRESS):
1509 {
1510 memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name));
1511 san->type = MBEDTLS_X509_SAN_IP_ADDRESS;
Przemek Stekiel01cb6eb2023-06-05 16:38:13 +02001512 // Only IPv6 (16 bytes) and IPv4 (4 bytes) types are supported
1513 if (san_buf->len == 4 || san_buf->len == 16) {
1514 memcpy(&san->san.unstructured_name,
1515 san_buf, sizeof(*san_buf));
1516 } else {
1517 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1518 }
Przemek Stekiel0ab5b932023-05-29 16:30:50 +02001519 }
1520 break;
Przemek Stekielecee12f2023-02-09 14:43:49 +01001521 /*
Andrzej Kurek154a6052023-04-30 14:11:49 -04001522 * rfc822Name
Przemek Stekielecee12f2023-02-09 14:43:49 +01001523 */
1524 case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_RFC822_NAME):
1525 {
1526 memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name));
1527 san->type = MBEDTLS_X509_SAN_RFC822_NAME;
1528 memcpy(&san->san.unstructured_name, san_buf, sizeof(*san_buf));
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001529 }
1530 break;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001531 /*
Andrzej Kureke12b01d2023-01-10 06:47:38 -05001532 * directoryName
1533 */
1534 case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_DIRECTORY_NAME):
1535 {
1536 size_t name_len;
1537 unsigned char *p = san_buf->p;
1538 memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name));
1539 san->type = MBEDTLS_X509_SAN_DIRECTORY_NAME;
1540
1541 ret = mbedtls_asn1_get_tag(&p, p + san_buf->len, &name_len,
1542 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE);
1543
1544 if (ret != 0) {
Andrzej Kurekbf8ccd82023-02-13 07:13:30 -05001545 return ret;
Andrzej Kureke12b01d2023-01-10 06:47:38 -05001546 }
1547
1548 if ((ret = mbedtls_x509_get_name(&p, p + name_len,
1549 &san->san.directory_name)) != 0) {
Andrzej Kurekbf8ccd82023-02-13 07:13:30 -05001550 return ret;
Andrzej Kureke12b01d2023-01-10 06:47:38 -05001551 }
1552 }
1553 break;
1554 /*
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001555 * Type not supported
1556 */
1557 default:
1558 return MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
1559 }
1560 return 0;
1561}
1562
Andrzej Kurekd40c2b62023-02-13 07:01:59 -05001563void mbedtls_x509_free_subject_alt_name(mbedtls_x509_subject_alternative_name *san)
1564{
1565 if (san->type == MBEDTLS_X509_SAN_DIRECTORY_NAME) {
1566 mbedtls_asn1_free_named_data_list_shallow(san->san.directory_name.next);
1567 }
1568}
1569
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001570#if !defined(MBEDTLS_X509_REMOVE_INFO)
1571int mbedtls_x509_info_subject_alt_name(char **buf, size_t *size,
1572 const mbedtls_x509_sequence
1573 *subject_alt_name,
1574 const char *prefix)
1575{
1576 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1577 size_t i;
1578 size_t n = *size;
1579 char *p = *buf;
1580 const mbedtls_x509_sequence *cur = subject_alt_name;
1581 mbedtls_x509_subject_alternative_name san;
1582 int parse_ret;
1583
1584 while (cur != NULL) {
1585 memset(&san, 0, sizeof(san));
1586 parse_ret = mbedtls_x509_parse_subject_alt_name(&cur->buf, &san);
1587 if (parse_ret != 0) {
1588 if (parse_ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE) {
1589 ret = mbedtls_snprintf(p, n, "\n%s <unsupported>", prefix);
1590 MBEDTLS_X509_SAFE_SNPRINTF;
1591 } else {
1592 ret = mbedtls_snprintf(p, n, "\n%s <malformed>", prefix);
1593 MBEDTLS_X509_SAFE_SNPRINTF;
1594 }
1595 cur = cur->next;
1596 continue;
1597 }
1598
1599 switch (san.type) {
1600 /*
1601 * otherName
1602 */
1603 case MBEDTLS_X509_SAN_OTHER_NAME:
1604 {
1605 mbedtls_x509_san_other_name *other_name = &san.san.other_name;
1606
1607 ret = mbedtls_snprintf(p, n, "\n%s otherName :", prefix);
1608 MBEDTLS_X509_SAFE_SNPRINTF;
1609
1610 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ON_HW_MODULE_NAME,
David Horstmanncfae6a12023-08-18 19:12:59 +01001611 &other_name->type_id) == 0) {
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001612 ret = mbedtls_snprintf(p, n, "\n%s hardware module name :", prefix);
1613 MBEDTLS_X509_SAFE_SNPRINTF;
1614 ret =
1615 mbedtls_snprintf(p, n, "\n%s hardware type : ", prefix);
1616 MBEDTLS_X509_SAFE_SNPRINTF;
1617
1618 ret = mbedtls_oid_get_numeric_string(p,
1619 n,
1620 &other_name->value.hardware_module_name.oid);
1621 MBEDTLS_X509_SAFE_SNPRINTF;
1622
1623 ret =
1624 mbedtls_snprintf(p, n, "\n%s hardware serial number : ", prefix);
1625 MBEDTLS_X509_SAFE_SNPRINTF;
1626
1627 for (i = 0; i < other_name->value.hardware_module_name.val.len; i++) {
1628 ret = mbedtls_snprintf(p,
1629 n,
1630 "%02X",
1631 other_name->value.hardware_module_name.val.p[i]);
1632 MBEDTLS_X509_SAFE_SNPRINTF;
1633 }
1634 }/* MBEDTLS_OID_ON_HW_MODULE_NAME */
1635 }
1636 break;
Andrzej Kurek7a05fab2023-02-13 10:03:07 -05001637 /*
1638 * uniformResourceIdentifier
1639 */
1640 case MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER:
1641 {
1642 ret = mbedtls_snprintf(p, n, "\n%s uniformResourceIdentifier : ", prefix);
1643 MBEDTLS_X509_SAFE_SNPRINTF;
1644 if (san.san.unstructured_name.len >= n) {
Przemek Stekiel01cb6eb2023-06-05 16:38:13 +02001645 if (n > 0) {
1646 *p = '\0';
1647 }
Andrzej Kurek7a05fab2023-02-13 10:03:07 -05001648 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1649 }
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001650
Andrzej Kurek7a05fab2023-02-13 10:03:07 -05001651 memcpy(p, san.san.unstructured_name.p, san.san.unstructured_name.len);
1652 p += san.san.unstructured_name.len;
1653 n -= san.san.unstructured_name.len;
1654 }
1655 break;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001656 /*
1657 * dNSName
Przemek Stekiel5b9e4162023-02-15 12:56:37 +01001658 * RFC822 Name
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001659 */
1660 case MBEDTLS_X509_SAN_DNS_NAME:
Przemek Stekiel5b9e4162023-02-15 12:56:37 +01001661 case MBEDTLS_X509_SAN_RFC822_NAME:
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001662 {
Przemek Stekiel82d250d2023-02-15 15:00:50 +01001663 const char *dns_name = "dNSName";
1664 const char *rfc822_name = "rfc822Name";
Przemek Stekiel5b9e4162023-02-15 12:56:37 +01001665
Przemek Stekiel82d250d2023-02-15 15:00:50 +01001666 ret = mbedtls_snprintf(p, n,
1667 "\n%s %s : ",
1668 prefix,
Przemek Stekiel093c97d2023-06-02 10:11:32 +02001669 san.type ==
1670 MBEDTLS_X509_SAN_DNS_NAME ? dns_name : rfc822_name);
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001671 MBEDTLS_X509_SAFE_SNPRINTF;
1672 if (san.san.unstructured_name.len >= n) {
Przemek Stekiel01cb6eb2023-06-05 16:38:13 +02001673 if (n > 0) {
1674 *p = '\0';
1675 }
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001676 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1677 }
1678
Przemek Stekiel093c97d2023-06-02 10:11:32 +02001679 memcpy(p, san.san.unstructured_name.p, san.san.unstructured_name.len);
1680 p += san.san.unstructured_name.len;
1681 n -= san.san.unstructured_name.len;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001682 }
1683 break;
Przemek Stekiel093c97d2023-06-02 10:11:32 +02001684 /*
1685 * iPAddress
1686 */
1687 case MBEDTLS_X509_SAN_IP_ADDRESS:
1688 {
1689 ret = mbedtls_snprintf(p, n, "\n%s %s : ",
1690 prefix, "iPAddress");
1691 MBEDTLS_X509_SAFE_SNPRINTF;
1692 if (san.san.unstructured_name.len >= n) {
Przemek Stekiel01cb6eb2023-06-05 16:38:13 +02001693 if (n > 0) {
1694 *p = '\0';
1695 }
Przemek Stekiel093c97d2023-06-02 10:11:32 +02001696 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1697 }
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001698
Przemek Stekiel093c97d2023-06-02 10:11:32 +02001699 unsigned char *ip = san.san.unstructured_name.p;
1700 // Only IPv6 (16 bytes) and IPv4 (4 bytes) types are supported
1701 if (san.san.unstructured_name.len == 4) {
Przemek Stekiel4d3fc212023-06-06 11:40:32 +02001702 ret = mbedtls_snprintf(p, n, "%u.%u.%u.%u", ip[0], ip[1], ip[2], ip[3]);
1703 MBEDTLS_X509_SAFE_SNPRINTF;
Przemek Stekiel093c97d2023-06-02 10:11:32 +02001704 } else if (san.san.unstructured_name.len == 16) {
Przemek Stekiel4d3fc212023-06-06 11:40:32 +02001705 ret = mbedtls_snprintf(p, n,
Przemek Stekiel01cb6eb2023-06-05 16:38:13 +02001706 "%X%X:%X%X:%X%X:%X%X:%X%X:%X%X:%X%X:%X%X",
1707 ip[0], ip[1], ip[2], ip[3], ip[4], ip[5], ip[6],
Przemek Stekiel4d3fc212023-06-06 11:40:32 +02001708 ip[7], ip[8], ip[9], ip[10], ip[11], ip[12], ip[13],
Przemek Stekiel01cb6eb2023-06-05 16:38:13 +02001709 ip[14], ip[15]);
Przemek Stekiel4d3fc212023-06-06 11:40:32 +02001710 MBEDTLS_X509_SAFE_SNPRINTF;
Przemek Stekiel093c97d2023-06-02 10:11:32 +02001711 } else {
Przemek Stekiel01cb6eb2023-06-05 16:38:13 +02001712 if (n > 0) {
1713 *p = '\0';
1714 }
Przemek Stekiel093c97d2023-06-02 10:11:32 +02001715 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1716 }
Przemek Stekiel093c97d2023-06-02 10:11:32 +02001717 }
1718 break;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001719 /*
Andrzej Kureke12b01d2023-01-10 06:47:38 -05001720 * directoryName
1721 */
1722 case MBEDTLS_X509_SAN_DIRECTORY_NAME:
1723 {
1724 ret = mbedtls_snprintf(p, n, "\n%s directoryName : ", prefix);
Andrzej Kurek5f0c6e82023-02-27 16:03:41 -05001725 if (ret < 0 || (size_t) ret >= n) {
1726 mbedtls_x509_free_subject_alt_name(&san);
1727 }
1728
Andrzej Kureke12b01d2023-01-10 06:47:38 -05001729 MBEDTLS_X509_SAFE_SNPRINTF;
1730 ret = mbedtls_x509_dn_gets(p, n, &san.san.directory_name);
Andrzej Kurekd40c2b62023-02-13 07:01:59 -05001731
Andrzej Kureke12b01d2023-01-10 06:47:38 -05001732 if (ret < 0) {
Andrzej Kurek5f0c6e82023-02-27 16:03:41 -05001733 mbedtls_x509_free_subject_alt_name(&san);
Przemek Stekiel01cb6eb2023-06-05 16:38:13 +02001734 if (n > 0) {
1735 *p = '\0';
1736 }
Andrzej Kureke12b01d2023-01-10 06:47:38 -05001737 return ret;
1738 }
1739
1740 p += ret;
1741 n -= ret;
1742 }
1743 break;
1744 /*
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001745 * Type not supported, skip item.
1746 */
1747 default:
1748 ret = mbedtls_snprintf(p, n, "\n%s <unsupported>", prefix);
1749 MBEDTLS_X509_SAFE_SNPRINTF;
1750 break;
1751 }
1752
Andrzej Kurekd40c2b62023-02-13 07:01:59 -05001753 /* So far memory is freed only in the case of directoryName
Andrzej Kurek5f0c6e82023-02-27 16:03:41 -05001754 * parsing succeeding, as mbedtls_x509_get_name allocates memory. */
Andrzej Kurekd40c2b62023-02-13 07:01:59 -05001755 mbedtls_x509_free_subject_alt_name(&san);
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001756 cur = cur->next;
1757 }
1758
1759 *p = '\0';
1760
1761 *size = n;
1762 *buf = p;
1763
1764 return 0;
1765}
1766
Demi Marie Obenour690b8c92022-12-04 04:24:22 -05001767#define PRINT_ITEM(i) \
1768 do { \
1769 ret = mbedtls_snprintf(p, n, "%s" i, sep); \
1770 MBEDTLS_X509_SAFE_SNPRINTF; \
1771 sep = ", "; \
1772 } while (0)
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001773
Demi Marie Obenour690b8c92022-12-04 04:24:22 -05001774#define CERT_TYPE(type, name) \
1775 do { \
1776 if (ns_cert_type & (type)) { \
1777 PRINT_ITEM(name); \
1778 } \
1779 } while (0)
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001780
1781int mbedtls_x509_info_cert_type(char **buf, size_t *size,
1782 unsigned char ns_cert_type)
1783{
1784 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1785 size_t n = *size;
1786 char *p = *buf;
1787 const char *sep = "";
1788
1789 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT, "SSL Client");
1790 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER, "SSL Server");
1791 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_EMAIL, "Email");
1792 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing");
1793 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_RESERVED, "Reserved");
1794 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_SSL_CA, "SSL CA");
1795 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA, "Email CA");
1796 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA");
1797
1798 *size = n;
1799 *buf = p;
1800
1801 return 0;
1802}
1803
Demi Marie Obenour690b8c92022-12-04 04:24:22 -05001804#define KEY_USAGE(code, name) \
1805 do { \
1806 if ((key_usage) & (code)) { \
1807 PRINT_ITEM(name); \
1808 } \
1809 } while (0)
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001810
1811int mbedtls_x509_info_key_usage(char **buf, size_t *size,
1812 unsigned int key_usage)
1813{
1814 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1815 size_t n = *size;
1816 char *p = *buf;
1817 const char *sep = "";
1818
1819 KEY_USAGE(MBEDTLS_X509_KU_DIGITAL_SIGNATURE, "Digital Signature");
1820 KEY_USAGE(MBEDTLS_X509_KU_NON_REPUDIATION, "Non Repudiation");
1821 KEY_USAGE(MBEDTLS_X509_KU_KEY_ENCIPHERMENT, "Key Encipherment");
1822 KEY_USAGE(MBEDTLS_X509_KU_DATA_ENCIPHERMENT, "Data Encipherment");
1823 KEY_USAGE(MBEDTLS_X509_KU_KEY_AGREEMENT, "Key Agreement");
1824 KEY_USAGE(MBEDTLS_X509_KU_KEY_CERT_SIGN, "Key Cert Sign");
1825 KEY_USAGE(MBEDTLS_X509_KU_CRL_SIGN, "CRL Sign");
1826 KEY_USAGE(MBEDTLS_X509_KU_ENCIPHER_ONLY, "Encipher Only");
1827 KEY_USAGE(MBEDTLS_X509_KU_DECIPHER_ONLY, "Decipher Only");
1828
1829 *size = n;
1830 *buf = p;
1831
1832 return 0;
1833}
1834#endif /* MBEDTLS_X509_REMOVE_INFO */
1835#endif /* MBEDTLS_X509_CRT_PARSE_C || MBEDTLS_X509_CSR_PARSE_C */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001836#endif /* MBEDTLS_X509_USE_C */