blob: 63dbd3a016f5ef8064353b39528f5693703f553f [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
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400568static int x509_date_is_valid(const mbedtls_x509_time *t)
Janos Follath87c98072017-02-03 12:36:59 +0000569{
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400570 unsigned int d;
571 switch (t->mon) {
572 case 1: case 3: case 5: case 7: case 8: case 10: case 12:
573 d = 31;
574 break;
575 case 4: case 6: case 9: case 11:
576 d = 30;
577 break;
578 case 2:
579 d = (unsigned int) t->year;
580 d = ((d & 3) || (!(d % 100) && (d % 400))) ? 28 : 29;
581 break;
582 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100583 return MBEDTLS_ERR_X509_INVALID_DATE;
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400584 }
Janos Follath87c98072017-02-03 12:36:59 +0000585
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400586 if ((unsigned int) (t->day - 1) >= d || /*(1 - days in month)*/
587 /*(unsigned int)( t->mon - 1 ) >= 12 ||*//*(1 - 12) checked above*/
588 (unsigned int) t->year > 9999 || /*(0 - 9999)*/
589 (unsigned int) t->hour > 23 || /*(0 - 23)*/
590 (unsigned int) t->min > 59 || /*(0 - 59)*/
591 (unsigned int) t->sec > 59) { /*(0 - 59)*/
592 return MBEDTLS_ERR_X509_INVALID_DATE;
Rich Evans7d5a55a2015-02-13 11:48:02 +0000593 }
Janos Follath87c98072017-02-03 12:36:59 +0000594
Gilles Peskine449bd832023-01-11 14:50:10 +0100595 return 0;
Rich Evans7d5a55a2015-02-13 11:48:02 +0000596}
597
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400598static int x509_parse2_int(const unsigned char *p)
Andres AG4b76aec2016-09-23 13:16:02 +0100599{
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400600 uint32_t d1 = p[0] - '0';
601 uint32_t d2 = p[1] - '0';
602 return (d1 < 10 && d2 < 10) ? (int) (d1 * 10 + d2) : -1;
Andres AG4b76aec2016-09-23 13:16:02 +0100603}
604
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200605/*
Janos Follath87c98072017-02-03 12:36:59 +0000606 * Parse an ASN1_UTC_TIME (yearlen=2) or ASN1_GENERALIZED_TIME (yearlen=4)
607 * field.
608 */
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400609static int x509_parse_time(const unsigned char *p, mbedtls_x509_time *tm,
610 size_t yearlen)
Janos Follath87c98072017-02-03 12:36:59 +0000611{
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400612 int x;
Janos Follath87c98072017-02-03 12:36:59 +0000613
614 /*
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400615 * Parse year, month, day, hour, minute, second
Janos Follath87c98072017-02-03 12:36:59 +0000616 */
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400617 tm->year = x509_parse2_int(p);
618 if (tm->year < 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100619 return MBEDTLS_ERR_X509_INVALID_DATE;
620 }
Janos Follath87c98072017-02-03 12:36:59 +0000621
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400622 if (4 == yearlen) {
623 x = tm->year * 100;
624 p += 2;
625 tm->year = x509_parse2_int(p);
626 if (tm->year < 0) {
627 return MBEDTLS_ERR_X509_INVALID_DATE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100628 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100629 } else {
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400630 x = (tm->year < 50) ? 2000 : 1900;
Janos Follath87c98072017-02-03 12:36:59 +0000631 }
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400632 tm->year += x;
Janos Follath87c98072017-02-03 12:36:59 +0000633
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400634 tm->mon = x509_parse2_int(p + 2);
635 tm->day = x509_parse2_int(p + 4);
636 tm->hour = x509_parse2_int(p + 6);
637 tm->min = x509_parse2_int(p + 8);
638 tm->sec = x509_parse2_int(p + 10);
Janos Follath87c98072017-02-03 12:36:59 +0000639
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400640 return x509_date_is_valid(tm);
Janos Follath87c98072017-02-03 12:36:59 +0000641}
642
643/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200644 * Time ::= CHOICE {
645 * utcTime UTCTime,
646 * generalTime GeneralizedTime }
647 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100648int mbedtls_x509_get_time(unsigned char **p, const unsigned char *end,
649 mbedtls_x509_time *tm)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200650{
Janos Follath865b3eb2019-12-16 11:46:15 +0000651 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Janos Follath87c98072017-02-03 12:36:59 +0000652 size_t len, year_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200653 unsigned char tag;
654
Gilles Peskine449bd832023-01-11 14:50:10 +0100655 if ((end - *p) < 1) {
656 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE,
657 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
658 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200659
660 tag = **p;
661
Gilles Peskine449bd832023-01-11 14:50:10 +0100662 if (tag == MBEDTLS_ASN1_UTC_TIME) {
Janos Follath87c98072017-02-03 12:36:59 +0000663 year_len = 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100664 } else if (tag == MBEDTLS_ASN1_GENERALIZED_TIME) {
Janos Follath87c98072017-02-03 12:36:59 +0000665 year_len = 4;
Gilles Peskine449bd832023-01-11 14:50:10 +0100666 } else {
667 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE,
668 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
669 }
Janos Follath87c98072017-02-03 12:36:59 +0000670
671 (*p)++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100672 ret = mbedtls_asn1_get_len(p, end, &len);
Janos Follath87c98072017-02-03 12:36:59 +0000673
Gilles Peskine449bd832023-01-11 14:50:10 +0100674 if (ret != 0) {
675 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE, ret);
676 }
Janos Follath87c98072017-02-03 12:36:59 +0000677
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400678 /* len is 12 or 14 depending on year_len, plus optional trailing 'Z' */
679 if (len != year_len + 10 &&
680 !(len == year_len + 11 && (*p)[(len - 1)] == 'Z')) {
681 return MBEDTLS_ERR_X509_INVALID_DATE;
682 }
683
684 (*p) += len;
685 return x509_parse_time(*p - len, tm, year_len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200686}
687
Gilles Peskine449bd832023-01-11 14:50:10 +0100688int mbedtls_x509_get_sig(unsigned char **p, const unsigned char *end, mbedtls_x509_buf *sig)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200689{
Janos Follath865b3eb2019-12-16 11:46:15 +0000690 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200691 size_t len;
Andres AG4bdbe092016-09-19 16:58:45 +0100692 int tag_type;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200693
Gilles Peskine449bd832023-01-11 14:50:10 +0100694 if ((end - *p) < 1) {
695 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_SIGNATURE,
696 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
697 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200698
Andres AG4bdbe092016-09-19 16:58:45 +0100699 tag_type = **p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200700
Gilles Peskine449bd832023-01-11 14:50:10 +0100701 if ((ret = mbedtls_asn1_get_bitstring_null(p, end, &len)) != 0) {
702 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_SIGNATURE, ret);
703 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200704
Andres AG4bdbe092016-09-19 16:58:45 +0100705 sig->tag = tag_type;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200706 sig->len = len;
707 sig->p = *p;
708
709 *p += len;
710
Gilles Peskine449bd832023-01-11 14:50:10 +0100711 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200712}
713
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100714/*
715 * Get signature algorithm from alg OID and optional parameters
716 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100717int mbedtls_x509_get_sig_alg(const mbedtls_x509_buf *sig_oid, const mbedtls_x509_buf *sig_params,
718 mbedtls_md_type_t *md_alg, mbedtls_pk_type_t *pk_alg,
719 void **sig_opts)
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
Gilles Peskine449bd832023-01-11 14:50:10 +0100723 if (*sig_opts != NULL) {
724 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
725 }
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200726
Gilles Peskine449bd832023-01-11 14:50:10 +0100727 if ((ret = mbedtls_oid_get_sig_alg(sig_oid, md_alg, pk_alg)) != 0) {
728 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG, ret);
729 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200731#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100732 if (*pk_alg == MBEDTLS_PK_RSASSA_PSS) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200733 mbedtls_pk_rsassa_pss_options *pss_opts;
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100734
Gilles Peskine449bd832023-01-11 14:50:10 +0100735 pss_opts = mbedtls_calloc(1, sizeof(mbedtls_pk_rsassa_pss_options));
736 if (pss_opts == NULL) {
737 return MBEDTLS_ERR_X509_ALLOC_FAILED;
738 }
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200739
Gilles Peskine449bd832023-01-11 14:50:10 +0100740 ret = mbedtls_x509_get_rsassa_pss_params(sig_params,
741 md_alg,
742 &pss_opts->mgf1_hash_id,
743 &pss_opts->expected_salt_len);
744 if (ret != 0) {
745 mbedtls_free(pss_opts);
746 return ret;
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200747 }
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100748
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200749 *sig_opts = (void *) pss_opts;
Gilles Peskine449bd832023-01-11 14:50:10 +0100750 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200751#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100752 {
753 /* Make sure parameters are absent or NULL */
Gilles Peskine449bd832023-01-11 14:50:10 +0100754 if ((sig_params->tag != MBEDTLS_ASN1_NULL && sig_params->tag != 0) ||
755 sig_params->len != 0) {
756 return MBEDTLS_ERR_X509_INVALID_ALG;
757 }
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100758 }
759
Gilles Peskine449bd832023-01-11 14:50:10 +0100760 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200761}
762
763/*
764 * X.509 Extensions (No parsing of extensions, pointer should
Brian J Murray1903fb32016-11-06 04:45:15 -0800765 * be either manually updated or extensions should be parsed!)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200766 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100767int mbedtls_x509_get_ext(unsigned char **p, const unsigned char *end,
768 mbedtls_x509_buf *ext, int tag)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200769{
Janos Follath865b3eb2019-12-16 11:46:15 +0000770 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200771 size_t len;
772
Hanno Becker3cddba82019-02-11 14:33:36 +0000773 /* Extension structure use EXPLICIT tagging. That is, the actual
774 * `Extensions` structure is wrapped by a tag-length pair using
775 * the respective context-specific tag. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100776 ret = mbedtls_asn1_get_tag(p, end, &ext->len,
777 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | tag);
778 if (ret != 0) {
779 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
780 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200781
Hanno Becker6ccfb182019-02-12 11:52:10 +0000782 ext->tag = MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | tag;
783 ext->p = *p;
784 end = *p + ext->len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200785
786 /*
787 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200788 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100789 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
790 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
791 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
792 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200793
Gilles Peskine449bd832023-01-11 14:50:10 +0100794 if (end != *p + len) {
795 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
796 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
797 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200798
Gilles Peskine449bd832023-01-11 14:50:10 +0100799 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200800}
801
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200802/*
803 * Store the name in printable form into buf; no more
804 * than size characters will be written
805 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100806int mbedtls_x509_dn_gets(char *buf, size_t size, const mbedtls_x509_name *dn)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200807{
Janos Follath865b3eb2019-12-16 11:46:15 +0000808 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Werner Lewisb33dacd2022-05-20 12:48:46 +0100809 size_t i, j, n;
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000810 unsigned char c, merge = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200811 const mbedtls_x509_name *name;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200812 const char *short_name = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200813 char s[MBEDTLS_X509_MAX_DN_NAME_SIZE], *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200814
Gilles Peskine449bd832023-01-11 14:50:10 +0100815 memset(s, 0, sizeof(s));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200816
817 name = dn;
818 p = buf;
819 n = size;
820
Gilles Peskine449bd832023-01-11 14:50:10 +0100821 while (name != NULL) {
822 if (!name->oid.p) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200823 name = name->next;
824 continue;
825 }
826
Gilles Peskine449bd832023-01-11 14:50:10 +0100827 if (name != dn) {
828 ret = mbedtls_snprintf(p, n, merge ? " + " : ", ");
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200829 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200830 }
831
Gilles Peskine449bd832023-01-11 14:50:10 +0100832 ret = mbedtls_oid_get_attr_short_name(&name->oid, &short_name);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200833
Gilles Peskine449bd832023-01-11 14:50:10 +0100834 if (ret == 0) {
835 ret = mbedtls_snprintf(p, n, "%s=", short_name);
836 } else {
837 ret = mbedtls_snprintf(p, n, "\?\?=");
838 }
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200839 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200840
Gilles Peskine449bd832023-01-11 14:50:10 +0100841 for (i = 0, j = 0; i < name->val.len; i++, j++) {
842 if (j >= sizeof(s) - 1) {
843 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
844 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200845
846 c = name->val.p[i];
Werner Lewisb33dacd2022-05-20 12:48:46 +0100847 // Special characters requiring escaping, RFC 1779
Gilles Peskine449bd832023-01-11 14:50:10 +0100848 if (c && strchr(",=+<>#;\"\\", c)) {
849 if (j + 1 >= sizeof(s) - 1) {
850 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
851 }
Werner Lewisb33dacd2022-05-20 12:48:46 +0100852 s[j++] = '\\';
853 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100854 if (c < 32 || c >= 127) {
855 s[j] = '?';
856 } else {
857 s[j] = c;
858 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200859 }
Werner Lewisb33dacd2022-05-20 12:48:46 +0100860 s[j] = '\0';
Gilles Peskine449bd832023-01-11 14:50:10 +0100861 ret = mbedtls_snprintf(p, n, "%s", s);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200862 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000863
864 merge = name->next_merged;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200865 name = name->next;
866 }
867
Gilles Peskine449bd832023-01-11 14:50:10 +0100868 return (int) (size - n);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200869}
870
871/*
872 * Store the serial in printable form into buf; no more
873 * than size characters will be written
874 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100875int mbedtls_x509_serial_gets(char *buf, size_t size, const mbedtls_x509_buf *serial)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200876{
Janos Follath865b3eb2019-12-16 11:46:15 +0000877 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200878 size_t i, n, nr;
879 char *p;
880
881 p = buf;
882 n = size;
883
Gilles Peskine449bd832023-01-11 14:50:10 +0100884 nr = (serial->len <= 32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200885 ? serial->len : 28;
886
Gilles Peskine449bd832023-01-11 14:50:10 +0100887 for (i = 0; i < nr; i++) {
888 if (i == 0 && nr > 1 && serial->p[i] == 0x0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200889 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100890 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200891
Gilles Peskine449bd832023-01-11 14:50:10 +0100892 ret = mbedtls_snprintf(p, n, "%02X%s",
893 serial->p[i], (i < nr - 1) ? ":" : "");
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200894 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200895 }
896
Gilles Peskine449bd832023-01-11 14:50:10 +0100897 if (nr != serial->len) {
898 ret = mbedtls_snprintf(p, n, "....");
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200899 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200900 }
901
Gilles Peskine449bd832023-01-11 14:50:10 +0100902 return (int) (size - n);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200903}
904
Hanno Becker612a2f12020-10-09 09:19:39 +0100905#if !defined(MBEDTLS_X509_REMOVE_INFO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200906/*
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200907 * Helper for writing signature algorithms
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100908 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100909int mbedtls_x509_sig_alg_gets(char *buf, size_t size, const mbedtls_x509_buf *sig_oid,
910 mbedtls_pk_type_t pk_alg, mbedtls_md_type_t md_alg,
911 const void *sig_opts)
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100912{
Janos Follath865b3eb2019-12-16 11:46:15 +0000913 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100914 char *p = buf;
915 size_t n = size;
916 const char *desc = NULL;
917
Gilles Peskine449bd832023-01-11 14:50:10 +0100918 ret = mbedtls_oid_get_sig_alg_desc(sig_oid, &desc);
919 if (ret != 0) {
920 ret = mbedtls_snprintf(p, n, "???");
921 } else {
922 ret = mbedtls_snprintf(p, n, "%s", desc);
923 }
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200924 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100925
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200926#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100927 if (pk_alg == MBEDTLS_PK_RSASSA_PSS) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200928 const mbedtls_pk_rsassa_pss_options *pss_opts;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100929
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200930 pss_opts = (const mbedtls_pk_rsassa_pss_options *) sig_opts;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100931
Gilles Peskine449bd832023-01-11 14:50:10 +0100932 const char *name = md_type_to_string(md_alg);
933 const char *mgf_name = md_type_to_string(pss_opts->mgf1_hash_id);
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100934
Gilles Peskine449bd832023-01-11 14:50:10 +0100935 ret = mbedtls_snprintf(p, n, " (%s, MGF1-%s, 0x%02X)",
936 name ? name : "???",
937 mgf_name ? mgf_name : "???",
938 (unsigned int) pss_opts->expected_salt_len);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200939 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100940 }
941#else
942 ((void) pk_alg);
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200943 ((void) md_alg);
944 ((void) sig_opts);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200945#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100946
Gilles Peskine449bd832023-01-11 14:50:10 +0100947 return (int) (size - n);
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100948}
Hanno Becker612a2f12020-10-09 09:19:39 +0100949#endif /* MBEDTLS_X509_REMOVE_INFO */
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100950
951/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200952 * Helper for writing "RSA key size", "EC key size", etc
953 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100954int mbedtls_x509_key_size_helper(char *buf, size_t buf_size, const char *name)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200955{
956 char *p = buf;
Manuel Pégourié-Gonnardfb317c52015-06-18 16:25:56 +0200957 size_t n = buf_size;
Janos Follath865b3eb2019-12-16 11:46:15 +0000958 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200959
Gilles Peskine449bd832023-01-11 14:50:10 +0100960 ret = mbedtls_snprintf(p, n, "%s key size", name);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200961 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200962
Gilles Peskine449bd832023-01-11 14:50:10 +0100963 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200964}
965
Glenn Strauss416dc032022-06-30 00:38:53 -0400966int mbedtls_x509_time_cmp(const mbedtls_x509_time *t1,
967 const mbedtls_x509_time *t2)
968{
Glenn Strauss5aef2972022-06-30 04:38:02 -0400969 int x;
970
971 x = (((t1->year << 9) | (t1->mon << 5) | (t1->day)) -
972 ((t2->year << 9) | (t2->mon << 5) | (t2->day)));
973 if (x != 0) {
974 return x;
Glenn Strauss416dc032022-06-30 00:38:53 -0400975 }
976
Glenn Strauss5aef2972022-06-30 04:38:02 -0400977 x = (((t1->hour << 12) | (t1->min << 6) | (t1->sec)) -
978 ((t2->hour << 12) | (t2->min << 6) | (t2->sec)));
979 return x;
Glenn Strauss416dc032022-06-30 00:38:53 -0400980}
981
Manuel Pégourié-Gonnard60c793b2015-06-18 20:52:58 +0200982#if defined(MBEDTLS_HAVE_TIME_DATE)
Glenn Strauss61d99302022-06-30 05:25:56 -0400983int mbedtls_x509_time_gmtime(mbedtls_time_t tt, mbedtls_x509_time *now)
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100984{
Glenn Strauss811eeb22022-06-30 05:28:50 -0400985 struct tm tm;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200986
Glenn Strauss811eeb22022-06-30 05:28:50 -0400987 if (mbedtls_platform_gmtime_r(&tt, &tm) == NULL) {
988 return -1;
Manuel Pégourié-Gonnard57e10d72015-06-22 18:59:21 +0200989 }
Manuel Pégourié-Gonnard864108d2015-05-29 10:11:03 +0200990
Glenn Strauss811eeb22022-06-30 05:28:50 -0400991 now->year = tm.tm_year + 1900;
992 now->mon = tm.tm_mon + 1;
993 now->day = tm.tm_mday;
994 now->hour = tm.tm_hour;
995 now->min = tm.tm_min;
996 now->sec = tm.tm_sec;
997 return 0;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100998}
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200999
Glenn Strauss61d99302022-06-30 05:25:56 -04001000static int x509_get_current_time(mbedtls_x509_time *now)
1001{
1002 return mbedtls_x509_time_gmtime(mbedtls_time(NULL), now);
1003}
1004
Gilles Peskine449bd832023-01-11 14:50:10 +01001005int mbedtls_x509_time_is_past(const mbedtls_x509_time *to)
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001006{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001007 mbedtls_x509_time now;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001008
Gilles Peskine449bd832023-01-11 14:50:10 +01001009 if (x509_get_current_time(&now) != 0) {
1010 return 1;
1011 }
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001012
Glenn Strauss416dc032022-06-30 00:38:53 -04001013 return mbedtls_x509_time_cmp(to, &now) < 0;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001014}
1015
Gilles Peskine449bd832023-01-11 14:50:10 +01001016int mbedtls_x509_time_is_future(const mbedtls_x509_time *from)
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001017{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001018 mbedtls_x509_time now;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001019
Gilles Peskine449bd832023-01-11 14:50:10 +01001020 if (x509_get_current_time(&now) != 0) {
1021 return 1;
1022 }
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001023
Glenn Strauss416dc032022-06-30 00:38:53 -04001024 return mbedtls_x509_time_cmp(from, &now) > 0;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001025}
1026
Manuel Pégourié-Gonnard60c793b2015-06-18 20:52:58 +02001027#else /* MBEDTLS_HAVE_TIME_DATE */
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001028
Gilles Peskine449bd832023-01-11 14:50:10 +01001029int mbedtls_x509_time_is_past(const mbedtls_x509_time *to)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001030{
1031 ((void) to);
Gilles Peskine449bd832023-01-11 14:50:10 +01001032 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001033}
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001034
Gilles Peskine449bd832023-01-11 14:50:10 +01001035int mbedtls_x509_time_is_future(const mbedtls_x509_time *from)
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001036{
1037 ((void) from);
Gilles Peskine449bd832023-01-11 14:50:10 +01001038 return 0;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001039}
Manuel Pégourié-Gonnard60c793b2015-06-18 20:52:58 +02001040#endif /* MBEDTLS_HAVE_TIME_DATE */
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001041
1042/* Common functions for parsing CRT and CSR. */
1043#if defined(MBEDTLS_X509_CRT_PARSE_C) || defined(MBEDTLS_X509_CSR_PARSE_C)
1044/*
1045 * OtherName ::= SEQUENCE {
1046 * type-id OBJECT IDENTIFIER,
1047 * value [0] EXPLICIT ANY DEFINED BY type-id }
1048 *
1049 * HardwareModuleName ::= SEQUENCE {
1050 * hwType OBJECT IDENTIFIER,
1051 * hwSerialNum OCTET STRING }
1052 *
1053 * NOTE: we currently only parse and use otherName of type HwModuleName,
1054 * as defined in RFC 4108.
1055 */
1056static int x509_get_other_name(const mbedtls_x509_buf *subject_alt_name,
1057 mbedtls_x509_san_other_name *other_name)
1058{
1059 int ret = 0;
1060 size_t len;
1061 unsigned char *p = subject_alt_name->p;
1062 const unsigned char *end = p + subject_alt_name->len;
1063 mbedtls_x509_buf cur_oid;
1064
1065 if ((subject_alt_name->tag &
1066 (MBEDTLS_ASN1_TAG_CLASS_MASK | MBEDTLS_ASN1_TAG_VALUE_MASK)) !=
1067 (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME)) {
1068 /*
1069 * The given subject alternative name is not of type "othername".
1070 */
1071 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1072 }
1073
1074 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1075 MBEDTLS_ASN1_OID)) != 0) {
1076 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1077 }
1078
1079 cur_oid.tag = MBEDTLS_ASN1_OID;
1080 cur_oid.p = p;
1081 cur_oid.len = len;
1082
1083 /*
1084 * Only HwModuleName is currently supported.
1085 */
1086 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ON_HW_MODULE_NAME, &cur_oid) != 0) {
1087 return MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
1088 }
1089
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001090 p += len;
1091 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1092 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC)) !=
1093 0) {
1094 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1095 }
1096
Hanno Beckerdae916b2019-09-13 14:21:13 +01001097 if (end != p + len) {
1098 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1099 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1100 }
1101
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001102 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1103 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1104 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1105 }
1106
Hanno Beckerdae916b2019-09-13 14:21:13 +01001107 if (end != p + len) {
1108 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1109 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1110 }
1111
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001112 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OID)) != 0) {
1113 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1114 }
1115
1116 other_name->value.hardware_module_name.oid.tag = MBEDTLS_ASN1_OID;
1117 other_name->value.hardware_module_name.oid.p = p;
1118 other_name->value.hardware_module_name.oid.len = len;
1119
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001120 p += len;
1121 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1122 MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1123 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1124 }
1125
1126 other_name->value.hardware_module_name.val.tag = MBEDTLS_ASN1_OCTET_STRING;
1127 other_name->value.hardware_module_name.val.p = p;
1128 other_name->value.hardware_module_name.val.len = len;
1129 p += len;
1130 if (p != end) {
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001131 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1132 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1133 }
1134 return 0;
1135}
1136
Przemek Stekiel21903ec2023-02-21 08:32:37 +01001137/* Check mbedtls_x509_get_subject_alt_name for detailed description.
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001138 *
Przemek Stekiel21903ec2023-02-21 08:32:37 +01001139 * In some cases while parsing subject alternative names the sequence tag is optional
1140 * (e.g. CertSerialNumber). This function is designed to handle such case.
Przemek Stekiel725688b2023-04-04 22:49:44 +02001141 */
Przemek Stekiel21903ec2023-02-21 08:32:37 +01001142int mbedtls_x509_get_subject_alt_name_ext(unsigned char **p,
1143 const unsigned char *end,
1144 mbedtls_x509_sequence *subject_alt_name)
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001145{
1146 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel21903ec2023-02-21 08:32:37 +01001147 size_t tag_len;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001148 mbedtls_asn1_sequence *cur = subject_alt_name;
1149
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001150 while (*p < end) {
Andrzej Kurek154a6052023-04-30 14:11:49 -04001151 mbedtls_x509_subject_alternative_name tmp_san_name;
Hanno Beckerae8f8c42019-09-13 12:24:56 +01001152 mbedtls_x509_buf tmp_san_buf;
Andrzej Kurek154a6052023-04-30 14:11:49 -04001153 memset(&tmp_san_name, 0, sizeof(tmp_san_name));
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001154
Hanno Beckerae8f8c42019-09-13 12:24:56 +01001155 tmp_san_buf.tag = **p;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001156 (*p)++;
Hanno Beckerae8f8c42019-09-13 12:24:56 +01001157
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001158 if ((ret = mbedtls_asn1_get_len(p, end, &tag_len)) != 0) {
1159 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1160 }
1161
Hanno Beckerae8f8c42019-09-13 12:24:56 +01001162 tmp_san_buf.p = *p;
1163 tmp_san_buf.len = tag_len;
1164
1165 if ((tmp_san_buf.tag & MBEDTLS_ASN1_TAG_CLASS_MASK) !=
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001166 MBEDTLS_ASN1_CONTEXT_SPECIFIC) {
1167 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1168 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
1169 }
1170
1171 /*
Andrzej Kurek154a6052023-04-30 14:11:49 -04001172 * Check that the SAN is structured correctly by parsing it.
1173 * The SAN structure is discarded afterwards.
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001174 */
Andrzej Kurek154a6052023-04-30 14:11:49 -04001175 ret = mbedtls_x509_parse_subject_alt_name(&tmp_san_buf, &tmp_san_name);
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001176 /*
1177 * In case the extension is malformed, return an error,
1178 * and clear the allocated sequences.
1179 */
1180 if (ret != 0 && ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE) {
1181 mbedtls_asn1_sequence_free(subject_alt_name->next);
1182 subject_alt_name->next = NULL;
1183 return ret;
1184 }
1185
Andrzej Kurek154a6052023-04-30 14:11:49 -04001186 mbedtls_x509_free_subject_alt_name(&tmp_san_name);
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001187 /* Allocate and assign next pointer */
1188 if (cur->buf.p != NULL) {
1189 if (cur->next != NULL) {
1190 return MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
1191 }
1192
1193 cur->next = mbedtls_calloc(1, sizeof(mbedtls_asn1_sequence));
1194
1195 if (cur->next == NULL) {
1196 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1197 MBEDTLS_ERR_ASN1_ALLOC_FAILED);
1198 }
1199
1200 cur = cur->next;
1201 }
1202
Hanno Beckerae8f8c42019-09-13 12:24:56 +01001203 cur->buf = tmp_san_buf;
1204 *p += tmp_san_buf.len;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001205 }
1206
1207 /* Set final sequence entry's next pointer to NULL */
1208 cur->next = NULL;
1209
1210 if (*p != end) {
1211 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1212 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1213 }
1214
1215 return 0;
1216}
1217
Przemek Stekiel21903ec2023-02-21 08:32:37 +01001218/*
1219 * SubjectAltName ::= GeneralNames
1220 *
1221 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
1222 *
1223 * GeneralName ::= CHOICE {
1224 * otherName [0] OtherName,
1225 * rfc822Name [1] IA5String,
1226 * dNSName [2] IA5String,
1227 * x400Address [3] ORAddress,
1228 * directoryName [4] Name,
1229 * ediPartyName [5] EDIPartyName,
1230 * uniformResourceIdentifier [6] IA5String,
1231 * iPAddress [7] OCTET STRING,
1232 * registeredID [8] OBJECT IDENTIFIER }
1233 *
1234 * OtherName ::= SEQUENCE {
1235 * type-id OBJECT IDENTIFIER,
1236 * value [0] EXPLICIT ANY DEFINED BY type-id }
1237 *
1238 * EDIPartyName ::= SEQUENCE {
1239 * nameAssigner [0] DirectoryString OPTIONAL,
1240 * partyName [1] DirectoryString }
1241 *
1242 * We list all types, but use the following GeneralName types from RFC 5280:
1243 * "dnsName", "uniformResourceIdentifier" and "hardware_module_name"
1244 * of type "otherName", as defined in RFC 4108.
1245 */
1246int mbedtls_x509_get_subject_alt_name(unsigned char **p,
1247 const unsigned char *end,
1248 mbedtls_x509_sequence *subject_alt_name)
1249{
1250 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1251 size_t len;
1252
1253 /* Get main sequence tag */
1254 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
1255 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1256 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1257 }
1258
1259 if (*p + len != end) {
1260 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1261 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1262 }
1263
1264 return mbedtls_x509_get_subject_alt_name_ext(p, end, subject_alt_name);
1265}
1266
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001267int mbedtls_x509_get_ns_cert_type(unsigned char **p,
1268 const unsigned char *end,
1269 unsigned char *ns_cert_type)
1270{
1271 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1272 mbedtls_x509_bitstring bs = { 0, 0, NULL };
1273
1274 if ((ret = mbedtls_asn1_get_bitstring(p, end, &bs)) != 0) {
1275 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1276 }
1277
Przemek Stekiel32e20912023-01-25 14:26:15 +01001278 /* A bitstring with no flags set is still technically valid, as it will mean
1279 that the certificate has no designated purpose at the time of creation. */
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001280 if (bs.len == 0) {
1281 *ns_cert_type = 0;
1282 return 0;
1283 }
1284
1285 if (bs.len != 1) {
1286 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1287 MBEDTLS_ERR_ASN1_INVALID_LENGTH);
1288 }
1289
1290 /* Get actual bitstring */
1291 *ns_cert_type = *bs.p;
1292 return 0;
1293}
1294
1295int mbedtls_x509_get_key_usage(unsigned char **p,
1296 const unsigned char *end,
1297 unsigned int *key_usage)
1298{
1299 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1300 size_t i;
1301 mbedtls_x509_bitstring bs = { 0, 0, NULL };
1302
1303 if ((ret = mbedtls_asn1_get_bitstring(p, end, &bs)) != 0) {
1304 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1305 }
1306
Przemek Stekiel32e20912023-01-25 14:26:15 +01001307 /* A bitstring with no flags set is still technically valid, as it will mean
1308 that the certificate has no designated purpose at the time of creation. */
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001309 if (bs.len == 0) {
1310 *key_usage = 0;
1311 return 0;
1312 }
1313
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001314 /* Get actual bitstring */
1315 *key_usage = 0;
1316 for (i = 0; i < bs.len && i < sizeof(unsigned int); i++) {
1317 *key_usage |= (unsigned int) bs.p[i] << (8*i);
1318 }
1319
1320 return 0;
1321}
1322
1323int mbedtls_x509_parse_subject_alt_name(const mbedtls_x509_buf *san_buf,
1324 mbedtls_x509_subject_alternative_name *san)
1325{
1326 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1327 switch (san_buf->tag &
1328 (MBEDTLS_ASN1_TAG_CLASS_MASK |
1329 MBEDTLS_ASN1_TAG_VALUE_MASK)) {
1330 /*
1331 * otherName
1332 */
1333 case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME):
1334 {
1335 mbedtls_x509_san_other_name other_name;
1336
1337 ret = x509_get_other_name(san_buf, &other_name);
1338 if (ret != 0) {
1339 return ret;
1340 }
1341
1342 memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name));
1343 san->type = MBEDTLS_X509_SAN_OTHER_NAME;
1344 memcpy(&san->san.other_name,
1345 &other_name, sizeof(other_name));
1346
1347 }
1348 break;
Andrzej Kurek7a05fab2023-02-13 10:03:07 -05001349 /*
1350 * uniformResourceIdentifier
1351 */
1352 case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER):
1353 {
1354 memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name));
1355 san->type = MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001356
Andrzej Kurek7a05fab2023-02-13 10:03:07 -05001357 memcpy(&san->san.unstructured_name,
1358 san_buf, sizeof(*san_buf));
1359
1360 }
1361 break;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001362 /*
1363 * dNSName
1364 */
1365 case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_DNS_NAME):
1366 {
1367 memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name));
1368 san->type = MBEDTLS_X509_SAN_DNS_NAME;
1369
1370 memcpy(&san->san.unstructured_name,
1371 san_buf, sizeof(*san_buf));
Przemek Stekielecee12f2023-02-09 14:43:49 +01001372 }
1373 break;
Przemek Stekiel0ab5b932023-05-29 16:30:50 +02001374 /*
1375 * IP address
1376 */
1377 case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_IP_ADDRESS):
1378 {
1379 memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name));
1380 san->type = MBEDTLS_X509_SAN_IP_ADDRESS;
Przemek Stekiel01cb6eb2023-06-05 16:38:13 +02001381 // Only IPv6 (16 bytes) and IPv4 (4 bytes) types are supported
1382 if (san_buf->len == 4 || san_buf->len == 16) {
1383 memcpy(&san->san.unstructured_name,
1384 san_buf, sizeof(*san_buf));
1385 } else {
1386 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1387 }
Przemek Stekiel0ab5b932023-05-29 16:30:50 +02001388 }
1389 break;
Przemek Stekielecee12f2023-02-09 14:43:49 +01001390 /*
Andrzej Kurek154a6052023-04-30 14:11:49 -04001391 * rfc822Name
Przemek Stekielecee12f2023-02-09 14:43:49 +01001392 */
1393 case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_RFC822_NAME):
1394 {
1395 memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name));
1396 san->type = MBEDTLS_X509_SAN_RFC822_NAME;
1397 memcpy(&san->san.unstructured_name, san_buf, sizeof(*san_buf));
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001398 }
1399 break;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001400 /*
Andrzej Kureke12b01d2023-01-10 06:47:38 -05001401 * directoryName
1402 */
1403 case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_DIRECTORY_NAME):
1404 {
1405 size_t name_len;
1406 unsigned char *p = san_buf->p;
1407 memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name));
1408 san->type = MBEDTLS_X509_SAN_DIRECTORY_NAME;
1409
1410 ret = mbedtls_asn1_get_tag(&p, p + san_buf->len, &name_len,
1411 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE);
1412
1413 if (ret != 0) {
Andrzej Kurekbf8ccd82023-02-13 07:13:30 -05001414 return ret;
Andrzej Kureke12b01d2023-01-10 06:47:38 -05001415 }
1416
1417 if ((ret = mbedtls_x509_get_name(&p, p + name_len,
1418 &san->san.directory_name)) != 0) {
Andrzej Kurekbf8ccd82023-02-13 07:13:30 -05001419 return ret;
Andrzej Kureke12b01d2023-01-10 06:47:38 -05001420 }
1421 }
1422 break;
1423 /*
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001424 * Type not supported
1425 */
1426 default:
1427 return MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
1428 }
1429 return 0;
1430}
1431
Andrzej Kurekd40c2b62023-02-13 07:01:59 -05001432void mbedtls_x509_free_subject_alt_name(mbedtls_x509_subject_alternative_name *san)
1433{
1434 if (san->type == MBEDTLS_X509_SAN_DIRECTORY_NAME) {
1435 mbedtls_asn1_free_named_data_list_shallow(san->san.directory_name.next);
1436 }
1437}
1438
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001439#if !defined(MBEDTLS_X509_REMOVE_INFO)
1440int mbedtls_x509_info_subject_alt_name(char **buf, size_t *size,
1441 const mbedtls_x509_sequence
1442 *subject_alt_name,
1443 const char *prefix)
1444{
1445 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1446 size_t i;
1447 size_t n = *size;
1448 char *p = *buf;
1449 const mbedtls_x509_sequence *cur = subject_alt_name;
1450 mbedtls_x509_subject_alternative_name san;
1451 int parse_ret;
1452
1453 while (cur != NULL) {
1454 memset(&san, 0, sizeof(san));
1455 parse_ret = mbedtls_x509_parse_subject_alt_name(&cur->buf, &san);
1456 if (parse_ret != 0) {
1457 if (parse_ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE) {
1458 ret = mbedtls_snprintf(p, n, "\n%s <unsupported>", prefix);
1459 MBEDTLS_X509_SAFE_SNPRINTF;
1460 } else {
1461 ret = mbedtls_snprintf(p, n, "\n%s <malformed>", prefix);
1462 MBEDTLS_X509_SAFE_SNPRINTF;
1463 }
1464 cur = cur->next;
1465 continue;
1466 }
1467
1468 switch (san.type) {
1469 /*
1470 * otherName
1471 */
1472 case MBEDTLS_X509_SAN_OTHER_NAME:
1473 {
1474 mbedtls_x509_san_other_name *other_name = &san.san.other_name;
1475
1476 ret = mbedtls_snprintf(p, n, "\n%s otherName :", prefix);
1477 MBEDTLS_X509_SAFE_SNPRINTF;
1478
1479 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ON_HW_MODULE_NAME,
1480 &other_name->value.hardware_module_name.oid) != 0) {
1481 ret = mbedtls_snprintf(p, n, "\n%s hardware module name :", prefix);
1482 MBEDTLS_X509_SAFE_SNPRINTF;
1483 ret =
1484 mbedtls_snprintf(p, n, "\n%s hardware type : ", prefix);
1485 MBEDTLS_X509_SAFE_SNPRINTF;
1486
1487 ret = mbedtls_oid_get_numeric_string(p,
1488 n,
1489 &other_name->value.hardware_module_name.oid);
1490 MBEDTLS_X509_SAFE_SNPRINTF;
1491
1492 ret =
1493 mbedtls_snprintf(p, n, "\n%s hardware serial number : ", prefix);
1494 MBEDTLS_X509_SAFE_SNPRINTF;
1495
1496 for (i = 0; i < other_name->value.hardware_module_name.val.len; i++) {
1497 ret = mbedtls_snprintf(p,
1498 n,
1499 "%02X",
1500 other_name->value.hardware_module_name.val.p[i]);
1501 MBEDTLS_X509_SAFE_SNPRINTF;
1502 }
1503 }/* MBEDTLS_OID_ON_HW_MODULE_NAME */
1504 }
1505 break;
Andrzej Kurek7a05fab2023-02-13 10:03:07 -05001506 /*
1507 * uniformResourceIdentifier
1508 */
1509 case MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER:
1510 {
1511 ret = mbedtls_snprintf(p, n, "\n%s uniformResourceIdentifier : ", prefix);
1512 MBEDTLS_X509_SAFE_SNPRINTF;
1513 if (san.san.unstructured_name.len >= n) {
Przemek Stekiel01cb6eb2023-06-05 16:38:13 +02001514 if (n > 0) {
1515 *p = '\0';
1516 }
Andrzej Kurek7a05fab2023-02-13 10:03:07 -05001517 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1518 }
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001519
Andrzej Kurek7a05fab2023-02-13 10:03:07 -05001520 memcpy(p, san.san.unstructured_name.p, san.san.unstructured_name.len);
1521 p += san.san.unstructured_name.len;
1522 n -= san.san.unstructured_name.len;
1523 }
1524 break;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001525 /*
1526 * dNSName
Przemek Stekiel5b9e4162023-02-15 12:56:37 +01001527 * RFC822 Name
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001528 */
1529 case MBEDTLS_X509_SAN_DNS_NAME:
Przemek Stekiel5b9e4162023-02-15 12:56:37 +01001530 case MBEDTLS_X509_SAN_RFC822_NAME:
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001531 {
Przemek Stekiel82d250d2023-02-15 15:00:50 +01001532 const char *dns_name = "dNSName";
1533 const char *rfc822_name = "rfc822Name";
Przemek Stekiel5b9e4162023-02-15 12:56:37 +01001534
Przemek Stekiel82d250d2023-02-15 15:00:50 +01001535 ret = mbedtls_snprintf(p, n,
1536 "\n%s %s : ",
1537 prefix,
Przemek Stekiel093c97d2023-06-02 10:11:32 +02001538 san.type ==
1539 MBEDTLS_X509_SAN_DNS_NAME ? dns_name : rfc822_name);
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001540 MBEDTLS_X509_SAFE_SNPRINTF;
1541 if (san.san.unstructured_name.len >= n) {
Przemek Stekiel01cb6eb2023-06-05 16:38:13 +02001542 if (n > 0) {
1543 *p = '\0';
1544 }
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001545 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1546 }
1547
Przemek Stekiel093c97d2023-06-02 10:11:32 +02001548 memcpy(p, san.san.unstructured_name.p, san.san.unstructured_name.len);
1549 p += san.san.unstructured_name.len;
1550 n -= san.san.unstructured_name.len;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001551 }
1552 break;
Przemek Stekiel093c97d2023-06-02 10:11:32 +02001553 /*
1554 * iPAddress
1555 */
1556 case MBEDTLS_X509_SAN_IP_ADDRESS:
1557 {
1558 ret = mbedtls_snprintf(p, n, "\n%s %s : ",
1559 prefix, "iPAddress");
1560 MBEDTLS_X509_SAFE_SNPRINTF;
1561 if (san.san.unstructured_name.len >= n) {
Przemek Stekiel01cb6eb2023-06-05 16:38:13 +02001562 if (n > 0) {
1563 *p = '\0';
1564 }
Przemek Stekiel093c97d2023-06-02 10:11:32 +02001565 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1566 }
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001567
Przemek Stekiel093c97d2023-06-02 10:11:32 +02001568 unsigned char *ip = san.san.unstructured_name.p;
1569 // Only IPv6 (16 bytes) and IPv4 (4 bytes) types are supported
1570 if (san.san.unstructured_name.len == 4) {
Przemek Stekiel4d3fc212023-06-06 11:40:32 +02001571 ret = mbedtls_snprintf(p, n, "%u.%u.%u.%u", ip[0], ip[1], ip[2], ip[3]);
1572 MBEDTLS_X509_SAFE_SNPRINTF;
Przemek Stekiel093c97d2023-06-02 10:11:32 +02001573 } else if (san.san.unstructured_name.len == 16) {
Przemek Stekiel4d3fc212023-06-06 11:40:32 +02001574 ret = mbedtls_snprintf(p, n,
Przemek Stekiel01cb6eb2023-06-05 16:38:13 +02001575 "%X%X:%X%X:%X%X:%X%X:%X%X:%X%X:%X%X:%X%X",
1576 ip[0], ip[1], ip[2], ip[3], ip[4], ip[5], ip[6],
Przemek Stekiel4d3fc212023-06-06 11:40:32 +02001577 ip[7], ip[8], ip[9], ip[10], ip[11], ip[12], ip[13],
Przemek Stekiel01cb6eb2023-06-05 16:38:13 +02001578 ip[14], ip[15]);
Przemek Stekiel4d3fc212023-06-06 11:40:32 +02001579 MBEDTLS_X509_SAFE_SNPRINTF;
Przemek Stekiel093c97d2023-06-02 10:11:32 +02001580 } else {
Przemek Stekiel01cb6eb2023-06-05 16:38:13 +02001581 if (n > 0) {
1582 *p = '\0';
1583 }
Przemek Stekiel093c97d2023-06-02 10:11:32 +02001584 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1585 }
Przemek Stekiel093c97d2023-06-02 10:11:32 +02001586 }
1587 break;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001588 /*
Andrzej Kureke12b01d2023-01-10 06:47:38 -05001589 * directoryName
1590 */
1591 case MBEDTLS_X509_SAN_DIRECTORY_NAME:
1592 {
1593 ret = mbedtls_snprintf(p, n, "\n%s directoryName : ", prefix);
Andrzej Kurek5f0c6e82023-02-27 16:03:41 -05001594 if (ret < 0 || (size_t) ret >= n) {
1595 mbedtls_x509_free_subject_alt_name(&san);
1596 }
1597
Andrzej Kureke12b01d2023-01-10 06:47:38 -05001598 MBEDTLS_X509_SAFE_SNPRINTF;
1599 ret = mbedtls_x509_dn_gets(p, n, &san.san.directory_name);
Andrzej Kurekd40c2b62023-02-13 07:01:59 -05001600
Andrzej Kureke12b01d2023-01-10 06:47:38 -05001601 if (ret < 0) {
Andrzej Kurek5f0c6e82023-02-27 16:03:41 -05001602 mbedtls_x509_free_subject_alt_name(&san);
Przemek Stekiel01cb6eb2023-06-05 16:38:13 +02001603 if (n > 0) {
1604 *p = '\0';
1605 }
Andrzej Kureke12b01d2023-01-10 06:47:38 -05001606 return ret;
1607 }
1608
1609 p += ret;
1610 n -= ret;
1611 }
1612 break;
1613 /*
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001614 * Type not supported, skip item.
1615 */
1616 default:
1617 ret = mbedtls_snprintf(p, n, "\n%s <unsupported>", prefix);
1618 MBEDTLS_X509_SAFE_SNPRINTF;
1619 break;
1620 }
1621
Andrzej Kurekd40c2b62023-02-13 07:01:59 -05001622 /* So far memory is freed only in the case of directoryName
Andrzej Kurek5f0c6e82023-02-27 16:03:41 -05001623 * parsing succeeding, as mbedtls_x509_get_name allocates memory. */
Andrzej Kurekd40c2b62023-02-13 07:01:59 -05001624 mbedtls_x509_free_subject_alt_name(&san);
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001625 cur = cur->next;
1626 }
1627
1628 *p = '\0';
1629
1630 *size = n;
1631 *buf = p;
1632
1633 return 0;
1634}
1635
1636#define PRINT_ITEM(i) \
1637 { \
1638 ret = mbedtls_snprintf(p, n, "%s" i, sep); \
1639 MBEDTLS_X509_SAFE_SNPRINTF; \
1640 sep = ", "; \
1641 }
1642
1643#define CERT_TYPE(type, name) \
1644 if (ns_cert_type & (type)) \
1645 PRINT_ITEM(name);
1646
1647int mbedtls_x509_info_cert_type(char **buf, size_t *size,
1648 unsigned char ns_cert_type)
1649{
1650 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1651 size_t n = *size;
1652 char *p = *buf;
1653 const char *sep = "";
1654
1655 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT, "SSL Client");
1656 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER, "SSL Server");
1657 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_EMAIL, "Email");
1658 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing");
1659 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_RESERVED, "Reserved");
1660 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_SSL_CA, "SSL CA");
1661 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA, "Email CA");
1662 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA");
1663
1664 *size = n;
1665 *buf = p;
1666
1667 return 0;
1668}
1669
1670#define KEY_USAGE(code, name) \
1671 if (key_usage & (code)) \
1672 PRINT_ITEM(name);
1673
1674int mbedtls_x509_info_key_usage(char **buf, size_t *size,
1675 unsigned int key_usage)
1676{
1677 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1678 size_t n = *size;
1679 char *p = *buf;
1680 const char *sep = "";
1681
1682 KEY_USAGE(MBEDTLS_X509_KU_DIGITAL_SIGNATURE, "Digital Signature");
1683 KEY_USAGE(MBEDTLS_X509_KU_NON_REPUDIATION, "Non Repudiation");
1684 KEY_USAGE(MBEDTLS_X509_KU_KEY_ENCIPHERMENT, "Key Encipherment");
1685 KEY_USAGE(MBEDTLS_X509_KU_DATA_ENCIPHERMENT, "Data Encipherment");
1686 KEY_USAGE(MBEDTLS_X509_KU_KEY_AGREEMENT, "Key Agreement");
1687 KEY_USAGE(MBEDTLS_X509_KU_KEY_CERT_SIGN, "Key Cert Sign");
1688 KEY_USAGE(MBEDTLS_X509_KU_CRL_SIGN, "CRL Sign");
1689 KEY_USAGE(MBEDTLS_X509_KU_ENCIPHER_ONLY, "Encipher Only");
1690 KEY_USAGE(MBEDTLS_X509_KU_DECIPHER_ONLY, "Decipher Only");
1691
1692 *size = n;
1693 *buf = p;
1694
1695 return 0;
1696}
1697#endif /* MBEDTLS_X509_REMOVE_INFO */
1698#endif /* MBEDTLS_X509_CRT_PARSE_C || MBEDTLS_X509_CSR_PARSE_C */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001699#endif /* MBEDTLS_X509_USE_C */