blob: 990393c3108ff4dcf6d3fc28e165e91aaf9634f5 [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
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker7c6b2c32013-09-16 13:49:26 +020018 */
19/*
20 * The ITU-T X.509 standard defines a certificate format for PKI.
21 *
Manuel Pégourié-Gonnard1c082f32014-06-12 22:34:55 +020022 * http://www.ietf.org/rfc/rfc5280.txt (Certificates and CRLs)
23 * http://www.ietf.org/rfc/rfc3279.txt (Alg IDs for CRLs)
24 * http://www.ietf.org/rfc/rfc2986.txt (CSRs, aka PKCS#10)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020025 *
26 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
27 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
28 */
29
Gilles Peskinedb09ef62020-06-03 01:43:33 +020030#include "common.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032#if defined(MBEDTLS_X509_USE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020033
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000034#include "mbedtls/x509.h"
35#include "mbedtls/asn1.h"
Janos Follath73c616b2019-12-18 15:07:04 +000036#include "mbedtls/error.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000037#include "mbedtls/oid.h"
Rich Evans00ab4702015-02-06 13:43:58 +000038
Rich Evans36796df2015-02-12 18:27:14 +000039#include <stdio.h>
Rich Evans00ab4702015-02-06 13:43:58 +000040#include <string.h>
41
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000043#include "mbedtls/pem.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020044#endif
45
Agathiyan Bragadeeshddc720d2023-07-26 15:51:49 +010046#include "mbedtls/asn1write.h"
Agathiyan Bragadeeshddc720d2023-07-26 15:51:49 +010047
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000048#include "mbedtls/platform.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020049
Simon Butcherb5b6af22016-07-13 14:46:18 +010050#if defined(MBEDTLS_HAVE_TIME)
51#include "mbedtls/platform_time.h"
52#endif
Nicholas Wilson512b4ee2017-12-05 12:07:33 +000053#if defined(MBEDTLS_HAVE_TIME_DATE)
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +010054#include "mbedtls/platform_util.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020055#include <time.h>
56#endif
57
Demi Marie Obenour690b8c92022-12-04 04:24:22 -050058#define CHECK(code) \
59 do { \
60 if ((ret = (code)) != 0) { \
61 return ret; \
62 } \
63 } while (0)
64
Hanno Becker1eeca412018-10-15 12:01:35 +010065#define CHECK_RANGE(min, max, val) \
Demi Marie Obenour690b8c92022-12-04 04:24:22 -050066 do { \
67 if ((val) < (min) || (val) > (max)) { \
68 return ret; \
Hanno Becker1eeca412018-10-15 12:01:35 +010069 } \
Gilles Peskine449bd832023-01-11 14:50:10 +010070 } while (0)
Rich Evans7d5a55a2015-02-13 11:48:02 +000071
Paul Bakker7c6b2c32013-09-16 13:49:26 +020072/*
73 * CertificateSerialNumber ::= INTEGER
74 */
Gilles Peskine449bd832023-01-11 14:50:10 +010075int mbedtls_x509_get_serial(unsigned char **p, const unsigned char *end,
76 mbedtls_x509_buf *serial)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020077{
Janos Follath865b3eb2019-12-16 11:46:15 +000078 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020079
Gilles Peskine449bd832023-01-11 14:50:10 +010080 if ((end - *p) < 1) {
81 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_SERIAL,
82 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
83 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +020084
Gilles Peskine449bd832023-01-11 14:50:10 +010085 if (**p != (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_PRIMITIVE | 2) &&
86 **p != MBEDTLS_ASN1_INTEGER) {
87 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_SERIAL,
88 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
89 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +020090
91 serial->tag = *(*p)++;
92
Gilles Peskine449bd832023-01-11 14:50:10 +010093 if ((ret = mbedtls_asn1_get_len(p, end, &serial->len)) != 0) {
94 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_SERIAL, ret);
95 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +020096
97 serial->p = *p;
98 *p += serial->len;
99
Gilles Peskine449bd832023-01-11 14:50:10 +0100100 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200101}
102
103/* Get an algorithm identifier without parameters (eg for signatures)
104 *
105 * AlgorithmIdentifier ::= SEQUENCE {
106 * algorithm OBJECT IDENTIFIER,
107 * parameters ANY DEFINED BY algorithm OPTIONAL }
108 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100109int mbedtls_x509_get_alg_null(unsigned char **p, const unsigned char *end,
110 mbedtls_x509_buf *alg)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200111{
Janos Follath865b3eb2019-12-16 11:46:15 +0000112 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200113
Gilles Peskine449bd832023-01-11 14:50:10 +0100114 if ((ret = mbedtls_asn1_get_alg_null(p, end, alg)) != 0) {
115 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
116 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200117
Gilles Peskine449bd832023-01-11 14:50:10 +0100118 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200119}
120
121/*
Antonin Décimo36e89b52019-01-23 15:24:37 +0100122 * Parse an algorithm identifier with (optional) parameters
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100123 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100124int mbedtls_x509_get_alg(unsigned char **p, const unsigned char *end,
125 mbedtls_x509_buf *alg, mbedtls_x509_buf *params)
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100126{
Janos Follath865b3eb2019-12-16 11:46:15 +0000127 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100128
Gilles Peskine449bd832023-01-11 14:50:10 +0100129 if ((ret = mbedtls_asn1_get_alg(p, end, alg, params)) != 0) {
130 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
131 }
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100132
Gilles Peskine449bd832023-01-11 14:50:10 +0100133 return 0;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100134}
135
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200136/*
137 * Convert md type to string
138 */
Dave Rodgmanffabb7b2023-06-28 16:22:50 +0100139#if !defined(MBEDTLS_X509_REMOVE_INFO) && defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
140
Gilles Peskine449bd832023-01-11 14:50:10 +0100141static inline const char *md_type_to_string(mbedtls_md_type_t md_alg)
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200142{
Gilles Peskine449bd832023-01-11 14:50:10 +0100143 switch (md_alg) {
Manuel Pégourié-Gonnard49e67f82023-03-16 11:39:20 +0100144#if defined(MBEDTLS_MD_CAN_MD5)
Gilles Peskine449bd832023-01-11 14:50:10 +0100145 case MBEDTLS_MD_MD5:
146 return "MD5";
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200147#endif
Manuel Pégourié-Gonnard49e67f82023-03-16 11:39:20 +0100148#if defined(MBEDTLS_MD_CAN_SHA1)
Gilles Peskine449bd832023-01-11 14:50:10 +0100149 case MBEDTLS_MD_SHA1:
150 return "SHA1";
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200151#endif
Manuel Pégourié-Gonnard49e67f82023-03-16 11:39:20 +0100152#if defined(MBEDTLS_MD_CAN_SHA224)
Gilles Peskine449bd832023-01-11 14:50:10 +0100153 case MBEDTLS_MD_SHA224:
154 return "SHA224";
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200155#endif
Manuel Pégourié-Gonnard49e67f82023-03-16 11:39:20 +0100156#if defined(MBEDTLS_MD_CAN_SHA256)
Gilles Peskine449bd832023-01-11 14:50:10 +0100157 case MBEDTLS_MD_SHA256:
158 return "SHA256";
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200159#endif
Manuel Pégourié-Gonnard49e67f82023-03-16 11:39:20 +0100160#if defined(MBEDTLS_MD_CAN_SHA384)
Gilles Peskine449bd832023-01-11 14:50:10 +0100161 case MBEDTLS_MD_SHA384:
162 return "SHA384";
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200163#endif
Manuel Pégourié-Gonnard49e67f82023-03-16 11:39:20 +0100164#if defined(MBEDTLS_MD_CAN_SHA512)
Gilles Peskine449bd832023-01-11 14:50:10 +0100165 case MBEDTLS_MD_SHA512:
166 return "SHA512";
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200167#endif
Manuel Pégourié-Gonnard49e67f82023-03-16 11:39:20 +0100168#if defined(MBEDTLS_MD_CAN_RIPEMD160)
Gilles Peskine449bd832023-01-11 14:50:10 +0100169 case MBEDTLS_MD_RIPEMD160:
170 return "RIPEMD160";
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200171#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100172 case MBEDTLS_MD_NONE:
173 return NULL;
174 default:
175 return NULL;
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200176 }
177}
178
Dave Rodgmanf032c982023-06-29 12:09:27 +0100179#endif /* !defined(MBEDTLS_X509_REMOVE_INFO) && defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) */
Dave Rodgmanffabb7b2023-06-28 16:22:50 +0100180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100182/*
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100183 * HashAlgorithm ::= AlgorithmIdentifier
184 *
185 * AlgorithmIdentifier ::= SEQUENCE {
186 * algorithm OBJECT IDENTIFIER,
187 * parameters ANY DEFINED BY algorithm OPTIONAL }
188 *
189 * For HashAlgorithm, parameters MUST be NULL or absent.
190 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100191static 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 +0100192{
Janos Follath865b3eb2019-12-16 11:46:15 +0000193 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100194 unsigned char *p;
195 const unsigned char *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200196 mbedtls_x509_buf md_oid;
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100197 size_t len;
198
199 /* Make sure we got a SEQUENCE and setup bounds */
Gilles Peskine449bd832023-01-11 14:50:10 +0100200 if (alg->tag != (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) {
201 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
202 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
203 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100204
Andrzej Kurekfeaebc52020-07-16 04:37:41 -0400205 p = alg->p;
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100206 end = p + alg->len;
207
Gilles Peskine449bd832023-01-11 14:50:10 +0100208 if (p >= end) {
209 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
210 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
211 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100212
213 /* Parse md_oid */
214 md_oid.tag = *p;
215
Gilles Peskine449bd832023-01-11 14:50:10 +0100216 if ((ret = mbedtls_asn1_get_tag(&p, end, &md_oid.len, MBEDTLS_ASN1_OID)) != 0) {
217 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
218 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100219
220 md_oid.p = p;
221 p += md_oid.len;
222
223 /* Get md_alg from md_oid */
Gilles Peskine449bd832023-01-11 14:50:10 +0100224 if ((ret = mbedtls_oid_get_md_alg(&md_oid, md_alg)) != 0) {
225 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
226 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100227
228 /* Make sure params is absent of NULL */
Gilles Peskine449bd832023-01-11 14:50:10 +0100229 if (p == end) {
230 return 0;
231 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100232
Gilles Peskine449bd832023-01-11 14:50:10 +0100233 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_NULL)) != 0 || len != 0) {
234 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
235 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100236
Gilles Peskine449bd832023-01-11 14:50:10 +0100237 if (p != end) {
238 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
239 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
240 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100241
Gilles Peskine449bd832023-01-11 14:50:10 +0100242 return 0;
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100243}
244
245/*
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100246 * RSASSA-PSS-params ::= SEQUENCE {
247 * hashAlgorithm [0] HashAlgorithm DEFAULT sha1Identifier,
248 * maskGenAlgorithm [1] MaskGenAlgorithm DEFAULT mgf1SHA1Identifier,
249 * saltLength [2] INTEGER DEFAULT 20,
250 * trailerField [3] INTEGER DEFAULT 1 }
251 * -- Note that the tags in this Sequence are explicit.
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200252 *
253 * RFC 4055 (which defines use of RSASSA-PSS in PKIX) states that the value
254 * of trailerField MUST be 1, and PKCS#1 v2.2 doesn't even define any other
Tom Cosgrove1797b052022-12-04 17:19:59 +0000255 * option. Enforce this at parsing time.
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100256 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100257int mbedtls_x509_get_rsassa_pss_params(const mbedtls_x509_buf *params,
258 mbedtls_md_type_t *md_alg, mbedtls_md_type_t *mgf_md,
259 int *salt_len)
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100260{
Janos Follath865b3eb2019-12-16 11:46:15 +0000261 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100262 unsigned char *p;
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100263 const unsigned char *end, *end2;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100264 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200265 mbedtls_x509_buf alg_id, alg_params;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100266
267 /* First set everything to defaults */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268 *md_alg = MBEDTLS_MD_SHA1;
269 *mgf_md = MBEDTLS_MD_SHA1;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100270 *salt_len = 20;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100271
272 /* Make sure params is a SEQUENCE and setup bounds */
Gilles Peskine449bd832023-01-11 14:50:10 +0100273 if (params->tag != (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) {
274 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
275 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
276 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100277
278 p = (unsigned char *) params->p;
279 end = p + params->len;
280
Gilles Peskine449bd832023-01-11 14:50:10 +0100281 if (p == end) {
282 return 0;
283 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100284
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100285 /*
286 * HashAlgorithm
287 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100288 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
289 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
290 0)) == 0) {
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100291 end2 = p + len;
292
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100293 /* HashAlgorithm ::= AlgorithmIdentifier (without parameters) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100294 if ((ret = mbedtls_x509_get_alg_null(&p, end2, &alg_id)) != 0) {
295 return ret;
296 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100297
Gilles Peskine449bd832023-01-11 14:50:10 +0100298 if ((ret = mbedtls_oid_get_md_alg(&alg_id, md_alg)) != 0) {
299 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
300 }
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100301
Gilles Peskine449bd832023-01-11 14:50:10 +0100302 if (p != end2) {
303 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
304 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
305 }
306 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
307 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100308 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100309
Gilles Peskine449bd832023-01-11 14:50:10 +0100310 if (p == end) {
311 return 0;
312 }
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100313
314 /*
315 * MaskGenAlgorithm
316 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100317 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
318 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
319 1)) == 0) {
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100320 end2 = p + len;
321
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100322 /* MaskGenAlgorithm ::= AlgorithmIdentifier (params = HashAlgorithm) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100323 if ((ret = mbedtls_x509_get_alg(&p, end2, &alg_id, &alg_params)) != 0) {
324 return ret;
325 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100326
327 /* Only MFG1 is recognised for now */
Gilles Peskine449bd832023-01-11 14:50:10 +0100328 if (MBEDTLS_OID_CMP(MBEDTLS_OID_MGF1, &alg_id) != 0) {
329 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE,
330 MBEDTLS_ERR_OID_NOT_FOUND);
331 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100332
333 /* Parse HashAlgorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +0100334 if ((ret = x509_get_hash_alg(&alg_params, mgf_md)) != 0) {
335 return ret;
336 }
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100337
Gilles Peskine449bd832023-01-11 14:50:10 +0100338 if (p != end2) {
339 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
340 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
341 }
342 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
343 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100344 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100345
Gilles Peskine449bd832023-01-11 14:50:10 +0100346 if (p == end) {
347 return 0;
348 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100349
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100350 /*
351 * salt_len
352 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100353 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
354 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
355 2)) == 0) {
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100356 end2 = p + len;
357
Gilles Peskine449bd832023-01-11 14:50:10 +0100358 if ((ret = mbedtls_asn1_get_int(&p, end2, salt_len)) != 0) {
359 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
360 }
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100361
Gilles Peskine449bd832023-01-11 14:50:10 +0100362 if (p != end2) {
363 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
364 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
365 }
366 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
367 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100368 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100369
Gilles Peskine449bd832023-01-11 14:50:10 +0100370 if (p == end) {
371 return 0;
372 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100373
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100374 /*
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200375 * trailer_field (if present, must be 1)
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100376 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100377 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
378 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
379 3)) == 0) {
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200380 int trailer_field;
381
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100382 end2 = p + len;
383
Gilles Peskine449bd832023-01-11 14:50:10 +0100384 if ((ret = mbedtls_asn1_get_int(&p, end2, &trailer_field)) != 0) {
385 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
386 }
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100387
Gilles Peskine449bd832023-01-11 14:50:10 +0100388 if (p != end2) {
389 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
390 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
391 }
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200392
Gilles Peskine449bd832023-01-11 14:50:10 +0100393 if (trailer_field != 1) {
394 return MBEDTLS_ERR_X509_INVALID_ALG;
395 }
396 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
397 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100398 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100399
Gilles Peskine449bd832023-01-11 14:50:10 +0100400 if (p != end) {
401 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
402 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
403 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100404
Gilles Peskine449bd832023-01-11 14:50:10 +0100405 return 0;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100406}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200407#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100408
409/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200410 * AttributeTypeAndValue ::= SEQUENCE {
411 * type AttributeType,
412 * value AttributeValue }
413 *
414 * AttributeType ::= OBJECT IDENTIFIER
415 *
416 * AttributeValue ::= ANY DEFINED BY AttributeType
417 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100418static int x509_get_attr_type_value(unsigned char **p,
419 const unsigned char *end,
420 mbedtls_x509_name *cur)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200421{
Janos Follath865b3eb2019-12-16 11:46:15 +0000422 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200423 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200424 mbedtls_x509_buf *oid;
425 mbedtls_x509_buf *val;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200426
Gilles Peskine449bd832023-01-11 14:50:10 +0100427 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
428 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
429 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME, ret);
430 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200431
Hanno Becker12f62fb2019-02-12 17:22:36 +0000432 end = *p + len;
433
Gilles Peskine449bd832023-01-11 14:50:10 +0100434 if ((end - *p) < 1) {
435 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME,
436 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
437 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200438
439 oid = &cur->oid;
440 oid->tag = **p;
441
Gilles Peskine449bd832023-01-11 14:50:10 +0100442 if ((ret = mbedtls_asn1_get_tag(p, end, &oid->len, MBEDTLS_ASN1_OID)) != 0) {
443 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME, ret);
444 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200445
446 oid->p = *p;
447 *p += oid->len;
448
Gilles Peskine449bd832023-01-11 14:50:10 +0100449 if ((end - *p) < 1) {
450 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME,
451 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
452 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200453
Gilles Peskine449bd832023-01-11 14:50:10 +0100454 if (**p != MBEDTLS_ASN1_BMP_STRING && **p != MBEDTLS_ASN1_UTF8_STRING &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200455 **p != MBEDTLS_ASN1_T61_STRING && **p != MBEDTLS_ASN1_PRINTABLE_STRING &&
456 **p != MBEDTLS_ASN1_IA5_STRING && **p != MBEDTLS_ASN1_UNIVERSAL_STRING &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100457 **p != MBEDTLS_ASN1_BIT_STRING) {
458 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME,
459 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
460 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200461
462 val = &cur->val;
463 val->tag = *(*p)++;
464
Gilles Peskine449bd832023-01-11 14:50:10 +0100465 if ((ret = mbedtls_asn1_get_len(p, end, &val->len)) != 0) {
466 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME, ret);
467 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200468
469 val->p = *p;
470 *p += val->len;
471
Gilles Peskine449bd832023-01-11 14:50:10 +0100472 if (*p != end) {
473 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME,
474 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Hanno Becker12f62fb2019-02-12 17:22:36 +0000475 }
476
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200477 cur->next = NULL;
478
Gilles Peskine449bd832023-01-11 14:50:10 +0100479 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200480}
481
482/*
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000483 * Name ::= CHOICE { -- only one possibility for now --
484 * rdnSequence RDNSequence }
485 *
486 * RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
487 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200488 * RelativeDistinguishedName ::=
489 * SET OF AttributeTypeAndValue
490 *
491 * AttributeTypeAndValue ::= SEQUENCE {
492 * type AttributeType,
493 * value AttributeValue }
494 *
495 * AttributeType ::= OBJECT IDENTIFIER
496 *
497 * AttributeValue ::= ANY DEFINED BY AttributeType
Manuel Pégourié-Gonnard5d861852014-10-17 12:41:41 +0200498 *
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000499 * The data structure is optimized for the common case where each RDN has only
500 * one element, which is represented as a list of AttributeTypeAndValue.
501 * For the general case we still use a flat list, but we mark elements of the
502 * same set so that they are "merged" together in the functions that consume
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200503 * this list, eg mbedtls_x509_dn_gets().
David Horstmanned794832022-10-04 18:12:06 +0100504 *
David Horstmann11307a12022-10-17 18:10:23 +0100505 * On success, this function may allocate a linked list starting at cur->next
David Horstmanned794832022-10-04 18:12:06 +0100506 * that must later be free'd by the caller using mbedtls_free(). In error
507 * cases, this function frees all allocated memory internally and the caller
508 * has no freeing responsibilities.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200509 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100510int mbedtls_x509_get_name(unsigned char **p, const unsigned char *end,
511 mbedtls_x509_name *cur)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200512{
Janos Follath865b3eb2019-12-16 11:46:15 +0000513 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard5d861852014-10-17 12:41:41 +0200514 size_t set_len;
515 const unsigned char *end_set;
David Horstmanned794832022-10-04 18:12:06 +0100516 mbedtls_x509_name *head = cur;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200517
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100518 /* don't use recursion, we'd risk stack overflow if not optimized */
Gilles Peskine449bd832023-01-11 14:50:10 +0100519 while (1) {
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100520 /*
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000521 * parse SET
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100522 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100523 if ((ret = mbedtls_asn1_get_tag(p, end, &set_len,
524 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SET)) != 0) {
525 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME, ret);
David Horstmanned794832022-10-04 18:12:06 +0100526 goto error;
527 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200528
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100529 end_set = *p + set_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200530
Gilles Peskine449bd832023-01-11 14:50:10 +0100531 while (1) {
532 if ((ret = x509_get_attr_type_value(p, end_set, cur)) != 0) {
David Horstmanned794832022-10-04 18:12:06 +0100533 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +0100534 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200535
Gilles Peskine449bd832023-01-11 14:50:10 +0100536 if (*p == end_set) {
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000537 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100538 }
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000539
Manuel Pégourié-Gonnardeecb43c2015-05-12 12:56:41 +0200540 /* Mark this item as being no the only one in a set */
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000541 cur->next_merged = 1;
542
Gilles Peskine449bd832023-01-11 14:50:10 +0100543 cur->next = mbedtls_calloc(1, sizeof(mbedtls_x509_name));
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000544
Gilles Peskine449bd832023-01-11 14:50:10 +0100545 if (cur->next == NULL) {
David Horstmanned794832022-10-04 18:12:06 +0100546 ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
547 goto error;
548 }
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000549
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000550 cur = cur->next;
551 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200552
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100553 /*
554 * continue until end of SEQUENCE is reached
555 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100556 if (*p == end) {
557 return 0;
558 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200559
Gilles Peskine449bd832023-01-11 14:50:10 +0100560 cur->next = mbedtls_calloc(1, sizeof(mbedtls_x509_name));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200561
Gilles Peskine449bd832023-01-11 14:50:10 +0100562 if (cur->next == NULL) {
David Horstmanned794832022-10-04 18:12:06 +0100563 ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
564 goto error;
565 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200566
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100567 cur = cur->next;
568 }
David Horstmanned794832022-10-04 18:12:06 +0100569
570error:
David Horstmanned794832022-10-04 18:12:06 +0100571 /* Skip the first element as we did not allocate it */
Gilles Peskine449bd832023-01-11 14:50:10 +0100572 mbedtls_asn1_free_named_data_list_shallow(head->next);
Glenn Straussa4b40412022-06-26 19:32:09 -0400573 head->next = NULL;
David Horstmanned794832022-10-04 18:12:06 +0100574
Gilles Peskine449bd832023-01-11 14:50:10 +0100575 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200576}
577
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400578static int x509_date_is_valid(const mbedtls_x509_time *t)
Janos Follath87c98072017-02-03 12:36:59 +0000579{
David Horstmanncdf52832023-07-05 09:58:03 +0100580 unsigned int month_days;
581 unsigned int year;
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400582 switch (t->mon) {
583 case 1: case 3: case 5: case 7: case 8: case 10: case 12:
David Horstmanncdf52832023-07-05 09:58:03 +0100584 month_days = 31;
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400585 break;
586 case 4: case 6: case 9: case 11:
David Horstmanncdf52832023-07-05 09:58:03 +0100587 month_days = 30;
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400588 break;
589 case 2:
David Horstmanncdf52832023-07-05 09:58:03 +0100590 year = (unsigned int) t->year;
591 month_days = ((year & 3) || (!(year % 100)
592 && (year % 400)))
593 ? 28 : 29;
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400594 break;
595 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100596 return MBEDTLS_ERR_X509_INVALID_DATE;
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400597 }
Janos Follath87c98072017-02-03 12:36:59 +0000598
David Horstmannb1d27bc2023-07-05 10:00:31 +0100599 if ((unsigned int) (t->day - 1) >= month_days || /* (1 - days in month) */
David Horstmann3ae1c4c2023-07-05 11:15:08 +0100600 /* (unsigned int) (t->mon - 1) >= 12 || */ /* (1 - 12) checked above */
David Horstmannb1d27bc2023-07-05 10:00:31 +0100601 (unsigned int) t->year > 9999 || /* (0 - 9999) */
602 (unsigned int) t->hour > 23 || /* (0 - 23) */
603 (unsigned int) t->min > 59 || /* (0 - 59) */
604 (unsigned int) t->sec > 59) { /* (0 - 59) */
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400605 return MBEDTLS_ERR_X509_INVALID_DATE;
Rich Evans7d5a55a2015-02-13 11:48:02 +0000606 }
Janos Follath87c98072017-02-03 12:36:59 +0000607
Gilles Peskine449bd832023-01-11 14:50:10 +0100608 return 0;
Rich Evans7d5a55a2015-02-13 11:48:02 +0000609}
610
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400611static int x509_parse2_int(const unsigned char *p)
Andres AG4b76aec2016-09-23 13:16:02 +0100612{
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400613 uint32_t d1 = p[0] - '0';
614 uint32_t d2 = p[1] - '0';
615 return (d1 < 10 && d2 < 10) ? (int) (d1 * 10 + d2) : -1;
Andres AG4b76aec2016-09-23 13:16:02 +0100616}
617
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200618/*
Janos Follath87c98072017-02-03 12:36:59 +0000619 * Parse an ASN1_UTC_TIME (yearlen=2) or ASN1_GENERALIZED_TIME (yearlen=4)
620 * field.
621 */
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400622static int x509_parse_time(const unsigned char *p, mbedtls_x509_time *tm,
623 size_t yearlen)
Janos Follath87c98072017-02-03 12:36:59 +0000624{
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400625 int x;
Janos Follath87c98072017-02-03 12:36:59 +0000626
627 /*
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400628 * Parse year, month, day, hour, minute, second
Janos Follath87c98072017-02-03 12:36:59 +0000629 */
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400630 tm->year = x509_parse2_int(p);
631 if (tm->year < 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100632 return MBEDTLS_ERR_X509_INVALID_DATE;
633 }
Janos Follath87c98072017-02-03 12:36:59 +0000634
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400635 if (4 == yearlen) {
636 x = tm->year * 100;
637 p += 2;
638 tm->year = x509_parse2_int(p);
639 if (tm->year < 0) {
640 return MBEDTLS_ERR_X509_INVALID_DATE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100641 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100642 } else {
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400643 x = (tm->year < 50) ? 2000 : 1900;
Janos Follath87c98072017-02-03 12:36:59 +0000644 }
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400645 tm->year += x;
Janos Follath87c98072017-02-03 12:36:59 +0000646
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400647 tm->mon = x509_parse2_int(p + 2);
648 tm->day = x509_parse2_int(p + 4);
649 tm->hour = x509_parse2_int(p + 6);
650 tm->min = x509_parse2_int(p + 8);
651 tm->sec = x509_parse2_int(p + 10);
Janos Follath87c98072017-02-03 12:36:59 +0000652
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400653 return x509_date_is_valid(tm);
Janos Follath87c98072017-02-03 12:36:59 +0000654}
655
656/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200657 * Time ::= CHOICE {
658 * utcTime UTCTime,
659 * generalTime GeneralizedTime }
660 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100661int mbedtls_x509_get_time(unsigned char **p, const unsigned char *end,
662 mbedtls_x509_time *tm)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200663{
Janos Follath865b3eb2019-12-16 11:46:15 +0000664 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Janos Follath87c98072017-02-03 12:36:59 +0000665 size_t len, year_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200666 unsigned char tag;
667
Gilles Peskine449bd832023-01-11 14:50:10 +0100668 if ((end - *p) < 1) {
669 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE,
670 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
671 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200672
673 tag = **p;
674
Gilles Peskine449bd832023-01-11 14:50:10 +0100675 if (tag == MBEDTLS_ASN1_UTC_TIME) {
Janos Follath87c98072017-02-03 12:36:59 +0000676 year_len = 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100677 } else if (tag == MBEDTLS_ASN1_GENERALIZED_TIME) {
Janos Follath87c98072017-02-03 12:36:59 +0000678 year_len = 4;
Gilles Peskine449bd832023-01-11 14:50:10 +0100679 } else {
680 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE,
681 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
682 }
Janos Follath87c98072017-02-03 12:36:59 +0000683
684 (*p)++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100685 ret = mbedtls_asn1_get_len(p, end, &len);
Janos Follath87c98072017-02-03 12:36:59 +0000686
Gilles Peskine449bd832023-01-11 14:50:10 +0100687 if (ret != 0) {
688 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE, ret);
689 }
Janos Follath87c98072017-02-03 12:36:59 +0000690
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400691 /* len is 12 or 14 depending on year_len, plus optional trailing 'Z' */
692 if (len != year_len + 10 &&
693 !(len == year_len + 11 && (*p)[(len - 1)] == 'Z')) {
694 return MBEDTLS_ERR_X509_INVALID_DATE;
695 }
696
697 (*p) += len;
698 return x509_parse_time(*p - len, tm, year_len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200699}
700
Gilles Peskine449bd832023-01-11 14:50:10 +0100701int mbedtls_x509_get_sig(unsigned char **p, const unsigned char *end, mbedtls_x509_buf *sig)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200702{
Janos Follath865b3eb2019-12-16 11:46:15 +0000703 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200704 size_t len;
Andres AG4bdbe092016-09-19 16:58:45 +0100705 int tag_type;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200706
Gilles Peskine449bd832023-01-11 14:50:10 +0100707 if ((end - *p) < 1) {
708 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_SIGNATURE,
709 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
710 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200711
Andres AG4bdbe092016-09-19 16:58:45 +0100712 tag_type = **p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200713
Gilles Peskine449bd832023-01-11 14:50:10 +0100714 if ((ret = mbedtls_asn1_get_bitstring_null(p, end, &len)) != 0) {
715 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_SIGNATURE, ret);
716 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200717
Andres AG4bdbe092016-09-19 16:58:45 +0100718 sig->tag = tag_type;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200719 sig->len = len;
720 sig->p = *p;
721
722 *p += len;
723
Gilles Peskine449bd832023-01-11 14:50:10 +0100724 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200725}
726
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100727/*
728 * Get signature algorithm from alg OID and optional parameters
729 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100730int mbedtls_x509_get_sig_alg(const mbedtls_x509_buf *sig_oid, const mbedtls_x509_buf *sig_params,
731 mbedtls_md_type_t *md_alg, mbedtls_pk_type_t *pk_alg,
732 void **sig_opts)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200733{
Janos Follath865b3eb2019-12-16 11:46:15 +0000734 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200735
Gilles Peskine449bd832023-01-11 14:50:10 +0100736 if (*sig_opts != NULL) {
737 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
738 }
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200739
Gilles Peskine449bd832023-01-11 14:50:10 +0100740 if ((ret = mbedtls_oid_get_sig_alg(sig_oid, md_alg, pk_alg)) != 0) {
741 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG, ret);
742 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200743
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200744#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100745 if (*pk_alg == MBEDTLS_PK_RSASSA_PSS) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200746 mbedtls_pk_rsassa_pss_options *pss_opts;
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100747
Gilles Peskine449bd832023-01-11 14:50:10 +0100748 pss_opts = mbedtls_calloc(1, sizeof(mbedtls_pk_rsassa_pss_options));
749 if (pss_opts == NULL) {
750 return MBEDTLS_ERR_X509_ALLOC_FAILED;
751 }
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200752
Gilles Peskine449bd832023-01-11 14:50:10 +0100753 ret = mbedtls_x509_get_rsassa_pss_params(sig_params,
754 md_alg,
755 &pss_opts->mgf1_hash_id,
756 &pss_opts->expected_salt_len);
757 if (ret != 0) {
758 mbedtls_free(pss_opts);
759 return ret;
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200760 }
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100761
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200762 *sig_opts = (void *) pss_opts;
Gilles Peskine449bd832023-01-11 14:50:10 +0100763 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200764#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100765 {
766 /* Make sure parameters are absent or NULL */
Gilles Peskine449bd832023-01-11 14:50:10 +0100767 if ((sig_params->tag != MBEDTLS_ASN1_NULL && sig_params->tag != 0) ||
768 sig_params->len != 0) {
769 return MBEDTLS_ERR_X509_INVALID_ALG;
770 }
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100771 }
772
Gilles Peskine449bd832023-01-11 14:50:10 +0100773 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200774}
775
776/*
777 * X.509 Extensions (No parsing of extensions, pointer should
Brian J Murray1903fb32016-11-06 04:45:15 -0800778 * be either manually updated or extensions should be parsed!)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200779 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100780int mbedtls_x509_get_ext(unsigned char **p, const unsigned char *end,
781 mbedtls_x509_buf *ext, int tag)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200782{
Janos Follath865b3eb2019-12-16 11:46:15 +0000783 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200784 size_t len;
785
Hanno Becker3cddba82019-02-11 14:33:36 +0000786 /* Extension structure use EXPLICIT tagging. That is, the actual
787 * `Extensions` structure is wrapped by a tag-length pair using
788 * the respective context-specific tag. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100789 ret = mbedtls_asn1_get_tag(p, end, &ext->len,
790 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | tag);
791 if (ret != 0) {
792 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
793 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200794
Hanno Becker6ccfb182019-02-12 11:52:10 +0000795 ext->tag = MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | tag;
796 ext->p = *p;
797 end = *p + ext->len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200798
799 /*
800 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200801 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100802 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
803 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
804 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
805 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200806
Gilles Peskine449bd832023-01-11 14:50:10 +0100807 if (end != *p + len) {
808 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
809 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
810 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200811
Gilles Peskine449bd832023-01-11 14:50:10 +0100812 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200813}
814
Agathiyan Bragadeesh9d2507c2023-07-24 16:35:57 +0100815static char nibble_to_hex_digit(int i)
Agathiyan Bragadeeshef2decb2023-07-21 15:47:47 +0100816{
Agathiyan Bragadeeshf0e1ac52023-07-24 16:43:36 +0100817 return (i < 10) ? (i + '0') : (i - 10 + 'A');
Agathiyan Bragadeeshef2decb2023-07-21 15:47:47 +0100818}
819
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200820/*
821 * Store the name in printable form into buf; no more
822 * than size characters will be written
823 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100824int mbedtls_x509_dn_gets(char *buf, size_t size, const mbedtls_x509_name *dn)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200825{
Janos Follath865b3eb2019-12-16 11:46:15 +0000826 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh5adffb22023-08-10 15:50:57 +0100827 size_t i, j, n, asn1_len_size, asn1_tag_size, asn1_tag_len_buf_start;
Agathiyan Bragadeeshc7959b22023-09-12 17:54:43 +0100828 /* 6 is enough as our asn1 write functions only write one byte for the tag and at most five bytes for the length*/
829 unsigned char asn1_tag_len_buf[6];
Agathiyan Bragadeeshddc720d2023-07-26 15:51:49 +0100830 unsigned char *asn1_len_p;
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000831 unsigned char c, merge = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200832 const mbedtls_x509_name *name;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200833 const char *short_name = NULL;
Agathiyan Bragadeesha1f5c2d2023-08-02 17:08:52 +0100834 char lowbits, highbits;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200835 char s[MBEDTLS_X509_MAX_DN_NAME_SIZE], *p;
Agathiyan Bragadeeshc9d74f32023-07-31 17:25:44 +0100836 int print_hexstring;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200837
Gilles Peskine449bd832023-01-11 14:50:10 +0100838 memset(s, 0, sizeof(s));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200839
840 name = dn;
841 p = buf;
842 n = size;
843
Gilles Peskine449bd832023-01-11 14:50:10 +0100844 while (name != NULL) {
845 if (!name->oid.p) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200846 name = name->next;
847 continue;
848 }
849
Gilles Peskine449bd832023-01-11 14:50:10 +0100850 if (name != dn) {
851 ret = mbedtls_snprintf(p, n, merge ? " + " : ", ");
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200852 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200853 }
854
Agathiyan Bragadeesh0a4b6d82023-08-02 15:05:57 +0100855 print_hexstring = (name->val.tag != MBEDTLS_ASN1_UTF8_STRING) &&
856 (name->val.tag != MBEDTLS_ASN1_PRINTABLE_STRING) &&
857 (name->val.tag != MBEDTLS_ASN1_IA5_STRING);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200858
Agathiyan Bragadeeshc9d74f32023-07-31 17:25:44 +0100859 if ((ret = mbedtls_oid_get_attr_short_name(&name->oid, &short_name)) == 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100860 ret = mbedtls_snprintf(p, n, "%s=", short_name);
861 } else {
Agathiyan Bragadeeshaf70c7d2023-08-10 16:39:23 +0100862 if ((ret = mbedtls_oid_get_numeric_string(p, n, &name->oid)) > 0) {
Agathiyan Bragadeeshf3b97242023-08-22 16:37:11 +0100863 n -= ret;
864 p += ret;
Agathiyan Bragadeeshaf70c7d2023-08-10 16:39:23 +0100865 ret = mbedtls_snprintf(p, n, "=");
Agathiyan Bragadeeshc9d74f32023-07-31 17:25:44 +0100866 print_hexstring = 1;
Agathiyan Bragadeesh8aa74ab2023-08-22 16:42:27 +0100867 } else if (ret == MBEDTLS_ERR_OID_BUF_TOO_SMALL) {
868 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
Agathiyan Bragadeesh15df0122023-08-22 17:50:00 +0100869 } else {
Agathiyan Bragadeeshddc720d2023-07-26 15:51:49 +0100870 ret = mbedtls_snprintf(p, n, "\?\?=");
871 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100872 }
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200873 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200874
Agathiyan Bragadeesh4987c8f2023-08-01 11:10:52 +0100875 if (print_hexstring) {
Agathiyan Bragadeeshddc720d2023-07-26 15:51:49 +0100876 s[0] = '#';
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200877
Agathiyan Bragadeeshc7959b22023-09-12 17:54:43 +0100878 asn1_len_p = asn1_tag_len_buf + sizeof(asn1_tag_len_buf);
Agathiyan Bragadeesh15df0122023-08-22 17:50:00 +0100879 if ((ret = mbedtls_asn1_write_len(&asn1_len_p, asn1_tag_len_buf, name->val.len)) < 0) {
Agathiyan Bragadeesh07f472a2023-08-22 16:29:39 +0100880 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
881 }
882 asn1_len_size = ret;
Agathiyan Bragadeesh15df0122023-08-22 17:50:00 +0100883 if ((ret = mbedtls_asn1_write_tag(&asn1_len_p, asn1_tag_len_buf, name->val.tag)) < 0) {
Agathiyan Bragadeesh07f472a2023-08-22 16:29:39 +0100884 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
885 }
886 asn1_tag_size = ret;
Agathiyan Bragadeeshc7959b22023-09-12 17:54:43 +0100887 asn1_tag_len_buf_start = sizeof(asn1_tag_len_buf) - asn1_len_size - asn1_tag_size;
Agathiyan Bragadeesh5adffb22023-08-10 15:50:57 +0100888 for (i = 0, j = 1; i < asn1_len_size + asn1_tag_size; i++) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100889 if (j + 1 >= sizeof(s) - 1) {
890 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
891 }
Agathiyan Bragadeesh5adffb22023-08-10 15:50:57 +0100892 c = asn1_tag_len_buf[asn1_tag_len_buf_start+i];
Agathiyan Bragadeesha1f5c2d2023-08-02 17:08:52 +0100893 lowbits = (c & 0x0F);
Agathiyan Bragadeesh2bf09a62023-08-10 14:37:00 +0100894 highbits = c >> 4;
Agathiyan Bragadeesh9d2507c2023-07-24 16:35:57 +0100895 s[j++] = nibble_to_hex_digit(highbits);
Agathiyan Bragadeesh5adffb22023-08-10 15:50:57 +0100896 s[j++] = nibble_to_hex_digit(lowbits);
Werner Lewisb33dacd2022-05-20 12:48:46 +0100897 }
Agathiyan Bragadeesh5adffb22023-08-10 15:50:57 +0100898 for (i = 0; i < name->val.len; i++) {
899 if (j + 1 >= sizeof(s) - 1) {
900 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
901 }
902 c = name->val.p[i];
903 lowbits = (c & 0x0F);
904 highbits = c >> 4;
905 s[j++] = nibble_to_hex_digit(highbits);
906 s[j++] = nibble_to_hex_digit(lowbits);
Agathiyan Bragadeeshddc720d2023-07-26 15:51:49 +0100907 }
908 } else {
909 for (i = 0, j = 0; i < name->val.len; i++, j++) {
910 if (j >= sizeof(s) - 1) {
911 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
912 }
913
914 c = name->val.p[i];
915 // Special characters requiring escaping, RFC 4514 Section 2.4
Agathiyan Bragadeesh022f86f2023-08-22 16:56:04 +0100916 if (c == '\0') {
917 return MBEDTLS_ERR_X509_INVALID_NAME;
Agathiyan Bragadeesh15df0122023-08-22 17:50:00 +0100918 } else {
Agathiyan Bragadeesha7f96302023-08-10 16:03:27 +0100919 if (strchr(",=+<>;\"\\", c) ||
Agathiyan Bragadeeshddc720d2023-07-26 15:51:49 +0100920 ((i == 0) && strchr("# ", c)) ||
921 ((i == name->val.len-1) && (c == ' '))) {
922 if (j + 1 >= sizeof(s) - 1) {
923 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
924 }
925 s[j++] = '\\';
926 }
927 }
928 if (c < 32 || c >= 127) {
929 if (j + 3 >= sizeof(s) - 1) {
930 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
931 }
932 s[j++] = '\\';
Agathiyan Bragadeesha1f5c2d2023-08-02 17:08:52 +0100933 lowbits = (c & 0x0F);
Agathiyan Bragadeesh2bf09a62023-08-10 14:37:00 +0100934 highbits = c >> 4;
Agathiyan Bragadeeshddc720d2023-07-26 15:51:49 +0100935 s[j++] = nibble_to_hex_digit(highbits);
936 s[j] = nibble_to_hex_digit(lowbits);
937 } else {
938 s[j] = c;
939 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100940 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200941 }
Werner Lewisb33dacd2022-05-20 12:48:46 +0100942 s[j] = '\0';
Gilles Peskine449bd832023-01-11 14:50:10 +0100943 ret = mbedtls_snprintf(p, n, "%s", s);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200944 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000945
946 merge = name->next_merged;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200947 name = name->next;
948 }
949
Gilles Peskine449bd832023-01-11 14:50:10 +0100950 return (int) (size - n);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200951}
952
953/*
954 * Store the serial in printable form into buf; no more
955 * than size characters will be written
956 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100957int mbedtls_x509_serial_gets(char *buf, size_t size, const mbedtls_x509_buf *serial)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200958{
Janos Follath865b3eb2019-12-16 11:46:15 +0000959 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200960 size_t i, n, nr;
961 char *p;
962
963 p = buf;
964 n = size;
965
Gilles Peskine449bd832023-01-11 14:50:10 +0100966 nr = (serial->len <= 32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200967 ? serial->len : 28;
968
Gilles Peskine449bd832023-01-11 14:50:10 +0100969 for (i = 0; i < nr; i++) {
970 if (i == 0 && nr > 1 && serial->p[i] == 0x0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200971 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100972 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200973
Gilles Peskine449bd832023-01-11 14:50:10 +0100974 ret = mbedtls_snprintf(p, n, "%02X%s",
975 serial->p[i], (i < nr - 1) ? ":" : "");
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200976 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200977 }
978
Gilles Peskine449bd832023-01-11 14:50:10 +0100979 if (nr != serial->len) {
980 ret = mbedtls_snprintf(p, n, "....");
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200981 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200982 }
983
Gilles Peskine449bd832023-01-11 14:50:10 +0100984 return (int) (size - n);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200985}
986
Hanno Becker612a2f12020-10-09 09:19:39 +0100987#if !defined(MBEDTLS_X509_REMOVE_INFO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200988/*
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200989 * Helper for writing signature algorithms
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100990 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100991int mbedtls_x509_sig_alg_gets(char *buf, size_t size, const mbedtls_x509_buf *sig_oid,
992 mbedtls_pk_type_t pk_alg, mbedtls_md_type_t md_alg,
993 const void *sig_opts)
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100994{
Janos Follath865b3eb2019-12-16 11:46:15 +0000995 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100996 char *p = buf;
997 size_t n = size;
998 const char *desc = NULL;
999
Gilles Peskine449bd832023-01-11 14:50:10 +01001000 ret = mbedtls_oid_get_sig_alg_desc(sig_oid, &desc);
1001 if (ret != 0) {
1002 ret = mbedtls_snprintf(p, n, "???");
1003 } else {
1004 ret = mbedtls_snprintf(p, n, "%s", desc);
1005 }
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001006 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +01001007
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001008#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001009 if (pk_alg == MBEDTLS_PK_RSASSA_PSS) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001010 const mbedtls_pk_rsassa_pss_options *pss_opts;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +01001011
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001012 pss_opts = (const mbedtls_pk_rsassa_pss_options *) sig_opts;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +01001013
Gilles Peskine449bd832023-01-11 14:50:10 +01001014 const char *name = md_type_to_string(md_alg);
1015 const char *mgf_name = md_type_to_string(pss_opts->mgf1_hash_id);
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +01001016
Gilles Peskine449bd832023-01-11 14:50:10 +01001017 ret = mbedtls_snprintf(p, n, " (%s, MGF1-%s, 0x%02X)",
1018 name ? name : "???",
1019 mgf_name ? mgf_name : "???",
1020 (unsigned int) pss_opts->expected_salt_len);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001021 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +01001022 }
1023#else
1024 ((void) pk_alg);
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +02001025 ((void) md_alg);
1026 ((void) sig_opts);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001027#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +01001028
Gilles Peskine449bd832023-01-11 14:50:10 +01001029 return (int) (size - n);
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +01001030}
Hanno Becker612a2f12020-10-09 09:19:39 +01001031#endif /* MBEDTLS_X509_REMOVE_INFO */
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +01001032
1033/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001034 * Helper for writing "RSA key size", "EC key size", etc
1035 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001036int mbedtls_x509_key_size_helper(char *buf, size_t buf_size, const char *name)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001037{
1038 char *p = buf;
Manuel Pégourié-Gonnardfb317c52015-06-18 16:25:56 +02001039 size_t n = buf_size;
Janos Follath865b3eb2019-12-16 11:46:15 +00001040 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001041
Gilles Peskine449bd832023-01-11 14:50:10 +01001042 ret = mbedtls_snprintf(p, n, "%s key size", name);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001043 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001044
Gilles Peskine449bd832023-01-11 14:50:10 +01001045 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001046}
1047
Glenn Strauss416dc032022-06-30 00:38:53 -04001048int mbedtls_x509_time_cmp(const mbedtls_x509_time *t1,
1049 const mbedtls_x509_time *t2)
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001050{
Glenn Strauss5aef2972022-06-30 04:38:02 -04001051 int x;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001052
Glenn Strauss5aef2972022-06-30 04:38:02 -04001053 x = (((t1->year << 9) | (t1->mon << 5) | (t1->day)) -
1054 ((t2->year << 9) | (t2->mon << 5) | (t2->day)));
1055 if (x != 0) {
1056 return x;
Manuel Pégourié-Gonnard57e10d72015-06-22 18:59:21 +02001057 }
Manuel Pégourié-Gonnard864108d2015-05-29 10:11:03 +02001058
Glenn Strauss5aef2972022-06-30 04:38:02 -04001059 x = (((t1->hour << 12) | (t1->min << 6) | (t1->sec)) -
1060 ((t2->hour << 12) | (t2->min << 6) | (t2->sec)));
1061 return x;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001062}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001063
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001064#if defined(MBEDTLS_HAVE_TIME_DATE)
Glenn Strauss61d99302022-06-30 05:25:56 -04001065int mbedtls_x509_time_gmtime(mbedtls_time_t tt, mbedtls_x509_time *now)
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001066{
Glenn Strauss811eeb22022-06-30 05:28:50 -04001067 struct tm tm;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001068
Glenn Strauss811eeb22022-06-30 05:28:50 -04001069 if (mbedtls_platform_gmtime_r(&tt, &tm) == NULL) {
1070 return -1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001071 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001072
Glenn Strauss811eeb22022-06-30 05:28:50 -04001073 now->year = tm.tm_year + 1900;
1074 now->mon = tm.tm_mon + 1;
1075 now->day = tm.tm_mday;
1076 now->hour = tm.tm_hour;
1077 now->min = tm.tm_min;
1078 now->sec = tm.tm_sec;
Gilles Peskine449bd832023-01-11 14:50:10 +01001079 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001080}
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001081
Glenn Strauss61d99302022-06-30 05:25:56 -04001082static int x509_get_current_time(mbedtls_x509_time *now)
1083{
1084 return mbedtls_x509_time_gmtime(mbedtls_time(NULL), now);
1085}
1086
Gilles Peskine449bd832023-01-11 14:50:10 +01001087int mbedtls_x509_time_is_past(const mbedtls_x509_time *to)
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001088{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001089 mbedtls_x509_time now;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001090
Gilles Peskine449bd832023-01-11 14:50:10 +01001091 if (x509_get_current_time(&now) != 0) {
1092 return 1;
1093 }
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001094
Glenn Strauss416dc032022-06-30 00:38:53 -04001095 return mbedtls_x509_time_cmp(to, &now) < 0;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001096}
1097
Gilles Peskine449bd832023-01-11 14:50:10 +01001098int mbedtls_x509_time_is_future(const mbedtls_x509_time *from)
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001099{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001100 mbedtls_x509_time now;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001101
Gilles Peskine449bd832023-01-11 14:50:10 +01001102 if (x509_get_current_time(&now) != 0) {
1103 return 1;
1104 }
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001105
Glenn Strauss416dc032022-06-30 00:38:53 -04001106 return mbedtls_x509_time_cmp(from, &now) > 0;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001107}
1108
Manuel Pégourié-Gonnard60c793b2015-06-18 20:52:58 +02001109#else /* MBEDTLS_HAVE_TIME_DATE */
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001110
Gilles Peskine449bd832023-01-11 14:50:10 +01001111int mbedtls_x509_time_is_past(const mbedtls_x509_time *to)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001112{
1113 ((void) to);
Gilles Peskine449bd832023-01-11 14:50:10 +01001114 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001115}
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001116
Gilles Peskine449bd832023-01-11 14:50:10 +01001117int mbedtls_x509_time_is_future(const mbedtls_x509_time *from)
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001118{
1119 ((void) from);
Gilles Peskine449bd832023-01-11 14:50:10 +01001120 return 0;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001121}
Manuel Pégourié-Gonnard60c793b2015-06-18 20:52:58 +02001122#endif /* MBEDTLS_HAVE_TIME_DATE */
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001123
1124/* Common functions for parsing CRT and CSR. */
1125#if defined(MBEDTLS_X509_CRT_PARSE_C) || defined(MBEDTLS_X509_CSR_PARSE_C)
1126/*
1127 * OtherName ::= SEQUENCE {
1128 * type-id OBJECT IDENTIFIER,
1129 * value [0] EXPLICIT ANY DEFINED BY type-id }
1130 *
1131 * HardwareModuleName ::= SEQUENCE {
1132 * hwType OBJECT IDENTIFIER,
1133 * hwSerialNum OCTET STRING }
1134 *
1135 * NOTE: we currently only parse and use otherName of type HwModuleName,
1136 * as defined in RFC 4108.
1137 */
1138static int x509_get_other_name(const mbedtls_x509_buf *subject_alt_name,
1139 mbedtls_x509_san_other_name *other_name)
1140{
1141 int ret = 0;
1142 size_t len;
1143 unsigned char *p = subject_alt_name->p;
1144 const unsigned char *end = p + subject_alt_name->len;
1145 mbedtls_x509_buf cur_oid;
1146
1147 if ((subject_alt_name->tag &
1148 (MBEDTLS_ASN1_TAG_CLASS_MASK | MBEDTLS_ASN1_TAG_VALUE_MASK)) !=
1149 (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME)) {
1150 /*
1151 * The given subject alternative name is not of type "othername".
1152 */
1153 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1154 }
1155
1156 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1157 MBEDTLS_ASN1_OID)) != 0) {
1158 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1159 }
1160
1161 cur_oid.tag = MBEDTLS_ASN1_OID;
1162 cur_oid.p = p;
1163 cur_oid.len = len;
1164
1165 /*
1166 * Only HwModuleName is currently supported.
1167 */
1168 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ON_HW_MODULE_NAME, &cur_oid) != 0) {
1169 return MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
1170 }
David Horstmann2ea44d22023-08-18 18:36:02 +01001171 other_name->type_id = cur_oid;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001172
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001173 p += len;
1174 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1175 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC)) !=
1176 0) {
1177 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1178 }
1179
Hanno Beckerdae916b2019-09-13 14:21:13 +01001180 if (end != p + len) {
1181 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1182 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1183 }
1184
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001185 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1186 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1187 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1188 }
1189
Hanno Beckerdae916b2019-09-13 14:21:13 +01001190 if (end != p + len) {
1191 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1192 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1193 }
1194
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001195 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OID)) != 0) {
1196 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1197 }
1198
1199 other_name->value.hardware_module_name.oid.tag = MBEDTLS_ASN1_OID;
1200 other_name->value.hardware_module_name.oid.p = p;
1201 other_name->value.hardware_module_name.oid.len = len;
1202
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001203 p += len;
1204 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1205 MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1206 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1207 }
1208
1209 other_name->value.hardware_module_name.val.tag = MBEDTLS_ASN1_OCTET_STRING;
1210 other_name->value.hardware_module_name.val.p = p;
1211 other_name->value.hardware_module_name.val.len = len;
1212 p += len;
1213 if (p != end) {
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001214 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1215 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1216 }
1217 return 0;
1218}
1219
Przemek Stekiel21903ec2023-02-21 08:32:37 +01001220/* Check mbedtls_x509_get_subject_alt_name for detailed description.
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001221 *
Przemek Stekiel21903ec2023-02-21 08:32:37 +01001222 * In some cases while parsing subject alternative names the sequence tag is optional
1223 * (e.g. CertSerialNumber). This function is designed to handle such case.
Przemek Stekiel725688b2023-04-04 22:49:44 +02001224 */
Przemek Stekiel21903ec2023-02-21 08:32:37 +01001225int mbedtls_x509_get_subject_alt_name_ext(unsigned char **p,
1226 const unsigned char *end,
1227 mbedtls_x509_sequence *subject_alt_name)
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001228{
1229 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel21903ec2023-02-21 08:32:37 +01001230 size_t tag_len;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001231 mbedtls_asn1_sequence *cur = subject_alt_name;
1232
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001233 while (*p < end) {
Andrzej Kurek154a6052023-04-30 14:11:49 -04001234 mbedtls_x509_subject_alternative_name tmp_san_name;
Hanno Beckerae8f8c42019-09-13 12:24:56 +01001235 mbedtls_x509_buf tmp_san_buf;
Andrzej Kurek154a6052023-04-30 14:11:49 -04001236 memset(&tmp_san_name, 0, sizeof(tmp_san_name));
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001237
Hanno Beckerae8f8c42019-09-13 12:24:56 +01001238 tmp_san_buf.tag = **p;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001239 (*p)++;
Hanno Beckerae8f8c42019-09-13 12:24:56 +01001240
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001241 if ((ret = mbedtls_asn1_get_len(p, end, &tag_len)) != 0) {
1242 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1243 }
1244
Hanno Beckerae8f8c42019-09-13 12:24:56 +01001245 tmp_san_buf.p = *p;
1246 tmp_san_buf.len = tag_len;
1247
1248 if ((tmp_san_buf.tag & MBEDTLS_ASN1_TAG_CLASS_MASK) !=
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001249 MBEDTLS_ASN1_CONTEXT_SPECIFIC) {
1250 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1251 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
1252 }
1253
1254 /*
Andrzej Kurek154a6052023-04-30 14:11:49 -04001255 * Check that the SAN is structured correctly by parsing it.
1256 * The SAN structure is discarded afterwards.
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001257 */
Andrzej Kurek154a6052023-04-30 14:11:49 -04001258 ret = mbedtls_x509_parse_subject_alt_name(&tmp_san_buf, &tmp_san_name);
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001259 /*
1260 * In case the extension is malformed, return an error,
1261 * and clear the allocated sequences.
1262 */
1263 if (ret != 0 && ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE) {
1264 mbedtls_asn1_sequence_free(subject_alt_name->next);
1265 subject_alt_name->next = NULL;
1266 return ret;
1267 }
1268
Andrzej Kurek154a6052023-04-30 14:11:49 -04001269 mbedtls_x509_free_subject_alt_name(&tmp_san_name);
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001270 /* Allocate and assign next pointer */
1271 if (cur->buf.p != NULL) {
1272 if (cur->next != NULL) {
1273 return MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
1274 }
1275
1276 cur->next = mbedtls_calloc(1, sizeof(mbedtls_asn1_sequence));
1277
1278 if (cur->next == NULL) {
1279 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1280 MBEDTLS_ERR_ASN1_ALLOC_FAILED);
1281 }
1282
1283 cur = cur->next;
1284 }
1285
Hanno Beckerae8f8c42019-09-13 12:24:56 +01001286 cur->buf = tmp_san_buf;
1287 *p += tmp_san_buf.len;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001288 }
1289
1290 /* Set final sequence entry's next pointer to NULL */
1291 cur->next = NULL;
1292
1293 if (*p != end) {
1294 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1295 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1296 }
1297
1298 return 0;
1299}
1300
Przemek Stekiel21903ec2023-02-21 08:32:37 +01001301/*
1302 * SubjectAltName ::= GeneralNames
1303 *
1304 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
1305 *
1306 * GeneralName ::= CHOICE {
1307 * otherName [0] OtherName,
1308 * rfc822Name [1] IA5String,
1309 * dNSName [2] IA5String,
1310 * x400Address [3] ORAddress,
1311 * directoryName [4] Name,
1312 * ediPartyName [5] EDIPartyName,
1313 * uniformResourceIdentifier [6] IA5String,
1314 * iPAddress [7] OCTET STRING,
1315 * registeredID [8] OBJECT IDENTIFIER }
1316 *
1317 * OtherName ::= SEQUENCE {
1318 * type-id OBJECT IDENTIFIER,
1319 * value [0] EXPLICIT ANY DEFINED BY type-id }
1320 *
1321 * EDIPartyName ::= SEQUENCE {
1322 * nameAssigner [0] DirectoryString OPTIONAL,
1323 * partyName [1] DirectoryString }
1324 *
1325 * We list all types, but use the following GeneralName types from RFC 5280:
1326 * "dnsName", "uniformResourceIdentifier" and "hardware_module_name"
1327 * of type "otherName", as defined in RFC 4108.
1328 */
1329int mbedtls_x509_get_subject_alt_name(unsigned char **p,
1330 const unsigned char *end,
1331 mbedtls_x509_sequence *subject_alt_name)
1332{
1333 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1334 size_t len;
1335
1336 /* Get main sequence tag */
1337 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
1338 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1339 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1340 }
1341
1342 if (*p + len != end) {
1343 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1344 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1345 }
1346
1347 return mbedtls_x509_get_subject_alt_name_ext(p, end, subject_alt_name);
1348}
1349
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001350int mbedtls_x509_get_ns_cert_type(unsigned char **p,
1351 const unsigned char *end,
1352 unsigned char *ns_cert_type)
1353{
1354 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1355 mbedtls_x509_bitstring bs = { 0, 0, NULL };
1356
1357 if ((ret = mbedtls_asn1_get_bitstring(p, end, &bs)) != 0) {
1358 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1359 }
1360
Przemek Stekiel32e20912023-01-25 14:26:15 +01001361 /* A bitstring with no flags set is still technically valid, as it will mean
1362 that the certificate has no designated purpose at the time of creation. */
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001363 if (bs.len == 0) {
1364 *ns_cert_type = 0;
1365 return 0;
1366 }
1367
1368 if (bs.len != 1) {
1369 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1370 MBEDTLS_ERR_ASN1_INVALID_LENGTH);
1371 }
1372
1373 /* Get actual bitstring */
1374 *ns_cert_type = *bs.p;
1375 return 0;
1376}
1377
1378int mbedtls_x509_get_key_usage(unsigned char **p,
1379 const unsigned char *end,
1380 unsigned int *key_usage)
1381{
1382 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1383 size_t i;
1384 mbedtls_x509_bitstring bs = { 0, 0, NULL };
1385
1386 if ((ret = mbedtls_asn1_get_bitstring(p, end, &bs)) != 0) {
1387 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1388 }
1389
Przemek Stekiel32e20912023-01-25 14:26:15 +01001390 /* A bitstring with no flags set is still technically valid, as it will mean
1391 that the certificate has no designated purpose at the time of creation. */
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001392 if (bs.len == 0) {
1393 *key_usage = 0;
1394 return 0;
1395 }
1396
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001397 /* Get actual bitstring */
1398 *key_usage = 0;
1399 for (i = 0; i < bs.len && i < sizeof(unsigned int); i++) {
1400 *key_usage |= (unsigned int) bs.p[i] << (8*i);
1401 }
1402
1403 return 0;
1404}
1405
1406int mbedtls_x509_parse_subject_alt_name(const mbedtls_x509_buf *san_buf,
1407 mbedtls_x509_subject_alternative_name *san)
1408{
1409 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1410 switch (san_buf->tag &
1411 (MBEDTLS_ASN1_TAG_CLASS_MASK |
1412 MBEDTLS_ASN1_TAG_VALUE_MASK)) {
1413 /*
1414 * otherName
1415 */
1416 case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME):
1417 {
1418 mbedtls_x509_san_other_name other_name;
1419
1420 ret = x509_get_other_name(san_buf, &other_name);
1421 if (ret != 0) {
1422 return ret;
1423 }
1424
1425 memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name));
1426 san->type = MBEDTLS_X509_SAN_OTHER_NAME;
1427 memcpy(&san->san.other_name,
1428 &other_name, sizeof(other_name));
1429
1430 }
1431 break;
Andrzej Kurek7a05fab2023-02-13 10:03:07 -05001432 /*
1433 * uniformResourceIdentifier
1434 */
1435 case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER):
1436 {
1437 memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name));
1438 san->type = MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001439
Andrzej Kurek7a05fab2023-02-13 10:03:07 -05001440 memcpy(&san->san.unstructured_name,
1441 san_buf, sizeof(*san_buf));
1442
1443 }
1444 break;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001445 /*
1446 * dNSName
1447 */
1448 case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_DNS_NAME):
1449 {
1450 memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name));
1451 san->type = MBEDTLS_X509_SAN_DNS_NAME;
1452
1453 memcpy(&san->san.unstructured_name,
1454 san_buf, sizeof(*san_buf));
Przemek Stekielecee12f2023-02-09 14:43:49 +01001455 }
1456 break;
Przemek Stekiel0ab5b932023-05-29 16:30:50 +02001457 /*
1458 * IP address
1459 */
1460 case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_IP_ADDRESS):
1461 {
1462 memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name));
1463 san->type = MBEDTLS_X509_SAN_IP_ADDRESS;
Przemek Stekiel01cb6eb2023-06-05 16:38:13 +02001464 // Only IPv6 (16 bytes) and IPv4 (4 bytes) types are supported
1465 if (san_buf->len == 4 || san_buf->len == 16) {
1466 memcpy(&san->san.unstructured_name,
1467 san_buf, sizeof(*san_buf));
1468 } else {
1469 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1470 }
Przemek Stekiel0ab5b932023-05-29 16:30:50 +02001471 }
1472 break;
Przemek Stekielecee12f2023-02-09 14:43:49 +01001473 /*
Andrzej Kurek154a6052023-04-30 14:11:49 -04001474 * rfc822Name
Przemek Stekielecee12f2023-02-09 14:43:49 +01001475 */
1476 case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_RFC822_NAME):
1477 {
1478 memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name));
1479 san->type = MBEDTLS_X509_SAN_RFC822_NAME;
1480 memcpy(&san->san.unstructured_name, san_buf, sizeof(*san_buf));
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001481 }
1482 break;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001483 /*
Andrzej Kureke12b01d2023-01-10 06:47:38 -05001484 * directoryName
1485 */
1486 case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_DIRECTORY_NAME):
1487 {
1488 size_t name_len;
1489 unsigned char *p = san_buf->p;
1490 memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name));
1491 san->type = MBEDTLS_X509_SAN_DIRECTORY_NAME;
1492
1493 ret = mbedtls_asn1_get_tag(&p, p + san_buf->len, &name_len,
1494 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE);
1495
1496 if (ret != 0) {
Andrzej Kurekbf8ccd82023-02-13 07:13:30 -05001497 return ret;
Andrzej Kureke12b01d2023-01-10 06:47:38 -05001498 }
1499
1500 if ((ret = mbedtls_x509_get_name(&p, p + name_len,
1501 &san->san.directory_name)) != 0) {
Andrzej Kurekbf8ccd82023-02-13 07:13:30 -05001502 return ret;
Andrzej Kureke12b01d2023-01-10 06:47:38 -05001503 }
1504 }
1505 break;
1506 /*
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001507 * Type not supported
1508 */
1509 default:
1510 return MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
1511 }
1512 return 0;
1513}
1514
Andrzej Kurekd40c2b62023-02-13 07:01:59 -05001515void mbedtls_x509_free_subject_alt_name(mbedtls_x509_subject_alternative_name *san)
1516{
1517 if (san->type == MBEDTLS_X509_SAN_DIRECTORY_NAME) {
1518 mbedtls_asn1_free_named_data_list_shallow(san->san.directory_name.next);
1519 }
1520}
1521
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001522#if !defined(MBEDTLS_X509_REMOVE_INFO)
1523int mbedtls_x509_info_subject_alt_name(char **buf, size_t *size,
1524 const mbedtls_x509_sequence
1525 *subject_alt_name,
1526 const char *prefix)
1527{
1528 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1529 size_t i;
1530 size_t n = *size;
1531 char *p = *buf;
1532 const mbedtls_x509_sequence *cur = subject_alt_name;
1533 mbedtls_x509_subject_alternative_name san;
1534 int parse_ret;
1535
1536 while (cur != NULL) {
1537 memset(&san, 0, sizeof(san));
1538 parse_ret = mbedtls_x509_parse_subject_alt_name(&cur->buf, &san);
1539 if (parse_ret != 0) {
1540 if (parse_ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE) {
1541 ret = mbedtls_snprintf(p, n, "\n%s <unsupported>", prefix);
1542 MBEDTLS_X509_SAFE_SNPRINTF;
1543 } else {
1544 ret = mbedtls_snprintf(p, n, "\n%s <malformed>", prefix);
1545 MBEDTLS_X509_SAFE_SNPRINTF;
1546 }
1547 cur = cur->next;
1548 continue;
1549 }
1550
1551 switch (san.type) {
1552 /*
1553 * otherName
1554 */
1555 case MBEDTLS_X509_SAN_OTHER_NAME:
1556 {
1557 mbedtls_x509_san_other_name *other_name = &san.san.other_name;
1558
1559 ret = mbedtls_snprintf(p, n, "\n%s otherName :", prefix);
1560 MBEDTLS_X509_SAFE_SNPRINTF;
1561
1562 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ON_HW_MODULE_NAME,
David Horstmanncfae6a12023-08-18 19:12:59 +01001563 &other_name->type_id) == 0) {
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001564 ret = mbedtls_snprintf(p, n, "\n%s hardware module name :", prefix);
1565 MBEDTLS_X509_SAFE_SNPRINTF;
1566 ret =
1567 mbedtls_snprintf(p, n, "\n%s hardware type : ", prefix);
1568 MBEDTLS_X509_SAFE_SNPRINTF;
1569
1570 ret = mbedtls_oid_get_numeric_string(p,
1571 n,
1572 &other_name->value.hardware_module_name.oid);
1573 MBEDTLS_X509_SAFE_SNPRINTF;
1574
1575 ret =
1576 mbedtls_snprintf(p, n, "\n%s hardware serial number : ", prefix);
1577 MBEDTLS_X509_SAFE_SNPRINTF;
1578
1579 for (i = 0; i < other_name->value.hardware_module_name.val.len; i++) {
1580 ret = mbedtls_snprintf(p,
1581 n,
1582 "%02X",
1583 other_name->value.hardware_module_name.val.p[i]);
1584 MBEDTLS_X509_SAFE_SNPRINTF;
1585 }
1586 }/* MBEDTLS_OID_ON_HW_MODULE_NAME */
1587 }
1588 break;
Andrzej Kurek7a05fab2023-02-13 10:03:07 -05001589 /*
1590 * uniformResourceIdentifier
1591 */
1592 case MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER:
1593 {
1594 ret = mbedtls_snprintf(p, n, "\n%s uniformResourceIdentifier : ", prefix);
1595 MBEDTLS_X509_SAFE_SNPRINTF;
1596 if (san.san.unstructured_name.len >= n) {
Przemek Stekiel01cb6eb2023-06-05 16:38:13 +02001597 if (n > 0) {
1598 *p = '\0';
1599 }
Andrzej Kurek7a05fab2023-02-13 10:03:07 -05001600 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1601 }
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001602
Andrzej Kurek7a05fab2023-02-13 10:03:07 -05001603 memcpy(p, san.san.unstructured_name.p, san.san.unstructured_name.len);
1604 p += san.san.unstructured_name.len;
1605 n -= san.san.unstructured_name.len;
1606 }
1607 break;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001608 /*
1609 * dNSName
Przemek Stekiel5b9e4162023-02-15 12:56:37 +01001610 * RFC822 Name
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001611 */
1612 case MBEDTLS_X509_SAN_DNS_NAME:
Przemek Stekiel5b9e4162023-02-15 12:56:37 +01001613 case MBEDTLS_X509_SAN_RFC822_NAME:
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001614 {
Przemek Stekiel82d250d2023-02-15 15:00:50 +01001615 const char *dns_name = "dNSName";
1616 const char *rfc822_name = "rfc822Name";
Przemek Stekiel5b9e4162023-02-15 12:56:37 +01001617
Przemek Stekiel82d250d2023-02-15 15:00:50 +01001618 ret = mbedtls_snprintf(p, n,
1619 "\n%s %s : ",
1620 prefix,
Przemek Stekiel093c97d2023-06-02 10:11:32 +02001621 san.type ==
1622 MBEDTLS_X509_SAN_DNS_NAME ? dns_name : rfc822_name);
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001623 MBEDTLS_X509_SAFE_SNPRINTF;
1624 if (san.san.unstructured_name.len >= n) {
Przemek Stekiel01cb6eb2023-06-05 16:38:13 +02001625 if (n > 0) {
1626 *p = '\0';
1627 }
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001628 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1629 }
1630
Przemek Stekiel093c97d2023-06-02 10:11:32 +02001631 memcpy(p, san.san.unstructured_name.p, san.san.unstructured_name.len);
1632 p += san.san.unstructured_name.len;
1633 n -= san.san.unstructured_name.len;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001634 }
1635 break;
Przemek Stekiel093c97d2023-06-02 10:11:32 +02001636 /*
1637 * iPAddress
1638 */
1639 case MBEDTLS_X509_SAN_IP_ADDRESS:
1640 {
1641 ret = mbedtls_snprintf(p, n, "\n%s %s : ",
1642 prefix, "iPAddress");
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 }
Przemek Stekiel093c97d2023-06-02 10:11:32 +02001648 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1649 }
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001650
Przemek Stekiel093c97d2023-06-02 10:11:32 +02001651 unsigned char *ip = san.san.unstructured_name.p;
1652 // Only IPv6 (16 bytes) and IPv4 (4 bytes) types are supported
1653 if (san.san.unstructured_name.len == 4) {
Przemek Stekiel4d3fc212023-06-06 11:40:32 +02001654 ret = mbedtls_snprintf(p, n, "%u.%u.%u.%u", ip[0], ip[1], ip[2], ip[3]);
1655 MBEDTLS_X509_SAFE_SNPRINTF;
Przemek Stekiel093c97d2023-06-02 10:11:32 +02001656 } else if (san.san.unstructured_name.len == 16) {
Przemek Stekiel4d3fc212023-06-06 11:40:32 +02001657 ret = mbedtls_snprintf(p, n,
Przemek Stekiel01cb6eb2023-06-05 16:38:13 +02001658 "%X%X:%X%X:%X%X:%X%X:%X%X:%X%X:%X%X:%X%X",
1659 ip[0], ip[1], ip[2], ip[3], ip[4], ip[5], ip[6],
Przemek Stekiel4d3fc212023-06-06 11:40:32 +02001660 ip[7], ip[8], ip[9], ip[10], ip[11], ip[12], ip[13],
Przemek Stekiel01cb6eb2023-06-05 16:38:13 +02001661 ip[14], ip[15]);
Przemek Stekiel4d3fc212023-06-06 11:40:32 +02001662 MBEDTLS_X509_SAFE_SNPRINTF;
Przemek Stekiel093c97d2023-06-02 10:11:32 +02001663 } else {
Przemek Stekiel01cb6eb2023-06-05 16:38:13 +02001664 if (n > 0) {
1665 *p = '\0';
1666 }
Przemek Stekiel093c97d2023-06-02 10:11:32 +02001667 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1668 }
Przemek Stekiel093c97d2023-06-02 10:11:32 +02001669 }
1670 break;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001671 /*
Andrzej Kureke12b01d2023-01-10 06:47:38 -05001672 * directoryName
1673 */
1674 case MBEDTLS_X509_SAN_DIRECTORY_NAME:
1675 {
1676 ret = mbedtls_snprintf(p, n, "\n%s directoryName : ", prefix);
Andrzej Kurek5f0c6e82023-02-27 16:03:41 -05001677 if (ret < 0 || (size_t) ret >= n) {
1678 mbedtls_x509_free_subject_alt_name(&san);
1679 }
1680
Andrzej Kureke12b01d2023-01-10 06:47:38 -05001681 MBEDTLS_X509_SAFE_SNPRINTF;
1682 ret = mbedtls_x509_dn_gets(p, n, &san.san.directory_name);
Andrzej Kurekd40c2b62023-02-13 07:01:59 -05001683
Andrzej Kureke12b01d2023-01-10 06:47:38 -05001684 if (ret < 0) {
Andrzej Kurek5f0c6e82023-02-27 16:03:41 -05001685 mbedtls_x509_free_subject_alt_name(&san);
Przemek Stekiel01cb6eb2023-06-05 16:38:13 +02001686 if (n > 0) {
1687 *p = '\0';
1688 }
Andrzej Kureke12b01d2023-01-10 06:47:38 -05001689 return ret;
1690 }
1691
1692 p += ret;
1693 n -= ret;
1694 }
1695 break;
1696 /*
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001697 * Type not supported, skip item.
1698 */
1699 default:
1700 ret = mbedtls_snprintf(p, n, "\n%s <unsupported>", prefix);
1701 MBEDTLS_X509_SAFE_SNPRINTF;
1702 break;
1703 }
1704
Andrzej Kurekd40c2b62023-02-13 07:01:59 -05001705 /* So far memory is freed only in the case of directoryName
Andrzej Kurek5f0c6e82023-02-27 16:03:41 -05001706 * parsing succeeding, as mbedtls_x509_get_name allocates memory. */
Andrzej Kurekd40c2b62023-02-13 07:01:59 -05001707 mbedtls_x509_free_subject_alt_name(&san);
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001708 cur = cur->next;
1709 }
1710
1711 *p = '\0';
1712
1713 *size = n;
1714 *buf = p;
1715
1716 return 0;
1717}
1718
Demi Marie Obenour690b8c92022-12-04 04:24:22 -05001719#define PRINT_ITEM(i) \
1720 do { \
1721 ret = mbedtls_snprintf(p, n, "%s" i, sep); \
1722 MBEDTLS_X509_SAFE_SNPRINTF; \
1723 sep = ", "; \
1724 } while (0)
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001725
Demi Marie Obenour690b8c92022-12-04 04:24:22 -05001726#define CERT_TYPE(type, name) \
1727 do { \
1728 if (ns_cert_type & (type)) { \
1729 PRINT_ITEM(name); \
1730 } \
1731 } while (0)
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001732
1733int mbedtls_x509_info_cert_type(char **buf, size_t *size,
1734 unsigned char ns_cert_type)
1735{
1736 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1737 size_t n = *size;
1738 char *p = *buf;
1739 const char *sep = "";
1740
1741 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT, "SSL Client");
1742 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER, "SSL Server");
1743 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_EMAIL, "Email");
1744 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing");
1745 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_RESERVED, "Reserved");
1746 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_SSL_CA, "SSL CA");
1747 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA, "Email CA");
1748 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA");
1749
1750 *size = n;
1751 *buf = p;
1752
1753 return 0;
1754}
1755
Demi Marie Obenour690b8c92022-12-04 04:24:22 -05001756#define KEY_USAGE(code, name) \
1757 do { \
1758 if ((key_usage) & (code)) { \
1759 PRINT_ITEM(name); \
1760 } \
1761 } while (0)
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001762
1763int mbedtls_x509_info_key_usage(char **buf, size_t *size,
1764 unsigned int key_usage)
1765{
1766 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1767 size_t n = *size;
1768 char *p = *buf;
1769 const char *sep = "";
1770
1771 KEY_USAGE(MBEDTLS_X509_KU_DIGITAL_SIGNATURE, "Digital Signature");
1772 KEY_USAGE(MBEDTLS_X509_KU_NON_REPUDIATION, "Non Repudiation");
1773 KEY_USAGE(MBEDTLS_X509_KU_KEY_ENCIPHERMENT, "Key Encipherment");
1774 KEY_USAGE(MBEDTLS_X509_KU_DATA_ENCIPHERMENT, "Data Encipherment");
1775 KEY_USAGE(MBEDTLS_X509_KU_KEY_AGREEMENT, "Key Agreement");
1776 KEY_USAGE(MBEDTLS_X509_KU_KEY_CERT_SIGN, "Key Cert Sign");
1777 KEY_USAGE(MBEDTLS_X509_KU_CRL_SIGN, "CRL Sign");
1778 KEY_USAGE(MBEDTLS_X509_KU_ENCIPHER_ONLY, "Encipher Only");
1779 KEY_USAGE(MBEDTLS_X509_KU_DECIPHER_ONLY, "Decipher Only");
1780
1781 *size = n;
1782 *buf = p;
1783
1784 return 0;
1785}
1786#endif /* MBEDTLS_X509_REMOVE_INFO */
1787#endif /* MBEDTLS_X509_CRT_PARSE_C || MBEDTLS_X509_CSR_PARSE_C */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001788#endif /* MBEDTLS_X509_USE_C */