Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1 | /* |
Manuel Pégourié-Gonnard | 1c082f3 | 2014-06-12 22:34:55 +0200 | [diff] [blame] | 2 | * X.509 common functions for parsing and verification |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 3 | * |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 4 | * Copyright The Mbed TLS Contributors |
Dave Rodgman | 7ff7965 | 2023-11-03 12:04:52 +0000 | [diff] [blame] | 5 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 6 | */ |
| 7 | /* |
| 8 | * The ITU-T X.509 standard defines a certificate format for PKI. |
| 9 | * |
Manuel Pégourié-Gonnard | 1c082f3 | 2014-06-12 22:34:55 +0200 | [diff] [blame] | 10 | * http://www.ietf.org/rfc/rfc5280.txt (Certificates and CRLs) |
| 11 | * http://www.ietf.org/rfc/rfc3279.txt (Alg IDs for CRLs) |
| 12 | * http://www.ietf.org/rfc/rfc2986.txt (CSRs, aka PKCS#10) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 13 | * |
| 14 | * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf |
| 15 | * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf |
| 16 | */ |
| 17 | |
Gilles Peskine | db09ef6 | 2020-06-03 01:43:33 +0200 | [diff] [blame] | 18 | #include "common.h" |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 19 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 20 | #if defined(MBEDTLS_X509_USE_C) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 21 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 22 | #include "mbedtls/x509.h" |
| 23 | #include "mbedtls/asn1.h" |
Janos Follath | 73c616b | 2019-12-18 15:07:04 +0000 | [diff] [blame] | 24 | #include "mbedtls/error.h" |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 25 | #include "mbedtls/oid.h" |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 26 | |
Rich Evans | 36796df | 2015-02-12 18:27:14 +0000 | [diff] [blame] | 27 | #include <stdio.h> |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 28 | #include <string.h> |
| 29 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 30 | #if defined(MBEDTLS_PEM_PARSE_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 31 | #include "mbedtls/pem.h" |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 32 | #endif |
| 33 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 34 | #include "mbedtls/platform.h" |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 35 | |
Simon Butcher | b5b6af2 | 2016-07-13 14:46:18 +0100 | [diff] [blame] | 36 | #if defined(MBEDTLS_HAVE_TIME) |
| 37 | #include "mbedtls/platform_time.h" |
| 38 | #endif |
Nicholas Wilson | 512b4ee | 2017-12-05 12:07:33 +0000 | [diff] [blame] | 39 | #if defined(MBEDTLS_HAVE_TIME_DATE) |
Andres Amaya Garcia | 1abb368 | 2018-08-16 21:42:09 +0100 | [diff] [blame] | 40 | #include "mbedtls/platform_util.h" |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 41 | #include <time.h> |
| 42 | #endif |
| 43 | |
Demi Marie Obenour | 0e20741 | 2022-12-04 04:24:22 -0500 | [diff] [blame] | 44 | #define CHECK(code) \ |
| 45 | do { \ |
| 46 | if ((ret = (code)) != 0) { \ |
| 47 | return ret; \ |
| 48 | } \ |
| 49 | } while (0) |
| 50 | |
Hanno Becker | 1eeca41 | 2018-10-15 12:01:35 +0100 | [diff] [blame] | 51 | #define CHECK_RANGE(min, max, val) \ |
Demi Marie Obenour | 0e20741 | 2022-12-04 04:24:22 -0500 | [diff] [blame] | 52 | do { \ |
| 53 | if ((val) < (min) || (val) > (max)) { \ |
| 54 | return ret; \ |
Hanno Becker | 1eeca41 | 2018-10-15 12:01:35 +0100 | [diff] [blame] | 55 | } \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 56 | } while (0) |
Rich Evans | 7d5a55a | 2015-02-13 11:48:02 +0000 | [diff] [blame] | 57 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 58 | /* |
| 59 | * CertificateSerialNumber ::= INTEGER |
| 60 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 61 | int mbedtls_x509_get_serial(unsigned char **p, const unsigned char *end, |
| 62 | mbedtls_x509_buf *serial) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 63 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 64 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 65 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 66 | if ((end - *p) < 1) { |
| 67 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_SERIAL, |
| 68 | MBEDTLS_ERR_ASN1_OUT_OF_DATA); |
| 69 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 70 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 71 | if (**p != (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_PRIMITIVE | 2) && |
| 72 | **p != MBEDTLS_ASN1_INTEGER) { |
| 73 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_SERIAL, |
| 74 | MBEDTLS_ERR_ASN1_UNEXPECTED_TAG); |
| 75 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 76 | |
| 77 | serial->tag = *(*p)++; |
| 78 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 79 | if ((ret = mbedtls_asn1_get_len(p, end, &serial->len)) != 0) { |
| 80 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_SERIAL, ret); |
| 81 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 82 | |
| 83 | serial->p = *p; |
| 84 | *p += serial->len; |
| 85 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 86 | return 0; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | /* Get an algorithm identifier without parameters (eg for signatures) |
| 90 | * |
| 91 | * AlgorithmIdentifier ::= SEQUENCE { |
| 92 | * algorithm OBJECT IDENTIFIER, |
| 93 | * parameters ANY DEFINED BY algorithm OPTIONAL } |
| 94 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 95 | int mbedtls_x509_get_alg_null(unsigned char **p, const unsigned char *end, |
| 96 | mbedtls_x509_buf *alg) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 97 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 98 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 99 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 100 | if ((ret = mbedtls_asn1_get_alg_null(p, end, alg)) != 0) { |
| 101 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret); |
| 102 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 103 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 104 | return 0; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | /* |
Antonin Décimo | 36e89b5 | 2019-01-23 15:24:37 +0100 | [diff] [blame] | 108 | * Parse an algorithm identifier with (optional) parameters |
Manuel Pégourié-Gonnard | 59a75d5 | 2014-01-22 10:12:57 +0100 | [diff] [blame] | 109 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 110 | int mbedtls_x509_get_alg(unsigned char **p, const unsigned char *end, |
| 111 | mbedtls_x509_buf *alg, mbedtls_x509_buf *params) |
Manuel Pégourié-Gonnard | 59a75d5 | 2014-01-22 10:12:57 +0100 | [diff] [blame] | 112 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 113 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 59a75d5 | 2014-01-22 10:12:57 +0100 | [diff] [blame] | 114 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 115 | if ((ret = mbedtls_asn1_get_alg(p, end, alg, params)) != 0) { |
| 116 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret); |
| 117 | } |
Manuel Pégourié-Gonnard | 59a75d5 | 2014-01-22 10:12:57 +0100 | [diff] [blame] | 118 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 119 | return 0; |
Manuel Pégourié-Gonnard | 59a75d5 | 2014-01-22 10:12:57 +0100 | [diff] [blame] | 120 | } |
| 121 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 122 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) |
Manuel Pégourié-Gonnard | 59a75d5 | 2014-01-22 10:12:57 +0100 | [diff] [blame] | 123 | /* |
Manuel Pégourié-Gonnard | e76b750 | 2014-01-23 19:15:29 +0100 | [diff] [blame] | 124 | * HashAlgorithm ::= AlgorithmIdentifier |
| 125 | * |
| 126 | * AlgorithmIdentifier ::= SEQUENCE { |
| 127 | * algorithm OBJECT IDENTIFIER, |
| 128 | * parameters ANY DEFINED BY algorithm OPTIONAL } |
| 129 | * |
| 130 | * For HashAlgorithm, parameters MUST be NULL or absent. |
| 131 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 132 | static int x509_get_hash_alg(const mbedtls_x509_buf *alg, mbedtls_md_type_t *md_alg) |
Manuel Pégourié-Gonnard | e76b750 | 2014-01-23 19:15:29 +0100 | [diff] [blame] | 133 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 134 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | e76b750 | 2014-01-23 19:15:29 +0100 | [diff] [blame] | 135 | unsigned char *p; |
| 136 | const unsigned char *end; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 137 | mbedtls_x509_buf md_oid; |
Manuel Pégourié-Gonnard | e76b750 | 2014-01-23 19:15:29 +0100 | [diff] [blame] | 138 | size_t len; |
| 139 | |
| 140 | /* Make sure we got a SEQUENCE and setup bounds */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 141 | if (alg->tag != (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) { |
| 142 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, |
| 143 | MBEDTLS_ERR_ASN1_UNEXPECTED_TAG); |
| 144 | } |
Manuel Pégourié-Gonnard | e76b750 | 2014-01-23 19:15:29 +0100 | [diff] [blame] | 145 | |
Andrzej Kurek | feaebc5 | 2020-07-16 04:37:41 -0400 | [diff] [blame] | 146 | p = alg->p; |
Manuel Pégourié-Gonnard | e76b750 | 2014-01-23 19:15:29 +0100 | [diff] [blame] | 147 | end = p + alg->len; |
| 148 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 149 | if (p >= end) { |
| 150 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, |
| 151 | MBEDTLS_ERR_ASN1_OUT_OF_DATA); |
| 152 | } |
Manuel Pégourié-Gonnard | e76b750 | 2014-01-23 19:15:29 +0100 | [diff] [blame] | 153 | |
| 154 | /* Parse md_oid */ |
| 155 | md_oid.tag = *p; |
| 156 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 157 | if ((ret = mbedtls_asn1_get_tag(&p, end, &md_oid.len, MBEDTLS_ASN1_OID)) != 0) { |
| 158 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret); |
| 159 | } |
Manuel Pégourié-Gonnard | e76b750 | 2014-01-23 19:15:29 +0100 | [diff] [blame] | 160 | |
| 161 | md_oid.p = p; |
| 162 | p += md_oid.len; |
| 163 | |
| 164 | /* Get md_alg from md_oid */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 165 | if ((ret = mbedtls_oid_get_md_alg(&md_oid, md_alg)) != 0) { |
| 166 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret); |
| 167 | } |
Manuel Pégourié-Gonnard | e76b750 | 2014-01-23 19:15:29 +0100 | [diff] [blame] | 168 | |
| 169 | /* Make sure params is absent of NULL */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 170 | if (p == end) { |
| 171 | return 0; |
| 172 | } |
Manuel Pégourié-Gonnard | e76b750 | 2014-01-23 19:15:29 +0100 | [diff] [blame] | 173 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 174 | if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_NULL)) != 0 || len != 0) { |
| 175 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret); |
| 176 | } |
Manuel Pégourié-Gonnard | e76b750 | 2014-01-23 19:15:29 +0100 | [diff] [blame] | 177 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 178 | if (p != end) { |
| 179 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, |
| 180 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH); |
| 181 | } |
Manuel Pégourié-Gonnard | e76b750 | 2014-01-23 19:15:29 +0100 | [diff] [blame] | 182 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 183 | return 0; |
Manuel Pégourié-Gonnard | e76b750 | 2014-01-23 19:15:29 +0100 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | /* |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 187 | * RSASSA-PSS-params ::= SEQUENCE { |
| 188 | * hashAlgorithm [0] HashAlgorithm DEFAULT sha1Identifier, |
| 189 | * maskGenAlgorithm [1] MaskGenAlgorithm DEFAULT mgf1SHA1Identifier, |
| 190 | * saltLength [2] INTEGER DEFAULT 20, |
| 191 | * trailerField [3] INTEGER DEFAULT 1 } |
| 192 | * -- Note that the tags in this Sequence are explicit. |
Manuel Pégourié-Gonnard | 78117d5 | 2014-05-31 17:08:16 +0200 | [diff] [blame] | 193 | * |
| 194 | * RFC 4055 (which defines use of RSASSA-PSS in PKIX) states that the value |
| 195 | * of trailerField MUST be 1, and PKCS#1 v2.2 doesn't even define any other |
Tom Cosgrove | 49f99bc | 2022-12-04 16:44:21 +0000 | [diff] [blame] | 196 | * option. Enforce this at parsing time. |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 197 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 198 | int mbedtls_x509_get_rsassa_pss_params(const mbedtls_x509_buf *params, |
| 199 | mbedtls_md_type_t *md_alg, mbedtls_md_type_t *mgf_md, |
| 200 | int *salt_len) |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 201 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 202 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 203 | unsigned char *p; |
Manuel Pégourié-Gonnard | 9c9cf5b | 2014-01-24 14:15:20 +0100 | [diff] [blame] | 204 | const unsigned char *end, *end2; |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 205 | size_t len; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 206 | mbedtls_x509_buf alg_id, alg_params; |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 207 | |
| 208 | /* First set everything to defaults */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 209 | *md_alg = MBEDTLS_MD_SHA1; |
| 210 | *mgf_md = MBEDTLS_MD_SHA1; |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 211 | *salt_len = 20; |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 212 | |
| 213 | /* Make sure params is a SEQUENCE and setup bounds */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 214 | if (params->tag != (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) { |
| 215 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, |
| 216 | MBEDTLS_ERR_ASN1_UNEXPECTED_TAG); |
| 217 | } |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 218 | |
| 219 | p = (unsigned char *) params->p; |
| 220 | end = p + params->len; |
| 221 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 222 | if (p == end) { |
| 223 | return 0; |
| 224 | } |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 225 | |
Manuel Pégourié-Gonnard | 9c9cf5b | 2014-01-24 14:15:20 +0100 | [diff] [blame] | 226 | /* |
| 227 | * HashAlgorithm |
| 228 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 229 | if ((ret = mbedtls_asn1_get_tag(&p, end, &len, |
| 230 | MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | |
| 231 | 0)) == 0) { |
Manuel Pégourié-Gonnard | 9c9cf5b | 2014-01-24 14:15:20 +0100 | [diff] [blame] | 232 | end2 = p + len; |
| 233 | |
Manuel Pégourié-Gonnard | e76b750 | 2014-01-23 19:15:29 +0100 | [diff] [blame] | 234 | /* HashAlgorithm ::= AlgorithmIdentifier (without parameters) */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 235 | if ((ret = mbedtls_x509_get_alg_null(&p, end2, &alg_id)) != 0) { |
| 236 | return ret; |
| 237 | } |
Manuel Pégourié-Gonnard | e76b750 | 2014-01-23 19:15:29 +0100 | [diff] [blame] | 238 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 239 | if ((ret = mbedtls_oid_get_md_alg(&alg_id, md_alg)) != 0) { |
| 240 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret); |
| 241 | } |
Manuel Pégourié-Gonnard | 9c9cf5b | 2014-01-24 14:15:20 +0100 | [diff] [blame] | 242 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 243 | if (p != end2) { |
| 244 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, |
| 245 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH); |
| 246 | } |
| 247 | } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) { |
| 248 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret); |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 249 | } |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 250 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 251 | if (p == end) { |
| 252 | return 0; |
| 253 | } |
Manuel Pégourié-Gonnard | 9c9cf5b | 2014-01-24 14:15:20 +0100 | [diff] [blame] | 254 | |
| 255 | /* |
| 256 | * MaskGenAlgorithm |
| 257 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 258 | if ((ret = mbedtls_asn1_get_tag(&p, end, &len, |
| 259 | MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | |
| 260 | 1)) == 0) { |
Manuel Pégourié-Gonnard | 9c9cf5b | 2014-01-24 14:15:20 +0100 | [diff] [blame] | 261 | end2 = p + len; |
| 262 | |
Manuel Pégourié-Gonnard | e76b750 | 2014-01-23 19:15:29 +0100 | [diff] [blame] | 263 | /* MaskGenAlgorithm ::= AlgorithmIdentifier (params = HashAlgorithm) */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 264 | if ((ret = mbedtls_x509_get_alg(&p, end2, &alg_id, &alg_params)) != 0) { |
| 265 | return ret; |
| 266 | } |
Manuel Pégourié-Gonnard | e76b750 | 2014-01-23 19:15:29 +0100 | [diff] [blame] | 267 | |
| 268 | /* Only MFG1 is recognised for now */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 269 | if (MBEDTLS_OID_CMP(MBEDTLS_OID_MGF1, &alg_id) != 0) { |
| 270 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE, |
| 271 | MBEDTLS_ERR_OID_NOT_FOUND); |
| 272 | } |
Manuel Pégourié-Gonnard | e76b750 | 2014-01-23 19:15:29 +0100 | [diff] [blame] | 273 | |
| 274 | /* Parse HashAlgorithm */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 275 | if ((ret = x509_get_hash_alg(&alg_params, mgf_md)) != 0) { |
| 276 | return ret; |
| 277 | } |
Manuel Pégourié-Gonnard | 9c9cf5b | 2014-01-24 14:15:20 +0100 | [diff] [blame] | 278 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 279 | if (p != end2) { |
| 280 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, |
| 281 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH); |
| 282 | } |
| 283 | } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) { |
| 284 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret); |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 285 | } |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 286 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 287 | if (p == end) { |
| 288 | return 0; |
| 289 | } |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 290 | |
Manuel Pégourié-Gonnard | 9c9cf5b | 2014-01-24 14:15:20 +0100 | [diff] [blame] | 291 | /* |
| 292 | * salt_len |
| 293 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 294 | if ((ret = mbedtls_asn1_get_tag(&p, end, &len, |
| 295 | MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | |
| 296 | 2)) == 0) { |
Manuel Pégourié-Gonnard | 9c9cf5b | 2014-01-24 14:15:20 +0100 | [diff] [blame] | 297 | end2 = p + len; |
| 298 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 299 | if ((ret = mbedtls_asn1_get_int(&p, end2, salt_len)) != 0) { |
| 300 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret); |
| 301 | } |
Manuel Pégourié-Gonnard | 9c9cf5b | 2014-01-24 14:15:20 +0100 | [diff] [blame] | 302 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 303 | if (p != end2) { |
| 304 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, |
| 305 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH); |
| 306 | } |
| 307 | } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) { |
| 308 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret); |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 309 | } |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 310 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 311 | if (p == end) { |
| 312 | return 0; |
| 313 | } |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 314 | |
Manuel Pégourié-Gonnard | 9c9cf5b | 2014-01-24 14:15:20 +0100 | [diff] [blame] | 315 | /* |
Manuel Pégourié-Gonnard | 78117d5 | 2014-05-31 17:08:16 +0200 | [diff] [blame] | 316 | * trailer_field (if present, must be 1) |
Manuel Pégourié-Gonnard | 9c9cf5b | 2014-01-24 14:15:20 +0100 | [diff] [blame] | 317 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 318 | if ((ret = mbedtls_asn1_get_tag(&p, end, &len, |
| 319 | MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | |
| 320 | 3)) == 0) { |
Manuel Pégourié-Gonnard | 78117d5 | 2014-05-31 17:08:16 +0200 | [diff] [blame] | 321 | int trailer_field; |
| 322 | |
Manuel Pégourié-Gonnard | 9c9cf5b | 2014-01-24 14:15:20 +0100 | [diff] [blame] | 323 | end2 = p + len; |
| 324 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 325 | if ((ret = mbedtls_asn1_get_int(&p, end2, &trailer_field)) != 0) { |
| 326 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret); |
| 327 | } |
Manuel Pégourié-Gonnard | 9c9cf5b | 2014-01-24 14:15:20 +0100 | [diff] [blame] | 328 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 329 | if (p != end2) { |
| 330 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, |
| 331 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH); |
| 332 | } |
Manuel Pégourié-Gonnard | 78117d5 | 2014-05-31 17:08:16 +0200 | [diff] [blame] | 333 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 334 | if (trailer_field != 1) { |
| 335 | return MBEDTLS_ERR_X509_INVALID_ALG; |
| 336 | } |
| 337 | } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) { |
| 338 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret); |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 339 | } |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 340 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 341 | if (p != end) { |
| 342 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, |
| 343 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH); |
| 344 | } |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 345 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 346 | return 0; |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 347 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 348 | #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */ |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 349 | |
| 350 | /* |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 351 | * AttributeTypeAndValue ::= SEQUENCE { |
| 352 | * type AttributeType, |
| 353 | * value AttributeValue } |
| 354 | * |
| 355 | * AttributeType ::= OBJECT IDENTIFIER |
| 356 | * |
| 357 | * AttributeValue ::= ANY DEFINED BY AttributeType |
| 358 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 359 | static int x509_get_attr_type_value(unsigned char **p, |
| 360 | const unsigned char *end, |
| 361 | mbedtls_x509_name *cur) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 362 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 363 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 364 | size_t len; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 365 | mbedtls_x509_buf *oid; |
| 366 | mbedtls_x509_buf *val; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 367 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 368 | if ((ret = mbedtls_asn1_get_tag(p, end, &len, |
| 369 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) { |
| 370 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME, ret); |
| 371 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 372 | |
Hanno Becker | 12f62fb | 2019-02-12 17:22:36 +0000 | [diff] [blame] | 373 | end = *p + len; |
| 374 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 375 | if ((end - *p) < 1) { |
| 376 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME, |
| 377 | MBEDTLS_ERR_ASN1_OUT_OF_DATA); |
| 378 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 379 | |
| 380 | oid = &cur->oid; |
| 381 | oid->tag = **p; |
| 382 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 383 | if ((ret = mbedtls_asn1_get_tag(p, end, &oid->len, MBEDTLS_ASN1_OID)) != 0) { |
| 384 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME, ret); |
| 385 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 386 | |
| 387 | oid->p = *p; |
| 388 | *p += oid->len; |
| 389 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 390 | if ((end - *p) < 1) { |
| 391 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME, |
| 392 | MBEDTLS_ERR_ASN1_OUT_OF_DATA); |
| 393 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 394 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 395 | if (**p != MBEDTLS_ASN1_BMP_STRING && **p != MBEDTLS_ASN1_UTF8_STRING && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 396 | **p != MBEDTLS_ASN1_T61_STRING && **p != MBEDTLS_ASN1_PRINTABLE_STRING && |
| 397 | **p != MBEDTLS_ASN1_IA5_STRING && **p != MBEDTLS_ASN1_UNIVERSAL_STRING && |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 398 | **p != MBEDTLS_ASN1_BIT_STRING) { |
| 399 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME, |
| 400 | MBEDTLS_ERR_ASN1_UNEXPECTED_TAG); |
| 401 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 402 | |
| 403 | val = &cur->val; |
| 404 | val->tag = *(*p)++; |
| 405 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 406 | if ((ret = mbedtls_asn1_get_len(p, end, &val->len)) != 0) { |
| 407 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME, ret); |
| 408 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 409 | |
| 410 | val->p = *p; |
| 411 | *p += val->len; |
| 412 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 413 | if (*p != end) { |
| 414 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME, |
| 415 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH); |
Hanno Becker | 12f62fb | 2019-02-12 17:22:36 +0000 | [diff] [blame] | 416 | } |
| 417 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 418 | cur->next = NULL; |
| 419 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 420 | return 0; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | /* |
Manuel Pégourié-Gonnard | 555fbf8 | 2015-02-04 17:11:55 +0000 | [diff] [blame] | 424 | * Name ::= CHOICE { -- only one possibility for now -- |
| 425 | * rdnSequence RDNSequence } |
| 426 | * |
| 427 | * RDNSequence ::= SEQUENCE OF RelativeDistinguishedName |
| 428 | * |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 429 | * RelativeDistinguishedName ::= |
| 430 | * SET OF AttributeTypeAndValue |
| 431 | * |
| 432 | * AttributeTypeAndValue ::= SEQUENCE { |
| 433 | * type AttributeType, |
| 434 | * value AttributeValue } |
| 435 | * |
| 436 | * AttributeType ::= OBJECT IDENTIFIER |
| 437 | * |
| 438 | * AttributeValue ::= ANY DEFINED BY AttributeType |
Manuel Pégourié-Gonnard | 5d86185 | 2014-10-17 12:41:41 +0200 | [diff] [blame] | 439 | * |
Manuel Pégourié-Gonnard | 555fbf8 | 2015-02-04 17:11:55 +0000 | [diff] [blame] | 440 | * The data structure is optimized for the common case where each RDN has only |
| 441 | * one element, which is represented as a list of AttributeTypeAndValue. |
| 442 | * For the general case we still use a flat list, but we mark elements of the |
| 443 | * same set so that they are "merged" together in the functions that consume |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 444 | * this list, eg mbedtls_x509_dn_gets(). |
David Horstmann | 8c176b4 | 2022-10-04 18:12:06 +0100 | [diff] [blame] | 445 | * |
David Horstmann | 5ad5e16 | 2022-10-17 18:10:23 +0100 | [diff] [blame] | 446 | * On success, this function may allocate a linked list starting at cur->next |
David Horstmann | 8c176b4 | 2022-10-04 18:12:06 +0100 | [diff] [blame] | 447 | * that must later be free'd by the caller using mbedtls_free(). In error |
| 448 | * cases, this function frees all allocated memory internally and the caller |
| 449 | * has no freeing responsibilities. |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 450 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 451 | int mbedtls_x509_get_name(unsigned char **p, const unsigned char *end, |
| 452 | mbedtls_x509_name *cur) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 453 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 454 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 5d86185 | 2014-10-17 12:41:41 +0200 | [diff] [blame] | 455 | size_t set_len; |
| 456 | const unsigned char *end_set; |
David Horstmann | 8c176b4 | 2022-10-04 18:12:06 +0100 | [diff] [blame] | 457 | mbedtls_x509_name *head = cur; |
| 458 | mbedtls_x509_name *prev, *allocated; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 459 | |
Manuel Pégourié-Gonnard | d681443 | 2014-11-12 01:25:31 +0100 | [diff] [blame] | 460 | /* don't use recursion, we'd risk stack overflow if not optimized */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 461 | while (1) { |
Manuel Pégourié-Gonnard | d681443 | 2014-11-12 01:25:31 +0100 | [diff] [blame] | 462 | /* |
Manuel Pégourié-Gonnard | 555fbf8 | 2015-02-04 17:11:55 +0000 | [diff] [blame] | 463 | * parse SET |
Manuel Pégourié-Gonnard | d681443 | 2014-11-12 01:25:31 +0100 | [diff] [blame] | 464 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 465 | if ((ret = mbedtls_asn1_get_tag(p, end, &set_len, |
| 466 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SET)) != 0) { |
| 467 | ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME, ret); |
David Horstmann | 8c176b4 | 2022-10-04 18:12:06 +0100 | [diff] [blame] | 468 | goto error; |
| 469 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 470 | |
Manuel Pégourié-Gonnard | d681443 | 2014-11-12 01:25:31 +0100 | [diff] [blame] | 471 | end_set = *p + set_len; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 472 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 473 | while (1) { |
| 474 | if ((ret = x509_get_attr_type_value(p, end_set, cur)) != 0) { |
David Horstmann | 8c176b4 | 2022-10-04 18:12:06 +0100 | [diff] [blame] | 475 | goto error; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 476 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 477 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 478 | if (*p == end_set) { |
Manuel Pégourié-Gonnard | 555fbf8 | 2015-02-04 17:11:55 +0000 | [diff] [blame] | 479 | break; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 480 | } |
Manuel Pégourié-Gonnard | 555fbf8 | 2015-02-04 17:11:55 +0000 | [diff] [blame] | 481 | |
Manuel Pégourié-Gonnard | eecb43c | 2015-05-12 12:56:41 +0200 | [diff] [blame] | 482 | /* Mark this item as being no the only one in a set */ |
Manuel Pégourié-Gonnard | 555fbf8 | 2015-02-04 17:11:55 +0000 | [diff] [blame] | 483 | cur->next_merged = 1; |
| 484 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 485 | cur->next = mbedtls_calloc(1, sizeof(mbedtls_x509_name)); |
Manuel Pégourié-Gonnard | 555fbf8 | 2015-02-04 17:11:55 +0000 | [diff] [blame] | 486 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 487 | if (cur->next == NULL) { |
David Horstmann | 8c176b4 | 2022-10-04 18:12:06 +0100 | [diff] [blame] | 488 | ret = MBEDTLS_ERR_X509_ALLOC_FAILED; |
| 489 | goto error; |
| 490 | } |
Manuel Pégourié-Gonnard | 555fbf8 | 2015-02-04 17:11:55 +0000 | [diff] [blame] | 491 | |
Manuel Pégourié-Gonnard | 555fbf8 | 2015-02-04 17:11:55 +0000 | [diff] [blame] | 492 | cur = cur->next; |
| 493 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 494 | |
Manuel Pégourié-Gonnard | d681443 | 2014-11-12 01:25:31 +0100 | [diff] [blame] | 495 | /* |
| 496 | * continue until end of SEQUENCE is reached |
| 497 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 498 | if (*p == end) { |
| 499 | return 0; |
| 500 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 501 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 502 | cur->next = mbedtls_calloc(1, sizeof(mbedtls_x509_name)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 503 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 504 | if (cur->next == NULL) { |
David Horstmann | 8c176b4 | 2022-10-04 18:12:06 +0100 | [diff] [blame] | 505 | ret = MBEDTLS_ERR_X509_ALLOC_FAILED; |
| 506 | goto error; |
| 507 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 508 | |
Manuel Pégourié-Gonnard | d681443 | 2014-11-12 01:25:31 +0100 | [diff] [blame] | 509 | cur = cur->next; |
| 510 | } |
David Horstmann | 8c176b4 | 2022-10-04 18:12:06 +0100 | [diff] [blame] | 511 | |
| 512 | error: |
David Horstmann | 8c176b4 | 2022-10-04 18:12:06 +0100 | [diff] [blame] | 513 | /* Skip the first element as we did not allocate it */ |
| 514 | allocated = head->next; |
| 515 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 516 | while (allocated != NULL) { |
David Horstmann | 8c176b4 | 2022-10-04 18:12:06 +0100 | [diff] [blame] | 517 | prev = allocated; |
| 518 | allocated = allocated->next; |
| 519 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 520 | mbedtls_platform_zeroize(prev, sizeof(*prev)); |
| 521 | mbedtls_free(prev); |
David Horstmann | 8c176b4 | 2022-10-04 18:12:06 +0100 | [diff] [blame] | 522 | } |
| 523 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 524 | mbedtls_platform_zeroize(head, sizeof(*head)); |
David Horstmann | 8c176b4 | 2022-10-04 18:12:06 +0100 | [diff] [blame] | 525 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 526 | return ret; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 527 | } |
| 528 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 529 | static int x509_parse_int(unsigned char **p, size_t n, int *res) |
Janos Follath | 87c9807 | 2017-02-03 12:36:59 +0000 | [diff] [blame] | 530 | { |
Rich Evans | 7d5a55a | 2015-02-13 11:48:02 +0000 | [diff] [blame] | 531 | *res = 0; |
Janos Follath | 87c9807 | 2017-02-03 12:36:59 +0000 | [diff] [blame] | 532 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 533 | for (; n > 0; --n) { |
| 534 | if ((**p < '0') || (**p > '9')) { |
| 535 | return MBEDTLS_ERR_X509_INVALID_DATE; |
| 536 | } |
Janos Follath | 87c9807 | 2017-02-03 12:36:59 +0000 | [diff] [blame] | 537 | |
Rich Evans | 7d5a55a | 2015-02-13 11:48:02 +0000 | [diff] [blame] | 538 | *res *= 10; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 539 | *res += (*(*p)++ - '0'); |
Rich Evans | 7d5a55a | 2015-02-13 11:48:02 +0000 | [diff] [blame] | 540 | } |
Janos Follath | 87c9807 | 2017-02-03 12:36:59 +0000 | [diff] [blame] | 541 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 542 | return 0; |
Rich Evans | 7d5a55a | 2015-02-13 11:48:02 +0000 | [diff] [blame] | 543 | } |
| 544 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 545 | static int x509_date_is_valid(const mbedtls_x509_time *t) |
Andres AG | 4b76aec | 2016-09-23 13:16:02 +0100 | [diff] [blame] | 546 | { |
| 547 | int ret = MBEDTLS_ERR_X509_INVALID_DATE; |
Andres Amaya Garcia | 735b37e | 2016-11-21 15:38:02 +0000 | [diff] [blame] | 548 | int month_len; |
Andres AG | 4b76aec | 2016-09-23 13:16:02 +0100 | [diff] [blame] | 549 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 550 | CHECK_RANGE(0, 9999, t->year); |
| 551 | CHECK_RANGE(0, 23, t->hour); |
| 552 | CHECK_RANGE(0, 59, t->min); |
| 553 | CHECK_RANGE(0, 59, t->sec); |
Andres AG | 4b76aec | 2016-09-23 13:16:02 +0100 | [diff] [blame] | 554 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 555 | switch (t->mon) { |
Andres AG | 4b76aec | 2016-09-23 13:16:02 +0100 | [diff] [blame] | 556 | case 1: case 3: case 5: case 7: case 8: case 10: case 12: |
Andres Amaya Garcia | 735b37e | 2016-11-21 15:38:02 +0000 | [diff] [blame] | 557 | month_len = 31; |
Andres AG | 4b76aec | 2016-09-23 13:16:02 +0100 | [diff] [blame] | 558 | break; |
| 559 | case 4: case 6: case 9: case 11: |
Andres Amaya Garcia | 735b37e | 2016-11-21 15:38:02 +0000 | [diff] [blame] | 560 | month_len = 30; |
Andres AG | 4b76aec | 2016-09-23 13:16:02 +0100 | [diff] [blame] | 561 | break; |
| 562 | case 2: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 563 | if ((!(t->year % 4) && t->year % 100) || |
| 564 | !(t->year % 400)) { |
Andres Amaya Garcia | 735b37e | 2016-11-21 15:38:02 +0000 | [diff] [blame] | 565 | month_len = 29; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 566 | } else { |
Andres Amaya Garcia | 735b37e | 2016-11-21 15:38:02 +0000 | [diff] [blame] | 567 | month_len = 28; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 568 | } |
Andres AG | 4b76aec | 2016-09-23 13:16:02 +0100 | [diff] [blame] | 569 | break; |
| 570 | default: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 571 | return ret; |
Andres AG | 4b76aec | 2016-09-23 13:16:02 +0100 | [diff] [blame] | 572 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 573 | CHECK_RANGE(1, month_len, t->day); |
Andres AG | 4b76aec | 2016-09-23 13:16:02 +0100 | [diff] [blame] | 574 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 575 | return 0; |
Andres AG | 4b76aec | 2016-09-23 13:16:02 +0100 | [diff] [blame] | 576 | } |
| 577 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 578 | /* |
Janos Follath | 87c9807 | 2017-02-03 12:36:59 +0000 | [diff] [blame] | 579 | * Parse an ASN1_UTC_TIME (yearlen=2) or ASN1_GENERALIZED_TIME (yearlen=4) |
| 580 | * field. |
| 581 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 582 | static int x509_parse_time(unsigned char **p, size_t len, size_t yearlen, |
| 583 | mbedtls_x509_time *tm) |
Janos Follath | 87c9807 | 2017-02-03 12:36:59 +0000 | [diff] [blame] | 584 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 585 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Janos Follath | 87c9807 | 2017-02-03 12:36:59 +0000 | [diff] [blame] | 586 | |
| 587 | /* |
| 588 | * Minimum length is 10 or 12 depending on yearlen |
| 589 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 590 | if (len < yearlen + 8) { |
| 591 | return MBEDTLS_ERR_X509_INVALID_DATE; |
| 592 | } |
Janos Follath | 87c9807 | 2017-02-03 12:36:59 +0000 | [diff] [blame] | 593 | len -= yearlen + 8; |
| 594 | |
| 595 | /* |
| 596 | * Parse year, month, day, hour, minute |
| 597 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 598 | CHECK(x509_parse_int(p, yearlen, &tm->year)); |
| 599 | if (2 == yearlen) { |
| 600 | if (tm->year < 50) { |
Hanno Becker | 61937d4 | 2017-04-26 15:01:23 +0100 | [diff] [blame] | 601 | tm->year += 100; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 602 | } |
Janos Follath | 87c9807 | 2017-02-03 12:36:59 +0000 | [diff] [blame] | 603 | |
Hanno Becker | 61937d4 | 2017-04-26 15:01:23 +0100 | [diff] [blame] | 604 | tm->year += 1900; |
Janos Follath | 87c9807 | 2017-02-03 12:36:59 +0000 | [diff] [blame] | 605 | } |
| 606 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 607 | CHECK(x509_parse_int(p, 2, &tm->mon)); |
| 608 | CHECK(x509_parse_int(p, 2, &tm->day)); |
| 609 | CHECK(x509_parse_int(p, 2, &tm->hour)); |
| 610 | CHECK(x509_parse_int(p, 2, &tm->min)); |
Janos Follath | 87c9807 | 2017-02-03 12:36:59 +0000 | [diff] [blame] | 611 | |
| 612 | /* |
| 613 | * Parse seconds if present |
| 614 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 615 | if (len >= 2) { |
| 616 | CHECK(x509_parse_int(p, 2, &tm->sec)); |
Janos Follath | 87c9807 | 2017-02-03 12:36:59 +0000 | [diff] [blame] | 617 | len -= 2; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 618 | } else { |
| 619 | return MBEDTLS_ERR_X509_INVALID_DATE; |
Janos Follath | 87c9807 | 2017-02-03 12:36:59 +0000 | [diff] [blame] | 620 | } |
Janos Follath | 87c9807 | 2017-02-03 12:36:59 +0000 | [diff] [blame] | 621 | |
| 622 | /* |
| 623 | * Parse trailing 'Z' if present |
| 624 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 625 | if (1 == len && 'Z' == **p) { |
Janos Follath | 87c9807 | 2017-02-03 12:36:59 +0000 | [diff] [blame] | 626 | (*p)++; |
| 627 | len--; |
| 628 | } |
| 629 | |
| 630 | /* |
| 631 | * We should have parsed all characters at this point |
| 632 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 633 | if (0 != len) { |
| 634 | return MBEDTLS_ERR_X509_INVALID_DATE; |
| 635 | } |
Janos Follath | 87c9807 | 2017-02-03 12:36:59 +0000 | [diff] [blame] | 636 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 637 | CHECK(x509_date_is_valid(tm)); |
Janos Follath | 87c9807 | 2017-02-03 12:36:59 +0000 | [diff] [blame] | 638 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 639 | return 0; |
Janos Follath | 87c9807 | 2017-02-03 12:36:59 +0000 | [diff] [blame] | 640 | } |
| 641 | |
| 642 | /* |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 643 | * Time ::= CHOICE { |
| 644 | * utcTime UTCTime, |
| 645 | * generalTime GeneralizedTime } |
| 646 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 647 | int mbedtls_x509_get_time(unsigned char **p, const unsigned char *end, |
| 648 | mbedtls_x509_time *tm) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 649 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 650 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Janos Follath | 87c9807 | 2017-02-03 12:36:59 +0000 | [diff] [blame] | 651 | size_t len, year_len; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 652 | unsigned char tag; |
| 653 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 654 | if ((end - *p) < 1) { |
| 655 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE, |
| 656 | MBEDTLS_ERR_ASN1_OUT_OF_DATA); |
| 657 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 658 | |
| 659 | tag = **p; |
| 660 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 661 | if (tag == MBEDTLS_ASN1_UTC_TIME) { |
Janos Follath | 87c9807 | 2017-02-03 12:36:59 +0000 | [diff] [blame] | 662 | year_len = 2; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 663 | } else if (tag == MBEDTLS_ASN1_GENERALIZED_TIME) { |
Janos Follath | 87c9807 | 2017-02-03 12:36:59 +0000 | [diff] [blame] | 664 | year_len = 4; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 665 | } else { |
| 666 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE, |
| 667 | MBEDTLS_ERR_ASN1_UNEXPECTED_TAG); |
| 668 | } |
Janos Follath | 87c9807 | 2017-02-03 12:36:59 +0000 | [diff] [blame] | 669 | |
| 670 | (*p)++; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 671 | ret = mbedtls_asn1_get_len(p, end, &len); |
Janos Follath | 87c9807 | 2017-02-03 12:36:59 +0000 | [diff] [blame] | 672 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 673 | if (ret != 0) { |
| 674 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE, ret); |
| 675 | } |
Janos Follath | 87c9807 | 2017-02-03 12:36:59 +0000 | [diff] [blame] | 676 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 677 | return x509_parse_time(p, len, year_len, tm); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 678 | } |
| 679 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 680 | int mbedtls_x509_get_sig(unsigned char **p, const unsigned char *end, mbedtls_x509_buf *sig) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 681 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 682 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 683 | size_t len; |
Andres AG | 4bdbe09 | 2016-09-19 16:58:45 +0100 | [diff] [blame] | 684 | int tag_type; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 685 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 686 | if ((end - *p) < 1) { |
| 687 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_SIGNATURE, |
| 688 | MBEDTLS_ERR_ASN1_OUT_OF_DATA); |
| 689 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 690 | |
Andres AG | 4bdbe09 | 2016-09-19 16:58:45 +0100 | [diff] [blame] | 691 | tag_type = **p; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 692 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 693 | if ((ret = mbedtls_asn1_get_bitstring_null(p, end, &len)) != 0) { |
| 694 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_SIGNATURE, ret); |
| 695 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 696 | |
Andres AG | 4bdbe09 | 2016-09-19 16:58:45 +0100 | [diff] [blame] | 697 | sig->tag = tag_type; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 698 | sig->len = len; |
| 699 | sig->p = *p; |
| 700 | |
| 701 | *p += len; |
| 702 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 703 | return 0; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 704 | } |
| 705 | |
Manuel Pégourié-Gonnard | cf975a3 | 2014-01-24 19:28:43 +0100 | [diff] [blame] | 706 | /* |
| 707 | * Get signature algorithm from alg OID and optional parameters |
| 708 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 709 | int mbedtls_x509_get_sig_alg(const mbedtls_x509_buf *sig_oid, const mbedtls_x509_buf *sig_params, |
| 710 | mbedtls_md_type_t *md_alg, mbedtls_pk_type_t *pk_alg, |
| 711 | void **sig_opts) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 712 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 713 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 714 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 715 | if (*sig_opts != NULL) { |
| 716 | return MBEDTLS_ERR_X509_BAD_INPUT_DATA; |
| 717 | } |
Manuel Pégourié-Gonnard | f75f2f7 | 2014-06-05 15:14:28 +0200 | [diff] [blame] | 718 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 719 | if ((ret = mbedtls_oid_get_sig_alg(sig_oid, md_alg, pk_alg)) != 0) { |
| 720 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG, ret); |
| 721 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 722 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 723 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 724 | if (*pk_alg == MBEDTLS_PK_RSASSA_PSS) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 725 | mbedtls_pk_rsassa_pss_options *pss_opts; |
Manuel Pégourié-Gonnard | cf975a3 | 2014-01-24 19:28:43 +0100 | [diff] [blame] | 726 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 727 | pss_opts = mbedtls_calloc(1, sizeof(mbedtls_pk_rsassa_pss_options)); |
| 728 | if (pss_opts == NULL) { |
| 729 | return MBEDTLS_ERR_X509_ALLOC_FAILED; |
| 730 | } |
Manuel Pégourié-Gonnard | f75f2f7 | 2014-06-05 15:14:28 +0200 | [diff] [blame] | 731 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 732 | ret = mbedtls_x509_get_rsassa_pss_params(sig_params, |
| 733 | md_alg, |
| 734 | &pss_opts->mgf1_hash_id, |
| 735 | &pss_opts->expected_salt_len); |
| 736 | if (ret != 0) { |
| 737 | mbedtls_free(pss_opts); |
| 738 | return ret; |
Manuel Pégourié-Gonnard | f75f2f7 | 2014-06-05 15:14:28 +0200 | [diff] [blame] | 739 | } |
Manuel Pégourié-Gonnard | cf975a3 | 2014-01-24 19:28:43 +0100 | [diff] [blame] | 740 | |
Manuel Pégourié-Gonnard | f75f2f7 | 2014-06-05 15:14:28 +0200 | [diff] [blame] | 741 | *sig_opts = (void *) pss_opts; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 742 | } else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 743 | #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */ |
Manuel Pégourié-Gonnard | cf975a3 | 2014-01-24 19:28:43 +0100 | [diff] [blame] | 744 | { |
| 745 | /* Make sure parameters are absent or NULL */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 746 | if ((sig_params->tag != MBEDTLS_ASN1_NULL && sig_params->tag != 0) || |
| 747 | sig_params->len != 0) { |
| 748 | return MBEDTLS_ERR_X509_INVALID_ALG; |
| 749 | } |
Manuel Pégourié-Gonnard | cf975a3 | 2014-01-24 19:28:43 +0100 | [diff] [blame] | 750 | } |
| 751 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 752 | return 0; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 753 | } |
| 754 | |
| 755 | /* |
| 756 | * X.509 Extensions (No parsing of extensions, pointer should |
Brian J Murray | 1903fb3 | 2016-11-06 04:45:15 -0800 | [diff] [blame] | 757 | * be either manually updated or extensions should be parsed!) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 758 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 759 | int mbedtls_x509_get_ext(unsigned char **p, const unsigned char *end, |
| 760 | mbedtls_x509_buf *ext, int tag) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 761 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 762 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 763 | size_t len; |
| 764 | |
Hanno Becker | 3cddba8 | 2019-02-11 14:33:36 +0000 | [diff] [blame] | 765 | /* Extension structure use EXPLICIT tagging. That is, the actual |
| 766 | * `Extensions` structure is wrapped by a tag-length pair using |
| 767 | * the respective context-specific tag. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 768 | ret = mbedtls_asn1_get_tag(p, end, &ext->len, |
| 769 | MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | tag); |
| 770 | if (ret != 0) { |
| 771 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret); |
| 772 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 773 | |
Hanno Becker | 6ccfb18 | 2019-02-12 11:52:10 +0000 | [diff] [blame] | 774 | ext->tag = MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | tag; |
| 775 | ext->p = *p; |
| 776 | end = *p + ext->len; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 777 | |
| 778 | /* |
| 779 | * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 780 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 781 | if ((ret = mbedtls_asn1_get_tag(p, end, &len, |
| 782 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) { |
| 783 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret); |
| 784 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 785 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 786 | if (end != *p + len) { |
| 787 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, |
| 788 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH); |
| 789 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 790 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 791 | return 0; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 792 | } |
| 793 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 794 | /* |
| 795 | * Store the name in printable form into buf; no more |
| 796 | * than size characters will be written |
| 797 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 798 | int mbedtls_x509_dn_gets(char *buf, size_t size, const mbedtls_x509_name *dn) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 799 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 800 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Werner Lewis | 02c9d3b | 2022-05-20 12:48:46 +0100 | [diff] [blame] | 801 | size_t i, j, n; |
Manuel Pégourié-Gonnard | 555fbf8 | 2015-02-04 17:11:55 +0000 | [diff] [blame] | 802 | unsigned char c, merge = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 803 | const mbedtls_x509_name *name; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 804 | const char *short_name = NULL; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 805 | char s[MBEDTLS_X509_MAX_DN_NAME_SIZE], *p; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 806 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 807 | memset(s, 0, sizeof(s)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 808 | |
| 809 | name = dn; |
| 810 | p = buf; |
| 811 | n = size; |
| 812 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 813 | while (name != NULL) { |
| 814 | if (!name->oid.p) { |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 815 | name = name->next; |
| 816 | continue; |
| 817 | } |
| 818 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 819 | if (name != dn) { |
| 820 | ret = mbedtls_snprintf(p, n, merge ? " + " : ", "); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 821 | MBEDTLS_X509_SAFE_SNPRINTF; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 822 | } |
| 823 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 824 | ret = mbedtls_oid_get_attr_short_name(&name->oid, &short_name); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 825 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 826 | if (ret == 0) { |
| 827 | ret = mbedtls_snprintf(p, n, "%s=", short_name); |
| 828 | } else { |
| 829 | ret = mbedtls_snprintf(p, n, "\?\?="); |
| 830 | } |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 831 | MBEDTLS_X509_SAFE_SNPRINTF; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 832 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 833 | for (i = 0, j = 0; i < name->val.len; i++, j++) { |
| 834 | if (j >= sizeof(s) - 1) { |
| 835 | return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL; |
| 836 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 837 | |
| 838 | c = name->val.p[i]; |
Werner Lewis | 02c9d3b | 2022-05-20 12:48:46 +0100 | [diff] [blame] | 839 | // Special characters requiring escaping, RFC 1779 |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 840 | if (c && strchr(",=+<>#;\"\\", c)) { |
| 841 | if (j + 1 >= sizeof(s) - 1) { |
| 842 | return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL; |
| 843 | } |
Werner Lewis | 02c9d3b | 2022-05-20 12:48:46 +0100 | [diff] [blame] | 844 | s[j++] = '\\'; |
| 845 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 846 | if (c < 32 || c >= 127) { |
| 847 | s[j] = '?'; |
| 848 | } else { |
| 849 | s[j] = c; |
| 850 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 851 | } |
Werner Lewis | 02c9d3b | 2022-05-20 12:48:46 +0100 | [diff] [blame] | 852 | s[j] = '\0'; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 853 | ret = mbedtls_snprintf(p, n, "%s", s); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 854 | MBEDTLS_X509_SAFE_SNPRINTF; |
Manuel Pégourié-Gonnard | 555fbf8 | 2015-02-04 17:11:55 +0000 | [diff] [blame] | 855 | |
| 856 | merge = name->next_merged; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 857 | name = name->next; |
| 858 | } |
| 859 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 860 | return (int) (size - n); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 861 | } |
| 862 | |
| 863 | /* |
| 864 | * Store the serial in printable form into buf; no more |
| 865 | * than size characters will be written |
| 866 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 867 | int mbedtls_x509_serial_gets(char *buf, size_t size, const mbedtls_x509_buf *serial) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 868 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 869 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 870 | size_t i, n, nr; |
| 871 | char *p; |
| 872 | |
| 873 | p = buf; |
| 874 | n = size; |
| 875 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 876 | nr = (serial->len <= 32) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 877 | ? serial->len : 28; |
| 878 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 879 | for (i = 0; i < nr; i++) { |
| 880 | if (i == 0 && nr > 1 && serial->p[i] == 0x0) { |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 881 | continue; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 882 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 883 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 884 | ret = mbedtls_snprintf(p, n, "%02X%s", |
| 885 | serial->p[i], (i < nr - 1) ? ":" : ""); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 886 | MBEDTLS_X509_SAFE_SNPRINTF; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 887 | } |
| 888 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 889 | if (nr != serial->len) { |
| 890 | ret = mbedtls_snprintf(p, n, "...."); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 891 | MBEDTLS_X509_SAFE_SNPRINTF; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 892 | } |
| 893 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 894 | return (int) (size - n); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 895 | } |
| 896 | |
| 897 | /* |
Manuel Pégourié-Gonnard | 9113603 | 2014-06-05 15:41:39 +0200 | [diff] [blame] | 898 | * Helper for writing signature algorithms |
Manuel Pégourié-Gonnard | cac31ee | 2014-01-25 11:50:59 +0100 | [diff] [blame] | 899 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 900 | int mbedtls_x509_sig_alg_gets(char *buf, size_t size, const mbedtls_x509_buf *sig_oid, |
| 901 | mbedtls_pk_type_t pk_alg, mbedtls_md_type_t md_alg, |
| 902 | const void *sig_opts) |
Manuel Pégourié-Gonnard | cac31ee | 2014-01-25 11:50:59 +0100 | [diff] [blame] | 903 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 904 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | cac31ee | 2014-01-25 11:50:59 +0100 | [diff] [blame] | 905 | char *p = buf; |
| 906 | size_t n = size; |
| 907 | const char *desc = NULL; |
| 908 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 909 | ret = mbedtls_oid_get_sig_alg_desc(sig_oid, &desc); |
| 910 | if (ret != 0) { |
| 911 | ret = mbedtls_snprintf(p, n, "???"); |
| 912 | } else { |
| 913 | ret = mbedtls_snprintf(p, n, "%s", desc); |
| 914 | } |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 915 | MBEDTLS_X509_SAFE_SNPRINTF; |
Manuel Pégourié-Gonnard | cac31ee | 2014-01-25 11:50:59 +0100 | [diff] [blame] | 916 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 917 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 918 | if (pk_alg == MBEDTLS_PK_RSASSA_PSS) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 919 | const mbedtls_pk_rsassa_pss_options *pss_opts; |
| 920 | const mbedtls_md_info_t *md_info, *mgf_md_info; |
Manuel Pégourié-Gonnard | cac31ee | 2014-01-25 11:50:59 +0100 | [diff] [blame] | 921 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 922 | pss_opts = (const mbedtls_pk_rsassa_pss_options *) sig_opts; |
Manuel Pégourié-Gonnard | cac31ee | 2014-01-25 11:50:59 +0100 | [diff] [blame] | 923 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 924 | md_info = mbedtls_md_info_from_type(md_alg); |
| 925 | mgf_md_info = mbedtls_md_info_from_type(pss_opts->mgf1_hash_id); |
Manuel Pégourié-Gonnard | cac31ee | 2014-01-25 11:50:59 +0100 | [diff] [blame] | 926 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 927 | ret = mbedtls_snprintf(p, n, " (%s, MGF1-%s, 0x%02X)", |
| 928 | md_info ? mbedtls_md_get_name(md_info) : "???", |
| 929 | mgf_md_info ? mbedtls_md_get_name(mgf_md_info) : "???", |
| 930 | (unsigned int) pss_opts->expected_salt_len); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 931 | MBEDTLS_X509_SAFE_SNPRINTF; |
Manuel Pégourié-Gonnard | cac31ee | 2014-01-25 11:50:59 +0100 | [diff] [blame] | 932 | } |
| 933 | #else |
| 934 | ((void) pk_alg); |
Manuel Pégourié-Gonnard | 9113603 | 2014-06-05 15:41:39 +0200 | [diff] [blame] | 935 | ((void) md_alg); |
| 936 | ((void) sig_opts); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 937 | #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */ |
Manuel Pégourié-Gonnard | cac31ee | 2014-01-25 11:50:59 +0100 | [diff] [blame] | 938 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 939 | return (int) (size - n); |
Manuel Pégourié-Gonnard | cac31ee | 2014-01-25 11:50:59 +0100 | [diff] [blame] | 940 | } |
| 941 | |
| 942 | /* |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 943 | * Helper for writing "RSA key size", "EC key size", etc |
| 944 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 945 | int mbedtls_x509_key_size_helper(char *buf, size_t buf_size, const char *name) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 946 | { |
| 947 | char *p = buf; |
Manuel Pégourié-Gonnard | fb317c5 | 2015-06-18 16:25:56 +0200 | [diff] [blame] | 948 | size_t n = buf_size; |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 949 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 950 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 951 | ret = mbedtls_snprintf(p, n, "%s key size", name); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 952 | MBEDTLS_X509_SAFE_SNPRINTF; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 953 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 954 | return 0; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 955 | } |
| 956 | |
Manuel Pégourié-Gonnard | 60c793b | 2015-06-18 20:52:58 +0200 | [diff] [blame] | 957 | #if defined(MBEDTLS_HAVE_TIME_DATE) |
Manuel Pégourié-Gonnard | 57e10d7 | 2015-06-22 18:59:21 +0200 | [diff] [blame] | 958 | /* |
| 959 | * Set the time structure to the current time. |
| 960 | * Return 0 on success, non-zero on failure. |
| 961 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 962 | static int x509_get_current_time(mbedtls_x509_time *now) |
Manuel Pégourié-Gonnard | 6304f78 | 2014-03-10 12:26:11 +0100 | [diff] [blame] | 963 | { |
Nicholas Wilson | 512b4ee | 2017-12-05 12:07:33 +0000 | [diff] [blame] | 964 | struct tm *lt, tm_buf; |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 965 | mbedtls_time_t tt; |
Manuel Pégourié-Gonnard | 57e10d7 | 2015-06-22 18:59:21 +0200 | [diff] [blame] | 966 | int ret = 0; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 967 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 968 | tt = mbedtls_time(NULL); |
| 969 | lt = mbedtls_platform_gmtime_r(&tt, &tm_buf); |
Manuel Pégourié-Gonnard | 864108d | 2015-05-29 10:11:03 +0200 | [diff] [blame] | 970 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 971 | if (lt == NULL) { |
Manuel Pégourié-Gonnard | 57e10d7 | 2015-06-22 18:59:21 +0200 | [diff] [blame] | 972 | ret = -1; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 973 | } else { |
Manuel Pégourié-Gonnard | 57e10d7 | 2015-06-22 18:59:21 +0200 | [diff] [blame] | 974 | now->year = lt->tm_year + 1900; |
| 975 | now->mon = lt->tm_mon + 1; |
| 976 | now->day = lt->tm_mday; |
| 977 | now->hour = lt->tm_hour; |
| 978 | now->min = lt->tm_min; |
| 979 | now->sec = lt->tm_sec; |
| 980 | } |
Manuel Pégourié-Gonnard | 864108d | 2015-05-29 10:11:03 +0200 | [diff] [blame] | 981 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 982 | return ret; |
Manuel Pégourié-Gonnard | 6304f78 | 2014-03-10 12:26:11 +0100 | [diff] [blame] | 983 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 984 | |
Manuel Pégourié-Gonnard | 6304f78 | 2014-03-10 12:26:11 +0100 | [diff] [blame] | 985 | /* |
| 986 | * Return 0 if before <= after, 1 otherwise |
| 987 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 988 | static int x509_check_time(const mbedtls_x509_time *before, const mbedtls_x509_time *after) |
Manuel Pégourié-Gonnard | 6304f78 | 2014-03-10 12:26:11 +0100 | [diff] [blame] | 989 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 990 | if (before->year > after->year) { |
| 991 | return 1; |
| 992 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 993 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 994 | if (before->year == after->year && |
| 995 | before->mon > after->mon) { |
| 996 | return 1; |
| 997 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 998 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 999 | if (before->year == after->year && |
Manuel Pégourié-Gonnard | 6304f78 | 2014-03-10 12:26:11 +0100 | [diff] [blame] | 1000 | before->mon == after->mon && |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1001 | before->day > after->day) { |
| 1002 | return 1; |
| 1003 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1004 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1005 | if (before->year == after->year && |
Manuel Pégourié-Gonnard | 6304f78 | 2014-03-10 12:26:11 +0100 | [diff] [blame] | 1006 | before->mon == after->mon && |
| 1007 | before->day == after->day && |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1008 | before->hour > after->hour) { |
| 1009 | return 1; |
| 1010 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1011 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1012 | if (before->year == after->year && |
Manuel Pégourié-Gonnard | 6304f78 | 2014-03-10 12:26:11 +0100 | [diff] [blame] | 1013 | before->mon == after->mon && |
| 1014 | before->day == after->day && |
| 1015 | before->hour == after->hour && |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1016 | before->min > after->min) { |
| 1017 | return 1; |
| 1018 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1019 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1020 | if (before->year == after->year && |
Manuel Pégourié-Gonnard | 6304f78 | 2014-03-10 12:26:11 +0100 | [diff] [blame] | 1021 | before->mon == after->mon && |
| 1022 | before->day == after->day && |
| 1023 | before->hour == after->hour && |
| 1024 | before->min == after->min && |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1025 | before->sec > after->sec) { |
| 1026 | return 1; |
| 1027 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1028 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1029 | return 0; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1030 | } |
Manuel Pégourié-Gonnard | 6304f78 | 2014-03-10 12:26:11 +0100 | [diff] [blame] | 1031 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1032 | int mbedtls_x509_time_is_past(const mbedtls_x509_time *to) |
Manuel Pégourié-Gonnard | 6304f78 | 2014-03-10 12:26:11 +0100 | [diff] [blame] | 1033 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1034 | mbedtls_x509_time now; |
Manuel Pégourié-Gonnard | 6304f78 | 2014-03-10 12:26:11 +0100 | [diff] [blame] | 1035 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1036 | if (x509_get_current_time(&now) != 0) { |
| 1037 | return 1; |
| 1038 | } |
Manuel Pégourié-Gonnard | 6304f78 | 2014-03-10 12:26:11 +0100 | [diff] [blame] | 1039 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1040 | return x509_check_time(&now, to); |
Manuel Pégourié-Gonnard | 6304f78 | 2014-03-10 12:26:11 +0100 | [diff] [blame] | 1041 | } |
| 1042 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1043 | int mbedtls_x509_time_is_future(const mbedtls_x509_time *from) |
Manuel Pégourié-Gonnard | 6304f78 | 2014-03-10 12:26:11 +0100 | [diff] [blame] | 1044 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1045 | mbedtls_x509_time now; |
Manuel Pégourié-Gonnard | 6304f78 | 2014-03-10 12:26:11 +0100 | [diff] [blame] | 1046 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1047 | if (x509_get_current_time(&now) != 0) { |
| 1048 | return 1; |
| 1049 | } |
Manuel Pégourié-Gonnard | 6304f78 | 2014-03-10 12:26:11 +0100 | [diff] [blame] | 1050 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1051 | return x509_check_time(from, &now); |
Manuel Pégourié-Gonnard | 6304f78 | 2014-03-10 12:26:11 +0100 | [diff] [blame] | 1052 | } |
| 1053 | |
Manuel Pégourié-Gonnard | 60c793b | 2015-06-18 20:52:58 +0200 | [diff] [blame] | 1054 | #else /* MBEDTLS_HAVE_TIME_DATE */ |
Manuel Pégourié-Gonnard | 6304f78 | 2014-03-10 12:26:11 +0100 | [diff] [blame] | 1055 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1056 | int mbedtls_x509_time_is_past(const mbedtls_x509_time *to) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1057 | { |
| 1058 | ((void) to); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1059 | return 0; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1060 | } |
Manuel Pégourié-Gonnard | 6304f78 | 2014-03-10 12:26:11 +0100 | [diff] [blame] | 1061 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1062 | int mbedtls_x509_time_is_future(const mbedtls_x509_time *from) |
Manuel Pégourié-Gonnard | 6304f78 | 2014-03-10 12:26:11 +0100 | [diff] [blame] | 1063 | { |
| 1064 | ((void) from); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1065 | return 0; |
Manuel Pégourié-Gonnard | 6304f78 | 2014-03-10 12:26:11 +0100 | [diff] [blame] | 1066 | } |
Manuel Pégourié-Gonnard | 60c793b | 2015-06-18 20:52:58 +0200 | [diff] [blame] | 1067 | #endif /* MBEDTLS_HAVE_TIME_DATE */ |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1068 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1069 | #if defined(MBEDTLS_SELF_TEST) |
Paul Bakker | e9e6ae3 | 2013-09-16 22:53:25 +0200 | [diff] [blame] | 1070 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 1071 | #include "mbedtls/x509_crt.h" |
| 1072 | #include "mbedtls/certs.h" |
Paul Bakker | e9e6ae3 | 2013-09-16 22:53:25 +0200 | [diff] [blame] | 1073 | |
| 1074 | /* |
| 1075 | * Checkup routine |
| 1076 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1077 | int mbedtls_x509_self_test(int verbose) |
Paul Bakker | e9e6ae3 | 2013-09-16 22:53:25 +0200 | [diff] [blame] | 1078 | { |
Junhwan Park | 39bdab7 | 2018-10-17 21:01:08 +0900 | [diff] [blame] | 1079 | int ret = 0; |
Gilles Peskine | 750c353 | 2017-05-05 18:56:30 +0200 | [diff] [blame] | 1080 | #if defined(MBEDTLS_CERTS_C) && defined(MBEDTLS_SHA256_C) |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 1081 | uint32_t flags; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1082 | mbedtls_x509_crt cacert; |
| 1083 | mbedtls_x509_crt clicert; |
Paul Bakker | e9e6ae3 | 2013-09-16 22:53:25 +0200 | [diff] [blame] | 1084 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1085 | if (verbose != 0) { |
| 1086 | mbedtls_printf(" X.509 certificate load: "); |
| 1087 | } |
Paul Bakker | e9e6ae3 | 2013-09-16 22:53:25 +0200 | [diff] [blame] | 1088 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1089 | mbedtls_x509_crt_init(&cacert); |
| 1090 | mbedtls_x509_crt_init(&clicert); |
Paul Bakker | e9e6ae3 | 2013-09-16 22:53:25 +0200 | [diff] [blame] | 1091 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1092 | ret = mbedtls_x509_crt_parse(&clicert, (const unsigned char *) mbedtls_test_cli_crt, |
| 1093 | mbedtls_test_cli_crt_len); |
| 1094 | if (ret != 0) { |
| 1095 | if (verbose != 0) { |
| 1096 | mbedtls_printf("failed\n"); |
| 1097 | } |
Paul Bakker | e9e6ae3 | 2013-09-16 22:53:25 +0200 | [diff] [blame] | 1098 | |
Junhwan Park | 39bdab7 | 2018-10-17 21:01:08 +0900 | [diff] [blame] | 1099 | goto cleanup; |
Paul Bakker | e9e6ae3 | 2013-09-16 22:53:25 +0200 | [diff] [blame] | 1100 | } |
| 1101 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1102 | ret = mbedtls_x509_crt_parse(&cacert, (const unsigned char *) mbedtls_test_ca_crt, |
| 1103 | mbedtls_test_ca_crt_len); |
| 1104 | if (ret != 0) { |
| 1105 | if (verbose != 0) { |
| 1106 | mbedtls_printf("failed\n"); |
| 1107 | } |
Paul Bakker | e9e6ae3 | 2013-09-16 22:53:25 +0200 | [diff] [blame] | 1108 | |
Junhwan Park | 39bdab7 | 2018-10-17 21:01:08 +0900 | [diff] [blame] | 1109 | goto cleanup; |
Paul Bakker | e9e6ae3 | 2013-09-16 22:53:25 +0200 | [diff] [blame] | 1110 | } |
| 1111 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1112 | if (verbose != 0) { |
| 1113 | mbedtls_printf("passed\n X.509 signature verify: "); |
| 1114 | } |
Paul Bakker | e9e6ae3 | 2013-09-16 22:53:25 +0200 | [diff] [blame] | 1115 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1116 | ret = mbedtls_x509_crt_verify(&clicert, &cacert, NULL, NULL, &flags, NULL, NULL); |
| 1117 | if (ret != 0) { |
| 1118 | if (verbose != 0) { |
| 1119 | mbedtls_printf("failed\n"); |
| 1120 | } |
Paul Bakker | e9e6ae3 | 2013-09-16 22:53:25 +0200 | [diff] [blame] | 1121 | |
Junhwan Park | 39bdab7 | 2018-10-17 21:01:08 +0900 | [diff] [blame] | 1122 | goto cleanup; |
Paul Bakker | e9e6ae3 | 2013-09-16 22:53:25 +0200 | [diff] [blame] | 1123 | } |
| 1124 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1125 | if (verbose != 0) { |
| 1126 | mbedtls_printf("passed\n\n"); |
| 1127 | } |
Paul Bakker | e9e6ae3 | 2013-09-16 22:53:25 +0200 | [diff] [blame] | 1128 | |
Junhwan Park | 39bdab7 | 2018-10-17 21:01:08 +0900 | [diff] [blame] | 1129 | cleanup: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1130 | mbedtls_x509_crt_free(&cacert); |
| 1131 | mbedtls_x509_crt_free(&clicert); |
Paul Bakker | e9e6ae3 | 2013-09-16 22:53:25 +0200 | [diff] [blame] | 1132 | #else |
| 1133 | ((void) verbose); |
Simon Butcher | 3aa6405 | 2020-03-27 16:55:35 +0000 | [diff] [blame] | 1134 | #endif /* MBEDTLS_CERTS_C && MBEDTLS_SHA256_C */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1135 | return ret; |
Paul Bakker | e9e6ae3 | 2013-09-16 22:53:25 +0200 | [diff] [blame] | 1136 | } |
| 1137 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1138 | #endif /* MBEDTLS_SELF_TEST */ |
Paul Bakker | e9e6ae3 | 2013-09-16 22:53:25 +0200 | [diff] [blame] | 1139 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1140 | #endif /* MBEDTLS_X509_USE_C */ |