blob: 0571687daa143a777fe3dada34f05ca99a51eada [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,
718 mbedtls_md_type_t *md_alg, mbedtls_pk_type_t *pk_alg,
719 void **sig_opts)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200720{
Janos Follath865b3eb2019-12-16 11:46:15 +0000721 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200722
Gilles Peskine449bd832023-01-11 14:50:10 +0100723 if (*sig_opts != NULL) {
724 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
725 }
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200726
Gilles Peskine449bd832023-01-11 14:50:10 +0100727 if ((ret = mbedtls_oid_get_sig_alg(sig_oid, md_alg, pk_alg)) != 0) {
728 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG, ret);
729 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200731#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100732 if (*pk_alg == MBEDTLS_PK_RSASSA_PSS) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200733 mbedtls_pk_rsassa_pss_options *pss_opts;
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100734
Gilles Peskine449bd832023-01-11 14:50:10 +0100735 pss_opts = mbedtls_calloc(1, sizeof(mbedtls_pk_rsassa_pss_options));
736 if (pss_opts == NULL) {
737 return MBEDTLS_ERR_X509_ALLOC_FAILED;
738 }
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200739
Gilles Peskine449bd832023-01-11 14:50:10 +0100740 ret = mbedtls_x509_get_rsassa_pss_params(sig_params,
741 md_alg,
742 &pss_opts->mgf1_hash_id,
743 &pss_opts->expected_salt_len);
744 if (ret != 0) {
745 mbedtls_free(pss_opts);
746 return ret;
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200747 }
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100748
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200749 *sig_opts = (void *) pss_opts;
Gilles Peskine449bd832023-01-11 14:50:10 +0100750 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200751#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100752 {
753 /* Make sure parameters are absent or NULL */
Gilles Peskine449bd832023-01-11 14:50:10 +0100754 if ((sig_params->tag != MBEDTLS_ASN1_NULL && sig_params->tag != 0) ||
755 sig_params->len != 0) {
756 return MBEDTLS_ERR_X509_INVALID_ALG;
757 }
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100758 }
759
Gilles Peskine449bd832023-01-11 14:50:10 +0100760 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200761}
762
763/*
764 * X.509 Extensions (No parsing of extensions, pointer should
Brian J Murray1903fb32016-11-06 04:45:15 -0800765 * be either manually updated or extensions should be parsed!)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200766 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100767int mbedtls_x509_get_ext(unsigned char **p, const unsigned char *end,
768 mbedtls_x509_buf *ext, int tag)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200769{
Janos Follath865b3eb2019-12-16 11:46:15 +0000770 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200771 size_t len;
772
Hanno Becker3cddba82019-02-11 14:33:36 +0000773 /* Extension structure use EXPLICIT tagging. That is, the actual
774 * `Extensions` structure is wrapped by a tag-length pair using
775 * the respective context-specific tag. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100776 ret = mbedtls_asn1_get_tag(p, end, &ext->len,
777 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | tag);
778 if (ret != 0) {
779 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
780 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200781
Hanno Becker6ccfb182019-02-12 11:52:10 +0000782 ext->tag = MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | tag;
783 ext->p = *p;
784 end = *p + ext->len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200785
786 /*
787 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200788 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100789 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
790 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
791 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
792 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200793
Gilles Peskine449bd832023-01-11 14:50:10 +0100794 if (end != *p + len) {
795 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
796 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
797 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200798
Gilles Peskine449bd832023-01-11 14:50:10 +0100799 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200800}
801
Agathiyan Bragadeesh9d2507c2023-07-24 16:35:57 +0100802static char nibble_to_hex_digit(int i)
Agathiyan Bragadeeshef2decb2023-07-21 15:47:47 +0100803{
Agathiyan Bragadeeshf0e1ac52023-07-24 16:43:36 +0100804 return (i < 10) ? (i + '0') : (i - 10 + 'A');
Agathiyan Bragadeeshef2decb2023-07-21 15:47:47 +0100805}
806
Sam Berry4f761942024-07-19 14:56:30 +0100807/* Return the x.y.z.... style numeric string for the given OID */
808int mbedtls_oid_get_numeric_string(char *buf, size_t size,
809 const mbedtls_asn1_buf *oid)
810{
811 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
812 char *p = buf;
813 size_t n = size;
814 unsigned int value = 0;
815
816 if (size > INT_MAX) {
817 /* Avoid overflow computing return value */
818 return MBEDTLS_ERR_ASN1_INVALID_LENGTH;
819 }
820
821 if (oid->len <= 0) {
822 /* OID must not be empty */
823 return MBEDTLS_ERR_ASN1_OUT_OF_DATA;
824 }
825
826 for (size_t i = 0; i < oid->len; i++) {
827 /* Prevent overflow in value. */
828 if (value > (UINT_MAX >> 7)) {
829 return MBEDTLS_ERR_ASN1_INVALID_DATA;
830 }
831 if ((value == 0) && ((oid->p[i]) == 0x80)) {
832 /* Overlong encoding is not allowed */
833 return MBEDTLS_ERR_ASN1_INVALID_DATA;
834 }
835
836 value <<= 7;
837 value |= oid->p[i] & 0x7F;
838
839 if (!(oid->p[i] & 0x80)) {
840 /* Last byte */
841 if (n == size) {
842 int component1;
843 unsigned int component2;
844 /* First subidentifier contains first two OID components */
845 if (value >= 80) {
846 component1 = '2';
847 component2 = value - 80;
848 } else if (value >= 40) {
849 component1 = '1';
850 component2 = value - 40;
851 } else {
852 component1 = '0';
853 component2 = value;
854 }
855 ret = mbedtls_snprintf(p, n, "%c.%u", component1, component2);
856 } else {
857 ret = mbedtls_snprintf(p, n, ".%u", value);
858 }
859 if (ret < 2 || (size_t) ret >= n) {
860 return MBEDTLS_ERR_OID_BUF_TOO_SMALL;
861 }
862 n -= (size_t) ret;
863 p += ret;
864 value = 0;
865 }
866 }
867
868 if (value != 0) {
869 /* Unterminated subidentifier */
870 return MBEDTLS_ERR_ASN1_OUT_OF_DATA;
871 }
872
873 return (int) (size - n);
874}
875
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200876/*
877 * Store the name in printable form into buf; no more
878 * than size characters will be written
879 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100880int mbedtls_x509_dn_gets(char *buf, size_t size, const mbedtls_x509_name *dn)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200881{
Janos Follath865b3eb2019-12-16 11:46:15 +0000882 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh5adffb22023-08-10 15:50:57 +0100883 size_t i, j, n, asn1_len_size, asn1_tag_size, asn1_tag_len_buf_start;
Agathiyan Bragadeeshc7959b22023-09-12 17:54:43 +0100884 /* 6 is enough as our asn1 write functions only write one byte for the tag and at most five bytes for the length*/
885 unsigned char asn1_tag_len_buf[6];
Agathiyan Bragadeeshddc720d2023-07-26 15:51:49 +0100886 unsigned char *asn1_len_p;
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000887 unsigned char c, merge = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200888 const mbedtls_x509_name *name;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200889 const char *short_name = NULL;
Agathiyan Bragadeesha1f5c2d2023-08-02 17:08:52 +0100890 char lowbits, highbits;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200891 char s[MBEDTLS_X509_MAX_DN_NAME_SIZE], *p;
Agathiyan Bragadeeshc9d74f32023-07-31 17:25:44 +0100892 int print_hexstring;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200893
Gilles Peskine449bd832023-01-11 14:50:10 +0100894 memset(s, 0, sizeof(s));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200895
896 name = dn;
897 p = buf;
898 n = size;
899
Gilles Peskine449bd832023-01-11 14:50:10 +0100900 while (name != NULL) {
901 if (!name->oid.p) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200902 name = name->next;
903 continue;
904 }
905
Gilles Peskine449bd832023-01-11 14:50:10 +0100906 if (name != dn) {
907 ret = mbedtls_snprintf(p, n, merge ? " + " : ", ");
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200908 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200909 }
910
Agathiyan Bragadeesh0a4b6d82023-08-02 15:05:57 +0100911 print_hexstring = (name->val.tag != MBEDTLS_ASN1_UTF8_STRING) &&
912 (name->val.tag != MBEDTLS_ASN1_PRINTABLE_STRING) &&
913 (name->val.tag != MBEDTLS_ASN1_IA5_STRING);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200914
Agathiyan Bragadeeshc9d74f32023-07-31 17:25:44 +0100915 if ((ret = mbedtls_oid_get_attr_short_name(&name->oid, &short_name)) == 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100916 ret = mbedtls_snprintf(p, n, "%s=", short_name);
917 } else {
Agathiyan Bragadeeshaf70c7d2023-08-10 16:39:23 +0100918 if ((ret = mbedtls_oid_get_numeric_string(p, n, &name->oid)) > 0) {
Agathiyan Bragadeeshf3b97242023-08-22 16:37:11 +0100919 n -= ret;
920 p += ret;
Agathiyan Bragadeeshaf70c7d2023-08-10 16:39:23 +0100921 ret = mbedtls_snprintf(p, n, "=");
Agathiyan Bragadeeshc9d74f32023-07-31 17:25:44 +0100922 print_hexstring = 1;
Agathiyan Bragadeesh8aa74ab2023-08-22 16:42:27 +0100923 } else if (ret == MBEDTLS_ERR_OID_BUF_TOO_SMALL) {
924 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
Agathiyan Bragadeesh15df0122023-08-22 17:50:00 +0100925 } else {
Agathiyan Bragadeeshddc720d2023-07-26 15:51:49 +0100926 ret = mbedtls_snprintf(p, n, "\?\?=");
927 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100928 }
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200929 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200930
Agathiyan Bragadeesh4987c8f2023-08-01 11:10:52 +0100931 if (print_hexstring) {
Agathiyan Bragadeeshddc720d2023-07-26 15:51:49 +0100932 s[0] = '#';
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200933
Agathiyan Bragadeeshc7959b22023-09-12 17:54:43 +0100934 asn1_len_p = asn1_tag_len_buf + sizeof(asn1_tag_len_buf);
Agathiyan Bragadeesh15df0122023-08-22 17:50:00 +0100935 if ((ret = mbedtls_asn1_write_len(&asn1_len_p, asn1_tag_len_buf, name->val.len)) < 0) {
Agathiyan Bragadeesh07f472a2023-08-22 16:29:39 +0100936 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
937 }
938 asn1_len_size = ret;
Agathiyan Bragadeesh15df0122023-08-22 17:50:00 +0100939 if ((ret = mbedtls_asn1_write_tag(&asn1_len_p, asn1_tag_len_buf, name->val.tag)) < 0) {
Agathiyan Bragadeesh07f472a2023-08-22 16:29:39 +0100940 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
941 }
942 asn1_tag_size = ret;
Agathiyan Bragadeeshc7959b22023-09-12 17:54:43 +0100943 asn1_tag_len_buf_start = sizeof(asn1_tag_len_buf) - asn1_len_size - asn1_tag_size;
Agathiyan Bragadeesh5adffb22023-08-10 15:50:57 +0100944 for (i = 0, j = 1; i < asn1_len_size + asn1_tag_size; i++) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100945 if (j + 1 >= sizeof(s) - 1) {
946 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
947 }
Agathiyan Bragadeesh5adffb22023-08-10 15:50:57 +0100948 c = asn1_tag_len_buf[asn1_tag_len_buf_start+i];
Agathiyan Bragadeesha1f5c2d2023-08-02 17:08:52 +0100949 lowbits = (c & 0x0F);
Agathiyan Bragadeesh2bf09a62023-08-10 14:37:00 +0100950 highbits = c >> 4;
Agathiyan Bragadeesh9d2507c2023-07-24 16:35:57 +0100951 s[j++] = nibble_to_hex_digit(highbits);
Agathiyan Bragadeesh5adffb22023-08-10 15:50:57 +0100952 s[j++] = nibble_to_hex_digit(lowbits);
Werner Lewisb33dacd2022-05-20 12:48:46 +0100953 }
Agathiyan Bragadeesh5adffb22023-08-10 15:50:57 +0100954 for (i = 0; i < name->val.len; i++) {
955 if (j + 1 >= sizeof(s) - 1) {
956 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
957 }
958 c = name->val.p[i];
959 lowbits = (c & 0x0F);
960 highbits = c >> 4;
961 s[j++] = nibble_to_hex_digit(highbits);
962 s[j++] = nibble_to_hex_digit(lowbits);
Agathiyan Bragadeeshddc720d2023-07-26 15:51:49 +0100963 }
964 } else {
965 for (i = 0, j = 0; i < name->val.len; i++, j++) {
966 if (j >= sizeof(s) - 1) {
967 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
968 }
969
970 c = name->val.p[i];
971 // Special characters requiring escaping, RFC 4514 Section 2.4
Agathiyan Bragadeesh022f86f2023-08-22 16:56:04 +0100972 if (c == '\0') {
973 return MBEDTLS_ERR_X509_INVALID_NAME;
Agathiyan Bragadeesh15df0122023-08-22 17:50:00 +0100974 } else {
Agathiyan Bragadeesha7f96302023-08-10 16:03:27 +0100975 if (strchr(",=+<>;\"\\", c) ||
Agathiyan Bragadeeshddc720d2023-07-26 15:51:49 +0100976 ((i == 0) && strchr("# ", c)) ||
977 ((i == name->val.len-1) && (c == ' '))) {
978 if (j + 1 >= sizeof(s) - 1) {
979 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
980 }
981 s[j++] = '\\';
982 }
983 }
984 if (c < 32 || c >= 127) {
985 if (j + 3 >= sizeof(s) - 1) {
986 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
987 }
988 s[j++] = '\\';
Agathiyan Bragadeesha1f5c2d2023-08-02 17:08:52 +0100989 lowbits = (c & 0x0F);
Agathiyan Bragadeesh2bf09a62023-08-10 14:37:00 +0100990 highbits = c >> 4;
Agathiyan Bragadeeshddc720d2023-07-26 15:51:49 +0100991 s[j++] = nibble_to_hex_digit(highbits);
992 s[j] = nibble_to_hex_digit(lowbits);
993 } else {
994 s[j] = c;
995 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100996 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200997 }
Werner Lewisb33dacd2022-05-20 12:48:46 +0100998 s[j] = '\0';
Gilles Peskine449bd832023-01-11 14:50:10 +0100999 ret = mbedtls_snprintf(p, n, "%s", s);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001000 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +00001001
1002 merge = name->next_merged;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001003 name = name->next;
1004 }
1005
Gilles Peskine449bd832023-01-11 14:50:10 +01001006 return (int) (size - n);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001007}
1008
1009/*
1010 * Store the serial in printable form into buf; no more
1011 * than size characters will be written
1012 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001013int mbedtls_x509_serial_gets(char *buf, size_t size, const mbedtls_x509_buf *serial)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001014{
Janos Follath865b3eb2019-12-16 11:46:15 +00001015 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001016 size_t i, n, nr;
1017 char *p;
1018
1019 p = buf;
1020 n = size;
1021
Gilles Peskine449bd832023-01-11 14:50:10 +01001022 nr = (serial->len <= 32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001023 ? serial->len : 28;
1024
Gilles Peskine449bd832023-01-11 14:50:10 +01001025 for (i = 0; i < nr; i++) {
1026 if (i == 0 && nr > 1 && serial->p[i] == 0x0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001027 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001028 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001029
Gilles Peskine449bd832023-01-11 14:50:10 +01001030 ret = mbedtls_snprintf(p, n, "%02X%s",
1031 serial->p[i], (i < nr - 1) ? ":" : "");
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001032 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001033 }
1034
Gilles Peskine449bd832023-01-11 14:50:10 +01001035 if (nr != serial->len) {
1036 ret = mbedtls_snprintf(p, n, "....");
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001037 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001038 }
1039
Gilles Peskine449bd832023-01-11 14:50:10 +01001040 return (int) (size - n);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001041}
1042
Hanno Becker612a2f12020-10-09 09:19:39 +01001043#if !defined(MBEDTLS_X509_REMOVE_INFO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001044/*
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +02001045 * Helper for writing signature algorithms
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +01001046 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001047int mbedtls_x509_sig_alg_gets(char *buf, size_t size, const mbedtls_x509_buf *sig_oid,
1048 mbedtls_pk_type_t pk_alg, mbedtls_md_type_t md_alg,
1049 const void *sig_opts)
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +01001050{
Janos Follath865b3eb2019-12-16 11:46:15 +00001051 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +01001052 char *p = buf;
1053 size_t n = size;
1054 const char *desc = NULL;
1055
Gilles Peskine449bd832023-01-11 14:50:10 +01001056 ret = mbedtls_oid_get_sig_alg_desc(sig_oid, &desc);
1057 if (ret != 0) {
1058 ret = mbedtls_snprintf(p, n, "???");
1059 } else {
1060 ret = mbedtls_snprintf(p, n, "%s", desc);
1061 }
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001062 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +01001063
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001064#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001065 if (pk_alg == MBEDTLS_PK_RSASSA_PSS) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001066 const mbedtls_pk_rsassa_pss_options *pss_opts;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +01001067
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001068 pss_opts = (const mbedtls_pk_rsassa_pss_options *) sig_opts;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +01001069
Gilles Peskine449bd832023-01-11 14:50:10 +01001070 const char *name = md_type_to_string(md_alg);
1071 const char *mgf_name = md_type_to_string(pss_opts->mgf1_hash_id);
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +01001072
Gilles Peskine449bd832023-01-11 14:50:10 +01001073 ret = mbedtls_snprintf(p, n, " (%s, MGF1-%s, 0x%02X)",
1074 name ? name : "???",
1075 mgf_name ? mgf_name : "???",
1076 (unsigned int) pss_opts->expected_salt_len);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001077 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +01001078 }
1079#else
1080 ((void) pk_alg);
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +02001081 ((void) md_alg);
1082 ((void) sig_opts);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001083#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +01001084
Gilles Peskine449bd832023-01-11 14:50:10 +01001085 return (int) (size - n);
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +01001086}
Hanno Becker612a2f12020-10-09 09:19:39 +01001087#endif /* MBEDTLS_X509_REMOVE_INFO */
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +01001088
1089/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001090 * Helper for writing "RSA key size", "EC key size", etc
1091 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001092int mbedtls_x509_key_size_helper(char *buf, size_t buf_size, const char *name)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001093{
1094 char *p = buf;
Manuel Pégourié-Gonnardfb317c52015-06-18 16:25:56 +02001095 size_t n = buf_size;
Janos Follath865b3eb2019-12-16 11:46:15 +00001096 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001097
Gilles Peskine449bd832023-01-11 14:50:10 +01001098 ret = mbedtls_snprintf(p, n, "%s key size", name);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001099 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001100
Gilles Peskine449bd832023-01-11 14:50:10 +01001101 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001102}
1103
Glenn Strauss416dc032022-06-30 00:38:53 -04001104int mbedtls_x509_time_cmp(const mbedtls_x509_time *t1,
1105 const mbedtls_x509_time *t2)
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001106{
Glenn Strauss5aef2972022-06-30 04:38:02 -04001107 int x;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001108
Glenn Strauss5aef2972022-06-30 04:38:02 -04001109 x = (((t1->year << 9) | (t1->mon << 5) | (t1->day)) -
1110 ((t2->year << 9) | (t2->mon << 5) | (t2->day)));
1111 if (x != 0) {
1112 return x;
Manuel Pégourié-Gonnard57e10d72015-06-22 18:59:21 +02001113 }
Manuel Pégourié-Gonnard864108d2015-05-29 10:11:03 +02001114
Glenn Strauss5aef2972022-06-30 04:38:02 -04001115 x = (((t1->hour << 12) | (t1->min << 6) | (t1->sec)) -
1116 ((t2->hour << 12) | (t2->min << 6) | (t2->sec)));
1117 return x;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001118}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001119
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001120#if defined(MBEDTLS_HAVE_TIME_DATE)
Glenn Strauss61d99302022-06-30 05:25:56 -04001121int mbedtls_x509_time_gmtime(mbedtls_time_t tt, mbedtls_x509_time *now)
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001122{
Glenn Strauss811eeb22022-06-30 05:28:50 -04001123 struct tm tm;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001124
Glenn Strauss811eeb22022-06-30 05:28:50 -04001125 if (mbedtls_platform_gmtime_r(&tt, &tm) == NULL) {
1126 return -1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001127 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001128
Glenn Strauss811eeb22022-06-30 05:28:50 -04001129 now->year = tm.tm_year + 1900;
1130 now->mon = tm.tm_mon + 1;
1131 now->day = tm.tm_mday;
1132 now->hour = tm.tm_hour;
1133 now->min = tm.tm_min;
1134 now->sec = tm.tm_sec;
Gilles Peskine449bd832023-01-11 14:50:10 +01001135 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001136}
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001137
Glenn Strauss61d99302022-06-30 05:25:56 -04001138static int x509_get_current_time(mbedtls_x509_time *now)
1139{
1140 return mbedtls_x509_time_gmtime(mbedtls_time(NULL), now);
1141}
1142
Gilles Peskine449bd832023-01-11 14:50:10 +01001143int mbedtls_x509_time_is_past(const mbedtls_x509_time *to)
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001144{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001145 mbedtls_x509_time now;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001146
Gilles Peskine449bd832023-01-11 14:50:10 +01001147 if (x509_get_current_time(&now) != 0) {
1148 return 1;
1149 }
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001150
Glenn Strauss416dc032022-06-30 00:38:53 -04001151 return mbedtls_x509_time_cmp(to, &now) < 0;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001152}
1153
Gilles Peskine449bd832023-01-11 14:50:10 +01001154int mbedtls_x509_time_is_future(const mbedtls_x509_time *from)
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001155{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001156 mbedtls_x509_time now;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001157
Gilles Peskine449bd832023-01-11 14:50:10 +01001158 if (x509_get_current_time(&now) != 0) {
1159 return 1;
1160 }
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001161
Glenn Strauss416dc032022-06-30 00:38:53 -04001162 return mbedtls_x509_time_cmp(from, &now) > 0;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001163}
1164
Manuel Pégourié-Gonnard60c793b2015-06-18 20:52:58 +02001165#else /* MBEDTLS_HAVE_TIME_DATE */
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001166
Gilles Peskine449bd832023-01-11 14:50:10 +01001167int mbedtls_x509_time_is_past(const mbedtls_x509_time *to)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001168{
1169 ((void) to);
Gilles Peskine449bd832023-01-11 14:50:10 +01001170 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001171}
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001172
Gilles Peskine449bd832023-01-11 14:50:10 +01001173int mbedtls_x509_time_is_future(const mbedtls_x509_time *from)
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001174{
1175 ((void) from);
Gilles Peskine449bd832023-01-11 14:50:10 +01001176 return 0;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001177}
Manuel Pégourié-Gonnard60c793b2015-06-18 20:52:58 +02001178#endif /* MBEDTLS_HAVE_TIME_DATE */
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001179
1180/* Common functions for parsing CRT and CSR. */
1181#if defined(MBEDTLS_X509_CRT_PARSE_C) || defined(MBEDTLS_X509_CSR_PARSE_C)
1182/*
1183 * OtherName ::= SEQUENCE {
1184 * type-id OBJECT IDENTIFIER,
1185 * value [0] EXPLICIT ANY DEFINED BY type-id }
1186 *
1187 * HardwareModuleName ::= SEQUENCE {
1188 * hwType OBJECT IDENTIFIER,
1189 * hwSerialNum OCTET STRING }
1190 *
1191 * NOTE: we currently only parse and use otherName of type HwModuleName,
1192 * as defined in RFC 4108.
1193 */
1194static int x509_get_other_name(const mbedtls_x509_buf *subject_alt_name,
1195 mbedtls_x509_san_other_name *other_name)
1196{
1197 int ret = 0;
1198 size_t len;
1199 unsigned char *p = subject_alt_name->p;
1200 const unsigned char *end = p + subject_alt_name->len;
1201 mbedtls_x509_buf cur_oid;
1202
1203 if ((subject_alt_name->tag &
1204 (MBEDTLS_ASN1_TAG_CLASS_MASK | MBEDTLS_ASN1_TAG_VALUE_MASK)) !=
1205 (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME)) {
1206 /*
1207 * The given subject alternative name is not of type "othername".
1208 */
1209 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1210 }
1211
1212 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1213 MBEDTLS_ASN1_OID)) != 0) {
1214 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1215 }
1216
1217 cur_oid.tag = MBEDTLS_ASN1_OID;
1218 cur_oid.p = p;
1219 cur_oid.len = len;
1220
1221 /*
1222 * Only HwModuleName is currently supported.
1223 */
1224 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ON_HW_MODULE_NAME, &cur_oid) != 0) {
1225 return MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
1226 }
David Horstmann2ea44d22023-08-18 18:36:02 +01001227 other_name->type_id = cur_oid;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001228
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001229 p += len;
1230 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1231 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC)) !=
1232 0) {
1233 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1234 }
1235
Hanno Beckerdae916b2019-09-13 14:21:13 +01001236 if (end != p + len) {
1237 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1238 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1239 }
1240
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001241 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1242 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1243 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1244 }
1245
Hanno Beckerdae916b2019-09-13 14:21:13 +01001246 if (end != p + len) {
1247 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1248 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1249 }
1250
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001251 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OID)) != 0) {
1252 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1253 }
1254
1255 other_name->value.hardware_module_name.oid.tag = MBEDTLS_ASN1_OID;
1256 other_name->value.hardware_module_name.oid.p = p;
1257 other_name->value.hardware_module_name.oid.len = len;
1258
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001259 p += len;
1260 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1261 MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1262 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1263 }
1264
1265 other_name->value.hardware_module_name.val.tag = MBEDTLS_ASN1_OCTET_STRING;
1266 other_name->value.hardware_module_name.val.p = p;
1267 other_name->value.hardware_module_name.val.len = len;
1268 p += len;
1269 if (p != end) {
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001270 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1271 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1272 }
1273 return 0;
1274}
1275
Przemek Stekiel21903ec2023-02-21 08:32:37 +01001276/* Check mbedtls_x509_get_subject_alt_name for detailed description.
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001277 *
Przemek Stekiel21903ec2023-02-21 08:32:37 +01001278 * In some cases while parsing subject alternative names the sequence tag is optional
1279 * (e.g. CertSerialNumber). This function is designed to handle such case.
Przemek Stekiel725688b2023-04-04 22:49:44 +02001280 */
Przemek Stekiel21903ec2023-02-21 08:32:37 +01001281int mbedtls_x509_get_subject_alt_name_ext(unsigned char **p,
1282 const unsigned char *end,
1283 mbedtls_x509_sequence *subject_alt_name)
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001284{
1285 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel21903ec2023-02-21 08:32:37 +01001286 size_t tag_len;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001287 mbedtls_asn1_sequence *cur = subject_alt_name;
1288
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001289 while (*p < end) {
Andrzej Kurek154a6052023-04-30 14:11:49 -04001290 mbedtls_x509_subject_alternative_name tmp_san_name;
Hanno Beckerae8f8c42019-09-13 12:24:56 +01001291 mbedtls_x509_buf tmp_san_buf;
Andrzej Kurek154a6052023-04-30 14:11:49 -04001292 memset(&tmp_san_name, 0, sizeof(tmp_san_name));
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001293
Hanno Beckerae8f8c42019-09-13 12:24:56 +01001294 tmp_san_buf.tag = **p;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001295 (*p)++;
Hanno Beckerae8f8c42019-09-13 12:24:56 +01001296
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001297 if ((ret = mbedtls_asn1_get_len(p, end, &tag_len)) != 0) {
1298 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1299 }
1300
Hanno Beckerae8f8c42019-09-13 12:24:56 +01001301 tmp_san_buf.p = *p;
1302 tmp_san_buf.len = tag_len;
1303
1304 if ((tmp_san_buf.tag & MBEDTLS_ASN1_TAG_CLASS_MASK) !=
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001305 MBEDTLS_ASN1_CONTEXT_SPECIFIC) {
1306 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1307 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
1308 }
1309
1310 /*
Andrzej Kurek154a6052023-04-30 14:11:49 -04001311 * Check that the SAN is structured correctly by parsing it.
1312 * The SAN structure is discarded afterwards.
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001313 */
Andrzej Kurek154a6052023-04-30 14:11:49 -04001314 ret = mbedtls_x509_parse_subject_alt_name(&tmp_san_buf, &tmp_san_name);
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001315 /*
1316 * In case the extension is malformed, return an error,
1317 * and clear the allocated sequences.
1318 */
1319 if (ret != 0 && ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE) {
1320 mbedtls_asn1_sequence_free(subject_alt_name->next);
1321 subject_alt_name->next = NULL;
1322 return ret;
1323 }
1324
Andrzej Kurek154a6052023-04-30 14:11:49 -04001325 mbedtls_x509_free_subject_alt_name(&tmp_san_name);
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001326 /* Allocate and assign next pointer */
1327 if (cur->buf.p != NULL) {
1328 if (cur->next != NULL) {
1329 return MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
1330 }
1331
1332 cur->next = mbedtls_calloc(1, sizeof(mbedtls_asn1_sequence));
1333
1334 if (cur->next == NULL) {
1335 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1336 MBEDTLS_ERR_ASN1_ALLOC_FAILED);
1337 }
1338
1339 cur = cur->next;
1340 }
1341
Hanno Beckerae8f8c42019-09-13 12:24:56 +01001342 cur->buf = tmp_san_buf;
1343 *p += tmp_san_buf.len;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001344 }
1345
1346 /* Set final sequence entry's next pointer to NULL */
1347 cur->next = NULL;
1348
1349 if (*p != end) {
1350 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1351 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1352 }
1353
1354 return 0;
1355}
1356
Przemek Stekiel21903ec2023-02-21 08:32:37 +01001357/*
1358 * SubjectAltName ::= GeneralNames
1359 *
1360 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
1361 *
1362 * GeneralName ::= CHOICE {
1363 * otherName [0] OtherName,
1364 * rfc822Name [1] IA5String,
1365 * dNSName [2] IA5String,
1366 * x400Address [3] ORAddress,
1367 * directoryName [4] Name,
1368 * ediPartyName [5] EDIPartyName,
1369 * uniformResourceIdentifier [6] IA5String,
1370 * iPAddress [7] OCTET STRING,
1371 * registeredID [8] OBJECT IDENTIFIER }
1372 *
1373 * OtherName ::= SEQUENCE {
1374 * type-id OBJECT IDENTIFIER,
1375 * value [0] EXPLICIT ANY DEFINED BY type-id }
1376 *
1377 * EDIPartyName ::= SEQUENCE {
1378 * nameAssigner [0] DirectoryString OPTIONAL,
1379 * partyName [1] DirectoryString }
1380 *
1381 * We list all types, but use the following GeneralName types from RFC 5280:
1382 * "dnsName", "uniformResourceIdentifier" and "hardware_module_name"
1383 * of type "otherName", as defined in RFC 4108.
1384 */
1385int mbedtls_x509_get_subject_alt_name(unsigned char **p,
1386 const unsigned char *end,
1387 mbedtls_x509_sequence *subject_alt_name)
1388{
1389 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1390 size_t len;
1391
1392 /* Get main sequence tag */
1393 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
1394 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1395 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1396 }
1397
1398 if (*p + len != end) {
1399 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1400 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1401 }
1402
1403 return mbedtls_x509_get_subject_alt_name_ext(p, end, subject_alt_name);
1404}
1405
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001406int mbedtls_x509_get_ns_cert_type(unsigned char **p,
1407 const unsigned char *end,
1408 unsigned char *ns_cert_type)
1409{
1410 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1411 mbedtls_x509_bitstring bs = { 0, 0, NULL };
1412
1413 if ((ret = mbedtls_asn1_get_bitstring(p, end, &bs)) != 0) {
1414 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1415 }
1416
Przemek Stekiel32e20912023-01-25 14:26:15 +01001417 /* A bitstring with no flags set is still technically valid, as it will mean
1418 that the certificate has no designated purpose at the time of creation. */
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001419 if (bs.len == 0) {
1420 *ns_cert_type = 0;
1421 return 0;
1422 }
1423
1424 if (bs.len != 1) {
1425 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1426 MBEDTLS_ERR_ASN1_INVALID_LENGTH);
1427 }
1428
1429 /* Get actual bitstring */
1430 *ns_cert_type = *bs.p;
1431 return 0;
1432}
1433
1434int mbedtls_x509_get_key_usage(unsigned char **p,
1435 const unsigned char *end,
1436 unsigned int *key_usage)
1437{
1438 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1439 size_t i;
1440 mbedtls_x509_bitstring bs = { 0, 0, NULL };
1441
1442 if ((ret = mbedtls_asn1_get_bitstring(p, end, &bs)) != 0) {
1443 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1444 }
1445
Przemek Stekiel32e20912023-01-25 14:26:15 +01001446 /* A bitstring with no flags set is still technically valid, as it will mean
1447 that the certificate has no designated purpose at the time of creation. */
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001448 if (bs.len == 0) {
1449 *key_usage = 0;
1450 return 0;
1451 }
1452
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001453 /* Get actual bitstring */
1454 *key_usage = 0;
1455 for (i = 0; i < bs.len && i < sizeof(unsigned int); i++) {
1456 *key_usage |= (unsigned int) bs.p[i] << (8*i);
1457 }
1458
1459 return 0;
1460}
1461
1462int mbedtls_x509_parse_subject_alt_name(const mbedtls_x509_buf *san_buf,
1463 mbedtls_x509_subject_alternative_name *san)
1464{
1465 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1466 switch (san_buf->tag &
1467 (MBEDTLS_ASN1_TAG_CLASS_MASK |
1468 MBEDTLS_ASN1_TAG_VALUE_MASK)) {
1469 /*
1470 * otherName
1471 */
1472 case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME):
1473 {
1474 mbedtls_x509_san_other_name other_name;
1475
1476 ret = x509_get_other_name(san_buf, &other_name);
1477 if (ret != 0) {
1478 return ret;
1479 }
1480
1481 memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name));
1482 san->type = MBEDTLS_X509_SAN_OTHER_NAME;
1483 memcpy(&san->san.other_name,
1484 &other_name, sizeof(other_name));
1485
1486 }
1487 break;
Andrzej Kurek7a05fab2023-02-13 10:03:07 -05001488 /*
1489 * uniformResourceIdentifier
1490 */
1491 case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER):
1492 {
1493 memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name));
1494 san->type = MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001495
Andrzej Kurek7a05fab2023-02-13 10:03:07 -05001496 memcpy(&san->san.unstructured_name,
1497 san_buf, sizeof(*san_buf));
1498
1499 }
1500 break;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001501 /*
1502 * dNSName
1503 */
1504 case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_DNS_NAME):
1505 {
1506 memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name));
1507 san->type = MBEDTLS_X509_SAN_DNS_NAME;
1508
1509 memcpy(&san->san.unstructured_name,
1510 san_buf, sizeof(*san_buf));
Przemek Stekielecee12f2023-02-09 14:43:49 +01001511 }
1512 break;
Przemek Stekiel0ab5b932023-05-29 16:30:50 +02001513 /*
1514 * IP address
1515 */
1516 case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_IP_ADDRESS):
1517 {
1518 memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name));
1519 san->type = MBEDTLS_X509_SAN_IP_ADDRESS;
Przemek Stekiel01cb6eb2023-06-05 16:38:13 +02001520 // Only IPv6 (16 bytes) and IPv4 (4 bytes) types are supported
1521 if (san_buf->len == 4 || san_buf->len == 16) {
1522 memcpy(&san->san.unstructured_name,
1523 san_buf, sizeof(*san_buf));
1524 } else {
1525 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1526 }
Przemek Stekiel0ab5b932023-05-29 16:30:50 +02001527 }
1528 break;
Przemek Stekielecee12f2023-02-09 14:43:49 +01001529 /*
Andrzej Kurek154a6052023-04-30 14:11:49 -04001530 * rfc822Name
Przemek Stekielecee12f2023-02-09 14:43:49 +01001531 */
1532 case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_RFC822_NAME):
1533 {
1534 memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name));
1535 san->type = MBEDTLS_X509_SAN_RFC822_NAME;
1536 memcpy(&san->san.unstructured_name, san_buf, sizeof(*san_buf));
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001537 }
1538 break;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001539 /*
Andrzej Kureke12b01d2023-01-10 06:47:38 -05001540 * directoryName
1541 */
1542 case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_DIRECTORY_NAME):
1543 {
1544 size_t name_len;
1545 unsigned char *p = san_buf->p;
1546 memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name));
1547 san->type = MBEDTLS_X509_SAN_DIRECTORY_NAME;
1548
1549 ret = mbedtls_asn1_get_tag(&p, p + san_buf->len, &name_len,
1550 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE);
1551
1552 if (ret != 0) {
Andrzej Kurekbf8ccd82023-02-13 07:13:30 -05001553 return ret;
Andrzej Kureke12b01d2023-01-10 06:47:38 -05001554 }
1555
1556 if ((ret = mbedtls_x509_get_name(&p, p + name_len,
1557 &san->san.directory_name)) != 0) {
Andrzej Kurekbf8ccd82023-02-13 07:13:30 -05001558 return ret;
Andrzej Kureke12b01d2023-01-10 06:47:38 -05001559 }
1560 }
1561 break;
1562 /*
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001563 * Type not supported
1564 */
1565 default:
1566 return MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
1567 }
1568 return 0;
1569}
1570
Andrzej Kurekd40c2b62023-02-13 07:01:59 -05001571void mbedtls_x509_free_subject_alt_name(mbedtls_x509_subject_alternative_name *san)
1572{
1573 if (san->type == MBEDTLS_X509_SAN_DIRECTORY_NAME) {
1574 mbedtls_asn1_free_named_data_list_shallow(san->san.directory_name.next);
1575 }
1576}
1577
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001578#if !defined(MBEDTLS_X509_REMOVE_INFO)
1579int mbedtls_x509_info_subject_alt_name(char **buf, size_t *size,
1580 const mbedtls_x509_sequence
1581 *subject_alt_name,
1582 const char *prefix)
1583{
1584 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1585 size_t i;
1586 size_t n = *size;
1587 char *p = *buf;
1588 const mbedtls_x509_sequence *cur = subject_alt_name;
1589 mbedtls_x509_subject_alternative_name san;
1590 int parse_ret;
1591
1592 while (cur != NULL) {
1593 memset(&san, 0, sizeof(san));
1594 parse_ret = mbedtls_x509_parse_subject_alt_name(&cur->buf, &san);
1595 if (parse_ret != 0) {
1596 if (parse_ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE) {
1597 ret = mbedtls_snprintf(p, n, "\n%s <unsupported>", prefix);
1598 MBEDTLS_X509_SAFE_SNPRINTF;
1599 } else {
1600 ret = mbedtls_snprintf(p, n, "\n%s <malformed>", prefix);
1601 MBEDTLS_X509_SAFE_SNPRINTF;
1602 }
1603 cur = cur->next;
1604 continue;
1605 }
1606
1607 switch (san.type) {
1608 /*
1609 * otherName
1610 */
1611 case MBEDTLS_X509_SAN_OTHER_NAME:
1612 {
1613 mbedtls_x509_san_other_name *other_name = &san.san.other_name;
1614
1615 ret = mbedtls_snprintf(p, n, "\n%s otherName :", prefix);
1616 MBEDTLS_X509_SAFE_SNPRINTF;
1617
1618 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ON_HW_MODULE_NAME,
David Horstmanncfae6a12023-08-18 19:12:59 +01001619 &other_name->type_id) == 0) {
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001620 ret = mbedtls_snprintf(p, n, "\n%s hardware module name :", prefix);
1621 MBEDTLS_X509_SAFE_SNPRINTF;
1622 ret =
1623 mbedtls_snprintf(p, n, "\n%s hardware type : ", prefix);
1624 MBEDTLS_X509_SAFE_SNPRINTF;
1625
1626 ret = mbedtls_oid_get_numeric_string(p,
1627 n,
1628 &other_name->value.hardware_module_name.oid);
1629 MBEDTLS_X509_SAFE_SNPRINTF;
1630
1631 ret =
1632 mbedtls_snprintf(p, n, "\n%s hardware serial number : ", prefix);
1633 MBEDTLS_X509_SAFE_SNPRINTF;
1634
1635 for (i = 0; i < other_name->value.hardware_module_name.val.len; i++) {
1636 ret = mbedtls_snprintf(p,
1637 n,
1638 "%02X",
1639 other_name->value.hardware_module_name.val.p[i]);
1640 MBEDTLS_X509_SAFE_SNPRINTF;
1641 }
1642 }/* MBEDTLS_OID_ON_HW_MODULE_NAME */
1643 }
1644 break;
Andrzej Kurek7a05fab2023-02-13 10:03:07 -05001645 /*
1646 * uniformResourceIdentifier
1647 */
1648 case MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER:
1649 {
1650 ret = mbedtls_snprintf(p, n, "\n%s uniformResourceIdentifier : ", prefix);
1651 MBEDTLS_X509_SAFE_SNPRINTF;
1652 if (san.san.unstructured_name.len >= n) {
Przemek Stekiel01cb6eb2023-06-05 16:38:13 +02001653 if (n > 0) {
1654 *p = '\0';
1655 }
Andrzej Kurek7a05fab2023-02-13 10:03:07 -05001656 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1657 }
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001658
Andrzej Kurek7a05fab2023-02-13 10:03:07 -05001659 memcpy(p, san.san.unstructured_name.p, san.san.unstructured_name.len);
1660 p += san.san.unstructured_name.len;
1661 n -= san.san.unstructured_name.len;
1662 }
1663 break;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001664 /*
1665 * dNSName
Przemek Stekiel5b9e4162023-02-15 12:56:37 +01001666 * RFC822 Name
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001667 */
1668 case MBEDTLS_X509_SAN_DNS_NAME:
Przemek Stekiel5b9e4162023-02-15 12:56:37 +01001669 case MBEDTLS_X509_SAN_RFC822_NAME:
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001670 {
Przemek Stekiel82d250d2023-02-15 15:00:50 +01001671 const char *dns_name = "dNSName";
1672 const char *rfc822_name = "rfc822Name";
Przemek Stekiel5b9e4162023-02-15 12:56:37 +01001673
Przemek Stekiel82d250d2023-02-15 15:00:50 +01001674 ret = mbedtls_snprintf(p, n,
1675 "\n%s %s : ",
1676 prefix,
Przemek Stekiel093c97d2023-06-02 10:11:32 +02001677 san.type ==
1678 MBEDTLS_X509_SAN_DNS_NAME ? dns_name : rfc822_name);
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001679 MBEDTLS_X509_SAFE_SNPRINTF;
1680 if (san.san.unstructured_name.len >= n) {
Przemek Stekiel01cb6eb2023-06-05 16:38:13 +02001681 if (n > 0) {
1682 *p = '\0';
1683 }
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001684 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1685 }
1686
Przemek Stekiel093c97d2023-06-02 10:11:32 +02001687 memcpy(p, san.san.unstructured_name.p, san.san.unstructured_name.len);
1688 p += san.san.unstructured_name.len;
1689 n -= san.san.unstructured_name.len;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001690 }
1691 break;
Przemek Stekiel093c97d2023-06-02 10:11:32 +02001692 /*
1693 * iPAddress
1694 */
1695 case MBEDTLS_X509_SAN_IP_ADDRESS:
1696 {
1697 ret = mbedtls_snprintf(p, n, "\n%s %s : ",
1698 prefix, "iPAddress");
1699 MBEDTLS_X509_SAFE_SNPRINTF;
1700 if (san.san.unstructured_name.len >= n) {
Przemek Stekiel01cb6eb2023-06-05 16:38:13 +02001701 if (n > 0) {
1702 *p = '\0';
1703 }
Przemek Stekiel093c97d2023-06-02 10:11:32 +02001704 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1705 }
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001706
Przemek Stekiel093c97d2023-06-02 10:11:32 +02001707 unsigned char *ip = san.san.unstructured_name.p;
1708 // Only IPv6 (16 bytes) and IPv4 (4 bytes) types are supported
1709 if (san.san.unstructured_name.len == 4) {
Przemek Stekiel4d3fc212023-06-06 11:40:32 +02001710 ret = mbedtls_snprintf(p, n, "%u.%u.%u.%u", ip[0], ip[1], ip[2], ip[3]);
1711 MBEDTLS_X509_SAFE_SNPRINTF;
Przemek Stekiel093c97d2023-06-02 10:11:32 +02001712 } else if (san.san.unstructured_name.len == 16) {
Przemek Stekiel4d3fc212023-06-06 11:40:32 +02001713 ret = mbedtls_snprintf(p, n,
Przemek Stekiel01cb6eb2023-06-05 16:38:13 +02001714 "%X%X:%X%X:%X%X:%X%X:%X%X:%X%X:%X%X:%X%X",
1715 ip[0], ip[1], ip[2], ip[3], ip[4], ip[5], ip[6],
Przemek Stekiel4d3fc212023-06-06 11:40:32 +02001716 ip[7], ip[8], ip[9], ip[10], ip[11], ip[12], ip[13],
Przemek Stekiel01cb6eb2023-06-05 16:38:13 +02001717 ip[14], ip[15]);
Przemek Stekiel4d3fc212023-06-06 11:40:32 +02001718 MBEDTLS_X509_SAFE_SNPRINTF;
Przemek Stekiel093c97d2023-06-02 10:11:32 +02001719 } else {
Przemek Stekiel01cb6eb2023-06-05 16:38:13 +02001720 if (n > 0) {
1721 *p = '\0';
1722 }
Przemek Stekiel093c97d2023-06-02 10:11:32 +02001723 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1724 }
Przemek Stekiel093c97d2023-06-02 10:11:32 +02001725 }
1726 break;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001727 /*
Andrzej Kureke12b01d2023-01-10 06:47:38 -05001728 * directoryName
1729 */
1730 case MBEDTLS_X509_SAN_DIRECTORY_NAME:
1731 {
1732 ret = mbedtls_snprintf(p, n, "\n%s directoryName : ", prefix);
Andrzej Kurek5f0c6e82023-02-27 16:03:41 -05001733 if (ret < 0 || (size_t) ret >= n) {
1734 mbedtls_x509_free_subject_alt_name(&san);
1735 }
1736
Andrzej Kureke12b01d2023-01-10 06:47:38 -05001737 MBEDTLS_X509_SAFE_SNPRINTF;
1738 ret = mbedtls_x509_dn_gets(p, n, &san.san.directory_name);
Andrzej Kurekd40c2b62023-02-13 07:01:59 -05001739
Andrzej Kureke12b01d2023-01-10 06:47:38 -05001740 if (ret < 0) {
Andrzej Kurek5f0c6e82023-02-27 16:03:41 -05001741 mbedtls_x509_free_subject_alt_name(&san);
Przemek Stekiel01cb6eb2023-06-05 16:38:13 +02001742 if (n > 0) {
1743 *p = '\0';
1744 }
Andrzej Kureke12b01d2023-01-10 06:47:38 -05001745 return ret;
1746 }
1747
1748 p += ret;
1749 n -= ret;
1750 }
1751 break;
1752 /*
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001753 * Type not supported, skip item.
1754 */
1755 default:
1756 ret = mbedtls_snprintf(p, n, "\n%s <unsupported>", prefix);
1757 MBEDTLS_X509_SAFE_SNPRINTF;
1758 break;
1759 }
1760
Andrzej Kurekd40c2b62023-02-13 07:01:59 -05001761 /* So far memory is freed only in the case of directoryName
Andrzej Kurek5f0c6e82023-02-27 16:03:41 -05001762 * parsing succeeding, as mbedtls_x509_get_name allocates memory. */
Andrzej Kurekd40c2b62023-02-13 07:01:59 -05001763 mbedtls_x509_free_subject_alt_name(&san);
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001764 cur = cur->next;
1765 }
1766
1767 *p = '\0';
1768
1769 *size = n;
1770 *buf = p;
1771
1772 return 0;
1773}
1774
Demi Marie Obenour690b8c92022-12-04 04:24:22 -05001775#define PRINT_ITEM(i) \
1776 do { \
1777 ret = mbedtls_snprintf(p, n, "%s" i, sep); \
1778 MBEDTLS_X509_SAFE_SNPRINTF; \
1779 sep = ", "; \
1780 } while (0)
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001781
Demi Marie Obenour690b8c92022-12-04 04:24:22 -05001782#define CERT_TYPE(type, name) \
1783 do { \
1784 if (ns_cert_type & (type)) { \
1785 PRINT_ITEM(name); \
1786 } \
1787 } while (0)
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001788
1789int mbedtls_x509_info_cert_type(char **buf, size_t *size,
1790 unsigned char ns_cert_type)
1791{
1792 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1793 size_t n = *size;
1794 char *p = *buf;
1795 const char *sep = "";
1796
1797 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT, "SSL Client");
1798 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER, "SSL Server");
1799 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_EMAIL, "Email");
1800 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing");
1801 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_RESERVED, "Reserved");
1802 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_SSL_CA, "SSL CA");
1803 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA, "Email CA");
1804 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA");
1805
1806 *size = n;
1807 *buf = p;
1808
1809 return 0;
1810}
1811
Demi Marie Obenour690b8c92022-12-04 04:24:22 -05001812#define KEY_USAGE(code, name) \
1813 do { \
1814 if ((key_usage) & (code)) { \
1815 PRINT_ITEM(name); \
1816 } \
1817 } while (0)
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001818
1819int mbedtls_x509_info_key_usage(char **buf, size_t *size,
1820 unsigned int key_usage)
1821{
1822 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1823 size_t n = *size;
1824 char *p = *buf;
1825 const char *sep = "";
1826
1827 KEY_USAGE(MBEDTLS_X509_KU_DIGITAL_SIGNATURE, "Digital Signature");
1828 KEY_USAGE(MBEDTLS_X509_KU_NON_REPUDIATION, "Non Repudiation");
1829 KEY_USAGE(MBEDTLS_X509_KU_KEY_ENCIPHERMENT, "Key Encipherment");
1830 KEY_USAGE(MBEDTLS_X509_KU_DATA_ENCIPHERMENT, "Data Encipherment");
1831 KEY_USAGE(MBEDTLS_X509_KU_KEY_AGREEMENT, "Key Agreement");
1832 KEY_USAGE(MBEDTLS_X509_KU_KEY_CERT_SIGN, "Key Cert Sign");
1833 KEY_USAGE(MBEDTLS_X509_KU_CRL_SIGN, "CRL Sign");
1834 KEY_USAGE(MBEDTLS_X509_KU_ENCIPHER_ONLY, "Encipher Only");
1835 KEY_USAGE(MBEDTLS_X509_KU_DECIPHER_ONLY, "Decipher Only");
1836
1837 *size = n;
1838 *buf = p;
1839
1840 return 0;
1841}
1842#endif /* MBEDTLS_X509_REMOVE_INFO */
1843#endif /* MBEDTLS_X509_CRT_PARSE_C || MBEDTLS_X509_CSR_PARSE_C */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001844#endif /* MBEDTLS_X509_USE_C */