blob: 42839e8f80ab87127793e7caab471f30e1b2ee0a [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#if defined(MBEDTLS_ASN1_WRITE_C)
47#include "mbedtls/asn1write.h"
48#endif
49
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000050#include "mbedtls/platform.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020051
Simon Butcherb5b6af22016-07-13 14:46:18 +010052#if defined(MBEDTLS_HAVE_TIME)
53#include "mbedtls/platform_time.h"
54#endif
Nicholas Wilson512b4ee2017-12-05 12:07:33 +000055#if defined(MBEDTLS_HAVE_TIME_DATE)
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +010056#include "mbedtls/platform_util.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020057#include <time.h>
58#endif
59
Demi Marie Obenour690b8c92022-12-04 04:24:22 -050060#define CHECK(code) \
61 do { \
62 if ((ret = (code)) != 0) { \
63 return ret; \
64 } \
65 } while (0)
66
Hanno Becker1eeca412018-10-15 12:01:35 +010067#define CHECK_RANGE(min, max, val) \
Demi Marie Obenour690b8c92022-12-04 04:24:22 -050068 do { \
69 if ((val) < (min) || (val) > (max)) { \
70 return ret; \
Hanno Becker1eeca412018-10-15 12:01:35 +010071 } \
Gilles Peskine449bd832023-01-11 14:50:10 +010072 } while (0)
Rich Evans7d5a55a2015-02-13 11:48:02 +000073
Paul Bakker7c6b2c32013-09-16 13:49:26 +020074/*
75 * CertificateSerialNumber ::= INTEGER
76 */
Gilles Peskine449bd832023-01-11 14:50:10 +010077int mbedtls_x509_get_serial(unsigned char **p, const unsigned char *end,
78 mbedtls_x509_buf *serial)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020079{
Janos Follath865b3eb2019-12-16 11:46:15 +000080 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020081
Gilles Peskine449bd832023-01-11 14:50:10 +010082 if ((end - *p) < 1) {
83 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_SERIAL,
84 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
85 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +020086
Gilles Peskine449bd832023-01-11 14:50:10 +010087 if (**p != (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_PRIMITIVE | 2) &&
88 **p != MBEDTLS_ASN1_INTEGER) {
89 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_SERIAL,
90 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
91 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +020092
93 serial->tag = *(*p)++;
94
Gilles Peskine449bd832023-01-11 14:50:10 +010095 if ((ret = mbedtls_asn1_get_len(p, end, &serial->len)) != 0) {
96 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_SERIAL, ret);
97 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +020098
99 serial->p = *p;
100 *p += serial->len;
101
Gilles Peskine449bd832023-01-11 14:50:10 +0100102 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200103}
104
105/* Get an algorithm identifier without parameters (eg for signatures)
106 *
107 * AlgorithmIdentifier ::= SEQUENCE {
108 * algorithm OBJECT IDENTIFIER,
109 * parameters ANY DEFINED BY algorithm OPTIONAL }
110 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100111int mbedtls_x509_get_alg_null(unsigned char **p, const unsigned char *end,
112 mbedtls_x509_buf *alg)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200113{
Janos Follath865b3eb2019-12-16 11:46:15 +0000114 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200115
Gilles Peskine449bd832023-01-11 14:50:10 +0100116 if ((ret = mbedtls_asn1_get_alg_null(p, end, alg)) != 0) {
117 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
118 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200119
Gilles Peskine449bd832023-01-11 14:50:10 +0100120 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200121}
122
123/*
Antonin Décimo36e89b52019-01-23 15:24:37 +0100124 * Parse an algorithm identifier with (optional) parameters
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100125 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100126int mbedtls_x509_get_alg(unsigned char **p, const unsigned char *end,
127 mbedtls_x509_buf *alg, mbedtls_x509_buf *params)
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100128{
Janos Follath865b3eb2019-12-16 11:46:15 +0000129 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100130
Gilles Peskine449bd832023-01-11 14:50:10 +0100131 if ((ret = mbedtls_asn1_get_alg(p, end, alg, params)) != 0) {
132 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
133 }
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100134
Gilles Peskine449bd832023-01-11 14:50:10 +0100135 return 0;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100136}
137
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200138/*
139 * Convert md type to string
140 */
Dave Rodgmanffabb7b2023-06-28 16:22:50 +0100141#if !defined(MBEDTLS_X509_REMOVE_INFO) && defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
142
Gilles Peskine449bd832023-01-11 14:50:10 +0100143static inline const char *md_type_to_string(mbedtls_md_type_t md_alg)
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200144{
Gilles Peskine449bd832023-01-11 14:50:10 +0100145 switch (md_alg) {
Manuel Pégourié-Gonnard49e67f82023-03-16 11:39:20 +0100146#if defined(MBEDTLS_MD_CAN_MD5)
Gilles Peskine449bd832023-01-11 14:50:10 +0100147 case MBEDTLS_MD_MD5:
148 return "MD5";
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200149#endif
Manuel Pégourié-Gonnard49e67f82023-03-16 11:39:20 +0100150#if defined(MBEDTLS_MD_CAN_SHA1)
Gilles Peskine449bd832023-01-11 14:50:10 +0100151 case MBEDTLS_MD_SHA1:
152 return "SHA1";
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200153#endif
Manuel Pégourié-Gonnard49e67f82023-03-16 11:39:20 +0100154#if defined(MBEDTLS_MD_CAN_SHA224)
Gilles Peskine449bd832023-01-11 14:50:10 +0100155 case MBEDTLS_MD_SHA224:
156 return "SHA224";
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200157#endif
Manuel Pégourié-Gonnard49e67f82023-03-16 11:39:20 +0100158#if defined(MBEDTLS_MD_CAN_SHA256)
Gilles Peskine449bd832023-01-11 14:50:10 +0100159 case MBEDTLS_MD_SHA256:
160 return "SHA256";
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200161#endif
Manuel Pégourié-Gonnard49e67f82023-03-16 11:39:20 +0100162#if defined(MBEDTLS_MD_CAN_SHA384)
Gilles Peskine449bd832023-01-11 14:50:10 +0100163 case MBEDTLS_MD_SHA384:
164 return "SHA384";
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200165#endif
Manuel Pégourié-Gonnard49e67f82023-03-16 11:39:20 +0100166#if defined(MBEDTLS_MD_CAN_SHA512)
Gilles Peskine449bd832023-01-11 14:50:10 +0100167 case MBEDTLS_MD_SHA512:
168 return "SHA512";
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200169#endif
Manuel Pégourié-Gonnard49e67f82023-03-16 11:39:20 +0100170#if defined(MBEDTLS_MD_CAN_RIPEMD160)
Gilles Peskine449bd832023-01-11 14:50:10 +0100171 case MBEDTLS_MD_RIPEMD160:
172 return "RIPEMD160";
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200173#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100174 case MBEDTLS_MD_NONE:
175 return NULL;
176 default:
177 return NULL;
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200178 }
179}
180
Dave Rodgmanf032c982023-06-29 12:09:27 +0100181#endif /* !defined(MBEDTLS_X509_REMOVE_INFO) && defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) */
Dave Rodgmanffabb7b2023-06-28 16:22:50 +0100182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100184/*
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100185 * HashAlgorithm ::= AlgorithmIdentifier
186 *
187 * AlgorithmIdentifier ::= SEQUENCE {
188 * algorithm OBJECT IDENTIFIER,
189 * parameters ANY DEFINED BY algorithm OPTIONAL }
190 *
191 * For HashAlgorithm, parameters MUST be NULL or absent.
192 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100193static 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 +0100194{
Janos Follath865b3eb2019-12-16 11:46:15 +0000195 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100196 unsigned char *p;
197 const unsigned char *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200198 mbedtls_x509_buf md_oid;
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100199 size_t len;
200
201 /* Make sure we got a SEQUENCE and setup bounds */
Gilles Peskine449bd832023-01-11 14:50:10 +0100202 if (alg->tag != (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) {
203 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
204 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
205 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100206
Andrzej Kurekfeaebc52020-07-16 04:37:41 -0400207 p = alg->p;
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100208 end = p + alg->len;
209
Gilles Peskine449bd832023-01-11 14:50:10 +0100210 if (p >= end) {
211 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
212 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
213 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100214
215 /* Parse md_oid */
216 md_oid.tag = *p;
217
Gilles Peskine449bd832023-01-11 14:50:10 +0100218 if ((ret = mbedtls_asn1_get_tag(&p, end, &md_oid.len, MBEDTLS_ASN1_OID)) != 0) {
219 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
220 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100221
222 md_oid.p = p;
223 p += md_oid.len;
224
225 /* Get md_alg from md_oid */
Gilles Peskine449bd832023-01-11 14:50:10 +0100226 if ((ret = mbedtls_oid_get_md_alg(&md_oid, md_alg)) != 0) {
227 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
228 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100229
230 /* Make sure params is absent of NULL */
Gilles Peskine449bd832023-01-11 14:50:10 +0100231 if (p == end) {
232 return 0;
233 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100234
Gilles Peskine449bd832023-01-11 14:50:10 +0100235 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_NULL)) != 0 || len != 0) {
236 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
237 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100238
Gilles Peskine449bd832023-01-11 14:50:10 +0100239 if (p != end) {
240 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
241 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
242 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100243
Gilles Peskine449bd832023-01-11 14:50:10 +0100244 return 0;
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100245}
246
247/*
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100248 * RSASSA-PSS-params ::= SEQUENCE {
249 * hashAlgorithm [0] HashAlgorithm DEFAULT sha1Identifier,
250 * maskGenAlgorithm [1] MaskGenAlgorithm DEFAULT mgf1SHA1Identifier,
251 * saltLength [2] INTEGER DEFAULT 20,
252 * trailerField [3] INTEGER DEFAULT 1 }
253 * -- Note that the tags in this Sequence are explicit.
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200254 *
255 * RFC 4055 (which defines use of RSASSA-PSS in PKIX) states that the value
256 * of trailerField MUST be 1, and PKCS#1 v2.2 doesn't even define any other
Tom Cosgrove1797b052022-12-04 17:19:59 +0000257 * option. Enforce this at parsing time.
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100258 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100259int mbedtls_x509_get_rsassa_pss_params(const mbedtls_x509_buf *params,
260 mbedtls_md_type_t *md_alg, mbedtls_md_type_t *mgf_md,
261 int *salt_len)
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100262{
Janos Follath865b3eb2019-12-16 11:46:15 +0000263 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100264 unsigned char *p;
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100265 const unsigned char *end, *end2;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100266 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267 mbedtls_x509_buf alg_id, alg_params;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100268
269 /* First set everything to defaults */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200270 *md_alg = MBEDTLS_MD_SHA1;
271 *mgf_md = MBEDTLS_MD_SHA1;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100272 *salt_len = 20;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100273
274 /* Make sure params is a SEQUENCE and setup bounds */
Gilles Peskine449bd832023-01-11 14:50:10 +0100275 if (params->tag != (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) {
276 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
277 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
278 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100279
280 p = (unsigned char *) params->p;
281 end = p + params->len;
282
Gilles Peskine449bd832023-01-11 14:50:10 +0100283 if (p == end) {
284 return 0;
285 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100286
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100287 /*
288 * HashAlgorithm
289 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100290 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
291 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
292 0)) == 0) {
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100293 end2 = p + len;
294
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100295 /* HashAlgorithm ::= AlgorithmIdentifier (without parameters) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100296 if ((ret = mbedtls_x509_get_alg_null(&p, end2, &alg_id)) != 0) {
297 return ret;
298 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100299
Gilles Peskine449bd832023-01-11 14:50:10 +0100300 if ((ret = mbedtls_oid_get_md_alg(&alg_id, md_alg)) != 0) {
301 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
302 }
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100303
Gilles Peskine449bd832023-01-11 14:50:10 +0100304 if (p != end2) {
305 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
306 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
307 }
308 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
309 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100310 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100311
Gilles Peskine449bd832023-01-11 14:50:10 +0100312 if (p == end) {
313 return 0;
314 }
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100315
316 /*
317 * MaskGenAlgorithm
318 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100319 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
320 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
321 1)) == 0) {
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100322 end2 = p + len;
323
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100324 /* MaskGenAlgorithm ::= AlgorithmIdentifier (params = HashAlgorithm) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100325 if ((ret = mbedtls_x509_get_alg(&p, end2, &alg_id, &alg_params)) != 0) {
326 return ret;
327 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100328
329 /* Only MFG1 is recognised for now */
Gilles Peskine449bd832023-01-11 14:50:10 +0100330 if (MBEDTLS_OID_CMP(MBEDTLS_OID_MGF1, &alg_id) != 0) {
331 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE,
332 MBEDTLS_ERR_OID_NOT_FOUND);
333 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100334
335 /* Parse HashAlgorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +0100336 if ((ret = x509_get_hash_alg(&alg_params, mgf_md)) != 0) {
337 return ret;
338 }
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100339
Gilles Peskine449bd832023-01-11 14:50:10 +0100340 if (p != end2) {
341 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
342 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
343 }
344 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
345 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100346 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100347
Gilles Peskine449bd832023-01-11 14:50:10 +0100348 if (p == end) {
349 return 0;
350 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100351
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100352 /*
353 * salt_len
354 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100355 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
356 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
357 2)) == 0) {
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100358 end2 = p + len;
359
Gilles Peskine449bd832023-01-11 14:50:10 +0100360 if ((ret = mbedtls_asn1_get_int(&p, end2, salt_len)) != 0) {
361 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
362 }
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100363
Gilles Peskine449bd832023-01-11 14:50:10 +0100364 if (p != end2) {
365 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
366 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
367 }
368 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
369 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100370 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100371
Gilles Peskine449bd832023-01-11 14:50:10 +0100372 if (p == end) {
373 return 0;
374 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100375
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100376 /*
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200377 * trailer_field (if present, must be 1)
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100378 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100379 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
380 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
381 3)) == 0) {
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200382 int trailer_field;
383
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100384 end2 = p + len;
385
Gilles Peskine449bd832023-01-11 14:50:10 +0100386 if ((ret = mbedtls_asn1_get_int(&p, end2, &trailer_field)) != 0) {
387 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
388 }
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100389
Gilles Peskine449bd832023-01-11 14:50:10 +0100390 if (p != end2) {
391 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
392 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
393 }
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200394
Gilles Peskine449bd832023-01-11 14:50:10 +0100395 if (trailer_field != 1) {
396 return MBEDTLS_ERR_X509_INVALID_ALG;
397 }
398 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
399 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100400 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100401
Gilles Peskine449bd832023-01-11 14:50:10 +0100402 if (p != end) {
403 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
404 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
405 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100406
Gilles Peskine449bd832023-01-11 14:50:10 +0100407 return 0;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100408}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200409#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100410
411/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200412 * AttributeTypeAndValue ::= SEQUENCE {
413 * type AttributeType,
414 * value AttributeValue }
415 *
416 * AttributeType ::= OBJECT IDENTIFIER
417 *
418 * AttributeValue ::= ANY DEFINED BY AttributeType
419 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100420static int x509_get_attr_type_value(unsigned char **p,
421 const unsigned char *end,
422 mbedtls_x509_name *cur)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200423{
Janos Follath865b3eb2019-12-16 11:46:15 +0000424 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200425 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200426 mbedtls_x509_buf *oid;
427 mbedtls_x509_buf *val;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200428
Gilles Peskine449bd832023-01-11 14:50:10 +0100429 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
430 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
431 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME, ret);
432 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200433
Hanno Becker12f62fb2019-02-12 17:22:36 +0000434 end = *p + 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
441 oid = &cur->oid;
442 oid->tag = **p;
443
Gilles Peskine449bd832023-01-11 14:50:10 +0100444 if ((ret = mbedtls_asn1_get_tag(p, end, &oid->len, MBEDTLS_ASN1_OID)) != 0) {
445 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME, ret);
446 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200447
448 oid->p = *p;
449 *p += oid->len;
450
Gilles Peskine449bd832023-01-11 14:50:10 +0100451 if ((end - *p) < 1) {
452 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME,
453 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
454 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200455
Gilles Peskine449bd832023-01-11 14:50:10 +0100456 if (**p != MBEDTLS_ASN1_BMP_STRING && **p != MBEDTLS_ASN1_UTF8_STRING &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457 **p != MBEDTLS_ASN1_T61_STRING && **p != MBEDTLS_ASN1_PRINTABLE_STRING &&
458 **p != MBEDTLS_ASN1_IA5_STRING && **p != MBEDTLS_ASN1_UNIVERSAL_STRING &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100459 **p != MBEDTLS_ASN1_BIT_STRING) {
460 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME,
461 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
462 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200463
464 val = &cur->val;
465 val->tag = *(*p)++;
466
Gilles Peskine449bd832023-01-11 14:50:10 +0100467 if ((ret = mbedtls_asn1_get_len(p, end, &val->len)) != 0) {
468 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME, ret);
469 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200470
471 val->p = *p;
472 *p += val->len;
473
Gilles Peskine449bd832023-01-11 14:50:10 +0100474 if (*p != end) {
475 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME,
476 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Hanno Becker12f62fb2019-02-12 17:22:36 +0000477 }
478
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200479 cur->next = NULL;
480
Gilles Peskine449bd832023-01-11 14:50:10 +0100481 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200482}
483
484/*
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000485 * Name ::= CHOICE { -- only one possibility for now --
486 * rdnSequence RDNSequence }
487 *
488 * RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
489 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200490 * RelativeDistinguishedName ::=
491 * SET OF AttributeTypeAndValue
492 *
493 * AttributeTypeAndValue ::= SEQUENCE {
494 * type AttributeType,
495 * value AttributeValue }
496 *
497 * AttributeType ::= OBJECT IDENTIFIER
498 *
499 * AttributeValue ::= ANY DEFINED BY AttributeType
Manuel Pégourié-Gonnard5d861852014-10-17 12:41:41 +0200500 *
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000501 * The data structure is optimized for the common case where each RDN has only
502 * one element, which is represented as a list of AttributeTypeAndValue.
503 * For the general case we still use a flat list, but we mark elements of the
504 * same set so that they are "merged" together in the functions that consume
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200505 * this list, eg mbedtls_x509_dn_gets().
David Horstmanned794832022-10-04 18:12:06 +0100506 *
David Horstmann11307a12022-10-17 18:10:23 +0100507 * On success, this function may allocate a linked list starting at cur->next
David Horstmanned794832022-10-04 18:12:06 +0100508 * that must later be free'd by the caller using mbedtls_free(). In error
509 * cases, this function frees all allocated memory internally and the caller
510 * has no freeing responsibilities.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200511 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100512int mbedtls_x509_get_name(unsigned char **p, const unsigned char *end,
513 mbedtls_x509_name *cur)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200514{
Janos Follath865b3eb2019-12-16 11:46:15 +0000515 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard5d861852014-10-17 12:41:41 +0200516 size_t set_len;
517 const unsigned char *end_set;
David Horstmanned794832022-10-04 18:12:06 +0100518 mbedtls_x509_name *head = cur;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200519
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100520 /* don't use recursion, we'd risk stack overflow if not optimized */
Gilles Peskine449bd832023-01-11 14:50:10 +0100521 while (1) {
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100522 /*
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000523 * parse SET
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100524 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100525 if ((ret = mbedtls_asn1_get_tag(p, end, &set_len,
526 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SET)) != 0) {
527 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME, ret);
David Horstmanned794832022-10-04 18:12:06 +0100528 goto error;
529 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200530
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100531 end_set = *p + set_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200532
Gilles Peskine449bd832023-01-11 14:50:10 +0100533 while (1) {
534 if ((ret = x509_get_attr_type_value(p, end_set, cur)) != 0) {
David Horstmanned794832022-10-04 18:12:06 +0100535 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +0100536 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200537
Gilles Peskine449bd832023-01-11 14:50:10 +0100538 if (*p == end_set) {
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000539 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100540 }
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000541
Manuel Pégourié-Gonnardeecb43c2015-05-12 12:56:41 +0200542 /* Mark this item as being no the only one in a set */
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000543 cur->next_merged = 1;
544
Gilles Peskine449bd832023-01-11 14:50:10 +0100545 cur->next = mbedtls_calloc(1, sizeof(mbedtls_x509_name));
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000546
Gilles Peskine449bd832023-01-11 14:50:10 +0100547 if (cur->next == NULL) {
David Horstmanned794832022-10-04 18:12:06 +0100548 ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
549 goto error;
550 }
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000551
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000552 cur = cur->next;
553 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200554
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100555 /*
556 * continue until end of SEQUENCE is reached
557 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100558 if (*p == end) {
559 return 0;
560 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200561
Gilles Peskine449bd832023-01-11 14:50:10 +0100562 cur->next = mbedtls_calloc(1, sizeof(mbedtls_x509_name));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200563
Gilles Peskine449bd832023-01-11 14:50:10 +0100564 if (cur->next == NULL) {
David Horstmanned794832022-10-04 18:12:06 +0100565 ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
566 goto error;
567 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200568
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100569 cur = cur->next;
570 }
David Horstmanned794832022-10-04 18:12:06 +0100571
572error:
David Horstmanned794832022-10-04 18:12:06 +0100573 /* Skip the first element as we did not allocate it */
Gilles Peskine449bd832023-01-11 14:50:10 +0100574 mbedtls_asn1_free_named_data_list_shallow(head->next);
Glenn Straussa4b40412022-06-26 19:32:09 -0400575 head->next = NULL;
David Horstmanned794832022-10-04 18:12:06 +0100576
Gilles Peskine449bd832023-01-11 14:50:10 +0100577 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200578}
579
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400580static int x509_date_is_valid(const mbedtls_x509_time *t)
Janos Follath87c98072017-02-03 12:36:59 +0000581{
David Horstmanncdf52832023-07-05 09:58:03 +0100582 unsigned int month_days;
583 unsigned int year;
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400584 switch (t->mon) {
585 case 1: case 3: case 5: case 7: case 8: case 10: case 12:
David Horstmanncdf52832023-07-05 09:58:03 +0100586 month_days = 31;
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400587 break;
588 case 4: case 6: case 9: case 11:
David Horstmanncdf52832023-07-05 09:58:03 +0100589 month_days = 30;
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400590 break;
591 case 2:
David Horstmanncdf52832023-07-05 09:58:03 +0100592 year = (unsigned int) t->year;
593 month_days = ((year & 3) || (!(year % 100)
594 && (year % 400)))
595 ? 28 : 29;
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400596 break;
597 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100598 return MBEDTLS_ERR_X509_INVALID_DATE;
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400599 }
Janos Follath87c98072017-02-03 12:36:59 +0000600
David Horstmannb1d27bc2023-07-05 10:00:31 +0100601 if ((unsigned int) (t->day - 1) >= month_days || /* (1 - days in month) */
David Horstmann3ae1c4c2023-07-05 11:15:08 +0100602 /* (unsigned int) (t->mon - 1) >= 12 || */ /* (1 - 12) checked above */
David Horstmannb1d27bc2023-07-05 10:00:31 +0100603 (unsigned int) t->year > 9999 || /* (0 - 9999) */
604 (unsigned int) t->hour > 23 || /* (0 - 23) */
605 (unsigned int) t->min > 59 || /* (0 - 59) */
606 (unsigned int) t->sec > 59) { /* (0 - 59) */
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400607 return MBEDTLS_ERR_X509_INVALID_DATE;
Rich Evans7d5a55a2015-02-13 11:48:02 +0000608 }
Janos Follath87c98072017-02-03 12:36:59 +0000609
Gilles Peskine449bd832023-01-11 14:50:10 +0100610 return 0;
Rich Evans7d5a55a2015-02-13 11:48:02 +0000611}
612
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400613static int x509_parse2_int(const unsigned char *p)
Andres AG4b76aec2016-09-23 13:16:02 +0100614{
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400615 uint32_t d1 = p[0] - '0';
616 uint32_t d2 = p[1] - '0';
617 return (d1 < 10 && d2 < 10) ? (int) (d1 * 10 + d2) : -1;
Andres AG4b76aec2016-09-23 13:16:02 +0100618}
619
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200620/*
Janos Follath87c98072017-02-03 12:36:59 +0000621 * Parse an ASN1_UTC_TIME (yearlen=2) or ASN1_GENERALIZED_TIME (yearlen=4)
622 * field.
623 */
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400624static int x509_parse_time(const unsigned char *p, mbedtls_x509_time *tm,
625 size_t yearlen)
Janos Follath87c98072017-02-03 12:36:59 +0000626{
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400627 int x;
Janos Follath87c98072017-02-03 12:36:59 +0000628
629 /*
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400630 * Parse year, month, day, hour, minute, second
Janos Follath87c98072017-02-03 12:36:59 +0000631 */
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400632 tm->year = x509_parse2_int(p);
633 if (tm->year < 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100634 return MBEDTLS_ERR_X509_INVALID_DATE;
635 }
Janos Follath87c98072017-02-03 12:36:59 +0000636
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400637 if (4 == yearlen) {
638 x = tm->year * 100;
639 p += 2;
640 tm->year = x509_parse2_int(p);
641 if (tm->year < 0) {
642 return MBEDTLS_ERR_X509_INVALID_DATE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100643 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100644 } else {
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400645 x = (tm->year < 50) ? 2000 : 1900;
Janos Follath87c98072017-02-03 12:36:59 +0000646 }
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400647 tm->year += x;
Janos Follath87c98072017-02-03 12:36:59 +0000648
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400649 tm->mon = x509_parse2_int(p + 2);
650 tm->day = x509_parse2_int(p + 4);
651 tm->hour = x509_parse2_int(p + 6);
652 tm->min = x509_parse2_int(p + 8);
653 tm->sec = x509_parse2_int(p + 10);
Janos Follath87c98072017-02-03 12:36:59 +0000654
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400655 return x509_date_is_valid(tm);
Janos Follath87c98072017-02-03 12:36:59 +0000656}
657
658/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200659 * Time ::= CHOICE {
660 * utcTime UTCTime,
661 * generalTime GeneralizedTime }
662 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100663int mbedtls_x509_get_time(unsigned char **p, const unsigned char *end,
664 mbedtls_x509_time *tm)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200665{
Janos Follath865b3eb2019-12-16 11:46:15 +0000666 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Janos Follath87c98072017-02-03 12:36:59 +0000667 size_t len, year_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200668 unsigned char tag;
669
Gilles Peskine449bd832023-01-11 14:50:10 +0100670 if ((end - *p) < 1) {
671 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE,
672 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
673 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200674
675 tag = **p;
676
Gilles Peskine449bd832023-01-11 14:50:10 +0100677 if (tag == MBEDTLS_ASN1_UTC_TIME) {
Janos Follath87c98072017-02-03 12:36:59 +0000678 year_len = 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100679 } else if (tag == MBEDTLS_ASN1_GENERALIZED_TIME) {
Janos Follath87c98072017-02-03 12:36:59 +0000680 year_len = 4;
Gilles Peskine449bd832023-01-11 14:50:10 +0100681 } else {
682 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE,
683 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
684 }
Janos Follath87c98072017-02-03 12:36:59 +0000685
686 (*p)++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100687 ret = mbedtls_asn1_get_len(p, end, &len);
Janos Follath87c98072017-02-03 12:36:59 +0000688
Gilles Peskine449bd832023-01-11 14:50:10 +0100689 if (ret != 0) {
690 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE, ret);
691 }
Janos Follath87c98072017-02-03 12:36:59 +0000692
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400693 /* len is 12 or 14 depending on year_len, plus optional trailing 'Z' */
694 if (len != year_len + 10 &&
695 !(len == year_len + 11 && (*p)[(len - 1)] == 'Z')) {
696 return MBEDTLS_ERR_X509_INVALID_DATE;
697 }
698
699 (*p) += len;
700 return x509_parse_time(*p - len, tm, year_len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200701}
702
Gilles Peskine449bd832023-01-11 14:50:10 +0100703int mbedtls_x509_get_sig(unsigned char **p, const unsigned char *end, mbedtls_x509_buf *sig)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200704{
Janos Follath865b3eb2019-12-16 11:46:15 +0000705 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200706 size_t len;
Andres AG4bdbe092016-09-19 16:58:45 +0100707 int tag_type;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200708
Gilles Peskine449bd832023-01-11 14:50:10 +0100709 if ((end - *p) < 1) {
710 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_SIGNATURE,
711 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
712 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200713
Andres AG4bdbe092016-09-19 16:58:45 +0100714 tag_type = **p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200715
Gilles Peskine449bd832023-01-11 14:50:10 +0100716 if ((ret = mbedtls_asn1_get_bitstring_null(p, end, &len)) != 0) {
717 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_SIGNATURE, ret);
718 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200719
Andres AG4bdbe092016-09-19 16:58:45 +0100720 sig->tag = tag_type;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200721 sig->len = len;
722 sig->p = *p;
723
724 *p += len;
725
Gilles Peskine449bd832023-01-11 14:50:10 +0100726 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200727}
728
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100729/*
730 * Get signature algorithm from alg OID and optional parameters
731 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100732int mbedtls_x509_get_sig_alg(const mbedtls_x509_buf *sig_oid, const mbedtls_x509_buf *sig_params,
733 mbedtls_md_type_t *md_alg, mbedtls_pk_type_t *pk_alg,
734 void **sig_opts)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200735{
Janos Follath865b3eb2019-12-16 11:46:15 +0000736 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200737
Gilles Peskine449bd832023-01-11 14:50:10 +0100738 if (*sig_opts != NULL) {
739 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
740 }
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200741
Gilles Peskine449bd832023-01-11 14:50:10 +0100742 if ((ret = mbedtls_oid_get_sig_alg(sig_oid, md_alg, pk_alg)) != 0) {
743 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG, ret);
744 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200745
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200746#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100747 if (*pk_alg == MBEDTLS_PK_RSASSA_PSS) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200748 mbedtls_pk_rsassa_pss_options *pss_opts;
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100749
Gilles Peskine449bd832023-01-11 14:50:10 +0100750 pss_opts = mbedtls_calloc(1, sizeof(mbedtls_pk_rsassa_pss_options));
751 if (pss_opts == NULL) {
752 return MBEDTLS_ERR_X509_ALLOC_FAILED;
753 }
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200754
Gilles Peskine449bd832023-01-11 14:50:10 +0100755 ret = mbedtls_x509_get_rsassa_pss_params(sig_params,
756 md_alg,
757 &pss_opts->mgf1_hash_id,
758 &pss_opts->expected_salt_len);
759 if (ret != 0) {
760 mbedtls_free(pss_opts);
761 return ret;
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200762 }
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100763
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200764 *sig_opts = (void *) pss_opts;
Gilles Peskine449bd832023-01-11 14:50:10 +0100765 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200766#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100767 {
768 /* Make sure parameters are absent or NULL */
Gilles Peskine449bd832023-01-11 14:50:10 +0100769 if ((sig_params->tag != MBEDTLS_ASN1_NULL && sig_params->tag != 0) ||
770 sig_params->len != 0) {
771 return MBEDTLS_ERR_X509_INVALID_ALG;
772 }
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100773 }
774
Gilles Peskine449bd832023-01-11 14:50:10 +0100775 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200776}
777
778/*
779 * X.509 Extensions (No parsing of extensions, pointer should
Brian J Murray1903fb32016-11-06 04:45:15 -0800780 * be either manually updated or extensions should be parsed!)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200781 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100782int mbedtls_x509_get_ext(unsigned char **p, const unsigned char *end,
783 mbedtls_x509_buf *ext, int tag)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200784{
Janos Follath865b3eb2019-12-16 11:46:15 +0000785 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200786 size_t len;
787
Hanno Becker3cddba82019-02-11 14:33:36 +0000788 /* Extension structure use EXPLICIT tagging. That is, the actual
789 * `Extensions` structure is wrapped by a tag-length pair using
790 * the respective context-specific tag. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100791 ret = mbedtls_asn1_get_tag(p, end, &ext->len,
792 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | tag);
793 if (ret != 0) {
794 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
795 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200796
Hanno Becker6ccfb182019-02-12 11:52:10 +0000797 ext->tag = MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | tag;
798 ext->p = *p;
799 end = *p + ext->len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200800
801 /*
802 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200803 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100804 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
805 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
806 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
807 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200808
Gilles Peskine449bd832023-01-11 14:50:10 +0100809 if (end != *p + len) {
810 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
811 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
812 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200813
Gilles Peskine449bd832023-01-11 14:50:10 +0100814 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200815}
816
Agathiyan Bragadeesh9d2507c2023-07-24 16:35:57 +0100817static char nibble_to_hex_digit(int i)
Agathiyan Bragadeeshef2decb2023-07-21 15:47:47 +0100818{
Agathiyan Bragadeeshf0e1ac52023-07-24 16:43:36 +0100819 return (i < 10) ? (i + '0') : (i - 10 + 'A');
Agathiyan Bragadeeshef2decb2023-07-21 15:47:47 +0100820}
821
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200822/*
823 * Store the name in printable form into buf; no more
824 * than size characters will be written
825 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100826int mbedtls_x509_dn_gets(char *buf, size_t size, const mbedtls_x509_name *dn)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200827{
Janos Follath865b3eb2019-12-16 11:46:15 +0000828 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh5adffb22023-08-10 15:50:57 +0100829 size_t i, j, n, asn1_len_size, asn1_tag_size, asn1_tag_len_buf_start;
830 unsigned char asn1_tag_len_buf[10];
Agathiyan Bragadeeshddc720d2023-07-26 15:51:49 +0100831 unsigned char *asn1_len_p;
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000832 unsigned char c, merge = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200833 const mbedtls_x509_name *name;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200834 const char *short_name = NULL;
Agathiyan Bragadeesha1f5c2d2023-08-02 17:08:52 +0100835 char lowbits, highbits;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200836 char s[MBEDTLS_X509_MAX_DN_NAME_SIZE], *p;
Agathiyan Bragadeeshc9d74f32023-07-31 17:25:44 +0100837 int print_hexstring;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200838
Gilles Peskine449bd832023-01-11 14:50:10 +0100839 memset(s, 0, sizeof(s));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200840
841 name = dn;
842 p = buf;
843 n = size;
844
Gilles Peskine449bd832023-01-11 14:50:10 +0100845 while (name != NULL) {
846 if (!name->oid.p) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200847 name = name->next;
848 continue;
849 }
850
Gilles Peskine449bd832023-01-11 14:50:10 +0100851 if (name != dn) {
852 ret = mbedtls_snprintf(p, n, merge ? " + " : ", ");
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200853 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200854 }
855
Agathiyan Bragadeesh0a4b6d82023-08-02 15:05:57 +0100856 print_hexstring = (name->val.tag != MBEDTLS_ASN1_UTF8_STRING) &&
857 (name->val.tag != MBEDTLS_ASN1_PRINTABLE_STRING) &&
858 (name->val.tag != MBEDTLS_ASN1_IA5_STRING);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200859
Agathiyan Bragadeeshc9d74f32023-07-31 17:25:44 +0100860 if ((ret = mbedtls_oid_get_attr_short_name(&name->oid, &short_name)) == 0) {
861 ret = mbedtls_snprintf(p, n, "%s=", short_name);
862 } else {
Agathiyan Bragadeeshaf70c7d2023-08-10 16:39:23 +0100863 if ((ret = mbedtls_oid_get_numeric_string(p, n, &name->oid)) > 0) {
Agathiyan Bragadeeshf3b97242023-08-22 16:37:11 +0100864 n -= ret;
865 p += ret;
Agathiyan Bragadeeshaf70c7d2023-08-10 16:39:23 +0100866 ret = mbedtls_snprintf(p, n, "=");
Agathiyan Bragadeeshc9d74f32023-07-31 17:25:44 +0100867 print_hexstring = 1;
Agathiyan Bragadeesh8aa74ab2023-08-22 16:42:27 +0100868 } else if (ret == MBEDTLS_ERR_OID_BUF_TOO_SMALL) {
869 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
870 }
871 else {
Agathiyan Bragadeeshddc720d2023-07-26 15:51:49 +0100872 ret = mbedtls_snprintf(p, n, "\?\?=");
873 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100874 }
Agathiyan Bragadeeshc9d74f32023-07-31 17:25:44 +0100875 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200876
Agathiyan Bragadeesh4987c8f2023-08-01 11:10:52 +0100877 if (print_hexstring) {
Agathiyan Bragadeeshee642d92023-08-10 14:08:27 +0100878#if defined(MBEDTLS_ASN1_WRITE_C)
Agathiyan Bragadeeshddc720d2023-07-26 15:51:49 +0100879 s[0] = '#';
Agathiyan Bragadeesh0eb66732023-07-31 16:10:07 +0100880
Agathiyan Bragadeesh5adffb22023-08-10 15:50:57 +0100881 asn1_len_p = asn1_tag_len_buf + 10;
Agathiyan Bragadeesh07f472a2023-08-22 16:29:39 +0100882 if((ret = mbedtls_asn1_write_len(&asn1_len_p, asn1_tag_len_buf, name->val.len)) < 0) {
883 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
884 }
885 asn1_len_size = ret;
886 if((ret = mbedtls_asn1_write_tag(&asn1_len_p, asn1_tag_len_buf, name->val.tag)) < 0) {
887 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
888 }
889 asn1_tag_size = ret;
Agathiyan Bragadeesh5adffb22023-08-10 15:50:57 +0100890 asn1_tag_len_buf_start = 10 - asn1_len_size - asn1_tag_size;
891 for (i = 0, j = 1; i < asn1_len_size + asn1_tag_size; i++) {
Agathiyan Bragadeeshddc720d2023-07-26 15:51:49 +0100892 if (j + 1 >= sizeof(s) - 1) {
Agathiyan Bragadeeshef2decb2023-07-21 15:47:47 +0100893 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
894 }
Agathiyan Bragadeesh5adffb22023-08-10 15:50:57 +0100895 c = asn1_tag_len_buf[asn1_tag_len_buf_start+i];
Agathiyan Bragadeesha1f5c2d2023-08-02 17:08:52 +0100896 lowbits = (c & 0x0F);
Agathiyan Bragadeesh2bf09a62023-08-10 14:37:00 +0100897 highbits = c >> 4;
Agathiyan Bragadeesh9d2507c2023-07-24 16:35:57 +0100898 s[j++] = nibble_to_hex_digit(highbits);
Agathiyan Bragadeesh5adffb22023-08-10 15:50:57 +0100899 s[j++] = nibble_to_hex_digit(lowbits);
900 }
901 for (i = 0; i < name->val.len; i++) {
902 if (j + 1 >= sizeof(s) - 1) {
903 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
904 }
905 c = name->val.p[i];
906 lowbits = (c & 0x0F);
907 highbits = c >> 4;
908 s[j++] = nibble_to_hex_digit(highbits);
909 s[j++] = nibble_to_hex_digit(lowbits);
Agathiyan Bragadeeshddc720d2023-07-26 15:51:49 +0100910 }
Agathiyan Bragadeeshee642d92023-08-10 14:08:27 +0100911#else
Agathiyan Bragadeesh0eb66732023-07-31 16:10:07 +0100912 return MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
Agathiyan Bragadeeshee642d92023-08-10 14:08:27 +0100913#endif
Agathiyan Bragadeeshddc720d2023-07-26 15:51:49 +0100914 } else {
915 for (i = 0, j = 0; i < name->val.len; i++, j++) {
916 if (j >= sizeof(s) - 1) {
917 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
918 }
919
920 c = name->val.p[i];
921 // Special characters requiring escaping, RFC 4514 Section 2.4
Agathiyan Bragadeesh022f86f2023-08-22 16:56:04 +0100922 if (c == '\0') {
923 return MBEDTLS_ERR_X509_INVALID_NAME;
924 }
925 else {
Agathiyan Bragadeesha7f96302023-08-10 16:03:27 +0100926 if (strchr(",=+<>;\"\\", c) ||
Agathiyan Bragadeeshddc720d2023-07-26 15:51:49 +0100927 ((i == 0) && strchr("# ", c)) ||
928 ((i == name->val.len-1) && (c == ' '))) {
929 if (j + 1 >= sizeof(s) - 1) {
930 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
931 }
932 s[j++] = '\\';
933 }
934 }
935 if (c < 32 || c >= 127) {
936 if (j + 3 >= sizeof(s) - 1) {
937 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
938 }
939 s[j++] = '\\';
Agathiyan Bragadeesha1f5c2d2023-08-02 17:08:52 +0100940 lowbits = (c & 0x0F);
Agathiyan Bragadeesh2bf09a62023-08-10 14:37:00 +0100941 highbits = c >> 4;
Agathiyan Bragadeeshddc720d2023-07-26 15:51:49 +0100942 s[j++] = nibble_to_hex_digit(highbits);
943 s[j] = nibble_to_hex_digit(lowbits);
944 } else {
945 s[j] = c;
946 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100947 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200948 }
Werner Lewisb33dacd2022-05-20 12:48:46 +0100949 s[j] = '\0';
Gilles Peskine449bd832023-01-11 14:50:10 +0100950 ret = mbedtls_snprintf(p, n, "%s", s);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200951 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000952
953 merge = name->next_merged;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200954 name = name->next;
955 }
956
Gilles Peskine449bd832023-01-11 14:50:10 +0100957 return (int) (size - n);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200958}
959
960/*
961 * Store the serial in printable form into buf; no more
962 * than size characters will be written
963 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100964int mbedtls_x509_serial_gets(char *buf, size_t size, const mbedtls_x509_buf *serial)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200965{
Janos Follath865b3eb2019-12-16 11:46:15 +0000966 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200967 size_t i, n, nr;
968 char *p;
969
970 p = buf;
971 n = size;
972
Gilles Peskine449bd832023-01-11 14:50:10 +0100973 nr = (serial->len <= 32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200974 ? serial->len : 28;
975
Gilles Peskine449bd832023-01-11 14:50:10 +0100976 for (i = 0; i < nr; i++) {
977 if (i == 0 && nr > 1 && serial->p[i] == 0x0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200978 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100979 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200980
Gilles Peskine449bd832023-01-11 14:50:10 +0100981 ret = mbedtls_snprintf(p, n, "%02X%s",
982 serial->p[i], (i < nr - 1) ? ":" : "");
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200983 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200984 }
985
Gilles Peskine449bd832023-01-11 14:50:10 +0100986 if (nr != serial->len) {
987 ret = mbedtls_snprintf(p, n, "....");
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200988 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200989 }
990
Gilles Peskine449bd832023-01-11 14:50:10 +0100991 return (int) (size - n);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200992}
993
Hanno Becker612a2f12020-10-09 09:19:39 +0100994#if !defined(MBEDTLS_X509_REMOVE_INFO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200995/*
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200996 * Helper for writing signature algorithms
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100997 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100998int mbedtls_x509_sig_alg_gets(char *buf, size_t size, const mbedtls_x509_buf *sig_oid,
999 mbedtls_pk_type_t pk_alg, mbedtls_md_type_t md_alg,
1000 const void *sig_opts)
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +01001001{
Janos Follath865b3eb2019-12-16 11:46:15 +00001002 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +01001003 char *p = buf;
1004 size_t n = size;
1005 const char *desc = NULL;
1006
Gilles Peskine449bd832023-01-11 14:50:10 +01001007 ret = mbedtls_oid_get_sig_alg_desc(sig_oid, &desc);
1008 if (ret != 0) {
1009 ret = mbedtls_snprintf(p, n, "???");
1010 } else {
1011 ret = mbedtls_snprintf(p, n, "%s", desc);
1012 }
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001013 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +01001014
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001015#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001016 if (pk_alg == MBEDTLS_PK_RSASSA_PSS) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001017 const mbedtls_pk_rsassa_pss_options *pss_opts;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +01001018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001019 pss_opts = (const mbedtls_pk_rsassa_pss_options *) sig_opts;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +01001020
Gilles Peskine449bd832023-01-11 14:50:10 +01001021 const char *name = md_type_to_string(md_alg);
1022 const char *mgf_name = md_type_to_string(pss_opts->mgf1_hash_id);
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +01001023
Gilles Peskine449bd832023-01-11 14:50:10 +01001024 ret = mbedtls_snprintf(p, n, " (%s, MGF1-%s, 0x%02X)",
1025 name ? name : "???",
1026 mgf_name ? mgf_name : "???",
1027 (unsigned int) pss_opts->expected_salt_len);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001028 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +01001029 }
1030#else
1031 ((void) pk_alg);
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +02001032 ((void) md_alg);
1033 ((void) sig_opts);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001034#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +01001035
Gilles Peskine449bd832023-01-11 14:50:10 +01001036 return (int) (size - n);
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +01001037}
Hanno Becker612a2f12020-10-09 09:19:39 +01001038#endif /* MBEDTLS_X509_REMOVE_INFO */
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +01001039
1040/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001041 * Helper for writing "RSA key size", "EC key size", etc
1042 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001043int mbedtls_x509_key_size_helper(char *buf, size_t buf_size, const char *name)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001044{
1045 char *p = buf;
Manuel Pégourié-Gonnardfb317c52015-06-18 16:25:56 +02001046 size_t n = buf_size;
Janos Follath865b3eb2019-12-16 11:46:15 +00001047 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001048
Gilles Peskine449bd832023-01-11 14:50:10 +01001049 ret = mbedtls_snprintf(p, n, "%s key size", name);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001050 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001051
Gilles Peskine449bd832023-01-11 14:50:10 +01001052 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001053}
1054
Glenn Strauss416dc032022-06-30 00:38:53 -04001055int mbedtls_x509_time_cmp(const mbedtls_x509_time *t1,
1056 const mbedtls_x509_time *t2)
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001057{
Glenn Strauss5aef2972022-06-30 04:38:02 -04001058 int x;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001059
Glenn Strauss5aef2972022-06-30 04:38:02 -04001060 x = (((t1->year << 9) | (t1->mon << 5) | (t1->day)) -
1061 ((t2->year << 9) | (t2->mon << 5) | (t2->day)));
1062 if (x != 0) {
1063 return x;
Manuel Pégourié-Gonnard57e10d72015-06-22 18:59:21 +02001064 }
Manuel Pégourié-Gonnard864108d2015-05-29 10:11:03 +02001065
Glenn Strauss5aef2972022-06-30 04:38:02 -04001066 x = (((t1->hour << 12) | (t1->min << 6) | (t1->sec)) -
1067 ((t2->hour << 12) | (t2->min << 6) | (t2->sec)));
1068 return x;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001069}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001070
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001071#if defined(MBEDTLS_HAVE_TIME_DATE)
Glenn Strauss61d99302022-06-30 05:25:56 -04001072int mbedtls_x509_time_gmtime(mbedtls_time_t tt, mbedtls_x509_time *now)
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001073{
Glenn Strauss811eeb22022-06-30 05:28:50 -04001074 struct tm tm;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001075
Glenn Strauss811eeb22022-06-30 05:28:50 -04001076 if (mbedtls_platform_gmtime_r(&tt, &tm) == NULL) {
1077 return -1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001078 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001079
Glenn Strauss811eeb22022-06-30 05:28:50 -04001080 now->year = tm.tm_year + 1900;
1081 now->mon = tm.tm_mon + 1;
1082 now->day = tm.tm_mday;
1083 now->hour = tm.tm_hour;
1084 now->min = tm.tm_min;
1085 now->sec = tm.tm_sec;
Gilles Peskine449bd832023-01-11 14:50:10 +01001086 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001087}
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001088
Glenn Strauss61d99302022-06-30 05:25:56 -04001089static int x509_get_current_time(mbedtls_x509_time *now)
1090{
1091 return mbedtls_x509_time_gmtime(mbedtls_time(NULL), now);
1092}
1093
Gilles Peskine449bd832023-01-11 14:50:10 +01001094int mbedtls_x509_time_is_past(const mbedtls_x509_time *to)
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001095{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001096 mbedtls_x509_time now;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001097
Gilles Peskine449bd832023-01-11 14:50:10 +01001098 if (x509_get_current_time(&now) != 0) {
1099 return 1;
1100 }
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001101
Glenn Strauss416dc032022-06-30 00:38:53 -04001102 return mbedtls_x509_time_cmp(to, &now) < 0;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001103}
1104
Gilles Peskine449bd832023-01-11 14:50:10 +01001105int mbedtls_x509_time_is_future(const mbedtls_x509_time *from)
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001106{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001107 mbedtls_x509_time now;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001108
Gilles Peskine449bd832023-01-11 14:50:10 +01001109 if (x509_get_current_time(&now) != 0) {
1110 return 1;
1111 }
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001112
Glenn Strauss416dc032022-06-30 00:38:53 -04001113 return mbedtls_x509_time_cmp(from, &now) > 0;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001114}
1115
Manuel Pégourié-Gonnard60c793b2015-06-18 20:52:58 +02001116#else /* MBEDTLS_HAVE_TIME_DATE */
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001117
Gilles Peskine449bd832023-01-11 14:50:10 +01001118int mbedtls_x509_time_is_past(const mbedtls_x509_time *to)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001119{
1120 ((void) to);
Gilles Peskine449bd832023-01-11 14:50:10 +01001121 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001122}
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001123
Gilles Peskine449bd832023-01-11 14:50:10 +01001124int mbedtls_x509_time_is_future(const mbedtls_x509_time *from)
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001125{
1126 ((void) from);
Gilles Peskine449bd832023-01-11 14:50:10 +01001127 return 0;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001128}
Manuel Pégourié-Gonnard60c793b2015-06-18 20:52:58 +02001129#endif /* MBEDTLS_HAVE_TIME_DATE */
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001130
1131/* Common functions for parsing CRT and CSR. */
1132#if defined(MBEDTLS_X509_CRT_PARSE_C) || defined(MBEDTLS_X509_CSR_PARSE_C)
1133/*
1134 * OtherName ::= SEQUENCE {
1135 * type-id OBJECT IDENTIFIER,
1136 * value [0] EXPLICIT ANY DEFINED BY type-id }
1137 *
1138 * HardwareModuleName ::= SEQUENCE {
1139 * hwType OBJECT IDENTIFIER,
1140 * hwSerialNum OCTET STRING }
1141 *
1142 * NOTE: we currently only parse and use otherName of type HwModuleName,
1143 * as defined in RFC 4108.
1144 */
1145static int x509_get_other_name(const mbedtls_x509_buf *subject_alt_name,
1146 mbedtls_x509_san_other_name *other_name)
1147{
1148 int ret = 0;
1149 size_t len;
1150 unsigned char *p = subject_alt_name->p;
1151 const unsigned char *end = p + subject_alt_name->len;
1152 mbedtls_x509_buf cur_oid;
1153
1154 if ((subject_alt_name->tag &
1155 (MBEDTLS_ASN1_TAG_CLASS_MASK | MBEDTLS_ASN1_TAG_VALUE_MASK)) !=
1156 (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME)) {
1157 /*
1158 * The given subject alternative name is not of type "othername".
1159 */
1160 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1161 }
1162
1163 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1164 MBEDTLS_ASN1_OID)) != 0) {
1165 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1166 }
1167
1168 cur_oid.tag = MBEDTLS_ASN1_OID;
1169 cur_oid.p = p;
1170 cur_oid.len = len;
1171
1172 /*
1173 * Only HwModuleName is currently supported.
1174 */
1175 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ON_HW_MODULE_NAME, &cur_oid) != 0) {
1176 return MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
1177 }
1178
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001179 p += len;
1180 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1181 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC)) !=
1182 0) {
1183 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1184 }
1185
Hanno Beckerdae916b2019-09-13 14:21:13 +01001186 if (end != p + len) {
1187 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1188 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1189 }
1190
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001191 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1192 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1193 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1194 }
1195
Hanno Beckerdae916b2019-09-13 14:21:13 +01001196 if (end != p + len) {
1197 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1198 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1199 }
1200
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001201 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OID)) != 0) {
1202 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1203 }
1204
1205 other_name->value.hardware_module_name.oid.tag = MBEDTLS_ASN1_OID;
1206 other_name->value.hardware_module_name.oid.p = p;
1207 other_name->value.hardware_module_name.oid.len = len;
1208
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001209 p += len;
1210 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1211 MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1212 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1213 }
1214
1215 other_name->value.hardware_module_name.val.tag = MBEDTLS_ASN1_OCTET_STRING;
1216 other_name->value.hardware_module_name.val.p = p;
1217 other_name->value.hardware_module_name.val.len = len;
1218 p += len;
1219 if (p != end) {
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001220 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1221 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1222 }
1223 return 0;
1224}
1225
Przemek Stekiel21903ec2023-02-21 08:32:37 +01001226/* Check mbedtls_x509_get_subject_alt_name for detailed description.
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001227 *
Przemek Stekiel21903ec2023-02-21 08:32:37 +01001228 * In some cases while parsing subject alternative names the sequence tag is optional
1229 * (e.g. CertSerialNumber). This function is designed to handle such case.
Przemek Stekiel725688b2023-04-04 22:49:44 +02001230 */
Przemek Stekiel21903ec2023-02-21 08:32:37 +01001231int mbedtls_x509_get_subject_alt_name_ext(unsigned char **p,
1232 const unsigned char *end,
1233 mbedtls_x509_sequence *subject_alt_name)
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001234{
1235 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel21903ec2023-02-21 08:32:37 +01001236 size_t tag_len;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001237 mbedtls_asn1_sequence *cur = subject_alt_name;
1238
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001239 while (*p < end) {
Andrzej Kurek154a6052023-04-30 14:11:49 -04001240 mbedtls_x509_subject_alternative_name tmp_san_name;
Hanno Beckerae8f8c42019-09-13 12:24:56 +01001241 mbedtls_x509_buf tmp_san_buf;
Andrzej Kurek154a6052023-04-30 14:11:49 -04001242 memset(&tmp_san_name, 0, sizeof(tmp_san_name));
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001243
Hanno Beckerae8f8c42019-09-13 12:24:56 +01001244 tmp_san_buf.tag = **p;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001245 (*p)++;
Hanno Beckerae8f8c42019-09-13 12:24:56 +01001246
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001247 if ((ret = mbedtls_asn1_get_len(p, end, &tag_len)) != 0) {
1248 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1249 }
1250
Hanno Beckerae8f8c42019-09-13 12:24:56 +01001251 tmp_san_buf.p = *p;
1252 tmp_san_buf.len = tag_len;
1253
1254 if ((tmp_san_buf.tag & MBEDTLS_ASN1_TAG_CLASS_MASK) !=
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001255 MBEDTLS_ASN1_CONTEXT_SPECIFIC) {
1256 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1257 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
1258 }
1259
1260 /*
Andrzej Kurek154a6052023-04-30 14:11:49 -04001261 * Check that the SAN is structured correctly by parsing it.
1262 * The SAN structure is discarded afterwards.
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001263 */
Andrzej Kurek154a6052023-04-30 14:11:49 -04001264 ret = mbedtls_x509_parse_subject_alt_name(&tmp_san_buf, &tmp_san_name);
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001265 /*
1266 * In case the extension is malformed, return an error,
1267 * and clear the allocated sequences.
1268 */
1269 if (ret != 0 && ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE) {
1270 mbedtls_asn1_sequence_free(subject_alt_name->next);
1271 subject_alt_name->next = NULL;
1272 return ret;
1273 }
1274
Andrzej Kurek154a6052023-04-30 14:11:49 -04001275 mbedtls_x509_free_subject_alt_name(&tmp_san_name);
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001276 /* Allocate and assign next pointer */
1277 if (cur->buf.p != NULL) {
1278 if (cur->next != NULL) {
1279 return MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
1280 }
1281
1282 cur->next = mbedtls_calloc(1, sizeof(mbedtls_asn1_sequence));
1283
1284 if (cur->next == NULL) {
1285 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1286 MBEDTLS_ERR_ASN1_ALLOC_FAILED);
1287 }
1288
1289 cur = cur->next;
1290 }
1291
Hanno Beckerae8f8c42019-09-13 12:24:56 +01001292 cur->buf = tmp_san_buf;
1293 *p += tmp_san_buf.len;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001294 }
1295
1296 /* Set final sequence entry's next pointer to NULL */
1297 cur->next = NULL;
1298
1299 if (*p != end) {
1300 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1301 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1302 }
1303
1304 return 0;
1305}
1306
Przemek Stekiel21903ec2023-02-21 08:32:37 +01001307/*
1308 * SubjectAltName ::= GeneralNames
1309 *
1310 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
1311 *
1312 * GeneralName ::= CHOICE {
1313 * otherName [0] OtherName,
1314 * rfc822Name [1] IA5String,
1315 * dNSName [2] IA5String,
1316 * x400Address [3] ORAddress,
1317 * directoryName [4] Name,
1318 * ediPartyName [5] EDIPartyName,
1319 * uniformResourceIdentifier [6] IA5String,
1320 * iPAddress [7] OCTET STRING,
1321 * registeredID [8] OBJECT IDENTIFIER }
1322 *
1323 * OtherName ::= SEQUENCE {
1324 * type-id OBJECT IDENTIFIER,
1325 * value [0] EXPLICIT ANY DEFINED BY type-id }
1326 *
1327 * EDIPartyName ::= SEQUENCE {
1328 * nameAssigner [0] DirectoryString OPTIONAL,
1329 * partyName [1] DirectoryString }
1330 *
1331 * We list all types, but use the following GeneralName types from RFC 5280:
1332 * "dnsName", "uniformResourceIdentifier" and "hardware_module_name"
1333 * of type "otherName", as defined in RFC 4108.
1334 */
1335int mbedtls_x509_get_subject_alt_name(unsigned char **p,
1336 const unsigned char *end,
1337 mbedtls_x509_sequence *subject_alt_name)
1338{
1339 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1340 size_t len;
1341
1342 /* Get main sequence tag */
1343 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
1344 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1345 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1346 }
1347
1348 if (*p + len != end) {
1349 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1350 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1351 }
1352
1353 return mbedtls_x509_get_subject_alt_name_ext(p, end, subject_alt_name);
1354}
1355
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001356int mbedtls_x509_get_ns_cert_type(unsigned char **p,
1357 const unsigned char *end,
1358 unsigned char *ns_cert_type)
1359{
1360 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1361 mbedtls_x509_bitstring bs = { 0, 0, NULL };
1362
1363 if ((ret = mbedtls_asn1_get_bitstring(p, end, &bs)) != 0) {
1364 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1365 }
1366
Przemek Stekiel32e20912023-01-25 14:26:15 +01001367 /* A bitstring with no flags set is still technically valid, as it will mean
1368 that the certificate has no designated purpose at the time of creation. */
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001369 if (bs.len == 0) {
1370 *ns_cert_type = 0;
1371 return 0;
1372 }
1373
1374 if (bs.len != 1) {
1375 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1376 MBEDTLS_ERR_ASN1_INVALID_LENGTH);
1377 }
1378
1379 /* Get actual bitstring */
1380 *ns_cert_type = *bs.p;
1381 return 0;
1382}
1383
1384int mbedtls_x509_get_key_usage(unsigned char **p,
1385 const unsigned char *end,
1386 unsigned int *key_usage)
1387{
1388 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1389 size_t i;
1390 mbedtls_x509_bitstring bs = { 0, 0, NULL };
1391
1392 if ((ret = mbedtls_asn1_get_bitstring(p, end, &bs)) != 0) {
1393 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1394 }
1395
Przemek Stekiel32e20912023-01-25 14:26:15 +01001396 /* A bitstring with no flags set is still technically valid, as it will mean
1397 that the certificate has no designated purpose at the time of creation. */
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001398 if (bs.len == 0) {
1399 *key_usage = 0;
1400 return 0;
1401 }
1402
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001403 /* Get actual bitstring */
1404 *key_usage = 0;
1405 for (i = 0; i < bs.len && i < sizeof(unsigned int); i++) {
1406 *key_usage |= (unsigned int) bs.p[i] << (8*i);
1407 }
1408
1409 return 0;
1410}
1411
1412int mbedtls_x509_parse_subject_alt_name(const mbedtls_x509_buf *san_buf,
1413 mbedtls_x509_subject_alternative_name *san)
1414{
1415 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1416 switch (san_buf->tag &
1417 (MBEDTLS_ASN1_TAG_CLASS_MASK |
1418 MBEDTLS_ASN1_TAG_VALUE_MASK)) {
1419 /*
1420 * otherName
1421 */
1422 case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME):
1423 {
1424 mbedtls_x509_san_other_name other_name;
1425
1426 ret = x509_get_other_name(san_buf, &other_name);
1427 if (ret != 0) {
1428 return ret;
1429 }
1430
1431 memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name));
1432 san->type = MBEDTLS_X509_SAN_OTHER_NAME;
1433 memcpy(&san->san.other_name,
1434 &other_name, sizeof(other_name));
1435
1436 }
1437 break;
Andrzej Kurek7a05fab2023-02-13 10:03:07 -05001438 /*
1439 * uniformResourceIdentifier
1440 */
1441 case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER):
1442 {
1443 memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name));
1444 san->type = MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001445
Andrzej Kurek7a05fab2023-02-13 10:03:07 -05001446 memcpy(&san->san.unstructured_name,
1447 san_buf, sizeof(*san_buf));
1448
1449 }
1450 break;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001451 /*
1452 * dNSName
1453 */
1454 case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_DNS_NAME):
1455 {
1456 memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name));
1457 san->type = MBEDTLS_X509_SAN_DNS_NAME;
1458
1459 memcpy(&san->san.unstructured_name,
1460 san_buf, sizeof(*san_buf));
Przemek Stekielecee12f2023-02-09 14:43:49 +01001461 }
1462 break;
Przemek Stekiel0ab5b932023-05-29 16:30:50 +02001463 /*
1464 * IP address
1465 */
1466 case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_IP_ADDRESS):
1467 {
1468 memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name));
1469 san->type = MBEDTLS_X509_SAN_IP_ADDRESS;
Przemek Stekiel01cb6eb2023-06-05 16:38:13 +02001470 // Only IPv6 (16 bytes) and IPv4 (4 bytes) types are supported
1471 if (san_buf->len == 4 || san_buf->len == 16) {
1472 memcpy(&san->san.unstructured_name,
1473 san_buf, sizeof(*san_buf));
1474 } else {
1475 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1476 }
Przemek Stekiel0ab5b932023-05-29 16:30:50 +02001477 }
1478 break;
Przemek Stekielecee12f2023-02-09 14:43:49 +01001479 /*
Andrzej Kurek154a6052023-04-30 14:11:49 -04001480 * rfc822Name
Przemek Stekielecee12f2023-02-09 14:43:49 +01001481 */
1482 case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_RFC822_NAME):
1483 {
1484 memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name));
1485 san->type = MBEDTLS_X509_SAN_RFC822_NAME;
1486 memcpy(&san->san.unstructured_name, san_buf, sizeof(*san_buf));
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001487 }
1488 break;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001489 /*
Andrzej Kureke12b01d2023-01-10 06:47:38 -05001490 * directoryName
1491 */
1492 case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_DIRECTORY_NAME):
1493 {
1494 size_t name_len;
1495 unsigned char *p = san_buf->p;
1496 memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name));
1497 san->type = MBEDTLS_X509_SAN_DIRECTORY_NAME;
1498
1499 ret = mbedtls_asn1_get_tag(&p, p + san_buf->len, &name_len,
1500 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE);
1501
1502 if (ret != 0) {
Andrzej Kurekbf8ccd82023-02-13 07:13:30 -05001503 return ret;
Andrzej Kureke12b01d2023-01-10 06:47:38 -05001504 }
1505
1506 if ((ret = mbedtls_x509_get_name(&p, p + name_len,
1507 &san->san.directory_name)) != 0) {
Andrzej Kurekbf8ccd82023-02-13 07:13:30 -05001508 return ret;
Andrzej Kureke12b01d2023-01-10 06:47:38 -05001509 }
1510 }
1511 break;
1512 /*
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001513 * Type not supported
1514 */
1515 default:
1516 return MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
1517 }
1518 return 0;
1519}
1520
Andrzej Kurekd40c2b62023-02-13 07:01:59 -05001521void mbedtls_x509_free_subject_alt_name(mbedtls_x509_subject_alternative_name *san)
1522{
1523 if (san->type == MBEDTLS_X509_SAN_DIRECTORY_NAME) {
1524 mbedtls_asn1_free_named_data_list_shallow(san->san.directory_name.next);
1525 }
1526}
1527
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001528#if !defined(MBEDTLS_X509_REMOVE_INFO)
1529int mbedtls_x509_info_subject_alt_name(char **buf, size_t *size,
1530 const mbedtls_x509_sequence
1531 *subject_alt_name,
1532 const char *prefix)
1533{
1534 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1535 size_t i;
1536 size_t n = *size;
1537 char *p = *buf;
1538 const mbedtls_x509_sequence *cur = subject_alt_name;
1539 mbedtls_x509_subject_alternative_name san;
1540 int parse_ret;
1541
1542 while (cur != NULL) {
1543 memset(&san, 0, sizeof(san));
1544 parse_ret = mbedtls_x509_parse_subject_alt_name(&cur->buf, &san);
1545 if (parse_ret != 0) {
1546 if (parse_ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE) {
1547 ret = mbedtls_snprintf(p, n, "\n%s <unsupported>", prefix);
1548 MBEDTLS_X509_SAFE_SNPRINTF;
1549 } else {
1550 ret = mbedtls_snprintf(p, n, "\n%s <malformed>", prefix);
1551 MBEDTLS_X509_SAFE_SNPRINTF;
1552 }
1553 cur = cur->next;
1554 continue;
1555 }
1556
1557 switch (san.type) {
1558 /*
1559 * otherName
1560 */
1561 case MBEDTLS_X509_SAN_OTHER_NAME:
1562 {
1563 mbedtls_x509_san_other_name *other_name = &san.san.other_name;
1564
1565 ret = mbedtls_snprintf(p, n, "\n%s otherName :", prefix);
1566 MBEDTLS_X509_SAFE_SNPRINTF;
1567
1568 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ON_HW_MODULE_NAME,
1569 &other_name->value.hardware_module_name.oid) != 0) {
1570 ret = mbedtls_snprintf(p, n, "\n%s hardware module name :", prefix);
1571 MBEDTLS_X509_SAFE_SNPRINTF;
1572 ret =
1573 mbedtls_snprintf(p, n, "\n%s hardware type : ", prefix);
1574 MBEDTLS_X509_SAFE_SNPRINTF;
1575
1576 ret = mbedtls_oid_get_numeric_string(p,
1577 n,
1578 &other_name->value.hardware_module_name.oid);
1579 MBEDTLS_X509_SAFE_SNPRINTF;
1580
1581 ret =
1582 mbedtls_snprintf(p, n, "\n%s hardware serial number : ", prefix);
1583 MBEDTLS_X509_SAFE_SNPRINTF;
1584
1585 for (i = 0; i < other_name->value.hardware_module_name.val.len; i++) {
1586 ret = mbedtls_snprintf(p,
1587 n,
1588 "%02X",
1589 other_name->value.hardware_module_name.val.p[i]);
1590 MBEDTLS_X509_SAFE_SNPRINTF;
1591 }
1592 }/* MBEDTLS_OID_ON_HW_MODULE_NAME */
1593 }
1594 break;
Andrzej Kurek7a05fab2023-02-13 10:03:07 -05001595 /*
1596 * uniformResourceIdentifier
1597 */
1598 case MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER:
1599 {
1600 ret = mbedtls_snprintf(p, n, "\n%s uniformResourceIdentifier : ", prefix);
1601 MBEDTLS_X509_SAFE_SNPRINTF;
1602 if (san.san.unstructured_name.len >= n) {
Przemek Stekiel01cb6eb2023-06-05 16:38:13 +02001603 if (n > 0) {
1604 *p = '\0';
1605 }
Andrzej Kurek7a05fab2023-02-13 10:03:07 -05001606 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1607 }
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001608
Andrzej Kurek7a05fab2023-02-13 10:03:07 -05001609 memcpy(p, san.san.unstructured_name.p, san.san.unstructured_name.len);
1610 p += san.san.unstructured_name.len;
1611 n -= san.san.unstructured_name.len;
1612 }
1613 break;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001614 /*
1615 * dNSName
Przemek Stekiel5b9e4162023-02-15 12:56:37 +01001616 * RFC822 Name
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001617 */
1618 case MBEDTLS_X509_SAN_DNS_NAME:
Przemek Stekiel5b9e4162023-02-15 12:56:37 +01001619 case MBEDTLS_X509_SAN_RFC822_NAME:
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001620 {
Przemek Stekiel82d250d2023-02-15 15:00:50 +01001621 const char *dns_name = "dNSName";
1622 const char *rfc822_name = "rfc822Name";
Przemek Stekiel5b9e4162023-02-15 12:56:37 +01001623
Przemek Stekiel82d250d2023-02-15 15:00:50 +01001624 ret = mbedtls_snprintf(p, n,
1625 "\n%s %s : ",
1626 prefix,
Przemek Stekiel093c97d2023-06-02 10:11:32 +02001627 san.type ==
1628 MBEDTLS_X509_SAN_DNS_NAME ? dns_name : rfc822_name);
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001629 MBEDTLS_X509_SAFE_SNPRINTF;
1630 if (san.san.unstructured_name.len >= n) {
Przemek Stekiel01cb6eb2023-06-05 16:38:13 +02001631 if (n > 0) {
1632 *p = '\0';
1633 }
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001634 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1635 }
1636
Przemek Stekiel093c97d2023-06-02 10:11:32 +02001637 memcpy(p, san.san.unstructured_name.p, san.san.unstructured_name.len);
1638 p += san.san.unstructured_name.len;
1639 n -= san.san.unstructured_name.len;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001640 }
1641 break;
Przemek Stekiel093c97d2023-06-02 10:11:32 +02001642 /*
1643 * iPAddress
1644 */
1645 case MBEDTLS_X509_SAN_IP_ADDRESS:
1646 {
1647 ret = mbedtls_snprintf(p, n, "\n%s %s : ",
1648 prefix, "iPAddress");
1649 MBEDTLS_X509_SAFE_SNPRINTF;
1650 if (san.san.unstructured_name.len >= n) {
Przemek Stekiel01cb6eb2023-06-05 16:38:13 +02001651 if (n > 0) {
1652 *p = '\0';
1653 }
Przemek Stekiel093c97d2023-06-02 10:11:32 +02001654 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1655 }
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001656
Przemek Stekiel093c97d2023-06-02 10:11:32 +02001657 unsigned char *ip = san.san.unstructured_name.p;
1658 // Only IPv6 (16 bytes) and IPv4 (4 bytes) types are supported
1659 if (san.san.unstructured_name.len == 4) {
Przemek Stekiel4d3fc212023-06-06 11:40:32 +02001660 ret = mbedtls_snprintf(p, n, "%u.%u.%u.%u", ip[0], ip[1], ip[2], ip[3]);
1661 MBEDTLS_X509_SAFE_SNPRINTF;
Przemek Stekiel093c97d2023-06-02 10:11:32 +02001662 } else if (san.san.unstructured_name.len == 16) {
Przemek Stekiel4d3fc212023-06-06 11:40:32 +02001663 ret = mbedtls_snprintf(p, n,
Przemek Stekiel01cb6eb2023-06-05 16:38:13 +02001664 "%X%X:%X%X:%X%X:%X%X:%X%X:%X%X:%X%X:%X%X",
1665 ip[0], ip[1], ip[2], ip[3], ip[4], ip[5], ip[6],
Przemek Stekiel4d3fc212023-06-06 11:40:32 +02001666 ip[7], ip[8], ip[9], ip[10], ip[11], ip[12], ip[13],
Przemek Stekiel01cb6eb2023-06-05 16:38:13 +02001667 ip[14], ip[15]);
Przemek Stekiel4d3fc212023-06-06 11:40:32 +02001668 MBEDTLS_X509_SAFE_SNPRINTF;
Przemek Stekiel093c97d2023-06-02 10:11:32 +02001669 } else {
Przemek Stekiel01cb6eb2023-06-05 16:38:13 +02001670 if (n > 0) {
1671 *p = '\0';
1672 }
Przemek Stekiel093c97d2023-06-02 10:11:32 +02001673 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1674 }
Przemek Stekiel093c97d2023-06-02 10:11:32 +02001675 }
1676 break;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001677 /*
Andrzej Kureke12b01d2023-01-10 06:47:38 -05001678 * directoryName
1679 */
1680 case MBEDTLS_X509_SAN_DIRECTORY_NAME:
1681 {
1682 ret = mbedtls_snprintf(p, n, "\n%s directoryName : ", prefix);
Andrzej Kurek5f0c6e82023-02-27 16:03:41 -05001683 if (ret < 0 || (size_t) ret >= n) {
1684 mbedtls_x509_free_subject_alt_name(&san);
1685 }
1686
Andrzej Kureke12b01d2023-01-10 06:47:38 -05001687 MBEDTLS_X509_SAFE_SNPRINTF;
1688 ret = mbedtls_x509_dn_gets(p, n, &san.san.directory_name);
Andrzej Kurekd40c2b62023-02-13 07:01:59 -05001689
Andrzej Kureke12b01d2023-01-10 06:47:38 -05001690 if (ret < 0) {
Andrzej Kurek5f0c6e82023-02-27 16:03:41 -05001691 mbedtls_x509_free_subject_alt_name(&san);
Przemek Stekiel01cb6eb2023-06-05 16:38:13 +02001692 if (n > 0) {
1693 *p = '\0';
1694 }
Andrzej Kureke12b01d2023-01-10 06:47:38 -05001695 return ret;
1696 }
1697
1698 p += ret;
1699 n -= ret;
1700 }
1701 break;
1702 /*
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001703 * Type not supported, skip item.
1704 */
1705 default:
1706 ret = mbedtls_snprintf(p, n, "\n%s <unsupported>", prefix);
1707 MBEDTLS_X509_SAFE_SNPRINTF;
1708 break;
1709 }
1710
Andrzej Kurekd40c2b62023-02-13 07:01:59 -05001711 /* So far memory is freed only in the case of directoryName
Andrzej Kurek5f0c6e82023-02-27 16:03:41 -05001712 * parsing succeeding, as mbedtls_x509_get_name allocates memory. */
Andrzej Kurekd40c2b62023-02-13 07:01:59 -05001713 mbedtls_x509_free_subject_alt_name(&san);
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001714 cur = cur->next;
1715 }
1716
1717 *p = '\0';
1718
1719 *size = n;
1720 *buf = p;
1721
1722 return 0;
1723}
1724
Demi Marie Obenour690b8c92022-12-04 04:24:22 -05001725#define PRINT_ITEM(i) \
1726 do { \
1727 ret = mbedtls_snprintf(p, n, "%s" i, sep); \
1728 MBEDTLS_X509_SAFE_SNPRINTF; \
1729 sep = ", "; \
1730 } while (0)
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001731
Demi Marie Obenour690b8c92022-12-04 04:24:22 -05001732#define CERT_TYPE(type, name) \
1733 do { \
1734 if (ns_cert_type & (type)) { \
1735 PRINT_ITEM(name); \
1736 } \
1737 } while (0)
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001738
1739int mbedtls_x509_info_cert_type(char **buf, size_t *size,
1740 unsigned char ns_cert_type)
1741{
1742 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1743 size_t n = *size;
1744 char *p = *buf;
1745 const char *sep = "";
1746
1747 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT, "SSL Client");
1748 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER, "SSL Server");
1749 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_EMAIL, "Email");
1750 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing");
1751 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_RESERVED, "Reserved");
1752 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_SSL_CA, "SSL CA");
1753 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA, "Email CA");
1754 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA");
1755
1756 *size = n;
1757 *buf = p;
1758
1759 return 0;
1760}
1761
Demi Marie Obenour690b8c92022-12-04 04:24:22 -05001762#define KEY_USAGE(code, name) \
1763 do { \
1764 if ((key_usage) & (code)) { \
1765 PRINT_ITEM(name); \
1766 } \
1767 } while (0)
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001768
1769int mbedtls_x509_info_key_usage(char **buf, size_t *size,
1770 unsigned int key_usage)
1771{
1772 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1773 size_t n = *size;
1774 char *p = *buf;
1775 const char *sep = "";
1776
1777 KEY_USAGE(MBEDTLS_X509_KU_DIGITAL_SIGNATURE, "Digital Signature");
1778 KEY_USAGE(MBEDTLS_X509_KU_NON_REPUDIATION, "Non Repudiation");
1779 KEY_USAGE(MBEDTLS_X509_KU_KEY_ENCIPHERMENT, "Key Encipherment");
1780 KEY_USAGE(MBEDTLS_X509_KU_DATA_ENCIPHERMENT, "Data Encipherment");
1781 KEY_USAGE(MBEDTLS_X509_KU_KEY_AGREEMENT, "Key Agreement");
1782 KEY_USAGE(MBEDTLS_X509_KU_KEY_CERT_SIGN, "Key Cert Sign");
1783 KEY_USAGE(MBEDTLS_X509_KU_CRL_SIGN, "CRL Sign");
1784 KEY_USAGE(MBEDTLS_X509_KU_ENCIPHER_ONLY, "Encipher Only");
1785 KEY_USAGE(MBEDTLS_X509_KU_DECIPHER_ONLY, "Decipher Only");
1786
1787 *size = n;
1788 *buf = p;
1789
1790 return 0;
1791}
1792#endif /* MBEDTLS_X509_REMOVE_INFO */
1793#endif /* MBEDTLS_X509_CRT_PARSE_C || MBEDTLS_X509_CSR_PARSE_C */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001794#endif /* MBEDTLS_X509_USE_C */