blob: 9f0dc6253bda602ff8d1565e80d6bfd6974e91a8 [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
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/platform.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020047
Simon Butcherb5b6af22016-07-13 14:46:18 +010048#if defined(MBEDTLS_HAVE_TIME)
49#include "mbedtls/platform_time.h"
50#endif
Nicholas Wilson512b4ee2017-12-05 12:07:33 +000051#if defined(MBEDTLS_HAVE_TIME_DATE)
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +010052#include "mbedtls/platform_util.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020053#include <time.h>
54#endif
55
Manuel Pégourié-Gonnard07018f92022-09-15 11:29:35 +020056#include "mbedtls/legacy_or_psa.h"
Przemek Stekielfd183662022-08-02 15:29:20 +020057
Gilles Peskine449bd832023-01-11 14:50:10 +010058#define CHECK(code) if ((ret = (code)) != 0) { return ret; }
Hanno Becker1eeca412018-10-15 12:01:35 +010059#define CHECK_RANGE(min, max, val) \
60 do \
61 { \
Gilles Peskine449bd832023-01-11 14:50:10 +010062 if ((val) < (min) || (val) > (max)) \
Hanno Becker1eeca412018-10-15 12:01:35 +010063 { \
Gilles Peskine449bd832023-01-11 14:50:10 +010064 return ret; \
Hanno Becker1eeca412018-10-15 12:01:35 +010065 } \
Gilles Peskine449bd832023-01-11 14:50:10 +010066 } while (0)
Rich Evans7d5a55a2015-02-13 11:48:02 +000067
Paul Bakker7c6b2c32013-09-16 13:49:26 +020068/*
69 * CertificateSerialNumber ::= INTEGER
70 */
Gilles Peskine449bd832023-01-11 14:50:10 +010071int mbedtls_x509_get_serial(unsigned char **p, const unsigned char *end,
72 mbedtls_x509_buf *serial)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020073{
Janos Follath865b3eb2019-12-16 11:46:15 +000074 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020075
Gilles Peskine449bd832023-01-11 14:50:10 +010076 if ((end - *p) < 1) {
77 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_SERIAL,
78 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
79 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +020080
Gilles Peskine449bd832023-01-11 14:50:10 +010081 if (**p != (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_PRIMITIVE | 2) &&
82 **p != MBEDTLS_ASN1_INTEGER) {
83 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_SERIAL,
84 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
85 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +020086
87 serial->tag = *(*p)++;
88
Gilles Peskine449bd832023-01-11 14:50:10 +010089 if ((ret = mbedtls_asn1_get_len(p, end, &serial->len)) != 0) {
90 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_SERIAL, ret);
91 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +020092
93 serial->p = *p;
94 *p += serial->len;
95
Gilles Peskine449bd832023-01-11 14:50:10 +010096 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020097}
98
99/* Get an algorithm identifier without parameters (eg for signatures)
100 *
101 * AlgorithmIdentifier ::= SEQUENCE {
102 * algorithm OBJECT IDENTIFIER,
103 * parameters ANY DEFINED BY algorithm OPTIONAL }
104 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100105int mbedtls_x509_get_alg_null(unsigned char **p, const unsigned char *end,
106 mbedtls_x509_buf *alg)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200107{
Janos Follath865b3eb2019-12-16 11:46:15 +0000108 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200109
Gilles Peskine449bd832023-01-11 14:50:10 +0100110 if ((ret = mbedtls_asn1_get_alg_null(p, end, alg)) != 0) {
111 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
112 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200113
Gilles Peskine449bd832023-01-11 14:50:10 +0100114 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200115}
116
117/*
Antonin Décimo36e89b52019-01-23 15:24:37 +0100118 * Parse an algorithm identifier with (optional) parameters
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100119 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100120int mbedtls_x509_get_alg(unsigned char **p, const unsigned char *end,
121 mbedtls_x509_buf *alg, mbedtls_x509_buf *params)
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100122{
Janos Follath865b3eb2019-12-16 11:46:15 +0000123 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100124
Gilles Peskine449bd832023-01-11 14:50:10 +0100125 if ((ret = mbedtls_asn1_get_alg(p, end, alg, params)) != 0) {
126 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
127 }
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100128
Gilles Peskine449bd832023-01-11 14:50:10 +0100129 return 0;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100130}
131
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200132/*
133 * Convert md type to string
134 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100135static inline const char *md_type_to_string(mbedtls_md_type_t md_alg)
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200136{
Gilles Peskine449bd832023-01-11 14:50:10 +0100137 switch (md_alg) {
Przemek Stekielfd183662022-08-02 15:29:20 +0200138#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +0100139 case MBEDTLS_MD_MD5:
140 return "MD5";
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200141#endif
Przemek Stekielfd183662022-08-02 15:29:20 +0200142#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +0100143 case MBEDTLS_MD_SHA1:
144 return "SHA1";
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200145#endif
Przemek Stekielfd183662022-08-02 15:29:20 +0200146#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +0100147 case MBEDTLS_MD_SHA224:
148 return "SHA224";
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200149#endif
Przemek Stekielfd183662022-08-02 15:29:20 +0200150#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +0100151 case MBEDTLS_MD_SHA256:
152 return "SHA256";
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200153#endif
Przemek Stekielfd183662022-08-02 15:29:20 +0200154#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +0100155 case MBEDTLS_MD_SHA384:
156 return "SHA384";
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200157#endif
Przemek Stekielfd183662022-08-02 15:29:20 +0200158#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +0100159 case MBEDTLS_MD_SHA512:
160 return "SHA512";
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200161#endif
Przemek Stekielfd183662022-08-02 15:29:20 +0200162#if defined(MBEDTLS_HAS_ALG_RIPEMD160_VIA_MD_OR_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +0100163 case MBEDTLS_MD_RIPEMD160:
164 return "RIPEMD160";
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200165#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100166 case MBEDTLS_MD_NONE:
167 return NULL;
168 default:
169 return NULL;
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200170 }
171}
172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200173#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100174/*
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100175 * HashAlgorithm ::= AlgorithmIdentifier
176 *
177 * AlgorithmIdentifier ::= SEQUENCE {
178 * algorithm OBJECT IDENTIFIER,
179 * parameters ANY DEFINED BY algorithm OPTIONAL }
180 *
181 * For HashAlgorithm, parameters MUST be NULL or absent.
182 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100183static 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 +0100184{
Janos Follath865b3eb2019-12-16 11:46:15 +0000185 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100186 unsigned char *p;
187 const unsigned char *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188 mbedtls_x509_buf md_oid;
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100189 size_t len;
190
191 /* Make sure we got a SEQUENCE and setup bounds */
Gilles Peskine449bd832023-01-11 14:50:10 +0100192 if (alg->tag != (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) {
193 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
194 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
195 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100196
Andrzej Kurekfeaebc52020-07-16 04:37:41 -0400197 p = alg->p;
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100198 end = p + alg->len;
199
Gilles Peskine449bd832023-01-11 14:50:10 +0100200 if (p >= end) {
201 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
202 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
203 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100204
205 /* Parse md_oid */
206 md_oid.tag = *p;
207
Gilles Peskine449bd832023-01-11 14:50:10 +0100208 if ((ret = mbedtls_asn1_get_tag(&p, end, &md_oid.len, MBEDTLS_ASN1_OID)) != 0) {
209 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
210 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100211
212 md_oid.p = p;
213 p += md_oid.len;
214
215 /* Get md_alg from md_oid */
Gilles Peskine449bd832023-01-11 14:50:10 +0100216 if ((ret = mbedtls_oid_get_md_alg(&md_oid, md_alg)) != 0) {
217 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
218 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100219
220 /* Make sure params is absent of NULL */
Gilles Peskine449bd832023-01-11 14:50:10 +0100221 if (p == end) {
222 return 0;
223 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100224
Gilles Peskine449bd832023-01-11 14:50:10 +0100225 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_NULL)) != 0 || len != 0) {
226 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
227 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100228
Gilles Peskine449bd832023-01-11 14:50:10 +0100229 if (p != end) {
230 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
231 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
232 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100233
Gilles Peskine449bd832023-01-11 14:50:10 +0100234 return 0;
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100235}
236
237/*
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100238 * RSASSA-PSS-params ::= SEQUENCE {
239 * hashAlgorithm [0] HashAlgorithm DEFAULT sha1Identifier,
240 * maskGenAlgorithm [1] MaskGenAlgorithm DEFAULT mgf1SHA1Identifier,
241 * saltLength [2] INTEGER DEFAULT 20,
242 * trailerField [3] INTEGER DEFAULT 1 }
243 * -- Note that the tags in this Sequence are explicit.
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200244 *
245 * RFC 4055 (which defines use of RSASSA-PSS in PKIX) states that the value
246 * of trailerField MUST be 1, and PKCS#1 v2.2 doesn't even define any other
Tom Cosgrove1797b052022-12-04 17:19:59 +0000247 * option. Enforce this at parsing time.
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100248 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100249int mbedtls_x509_get_rsassa_pss_params(const mbedtls_x509_buf *params,
250 mbedtls_md_type_t *md_alg, mbedtls_md_type_t *mgf_md,
251 int *salt_len)
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100252{
Janos Follath865b3eb2019-12-16 11:46:15 +0000253 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100254 unsigned char *p;
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100255 const unsigned char *end, *end2;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100256 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257 mbedtls_x509_buf alg_id, alg_params;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100258
259 /* First set everything to defaults */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260 *md_alg = MBEDTLS_MD_SHA1;
261 *mgf_md = MBEDTLS_MD_SHA1;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100262 *salt_len = 20;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100263
264 /* Make sure params is a SEQUENCE and setup bounds */
Gilles Peskine449bd832023-01-11 14:50:10 +0100265 if (params->tag != (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) {
266 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
267 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
268 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100269
270 p = (unsigned char *) params->p;
271 end = p + params->len;
272
Gilles Peskine449bd832023-01-11 14:50:10 +0100273 if (p == end) {
274 return 0;
275 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100276
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100277 /*
278 * HashAlgorithm
279 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100280 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
281 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
282 0)) == 0) {
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100283 end2 = p + len;
284
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100285 /* HashAlgorithm ::= AlgorithmIdentifier (without parameters) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100286 if ((ret = mbedtls_x509_get_alg_null(&p, end2, &alg_id)) != 0) {
287 return ret;
288 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100289
Gilles Peskine449bd832023-01-11 14:50:10 +0100290 if ((ret = mbedtls_oid_get_md_alg(&alg_id, md_alg)) != 0) {
291 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
292 }
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100293
Gilles Peskine449bd832023-01-11 14:50:10 +0100294 if (p != end2) {
295 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
296 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
297 }
298 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
299 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100300 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100301
Gilles Peskine449bd832023-01-11 14:50:10 +0100302 if (p == end) {
303 return 0;
304 }
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100305
306 /*
307 * MaskGenAlgorithm
308 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100309 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
310 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
311 1)) == 0) {
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100312 end2 = p + len;
313
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100314 /* MaskGenAlgorithm ::= AlgorithmIdentifier (params = HashAlgorithm) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100315 if ((ret = mbedtls_x509_get_alg(&p, end2, &alg_id, &alg_params)) != 0) {
316 return ret;
317 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100318
319 /* Only MFG1 is recognised for now */
Gilles Peskine449bd832023-01-11 14:50:10 +0100320 if (MBEDTLS_OID_CMP(MBEDTLS_OID_MGF1, &alg_id) != 0) {
321 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE,
322 MBEDTLS_ERR_OID_NOT_FOUND);
323 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100324
325 /* Parse HashAlgorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +0100326 if ((ret = x509_get_hash_alg(&alg_params, mgf_md)) != 0) {
327 return ret;
328 }
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100329
Gilles Peskine449bd832023-01-11 14:50:10 +0100330 if (p != end2) {
331 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
332 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
333 }
334 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
335 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100336 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100337
Gilles Peskine449bd832023-01-11 14:50:10 +0100338 if (p == end) {
339 return 0;
340 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100341
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100342 /*
343 * salt_len
344 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100345 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
346 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
347 2)) == 0) {
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100348 end2 = p + len;
349
Gilles Peskine449bd832023-01-11 14:50:10 +0100350 if ((ret = mbedtls_asn1_get_int(&p, end2, salt_len)) != 0) {
351 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
352 }
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100353
Gilles Peskine449bd832023-01-11 14:50:10 +0100354 if (p != end2) {
355 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
356 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
357 }
358 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
359 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100360 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100361
Gilles Peskine449bd832023-01-11 14:50:10 +0100362 if (p == end) {
363 return 0;
364 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100365
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100366 /*
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200367 * trailer_field (if present, must be 1)
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100368 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100369 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
370 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
371 3)) == 0) {
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200372 int trailer_field;
373
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100374 end2 = p + len;
375
Gilles Peskine449bd832023-01-11 14:50:10 +0100376 if ((ret = mbedtls_asn1_get_int(&p, end2, &trailer_field)) != 0) {
377 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
378 }
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100379
Gilles Peskine449bd832023-01-11 14:50:10 +0100380 if (p != end2) {
381 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
382 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
383 }
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200384
Gilles Peskine449bd832023-01-11 14:50:10 +0100385 if (trailer_field != 1) {
386 return MBEDTLS_ERR_X509_INVALID_ALG;
387 }
388 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
389 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100390 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100391
Gilles Peskine449bd832023-01-11 14:50:10 +0100392 if (p != end) {
393 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
394 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
395 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100396
Gilles Peskine449bd832023-01-11 14:50:10 +0100397 return 0;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100398}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200399#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100400
401/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200402 * AttributeTypeAndValue ::= SEQUENCE {
403 * type AttributeType,
404 * value AttributeValue }
405 *
406 * AttributeType ::= OBJECT IDENTIFIER
407 *
408 * AttributeValue ::= ANY DEFINED BY AttributeType
409 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100410static int x509_get_attr_type_value(unsigned char **p,
411 const unsigned char *end,
412 mbedtls_x509_name *cur)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200413{
Janos Follath865b3eb2019-12-16 11:46:15 +0000414 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200415 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200416 mbedtls_x509_buf *oid;
417 mbedtls_x509_buf *val;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200418
Gilles Peskine449bd832023-01-11 14:50:10 +0100419 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
420 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
421 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME, ret);
422 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200423
Hanno Becker12f62fb2019-02-12 17:22:36 +0000424 end = *p + len;
425
Gilles Peskine449bd832023-01-11 14:50:10 +0100426 if ((end - *p) < 1) {
427 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME,
428 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
429 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200430
431 oid = &cur->oid;
432 oid->tag = **p;
433
Gilles Peskine449bd832023-01-11 14:50:10 +0100434 if ((ret = mbedtls_asn1_get_tag(p, end, &oid->len, MBEDTLS_ASN1_OID)) != 0) {
435 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME, ret);
436 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200437
438 oid->p = *p;
439 *p += oid->len;
440
Gilles Peskine449bd832023-01-11 14:50:10 +0100441 if ((end - *p) < 1) {
442 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME,
443 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
444 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200445
Gilles Peskine449bd832023-01-11 14:50:10 +0100446 if (**p != MBEDTLS_ASN1_BMP_STRING && **p != MBEDTLS_ASN1_UTF8_STRING &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200447 **p != MBEDTLS_ASN1_T61_STRING && **p != MBEDTLS_ASN1_PRINTABLE_STRING &&
448 **p != MBEDTLS_ASN1_IA5_STRING && **p != MBEDTLS_ASN1_UNIVERSAL_STRING &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100449 **p != MBEDTLS_ASN1_BIT_STRING) {
450 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME,
451 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
452 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200453
454 val = &cur->val;
455 val->tag = *(*p)++;
456
Gilles Peskine449bd832023-01-11 14:50:10 +0100457 if ((ret = mbedtls_asn1_get_len(p, end, &val->len)) != 0) {
458 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME, ret);
459 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200460
461 val->p = *p;
462 *p += val->len;
463
Gilles Peskine449bd832023-01-11 14:50:10 +0100464 if (*p != end) {
465 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME,
466 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Hanno Becker12f62fb2019-02-12 17:22:36 +0000467 }
468
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200469 cur->next = NULL;
470
Gilles Peskine449bd832023-01-11 14:50:10 +0100471 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200472}
473
474/*
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000475 * Name ::= CHOICE { -- only one possibility for now --
476 * rdnSequence RDNSequence }
477 *
478 * RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
479 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200480 * RelativeDistinguishedName ::=
481 * SET OF AttributeTypeAndValue
482 *
483 * AttributeTypeAndValue ::= SEQUENCE {
484 * type AttributeType,
485 * value AttributeValue }
486 *
487 * AttributeType ::= OBJECT IDENTIFIER
488 *
489 * AttributeValue ::= ANY DEFINED BY AttributeType
Manuel Pégourié-Gonnard5d861852014-10-17 12:41:41 +0200490 *
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000491 * The data structure is optimized for the common case where each RDN has only
492 * one element, which is represented as a list of AttributeTypeAndValue.
493 * For the general case we still use a flat list, but we mark elements of the
494 * same set so that they are "merged" together in the functions that consume
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200495 * this list, eg mbedtls_x509_dn_gets().
David Horstmanned794832022-10-04 18:12:06 +0100496 *
David Horstmann11307a12022-10-17 18:10:23 +0100497 * On success, this function may allocate a linked list starting at cur->next
David Horstmanned794832022-10-04 18:12:06 +0100498 * that must later be free'd by the caller using mbedtls_free(). In error
499 * cases, this function frees all allocated memory internally and the caller
500 * has no freeing responsibilities.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200501 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100502int mbedtls_x509_get_name(unsigned char **p, const unsigned char *end,
503 mbedtls_x509_name *cur)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200504{
Janos Follath865b3eb2019-12-16 11:46:15 +0000505 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard5d861852014-10-17 12:41:41 +0200506 size_t set_len;
507 const unsigned char *end_set;
David Horstmanned794832022-10-04 18:12:06 +0100508 mbedtls_x509_name *head = cur;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200509
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100510 /* don't use recursion, we'd risk stack overflow if not optimized */
Gilles Peskine449bd832023-01-11 14:50:10 +0100511 while (1) {
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100512 /*
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000513 * parse SET
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100514 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100515 if ((ret = mbedtls_asn1_get_tag(p, end, &set_len,
516 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SET)) != 0) {
517 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME, ret);
David Horstmanned794832022-10-04 18:12:06 +0100518 goto error;
519 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200520
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100521 end_set = *p + set_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200522
Gilles Peskine449bd832023-01-11 14:50:10 +0100523 while (1) {
524 if ((ret = x509_get_attr_type_value(p, end_set, cur)) != 0) {
David Horstmanned794832022-10-04 18:12:06 +0100525 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +0100526 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200527
Gilles Peskine449bd832023-01-11 14:50:10 +0100528 if (*p == end_set) {
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000529 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100530 }
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000531
Manuel Pégourié-Gonnardeecb43c2015-05-12 12:56:41 +0200532 /* Mark this item as being no the only one in a set */
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000533 cur->next_merged = 1;
534
Gilles Peskine449bd832023-01-11 14:50:10 +0100535 cur->next = mbedtls_calloc(1, sizeof(mbedtls_x509_name));
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000536
Gilles Peskine449bd832023-01-11 14:50:10 +0100537 if (cur->next == NULL) {
David Horstmanned794832022-10-04 18:12:06 +0100538 ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
539 goto error;
540 }
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000541
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000542 cur = cur->next;
543 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200544
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100545 /*
546 * continue until end of SEQUENCE is reached
547 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100548 if (*p == end) {
549 return 0;
550 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200551
Gilles Peskine449bd832023-01-11 14:50:10 +0100552 cur->next = mbedtls_calloc(1, sizeof(mbedtls_x509_name));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200553
Gilles Peskine449bd832023-01-11 14:50:10 +0100554 if (cur->next == NULL) {
David Horstmanned794832022-10-04 18:12:06 +0100555 ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
556 goto error;
557 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200558
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100559 cur = cur->next;
560 }
David Horstmanned794832022-10-04 18:12:06 +0100561
562error:
David Horstmanned794832022-10-04 18:12:06 +0100563 /* Skip the first element as we did not allocate it */
Gilles Peskine449bd832023-01-11 14:50:10 +0100564 mbedtls_asn1_free_named_data_list_shallow(head->next);
Glenn Straussa4b40412022-06-26 19:32:09 -0400565 head->next = NULL;
David Horstmanned794832022-10-04 18:12:06 +0100566
Gilles Peskine449bd832023-01-11 14:50:10 +0100567 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200568}
569
Gilles Peskine449bd832023-01-11 14:50:10 +0100570static int x509_parse_int(unsigned char **p, size_t n, int *res)
Janos Follath87c98072017-02-03 12:36:59 +0000571{
Rich Evans7d5a55a2015-02-13 11:48:02 +0000572 *res = 0;
Janos Follath87c98072017-02-03 12:36:59 +0000573
Gilles Peskine449bd832023-01-11 14:50:10 +0100574 for (; n > 0; --n) {
575 if ((**p < '0') || (**p > '9')) {
576 return MBEDTLS_ERR_X509_INVALID_DATE;
577 }
Janos Follath87c98072017-02-03 12:36:59 +0000578
Rich Evans7d5a55a2015-02-13 11:48:02 +0000579 *res *= 10;
Gilles Peskine449bd832023-01-11 14:50:10 +0100580 *res += (*(*p)++ - '0');
Rich Evans7d5a55a2015-02-13 11:48:02 +0000581 }
Janos Follath87c98072017-02-03 12:36:59 +0000582
Gilles Peskine449bd832023-01-11 14:50:10 +0100583 return 0;
Rich Evans7d5a55a2015-02-13 11:48:02 +0000584}
585
Gilles Peskine449bd832023-01-11 14:50:10 +0100586static int x509_date_is_valid(const mbedtls_x509_time *t)
Andres AG4b76aec2016-09-23 13:16:02 +0100587{
588 int ret = MBEDTLS_ERR_X509_INVALID_DATE;
Andres Amaya Garcia735b37e2016-11-21 15:38:02 +0000589 int month_len;
Andres AG4b76aec2016-09-23 13:16:02 +0100590
Gilles Peskine449bd832023-01-11 14:50:10 +0100591 CHECK_RANGE(0, 9999, t->year);
592 CHECK_RANGE(0, 23, t->hour);
593 CHECK_RANGE(0, 59, t->min);
594 CHECK_RANGE(0, 59, t->sec);
Andres AG4b76aec2016-09-23 13:16:02 +0100595
Gilles Peskine449bd832023-01-11 14:50:10 +0100596 switch (t->mon) {
Andres AG4b76aec2016-09-23 13:16:02 +0100597 case 1: case 3: case 5: case 7: case 8: case 10: case 12:
Andres Amaya Garcia735b37e2016-11-21 15:38:02 +0000598 month_len = 31;
Andres AG4b76aec2016-09-23 13:16:02 +0100599 break;
600 case 4: case 6: case 9: case 11:
Andres Amaya Garcia735b37e2016-11-21 15:38:02 +0000601 month_len = 30;
Andres AG4b76aec2016-09-23 13:16:02 +0100602 break;
603 case 2:
Gilles Peskine449bd832023-01-11 14:50:10 +0100604 if ((!(t->year % 4) && t->year % 100) ||
605 !(t->year % 400)) {
Andres Amaya Garcia735b37e2016-11-21 15:38:02 +0000606 month_len = 29;
Gilles Peskine449bd832023-01-11 14:50:10 +0100607 } else {
Andres Amaya Garcia735b37e2016-11-21 15:38:02 +0000608 month_len = 28;
Gilles Peskine449bd832023-01-11 14:50:10 +0100609 }
Andres AG4b76aec2016-09-23 13:16:02 +0100610 break;
611 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100612 return ret;
Andres AG4b76aec2016-09-23 13:16:02 +0100613 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100614 CHECK_RANGE(1, month_len, t->day);
Andres AG4b76aec2016-09-23 13:16:02 +0100615
Gilles Peskine449bd832023-01-11 14:50:10 +0100616 return 0;
Andres AG4b76aec2016-09-23 13:16:02 +0100617}
618
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200619/*
Janos Follath87c98072017-02-03 12:36:59 +0000620 * Parse an ASN1_UTC_TIME (yearlen=2) or ASN1_GENERALIZED_TIME (yearlen=4)
621 * field.
622 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100623static int x509_parse_time(unsigned char **p, size_t len, size_t yearlen,
624 mbedtls_x509_time *tm)
Janos Follath87c98072017-02-03 12:36:59 +0000625{
Janos Follath865b3eb2019-12-16 11:46:15 +0000626 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Janos Follath87c98072017-02-03 12:36:59 +0000627
628 /*
629 * Minimum length is 10 or 12 depending on yearlen
630 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100631 if (len < yearlen + 8) {
632 return MBEDTLS_ERR_X509_INVALID_DATE;
633 }
Janos Follath87c98072017-02-03 12:36:59 +0000634 len -= yearlen + 8;
635
636 /*
637 * Parse year, month, day, hour, minute
638 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100639 CHECK(x509_parse_int(p, yearlen, &tm->year));
640 if (2 == yearlen) {
641 if (tm->year < 50) {
Hanno Becker61937d42017-04-26 15:01:23 +0100642 tm->year += 100;
Gilles Peskine449bd832023-01-11 14:50:10 +0100643 }
Janos Follath87c98072017-02-03 12:36:59 +0000644
Hanno Becker61937d42017-04-26 15:01:23 +0100645 tm->year += 1900;
Janos Follath87c98072017-02-03 12:36:59 +0000646 }
647
Gilles Peskine449bd832023-01-11 14:50:10 +0100648 CHECK(x509_parse_int(p, 2, &tm->mon));
649 CHECK(x509_parse_int(p, 2, &tm->day));
650 CHECK(x509_parse_int(p, 2, &tm->hour));
651 CHECK(x509_parse_int(p, 2, &tm->min));
Janos Follath87c98072017-02-03 12:36:59 +0000652
653 /*
654 * Parse seconds if present
655 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100656 if (len >= 2) {
657 CHECK(x509_parse_int(p, 2, &tm->sec));
Janos Follath87c98072017-02-03 12:36:59 +0000658 len -= 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100659 } else {
660 return MBEDTLS_ERR_X509_INVALID_DATE;
Janos Follath87c98072017-02-03 12:36:59 +0000661 }
Janos Follath87c98072017-02-03 12:36:59 +0000662
663 /*
664 * Parse trailing 'Z' if present
665 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100666 if (1 == len && 'Z' == **p) {
Janos Follath87c98072017-02-03 12:36:59 +0000667 (*p)++;
668 len--;
669 }
670
671 /*
672 * We should have parsed all characters at this point
673 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100674 if (0 != len) {
675 return MBEDTLS_ERR_X509_INVALID_DATE;
676 }
Janos Follath87c98072017-02-03 12:36:59 +0000677
Gilles Peskine449bd832023-01-11 14:50:10 +0100678 CHECK(x509_date_is_valid(tm));
Janos Follath87c98072017-02-03 12:36:59 +0000679
Gilles Peskine449bd832023-01-11 14:50:10 +0100680 return 0;
Janos Follath87c98072017-02-03 12:36:59 +0000681}
682
683/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200684 * Time ::= CHOICE {
685 * utcTime UTCTime,
686 * generalTime GeneralizedTime }
687 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100688int mbedtls_x509_get_time(unsigned char **p, const unsigned char *end,
689 mbedtls_x509_time *tm)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200690{
Janos Follath865b3eb2019-12-16 11:46:15 +0000691 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Janos Follath87c98072017-02-03 12:36:59 +0000692 size_t len, year_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200693 unsigned char tag;
694
Gilles Peskine449bd832023-01-11 14:50:10 +0100695 if ((end - *p) < 1) {
696 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE,
697 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
698 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200699
700 tag = **p;
701
Gilles Peskine449bd832023-01-11 14:50:10 +0100702 if (tag == MBEDTLS_ASN1_UTC_TIME) {
Janos Follath87c98072017-02-03 12:36:59 +0000703 year_len = 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100704 } else if (tag == MBEDTLS_ASN1_GENERALIZED_TIME) {
Janos Follath87c98072017-02-03 12:36:59 +0000705 year_len = 4;
Gilles Peskine449bd832023-01-11 14:50:10 +0100706 } else {
707 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE,
708 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
709 }
Janos Follath87c98072017-02-03 12:36:59 +0000710
711 (*p)++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100712 ret = mbedtls_asn1_get_len(p, end, &len);
Janos Follath87c98072017-02-03 12:36:59 +0000713
Gilles Peskine449bd832023-01-11 14:50:10 +0100714 if (ret != 0) {
715 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE, ret);
716 }
Janos Follath87c98072017-02-03 12:36:59 +0000717
Gilles Peskine449bd832023-01-11 14:50:10 +0100718 return x509_parse_time(p, len, year_len, tm);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200719}
720
Gilles Peskine449bd832023-01-11 14:50:10 +0100721int mbedtls_x509_get_sig(unsigned char **p, const unsigned char *end, mbedtls_x509_buf *sig)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200722{
Janos Follath865b3eb2019-12-16 11:46:15 +0000723 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200724 size_t len;
Andres AG4bdbe092016-09-19 16:58:45 +0100725 int tag_type;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200726
Gilles Peskine449bd832023-01-11 14:50:10 +0100727 if ((end - *p) < 1) {
728 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_SIGNATURE,
729 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
730 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200731
Andres AG4bdbe092016-09-19 16:58:45 +0100732 tag_type = **p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200733
Gilles Peskine449bd832023-01-11 14:50:10 +0100734 if ((ret = mbedtls_asn1_get_bitstring_null(p, end, &len)) != 0) {
735 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_SIGNATURE, ret);
736 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200737
Andres AG4bdbe092016-09-19 16:58:45 +0100738 sig->tag = tag_type;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200739 sig->len = len;
740 sig->p = *p;
741
742 *p += len;
743
Gilles Peskine449bd832023-01-11 14:50:10 +0100744 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200745}
746
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100747/*
748 * Get signature algorithm from alg OID and optional parameters
749 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100750int mbedtls_x509_get_sig_alg(const mbedtls_x509_buf *sig_oid, const mbedtls_x509_buf *sig_params,
751 mbedtls_md_type_t *md_alg, mbedtls_pk_type_t *pk_alg,
752 void **sig_opts)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200753{
Janos Follath865b3eb2019-12-16 11:46:15 +0000754 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200755
Gilles Peskine449bd832023-01-11 14:50:10 +0100756 if (*sig_opts != NULL) {
757 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
758 }
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200759
Gilles Peskine449bd832023-01-11 14:50:10 +0100760 if ((ret = mbedtls_oid_get_sig_alg(sig_oid, md_alg, pk_alg)) != 0) {
761 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG, ret);
762 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200764#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100765 if (*pk_alg == MBEDTLS_PK_RSASSA_PSS) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200766 mbedtls_pk_rsassa_pss_options *pss_opts;
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100767
Gilles Peskine449bd832023-01-11 14:50:10 +0100768 pss_opts = mbedtls_calloc(1, sizeof(mbedtls_pk_rsassa_pss_options));
769 if (pss_opts == NULL) {
770 return MBEDTLS_ERR_X509_ALLOC_FAILED;
771 }
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200772
Gilles Peskine449bd832023-01-11 14:50:10 +0100773 ret = mbedtls_x509_get_rsassa_pss_params(sig_params,
774 md_alg,
775 &pss_opts->mgf1_hash_id,
776 &pss_opts->expected_salt_len);
777 if (ret != 0) {
778 mbedtls_free(pss_opts);
779 return ret;
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200780 }
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100781
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200782 *sig_opts = (void *) pss_opts;
Gilles Peskine449bd832023-01-11 14:50:10 +0100783 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200784#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100785 {
786 /* Make sure parameters are absent or NULL */
Gilles Peskine449bd832023-01-11 14:50:10 +0100787 if ((sig_params->tag != MBEDTLS_ASN1_NULL && sig_params->tag != 0) ||
788 sig_params->len != 0) {
789 return MBEDTLS_ERR_X509_INVALID_ALG;
790 }
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100791 }
792
Gilles Peskine449bd832023-01-11 14:50:10 +0100793 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200794}
795
796/*
797 * X.509 Extensions (No parsing of extensions, pointer should
Brian J Murray1903fb32016-11-06 04:45:15 -0800798 * be either manually updated or extensions should be parsed!)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200799 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100800int mbedtls_x509_get_ext(unsigned char **p, const unsigned char *end,
801 mbedtls_x509_buf *ext, int tag)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200802{
Janos Follath865b3eb2019-12-16 11:46:15 +0000803 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200804 size_t len;
805
Hanno Becker3cddba82019-02-11 14:33:36 +0000806 /* Extension structure use EXPLICIT tagging. That is, the actual
807 * `Extensions` structure is wrapped by a tag-length pair using
808 * the respective context-specific tag. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100809 ret = mbedtls_asn1_get_tag(p, end, &ext->len,
810 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | tag);
811 if (ret != 0) {
812 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
813 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200814
Hanno Becker6ccfb182019-02-12 11:52:10 +0000815 ext->tag = MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | tag;
816 ext->p = *p;
817 end = *p + ext->len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200818
819 /*
820 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200821 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100822 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
823 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
824 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
825 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200826
Gilles Peskine449bd832023-01-11 14:50:10 +0100827 if (end != *p + len) {
828 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
829 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
830 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200831
Gilles Peskine449bd832023-01-11 14:50:10 +0100832 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200833}
834
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200835/*
836 * Store the name in printable form into buf; no more
837 * than size characters will be written
838 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100839int mbedtls_x509_dn_gets(char *buf, size_t size, const mbedtls_x509_name *dn)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200840{
Janos Follath865b3eb2019-12-16 11:46:15 +0000841 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Werner Lewisb33dacd2022-05-20 12:48:46 +0100842 size_t i, j, n;
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000843 unsigned char c, merge = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200844 const mbedtls_x509_name *name;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200845 const char *short_name = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200846 char s[MBEDTLS_X509_MAX_DN_NAME_SIZE], *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200847
Gilles Peskine449bd832023-01-11 14:50:10 +0100848 memset(s, 0, sizeof(s));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200849
850 name = dn;
851 p = buf;
852 n = size;
853
Gilles Peskine449bd832023-01-11 14:50:10 +0100854 while (name != NULL) {
855 if (!name->oid.p) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200856 name = name->next;
857 continue;
858 }
859
Gilles Peskine449bd832023-01-11 14:50:10 +0100860 if (name != dn) {
861 ret = mbedtls_snprintf(p, n, merge ? " + " : ", ");
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200862 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200863 }
864
Gilles Peskine449bd832023-01-11 14:50:10 +0100865 ret = mbedtls_oid_get_attr_short_name(&name->oid, &short_name);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200866
Gilles Peskine449bd832023-01-11 14:50:10 +0100867 if (ret == 0) {
868 ret = mbedtls_snprintf(p, n, "%s=", short_name);
869 } else {
870 ret = mbedtls_snprintf(p, n, "\?\?=");
871 }
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200872 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200873
Gilles Peskine449bd832023-01-11 14:50:10 +0100874 for (i = 0, j = 0; i < name->val.len; i++, j++) {
875 if (j >= sizeof(s) - 1) {
876 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
877 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200878
879 c = name->val.p[i];
Werner Lewisb33dacd2022-05-20 12:48:46 +0100880 // Special characters requiring escaping, RFC 1779
Gilles Peskine449bd832023-01-11 14:50:10 +0100881 if (c && strchr(",=+<>#;\"\\", c)) {
882 if (j + 1 >= sizeof(s) - 1) {
883 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
884 }
Werner Lewisb33dacd2022-05-20 12:48:46 +0100885 s[j++] = '\\';
886 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100887 if (c < 32 || c >= 127) {
888 s[j] = '?';
889 } else {
890 s[j] = c;
891 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200892 }
Werner Lewisb33dacd2022-05-20 12:48:46 +0100893 s[j] = '\0';
Gilles Peskine449bd832023-01-11 14:50:10 +0100894 ret = mbedtls_snprintf(p, n, "%s", s);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200895 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000896
897 merge = name->next_merged;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200898 name = name->next;
899 }
900
Gilles Peskine449bd832023-01-11 14:50:10 +0100901 return (int) (size - n);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200902}
903
904/*
905 * Store the serial in printable form into buf; no more
906 * than size characters will be written
907 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100908int mbedtls_x509_serial_gets(char *buf, size_t size, const mbedtls_x509_buf *serial)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200909{
Janos Follath865b3eb2019-12-16 11:46:15 +0000910 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200911 size_t i, n, nr;
912 char *p;
913
914 p = buf;
915 n = size;
916
Gilles Peskine449bd832023-01-11 14:50:10 +0100917 nr = (serial->len <= 32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200918 ? serial->len : 28;
919
Gilles Peskine449bd832023-01-11 14:50:10 +0100920 for (i = 0; i < nr; i++) {
921 if (i == 0 && nr > 1 && serial->p[i] == 0x0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200922 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100923 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200924
Gilles Peskine449bd832023-01-11 14:50:10 +0100925 ret = mbedtls_snprintf(p, n, "%02X%s",
926 serial->p[i], (i < nr - 1) ? ":" : "");
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200927 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200928 }
929
Gilles Peskine449bd832023-01-11 14:50:10 +0100930 if (nr != serial->len) {
931 ret = mbedtls_snprintf(p, n, "....");
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200932 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200933 }
934
Gilles Peskine449bd832023-01-11 14:50:10 +0100935 return (int) (size - n);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200936}
937
Hanno Becker612a2f12020-10-09 09:19:39 +0100938#if !defined(MBEDTLS_X509_REMOVE_INFO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200939/*
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200940 * Helper for writing signature algorithms
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100941 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100942int mbedtls_x509_sig_alg_gets(char *buf, size_t size, const mbedtls_x509_buf *sig_oid,
943 mbedtls_pk_type_t pk_alg, mbedtls_md_type_t md_alg,
944 const void *sig_opts)
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100945{
Janos Follath865b3eb2019-12-16 11:46:15 +0000946 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100947 char *p = buf;
948 size_t n = size;
949 const char *desc = NULL;
950
Gilles Peskine449bd832023-01-11 14:50:10 +0100951 ret = mbedtls_oid_get_sig_alg_desc(sig_oid, &desc);
952 if (ret != 0) {
953 ret = mbedtls_snprintf(p, n, "???");
954 } else {
955 ret = mbedtls_snprintf(p, n, "%s", desc);
956 }
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200957 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200959#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100960 if (pk_alg == MBEDTLS_PK_RSASSA_PSS) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200961 const mbedtls_pk_rsassa_pss_options *pss_opts;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100962
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200963 pss_opts = (const mbedtls_pk_rsassa_pss_options *) sig_opts;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100964
Gilles Peskine449bd832023-01-11 14:50:10 +0100965 const char *name = md_type_to_string(md_alg);
966 const char *mgf_name = md_type_to_string(pss_opts->mgf1_hash_id);
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100967
Gilles Peskine449bd832023-01-11 14:50:10 +0100968 ret = mbedtls_snprintf(p, n, " (%s, MGF1-%s, 0x%02X)",
969 name ? name : "???",
970 mgf_name ? mgf_name : "???",
971 (unsigned int) pss_opts->expected_salt_len);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200972 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100973 }
974#else
975 ((void) pk_alg);
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200976 ((void) md_alg);
977 ((void) sig_opts);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200978#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100979
Gilles Peskine449bd832023-01-11 14:50:10 +0100980 return (int) (size - n);
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100981}
Hanno Becker612a2f12020-10-09 09:19:39 +0100982#endif /* MBEDTLS_X509_REMOVE_INFO */
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100983
984/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200985 * Helper for writing "RSA key size", "EC key size", etc
986 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100987int mbedtls_x509_key_size_helper(char *buf, size_t buf_size, const char *name)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200988{
989 char *p = buf;
Manuel Pégourié-Gonnardfb317c52015-06-18 16:25:56 +0200990 size_t n = buf_size;
Janos Follath865b3eb2019-12-16 11:46:15 +0000991 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200992
Gilles Peskine449bd832023-01-11 14:50:10 +0100993 ret = mbedtls_snprintf(p, n, "%s key size", name);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200994 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200995
Gilles Peskine449bd832023-01-11 14:50:10 +0100996 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200997}
998
Manuel Pégourié-Gonnard60c793b2015-06-18 20:52:58 +0200999#if defined(MBEDTLS_HAVE_TIME_DATE)
Manuel Pégourié-Gonnard57e10d72015-06-22 18:59:21 +02001000/*
1001 * Set the time structure to the current time.
1002 * Return 0 on success, non-zero on failure.
1003 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001004static int x509_get_current_time(mbedtls_x509_time *now)
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001005{
Nicholas Wilson512b4ee2017-12-05 12:07:33 +00001006 struct tm *lt, tm_buf;
SimonBd5800b72016-04-26 07:43:27 +01001007 mbedtls_time_t tt;
Manuel Pégourié-Gonnard57e10d72015-06-22 18:59:21 +02001008 int ret = 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001009
Gilles Peskine449bd832023-01-11 14:50:10 +01001010 tt = mbedtls_time(NULL);
1011 lt = mbedtls_platform_gmtime_r(&tt, &tm_buf);
Manuel Pégourié-Gonnard864108d2015-05-29 10:11:03 +02001012
Gilles Peskine449bd832023-01-11 14:50:10 +01001013 if (lt == NULL) {
Manuel Pégourié-Gonnard57e10d72015-06-22 18:59:21 +02001014 ret = -1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001015 } else {
Manuel Pégourié-Gonnard57e10d72015-06-22 18:59:21 +02001016 now->year = lt->tm_year + 1900;
1017 now->mon = lt->tm_mon + 1;
1018 now->day = lt->tm_mday;
1019 now->hour = lt->tm_hour;
1020 now->min = lt->tm_min;
1021 now->sec = lt->tm_sec;
1022 }
Manuel Pégourié-Gonnard864108d2015-05-29 10:11:03 +02001023
Gilles Peskine449bd832023-01-11 14:50:10 +01001024 return ret;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001025}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001026
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001027/*
1028 * Return 0 if before <= after, 1 otherwise
1029 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001030static int x509_check_time(const mbedtls_x509_time *before, const mbedtls_x509_time *after)
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001031{
Gilles Peskine449bd832023-01-11 14:50:10 +01001032 if (before->year > after->year) {
1033 return 1;
1034 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001035
Gilles Peskine449bd832023-01-11 14:50:10 +01001036 if (before->year == after->year &&
1037 before->mon > after->mon) {
1038 return 1;
1039 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001040
Gilles Peskine449bd832023-01-11 14:50:10 +01001041 if (before->year == after->year &&
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001042 before->mon == after->mon &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001043 before->day > after->day) {
1044 return 1;
1045 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001046
Gilles Peskine449bd832023-01-11 14:50:10 +01001047 if (before->year == after->year &&
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001048 before->mon == after->mon &&
1049 before->day == after->day &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001050 before->hour > after->hour) {
1051 return 1;
1052 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001053
Gilles Peskine449bd832023-01-11 14:50:10 +01001054 if (before->year == after->year &&
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001055 before->mon == after->mon &&
1056 before->day == after->day &&
1057 before->hour == after->hour &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001058 before->min > after->min) {
1059 return 1;
1060 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001061
Gilles Peskine449bd832023-01-11 14:50:10 +01001062 if (before->year == after->year &&
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001063 before->mon == after->mon &&
1064 before->day == after->day &&
1065 before->hour == after->hour &&
1066 before->min == after->min &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001067 before->sec > after->sec) {
1068 return 1;
1069 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001070
Gilles Peskine449bd832023-01-11 14:50:10 +01001071 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001072}
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001073
Gilles Peskine449bd832023-01-11 14:50:10 +01001074int mbedtls_x509_time_is_past(const mbedtls_x509_time *to)
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001075{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001076 mbedtls_x509_time now;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001077
Gilles Peskine449bd832023-01-11 14:50:10 +01001078 if (x509_get_current_time(&now) != 0) {
1079 return 1;
1080 }
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001081
Gilles Peskine449bd832023-01-11 14:50:10 +01001082 return x509_check_time(&now, to);
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001083}
1084
Gilles Peskine449bd832023-01-11 14:50:10 +01001085int mbedtls_x509_time_is_future(const mbedtls_x509_time *from)
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001086{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001087 mbedtls_x509_time now;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001088
Gilles Peskine449bd832023-01-11 14:50:10 +01001089 if (x509_get_current_time(&now) != 0) {
1090 return 1;
1091 }
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001092
Gilles Peskine449bd832023-01-11 14:50:10 +01001093 return x509_check_time(from, &now);
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001094}
1095
Manuel Pégourié-Gonnard60c793b2015-06-18 20:52:58 +02001096#else /* MBEDTLS_HAVE_TIME_DATE */
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001097
Gilles Peskine449bd832023-01-11 14:50:10 +01001098int mbedtls_x509_time_is_past(const mbedtls_x509_time *to)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001099{
1100 ((void) to);
Gilles Peskine449bd832023-01-11 14:50:10 +01001101 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001102}
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001103
Gilles Peskine449bd832023-01-11 14:50:10 +01001104int mbedtls_x509_time_is_future(const mbedtls_x509_time *from)
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001105{
1106 ((void) from);
Gilles Peskine449bd832023-01-11 14:50:10 +01001107 return 0;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001108}
Manuel Pégourié-Gonnard60c793b2015-06-18 20:52:58 +02001109#endif /* MBEDTLS_HAVE_TIME_DATE */
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001110
1111/* Common functions for parsing CRT and CSR. */
1112#if defined(MBEDTLS_X509_CRT_PARSE_C) || defined(MBEDTLS_X509_CSR_PARSE_C)
1113/*
1114 * OtherName ::= SEQUENCE {
1115 * type-id OBJECT IDENTIFIER,
1116 * value [0] EXPLICIT ANY DEFINED BY type-id }
1117 *
1118 * HardwareModuleName ::= SEQUENCE {
1119 * hwType OBJECT IDENTIFIER,
1120 * hwSerialNum OCTET STRING }
1121 *
1122 * NOTE: we currently only parse and use otherName of type HwModuleName,
1123 * as defined in RFC 4108.
1124 */
1125static int x509_get_other_name(const mbedtls_x509_buf *subject_alt_name,
1126 mbedtls_x509_san_other_name *other_name)
1127{
1128 int ret = 0;
1129 size_t len;
1130 unsigned char *p = subject_alt_name->p;
1131 const unsigned char *end = p + subject_alt_name->len;
1132 mbedtls_x509_buf cur_oid;
1133
1134 if ((subject_alt_name->tag &
1135 (MBEDTLS_ASN1_TAG_CLASS_MASK | MBEDTLS_ASN1_TAG_VALUE_MASK)) !=
1136 (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME)) {
1137 /*
1138 * The given subject alternative name is not of type "othername".
1139 */
1140 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1141 }
1142
1143 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1144 MBEDTLS_ASN1_OID)) != 0) {
1145 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1146 }
1147
1148 cur_oid.tag = MBEDTLS_ASN1_OID;
1149 cur_oid.p = p;
1150 cur_oid.len = len;
1151
1152 /*
1153 * Only HwModuleName is currently supported.
1154 */
1155 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ON_HW_MODULE_NAME, &cur_oid) != 0) {
1156 return MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
1157 }
1158
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001159 p += len;
1160 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1161 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC)) !=
1162 0) {
1163 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1164 }
1165
Hanno Beckerdae916b2019-09-13 14:21:13 +01001166 if (end != p + len) {
1167 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1168 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1169 }
1170
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001171 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1172 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1173 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1174 }
1175
Hanno Beckerdae916b2019-09-13 14:21:13 +01001176 if (end != p + len) {
1177 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1178 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1179 }
1180
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001181 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OID)) != 0) {
1182 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1183 }
1184
1185 other_name->value.hardware_module_name.oid.tag = MBEDTLS_ASN1_OID;
1186 other_name->value.hardware_module_name.oid.p = p;
1187 other_name->value.hardware_module_name.oid.len = len;
1188
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001189 p += len;
1190 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1191 MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1192 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1193 }
1194
1195 other_name->value.hardware_module_name.val.tag = MBEDTLS_ASN1_OCTET_STRING;
1196 other_name->value.hardware_module_name.val.p = p;
1197 other_name->value.hardware_module_name.val.len = len;
1198 p += len;
1199 if (p != end) {
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001200 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1201 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1202 }
1203 return 0;
1204}
1205
1206/*
1207 * SubjectAltName ::= GeneralNames
1208 *
1209 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
1210 *
1211 * GeneralName ::= CHOICE {
1212 * otherName [0] OtherName,
1213 * rfc822Name [1] IA5String,
1214 * dNSName [2] IA5String,
1215 * x400Address [3] ORAddress,
1216 * directoryName [4] Name,
1217 * ediPartyName [5] EDIPartyName,
1218 * uniformResourceIdentifier [6] IA5String,
1219 * iPAddress [7] OCTET STRING,
1220 * registeredID [8] OBJECT IDENTIFIER }
1221 *
1222 * OtherName ::= SEQUENCE {
1223 * type-id OBJECT IDENTIFIER,
1224 * value [0] EXPLICIT ANY DEFINED BY type-id }
1225 *
1226 * EDIPartyName ::= SEQUENCE {
1227 * nameAssigner [0] DirectoryString OPTIONAL,
1228 * partyName [1] DirectoryString }
1229 *
Andrzej Kurek7a05fab2023-02-13 10:03:07 -05001230 * NOTE: we list all types, but only use "dnsName", "otherName" and
1231 * "uniformResourceIdentifier", as defined in RFC 5280, at this point.
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001232 */
1233int mbedtls_x509_get_subject_alt_name(unsigned char **p,
1234 const unsigned char *end,
1235 mbedtls_x509_sequence *subject_alt_name)
1236{
1237 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1238 size_t len, tag_len;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001239 mbedtls_asn1_sequence *cur = subject_alt_name;
1240
1241 /* Get main sequence tag */
1242 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
1243 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1244 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1245 }
1246
1247 if (*p + len != end) {
1248 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1249 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1250 }
1251
1252 while (*p < end) {
1253 mbedtls_x509_subject_alternative_name dummy_san_buf;
Hanno Beckerae8f8c42019-09-13 12:24:56 +01001254 mbedtls_x509_buf tmp_san_buf;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001255 memset(&dummy_san_buf, 0, sizeof(dummy_san_buf));
1256
Hanno Beckerae8f8c42019-09-13 12:24:56 +01001257 tmp_san_buf.tag = **p;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001258 (*p)++;
Hanno Beckerae8f8c42019-09-13 12:24:56 +01001259
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001260 if ((ret = mbedtls_asn1_get_len(p, end, &tag_len)) != 0) {
1261 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1262 }
1263
Hanno Beckerae8f8c42019-09-13 12:24:56 +01001264 tmp_san_buf.p = *p;
1265 tmp_san_buf.len = tag_len;
1266
1267 if ((tmp_san_buf.tag & MBEDTLS_ASN1_TAG_CLASS_MASK) !=
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001268 MBEDTLS_ASN1_CONTEXT_SPECIFIC) {
1269 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1270 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
1271 }
1272
1273 /*
1274 * Check that the SAN is structured correctly.
1275 */
Hanno Beckerae8f8c42019-09-13 12:24:56 +01001276 ret = mbedtls_x509_parse_subject_alt_name(&tmp_san_buf, &dummy_san_buf);
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001277 /*
1278 * In case the extension is malformed, return an error,
1279 * and clear the allocated sequences.
1280 */
1281 if (ret != 0 && ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE) {
1282 mbedtls_asn1_sequence_free(subject_alt_name->next);
1283 subject_alt_name->next = NULL;
1284 return ret;
1285 }
1286
1287 /* Allocate and assign next pointer */
1288 if (cur->buf.p != NULL) {
1289 if (cur->next != NULL) {
1290 return MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
1291 }
1292
1293 cur->next = mbedtls_calloc(1, sizeof(mbedtls_asn1_sequence));
1294
1295 if (cur->next == NULL) {
1296 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1297 MBEDTLS_ERR_ASN1_ALLOC_FAILED);
1298 }
1299
1300 cur = cur->next;
1301 }
1302
Hanno Beckerae8f8c42019-09-13 12:24:56 +01001303 cur->buf = tmp_san_buf;
1304 *p += tmp_san_buf.len;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001305 }
1306
1307 /* Set final sequence entry's next pointer to NULL */
1308 cur->next = NULL;
1309
1310 if (*p != end) {
1311 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1312 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1313 }
1314
1315 return 0;
1316}
1317
1318int mbedtls_x509_get_ns_cert_type(unsigned char **p,
1319 const unsigned char *end,
1320 unsigned char *ns_cert_type)
1321{
1322 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1323 mbedtls_x509_bitstring bs = { 0, 0, NULL };
1324
1325 if ((ret = mbedtls_asn1_get_bitstring(p, end, &bs)) != 0) {
1326 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1327 }
1328
Przemek Stekiel32e20912023-01-25 14:26:15 +01001329 /* A bitstring with no flags set is still technically valid, as it will mean
1330 that the certificate has no designated purpose at the time of creation. */
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001331 if (bs.len == 0) {
1332 *ns_cert_type = 0;
1333 return 0;
1334 }
1335
1336 if (bs.len != 1) {
1337 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1338 MBEDTLS_ERR_ASN1_INVALID_LENGTH);
1339 }
1340
1341 /* Get actual bitstring */
1342 *ns_cert_type = *bs.p;
1343 return 0;
1344}
1345
1346int mbedtls_x509_get_key_usage(unsigned char **p,
1347 const unsigned char *end,
1348 unsigned int *key_usage)
1349{
1350 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1351 size_t i;
1352 mbedtls_x509_bitstring bs = { 0, 0, NULL };
1353
1354 if ((ret = mbedtls_asn1_get_bitstring(p, end, &bs)) != 0) {
1355 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1356 }
1357
Przemek Stekiel32e20912023-01-25 14:26:15 +01001358 /* A bitstring with no flags set is still technically valid, as it will mean
1359 that the certificate has no designated purpose at the time of creation. */
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001360 if (bs.len == 0) {
1361 *key_usage = 0;
1362 return 0;
1363 }
1364
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001365 /* Get actual bitstring */
1366 *key_usage = 0;
1367 for (i = 0; i < bs.len && i < sizeof(unsigned int); i++) {
1368 *key_usage |= (unsigned int) bs.p[i] << (8*i);
1369 }
1370
1371 return 0;
1372}
1373
1374int mbedtls_x509_parse_subject_alt_name(const mbedtls_x509_buf *san_buf,
1375 mbedtls_x509_subject_alternative_name *san)
1376{
1377 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1378 switch (san_buf->tag &
1379 (MBEDTLS_ASN1_TAG_CLASS_MASK |
1380 MBEDTLS_ASN1_TAG_VALUE_MASK)) {
1381 /*
1382 * otherName
1383 */
1384 case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME):
1385 {
1386 mbedtls_x509_san_other_name other_name;
1387
1388 ret = x509_get_other_name(san_buf, &other_name);
1389 if (ret != 0) {
1390 return ret;
1391 }
1392
1393 memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name));
1394 san->type = MBEDTLS_X509_SAN_OTHER_NAME;
1395 memcpy(&san->san.other_name,
1396 &other_name, sizeof(other_name));
1397
1398 }
1399 break;
Andrzej Kurek7a05fab2023-02-13 10:03:07 -05001400 /*
1401 * uniformResourceIdentifier
1402 */
1403 case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER):
1404 {
1405 memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name));
1406 san->type = MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001407
Andrzej Kurek7a05fab2023-02-13 10:03:07 -05001408 memcpy(&san->san.unstructured_name,
1409 san_buf, sizeof(*san_buf));
1410
1411 }
1412 break;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001413 /*
1414 * dNSName
1415 */
1416 case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_DNS_NAME):
1417 {
1418 memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name));
1419 san->type = MBEDTLS_X509_SAN_DNS_NAME;
1420
1421 memcpy(&san->san.unstructured_name,
1422 san_buf, sizeof(*san_buf));
1423
1424 }
1425 break;
1426
1427 /*
1428 * Type not supported
1429 */
1430 default:
1431 return MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
1432 }
1433 return 0;
1434}
1435
1436#if !defined(MBEDTLS_X509_REMOVE_INFO)
1437int mbedtls_x509_info_subject_alt_name(char **buf, size_t *size,
1438 const mbedtls_x509_sequence
1439 *subject_alt_name,
1440 const char *prefix)
1441{
1442 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1443 size_t i;
1444 size_t n = *size;
1445 char *p = *buf;
1446 const mbedtls_x509_sequence *cur = subject_alt_name;
1447 mbedtls_x509_subject_alternative_name san;
1448 int parse_ret;
1449
1450 while (cur != NULL) {
1451 memset(&san, 0, sizeof(san));
1452 parse_ret = mbedtls_x509_parse_subject_alt_name(&cur->buf, &san);
1453 if (parse_ret != 0) {
1454 if (parse_ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE) {
1455 ret = mbedtls_snprintf(p, n, "\n%s <unsupported>", prefix);
1456 MBEDTLS_X509_SAFE_SNPRINTF;
1457 } else {
1458 ret = mbedtls_snprintf(p, n, "\n%s <malformed>", prefix);
1459 MBEDTLS_X509_SAFE_SNPRINTF;
1460 }
1461 cur = cur->next;
1462 continue;
1463 }
1464
1465 switch (san.type) {
1466 /*
1467 * otherName
1468 */
1469 case MBEDTLS_X509_SAN_OTHER_NAME:
1470 {
1471 mbedtls_x509_san_other_name *other_name = &san.san.other_name;
1472
1473 ret = mbedtls_snprintf(p, n, "\n%s otherName :", prefix);
1474 MBEDTLS_X509_SAFE_SNPRINTF;
1475
1476 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ON_HW_MODULE_NAME,
1477 &other_name->value.hardware_module_name.oid) != 0) {
1478 ret = mbedtls_snprintf(p, n, "\n%s hardware module name :", prefix);
1479 MBEDTLS_X509_SAFE_SNPRINTF;
1480 ret =
1481 mbedtls_snprintf(p, n, "\n%s hardware type : ", prefix);
1482 MBEDTLS_X509_SAFE_SNPRINTF;
1483
1484 ret = mbedtls_oid_get_numeric_string(p,
1485 n,
1486 &other_name->value.hardware_module_name.oid);
1487 MBEDTLS_X509_SAFE_SNPRINTF;
1488
1489 ret =
1490 mbedtls_snprintf(p, n, "\n%s hardware serial number : ", prefix);
1491 MBEDTLS_X509_SAFE_SNPRINTF;
1492
1493 for (i = 0; i < other_name->value.hardware_module_name.val.len; i++) {
1494 ret = mbedtls_snprintf(p,
1495 n,
1496 "%02X",
1497 other_name->value.hardware_module_name.val.p[i]);
1498 MBEDTLS_X509_SAFE_SNPRINTF;
1499 }
1500 }/* MBEDTLS_OID_ON_HW_MODULE_NAME */
1501 }
1502 break;
Andrzej Kurek7a05fab2023-02-13 10:03:07 -05001503 /*
1504 * uniformResourceIdentifier
1505 */
1506 case MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER:
1507 {
1508 ret = mbedtls_snprintf(p, n, "\n%s uniformResourceIdentifier : ", prefix);
1509 MBEDTLS_X509_SAFE_SNPRINTF;
1510 if (san.san.unstructured_name.len >= n) {
1511 *p = '\0';
1512 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1513 }
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001514
Andrzej Kurek7a05fab2023-02-13 10:03:07 -05001515 memcpy(p, san.san.unstructured_name.p, san.san.unstructured_name.len);
1516 p += san.san.unstructured_name.len;
1517 n -= san.san.unstructured_name.len;
1518 }
1519 break;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001520 /*
1521 * dNSName
1522 */
1523 case MBEDTLS_X509_SAN_DNS_NAME:
1524 {
1525 ret = mbedtls_snprintf(p, n, "\n%s dNSName : ", prefix);
1526 MBEDTLS_X509_SAFE_SNPRINTF;
1527 if (san.san.unstructured_name.len >= n) {
1528 *p = '\0';
1529 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1530 }
1531
1532 memcpy(p, san.san.unstructured_name.p, san.san.unstructured_name.len);
1533 p += san.san.unstructured_name.len;
1534 n -= san.san.unstructured_name.len;
1535 }
1536 break;
1537
1538 /*
1539 * Type not supported, skip item.
1540 */
1541 default:
1542 ret = mbedtls_snprintf(p, n, "\n%s <unsupported>", prefix);
1543 MBEDTLS_X509_SAFE_SNPRINTF;
1544 break;
1545 }
1546
1547 cur = cur->next;
1548 }
1549
1550 *p = '\0';
1551
1552 *size = n;
1553 *buf = p;
1554
1555 return 0;
1556}
1557
1558#define PRINT_ITEM(i) \
1559 { \
1560 ret = mbedtls_snprintf(p, n, "%s" i, sep); \
1561 MBEDTLS_X509_SAFE_SNPRINTF; \
1562 sep = ", "; \
1563 }
1564
1565#define CERT_TYPE(type, name) \
1566 if (ns_cert_type & (type)) \
1567 PRINT_ITEM(name);
1568
1569int mbedtls_x509_info_cert_type(char **buf, size_t *size,
1570 unsigned char ns_cert_type)
1571{
1572 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1573 size_t n = *size;
1574 char *p = *buf;
1575 const char *sep = "";
1576
1577 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT, "SSL Client");
1578 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER, "SSL Server");
1579 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_EMAIL, "Email");
1580 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing");
1581 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_RESERVED, "Reserved");
1582 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_SSL_CA, "SSL CA");
1583 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA, "Email CA");
1584 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA");
1585
1586 *size = n;
1587 *buf = p;
1588
1589 return 0;
1590}
1591
1592#define KEY_USAGE(code, name) \
1593 if (key_usage & (code)) \
1594 PRINT_ITEM(name);
1595
1596int mbedtls_x509_info_key_usage(char **buf, size_t *size,
1597 unsigned int key_usage)
1598{
1599 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1600 size_t n = *size;
1601 char *p = *buf;
1602 const char *sep = "";
1603
1604 KEY_USAGE(MBEDTLS_X509_KU_DIGITAL_SIGNATURE, "Digital Signature");
1605 KEY_USAGE(MBEDTLS_X509_KU_NON_REPUDIATION, "Non Repudiation");
1606 KEY_USAGE(MBEDTLS_X509_KU_KEY_ENCIPHERMENT, "Key Encipherment");
1607 KEY_USAGE(MBEDTLS_X509_KU_DATA_ENCIPHERMENT, "Data Encipherment");
1608 KEY_USAGE(MBEDTLS_X509_KU_KEY_AGREEMENT, "Key Agreement");
1609 KEY_USAGE(MBEDTLS_X509_KU_KEY_CERT_SIGN, "Key Cert Sign");
1610 KEY_USAGE(MBEDTLS_X509_KU_CRL_SIGN, "CRL Sign");
1611 KEY_USAGE(MBEDTLS_X509_KU_ENCIPHER_ONLY, "Encipher Only");
1612 KEY_USAGE(MBEDTLS_X509_KU_DECIPHER_ONLY, "Decipher Only");
1613
1614 *size = n;
1615 *buf = p;
1616
1617 return 0;
1618}
1619#endif /* MBEDTLS_X509_REMOVE_INFO */
1620#endif /* MBEDTLS_X509_CRT_PARSE_C || MBEDTLS_X509_CSR_PARSE_C */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001621#endif /* MBEDTLS_X509_USE_C */