blob: f8695d4a983fda49b1582537d29c05c3c9bb8d49 [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
1204/*
1205 * SubjectAltName ::= GeneralNames
1206 *
1207 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
1208 *
1209 * GeneralName ::= CHOICE {
1210 * otherName [0] OtherName,
1211 * rfc822Name [1] IA5String,
1212 * dNSName [2] IA5String,
1213 * x400Address [3] ORAddress,
1214 * directoryName [4] Name,
1215 * ediPartyName [5] EDIPartyName,
1216 * uniformResourceIdentifier [6] IA5String,
1217 * iPAddress [7] OCTET STRING,
1218 * registeredID [8] OBJECT IDENTIFIER }
1219 *
1220 * OtherName ::= SEQUENCE {
1221 * type-id OBJECT IDENTIFIER,
1222 * value [0] EXPLICIT ANY DEFINED BY type-id }
1223 *
1224 * EDIPartyName ::= SEQUENCE {
1225 * nameAssigner [0] DirectoryString OPTIONAL,
1226 * partyName [1] DirectoryString }
1227 *
Andrzej Kurek81b0b892023-02-16 06:55:10 -05001228 * We list all types, but use the following GeneralName types from RFC 5280:
1229 * "dnsName", "uniformResourceIdentifier" and "hardware_module_name"
1230 * of type "otherName", as defined in RFC 4108.
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001231 */
1232int mbedtls_x509_get_subject_alt_name(unsigned char **p,
1233 const unsigned char *end,
1234 mbedtls_x509_sequence *subject_alt_name)
1235{
1236 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1237 size_t len, tag_len;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001238 mbedtls_asn1_sequence *cur = subject_alt_name;
1239
1240 /* Get main sequence tag */
1241 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
1242 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1243 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1244 }
1245
1246 if (*p + len != end) {
1247 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1248 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1249 }
1250
1251 while (*p < end) {
1252 mbedtls_x509_subject_alternative_name dummy_san_buf;
Hanno Beckerae8f8c42019-09-13 12:24:56 +01001253 mbedtls_x509_buf tmp_san_buf;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001254 memset(&dummy_san_buf, 0, sizeof(dummy_san_buf));
1255
Hanno Beckerae8f8c42019-09-13 12:24:56 +01001256 tmp_san_buf.tag = **p;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001257 (*p)++;
Hanno Beckerae8f8c42019-09-13 12:24:56 +01001258
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001259 if ((ret = mbedtls_asn1_get_len(p, end, &tag_len)) != 0) {
1260 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1261 }
1262
Hanno Beckerae8f8c42019-09-13 12:24:56 +01001263 tmp_san_buf.p = *p;
1264 tmp_san_buf.len = tag_len;
1265
1266 if ((tmp_san_buf.tag & MBEDTLS_ASN1_TAG_CLASS_MASK) !=
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001267 MBEDTLS_ASN1_CONTEXT_SPECIFIC) {
1268 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1269 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
1270 }
1271
1272 /*
1273 * Check that the SAN is structured correctly.
1274 */
Hanno Beckerae8f8c42019-09-13 12:24:56 +01001275 ret = mbedtls_x509_parse_subject_alt_name(&tmp_san_buf, &dummy_san_buf);
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001276 /*
1277 * In case the extension is malformed, return an error,
1278 * and clear the allocated sequences.
1279 */
1280 if (ret != 0 && ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE) {
1281 mbedtls_asn1_sequence_free(subject_alt_name->next);
1282 subject_alt_name->next = NULL;
1283 return ret;
1284 }
1285
Andrzej Kurekd40c2b62023-02-13 07:01:59 -05001286 mbedtls_x509_free_subject_alt_name(&dummy_san_buf);
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001287 /* 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));
Przemek Stekielecee12f2023-02-09 14:43:49 +01001423 }
1424 break;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001425
Przemek Stekielecee12f2023-02-09 14:43:49 +01001426 /*
1427 * RFC822 Name
1428 */
1429 case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_RFC822_NAME):
1430 {
1431 memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name));
1432 san->type = MBEDTLS_X509_SAN_RFC822_NAME;
1433 memcpy(&san->san.unstructured_name, san_buf, sizeof(*san_buf));
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001434 }
1435 break;
1436
1437 /*
Andrzej Kureke12b01d2023-01-10 06:47:38 -05001438 * directoryName
1439 */
1440 case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_DIRECTORY_NAME):
1441 {
1442 size_t name_len;
1443 unsigned char *p = san_buf->p;
1444 memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name));
1445 san->type = MBEDTLS_X509_SAN_DIRECTORY_NAME;
1446
1447 ret = mbedtls_asn1_get_tag(&p, p + san_buf->len, &name_len,
1448 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE);
1449
1450 if (ret != 0) {
1451 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1452 ret);
1453 }
1454
1455 if ((ret = mbedtls_x509_get_name(&p, p + name_len,
1456 &san->san.directory_name)) != 0) {
1457 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1458 ret);
1459 }
1460 }
1461 break;
1462 /*
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001463 * Type not supported
1464 */
1465 default:
1466 return MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
1467 }
1468 return 0;
1469}
1470
Andrzej Kurekd40c2b62023-02-13 07:01:59 -05001471void mbedtls_x509_free_subject_alt_name(mbedtls_x509_subject_alternative_name *san)
1472{
1473 if (san->type == MBEDTLS_X509_SAN_DIRECTORY_NAME) {
1474 mbedtls_asn1_free_named_data_list_shallow(san->san.directory_name.next);
1475 }
1476}
1477
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001478#if !defined(MBEDTLS_X509_REMOVE_INFO)
1479int mbedtls_x509_info_subject_alt_name(char **buf, size_t *size,
1480 const mbedtls_x509_sequence
1481 *subject_alt_name,
1482 const char *prefix)
1483{
1484 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1485 size_t i;
1486 size_t n = *size;
1487 char *p = *buf;
1488 const mbedtls_x509_sequence *cur = subject_alt_name;
1489 mbedtls_x509_subject_alternative_name san;
1490 int parse_ret;
1491
1492 while (cur != NULL) {
1493 memset(&san, 0, sizeof(san));
1494 parse_ret = mbedtls_x509_parse_subject_alt_name(&cur->buf, &san);
1495 if (parse_ret != 0) {
1496 if (parse_ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE) {
1497 ret = mbedtls_snprintf(p, n, "\n%s <unsupported>", prefix);
1498 MBEDTLS_X509_SAFE_SNPRINTF;
1499 } else {
1500 ret = mbedtls_snprintf(p, n, "\n%s <malformed>", prefix);
1501 MBEDTLS_X509_SAFE_SNPRINTF;
1502 }
1503 cur = cur->next;
1504 continue;
1505 }
1506
1507 switch (san.type) {
1508 /*
1509 * otherName
1510 */
1511 case MBEDTLS_X509_SAN_OTHER_NAME:
1512 {
1513 mbedtls_x509_san_other_name *other_name = &san.san.other_name;
1514
1515 ret = mbedtls_snprintf(p, n, "\n%s otherName :", prefix);
1516 MBEDTLS_X509_SAFE_SNPRINTF;
1517
1518 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ON_HW_MODULE_NAME,
1519 &other_name->value.hardware_module_name.oid) != 0) {
1520 ret = mbedtls_snprintf(p, n, "\n%s hardware module name :", prefix);
1521 MBEDTLS_X509_SAFE_SNPRINTF;
1522 ret =
1523 mbedtls_snprintf(p, n, "\n%s hardware type : ", prefix);
1524 MBEDTLS_X509_SAFE_SNPRINTF;
1525
1526 ret = mbedtls_oid_get_numeric_string(p,
1527 n,
1528 &other_name->value.hardware_module_name.oid);
1529 MBEDTLS_X509_SAFE_SNPRINTF;
1530
1531 ret =
1532 mbedtls_snprintf(p, n, "\n%s hardware serial number : ", prefix);
1533 MBEDTLS_X509_SAFE_SNPRINTF;
1534
1535 for (i = 0; i < other_name->value.hardware_module_name.val.len; i++) {
1536 ret = mbedtls_snprintf(p,
1537 n,
1538 "%02X",
1539 other_name->value.hardware_module_name.val.p[i]);
1540 MBEDTLS_X509_SAFE_SNPRINTF;
1541 }
1542 }/* MBEDTLS_OID_ON_HW_MODULE_NAME */
1543 }
1544 break;
Andrzej Kurek7a05fab2023-02-13 10:03:07 -05001545 /*
1546 * uniformResourceIdentifier
1547 */
1548 case MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER:
1549 {
1550 ret = mbedtls_snprintf(p, n, "\n%s uniformResourceIdentifier : ", prefix);
1551 MBEDTLS_X509_SAFE_SNPRINTF;
1552 if (san.san.unstructured_name.len >= n) {
1553 *p = '\0';
1554 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1555 }
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001556
Andrzej Kurek7a05fab2023-02-13 10:03:07 -05001557 memcpy(p, san.san.unstructured_name.p, san.san.unstructured_name.len);
1558 p += san.san.unstructured_name.len;
1559 n -= san.san.unstructured_name.len;
1560 }
1561 break;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001562 /*
1563 * dNSName
Przemek Stekiel5b9e4162023-02-15 12:56:37 +01001564 * RFC822 Name
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001565 */
1566 case MBEDTLS_X509_SAN_DNS_NAME:
Przemek Stekiel5b9e4162023-02-15 12:56:37 +01001567 case MBEDTLS_X509_SAN_RFC822_NAME:
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001568 {
Przemek Stekiel82d250d2023-02-15 15:00:50 +01001569 const char *dns_name = "dNSName";
1570 const char *rfc822_name = "rfc822Name";
Przemek Stekiel5b9e4162023-02-15 12:56:37 +01001571
Przemek Stekiel82d250d2023-02-15 15:00:50 +01001572 ret = mbedtls_snprintf(p, n,
1573 "\n%s %s : ",
1574 prefix,
1575 san.type ==
1576 MBEDTLS_X509_SAN_DNS_NAME ? dns_name : rfc822_name);
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001577 MBEDTLS_X509_SAFE_SNPRINTF;
1578 if (san.san.unstructured_name.len >= n) {
1579 *p = '\0';
1580 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1581 }
1582
1583 memcpy(p, san.san.unstructured_name.p, san.san.unstructured_name.len);
1584 p += san.san.unstructured_name.len;
1585 n -= san.san.unstructured_name.len;
1586 }
1587 break;
1588
1589 /*
Andrzej Kureke12b01d2023-01-10 06:47:38 -05001590 * directoryName
1591 */
1592 case MBEDTLS_X509_SAN_DIRECTORY_NAME:
1593 {
1594 ret = mbedtls_snprintf(p, n, "\n%s directoryName : ", prefix);
1595 MBEDTLS_X509_SAFE_SNPRINTF;
1596 ret = mbedtls_x509_dn_gets(p, n, &san.san.directory_name);
Andrzej Kurekd40c2b62023-02-13 07:01:59 -05001597
Andrzej Kureke12b01d2023-01-10 06:47:38 -05001598 if (ret < 0) {
1599 return ret;
1600 }
1601
1602 p += ret;
1603 n -= ret;
1604 }
1605 break;
1606 /*
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001607 * Type not supported, skip item.
1608 */
1609 default:
1610 ret = mbedtls_snprintf(p, n, "\n%s <unsupported>", prefix);
1611 MBEDTLS_X509_SAFE_SNPRINTF;
1612 break;
1613 }
1614
Andrzej Kurekd40c2b62023-02-13 07:01:59 -05001615 /* So far memory is freed only in the case of directoryName
1616 * parsing succeeding, as mbedtls_x509_dn_gets allocates memory. */
1617 mbedtls_x509_free_subject_alt_name(&san);
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001618 cur = cur->next;
1619 }
1620
1621 *p = '\0';
1622
1623 *size = n;
1624 *buf = p;
1625
1626 return 0;
1627}
1628
1629#define PRINT_ITEM(i) \
1630 { \
1631 ret = mbedtls_snprintf(p, n, "%s" i, sep); \
1632 MBEDTLS_X509_SAFE_SNPRINTF; \
1633 sep = ", "; \
1634 }
1635
1636#define CERT_TYPE(type, name) \
1637 if (ns_cert_type & (type)) \
1638 PRINT_ITEM(name);
1639
1640int mbedtls_x509_info_cert_type(char **buf, size_t *size,
1641 unsigned char ns_cert_type)
1642{
1643 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1644 size_t n = *size;
1645 char *p = *buf;
1646 const char *sep = "";
1647
1648 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT, "SSL Client");
1649 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER, "SSL Server");
1650 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_EMAIL, "Email");
1651 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing");
1652 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_RESERVED, "Reserved");
1653 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_SSL_CA, "SSL CA");
1654 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA, "Email CA");
1655 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA");
1656
1657 *size = n;
1658 *buf = p;
1659
1660 return 0;
1661}
1662
1663#define KEY_USAGE(code, name) \
1664 if (key_usage & (code)) \
1665 PRINT_ITEM(name);
1666
1667int mbedtls_x509_info_key_usage(char **buf, size_t *size,
1668 unsigned int key_usage)
1669{
1670 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1671 size_t n = *size;
1672 char *p = *buf;
1673 const char *sep = "";
1674
1675 KEY_USAGE(MBEDTLS_X509_KU_DIGITAL_SIGNATURE, "Digital Signature");
1676 KEY_USAGE(MBEDTLS_X509_KU_NON_REPUDIATION, "Non Repudiation");
1677 KEY_USAGE(MBEDTLS_X509_KU_KEY_ENCIPHERMENT, "Key Encipherment");
1678 KEY_USAGE(MBEDTLS_X509_KU_DATA_ENCIPHERMENT, "Data Encipherment");
1679 KEY_USAGE(MBEDTLS_X509_KU_KEY_AGREEMENT, "Key Agreement");
1680 KEY_USAGE(MBEDTLS_X509_KU_KEY_CERT_SIGN, "Key Cert Sign");
1681 KEY_USAGE(MBEDTLS_X509_KU_CRL_SIGN, "CRL Sign");
1682 KEY_USAGE(MBEDTLS_X509_KU_ENCIPHER_ONLY, "Encipher Only");
1683 KEY_USAGE(MBEDTLS_X509_KU_DECIPHER_ONLY, "Decipher Only");
1684
1685 *size = n;
1686 *buf = p;
1687
1688 return 0;
1689}
1690#endif /* MBEDTLS_X509_REMOVE_INFO */
1691#endif /* MBEDTLS_X509_CRT_PARSE_C || MBEDTLS_X509_CSR_PARSE_C */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001692#endif /* MBEDTLS_X509_USE_C */