blob: 747ba811dd0966707597e6ae8d3cea4b43fc9529 [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
Gilles Peskine449bd832023-01-11 14:50:10 +010056#define CHECK(code) if ((ret = (code)) != 0) { return ret; }
Hanno Becker1eeca412018-10-15 12:01:35 +010057#define CHECK_RANGE(min, max, val) \
58 do \
59 { \
Gilles Peskine449bd832023-01-11 14:50:10 +010060 if ((val) < (min) || (val) > (max)) \
Hanno Becker1eeca412018-10-15 12:01:35 +010061 { \
Gilles Peskine449bd832023-01-11 14:50:10 +010062 return ret; \
Hanno Becker1eeca412018-10-15 12:01:35 +010063 } \
Gilles Peskine449bd832023-01-11 14:50:10 +010064 } while (0)
Rich Evans7d5a55a2015-02-13 11:48:02 +000065
Paul Bakker7c6b2c32013-09-16 13:49:26 +020066/*
67 * CertificateSerialNumber ::= INTEGER
68 */
Gilles Peskine449bd832023-01-11 14:50:10 +010069int mbedtls_x509_get_serial(unsigned char **p, const unsigned char *end,
70 mbedtls_x509_buf *serial)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020071{
Janos Follath865b3eb2019-12-16 11:46:15 +000072 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020073
Gilles Peskine449bd832023-01-11 14:50:10 +010074 if ((end - *p) < 1) {
75 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_SERIAL,
76 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
77 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +020078
Gilles Peskine449bd832023-01-11 14:50:10 +010079 if (**p != (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_PRIMITIVE | 2) &&
80 **p != MBEDTLS_ASN1_INTEGER) {
81 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_SERIAL,
82 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
83 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +020084
85 serial->tag = *(*p)++;
86
Gilles Peskine449bd832023-01-11 14:50:10 +010087 if ((ret = mbedtls_asn1_get_len(p, end, &serial->len)) != 0) {
88 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_SERIAL, ret);
89 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +020090
91 serial->p = *p;
92 *p += serial->len;
93
Gilles Peskine449bd832023-01-11 14:50:10 +010094 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020095}
96
97/* Get an algorithm identifier without parameters (eg for signatures)
98 *
99 * AlgorithmIdentifier ::= SEQUENCE {
100 * algorithm OBJECT IDENTIFIER,
101 * parameters ANY DEFINED BY algorithm OPTIONAL }
102 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100103int mbedtls_x509_get_alg_null(unsigned char **p, const unsigned char *end,
104 mbedtls_x509_buf *alg)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200105{
Janos Follath865b3eb2019-12-16 11:46:15 +0000106 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200107
Gilles Peskine449bd832023-01-11 14:50:10 +0100108 if ((ret = mbedtls_asn1_get_alg_null(p, end, alg)) != 0) {
109 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
110 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200111
Gilles Peskine449bd832023-01-11 14:50:10 +0100112 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200113}
114
115/*
Antonin Décimo36e89b52019-01-23 15:24:37 +0100116 * Parse an algorithm identifier with (optional) parameters
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100117 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100118int mbedtls_x509_get_alg(unsigned char **p, const unsigned char *end,
119 mbedtls_x509_buf *alg, mbedtls_x509_buf *params)
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100120{
Janos Follath865b3eb2019-12-16 11:46:15 +0000121 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100122
Gilles Peskine449bd832023-01-11 14:50:10 +0100123 if ((ret = mbedtls_asn1_get_alg(p, end, alg, params)) != 0) {
124 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
125 }
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100126
Gilles Peskine449bd832023-01-11 14:50:10 +0100127 return 0;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100128}
129
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200130/*
131 * Convert md type to string
132 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100133static inline const char *md_type_to_string(mbedtls_md_type_t md_alg)
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200134{
Gilles Peskine449bd832023-01-11 14:50:10 +0100135 switch (md_alg) {
Manuel Pégourié-Gonnard49e67f82023-03-16 11:39:20 +0100136#if defined(MBEDTLS_MD_CAN_MD5)
Gilles Peskine449bd832023-01-11 14:50:10 +0100137 case MBEDTLS_MD_MD5:
138 return "MD5";
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200139#endif
Manuel Pégourié-Gonnard49e67f82023-03-16 11:39:20 +0100140#if defined(MBEDTLS_MD_CAN_SHA1)
Gilles Peskine449bd832023-01-11 14:50:10 +0100141 case MBEDTLS_MD_SHA1:
142 return "SHA1";
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200143#endif
Manuel Pégourié-Gonnard49e67f82023-03-16 11:39:20 +0100144#if defined(MBEDTLS_MD_CAN_SHA224)
Gilles Peskine449bd832023-01-11 14:50:10 +0100145 case MBEDTLS_MD_SHA224:
146 return "SHA224";
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200147#endif
Manuel Pégourié-Gonnard49e67f82023-03-16 11:39:20 +0100148#if defined(MBEDTLS_MD_CAN_SHA256)
Gilles Peskine449bd832023-01-11 14:50:10 +0100149 case MBEDTLS_MD_SHA256:
150 return "SHA256";
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200151#endif
Manuel Pégourié-Gonnard49e67f82023-03-16 11:39:20 +0100152#if defined(MBEDTLS_MD_CAN_SHA384)
Gilles Peskine449bd832023-01-11 14:50:10 +0100153 case MBEDTLS_MD_SHA384:
154 return "SHA384";
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200155#endif
Manuel Pégourié-Gonnard49e67f82023-03-16 11:39:20 +0100156#if defined(MBEDTLS_MD_CAN_SHA512)
Gilles Peskine449bd832023-01-11 14:50:10 +0100157 case MBEDTLS_MD_SHA512:
158 return "SHA512";
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200159#endif
Manuel Pégourié-Gonnard49e67f82023-03-16 11:39:20 +0100160#if defined(MBEDTLS_MD_CAN_RIPEMD160)
Gilles Peskine449bd832023-01-11 14:50:10 +0100161 case MBEDTLS_MD_RIPEMD160:
162 return "RIPEMD160";
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200163#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100164 case MBEDTLS_MD_NONE:
165 return NULL;
166 default:
167 return NULL;
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200168 }
169}
170
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200171#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100172/*
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100173 * HashAlgorithm ::= AlgorithmIdentifier
174 *
175 * AlgorithmIdentifier ::= SEQUENCE {
176 * algorithm OBJECT IDENTIFIER,
177 * parameters ANY DEFINED BY algorithm OPTIONAL }
178 *
179 * For HashAlgorithm, parameters MUST be NULL or absent.
180 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100181static 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 +0100182{
Janos Follath865b3eb2019-12-16 11:46:15 +0000183 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100184 unsigned char *p;
185 const unsigned char *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186 mbedtls_x509_buf md_oid;
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100187 size_t len;
188
189 /* Make sure we got a SEQUENCE and setup bounds */
Gilles Peskine449bd832023-01-11 14:50:10 +0100190 if (alg->tag != (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) {
191 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
192 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
193 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100194
Andrzej Kurekfeaebc52020-07-16 04:37:41 -0400195 p = alg->p;
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100196 end = p + alg->len;
197
Gilles Peskine449bd832023-01-11 14:50:10 +0100198 if (p >= end) {
199 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
200 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
201 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100202
203 /* Parse md_oid */
204 md_oid.tag = *p;
205
Gilles Peskine449bd832023-01-11 14:50:10 +0100206 if ((ret = mbedtls_asn1_get_tag(&p, end, &md_oid.len, MBEDTLS_ASN1_OID)) != 0) {
207 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
208 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100209
210 md_oid.p = p;
211 p += md_oid.len;
212
213 /* Get md_alg from md_oid */
Gilles Peskine449bd832023-01-11 14:50:10 +0100214 if ((ret = mbedtls_oid_get_md_alg(&md_oid, md_alg)) != 0) {
215 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
216 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100217
218 /* Make sure params is absent of NULL */
Gilles Peskine449bd832023-01-11 14:50:10 +0100219 if (p == end) {
220 return 0;
221 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100222
Gilles Peskine449bd832023-01-11 14:50:10 +0100223 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_NULL)) != 0 || len != 0) {
224 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
225 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100226
Gilles Peskine449bd832023-01-11 14:50:10 +0100227 if (p != end) {
228 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
229 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
230 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100231
Gilles Peskine449bd832023-01-11 14:50:10 +0100232 return 0;
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100233}
234
235/*
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100236 * RSASSA-PSS-params ::= SEQUENCE {
237 * hashAlgorithm [0] HashAlgorithm DEFAULT sha1Identifier,
238 * maskGenAlgorithm [1] MaskGenAlgorithm DEFAULT mgf1SHA1Identifier,
239 * saltLength [2] INTEGER DEFAULT 20,
240 * trailerField [3] INTEGER DEFAULT 1 }
241 * -- Note that the tags in this Sequence are explicit.
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200242 *
243 * RFC 4055 (which defines use of RSASSA-PSS in PKIX) states that the value
244 * of trailerField MUST be 1, and PKCS#1 v2.2 doesn't even define any other
Tom Cosgrove1797b052022-12-04 17:19:59 +0000245 * option. Enforce this at parsing time.
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100246 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100247int mbedtls_x509_get_rsassa_pss_params(const mbedtls_x509_buf *params,
248 mbedtls_md_type_t *md_alg, mbedtls_md_type_t *mgf_md,
249 int *salt_len)
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100250{
Janos Follath865b3eb2019-12-16 11:46:15 +0000251 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100252 unsigned char *p;
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100253 const unsigned char *end, *end2;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100254 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200255 mbedtls_x509_buf alg_id, alg_params;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100256
257 /* First set everything to defaults */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200258 *md_alg = MBEDTLS_MD_SHA1;
259 *mgf_md = MBEDTLS_MD_SHA1;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100260 *salt_len = 20;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100261
262 /* Make sure params is a SEQUENCE and setup bounds */
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 if (params->tag != (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) {
264 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
265 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
266 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100267
268 p = (unsigned char *) params->p;
269 end = p + params->len;
270
Gilles Peskine449bd832023-01-11 14:50:10 +0100271 if (p == end) {
272 return 0;
273 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100274
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100275 /*
276 * HashAlgorithm
277 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100278 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
279 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
280 0)) == 0) {
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100281 end2 = p + len;
282
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100283 /* HashAlgorithm ::= AlgorithmIdentifier (without parameters) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100284 if ((ret = mbedtls_x509_get_alg_null(&p, end2, &alg_id)) != 0) {
285 return ret;
286 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100287
Gilles Peskine449bd832023-01-11 14:50:10 +0100288 if ((ret = mbedtls_oid_get_md_alg(&alg_id, md_alg)) != 0) {
289 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
290 }
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100291
Gilles Peskine449bd832023-01-11 14:50:10 +0100292 if (p != end2) {
293 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
294 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
295 }
296 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
297 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100298 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100299
Gilles Peskine449bd832023-01-11 14:50:10 +0100300 if (p == end) {
301 return 0;
302 }
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100303
304 /*
305 * MaskGenAlgorithm
306 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100307 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
308 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
309 1)) == 0) {
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100310 end2 = p + len;
311
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100312 /* MaskGenAlgorithm ::= AlgorithmIdentifier (params = HashAlgorithm) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100313 if ((ret = mbedtls_x509_get_alg(&p, end2, &alg_id, &alg_params)) != 0) {
314 return ret;
315 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100316
317 /* Only MFG1 is recognised for now */
Gilles Peskine449bd832023-01-11 14:50:10 +0100318 if (MBEDTLS_OID_CMP(MBEDTLS_OID_MGF1, &alg_id) != 0) {
319 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE,
320 MBEDTLS_ERR_OID_NOT_FOUND);
321 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100322
323 /* Parse HashAlgorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +0100324 if ((ret = x509_get_hash_alg(&alg_params, mgf_md)) != 0) {
325 return ret;
326 }
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100327
Gilles Peskine449bd832023-01-11 14:50:10 +0100328 if (p != end2) {
329 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
330 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
331 }
332 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
333 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100334 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100335
Gilles Peskine449bd832023-01-11 14:50:10 +0100336 if (p == end) {
337 return 0;
338 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100339
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100340 /*
341 * salt_len
342 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100343 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
344 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
345 2)) == 0) {
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100346 end2 = p + len;
347
Gilles Peskine449bd832023-01-11 14:50:10 +0100348 if ((ret = mbedtls_asn1_get_int(&p, end2, salt_len)) != 0) {
349 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
350 }
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100351
Gilles Peskine449bd832023-01-11 14:50:10 +0100352 if (p != end2) {
353 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
354 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
355 }
356 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
357 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100358 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100359
Gilles Peskine449bd832023-01-11 14:50:10 +0100360 if (p == end) {
361 return 0;
362 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100363
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100364 /*
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200365 * trailer_field (if present, must be 1)
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100366 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100367 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
368 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
369 3)) == 0) {
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200370 int trailer_field;
371
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100372 end2 = p + len;
373
Gilles Peskine449bd832023-01-11 14:50:10 +0100374 if ((ret = mbedtls_asn1_get_int(&p, end2, &trailer_field)) != 0) {
375 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
376 }
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100377
Gilles Peskine449bd832023-01-11 14:50:10 +0100378 if (p != end2) {
379 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
380 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
381 }
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200382
Gilles Peskine449bd832023-01-11 14:50:10 +0100383 if (trailer_field != 1) {
384 return MBEDTLS_ERR_X509_INVALID_ALG;
385 }
386 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
387 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100388 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100389
Gilles Peskine449bd832023-01-11 14:50:10 +0100390 if (p != end) {
391 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
392 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
393 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100394
Gilles Peskine449bd832023-01-11 14:50:10 +0100395 return 0;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100396}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200397#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100398
399/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200400 * AttributeTypeAndValue ::= SEQUENCE {
401 * type AttributeType,
402 * value AttributeValue }
403 *
404 * AttributeType ::= OBJECT IDENTIFIER
405 *
406 * AttributeValue ::= ANY DEFINED BY AttributeType
407 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100408static int x509_get_attr_type_value(unsigned char **p,
409 const unsigned char *end,
410 mbedtls_x509_name *cur)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200411{
Janos Follath865b3eb2019-12-16 11:46:15 +0000412 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200413 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200414 mbedtls_x509_buf *oid;
415 mbedtls_x509_buf *val;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200416
Gilles Peskine449bd832023-01-11 14:50:10 +0100417 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
418 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
419 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME, ret);
420 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200421
Hanno Becker12f62fb2019-02-12 17:22:36 +0000422 end = *p + len;
423
Gilles Peskine449bd832023-01-11 14:50:10 +0100424 if ((end - *p) < 1) {
425 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME,
426 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
427 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200428
429 oid = &cur->oid;
430 oid->tag = **p;
431
Gilles Peskine449bd832023-01-11 14:50:10 +0100432 if ((ret = mbedtls_asn1_get_tag(p, end, &oid->len, MBEDTLS_ASN1_OID)) != 0) {
433 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME, ret);
434 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200435
436 oid->p = *p;
437 *p += oid->len;
438
Gilles Peskine449bd832023-01-11 14:50:10 +0100439 if ((end - *p) < 1) {
440 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME,
441 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
442 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200443
Gilles Peskine449bd832023-01-11 14:50:10 +0100444 if (**p != MBEDTLS_ASN1_BMP_STRING && **p != MBEDTLS_ASN1_UTF8_STRING &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200445 **p != MBEDTLS_ASN1_T61_STRING && **p != MBEDTLS_ASN1_PRINTABLE_STRING &&
446 **p != MBEDTLS_ASN1_IA5_STRING && **p != MBEDTLS_ASN1_UNIVERSAL_STRING &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100447 **p != MBEDTLS_ASN1_BIT_STRING) {
448 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME,
449 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
450 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200451
452 val = &cur->val;
453 val->tag = *(*p)++;
454
Gilles Peskine449bd832023-01-11 14:50:10 +0100455 if ((ret = mbedtls_asn1_get_len(p, end, &val->len)) != 0) {
456 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME, ret);
457 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200458
459 val->p = *p;
460 *p += val->len;
461
Gilles Peskine449bd832023-01-11 14:50:10 +0100462 if (*p != end) {
463 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME,
464 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Hanno Becker12f62fb2019-02-12 17:22:36 +0000465 }
466
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200467 cur->next = NULL;
468
Gilles Peskine449bd832023-01-11 14:50:10 +0100469 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200470}
471
472/*
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000473 * Name ::= CHOICE { -- only one possibility for now --
474 * rdnSequence RDNSequence }
475 *
476 * RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
477 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200478 * RelativeDistinguishedName ::=
479 * SET OF AttributeTypeAndValue
480 *
481 * AttributeTypeAndValue ::= SEQUENCE {
482 * type AttributeType,
483 * value AttributeValue }
484 *
485 * AttributeType ::= OBJECT IDENTIFIER
486 *
487 * AttributeValue ::= ANY DEFINED BY AttributeType
Manuel Pégourié-Gonnard5d861852014-10-17 12:41:41 +0200488 *
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000489 * The data structure is optimized for the common case where each RDN has only
490 * one element, which is represented as a list of AttributeTypeAndValue.
491 * For the general case we still use a flat list, but we mark elements of the
492 * same set so that they are "merged" together in the functions that consume
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200493 * this list, eg mbedtls_x509_dn_gets().
David Horstmanned794832022-10-04 18:12:06 +0100494 *
David Horstmann11307a12022-10-17 18:10:23 +0100495 * On success, this function may allocate a linked list starting at cur->next
David Horstmanned794832022-10-04 18:12:06 +0100496 * that must later be free'd by the caller using mbedtls_free(). In error
497 * cases, this function frees all allocated memory internally and the caller
498 * has no freeing responsibilities.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200499 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100500int mbedtls_x509_get_name(unsigned char **p, const unsigned char *end,
501 mbedtls_x509_name *cur)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200502{
Janos Follath865b3eb2019-12-16 11:46:15 +0000503 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard5d861852014-10-17 12:41:41 +0200504 size_t set_len;
505 const unsigned char *end_set;
David Horstmanned794832022-10-04 18:12:06 +0100506 mbedtls_x509_name *head = cur;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200507
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100508 /* don't use recursion, we'd risk stack overflow if not optimized */
Gilles Peskine449bd832023-01-11 14:50:10 +0100509 while (1) {
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100510 /*
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000511 * parse SET
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100512 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100513 if ((ret = mbedtls_asn1_get_tag(p, end, &set_len,
514 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SET)) != 0) {
515 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME, ret);
David Horstmanned794832022-10-04 18:12:06 +0100516 goto error;
517 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200518
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100519 end_set = *p + set_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200520
Gilles Peskine449bd832023-01-11 14:50:10 +0100521 while (1) {
522 if ((ret = x509_get_attr_type_value(p, end_set, cur)) != 0) {
David Horstmanned794832022-10-04 18:12:06 +0100523 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +0100524 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200525
Gilles Peskine449bd832023-01-11 14:50:10 +0100526 if (*p == end_set) {
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000527 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100528 }
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000529
Manuel Pégourié-Gonnardeecb43c2015-05-12 12:56:41 +0200530 /* Mark this item as being no the only one in a set */
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000531 cur->next_merged = 1;
532
Gilles Peskine449bd832023-01-11 14:50:10 +0100533 cur->next = mbedtls_calloc(1, sizeof(mbedtls_x509_name));
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000534
Gilles Peskine449bd832023-01-11 14:50:10 +0100535 if (cur->next == NULL) {
David Horstmanned794832022-10-04 18:12:06 +0100536 ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
537 goto error;
538 }
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000539
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000540 cur = cur->next;
541 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200542
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100543 /*
544 * continue until end of SEQUENCE is reached
545 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100546 if (*p == end) {
547 return 0;
548 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200549
Gilles Peskine449bd832023-01-11 14:50:10 +0100550 cur->next = mbedtls_calloc(1, sizeof(mbedtls_x509_name));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200551
Gilles Peskine449bd832023-01-11 14:50:10 +0100552 if (cur->next == NULL) {
David Horstmanned794832022-10-04 18:12:06 +0100553 ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
554 goto error;
555 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200556
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100557 cur = cur->next;
558 }
David Horstmanned794832022-10-04 18:12:06 +0100559
560error:
David Horstmanned794832022-10-04 18:12:06 +0100561 /* Skip the first element as we did not allocate it */
Gilles Peskine449bd832023-01-11 14:50:10 +0100562 mbedtls_asn1_free_named_data_list_shallow(head->next);
Glenn Straussa4b40412022-06-26 19:32:09 -0400563 head->next = NULL;
David Horstmanned794832022-10-04 18:12:06 +0100564
Gilles Peskine449bd832023-01-11 14:50:10 +0100565 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200566}
567
Gilles Peskine449bd832023-01-11 14:50:10 +0100568static int x509_parse_int(unsigned char **p, size_t n, int *res)
Janos Follath87c98072017-02-03 12:36:59 +0000569{
Rich Evans7d5a55a2015-02-13 11:48:02 +0000570 *res = 0;
Janos Follath87c98072017-02-03 12:36:59 +0000571
Gilles Peskine449bd832023-01-11 14:50:10 +0100572 for (; n > 0; --n) {
573 if ((**p < '0') || (**p > '9')) {
574 return MBEDTLS_ERR_X509_INVALID_DATE;
575 }
Janos Follath87c98072017-02-03 12:36:59 +0000576
Rich Evans7d5a55a2015-02-13 11:48:02 +0000577 *res *= 10;
Gilles Peskine449bd832023-01-11 14:50:10 +0100578 *res += (*(*p)++ - '0');
Rich Evans7d5a55a2015-02-13 11:48:02 +0000579 }
Janos Follath87c98072017-02-03 12:36:59 +0000580
Gilles Peskine449bd832023-01-11 14:50:10 +0100581 return 0;
Rich Evans7d5a55a2015-02-13 11:48:02 +0000582}
583
Gilles Peskine449bd832023-01-11 14:50:10 +0100584static int x509_date_is_valid(const mbedtls_x509_time *t)
Andres AG4b76aec2016-09-23 13:16:02 +0100585{
586 int ret = MBEDTLS_ERR_X509_INVALID_DATE;
Andres Amaya Garcia735b37e2016-11-21 15:38:02 +0000587 int month_len;
Andres AG4b76aec2016-09-23 13:16:02 +0100588
Gilles Peskine449bd832023-01-11 14:50:10 +0100589 CHECK_RANGE(0, 9999, t->year);
590 CHECK_RANGE(0, 23, t->hour);
591 CHECK_RANGE(0, 59, t->min);
592 CHECK_RANGE(0, 59, t->sec);
Andres AG4b76aec2016-09-23 13:16:02 +0100593
Gilles Peskine449bd832023-01-11 14:50:10 +0100594 switch (t->mon) {
Andres AG4b76aec2016-09-23 13:16:02 +0100595 case 1: case 3: case 5: case 7: case 8: case 10: case 12:
Andres Amaya Garcia735b37e2016-11-21 15:38:02 +0000596 month_len = 31;
Andres AG4b76aec2016-09-23 13:16:02 +0100597 break;
598 case 4: case 6: case 9: case 11:
Andres Amaya Garcia735b37e2016-11-21 15:38:02 +0000599 month_len = 30;
Andres AG4b76aec2016-09-23 13:16:02 +0100600 break;
601 case 2:
Gilles Peskine449bd832023-01-11 14:50:10 +0100602 if ((!(t->year % 4) && t->year % 100) ||
603 !(t->year % 400)) {
Andres Amaya Garcia735b37e2016-11-21 15:38:02 +0000604 month_len = 29;
Gilles Peskine449bd832023-01-11 14:50:10 +0100605 } else {
Andres Amaya Garcia735b37e2016-11-21 15:38:02 +0000606 month_len = 28;
Gilles Peskine449bd832023-01-11 14:50:10 +0100607 }
Andres AG4b76aec2016-09-23 13:16:02 +0100608 break;
609 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100610 return ret;
Andres AG4b76aec2016-09-23 13:16:02 +0100611 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100612 CHECK_RANGE(1, month_len, t->day);
Andres AG4b76aec2016-09-23 13:16:02 +0100613
Gilles Peskine449bd832023-01-11 14:50:10 +0100614 return 0;
Andres AG4b76aec2016-09-23 13:16:02 +0100615}
616
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200617/*
Janos Follath87c98072017-02-03 12:36:59 +0000618 * Parse an ASN1_UTC_TIME (yearlen=2) or ASN1_GENERALIZED_TIME (yearlen=4)
619 * field.
620 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100621static int x509_parse_time(unsigned char **p, size_t len, size_t yearlen,
622 mbedtls_x509_time *tm)
Janos Follath87c98072017-02-03 12:36:59 +0000623{
Janos Follath865b3eb2019-12-16 11:46:15 +0000624 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Janos Follath87c98072017-02-03 12:36:59 +0000625
626 /*
627 * Minimum length is 10 or 12 depending on yearlen
628 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100629 if (len < yearlen + 8) {
630 return MBEDTLS_ERR_X509_INVALID_DATE;
631 }
Janos Follath87c98072017-02-03 12:36:59 +0000632 len -= yearlen + 8;
633
634 /*
635 * Parse year, month, day, hour, minute
636 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100637 CHECK(x509_parse_int(p, yearlen, &tm->year));
638 if (2 == yearlen) {
639 if (tm->year < 50) {
Hanno Becker61937d42017-04-26 15:01:23 +0100640 tm->year += 100;
Gilles Peskine449bd832023-01-11 14:50:10 +0100641 }
Janos Follath87c98072017-02-03 12:36:59 +0000642
Hanno Becker61937d42017-04-26 15:01:23 +0100643 tm->year += 1900;
Janos Follath87c98072017-02-03 12:36:59 +0000644 }
645
Gilles Peskine449bd832023-01-11 14:50:10 +0100646 CHECK(x509_parse_int(p, 2, &tm->mon));
647 CHECK(x509_parse_int(p, 2, &tm->day));
648 CHECK(x509_parse_int(p, 2, &tm->hour));
649 CHECK(x509_parse_int(p, 2, &tm->min));
Janos Follath87c98072017-02-03 12:36:59 +0000650
651 /*
652 * Parse seconds if present
653 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100654 if (len >= 2) {
655 CHECK(x509_parse_int(p, 2, &tm->sec));
Janos Follath87c98072017-02-03 12:36:59 +0000656 len -= 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100657 } else {
658 return MBEDTLS_ERR_X509_INVALID_DATE;
Janos Follath87c98072017-02-03 12:36:59 +0000659 }
Janos Follath87c98072017-02-03 12:36:59 +0000660
661 /*
662 * Parse trailing 'Z' if present
663 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100664 if (1 == len && 'Z' == **p) {
Janos Follath87c98072017-02-03 12:36:59 +0000665 (*p)++;
666 len--;
667 }
668
669 /*
670 * We should have parsed all characters at this point
671 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100672 if (0 != len) {
673 return MBEDTLS_ERR_X509_INVALID_DATE;
674 }
Janos Follath87c98072017-02-03 12:36:59 +0000675
Gilles Peskine449bd832023-01-11 14:50:10 +0100676 CHECK(x509_date_is_valid(tm));
Janos Follath87c98072017-02-03 12:36:59 +0000677
Gilles Peskine449bd832023-01-11 14:50:10 +0100678 return 0;
Janos Follath87c98072017-02-03 12:36:59 +0000679}
680
681/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200682 * Time ::= CHOICE {
683 * utcTime UTCTime,
684 * generalTime GeneralizedTime }
685 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100686int mbedtls_x509_get_time(unsigned char **p, const unsigned char *end,
687 mbedtls_x509_time *tm)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200688{
Janos Follath865b3eb2019-12-16 11:46:15 +0000689 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Janos Follath87c98072017-02-03 12:36:59 +0000690 size_t len, year_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200691 unsigned char tag;
692
Gilles Peskine449bd832023-01-11 14:50:10 +0100693 if ((end - *p) < 1) {
694 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE,
695 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
696 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200697
698 tag = **p;
699
Gilles Peskine449bd832023-01-11 14:50:10 +0100700 if (tag == MBEDTLS_ASN1_UTC_TIME) {
Janos Follath87c98072017-02-03 12:36:59 +0000701 year_len = 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100702 } else if (tag == MBEDTLS_ASN1_GENERALIZED_TIME) {
Janos Follath87c98072017-02-03 12:36:59 +0000703 year_len = 4;
Gilles Peskine449bd832023-01-11 14:50:10 +0100704 } else {
705 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE,
706 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
707 }
Janos Follath87c98072017-02-03 12:36:59 +0000708
709 (*p)++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100710 ret = mbedtls_asn1_get_len(p, end, &len);
Janos Follath87c98072017-02-03 12:36:59 +0000711
Gilles Peskine449bd832023-01-11 14:50:10 +0100712 if (ret != 0) {
713 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE, ret);
714 }
Janos Follath87c98072017-02-03 12:36:59 +0000715
Gilles Peskine449bd832023-01-11 14:50:10 +0100716 return x509_parse_time(p, len, year_len, tm);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200717}
718
Gilles Peskine449bd832023-01-11 14:50:10 +0100719int mbedtls_x509_get_sig(unsigned char **p, const unsigned char *end, mbedtls_x509_buf *sig)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200720{
Janos Follath865b3eb2019-12-16 11:46:15 +0000721 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200722 size_t len;
Andres AG4bdbe092016-09-19 16:58:45 +0100723 int tag_type;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200724
Gilles Peskine449bd832023-01-11 14:50:10 +0100725 if ((end - *p) < 1) {
726 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_SIGNATURE,
727 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
728 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200729
Andres AG4bdbe092016-09-19 16:58:45 +0100730 tag_type = **p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200731
Gilles Peskine449bd832023-01-11 14:50:10 +0100732 if ((ret = mbedtls_asn1_get_bitstring_null(p, end, &len)) != 0) {
733 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_SIGNATURE, ret);
734 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200735
Andres AG4bdbe092016-09-19 16:58:45 +0100736 sig->tag = tag_type;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200737 sig->len = len;
738 sig->p = *p;
739
740 *p += len;
741
Gilles Peskine449bd832023-01-11 14:50:10 +0100742 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200743}
744
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100745/*
746 * Get signature algorithm from alg OID and optional parameters
747 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100748int mbedtls_x509_get_sig_alg(const mbedtls_x509_buf *sig_oid, const mbedtls_x509_buf *sig_params,
749 mbedtls_md_type_t *md_alg, mbedtls_pk_type_t *pk_alg,
750 void **sig_opts)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200751{
Janos Follath865b3eb2019-12-16 11:46:15 +0000752 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200753
Gilles Peskine449bd832023-01-11 14:50:10 +0100754 if (*sig_opts != NULL) {
755 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
756 }
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200757
Gilles Peskine449bd832023-01-11 14:50:10 +0100758 if ((ret = mbedtls_oid_get_sig_alg(sig_oid, md_alg, pk_alg)) != 0) {
759 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG, ret);
760 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200761
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200762#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100763 if (*pk_alg == MBEDTLS_PK_RSASSA_PSS) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200764 mbedtls_pk_rsassa_pss_options *pss_opts;
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100765
Gilles Peskine449bd832023-01-11 14:50:10 +0100766 pss_opts = mbedtls_calloc(1, sizeof(mbedtls_pk_rsassa_pss_options));
767 if (pss_opts == NULL) {
768 return MBEDTLS_ERR_X509_ALLOC_FAILED;
769 }
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200770
Gilles Peskine449bd832023-01-11 14:50:10 +0100771 ret = mbedtls_x509_get_rsassa_pss_params(sig_params,
772 md_alg,
773 &pss_opts->mgf1_hash_id,
774 &pss_opts->expected_salt_len);
775 if (ret != 0) {
776 mbedtls_free(pss_opts);
777 return ret;
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200778 }
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100779
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200780 *sig_opts = (void *) pss_opts;
Gilles Peskine449bd832023-01-11 14:50:10 +0100781 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200782#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100783 {
784 /* Make sure parameters are absent or NULL */
Gilles Peskine449bd832023-01-11 14:50:10 +0100785 if ((sig_params->tag != MBEDTLS_ASN1_NULL && sig_params->tag != 0) ||
786 sig_params->len != 0) {
787 return MBEDTLS_ERR_X509_INVALID_ALG;
788 }
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100789 }
790
Gilles Peskine449bd832023-01-11 14:50:10 +0100791 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200792}
793
794/*
795 * X.509 Extensions (No parsing of extensions, pointer should
Brian J Murray1903fb32016-11-06 04:45:15 -0800796 * be either manually updated or extensions should be parsed!)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200797 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100798int mbedtls_x509_get_ext(unsigned char **p, const unsigned char *end,
799 mbedtls_x509_buf *ext, int tag)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200800{
Janos Follath865b3eb2019-12-16 11:46:15 +0000801 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200802 size_t len;
803
Hanno Becker3cddba82019-02-11 14:33:36 +0000804 /* Extension structure use EXPLICIT tagging. That is, the actual
805 * `Extensions` structure is wrapped by a tag-length pair using
806 * the respective context-specific tag. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100807 ret = mbedtls_asn1_get_tag(p, end, &ext->len,
808 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | tag);
809 if (ret != 0) {
810 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
811 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200812
Hanno Becker6ccfb182019-02-12 11:52:10 +0000813 ext->tag = MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | tag;
814 ext->p = *p;
815 end = *p + ext->len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200816
817 /*
818 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200819 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100820 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
821 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
822 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
823 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200824
Gilles Peskine449bd832023-01-11 14:50:10 +0100825 if (end != *p + len) {
826 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
827 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
828 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200829
Gilles Peskine449bd832023-01-11 14:50:10 +0100830 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200831}
832
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200833/*
834 * Store the name in printable form into buf; no more
835 * than size characters will be written
836 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100837int mbedtls_x509_dn_gets(char *buf, size_t size, const mbedtls_x509_name *dn)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200838{
Janos Follath865b3eb2019-12-16 11:46:15 +0000839 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Werner Lewisb33dacd2022-05-20 12:48:46 +0100840 size_t i, j, n;
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000841 unsigned char c, merge = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200842 const mbedtls_x509_name *name;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200843 const char *short_name = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200844 char s[MBEDTLS_X509_MAX_DN_NAME_SIZE], *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200845
Gilles Peskine449bd832023-01-11 14:50:10 +0100846 memset(s, 0, sizeof(s));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200847
848 name = dn;
849 p = buf;
850 n = size;
851
Gilles Peskine449bd832023-01-11 14:50:10 +0100852 while (name != NULL) {
853 if (!name->oid.p) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200854 name = name->next;
855 continue;
856 }
857
Gilles Peskine449bd832023-01-11 14:50:10 +0100858 if (name != dn) {
859 ret = mbedtls_snprintf(p, n, merge ? " + " : ", ");
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200860 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200861 }
862
Gilles Peskine449bd832023-01-11 14:50:10 +0100863 ret = mbedtls_oid_get_attr_short_name(&name->oid, &short_name);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200864
Gilles Peskine449bd832023-01-11 14:50:10 +0100865 if (ret == 0) {
866 ret = mbedtls_snprintf(p, n, "%s=", short_name);
867 } else {
868 ret = mbedtls_snprintf(p, n, "\?\?=");
869 }
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200870 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200871
Gilles Peskine449bd832023-01-11 14:50:10 +0100872 for (i = 0, j = 0; i < name->val.len; i++, j++) {
873 if (j >= sizeof(s) - 1) {
874 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
875 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200876
877 c = name->val.p[i];
Werner Lewisb33dacd2022-05-20 12:48:46 +0100878 // Special characters requiring escaping, RFC 1779
Gilles Peskine449bd832023-01-11 14:50:10 +0100879 if (c && strchr(",=+<>#;\"\\", c)) {
880 if (j + 1 >= sizeof(s) - 1) {
881 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
882 }
Werner Lewisb33dacd2022-05-20 12:48:46 +0100883 s[j++] = '\\';
884 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100885 if (c < 32 || c >= 127) {
886 s[j] = '?';
887 } else {
888 s[j] = c;
889 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200890 }
Werner Lewisb33dacd2022-05-20 12:48:46 +0100891 s[j] = '\0';
Gilles Peskine449bd832023-01-11 14:50:10 +0100892 ret = mbedtls_snprintf(p, n, "%s", s);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200893 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000894
895 merge = name->next_merged;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200896 name = name->next;
897 }
898
Gilles Peskine449bd832023-01-11 14:50:10 +0100899 return (int) (size - n);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200900}
901
902/*
903 * Store the serial in printable form into buf; no more
904 * than size characters will be written
905 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100906int mbedtls_x509_serial_gets(char *buf, size_t size, const mbedtls_x509_buf *serial)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200907{
Janos Follath865b3eb2019-12-16 11:46:15 +0000908 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200909 size_t i, n, nr;
910 char *p;
911
912 p = buf;
913 n = size;
914
Gilles Peskine449bd832023-01-11 14:50:10 +0100915 nr = (serial->len <= 32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200916 ? serial->len : 28;
917
Gilles Peskine449bd832023-01-11 14:50:10 +0100918 for (i = 0; i < nr; i++) {
919 if (i == 0 && nr > 1 && serial->p[i] == 0x0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200920 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100921 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200922
Gilles Peskine449bd832023-01-11 14:50:10 +0100923 ret = mbedtls_snprintf(p, n, "%02X%s",
924 serial->p[i], (i < nr - 1) ? ":" : "");
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200925 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200926 }
927
Gilles Peskine449bd832023-01-11 14:50:10 +0100928 if (nr != serial->len) {
929 ret = mbedtls_snprintf(p, n, "....");
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200930 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200931 }
932
Gilles Peskine449bd832023-01-11 14:50:10 +0100933 return (int) (size - n);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200934}
935
Hanno Becker612a2f12020-10-09 09:19:39 +0100936#if !defined(MBEDTLS_X509_REMOVE_INFO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200937/*
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200938 * Helper for writing signature algorithms
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100939 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100940int mbedtls_x509_sig_alg_gets(char *buf, size_t size, const mbedtls_x509_buf *sig_oid,
941 mbedtls_pk_type_t pk_alg, mbedtls_md_type_t md_alg,
942 const void *sig_opts)
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100943{
Janos Follath865b3eb2019-12-16 11:46:15 +0000944 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100945 char *p = buf;
946 size_t n = size;
947 const char *desc = NULL;
948
Gilles Peskine449bd832023-01-11 14:50:10 +0100949 ret = mbedtls_oid_get_sig_alg_desc(sig_oid, &desc);
950 if (ret != 0) {
951 ret = mbedtls_snprintf(p, n, "???");
952 } else {
953 ret = mbedtls_snprintf(p, n, "%s", desc);
954 }
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200955 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100956
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200957#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100958 if (pk_alg == MBEDTLS_PK_RSASSA_PSS) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200959 const mbedtls_pk_rsassa_pss_options *pss_opts;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100960
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200961 pss_opts = (const mbedtls_pk_rsassa_pss_options *) sig_opts;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100962
Gilles Peskine449bd832023-01-11 14:50:10 +0100963 const char *name = md_type_to_string(md_alg);
964 const char *mgf_name = md_type_to_string(pss_opts->mgf1_hash_id);
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100965
Gilles Peskine449bd832023-01-11 14:50:10 +0100966 ret = mbedtls_snprintf(p, n, " (%s, MGF1-%s, 0x%02X)",
967 name ? name : "???",
968 mgf_name ? mgf_name : "???",
969 (unsigned int) pss_opts->expected_salt_len);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200970 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100971 }
972#else
973 ((void) pk_alg);
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200974 ((void) md_alg);
975 ((void) sig_opts);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200976#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100977
Gilles Peskine449bd832023-01-11 14:50:10 +0100978 return (int) (size - n);
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100979}
Hanno Becker612a2f12020-10-09 09:19:39 +0100980#endif /* MBEDTLS_X509_REMOVE_INFO */
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100981
982/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200983 * Helper for writing "RSA key size", "EC key size", etc
984 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100985int mbedtls_x509_key_size_helper(char *buf, size_t buf_size, const char *name)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200986{
987 char *p = buf;
Manuel Pégourié-Gonnardfb317c52015-06-18 16:25:56 +0200988 size_t n = buf_size;
Janos Follath865b3eb2019-12-16 11:46:15 +0000989 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200990
Gilles Peskine449bd832023-01-11 14:50:10 +0100991 ret = mbedtls_snprintf(p, n, "%s key size", name);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200992 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200993
Gilles Peskine449bd832023-01-11 14:50:10 +0100994 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200995}
996
Manuel Pégourié-Gonnard60c793b2015-06-18 20:52:58 +0200997#if defined(MBEDTLS_HAVE_TIME_DATE)
Manuel Pégourié-Gonnard57e10d72015-06-22 18:59:21 +0200998/*
999 * Set the time structure to the current time.
1000 * Return 0 on success, non-zero on failure.
1001 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001002static int x509_get_current_time(mbedtls_x509_time *now)
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001003{
Nicholas Wilson512b4ee2017-12-05 12:07:33 +00001004 struct tm *lt, tm_buf;
SimonBd5800b72016-04-26 07:43:27 +01001005 mbedtls_time_t tt;
Manuel Pégourié-Gonnard57e10d72015-06-22 18:59:21 +02001006 int ret = 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001007
Gilles Peskine449bd832023-01-11 14:50:10 +01001008 tt = mbedtls_time(NULL);
1009 lt = mbedtls_platform_gmtime_r(&tt, &tm_buf);
Manuel Pégourié-Gonnard864108d2015-05-29 10:11:03 +02001010
Gilles Peskine449bd832023-01-11 14:50:10 +01001011 if (lt == NULL) {
Manuel Pégourié-Gonnard57e10d72015-06-22 18:59:21 +02001012 ret = -1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001013 } else {
Manuel Pégourié-Gonnard57e10d72015-06-22 18:59:21 +02001014 now->year = lt->tm_year + 1900;
1015 now->mon = lt->tm_mon + 1;
1016 now->day = lt->tm_mday;
1017 now->hour = lt->tm_hour;
1018 now->min = lt->tm_min;
1019 now->sec = lt->tm_sec;
1020 }
Manuel Pégourié-Gonnard864108d2015-05-29 10:11:03 +02001021
Gilles Peskine449bd832023-01-11 14:50:10 +01001022 return ret;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001023}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001024
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001025/*
1026 * Return 0 if before <= after, 1 otherwise
1027 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001028static int x509_check_time(const mbedtls_x509_time *before, const mbedtls_x509_time *after)
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001029{
Gilles Peskine449bd832023-01-11 14:50:10 +01001030 if (before->year > after->year) {
1031 return 1;
1032 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001033
Gilles Peskine449bd832023-01-11 14:50:10 +01001034 if (before->year == after->year &&
1035 before->mon > after->mon) {
1036 return 1;
1037 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001038
Gilles Peskine449bd832023-01-11 14:50:10 +01001039 if (before->year == after->year &&
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001040 before->mon == after->mon &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001041 before->day > after->day) {
1042 return 1;
1043 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001044
Gilles Peskine449bd832023-01-11 14:50:10 +01001045 if (before->year == after->year &&
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001046 before->mon == after->mon &&
1047 before->day == after->day &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001048 before->hour > after->hour) {
1049 return 1;
1050 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001051
Gilles Peskine449bd832023-01-11 14:50:10 +01001052 if (before->year == after->year &&
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001053 before->mon == after->mon &&
1054 before->day == after->day &&
1055 before->hour == after->hour &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001056 before->min > after->min) {
1057 return 1;
1058 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001059
Gilles Peskine449bd832023-01-11 14:50:10 +01001060 if (before->year == after->year &&
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001061 before->mon == after->mon &&
1062 before->day == after->day &&
1063 before->hour == after->hour &&
1064 before->min == after->min &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001065 before->sec > after->sec) {
1066 return 1;
1067 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001068
Gilles Peskine449bd832023-01-11 14:50:10 +01001069 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001070}
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001071
Gilles Peskine449bd832023-01-11 14:50:10 +01001072int mbedtls_x509_time_is_past(const mbedtls_x509_time *to)
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001073{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001074 mbedtls_x509_time now;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001075
Gilles Peskine449bd832023-01-11 14:50:10 +01001076 if (x509_get_current_time(&now) != 0) {
1077 return 1;
1078 }
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001079
Gilles Peskine449bd832023-01-11 14:50:10 +01001080 return x509_check_time(&now, to);
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001081}
1082
Gilles Peskine449bd832023-01-11 14:50:10 +01001083int mbedtls_x509_time_is_future(const mbedtls_x509_time *from)
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001084{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001085 mbedtls_x509_time now;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001086
Gilles Peskine449bd832023-01-11 14:50:10 +01001087 if (x509_get_current_time(&now) != 0) {
1088 return 1;
1089 }
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001090
Gilles Peskine449bd832023-01-11 14:50:10 +01001091 return x509_check_time(from, &now);
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001092}
1093
Manuel Pégourié-Gonnard60c793b2015-06-18 20:52:58 +02001094#else /* MBEDTLS_HAVE_TIME_DATE */
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001095
Gilles Peskine449bd832023-01-11 14:50:10 +01001096int mbedtls_x509_time_is_past(const mbedtls_x509_time *to)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001097{
1098 ((void) to);
Gilles Peskine449bd832023-01-11 14:50:10 +01001099 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001100}
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001101
Gilles Peskine449bd832023-01-11 14:50:10 +01001102int mbedtls_x509_time_is_future(const mbedtls_x509_time *from)
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001103{
1104 ((void) from);
Gilles Peskine449bd832023-01-11 14:50:10 +01001105 return 0;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001106}
Manuel Pégourié-Gonnard60c793b2015-06-18 20:52:58 +02001107#endif /* MBEDTLS_HAVE_TIME_DATE */
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001108
1109/* Common functions for parsing CRT and CSR. */
1110#if defined(MBEDTLS_X509_CRT_PARSE_C) || defined(MBEDTLS_X509_CSR_PARSE_C)
1111/*
1112 * OtherName ::= SEQUENCE {
1113 * type-id OBJECT IDENTIFIER,
1114 * value [0] EXPLICIT ANY DEFINED BY type-id }
1115 *
1116 * HardwareModuleName ::= SEQUENCE {
1117 * hwType OBJECT IDENTIFIER,
1118 * hwSerialNum OCTET STRING }
1119 *
1120 * NOTE: we currently only parse and use otherName of type HwModuleName,
1121 * as defined in RFC 4108.
1122 */
1123static int x509_get_other_name(const mbedtls_x509_buf *subject_alt_name,
1124 mbedtls_x509_san_other_name *other_name)
1125{
1126 int ret = 0;
1127 size_t len;
1128 unsigned char *p = subject_alt_name->p;
1129 const unsigned char *end = p + subject_alt_name->len;
1130 mbedtls_x509_buf cur_oid;
1131
1132 if ((subject_alt_name->tag &
1133 (MBEDTLS_ASN1_TAG_CLASS_MASK | MBEDTLS_ASN1_TAG_VALUE_MASK)) !=
1134 (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME)) {
1135 /*
1136 * The given subject alternative name is not of type "othername".
1137 */
1138 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1139 }
1140
1141 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1142 MBEDTLS_ASN1_OID)) != 0) {
1143 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1144 }
1145
1146 cur_oid.tag = MBEDTLS_ASN1_OID;
1147 cur_oid.p = p;
1148 cur_oid.len = len;
1149
1150 /*
1151 * Only HwModuleName is currently supported.
1152 */
1153 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ON_HW_MODULE_NAME, &cur_oid) != 0) {
1154 return MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
1155 }
1156
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001157 p += len;
1158 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1159 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC)) !=
1160 0) {
1161 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1162 }
1163
Hanno Beckerdae916b2019-09-13 14:21:13 +01001164 if (end != p + len) {
1165 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1166 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1167 }
1168
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001169 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1170 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1171 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1172 }
1173
Hanno Beckerdae916b2019-09-13 14:21:13 +01001174 if (end != p + len) {
1175 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1176 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1177 }
1178
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001179 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OID)) != 0) {
1180 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1181 }
1182
1183 other_name->value.hardware_module_name.oid.tag = MBEDTLS_ASN1_OID;
1184 other_name->value.hardware_module_name.oid.p = p;
1185 other_name->value.hardware_module_name.oid.len = len;
1186
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001187 p += len;
1188 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1189 MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1190 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1191 }
1192
1193 other_name->value.hardware_module_name.val.tag = MBEDTLS_ASN1_OCTET_STRING;
1194 other_name->value.hardware_module_name.val.p = p;
1195 other_name->value.hardware_module_name.val.len = len;
1196 p += len;
1197 if (p != end) {
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001198 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1199 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1200 }
1201 return 0;
1202}
1203
Przemek Stekiel21903ec2023-02-21 08:32:37 +01001204/* Check mbedtls_x509_get_subject_alt_name for detailed description.
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001205 *
Przemek Stekiel21903ec2023-02-21 08:32:37 +01001206 * In some cases while parsing subject alternative names the sequence tag is optional
1207 * (e.g. CertSerialNumber). This function is designed to handle such case.
1208*/
1209int mbedtls_x509_get_subject_alt_name_ext(unsigned char **p,
1210 const unsigned char *end,
1211 mbedtls_x509_sequence *subject_alt_name)
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001212{
1213 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel21903ec2023-02-21 08:32:37 +01001214 size_t tag_len;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001215 mbedtls_asn1_sequence *cur = subject_alt_name;
1216
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001217 while (*p < end) {
1218 mbedtls_x509_subject_alternative_name dummy_san_buf;
Hanno Beckerae8f8c42019-09-13 12:24:56 +01001219 mbedtls_x509_buf tmp_san_buf;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001220 memset(&dummy_san_buf, 0, sizeof(dummy_san_buf));
1221
Hanno Beckerae8f8c42019-09-13 12:24:56 +01001222 tmp_san_buf.tag = **p;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001223 (*p)++;
Hanno Beckerae8f8c42019-09-13 12:24:56 +01001224
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001225 if ((ret = mbedtls_asn1_get_len(p, end, &tag_len)) != 0) {
1226 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1227 }
1228
Hanno Beckerae8f8c42019-09-13 12:24:56 +01001229 tmp_san_buf.p = *p;
1230 tmp_san_buf.len = tag_len;
1231
1232 if ((tmp_san_buf.tag & MBEDTLS_ASN1_TAG_CLASS_MASK) !=
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001233 MBEDTLS_ASN1_CONTEXT_SPECIFIC) {
1234 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1235 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
1236 }
1237
1238 /*
1239 * Check that the SAN is structured correctly.
1240 */
Hanno Beckerae8f8c42019-09-13 12:24:56 +01001241 ret = mbedtls_x509_parse_subject_alt_name(&tmp_san_buf, &dummy_san_buf);
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001242 /*
1243 * In case the extension is malformed, return an error,
1244 * and clear the allocated sequences.
1245 */
1246 if (ret != 0 && ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE) {
1247 mbedtls_asn1_sequence_free(subject_alt_name->next);
1248 subject_alt_name->next = NULL;
1249 return ret;
1250 }
1251
Andrzej Kurekd40c2b62023-02-13 07:01:59 -05001252 mbedtls_x509_free_subject_alt_name(&dummy_san_buf);
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001253 /* Allocate and assign next pointer */
1254 if (cur->buf.p != NULL) {
1255 if (cur->next != NULL) {
1256 return MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
1257 }
1258
1259 cur->next = mbedtls_calloc(1, sizeof(mbedtls_asn1_sequence));
1260
1261 if (cur->next == NULL) {
1262 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1263 MBEDTLS_ERR_ASN1_ALLOC_FAILED);
1264 }
1265
1266 cur = cur->next;
1267 }
1268
Hanno Beckerae8f8c42019-09-13 12:24:56 +01001269 cur->buf = tmp_san_buf;
1270 *p += tmp_san_buf.len;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001271 }
1272
1273 /* Set final sequence entry's next pointer to NULL */
1274 cur->next = NULL;
1275
1276 if (*p != end) {
1277 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1278 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1279 }
1280
1281 return 0;
1282}
1283
Przemek Stekiel21903ec2023-02-21 08:32:37 +01001284/*
1285 * SubjectAltName ::= GeneralNames
1286 *
1287 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
1288 *
1289 * GeneralName ::= CHOICE {
1290 * otherName [0] OtherName,
1291 * rfc822Name [1] IA5String,
1292 * dNSName [2] IA5String,
1293 * x400Address [3] ORAddress,
1294 * directoryName [4] Name,
1295 * ediPartyName [5] EDIPartyName,
1296 * uniformResourceIdentifier [6] IA5String,
1297 * iPAddress [7] OCTET STRING,
1298 * registeredID [8] OBJECT IDENTIFIER }
1299 *
1300 * OtherName ::= SEQUENCE {
1301 * type-id OBJECT IDENTIFIER,
1302 * value [0] EXPLICIT ANY DEFINED BY type-id }
1303 *
1304 * EDIPartyName ::= SEQUENCE {
1305 * nameAssigner [0] DirectoryString OPTIONAL,
1306 * partyName [1] DirectoryString }
1307 *
1308 * We list all types, but use the following GeneralName types from RFC 5280:
1309 * "dnsName", "uniformResourceIdentifier" and "hardware_module_name"
1310 * of type "otherName", as defined in RFC 4108.
1311 */
1312int mbedtls_x509_get_subject_alt_name(unsigned char **p,
1313 const unsigned char *end,
1314 mbedtls_x509_sequence *subject_alt_name)
1315{
1316 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1317 size_t len;
1318
1319 /* Get main sequence tag */
1320 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
1321 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1322 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1323 }
1324
1325 if (*p + len != end) {
1326 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1327 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1328 }
1329
1330 return mbedtls_x509_get_subject_alt_name_ext(p, end, subject_alt_name);
1331}
1332
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001333int mbedtls_x509_get_ns_cert_type(unsigned char **p,
1334 const unsigned char *end,
1335 unsigned char *ns_cert_type)
1336{
1337 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1338 mbedtls_x509_bitstring bs = { 0, 0, NULL };
1339
1340 if ((ret = mbedtls_asn1_get_bitstring(p, end, &bs)) != 0) {
1341 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1342 }
1343
Przemek Stekiel32e20912023-01-25 14:26:15 +01001344 /* A bitstring with no flags set is still technically valid, as it will mean
1345 that the certificate has no designated purpose at the time of creation. */
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001346 if (bs.len == 0) {
1347 *ns_cert_type = 0;
1348 return 0;
1349 }
1350
1351 if (bs.len != 1) {
1352 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1353 MBEDTLS_ERR_ASN1_INVALID_LENGTH);
1354 }
1355
1356 /* Get actual bitstring */
1357 *ns_cert_type = *bs.p;
1358 return 0;
1359}
1360
1361int mbedtls_x509_get_key_usage(unsigned char **p,
1362 const unsigned char *end,
1363 unsigned int *key_usage)
1364{
1365 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1366 size_t i;
1367 mbedtls_x509_bitstring bs = { 0, 0, NULL };
1368
1369 if ((ret = mbedtls_asn1_get_bitstring(p, end, &bs)) != 0) {
1370 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1371 }
1372
Przemek Stekiel32e20912023-01-25 14:26:15 +01001373 /* A bitstring with no flags set is still technically valid, as it will mean
1374 that the certificate has no designated purpose at the time of creation. */
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001375 if (bs.len == 0) {
1376 *key_usage = 0;
1377 return 0;
1378 }
1379
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001380 /* Get actual bitstring */
1381 *key_usage = 0;
1382 for (i = 0; i < bs.len && i < sizeof(unsigned int); i++) {
1383 *key_usage |= (unsigned int) bs.p[i] << (8*i);
1384 }
1385
1386 return 0;
1387}
1388
1389int mbedtls_x509_parse_subject_alt_name(const mbedtls_x509_buf *san_buf,
1390 mbedtls_x509_subject_alternative_name *san)
1391{
1392 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1393 switch (san_buf->tag &
1394 (MBEDTLS_ASN1_TAG_CLASS_MASK |
1395 MBEDTLS_ASN1_TAG_VALUE_MASK)) {
1396 /*
1397 * otherName
1398 */
1399 case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME):
1400 {
1401 mbedtls_x509_san_other_name other_name;
1402
1403 ret = x509_get_other_name(san_buf, &other_name);
1404 if (ret != 0) {
1405 return ret;
1406 }
1407
1408 memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name));
1409 san->type = MBEDTLS_X509_SAN_OTHER_NAME;
1410 memcpy(&san->san.other_name,
1411 &other_name, sizeof(other_name));
1412
1413 }
1414 break;
Andrzej Kurek7a05fab2023-02-13 10:03:07 -05001415 /*
1416 * uniformResourceIdentifier
1417 */
1418 case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER):
1419 {
1420 memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name));
1421 san->type = MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001422
Andrzej Kurek7a05fab2023-02-13 10:03:07 -05001423 memcpy(&san->san.unstructured_name,
1424 san_buf, sizeof(*san_buf));
1425
1426 }
1427 break;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001428 /*
1429 * dNSName
1430 */
1431 case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_DNS_NAME):
1432 {
1433 memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name));
1434 san->type = MBEDTLS_X509_SAN_DNS_NAME;
1435
1436 memcpy(&san->san.unstructured_name,
1437 san_buf, sizeof(*san_buf));
Przemek Stekielecee12f2023-02-09 14:43:49 +01001438 }
1439 break;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001440
Przemek Stekielecee12f2023-02-09 14:43:49 +01001441 /*
1442 * RFC822 Name
1443 */
1444 case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_RFC822_NAME):
1445 {
1446 memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name));
1447 san->type = MBEDTLS_X509_SAN_RFC822_NAME;
1448 memcpy(&san->san.unstructured_name, san_buf, sizeof(*san_buf));
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001449 }
1450 break;
1451
1452 /*
Andrzej Kureke12b01d2023-01-10 06:47:38 -05001453 * directoryName
1454 */
1455 case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_DIRECTORY_NAME):
1456 {
1457 size_t name_len;
1458 unsigned char *p = san_buf->p;
1459 memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name));
1460 san->type = MBEDTLS_X509_SAN_DIRECTORY_NAME;
1461
1462 ret = mbedtls_asn1_get_tag(&p, p + san_buf->len, &name_len,
1463 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE);
1464
1465 if (ret != 0) {
Andrzej Kurekbf8ccd82023-02-13 07:13:30 -05001466 return ret;
Andrzej Kureke12b01d2023-01-10 06:47:38 -05001467 }
1468
1469 if ((ret = mbedtls_x509_get_name(&p, p + name_len,
1470 &san->san.directory_name)) != 0) {
Andrzej Kurekbf8ccd82023-02-13 07:13:30 -05001471 return ret;
Andrzej Kureke12b01d2023-01-10 06:47:38 -05001472 }
1473 }
1474 break;
1475 /*
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001476 * Type not supported
1477 */
1478 default:
1479 return MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
1480 }
1481 return 0;
1482}
1483
Andrzej Kurekd40c2b62023-02-13 07:01:59 -05001484void mbedtls_x509_free_subject_alt_name(mbedtls_x509_subject_alternative_name *san)
1485{
1486 if (san->type == MBEDTLS_X509_SAN_DIRECTORY_NAME) {
1487 mbedtls_asn1_free_named_data_list_shallow(san->san.directory_name.next);
1488 }
1489}
1490
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001491#if !defined(MBEDTLS_X509_REMOVE_INFO)
1492int mbedtls_x509_info_subject_alt_name(char **buf, size_t *size,
1493 const mbedtls_x509_sequence
1494 *subject_alt_name,
1495 const char *prefix)
1496{
1497 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1498 size_t i;
1499 size_t n = *size;
1500 char *p = *buf;
1501 const mbedtls_x509_sequence *cur = subject_alt_name;
1502 mbedtls_x509_subject_alternative_name san;
1503 int parse_ret;
1504
1505 while (cur != NULL) {
1506 memset(&san, 0, sizeof(san));
1507 parse_ret = mbedtls_x509_parse_subject_alt_name(&cur->buf, &san);
1508 if (parse_ret != 0) {
1509 if (parse_ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE) {
1510 ret = mbedtls_snprintf(p, n, "\n%s <unsupported>", prefix);
1511 MBEDTLS_X509_SAFE_SNPRINTF;
1512 } else {
1513 ret = mbedtls_snprintf(p, n, "\n%s <malformed>", prefix);
1514 MBEDTLS_X509_SAFE_SNPRINTF;
1515 }
1516 cur = cur->next;
1517 continue;
1518 }
1519
1520 switch (san.type) {
1521 /*
1522 * otherName
1523 */
1524 case MBEDTLS_X509_SAN_OTHER_NAME:
1525 {
1526 mbedtls_x509_san_other_name *other_name = &san.san.other_name;
1527
1528 ret = mbedtls_snprintf(p, n, "\n%s otherName :", prefix);
1529 MBEDTLS_X509_SAFE_SNPRINTF;
1530
1531 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ON_HW_MODULE_NAME,
1532 &other_name->value.hardware_module_name.oid) != 0) {
1533 ret = mbedtls_snprintf(p, n, "\n%s hardware module name :", prefix);
1534 MBEDTLS_X509_SAFE_SNPRINTF;
1535 ret =
1536 mbedtls_snprintf(p, n, "\n%s hardware type : ", prefix);
1537 MBEDTLS_X509_SAFE_SNPRINTF;
1538
1539 ret = mbedtls_oid_get_numeric_string(p,
1540 n,
1541 &other_name->value.hardware_module_name.oid);
1542 MBEDTLS_X509_SAFE_SNPRINTF;
1543
1544 ret =
1545 mbedtls_snprintf(p, n, "\n%s hardware serial number : ", prefix);
1546 MBEDTLS_X509_SAFE_SNPRINTF;
1547
1548 for (i = 0; i < other_name->value.hardware_module_name.val.len; i++) {
1549 ret = mbedtls_snprintf(p,
1550 n,
1551 "%02X",
1552 other_name->value.hardware_module_name.val.p[i]);
1553 MBEDTLS_X509_SAFE_SNPRINTF;
1554 }
1555 }/* MBEDTLS_OID_ON_HW_MODULE_NAME */
1556 }
1557 break;
Andrzej Kurek7a05fab2023-02-13 10:03:07 -05001558 /*
1559 * uniformResourceIdentifier
1560 */
1561 case MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER:
1562 {
1563 ret = mbedtls_snprintf(p, n, "\n%s uniformResourceIdentifier : ", prefix);
1564 MBEDTLS_X509_SAFE_SNPRINTF;
1565 if (san.san.unstructured_name.len >= n) {
1566 *p = '\0';
1567 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1568 }
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001569
Andrzej Kurek7a05fab2023-02-13 10:03:07 -05001570 memcpy(p, san.san.unstructured_name.p, san.san.unstructured_name.len);
1571 p += san.san.unstructured_name.len;
1572 n -= san.san.unstructured_name.len;
1573 }
1574 break;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001575 /*
1576 * dNSName
Przemek Stekiel5b9e4162023-02-15 12:56:37 +01001577 * RFC822 Name
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001578 */
1579 case MBEDTLS_X509_SAN_DNS_NAME:
Przemek Stekiel5b9e4162023-02-15 12:56:37 +01001580 case MBEDTLS_X509_SAN_RFC822_NAME:
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001581 {
Przemek Stekiel82d250d2023-02-15 15:00:50 +01001582 const char *dns_name = "dNSName";
1583 const char *rfc822_name = "rfc822Name";
Przemek Stekiel5b9e4162023-02-15 12:56:37 +01001584
Przemek Stekiel82d250d2023-02-15 15:00:50 +01001585 ret = mbedtls_snprintf(p, n,
1586 "\n%s %s : ",
1587 prefix,
1588 san.type ==
1589 MBEDTLS_X509_SAN_DNS_NAME ? dns_name : rfc822_name);
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001590 MBEDTLS_X509_SAFE_SNPRINTF;
1591 if (san.san.unstructured_name.len >= n) {
1592 *p = '\0';
1593 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1594 }
1595
1596 memcpy(p, san.san.unstructured_name.p, san.san.unstructured_name.len);
1597 p += san.san.unstructured_name.len;
1598 n -= san.san.unstructured_name.len;
1599 }
1600 break;
1601
1602 /*
Andrzej Kureke12b01d2023-01-10 06:47:38 -05001603 * directoryName
1604 */
1605 case MBEDTLS_X509_SAN_DIRECTORY_NAME:
1606 {
1607 ret = mbedtls_snprintf(p, n, "\n%s directoryName : ", prefix);
Andrzej Kurek5f0c6e82023-02-27 16:03:41 -05001608 if (ret < 0 || (size_t) ret >= n) {
1609 mbedtls_x509_free_subject_alt_name(&san);
1610 }
1611
Andrzej Kureke12b01d2023-01-10 06:47:38 -05001612 MBEDTLS_X509_SAFE_SNPRINTF;
1613 ret = mbedtls_x509_dn_gets(p, n, &san.san.directory_name);
Andrzej Kurekd40c2b62023-02-13 07:01:59 -05001614
Andrzej Kureke12b01d2023-01-10 06:47:38 -05001615 if (ret < 0) {
Andrzej Kurek5f0c6e82023-02-27 16:03:41 -05001616 mbedtls_x509_free_subject_alt_name(&san);
Andrzej Kureke12b01d2023-01-10 06:47:38 -05001617 return ret;
1618 }
1619
1620 p += ret;
1621 n -= ret;
1622 }
1623 break;
1624 /*
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001625 * Type not supported, skip item.
1626 */
1627 default:
1628 ret = mbedtls_snprintf(p, n, "\n%s <unsupported>", prefix);
1629 MBEDTLS_X509_SAFE_SNPRINTF;
1630 break;
1631 }
1632
Andrzej Kurekd40c2b62023-02-13 07:01:59 -05001633 /* So far memory is freed only in the case of directoryName
Andrzej Kurek5f0c6e82023-02-27 16:03:41 -05001634 * parsing succeeding, as mbedtls_x509_get_name allocates memory. */
Andrzej Kurekd40c2b62023-02-13 07:01:59 -05001635 mbedtls_x509_free_subject_alt_name(&san);
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001636 cur = cur->next;
1637 }
1638
1639 *p = '\0';
1640
1641 *size = n;
1642 *buf = p;
1643
1644 return 0;
1645}
1646
1647#define PRINT_ITEM(i) \
1648 { \
1649 ret = mbedtls_snprintf(p, n, "%s" i, sep); \
1650 MBEDTLS_X509_SAFE_SNPRINTF; \
1651 sep = ", "; \
1652 }
1653
1654#define CERT_TYPE(type, name) \
1655 if (ns_cert_type & (type)) \
1656 PRINT_ITEM(name);
1657
1658int mbedtls_x509_info_cert_type(char **buf, size_t *size,
1659 unsigned char ns_cert_type)
1660{
1661 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1662 size_t n = *size;
1663 char *p = *buf;
1664 const char *sep = "";
1665
1666 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT, "SSL Client");
1667 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER, "SSL Server");
1668 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_EMAIL, "Email");
1669 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing");
1670 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_RESERVED, "Reserved");
1671 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_SSL_CA, "SSL CA");
1672 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA, "Email CA");
1673 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA");
1674
1675 *size = n;
1676 *buf = p;
1677
1678 return 0;
1679}
1680
1681#define KEY_USAGE(code, name) \
1682 if (key_usage & (code)) \
1683 PRINT_ITEM(name);
1684
1685int mbedtls_x509_info_key_usage(char **buf, size_t *size,
1686 unsigned int key_usage)
1687{
1688 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1689 size_t n = *size;
1690 char *p = *buf;
1691 const char *sep = "";
1692
1693 KEY_USAGE(MBEDTLS_X509_KU_DIGITAL_SIGNATURE, "Digital Signature");
1694 KEY_USAGE(MBEDTLS_X509_KU_NON_REPUDIATION, "Non Repudiation");
1695 KEY_USAGE(MBEDTLS_X509_KU_KEY_ENCIPHERMENT, "Key Encipherment");
1696 KEY_USAGE(MBEDTLS_X509_KU_DATA_ENCIPHERMENT, "Data Encipherment");
1697 KEY_USAGE(MBEDTLS_X509_KU_KEY_AGREEMENT, "Key Agreement");
1698 KEY_USAGE(MBEDTLS_X509_KU_KEY_CERT_SIGN, "Key Cert Sign");
1699 KEY_USAGE(MBEDTLS_X509_KU_CRL_SIGN, "CRL Sign");
1700 KEY_USAGE(MBEDTLS_X509_KU_ENCIPHER_ONLY, "Encipher Only");
1701 KEY_USAGE(MBEDTLS_X509_KU_DECIPHER_ONLY, "Decipher Only");
1702
1703 *size = n;
1704 *buf = p;
1705
1706 return 0;
1707}
1708#endif /* MBEDTLS_X509_REMOVE_INFO */
1709#endif /* MBEDTLS_X509_CRT_PARSE_C || MBEDTLS_X509_CSR_PARSE_C */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001710#endif /* MBEDTLS_X509_USE_C */