blob: b8a866ac38ef75b25b70fc01a2c7eda3eb650fd2 [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
Demi Marie Obenour690b8c92022-12-04 04:24:22 -050056#define CHECK(code) \
57 do { \
58 if ((ret = (code)) != 0) { \
59 return ret; \
60 } \
61 } while (0)
62
Hanno Becker1eeca412018-10-15 12:01:35 +010063#define CHECK_RANGE(min, max, val) \
Demi Marie Obenour690b8c92022-12-04 04:24:22 -050064 do { \
65 if ((val) < (min) || (val) > (max)) { \
66 return ret; \
Hanno Becker1eeca412018-10-15 12:01:35 +010067 } \
Gilles Peskine449bd832023-01-11 14:50:10 +010068 } while (0)
Rich Evans7d5a55a2015-02-13 11:48:02 +000069
Paul Bakker7c6b2c32013-09-16 13:49:26 +020070/*
71 * CertificateSerialNumber ::= INTEGER
72 */
Gilles Peskine449bd832023-01-11 14:50:10 +010073int mbedtls_x509_get_serial(unsigned char **p, const unsigned char *end,
74 mbedtls_x509_buf *serial)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020075{
Janos Follath865b3eb2019-12-16 11:46:15 +000076 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020077
Gilles Peskine449bd832023-01-11 14:50:10 +010078 if ((end - *p) < 1) {
79 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_SERIAL,
80 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
81 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +020082
Gilles Peskine449bd832023-01-11 14:50:10 +010083 if (**p != (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_PRIMITIVE | 2) &&
84 **p != MBEDTLS_ASN1_INTEGER) {
85 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_SERIAL,
86 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
87 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +020088
89 serial->tag = *(*p)++;
90
Gilles Peskine449bd832023-01-11 14:50:10 +010091 if ((ret = mbedtls_asn1_get_len(p, end, &serial->len)) != 0) {
92 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_SERIAL, ret);
93 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +020094
95 serial->p = *p;
96 *p += serial->len;
97
Gilles Peskine449bd832023-01-11 14:50:10 +010098 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020099}
100
101/* Get an algorithm identifier without parameters (eg for signatures)
102 *
103 * AlgorithmIdentifier ::= SEQUENCE {
104 * algorithm OBJECT IDENTIFIER,
105 * parameters ANY DEFINED BY algorithm OPTIONAL }
106 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100107int mbedtls_x509_get_alg_null(unsigned char **p, const unsigned char *end,
108 mbedtls_x509_buf *alg)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200109{
Janos Follath865b3eb2019-12-16 11:46:15 +0000110 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200111
Gilles Peskine449bd832023-01-11 14:50:10 +0100112 if ((ret = mbedtls_asn1_get_alg_null(p, end, alg)) != 0) {
113 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
114 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200115
Gilles Peskine449bd832023-01-11 14:50:10 +0100116 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200117}
118
119/*
Antonin Décimo36e89b52019-01-23 15:24:37 +0100120 * Parse an algorithm identifier with (optional) parameters
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100121 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100122int mbedtls_x509_get_alg(unsigned char **p, const unsigned char *end,
123 mbedtls_x509_buf *alg, mbedtls_x509_buf *params)
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100124{
Janos Follath865b3eb2019-12-16 11:46:15 +0000125 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100126
Gilles Peskine449bd832023-01-11 14:50:10 +0100127 if ((ret = mbedtls_asn1_get_alg(p, end, alg, params)) != 0) {
128 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
129 }
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100130
Gilles Peskine449bd832023-01-11 14:50:10 +0100131 return 0;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100132}
133
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200134/*
135 * Convert md type to string
136 */
Dave Rodgmanffabb7b2023-06-28 16:22:50 +0100137#if !defined(MBEDTLS_X509_REMOVE_INFO) && defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
138
Gilles Peskine449bd832023-01-11 14:50:10 +0100139static inline const char *md_type_to_string(mbedtls_md_type_t md_alg)
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200140{
Gilles Peskine449bd832023-01-11 14:50:10 +0100141 switch (md_alg) {
Manuel Pégourié-Gonnard49e67f82023-03-16 11:39:20 +0100142#if defined(MBEDTLS_MD_CAN_MD5)
Gilles Peskine449bd832023-01-11 14:50:10 +0100143 case MBEDTLS_MD_MD5:
144 return "MD5";
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200145#endif
Manuel Pégourié-Gonnard49e67f82023-03-16 11:39:20 +0100146#if defined(MBEDTLS_MD_CAN_SHA1)
Gilles Peskine449bd832023-01-11 14:50:10 +0100147 case MBEDTLS_MD_SHA1:
148 return "SHA1";
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200149#endif
Manuel Pégourié-Gonnard49e67f82023-03-16 11:39:20 +0100150#if defined(MBEDTLS_MD_CAN_SHA224)
Gilles Peskine449bd832023-01-11 14:50:10 +0100151 case MBEDTLS_MD_SHA224:
152 return "SHA224";
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200153#endif
Manuel Pégourié-Gonnard49e67f82023-03-16 11:39:20 +0100154#if defined(MBEDTLS_MD_CAN_SHA256)
Gilles Peskine449bd832023-01-11 14:50:10 +0100155 case MBEDTLS_MD_SHA256:
156 return "SHA256";
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200157#endif
Manuel Pégourié-Gonnard49e67f82023-03-16 11:39:20 +0100158#if defined(MBEDTLS_MD_CAN_SHA384)
Gilles Peskine449bd832023-01-11 14:50:10 +0100159 case MBEDTLS_MD_SHA384:
160 return "SHA384";
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200161#endif
Manuel Pégourié-Gonnard49e67f82023-03-16 11:39:20 +0100162#if defined(MBEDTLS_MD_CAN_SHA512)
Gilles Peskine449bd832023-01-11 14:50:10 +0100163 case MBEDTLS_MD_SHA512:
164 return "SHA512";
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200165#endif
Manuel Pégourié-Gonnard49e67f82023-03-16 11:39:20 +0100166#if defined(MBEDTLS_MD_CAN_RIPEMD160)
Gilles Peskine449bd832023-01-11 14:50:10 +0100167 case MBEDTLS_MD_RIPEMD160:
168 return "RIPEMD160";
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200169#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100170 case MBEDTLS_MD_NONE:
171 return NULL;
172 default:
173 return NULL;
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200174 }
175}
176
Dave Rodgmanf032c982023-06-29 12:09:27 +0100177#endif /* !defined(MBEDTLS_X509_REMOVE_INFO) && defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) */
Dave Rodgmanffabb7b2023-06-28 16:22:50 +0100178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100180/*
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100181 * HashAlgorithm ::= AlgorithmIdentifier
182 *
183 * AlgorithmIdentifier ::= SEQUENCE {
184 * algorithm OBJECT IDENTIFIER,
185 * parameters ANY DEFINED BY algorithm OPTIONAL }
186 *
187 * For HashAlgorithm, parameters MUST be NULL or absent.
188 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100189static 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 +0100190{
Janos Follath865b3eb2019-12-16 11:46:15 +0000191 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100192 unsigned char *p;
193 const unsigned char *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194 mbedtls_x509_buf md_oid;
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100195 size_t len;
196
197 /* Make sure we got a SEQUENCE and setup bounds */
Gilles Peskine449bd832023-01-11 14:50:10 +0100198 if (alg->tag != (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) {
199 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
200 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
201 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100202
Andrzej Kurekfeaebc52020-07-16 04:37:41 -0400203 p = alg->p;
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100204 end = p + alg->len;
205
Gilles Peskine449bd832023-01-11 14:50:10 +0100206 if (p >= end) {
207 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
208 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
209 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100210
211 /* Parse md_oid */
212 md_oid.tag = *p;
213
Gilles Peskine449bd832023-01-11 14:50:10 +0100214 if ((ret = mbedtls_asn1_get_tag(&p, end, &md_oid.len, MBEDTLS_ASN1_OID)) != 0) {
215 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
216 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100217
218 md_oid.p = p;
219 p += md_oid.len;
220
221 /* Get md_alg from md_oid */
Gilles Peskine449bd832023-01-11 14:50:10 +0100222 if ((ret = mbedtls_oid_get_md_alg(&md_oid, md_alg)) != 0) {
223 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
224 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100225
226 /* Make sure params is absent of NULL */
Gilles Peskine449bd832023-01-11 14:50:10 +0100227 if (p == end) {
228 return 0;
229 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100230
Gilles Peskine449bd832023-01-11 14:50:10 +0100231 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_NULL)) != 0 || len != 0) {
232 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
233 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100234
Gilles Peskine449bd832023-01-11 14:50:10 +0100235 if (p != end) {
236 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
237 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
238 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100239
Gilles Peskine449bd832023-01-11 14:50:10 +0100240 return 0;
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100241}
242
243/*
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100244 * RSASSA-PSS-params ::= SEQUENCE {
245 * hashAlgorithm [0] HashAlgorithm DEFAULT sha1Identifier,
246 * maskGenAlgorithm [1] MaskGenAlgorithm DEFAULT mgf1SHA1Identifier,
247 * saltLength [2] INTEGER DEFAULT 20,
248 * trailerField [3] INTEGER DEFAULT 1 }
249 * -- Note that the tags in this Sequence are explicit.
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200250 *
251 * RFC 4055 (which defines use of RSASSA-PSS in PKIX) states that the value
252 * of trailerField MUST be 1, and PKCS#1 v2.2 doesn't even define any other
Tom Cosgrove1797b052022-12-04 17:19:59 +0000253 * option. Enforce this at parsing time.
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100254 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100255int mbedtls_x509_get_rsassa_pss_params(const mbedtls_x509_buf *params,
256 mbedtls_md_type_t *md_alg, mbedtls_md_type_t *mgf_md,
257 int *salt_len)
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100258{
Janos Follath865b3eb2019-12-16 11:46:15 +0000259 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100260 unsigned char *p;
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100261 const unsigned char *end, *end2;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100262 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200263 mbedtls_x509_buf alg_id, alg_params;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100264
265 /* First set everything to defaults */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200266 *md_alg = MBEDTLS_MD_SHA1;
267 *mgf_md = MBEDTLS_MD_SHA1;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100268 *salt_len = 20;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100269
270 /* Make sure params is a SEQUENCE and setup bounds */
Gilles Peskine449bd832023-01-11 14:50:10 +0100271 if (params->tag != (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) {
272 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
273 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
274 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100275
276 p = (unsigned char *) params->p;
277 end = p + params->len;
278
Gilles Peskine449bd832023-01-11 14:50:10 +0100279 if (p == end) {
280 return 0;
281 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100282
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100283 /*
284 * HashAlgorithm
285 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100286 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
287 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
288 0)) == 0) {
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100289 end2 = p + len;
290
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100291 /* HashAlgorithm ::= AlgorithmIdentifier (without parameters) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100292 if ((ret = mbedtls_x509_get_alg_null(&p, end2, &alg_id)) != 0) {
293 return ret;
294 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100295
Gilles Peskine449bd832023-01-11 14:50:10 +0100296 if ((ret = mbedtls_oid_get_md_alg(&alg_id, md_alg)) != 0) {
297 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
298 }
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100299
Gilles Peskine449bd832023-01-11 14:50:10 +0100300 if (p != end2) {
301 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
302 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
303 }
304 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
305 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100306 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100307
Gilles Peskine449bd832023-01-11 14:50:10 +0100308 if (p == end) {
309 return 0;
310 }
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100311
312 /*
313 * MaskGenAlgorithm
314 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100315 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
316 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
317 1)) == 0) {
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100318 end2 = p + len;
319
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100320 /* MaskGenAlgorithm ::= AlgorithmIdentifier (params = HashAlgorithm) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100321 if ((ret = mbedtls_x509_get_alg(&p, end2, &alg_id, &alg_params)) != 0) {
322 return ret;
323 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100324
325 /* Only MFG1 is recognised for now */
Gilles Peskine449bd832023-01-11 14:50:10 +0100326 if (MBEDTLS_OID_CMP(MBEDTLS_OID_MGF1, &alg_id) != 0) {
327 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE,
328 MBEDTLS_ERR_OID_NOT_FOUND);
329 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100330
331 /* Parse HashAlgorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +0100332 if ((ret = x509_get_hash_alg(&alg_params, mgf_md)) != 0) {
333 return ret;
334 }
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100335
Gilles Peskine449bd832023-01-11 14:50:10 +0100336 if (p != end2) {
337 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
338 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
339 }
340 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
341 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100342 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100343
Gilles Peskine449bd832023-01-11 14:50:10 +0100344 if (p == end) {
345 return 0;
346 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100347
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100348 /*
349 * salt_len
350 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100351 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
352 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
353 2)) == 0) {
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100354 end2 = p + len;
355
Gilles Peskine449bd832023-01-11 14:50:10 +0100356 if ((ret = mbedtls_asn1_get_int(&p, end2, salt_len)) != 0) {
357 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
358 }
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100359
Gilles Peskine449bd832023-01-11 14:50:10 +0100360 if (p != end2) {
361 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
362 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
363 }
364 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
365 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100366 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100367
Gilles Peskine449bd832023-01-11 14:50:10 +0100368 if (p == end) {
369 return 0;
370 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100371
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100372 /*
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200373 * trailer_field (if present, must be 1)
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100374 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100375 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
376 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
377 3)) == 0) {
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200378 int trailer_field;
379
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100380 end2 = p + len;
381
Gilles Peskine449bd832023-01-11 14:50:10 +0100382 if ((ret = mbedtls_asn1_get_int(&p, end2, &trailer_field)) != 0) {
383 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
384 }
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100385
Gilles Peskine449bd832023-01-11 14:50:10 +0100386 if (p != end2) {
387 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
388 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
389 }
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200390
Gilles Peskine449bd832023-01-11 14:50:10 +0100391 if (trailer_field != 1) {
392 return MBEDTLS_ERR_X509_INVALID_ALG;
393 }
394 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
395 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100396 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100397
Gilles Peskine449bd832023-01-11 14:50:10 +0100398 if (p != end) {
399 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
400 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
401 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100402
Gilles Peskine449bd832023-01-11 14:50:10 +0100403 return 0;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100404}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200405#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100406
407/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200408 * AttributeTypeAndValue ::= SEQUENCE {
409 * type AttributeType,
410 * value AttributeValue }
411 *
412 * AttributeType ::= OBJECT IDENTIFIER
413 *
414 * AttributeValue ::= ANY DEFINED BY AttributeType
415 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100416static int x509_get_attr_type_value(unsigned char **p,
417 const unsigned char *end,
418 mbedtls_x509_name *cur)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200419{
Janos Follath865b3eb2019-12-16 11:46:15 +0000420 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200421 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200422 mbedtls_x509_buf *oid;
423 mbedtls_x509_buf *val;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200424
Gilles Peskine449bd832023-01-11 14:50:10 +0100425 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
426 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
427 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME, ret);
428 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200429
Hanno Becker12f62fb2019-02-12 17:22:36 +0000430 end = *p + len;
431
Gilles Peskine449bd832023-01-11 14:50:10 +0100432 if ((end - *p) < 1) {
433 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME,
434 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
435 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200436
437 oid = &cur->oid;
438 oid->tag = **p;
439
Gilles Peskine449bd832023-01-11 14:50:10 +0100440 if ((ret = mbedtls_asn1_get_tag(p, end, &oid->len, MBEDTLS_ASN1_OID)) != 0) {
441 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME, ret);
442 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200443
444 oid->p = *p;
445 *p += oid->len;
446
Gilles Peskine449bd832023-01-11 14:50:10 +0100447 if ((end - *p) < 1) {
448 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME,
449 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
450 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200451
Gilles Peskine449bd832023-01-11 14:50:10 +0100452 if (**p != MBEDTLS_ASN1_BMP_STRING && **p != MBEDTLS_ASN1_UTF8_STRING &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200453 **p != MBEDTLS_ASN1_T61_STRING && **p != MBEDTLS_ASN1_PRINTABLE_STRING &&
454 **p != MBEDTLS_ASN1_IA5_STRING && **p != MBEDTLS_ASN1_UNIVERSAL_STRING &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100455 **p != MBEDTLS_ASN1_BIT_STRING) {
456 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME,
457 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
458 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200459
460 val = &cur->val;
461 val->tag = *(*p)++;
462
Gilles Peskine449bd832023-01-11 14:50:10 +0100463 if ((ret = mbedtls_asn1_get_len(p, end, &val->len)) != 0) {
464 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME, ret);
465 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200466
467 val->p = *p;
468 *p += val->len;
469
Gilles Peskine449bd832023-01-11 14:50:10 +0100470 if (*p != end) {
471 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME,
472 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Hanno Becker12f62fb2019-02-12 17:22:36 +0000473 }
474
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200475 cur->next = NULL;
476
Gilles Peskine449bd832023-01-11 14:50:10 +0100477 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200478}
479
480/*
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000481 * Name ::= CHOICE { -- only one possibility for now --
482 * rdnSequence RDNSequence }
483 *
484 * RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
485 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200486 * RelativeDistinguishedName ::=
487 * SET OF AttributeTypeAndValue
488 *
489 * AttributeTypeAndValue ::= SEQUENCE {
490 * type AttributeType,
491 * value AttributeValue }
492 *
493 * AttributeType ::= OBJECT IDENTIFIER
494 *
495 * AttributeValue ::= ANY DEFINED BY AttributeType
Manuel Pégourié-Gonnard5d861852014-10-17 12:41:41 +0200496 *
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000497 * The data structure is optimized for the common case where each RDN has only
498 * one element, which is represented as a list of AttributeTypeAndValue.
499 * For the general case we still use a flat list, but we mark elements of the
500 * same set so that they are "merged" together in the functions that consume
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200501 * this list, eg mbedtls_x509_dn_gets().
David Horstmanned794832022-10-04 18:12:06 +0100502 *
David Horstmann11307a12022-10-17 18:10:23 +0100503 * On success, this function may allocate a linked list starting at cur->next
David Horstmanned794832022-10-04 18:12:06 +0100504 * that must later be free'd by the caller using mbedtls_free(). In error
505 * cases, this function frees all allocated memory internally and the caller
506 * has no freeing responsibilities.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200507 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100508int mbedtls_x509_get_name(unsigned char **p, const unsigned char *end,
509 mbedtls_x509_name *cur)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200510{
Janos Follath865b3eb2019-12-16 11:46:15 +0000511 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard5d861852014-10-17 12:41:41 +0200512 size_t set_len;
513 const unsigned char *end_set;
David Horstmanned794832022-10-04 18:12:06 +0100514 mbedtls_x509_name *head = cur;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200515
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100516 /* don't use recursion, we'd risk stack overflow if not optimized */
Gilles Peskine449bd832023-01-11 14:50:10 +0100517 while (1) {
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100518 /*
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000519 * parse SET
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100520 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100521 if ((ret = mbedtls_asn1_get_tag(p, end, &set_len,
522 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SET)) != 0) {
523 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME, ret);
David Horstmanned794832022-10-04 18:12:06 +0100524 goto error;
525 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200526
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100527 end_set = *p + set_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200528
Gilles Peskine449bd832023-01-11 14:50:10 +0100529 while (1) {
530 if ((ret = x509_get_attr_type_value(p, end_set, cur)) != 0) {
David Horstmanned794832022-10-04 18:12:06 +0100531 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +0100532 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200533
Gilles Peskine449bd832023-01-11 14:50:10 +0100534 if (*p == end_set) {
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000535 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100536 }
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000537
Manuel Pégourié-Gonnardeecb43c2015-05-12 12:56:41 +0200538 /* Mark this item as being no the only one in a set */
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000539 cur->next_merged = 1;
540
Gilles Peskine449bd832023-01-11 14:50:10 +0100541 cur->next = mbedtls_calloc(1, sizeof(mbedtls_x509_name));
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000542
Gilles Peskine449bd832023-01-11 14:50:10 +0100543 if (cur->next == NULL) {
David Horstmanned794832022-10-04 18:12:06 +0100544 ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
545 goto error;
546 }
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000547
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000548 cur = cur->next;
549 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200550
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100551 /*
552 * continue until end of SEQUENCE is reached
553 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100554 if (*p == end) {
555 return 0;
556 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200557
Gilles Peskine449bd832023-01-11 14:50:10 +0100558 cur->next = mbedtls_calloc(1, sizeof(mbedtls_x509_name));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200559
Gilles Peskine449bd832023-01-11 14:50:10 +0100560 if (cur->next == NULL) {
David Horstmanned794832022-10-04 18:12:06 +0100561 ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
562 goto error;
563 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200564
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100565 cur = cur->next;
566 }
David Horstmanned794832022-10-04 18:12:06 +0100567
568error:
David Horstmanned794832022-10-04 18:12:06 +0100569 /* Skip the first element as we did not allocate it */
Gilles Peskine449bd832023-01-11 14:50:10 +0100570 mbedtls_asn1_free_named_data_list_shallow(head->next);
Glenn Straussa4b40412022-06-26 19:32:09 -0400571 head->next = NULL;
David Horstmanned794832022-10-04 18:12:06 +0100572
Gilles Peskine449bd832023-01-11 14:50:10 +0100573 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200574}
575
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400576static int x509_date_is_valid(const mbedtls_x509_time *t)
Janos Follath87c98072017-02-03 12:36:59 +0000577{
David Horstmanncdf52832023-07-05 09:58:03 +0100578 unsigned int month_days;
579 unsigned int year;
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400580 switch (t->mon) {
581 case 1: case 3: case 5: case 7: case 8: case 10: case 12:
David Horstmanncdf52832023-07-05 09:58:03 +0100582 month_days = 31;
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400583 break;
584 case 4: case 6: case 9: case 11:
David Horstmanncdf52832023-07-05 09:58:03 +0100585 month_days = 30;
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400586 break;
587 case 2:
David Horstmanncdf52832023-07-05 09:58:03 +0100588 year = (unsigned int) t->year;
589 month_days = ((year & 3) || (!(year % 100)
590 && (year % 400)))
591 ? 28 : 29;
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400592 break;
593 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100594 return MBEDTLS_ERR_X509_INVALID_DATE;
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400595 }
Janos Follath87c98072017-02-03 12:36:59 +0000596
David Horstmannb1d27bc2023-07-05 10:00:31 +0100597 if ((unsigned int) (t->day - 1) >= month_days || /* (1 - days in month) */
David Horstmann3ae1c4c2023-07-05 11:15:08 +0100598 /* (unsigned int) (t->mon - 1) >= 12 || */ /* (1 - 12) checked above */
David Horstmannb1d27bc2023-07-05 10:00:31 +0100599 (unsigned int) t->year > 9999 || /* (0 - 9999) */
600 (unsigned int) t->hour > 23 || /* (0 - 23) */
601 (unsigned int) t->min > 59 || /* (0 - 59) */
602 (unsigned int) t->sec > 59) { /* (0 - 59) */
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400603 return MBEDTLS_ERR_X509_INVALID_DATE;
Rich Evans7d5a55a2015-02-13 11:48:02 +0000604 }
Janos Follath87c98072017-02-03 12:36:59 +0000605
Gilles Peskine449bd832023-01-11 14:50:10 +0100606 return 0;
Rich Evans7d5a55a2015-02-13 11:48:02 +0000607}
608
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400609static int x509_parse2_int(const unsigned char *p)
Andres AG4b76aec2016-09-23 13:16:02 +0100610{
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400611 uint32_t d1 = p[0] - '0';
612 uint32_t d2 = p[1] - '0';
613 return (d1 < 10 && d2 < 10) ? (int) (d1 * 10 + d2) : -1;
Andres AG4b76aec2016-09-23 13:16:02 +0100614}
615
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200616/*
Janos Follath87c98072017-02-03 12:36:59 +0000617 * Parse an ASN1_UTC_TIME (yearlen=2) or ASN1_GENERALIZED_TIME (yearlen=4)
618 * field.
619 */
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400620static int x509_parse_time(const unsigned char *p, mbedtls_x509_time *tm,
621 size_t yearlen)
Janos Follath87c98072017-02-03 12:36:59 +0000622{
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400623 int x;
Janos Follath87c98072017-02-03 12:36:59 +0000624
625 /*
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400626 * Parse year, month, day, hour, minute, second
Janos Follath87c98072017-02-03 12:36:59 +0000627 */
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400628 tm->year = x509_parse2_int(p);
629 if (tm->year < 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100630 return MBEDTLS_ERR_X509_INVALID_DATE;
631 }
Janos Follath87c98072017-02-03 12:36:59 +0000632
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400633 if (4 == yearlen) {
634 x = tm->year * 100;
635 p += 2;
636 tm->year = x509_parse2_int(p);
637 if (tm->year < 0) {
638 return MBEDTLS_ERR_X509_INVALID_DATE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100639 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100640 } else {
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400641 x = (tm->year < 50) ? 2000 : 1900;
Janos Follath87c98072017-02-03 12:36:59 +0000642 }
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400643 tm->year += x;
Janos Follath87c98072017-02-03 12:36:59 +0000644
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400645 tm->mon = x509_parse2_int(p + 2);
646 tm->day = x509_parse2_int(p + 4);
647 tm->hour = x509_parse2_int(p + 6);
648 tm->min = x509_parse2_int(p + 8);
649 tm->sec = x509_parse2_int(p + 10);
Janos Follath87c98072017-02-03 12:36:59 +0000650
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400651 return x509_date_is_valid(tm);
Janos Follath87c98072017-02-03 12:36:59 +0000652}
653
654/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200655 * Time ::= CHOICE {
656 * utcTime UTCTime,
657 * generalTime GeneralizedTime }
658 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100659int mbedtls_x509_get_time(unsigned char **p, const unsigned char *end,
660 mbedtls_x509_time *tm)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200661{
Janos Follath865b3eb2019-12-16 11:46:15 +0000662 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Janos Follath87c98072017-02-03 12:36:59 +0000663 size_t len, year_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200664 unsigned char tag;
665
Gilles Peskine449bd832023-01-11 14:50:10 +0100666 if ((end - *p) < 1) {
667 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE,
668 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
669 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200670
671 tag = **p;
672
Gilles Peskine449bd832023-01-11 14:50:10 +0100673 if (tag == MBEDTLS_ASN1_UTC_TIME) {
Janos Follath87c98072017-02-03 12:36:59 +0000674 year_len = 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100675 } else if (tag == MBEDTLS_ASN1_GENERALIZED_TIME) {
Janos Follath87c98072017-02-03 12:36:59 +0000676 year_len = 4;
Gilles Peskine449bd832023-01-11 14:50:10 +0100677 } else {
678 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE,
679 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
680 }
Janos Follath87c98072017-02-03 12:36:59 +0000681
682 (*p)++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100683 ret = mbedtls_asn1_get_len(p, end, &len);
Janos Follath87c98072017-02-03 12:36:59 +0000684
Gilles Peskine449bd832023-01-11 14:50:10 +0100685 if (ret != 0) {
686 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE, ret);
687 }
Janos Follath87c98072017-02-03 12:36:59 +0000688
Glenn Strauss06c31fc2022-06-30 13:07:55 -0400689 /* len is 12 or 14 depending on year_len, plus optional trailing 'Z' */
690 if (len != year_len + 10 &&
691 !(len == year_len + 11 && (*p)[(len - 1)] == 'Z')) {
692 return MBEDTLS_ERR_X509_INVALID_DATE;
693 }
694
695 (*p) += len;
696 return x509_parse_time(*p - len, tm, year_len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200697}
698
Gilles Peskine449bd832023-01-11 14:50:10 +0100699int mbedtls_x509_get_sig(unsigned char **p, const unsigned char *end, mbedtls_x509_buf *sig)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200700{
Janos Follath865b3eb2019-12-16 11:46:15 +0000701 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200702 size_t len;
Andres AG4bdbe092016-09-19 16:58:45 +0100703 int tag_type;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200704
Gilles Peskine449bd832023-01-11 14:50:10 +0100705 if ((end - *p) < 1) {
706 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_SIGNATURE,
707 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
708 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200709
Andres AG4bdbe092016-09-19 16:58:45 +0100710 tag_type = **p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200711
Gilles Peskine449bd832023-01-11 14:50:10 +0100712 if ((ret = mbedtls_asn1_get_bitstring_null(p, end, &len)) != 0) {
713 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_SIGNATURE, ret);
714 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200715
Andres AG4bdbe092016-09-19 16:58:45 +0100716 sig->tag = tag_type;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200717 sig->len = len;
718 sig->p = *p;
719
720 *p += len;
721
Gilles Peskine449bd832023-01-11 14:50:10 +0100722 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200723}
724
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100725/*
726 * Get signature algorithm from alg OID and optional parameters
727 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100728int mbedtls_x509_get_sig_alg(const mbedtls_x509_buf *sig_oid, const mbedtls_x509_buf *sig_params,
729 mbedtls_md_type_t *md_alg, mbedtls_pk_type_t *pk_alg,
730 void **sig_opts)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200731{
Janos Follath865b3eb2019-12-16 11:46:15 +0000732 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200733
Gilles Peskine449bd832023-01-11 14:50:10 +0100734 if (*sig_opts != NULL) {
735 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
736 }
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200737
Gilles Peskine449bd832023-01-11 14:50:10 +0100738 if ((ret = mbedtls_oid_get_sig_alg(sig_oid, md_alg, pk_alg)) != 0) {
739 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG, ret);
740 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200742#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100743 if (*pk_alg == MBEDTLS_PK_RSASSA_PSS) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200744 mbedtls_pk_rsassa_pss_options *pss_opts;
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100745
Gilles Peskine449bd832023-01-11 14:50:10 +0100746 pss_opts = mbedtls_calloc(1, sizeof(mbedtls_pk_rsassa_pss_options));
747 if (pss_opts == NULL) {
748 return MBEDTLS_ERR_X509_ALLOC_FAILED;
749 }
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200750
Gilles Peskine449bd832023-01-11 14:50:10 +0100751 ret = mbedtls_x509_get_rsassa_pss_params(sig_params,
752 md_alg,
753 &pss_opts->mgf1_hash_id,
754 &pss_opts->expected_salt_len);
755 if (ret != 0) {
756 mbedtls_free(pss_opts);
757 return ret;
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200758 }
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100759
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200760 *sig_opts = (void *) pss_opts;
Gilles Peskine449bd832023-01-11 14:50:10 +0100761 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200762#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100763 {
764 /* Make sure parameters are absent or NULL */
Gilles Peskine449bd832023-01-11 14:50:10 +0100765 if ((sig_params->tag != MBEDTLS_ASN1_NULL && sig_params->tag != 0) ||
766 sig_params->len != 0) {
767 return MBEDTLS_ERR_X509_INVALID_ALG;
768 }
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100769 }
770
Gilles Peskine449bd832023-01-11 14:50:10 +0100771 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200772}
773
774/*
775 * X.509 Extensions (No parsing of extensions, pointer should
Brian J Murray1903fb32016-11-06 04:45:15 -0800776 * be either manually updated or extensions should be parsed!)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200777 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100778int mbedtls_x509_get_ext(unsigned char **p, const unsigned char *end,
779 mbedtls_x509_buf *ext, int tag)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200780{
Janos Follath865b3eb2019-12-16 11:46:15 +0000781 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200782 size_t len;
783
Hanno Becker3cddba82019-02-11 14:33:36 +0000784 /* Extension structure use EXPLICIT tagging. That is, the actual
785 * `Extensions` structure is wrapped by a tag-length pair using
786 * the respective context-specific tag. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100787 ret = mbedtls_asn1_get_tag(p, end, &ext->len,
788 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | tag);
789 if (ret != 0) {
790 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
791 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200792
Hanno Becker6ccfb182019-02-12 11:52:10 +0000793 ext->tag = MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | tag;
794 ext->p = *p;
795 end = *p + ext->len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200796
797 /*
798 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200799 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100800 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
801 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
802 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
803 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200804
Gilles Peskine449bd832023-01-11 14:50:10 +0100805 if (end != *p + len) {
806 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
807 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
808 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200809
Gilles Peskine449bd832023-01-11 14:50:10 +0100810 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200811}
812
Agathiyan Bragadeesh9d2507c2023-07-24 16:35:57 +0100813static char nibble_to_hex_digit(int i)
Agathiyan Bragadeeshef2decb2023-07-21 15:47:47 +0100814{
815 return (i < 10) ? (i | 0x30) : ((i - 9) | 0x40);
816}
817
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200818/*
819 * Store the name in printable form into buf; no more
820 * than size characters will be written
821 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100822int mbedtls_x509_dn_gets(char *buf, size_t size, const mbedtls_x509_name *dn)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200823{
Janos Follath865b3eb2019-12-16 11:46:15 +0000824 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Werner Lewisb33dacd2022-05-20 12:48:46 +0100825 size_t i, j, n;
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000826 unsigned char c, merge = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200827 const mbedtls_x509_name *name;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200828 const char *short_name = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200829 char s[MBEDTLS_X509_MAX_DN_NAME_SIZE], *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200830
Gilles Peskine449bd832023-01-11 14:50:10 +0100831 memset(s, 0, sizeof(s));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200832
833 name = dn;
834 p = buf;
835 n = size;
836
Gilles Peskine449bd832023-01-11 14:50:10 +0100837 while (name != NULL) {
838 if (!name->oid.p) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200839 name = name->next;
840 continue;
841 }
842
Gilles Peskine449bd832023-01-11 14:50:10 +0100843 if (name != dn) {
844 ret = mbedtls_snprintf(p, n, merge ? " + " : ", ");
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200845 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200846 }
847
Gilles Peskine449bd832023-01-11 14:50:10 +0100848 ret = mbedtls_oid_get_attr_short_name(&name->oid, &short_name);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200849
Gilles Peskine449bd832023-01-11 14:50:10 +0100850 if (ret == 0) {
851 ret = mbedtls_snprintf(p, n, "%s=", short_name);
852 } else {
853 ret = mbedtls_snprintf(p, n, "\?\?=");
854 }
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200855 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200856
Gilles Peskine449bd832023-01-11 14:50:10 +0100857 for (i = 0, j = 0; i < name->val.len; i++, j++) {
858 if (j >= sizeof(s) - 1) {
859 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
860 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200861
862 c = name->val.p[i];
Agathiyan Bragadeesh48513b82023-07-20 16:19:05 +0100863 // Special characters requiring escaping, RFC 4514 Section 2.4
864 if (c) {
Agathiyan Bragadeeshef2decb2023-07-21 15:47:47 +0100865 if (strchr(",=+<>;\"\\+", c) ||
866 ((i == 0) && strchr("# ", c)) ||
867 ((i == name->val.len-1) && (c == ' '))) {
Agathiyan Bragadeesh48513b82023-07-20 16:19:05 +0100868 if (j + 1 >= sizeof(s) - 1) {
869 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
870 }
871 s[j++] = '\\';
Gilles Peskine449bd832023-01-11 14:50:10 +0100872 }
Werner Lewisb33dacd2022-05-20 12:48:46 +0100873 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100874 if (c < 32 || c >= 127) {
Agathiyan Bragadeeshef2decb2023-07-21 15:47:47 +0100875 if (j + 3 >= sizeof(s) - 1) {
876 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
877 }
878 s[j++] = '\\';
879 char lowbits = (c & 0x0F);
880 char highbits = c>>4;
Agathiyan Bragadeesh9d2507c2023-07-24 16:35:57 +0100881 s[j++] = nibble_to_hex_digit(highbits);
882 s[j] = nibble_to_hex_digit(lowbits);
Gilles Peskine449bd832023-01-11 14:50:10 +0100883 } else {
884 s[j] = c;
885 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200886 }
Werner Lewisb33dacd2022-05-20 12:48:46 +0100887 s[j] = '\0';
Gilles Peskine449bd832023-01-11 14:50:10 +0100888 ret = mbedtls_snprintf(p, n, "%s", s);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200889 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000890
891 merge = name->next_merged;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200892 name = name->next;
893 }
894
Gilles Peskine449bd832023-01-11 14:50:10 +0100895 return (int) (size - n);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200896}
897
898/*
899 * Store the serial in printable form into buf; no more
900 * than size characters will be written
901 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100902int mbedtls_x509_serial_gets(char *buf, size_t size, const mbedtls_x509_buf *serial)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200903{
Janos Follath865b3eb2019-12-16 11:46:15 +0000904 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200905 size_t i, n, nr;
906 char *p;
907
908 p = buf;
909 n = size;
910
Gilles Peskine449bd832023-01-11 14:50:10 +0100911 nr = (serial->len <= 32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200912 ? serial->len : 28;
913
Gilles Peskine449bd832023-01-11 14:50:10 +0100914 for (i = 0; i < nr; i++) {
915 if (i == 0 && nr > 1 && serial->p[i] == 0x0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200916 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100917 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200918
Gilles Peskine449bd832023-01-11 14:50:10 +0100919 ret = mbedtls_snprintf(p, n, "%02X%s",
920 serial->p[i], (i < nr - 1) ? ":" : "");
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200921 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200922 }
923
Gilles Peskine449bd832023-01-11 14:50:10 +0100924 if (nr != serial->len) {
925 ret = mbedtls_snprintf(p, n, "....");
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200926 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200927 }
928
Gilles Peskine449bd832023-01-11 14:50:10 +0100929 return (int) (size - n);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200930}
931
Hanno Becker612a2f12020-10-09 09:19:39 +0100932#if !defined(MBEDTLS_X509_REMOVE_INFO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200933/*
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200934 * Helper for writing signature algorithms
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100935 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100936int mbedtls_x509_sig_alg_gets(char *buf, size_t size, const mbedtls_x509_buf *sig_oid,
937 mbedtls_pk_type_t pk_alg, mbedtls_md_type_t md_alg,
938 const void *sig_opts)
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100939{
Janos Follath865b3eb2019-12-16 11:46:15 +0000940 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100941 char *p = buf;
942 size_t n = size;
943 const char *desc = NULL;
944
Gilles Peskine449bd832023-01-11 14:50:10 +0100945 ret = mbedtls_oid_get_sig_alg_desc(sig_oid, &desc);
946 if (ret != 0) {
947 ret = mbedtls_snprintf(p, n, "???");
948 } else {
949 ret = mbedtls_snprintf(p, n, "%s", desc);
950 }
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200951 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100952
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200953#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100954 if (pk_alg == MBEDTLS_PK_RSASSA_PSS) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200955 const mbedtls_pk_rsassa_pss_options *pss_opts;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100956
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200957 pss_opts = (const mbedtls_pk_rsassa_pss_options *) sig_opts;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100958
Gilles Peskine449bd832023-01-11 14:50:10 +0100959 const char *name = md_type_to_string(md_alg);
960 const char *mgf_name = md_type_to_string(pss_opts->mgf1_hash_id);
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100961
Gilles Peskine449bd832023-01-11 14:50:10 +0100962 ret = mbedtls_snprintf(p, n, " (%s, MGF1-%s, 0x%02X)",
963 name ? name : "???",
964 mgf_name ? mgf_name : "???",
965 (unsigned int) pss_opts->expected_salt_len);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200966 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100967 }
968#else
969 ((void) pk_alg);
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200970 ((void) md_alg);
971 ((void) sig_opts);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200972#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100973
Gilles Peskine449bd832023-01-11 14:50:10 +0100974 return (int) (size - n);
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100975}
Hanno Becker612a2f12020-10-09 09:19:39 +0100976#endif /* MBEDTLS_X509_REMOVE_INFO */
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100977
978/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200979 * Helper for writing "RSA key size", "EC key size", etc
980 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100981int mbedtls_x509_key_size_helper(char *buf, size_t buf_size, const char *name)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200982{
983 char *p = buf;
Manuel Pégourié-Gonnardfb317c52015-06-18 16:25:56 +0200984 size_t n = buf_size;
Janos Follath865b3eb2019-12-16 11:46:15 +0000985 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200986
Gilles Peskine449bd832023-01-11 14:50:10 +0100987 ret = mbedtls_snprintf(p, n, "%s key size", name);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200988 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200989
Gilles Peskine449bd832023-01-11 14:50:10 +0100990 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200991}
992
Glenn Strauss416dc032022-06-30 00:38:53 -0400993int mbedtls_x509_time_cmp(const mbedtls_x509_time *t1,
994 const mbedtls_x509_time *t2)
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100995{
Glenn Strauss5aef2972022-06-30 04:38:02 -0400996 int x;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200997
Glenn Strauss5aef2972022-06-30 04:38:02 -0400998 x = (((t1->year << 9) | (t1->mon << 5) | (t1->day)) -
999 ((t2->year << 9) | (t2->mon << 5) | (t2->day)));
1000 if (x != 0) {
1001 return x;
Manuel Pégourié-Gonnard57e10d72015-06-22 18:59:21 +02001002 }
Manuel Pégourié-Gonnard864108d2015-05-29 10:11:03 +02001003
Glenn Strauss5aef2972022-06-30 04:38:02 -04001004 x = (((t1->hour << 12) | (t1->min << 6) | (t1->sec)) -
1005 ((t2->hour << 12) | (t2->min << 6) | (t2->sec)));
1006 return x;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001007}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001008
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001009#if defined(MBEDTLS_HAVE_TIME_DATE)
Glenn Strauss61d99302022-06-30 05:25:56 -04001010int mbedtls_x509_time_gmtime(mbedtls_time_t tt, mbedtls_x509_time *now)
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001011{
Glenn Strauss811eeb22022-06-30 05:28:50 -04001012 struct tm tm;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001013
Glenn Strauss811eeb22022-06-30 05:28:50 -04001014 if (mbedtls_platform_gmtime_r(&tt, &tm) == NULL) {
1015 return -1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001016 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001017
Glenn Strauss811eeb22022-06-30 05:28:50 -04001018 now->year = tm.tm_year + 1900;
1019 now->mon = tm.tm_mon + 1;
1020 now->day = tm.tm_mday;
1021 now->hour = tm.tm_hour;
1022 now->min = tm.tm_min;
1023 now->sec = tm.tm_sec;
Gilles Peskine449bd832023-01-11 14:50:10 +01001024 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001025}
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001026
Glenn Strauss61d99302022-06-30 05:25:56 -04001027static int x509_get_current_time(mbedtls_x509_time *now)
1028{
1029 return mbedtls_x509_time_gmtime(mbedtls_time(NULL), now);
1030}
1031
Gilles Peskine449bd832023-01-11 14:50:10 +01001032int mbedtls_x509_time_is_past(const mbedtls_x509_time *to)
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001033{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001034 mbedtls_x509_time now;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001035
Gilles Peskine449bd832023-01-11 14:50:10 +01001036 if (x509_get_current_time(&now) != 0) {
1037 return 1;
1038 }
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001039
Glenn Strauss416dc032022-06-30 00:38:53 -04001040 return mbedtls_x509_time_cmp(to, &now) < 0;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001041}
1042
Gilles Peskine449bd832023-01-11 14:50:10 +01001043int mbedtls_x509_time_is_future(const mbedtls_x509_time *from)
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001044{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001045 mbedtls_x509_time now;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001046
Gilles Peskine449bd832023-01-11 14:50:10 +01001047 if (x509_get_current_time(&now) != 0) {
1048 return 1;
1049 }
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001050
Glenn Strauss416dc032022-06-30 00:38:53 -04001051 return mbedtls_x509_time_cmp(from, &now) > 0;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001052}
1053
Manuel Pégourié-Gonnard60c793b2015-06-18 20:52:58 +02001054#else /* MBEDTLS_HAVE_TIME_DATE */
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001055
Gilles Peskine449bd832023-01-11 14:50:10 +01001056int mbedtls_x509_time_is_past(const mbedtls_x509_time *to)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001057{
1058 ((void) to);
Gilles Peskine449bd832023-01-11 14:50:10 +01001059 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001060}
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001061
Gilles Peskine449bd832023-01-11 14:50:10 +01001062int mbedtls_x509_time_is_future(const mbedtls_x509_time *from)
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001063{
1064 ((void) from);
Gilles Peskine449bd832023-01-11 14:50:10 +01001065 return 0;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001066}
Manuel Pégourié-Gonnard60c793b2015-06-18 20:52:58 +02001067#endif /* MBEDTLS_HAVE_TIME_DATE */
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001068
1069/* Common functions for parsing CRT and CSR. */
1070#if defined(MBEDTLS_X509_CRT_PARSE_C) || defined(MBEDTLS_X509_CSR_PARSE_C)
1071/*
1072 * OtherName ::= SEQUENCE {
1073 * type-id OBJECT IDENTIFIER,
1074 * value [0] EXPLICIT ANY DEFINED BY type-id }
1075 *
1076 * HardwareModuleName ::= SEQUENCE {
1077 * hwType OBJECT IDENTIFIER,
1078 * hwSerialNum OCTET STRING }
1079 *
1080 * NOTE: we currently only parse and use otherName of type HwModuleName,
1081 * as defined in RFC 4108.
1082 */
1083static int x509_get_other_name(const mbedtls_x509_buf *subject_alt_name,
1084 mbedtls_x509_san_other_name *other_name)
1085{
1086 int ret = 0;
1087 size_t len;
1088 unsigned char *p = subject_alt_name->p;
1089 const unsigned char *end = p + subject_alt_name->len;
1090 mbedtls_x509_buf cur_oid;
1091
1092 if ((subject_alt_name->tag &
1093 (MBEDTLS_ASN1_TAG_CLASS_MASK | MBEDTLS_ASN1_TAG_VALUE_MASK)) !=
1094 (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME)) {
1095 /*
1096 * The given subject alternative name is not of type "othername".
1097 */
1098 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1099 }
1100
1101 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1102 MBEDTLS_ASN1_OID)) != 0) {
1103 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1104 }
1105
1106 cur_oid.tag = MBEDTLS_ASN1_OID;
1107 cur_oid.p = p;
1108 cur_oid.len = len;
1109
1110 /*
1111 * Only HwModuleName is currently supported.
1112 */
1113 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ON_HW_MODULE_NAME, &cur_oid) != 0) {
1114 return MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
1115 }
1116
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001117 p += len;
1118 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1119 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC)) !=
1120 0) {
1121 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1122 }
1123
Hanno Beckerdae916b2019-09-13 14:21:13 +01001124 if (end != p + len) {
1125 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1126 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1127 }
1128
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001129 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1130 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1131 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1132 }
1133
Hanno Beckerdae916b2019-09-13 14:21:13 +01001134 if (end != p + len) {
1135 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1136 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1137 }
1138
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001139 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OID)) != 0) {
1140 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1141 }
1142
1143 other_name->value.hardware_module_name.oid.tag = MBEDTLS_ASN1_OID;
1144 other_name->value.hardware_module_name.oid.p = p;
1145 other_name->value.hardware_module_name.oid.len = len;
1146
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001147 p += len;
1148 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1149 MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1150 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1151 }
1152
1153 other_name->value.hardware_module_name.val.tag = MBEDTLS_ASN1_OCTET_STRING;
1154 other_name->value.hardware_module_name.val.p = p;
1155 other_name->value.hardware_module_name.val.len = len;
1156 p += len;
1157 if (p != end) {
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001158 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1159 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1160 }
1161 return 0;
1162}
1163
Przemek Stekiel21903ec2023-02-21 08:32:37 +01001164/* Check mbedtls_x509_get_subject_alt_name for detailed description.
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001165 *
Przemek Stekiel21903ec2023-02-21 08:32:37 +01001166 * In some cases while parsing subject alternative names the sequence tag is optional
1167 * (e.g. CertSerialNumber). This function is designed to handle such case.
Przemek Stekiel725688b2023-04-04 22:49:44 +02001168 */
Przemek Stekiel21903ec2023-02-21 08:32:37 +01001169int mbedtls_x509_get_subject_alt_name_ext(unsigned char **p,
1170 const unsigned char *end,
1171 mbedtls_x509_sequence *subject_alt_name)
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001172{
1173 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel21903ec2023-02-21 08:32:37 +01001174 size_t tag_len;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001175 mbedtls_asn1_sequence *cur = subject_alt_name;
1176
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001177 while (*p < end) {
Andrzej Kurek154a6052023-04-30 14:11:49 -04001178 mbedtls_x509_subject_alternative_name tmp_san_name;
Hanno Beckerae8f8c42019-09-13 12:24:56 +01001179 mbedtls_x509_buf tmp_san_buf;
Andrzej Kurek154a6052023-04-30 14:11:49 -04001180 memset(&tmp_san_name, 0, sizeof(tmp_san_name));
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001181
Hanno Beckerae8f8c42019-09-13 12:24:56 +01001182 tmp_san_buf.tag = **p;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001183 (*p)++;
Hanno Beckerae8f8c42019-09-13 12:24:56 +01001184
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001185 if ((ret = mbedtls_asn1_get_len(p, end, &tag_len)) != 0) {
1186 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1187 }
1188
Hanno Beckerae8f8c42019-09-13 12:24:56 +01001189 tmp_san_buf.p = *p;
1190 tmp_san_buf.len = tag_len;
1191
1192 if ((tmp_san_buf.tag & MBEDTLS_ASN1_TAG_CLASS_MASK) !=
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001193 MBEDTLS_ASN1_CONTEXT_SPECIFIC) {
1194 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1195 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
1196 }
1197
1198 /*
Andrzej Kurek154a6052023-04-30 14:11:49 -04001199 * Check that the SAN is structured correctly by parsing it.
1200 * The SAN structure is discarded afterwards.
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001201 */
Andrzej Kurek154a6052023-04-30 14:11:49 -04001202 ret = mbedtls_x509_parse_subject_alt_name(&tmp_san_buf, &tmp_san_name);
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001203 /*
1204 * In case the extension is malformed, return an error,
1205 * and clear the allocated sequences.
1206 */
1207 if (ret != 0 && ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE) {
1208 mbedtls_asn1_sequence_free(subject_alt_name->next);
1209 subject_alt_name->next = NULL;
1210 return ret;
1211 }
1212
Andrzej Kurek154a6052023-04-30 14:11:49 -04001213 mbedtls_x509_free_subject_alt_name(&tmp_san_name);
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001214 /* Allocate and assign next pointer */
1215 if (cur->buf.p != NULL) {
1216 if (cur->next != NULL) {
1217 return MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
1218 }
1219
1220 cur->next = mbedtls_calloc(1, sizeof(mbedtls_asn1_sequence));
1221
1222 if (cur->next == NULL) {
1223 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1224 MBEDTLS_ERR_ASN1_ALLOC_FAILED);
1225 }
1226
1227 cur = cur->next;
1228 }
1229
Hanno Beckerae8f8c42019-09-13 12:24:56 +01001230 cur->buf = tmp_san_buf;
1231 *p += tmp_san_buf.len;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001232 }
1233
1234 /* Set final sequence entry's next pointer to NULL */
1235 cur->next = NULL;
1236
1237 if (*p != end) {
1238 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1239 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1240 }
1241
1242 return 0;
1243}
1244
Przemek Stekiel21903ec2023-02-21 08:32:37 +01001245/*
1246 * SubjectAltName ::= GeneralNames
1247 *
1248 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
1249 *
1250 * GeneralName ::= CHOICE {
1251 * otherName [0] OtherName,
1252 * rfc822Name [1] IA5String,
1253 * dNSName [2] IA5String,
1254 * x400Address [3] ORAddress,
1255 * directoryName [4] Name,
1256 * ediPartyName [5] EDIPartyName,
1257 * uniformResourceIdentifier [6] IA5String,
1258 * iPAddress [7] OCTET STRING,
1259 * registeredID [8] OBJECT IDENTIFIER }
1260 *
1261 * OtherName ::= SEQUENCE {
1262 * type-id OBJECT IDENTIFIER,
1263 * value [0] EXPLICIT ANY DEFINED BY type-id }
1264 *
1265 * EDIPartyName ::= SEQUENCE {
1266 * nameAssigner [0] DirectoryString OPTIONAL,
1267 * partyName [1] DirectoryString }
1268 *
1269 * We list all types, but use the following GeneralName types from RFC 5280:
1270 * "dnsName", "uniformResourceIdentifier" and "hardware_module_name"
1271 * of type "otherName", as defined in RFC 4108.
1272 */
1273int mbedtls_x509_get_subject_alt_name(unsigned char **p,
1274 const unsigned char *end,
1275 mbedtls_x509_sequence *subject_alt_name)
1276{
1277 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1278 size_t len;
1279
1280 /* Get main sequence tag */
1281 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
1282 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1283 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1284 }
1285
1286 if (*p + len != end) {
1287 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1288 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1289 }
1290
1291 return mbedtls_x509_get_subject_alt_name_ext(p, end, subject_alt_name);
1292}
1293
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001294int mbedtls_x509_get_ns_cert_type(unsigned char **p,
1295 const unsigned char *end,
1296 unsigned char *ns_cert_type)
1297{
1298 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1299 mbedtls_x509_bitstring bs = { 0, 0, NULL };
1300
1301 if ((ret = mbedtls_asn1_get_bitstring(p, end, &bs)) != 0) {
1302 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1303 }
1304
Przemek Stekiel32e20912023-01-25 14:26:15 +01001305 /* A bitstring with no flags set is still technically valid, as it will mean
1306 that the certificate has no designated purpose at the time of creation. */
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001307 if (bs.len == 0) {
1308 *ns_cert_type = 0;
1309 return 0;
1310 }
1311
1312 if (bs.len != 1) {
1313 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1314 MBEDTLS_ERR_ASN1_INVALID_LENGTH);
1315 }
1316
1317 /* Get actual bitstring */
1318 *ns_cert_type = *bs.p;
1319 return 0;
1320}
1321
1322int mbedtls_x509_get_key_usage(unsigned char **p,
1323 const unsigned char *end,
1324 unsigned int *key_usage)
1325{
1326 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1327 size_t i;
1328 mbedtls_x509_bitstring bs = { 0, 0, NULL };
1329
1330 if ((ret = mbedtls_asn1_get_bitstring(p, end, &bs)) != 0) {
1331 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1332 }
1333
Przemek Stekiel32e20912023-01-25 14:26:15 +01001334 /* A bitstring with no flags set is still technically valid, as it will mean
1335 that the certificate has no designated purpose at the time of creation. */
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001336 if (bs.len == 0) {
1337 *key_usage = 0;
1338 return 0;
1339 }
1340
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001341 /* Get actual bitstring */
1342 *key_usage = 0;
1343 for (i = 0; i < bs.len && i < sizeof(unsigned int); i++) {
1344 *key_usage |= (unsigned int) bs.p[i] << (8*i);
1345 }
1346
1347 return 0;
1348}
1349
1350int mbedtls_x509_parse_subject_alt_name(const mbedtls_x509_buf *san_buf,
1351 mbedtls_x509_subject_alternative_name *san)
1352{
1353 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1354 switch (san_buf->tag &
1355 (MBEDTLS_ASN1_TAG_CLASS_MASK |
1356 MBEDTLS_ASN1_TAG_VALUE_MASK)) {
1357 /*
1358 * otherName
1359 */
1360 case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME):
1361 {
1362 mbedtls_x509_san_other_name other_name;
1363
1364 ret = x509_get_other_name(san_buf, &other_name);
1365 if (ret != 0) {
1366 return ret;
1367 }
1368
1369 memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name));
1370 san->type = MBEDTLS_X509_SAN_OTHER_NAME;
1371 memcpy(&san->san.other_name,
1372 &other_name, sizeof(other_name));
1373
1374 }
1375 break;
Andrzej Kurek7a05fab2023-02-13 10:03:07 -05001376 /*
1377 * uniformResourceIdentifier
1378 */
1379 case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER):
1380 {
1381 memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name));
1382 san->type = MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001383
Andrzej Kurek7a05fab2023-02-13 10:03:07 -05001384 memcpy(&san->san.unstructured_name,
1385 san_buf, sizeof(*san_buf));
1386
1387 }
1388 break;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001389 /*
1390 * dNSName
1391 */
1392 case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_DNS_NAME):
1393 {
1394 memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name));
1395 san->type = MBEDTLS_X509_SAN_DNS_NAME;
1396
1397 memcpy(&san->san.unstructured_name,
1398 san_buf, sizeof(*san_buf));
Przemek Stekielecee12f2023-02-09 14:43:49 +01001399 }
1400 break;
Przemek Stekiel0ab5b932023-05-29 16:30:50 +02001401 /*
1402 * IP address
1403 */
1404 case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_IP_ADDRESS):
1405 {
1406 memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name));
1407 san->type = MBEDTLS_X509_SAN_IP_ADDRESS;
Przemek Stekiel01cb6eb2023-06-05 16:38:13 +02001408 // Only IPv6 (16 bytes) and IPv4 (4 bytes) types are supported
1409 if (san_buf->len == 4 || san_buf->len == 16) {
1410 memcpy(&san->san.unstructured_name,
1411 san_buf, sizeof(*san_buf));
1412 } else {
1413 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1414 }
Przemek Stekiel0ab5b932023-05-29 16:30:50 +02001415 }
1416 break;
Przemek Stekielecee12f2023-02-09 14:43:49 +01001417 /*
Andrzej Kurek154a6052023-04-30 14:11:49 -04001418 * rfc822Name
Przemek Stekielecee12f2023-02-09 14:43:49 +01001419 */
1420 case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_RFC822_NAME):
1421 {
1422 memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name));
1423 san->type = MBEDTLS_X509_SAN_RFC822_NAME;
1424 memcpy(&san->san.unstructured_name, san_buf, sizeof(*san_buf));
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001425 }
1426 break;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001427 /*
Andrzej Kureke12b01d2023-01-10 06:47:38 -05001428 * directoryName
1429 */
1430 case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_DIRECTORY_NAME):
1431 {
1432 size_t name_len;
1433 unsigned char *p = san_buf->p;
1434 memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name));
1435 san->type = MBEDTLS_X509_SAN_DIRECTORY_NAME;
1436
1437 ret = mbedtls_asn1_get_tag(&p, p + san_buf->len, &name_len,
1438 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE);
1439
1440 if (ret != 0) {
Andrzej Kurekbf8ccd82023-02-13 07:13:30 -05001441 return ret;
Andrzej Kureke12b01d2023-01-10 06:47:38 -05001442 }
1443
1444 if ((ret = mbedtls_x509_get_name(&p, p + name_len,
1445 &san->san.directory_name)) != 0) {
Andrzej Kurekbf8ccd82023-02-13 07:13:30 -05001446 return ret;
Andrzej Kureke12b01d2023-01-10 06:47:38 -05001447 }
1448 }
1449 break;
1450 /*
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001451 * Type not supported
1452 */
1453 default:
1454 return MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
1455 }
1456 return 0;
1457}
1458
Andrzej Kurekd40c2b62023-02-13 07:01:59 -05001459void mbedtls_x509_free_subject_alt_name(mbedtls_x509_subject_alternative_name *san)
1460{
1461 if (san->type == MBEDTLS_X509_SAN_DIRECTORY_NAME) {
1462 mbedtls_asn1_free_named_data_list_shallow(san->san.directory_name.next);
1463 }
1464}
1465
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001466#if !defined(MBEDTLS_X509_REMOVE_INFO)
1467int mbedtls_x509_info_subject_alt_name(char **buf, size_t *size,
1468 const mbedtls_x509_sequence
1469 *subject_alt_name,
1470 const char *prefix)
1471{
1472 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1473 size_t i;
1474 size_t n = *size;
1475 char *p = *buf;
1476 const mbedtls_x509_sequence *cur = subject_alt_name;
1477 mbedtls_x509_subject_alternative_name san;
1478 int parse_ret;
1479
1480 while (cur != NULL) {
1481 memset(&san, 0, sizeof(san));
1482 parse_ret = mbedtls_x509_parse_subject_alt_name(&cur->buf, &san);
1483 if (parse_ret != 0) {
1484 if (parse_ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE) {
1485 ret = mbedtls_snprintf(p, n, "\n%s <unsupported>", prefix);
1486 MBEDTLS_X509_SAFE_SNPRINTF;
1487 } else {
1488 ret = mbedtls_snprintf(p, n, "\n%s <malformed>", prefix);
1489 MBEDTLS_X509_SAFE_SNPRINTF;
1490 }
1491 cur = cur->next;
1492 continue;
1493 }
1494
1495 switch (san.type) {
1496 /*
1497 * otherName
1498 */
1499 case MBEDTLS_X509_SAN_OTHER_NAME:
1500 {
1501 mbedtls_x509_san_other_name *other_name = &san.san.other_name;
1502
1503 ret = mbedtls_snprintf(p, n, "\n%s otherName :", prefix);
1504 MBEDTLS_X509_SAFE_SNPRINTF;
1505
1506 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ON_HW_MODULE_NAME,
1507 &other_name->value.hardware_module_name.oid) != 0) {
1508 ret = mbedtls_snprintf(p, n, "\n%s hardware module name :", prefix);
1509 MBEDTLS_X509_SAFE_SNPRINTF;
1510 ret =
1511 mbedtls_snprintf(p, n, "\n%s hardware type : ", prefix);
1512 MBEDTLS_X509_SAFE_SNPRINTF;
1513
1514 ret = mbedtls_oid_get_numeric_string(p,
1515 n,
1516 &other_name->value.hardware_module_name.oid);
1517 MBEDTLS_X509_SAFE_SNPRINTF;
1518
1519 ret =
1520 mbedtls_snprintf(p, n, "\n%s hardware serial number : ", prefix);
1521 MBEDTLS_X509_SAFE_SNPRINTF;
1522
1523 for (i = 0; i < other_name->value.hardware_module_name.val.len; i++) {
1524 ret = mbedtls_snprintf(p,
1525 n,
1526 "%02X",
1527 other_name->value.hardware_module_name.val.p[i]);
1528 MBEDTLS_X509_SAFE_SNPRINTF;
1529 }
1530 }/* MBEDTLS_OID_ON_HW_MODULE_NAME */
1531 }
1532 break;
Andrzej Kurek7a05fab2023-02-13 10:03:07 -05001533 /*
1534 * uniformResourceIdentifier
1535 */
1536 case MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER:
1537 {
1538 ret = mbedtls_snprintf(p, n, "\n%s uniformResourceIdentifier : ", prefix);
1539 MBEDTLS_X509_SAFE_SNPRINTF;
1540 if (san.san.unstructured_name.len >= n) {
Przemek Stekiel01cb6eb2023-06-05 16:38:13 +02001541 if (n > 0) {
1542 *p = '\0';
1543 }
Andrzej Kurek7a05fab2023-02-13 10:03:07 -05001544 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1545 }
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001546
Andrzej Kurek7a05fab2023-02-13 10:03:07 -05001547 memcpy(p, san.san.unstructured_name.p, san.san.unstructured_name.len);
1548 p += san.san.unstructured_name.len;
1549 n -= san.san.unstructured_name.len;
1550 }
1551 break;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001552 /*
1553 * dNSName
Przemek Stekiel5b9e4162023-02-15 12:56:37 +01001554 * RFC822 Name
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001555 */
1556 case MBEDTLS_X509_SAN_DNS_NAME:
Przemek Stekiel5b9e4162023-02-15 12:56:37 +01001557 case MBEDTLS_X509_SAN_RFC822_NAME:
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001558 {
Przemek Stekiel82d250d2023-02-15 15:00:50 +01001559 const char *dns_name = "dNSName";
1560 const char *rfc822_name = "rfc822Name";
Przemek Stekiel5b9e4162023-02-15 12:56:37 +01001561
Przemek Stekiel82d250d2023-02-15 15:00:50 +01001562 ret = mbedtls_snprintf(p, n,
1563 "\n%s %s : ",
1564 prefix,
Przemek Stekiel093c97d2023-06-02 10:11:32 +02001565 san.type ==
1566 MBEDTLS_X509_SAN_DNS_NAME ? dns_name : rfc822_name);
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001567 MBEDTLS_X509_SAFE_SNPRINTF;
1568 if (san.san.unstructured_name.len >= n) {
Przemek Stekiel01cb6eb2023-06-05 16:38:13 +02001569 if (n > 0) {
1570 *p = '\0';
1571 }
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001572 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1573 }
1574
Przemek Stekiel093c97d2023-06-02 10:11:32 +02001575 memcpy(p, san.san.unstructured_name.p, san.san.unstructured_name.len);
1576 p += san.san.unstructured_name.len;
1577 n -= san.san.unstructured_name.len;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001578 }
1579 break;
Przemek Stekiel093c97d2023-06-02 10:11:32 +02001580 /*
1581 * iPAddress
1582 */
1583 case MBEDTLS_X509_SAN_IP_ADDRESS:
1584 {
1585 ret = mbedtls_snprintf(p, n, "\n%s %s : ",
1586 prefix, "iPAddress");
1587 MBEDTLS_X509_SAFE_SNPRINTF;
1588 if (san.san.unstructured_name.len >= n) {
Przemek Stekiel01cb6eb2023-06-05 16:38:13 +02001589 if (n > 0) {
1590 *p = '\0';
1591 }
Przemek Stekiel093c97d2023-06-02 10:11:32 +02001592 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1593 }
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001594
Przemek Stekiel093c97d2023-06-02 10:11:32 +02001595 unsigned char *ip = san.san.unstructured_name.p;
1596 // Only IPv6 (16 bytes) and IPv4 (4 bytes) types are supported
1597 if (san.san.unstructured_name.len == 4) {
Przemek Stekiel4d3fc212023-06-06 11:40:32 +02001598 ret = mbedtls_snprintf(p, n, "%u.%u.%u.%u", ip[0], ip[1], ip[2], ip[3]);
1599 MBEDTLS_X509_SAFE_SNPRINTF;
Przemek Stekiel093c97d2023-06-02 10:11:32 +02001600 } else if (san.san.unstructured_name.len == 16) {
Przemek Stekiel4d3fc212023-06-06 11:40:32 +02001601 ret = mbedtls_snprintf(p, n,
Przemek Stekiel01cb6eb2023-06-05 16:38:13 +02001602 "%X%X:%X%X:%X%X:%X%X:%X%X:%X%X:%X%X:%X%X",
1603 ip[0], ip[1], ip[2], ip[3], ip[4], ip[5], ip[6],
Przemek Stekiel4d3fc212023-06-06 11:40:32 +02001604 ip[7], ip[8], ip[9], ip[10], ip[11], ip[12], ip[13],
Przemek Stekiel01cb6eb2023-06-05 16:38:13 +02001605 ip[14], ip[15]);
Przemek Stekiel4d3fc212023-06-06 11:40:32 +02001606 MBEDTLS_X509_SAFE_SNPRINTF;
Przemek Stekiel093c97d2023-06-02 10:11:32 +02001607 } else {
Przemek Stekiel01cb6eb2023-06-05 16:38:13 +02001608 if (n > 0) {
1609 *p = '\0';
1610 }
Przemek Stekiel093c97d2023-06-02 10:11:32 +02001611 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1612 }
Przemek Stekiel093c97d2023-06-02 10:11:32 +02001613 }
1614 break;
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001615 /*
Andrzej Kureke12b01d2023-01-10 06:47:38 -05001616 * directoryName
1617 */
1618 case MBEDTLS_X509_SAN_DIRECTORY_NAME:
1619 {
1620 ret = mbedtls_snprintf(p, n, "\n%s directoryName : ", prefix);
Andrzej Kurek5f0c6e82023-02-27 16:03:41 -05001621 if (ret < 0 || (size_t) ret >= n) {
1622 mbedtls_x509_free_subject_alt_name(&san);
1623 }
1624
Andrzej Kureke12b01d2023-01-10 06:47:38 -05001625 MBEDTLS_X509_SAFE_SNPRINTF;
1626 ret = mbedtls_x509_dn_gets(p, n, &san.san.directory_name);
Andrzej Kurekd40c2b62023-02-13 07:01:59 -05001627
Andrzej Kureke12b01d2023-01-10 06:47:38 -05001628 if (ret < 0) {
Andrzej Kurek5f0c6e82023-02-27 16:03:41 -05001629 mbedtls_x509_free_subject_alt_name(&san);
Przemek Stekiel01cb6eb2023-06-05 16:38:13 +02001630 if (n > 0) {
1631 *p = '\0';
1632 }
Andrzej Kureke12b01d2023-01-10 06:47:38 -05001633 return ret;
1634 }
1635
1636 p += ret;
1637 n -= ret;
1638 }
1639 break;
1640 /*
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001641 * Type not supported, skip item.
1642 */
1643 default:
1644 ret = mbedtls_snprintf(p, n, "\n%s <unsupported>", prefix);
1645 MBEDTLS_X509_SAFE_SNPRINTF;
1646 break;
1647 }
1648
Andrzej Kurekd40c2b62023-02-13 07:01:59 -05001649 /* So far memory is freed only in the case of directoryName
Andrzej Kurek5f0c6e82023-02-27 16:03:41 -05001650 * parsing succeeding, as mbedtls_x509_get_name allocates memory. */
Andrzej Kurekd40c2b62023-02-13 07:01:59 -05001651 mbedtls_x509_free_subject_alt_name(&san);
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001652 cur = cur->next;
1653 }
1654
1655 *p = '\0';
1656
1657 *size = n;
1658 *buf = p;
1659
1660 return 0;
1661}
1662
Demi Marie Obenour690b8c92022-12-04 04:24:22 -05001663#define PRINT_ITEM(i) \
1664 do { \
1665 ret = mbedtls_snprintf(p, n, "%s" i, sep); \
1666 MBEDTLS_X509_SAFE_SNPRINTF; \
1667 sep = ", "; \
1668 } while (0)
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001669
Demi Marie Obenour690b8c92022-12-04 04:24:22 -05001670#define CERT_TYPE(type, name) \
1671 do { \
1672 if (ns_cert_type & (type)) { \
1673 PRINT_ITEM(name); \
1674 } \
1675 } while (0)
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001676
1677int mbedtls_x509_info_cert_type(char **buf, size_t *size,
1678 unsigned char ns_cert_type)
1679{
1680 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1681 size_t n = *size;
1682 char *p = *buf;
1683 const char *sep = "";
1684
1685 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT, "SSL Client");
1686 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER, "SSL Server");
1687 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_EMAIL, "Email");
1688 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing");
1689 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_RESERVED, "Reserved");
1690 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_SSL_CA, "SSL CA");
1691 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA, "Email CA");
1692 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA");
1693
1694 *size = n;
1695 *buf = p;
1696
1697 return 0;
1698}
1699
Demi Marie Obenour690b8c92022-12-04 04:24:22 -05001700#define KEY_USAGE(code, name) \
1701 do { \
1702 if ((key_usage) & (code)) { \
1703 PRINT_ITEM(name); \
1704 } \
1705 } while (0)
Przemek Stekielcf6ff0f2023-01-17 10:26:40 +01001706
1707int mbedtls_x509_info_key_usage(char **buf, size_t *size,
1708 unsigned int key_usage)
1709{
1710 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1711 size_t n = *size;
1712 char *p = *buf;
1713 const char *sep = "";
1714
1715 KEY_USAGE(MBEDTLS_X509_KU_DIGITAL_SIGNATURE, "Digital Signature");
1716 KEY_USAGE(MBEDTLS_X509_KU_NON_REPUDIATION, "Non Repudiation");
1717 KEY_USAGE(MBEDTLS_X509_KU_KEY_ENCIPHERMENT, "Key Encipherment");
1718 KEY_USAGE(MBEDTLS_X509_KU_DATA_ENCIPHERMENT, "Data Encipherment");
1719 KEY_USAGE(MBEDTLS_X509_KU_KEY_AGREEMENT, "Key Agreement");
1720 KEY_USAGE(MBEDTLS_X509_KU_KEY_CERT_SIGN, "Key Cert Sign");
1721 KEY_USAGE(MBEDTLS_X509_KU_CRL_SIGN, "CRL Sign");
1722 KEY_USAGE(MBEDTLS_X509_KU_ENCIPHER_ONLY, "Encipher Only");
1723 KEY_USAGE(MBEDTLS_X509_KU_DECIPHER_ONLY, "Decipher Only");
1724
1725 *size = n;
1726 *buf = p;
1727
1728 return 0;
1729}
1730#endif /* MBEDTLS_X509_REMOVE_INFO */
1731#endif /* MBEDTLS_X509_CRT_PARSE_C || MBEDTLS_X509_CSR_PARSE_C */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001732#endif /* MBEDTLS_X509_USE_C */