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 certificate 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 |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 5 | * 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 Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 18 | */ |
| 19 | /* |
| 20 | * The ITU-T X.509 standard defines a certificate format for PKI. |
| 21 | * |
Manuel Pégourié-Gonnard | 1c082f3 | 2014-06-12 22:34:55 +0200 | [diff] [blame] | 22 | * 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 Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 25 | * |
| 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 |
Manuel Pégourié-Gonnard | 2f09d59 | 2017-07-03 18:30:43 +0200 | [diff] [blame] | 28 | * |
| 29 | * [SIRO] https://cabforum.org/wp-content/uploads/Chunghwatelecom201503cabforumV4.pdf |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 30 | */ |
| 31 | |
Gilles Peskine | db09ef6 | 2020-06-03 01:43:33 +0200 | [diff] [blame] | 32 | #include "common.h" |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 33 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 34 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 35 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 36 | #include "mbedtls/x509_crt.h" |
Janos Follath | 73c616b | 2019-12-18 15:07:04 +0000 | [diff] [blame] | 37 | #include "mbedtls/error.h" |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 38 | #include "mbedtls/oid.h" |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 39 | #include "mbedtls/platform_util.h" |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 40 | |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 41 | #include <string.h> |
| 42 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 43 | #if defined(MBEDTLS_PEM_PARSE_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 44 | #include "mbedtls/pem.h" |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 45 | #endif |
| 46 | |
Andrzej Kurek | d4a6553 | 2018-10-31 06:18:39 -0400 | [diff] [blame] | 47 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 48 | #include "psa/crypto.h" |
| 49 | #include "mbedtls/psa_util.h" |
pespacek | 7599a77 | 2022-02-07 14:40:55 +0100 | [diff] [blame] | 50 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Przemek Stekiel | 40afdd2 | 2022-09-06 13:08:28 +0200 | [diff] [blame] | 51 | #include "hash_info.h" |
Andrzej Kurek | d4a6553 | 2018-10-31 06:18:39 -0400 | [diff] [blame] | 52 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 53 | #include "mbedtls/platform.h" |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 54 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 55 | #if defined(MBEDTLS_THREADING_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 56 | #include "mbedtls/threading.h" |
Manuel Pégourié-Gonnard | 5ad68e4 | 2013-11-28 17:11:54 +0100 | [diff] [blame] | 57 | #endif |
| 58 | |
Daniel Axtens | f071024 | 2020-05-28 11:43:41 +1000 | [diff] [blame] | 59 | #if defined(MBEDTLS_HAVE_TIME) |
Paul Bakker | fa6a620 | 2013-10-28 18:48:30 +0100 | [diff] [blame] | 60 | #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 61 | #include <windows.h> |
| 62 | #else |
| 63 | #include <time.h> |
| 64 | #endif |
Daniel Axtens | f071024 | 2020-05-28 11:43:41 +1000 | [diff] [blame] | 65 | #endif |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 66 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 67 | #if defined(MBEDTLS_FS_IO) |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 68 | #include <stdio.h> |
Paul Bakker | 5ff3f91 | 2014-04-04 15:08:20 +0200 | [diff] [blame] | 69 | #if !defined(_WIN32) || defined(EFIX64) || defined(EFI32) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 70 | #include <sys/types.h> |
| 71 | #include <sys/stat.h> |
Martino Facchin | 0ec05ec | 2021-05-04 11:47:36 +0200 | [diff] [blame] | 72 | #if defined(__MBED__) |
| 73 | #include <platform/mbed_retarget.h> |
| 74 | #else |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 75 | #include <dirent.h> |
Martino Facchin | 0ec05ec | 2021-05-04 11:47:36 +0200 | [diff] [blame] | 76 | #endif /* __MBED__ */ |
Eduardo Silva | e1bfffc | 2019-04-25 10:43:26 -0600 | [diff] [blame] | 77 | #include <errno.h> |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 78 | #endif /* !_WIN32 || EFIX64 || EFI32 */ |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 79 | #endif |
| 80 | |
Manuel Pégourié-Gonnard | c547d1a | 2017-07-05 13:28:45 +0200 | [diff] [blame] | 81 | /* |
| 82 | * Item in a verification chain: cert and flags for it |
| 83 | */ |
| 84 | typedef struct { |
| 85 | mbedtls_x509_crt *crt; |
| 86 | uint32_t flags; |
| 87 | } x509_crt_verify_chain_item; |
| 88 | |
| 89 | /* |
| 90 | * Max size of verification chain: end-entity + intermediates + trusted root |
| 91 | */ |
| 92 | #define X509_MAX_VERIFY_CHAIN_SIZE ( MBEDTLS_X509_MAX_INTERMEDIATE_CA + 2 ) |
Paul Bakker | 3461772 | 2014-06-13 17:20:13 +0200 | [diff] [blame] | 93 | |
Gilles Peskine | ffb92da | 2021-06-02 00:03:26 +0200 | [diff] [blame] | 94 | /* Default profile. Do not remove items unless there are serious security |
| 95 | * concerns. */ |
Manuel Pégourié-Gonnard | 88db5da | 2015-06-15 14:34:59 +0200 | [diff] [blame] | 96 | const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default = |
| 97 | { |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 98 | /* Hashes from SHA-256 and above. Note that this selection |
| 99 | * should be aligned with ssl_preset_default_hashes in ssl_tls.c. */ |
Manuel Pégourié-Gonnard | f8ea856 | 2015-06-15 15:33:19 +0200 | [diff] [blame] | 100 | MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) | |
| 101 | MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) | |
| 102 | MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ), |
| 103 | 0xFFFFFFF, /* Any PK alg */ |
| 104 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 105 | /* Curves at or above 128-bit security level. Note that this selection |
| 106 | * should be aligned with ssl_preset_default_curves in ssl_tls.c. */ |
Manuel Pégourié-Gonnard | f8ea856 | 2015-06-15 15:33:19 +0200 | [diff] [blame] | 107 | MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) | |
| 108 | MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ) | |
| 109 | MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP521R1 ) | |
| 110 | MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP256R1 ) | |
| 111 | MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP384R1 ) | |
| 112 | MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP512R1 ) | |
Gilles Peskine | 3995750 | 2021-06-17 23:17:52 +0200 | [diff] [blame] | 113 | 0, |
Manuel Pégourié-Gonnard | f8ea856 | 2015-06-15 15:33:19 +0200 | [diff] [blame] | 114 | #else |
| 115 | 0, |
| 116 | #endif |
Manuel Pégourié-Gonnard | 88db5da | 2015-06-15 14:34:59 +0200 | [diff] [blame] | 117 | 2048, |
| 118 | }; |
| 119 | |
Gilles Peskine | ffb92da | 2021-06-02 00:03:26 +0200 | [diff] [blame] | 120 | /* Next-generation profile. Currently identical to the default, but may |
| 121 | * be tightened at any time. */ |
| 122 | const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next = |
Gilles Peskine | 2c69fa2 | 2021-06-02 00:33:33 +0200 | [diff] [blame] | 123 | { |
| 124 | /* Hashes from SHA-256 and above. */ |
| 125 | MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) | |
| 126 | MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) | |
| 127 | MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ), |
| 128 | 0xFFFFFFF, /* Any PK alg */ |
| 129 | #if defined(MBEDTLS_ECP_C) |
| 130 | /* Curves at or above 128-bit security level. */ |
| 131 | MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) | |
| 132 | MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ) | |
| 133 | MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP521R1 ) | |
| 134 | MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP256R1 ) | |
| 135 | MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP384R1 ) | |
| 136 | MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP512R1 ) | |
| 137 | MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256K1 ), |
| 138 | #else |
| 139 | 0, |
| 140 | #endif |
| 141 | 2048, |
| 142 | }; |
Gilles Peskine | ffb92da | 2021-06-02 00:03:26 +0200 | [diff] [blame] | 143 | |
Manuel Pégourié-Gonnard | 88db5da | 2015-06-15 14:34:59 +0200 | [diff] [blame] | 144 | /* |
| 145 | * NSA Suite B Profile |
| 146 | */ |
Manuel Pégourié-Gonnard | 88db5da | 2015-06-15 14:34:59 +0200 | [diff] [blame] | 147 | const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb = |
| 148 | { |
Manuel Pégourié-Gonnard | f8ea856 | 2015-06-15 15:33:19 +0200 | [diff] [blame] | 149 | /* Only SHA-256 and 384 */ |
| 150 | MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) | |
| 151 | MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ), |
| 152 | /* Only ECDSA */ |
Ron Eldor | 85e1dcf | 2018-02-06 15:59:38 +0200 | [diff] [blame] | 153 | MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_ECDSA ) | |
| 154 | MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_ECKEY ), |
Manuel Pégourié-Gonnard | f8ea856 | 2015-06-15 15:33:19 +0200 | [diff] [blame] | 155 | #if defined(MBEDTLS_ECP_C) |
| 156 | /* Only NIST P-256 and P-384 */ |
| 157 | MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) | |
| 158 | MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ), |
| 159 | #else |
| 160 | 0, |
| 161 | #endif |
| 162 | 0, |
Manuel Pégourié-Gonnard | 88db5da | 2015-06-15 14:34:59 +0200 | [diff] [blame] | 163 | }; |
| 164 | |
| 165 | /* |
Manuel Pégourié-Gonnard | 9d4c2c4 | 2021-06-18 09:48:27 +0200 | [diff] [blame] | 166 | * Empty / all-forbidden profile |
| 167 | */ |
| 168 | const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_none = |
| 169 | { |
| 170 | 0, |
| 171 | 0, |
| 172 | 0, |
| 173 | (uint32_t) -1, |
| 174 | }; |
| 175 | |
| 176 | /* |
Manuel Pégourié-Gonnard | cbb1f6e | 2015-06-15 16:17:55 +0200 | [diff] [blame] | 177 | * Check md_alg against profile |
Manuel Pégourié-Gonnard | 3f81691 | 2017-10-26 10:24:16 +0200 | [diff] [blame] | 178 | * Return 0 if md_alg is acceptable for this profile, -1 otherwise |
Manuel Pégourié-Gonnard | cbb1f6e | 2015-06-15 16:17:55 +0200 | [diff] [blame] | 179 | */ |
| 180 | static int x509_profile_check_md_alg( const mbedtls_x509_crt_profile *profile, |
| 181 | mbedtls_md_type_t md_alg ) |
| 182 | { |
Philippe Antoine | b5b2543 | 2018-05-11 11:06:29 +0200 | [diff] [blame] | 183 | if( md_alg == MBEDTLS_MD_NONE ) |
| 184 | return( -1 ); |
| 185 | |
Manuel Pégourié-Gonnard | cbb1f6e | 2015-06-15 16:17:55 +0200 | [diff] [blame] | 186 | if( ( profile->allowed_mds & MBEDTLS_X509_ID_FLAG( md_alg ) ) != 0 ) |
| 187 | return( 0 ); |
| 188 | |
| 189 | return( -1 ); |
| 190 | } |
| 191 | |
| 192 | /* |
| 193 | * Check pk_alg against profile |
Manuel Pégourié-Gonnard | 3f81691 | 2017-10-26 10:24:16 +0200 | [diff] [blame] | 194 | * Return 0 if pk_alg is acceptable for this profile, -1 otherwise |
Manuel Pégourié-Gonnard | cbb1f6e | 2015-06-15 16:17:55 +0200 | [diff] [blame] | 195 | */ |
| 196 | static int x509_profile_check_pk_alg( const mbedtls_x509_crt_profile *profile, |
| 197 | mbedtls_pk_type_t pk_alg ) |
| 198 | { |
Philippe Antoine | b5b2543 | 2018-05-11 11:06:29 +0200 | [diff] [blame] | 199 | if( pk_alg == MBEDTLS_PK_NONE ) |
| 200 | return( -1 ); |
| 201 | |
Manuel Pégourié-Gonnard | cbb1f6e | 2015-06-15 16:17:55 +0200 | [diff] [blame] | 202 | if( ( profile->allowed_pks & MBEDTLS_X509_ID_FLAG( pk_alg ) ) != 0 ) |
| 203 | return( 0 ); |
| 204 | |
| 205 | return( -1 ); |
| 206 | } |
| 207 | |
| 208 | /* |
| 209 | * Check key against profile |
Manuel Pégourié-Gonnard | 3f81691 | 2017-10-26 10:24:16 +0200 | [diff] [blame] | 210 | * Return 0 if pk is acceptable for this profile, -1 otherwise |
Manuel Pégourié-Gonnard | cbb1f6e | 2015-06-15 16:17:55 +0200 | [diff] [blame] | 211 | */ |
| 212 | static int x509_profile_check_key( const mbedtls_x509_crt_profile *profile, |
Manuel Pégourié-Gonnard | cbb1f6e | 2015-06-15 16:17:55 +0200 | [diff] [blame] | 213 | const mbedtls_pk_context *pk ) |
| 214 | { |
Manuel Pégourié-Gonnard | 3f81691 | 2017-10-26 10:24:16 +0200 | [diff] [blame] | 215 | const mbedtls_pk_type_t pk_alg = mbedtls_pk_get_type( pk ); |
Manuel Pégourié-Gonnard | 19773ff | 2017-10-24 10:51:26 +0200 | [diff] [blame] | 216 | |
Manuel Pégourié-Gonnard | cbb1f6e | 2015-06-15 16:17:55 +0200 | [diff] [blame] | 217 | #if defined(MBEDTLS_RSA_C) |
| 218 | if( pk_alg == MBEDTLS_PK_RSA || pk_alg == MBEDTLS_PK_RSASSA_PSS ) |
| 219 | { |
Manuel Pégourié-Gonnard | 097c7bb | 2015-06-18 16:43:38 +0200 | [diff] [blame] | 220 | if( mbedtls_pk_get_bitlen( pk ) >= profile->rsa_min_bitlen ) |
Manuel Pégourié-Gonnard | cbb1f6e | 2015-06-15 16:17:55 +0200 | [diff] [blame] | 221 | return( 0 ); |
| 222 | |
| 223 | return( -1 ); |
| 224 | } |
| 225 | #endif |
| 226 | |
Manuel Pégourié-Gonnard | 65eefc8 | 2015-10-23 14:08:48 +0200 | [diff] [blame] | 227 | #if defined(MBEDTLS_ECP_C) |
| 228 | if( pk_alg == MBEDTLS_PK_ECDSA || |
| 229 | pk_alg == MBEDTLS_PK_ECKEY || |
| 230 | pk_alg == MBEDTLS_PK_ECKEY_DH ) |
Manuel Pégourié-Gonnard | cbb1f6e | 2015-06-15 16:17:55 +0200 | [diff] [blame] | 231 | { |
Manuel Pégourié-Gonnard | 3f81691 | 2017-10-26 10:24:16 +0200 | [diff] [blame] | 232 | const mbedtls_ecp_group_id gid = mbedtls_pk_ec( *pk )->grp.id; |
Manuel Pégourié-Gonnard | cbb1f6e | 2015-06-15 16:17:55 +0200 | [diff] [blame] | 233 | |
Philippe Antoine | b5b2543 | 2018-05-11 11:06:29 +0200 | [diff] [blame] | 234 | if( gid == MBEDTLS_ECP_DP_NONE ) |
| 235 | return( -1 ); |
| 236 | |
Manuel Pégourié-Gonnard | cbb1f6e | 2015-06-15 16:17:55 +0200 | [diff] [blame] | 237 | if( ( profile->allowed_curves & MBEDTLS_X509_ID_FLAG( gid ) ) != 0 ) |
| 238 | return( 0 ); |
| 239 | |
| 240 | return( -1 ); |
| 241 | } |
| 242 | #endif |
| 243 | |
| 244 | return( -1 ); |
| 245 | } |
| 246 | |
| 247 | /* |
Hanno Becker | 1f8527f | 2018-11-02 09:19:16 +0000 | [diff] [blame] | 248 | * Like memcmp, but case-insensitive and always returns -1 if different |
| 249 | */ |
| 250 | static int x509_memcasecmp( const void *s1, const void *s2, size_t len ) |
| 251 | { |
| 252 | size_t i; |
| 253 | unsigned char diff; |
| 254 | const unsigned char *n1 = s1, *n2 = s2; |
| 255 | |
| 256 | for( i = 0; i < len; i++ ) |
| 257 | { |
| 258 | diff = n1[i] ^ n2[i]; |
| 259 | |
| 260 | if( diff == 0 ) |
| 261 | continue; |
| 262 | |
| 263 | if( diff == 32 && |
| 264 | ( ( n1[i] >= 'a' && n1[i] <= 'z' ) || |
| 265 | ( n1[i] >= 'A' && n1[i] <= 'Z' ) ) ) |
| 266 | { |
| 267 | continue; |
| 268 | } |
| 269 | |
| 270 | return( -1 ); |
| 271 | } |
| 272 | |
| 273 | return( 0 ); |
| 274 | } |
| 275 | |
| 276 | /* |
| 277 | * Return 0 if name matches wildcard, -1 otherwise |
| 278 | */ |
| 279 | static int x509_check_wildcard( const char *cn, const mbedtls_x509_buf *name ) |
| 280 | { |
| 281 | size_t i; |
| 282 | size_t cn_idx = 0, cn_len = strlen( cn ); |
| 283 | |
| 284 | /* We can't have a match if there is no wildcard to match */ |
| 285 | if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' ) |
| 286 | return( -1 ); |
| 287 | |
| 288 | for( i = 0; i < cn_len; ++i ) |
| 289 | { |
| 290 | if( cn[i] == '.' ) |
| 291 | { |
| 292 | cn_idx = i; |
| 293 | break; |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | if( cn_idx == 0 ) |
| 298 | return( -1 ); |
| 299 | |
| 300 | if( cn_len - cn_idx == name->len - 1 && |
| 301 | x509_memcasecmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 ) |
| 302 | { |
| 303 | return( 0 ); |
| 304 | } |
| 305 | |
| 306 | return( -1 ); |
| 307 | } |
| 308 | |
| 309 | /* |
| 310 | * Compare two X.509 strings, case-insensitive, and allowing for some encoding |
| 311 | * variations (but not all). |
| 312 | * |
| 313 | * Return 0 if equal, -1 otherwise. |
| 314 | */ |
| 315 | static int x509_string_cmp( const mbedtls_x509_buf *a, const mbedtls_x509_buf *b ) |
| 316 | { |
| 317 | if( a->tag == b->tag && |
| 318 | a->len == b->len && |
| 319 | memcmp( a->p, b->p, b->len ) == 0 ) |
| 320 | { |
| 321 | return( 0 ); |
| 322 | } |
| 323 | |
| 324 | if( ( a->tag == MBEDTLS_ASN1_UTF8_STRING || a->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) && |
| 325 | ( b->tag == MBEDTLS_ASN1_UTF8_STRING || b->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) && |
| 326 | a->len == b->len && |
| 327 | x509_memcasecmp( a->p, b->p, b->len ) == 0 ) |
| 328 | { |
| 329 | return( 0 ); |
| 330 | } |
| 331 | |
| 332 | return( -1 ); |
| 333 | } |
| 334 | |
| 335 | /* |
| 336 | * Compare two X.509 Names (aka rdnSequence). |
| 337 | * |
| 338 | * See RFC 5280 section 7.1, though we don't implement the whole algorithm: |
| 339 | * we sometimes return unequal when the full algorithm would return equal, |
| 340 | * but never the other way. (In particular, we don't do Unicode normalisation |
| 341 | * or space folding.) |
| 342 | * |
| 343 | * Return 0 if equal, -1 otherwise. |
| 344 | */ |
| 345 | static int x509_name_cmp( const mbedtls_x509_name *a, const mbedtls_x509_name *b ) |
| 346 | { |
| 347 | /* Avoid recursion, it might not be optimised by the compiler */ |
| 348 | while( a != NULL || b != NULL ) |
| 349 | { |
| 350 | if( a == NULL || b == NULL ) |
| 351 | return( -1 ); |
| 352 | |
| 353 | /* type */ |
| 354 | if( a->oid.tag != b->oid.tag || |
| 355 | a->oid.len != b->oid.len || |
| 356 | memcmp( a->oid.p, b->oid.p, b->oid.len ) != 0 ) |
| 357 | { |
| 358 | return( -1 ); |
| 359 | } |
| 360 | |
| 361 | /* value */ |
| 362 | if( x509_string_cmp( &a->val, &b->val ) != 0 ) |
| 363 | return( -1 ); |
| 364 | |
| 365 | /* structure of the list of sets */ |
| 366 | if( a->next_merged != b->next_merged ) |
| 367 | return( -1 ); |
| 368 | |
| 369 | a = a->next; |
| 370 | b = b->next; |
| 371 | } |
| 372 | |
| 373 | /* a == NULL == b */ |
| 374 | return( 0 ); |
| 375 | } |
| 376 | |
| 377 | /* |
Manuel Pégourié-Gonnard | 83e923b | 2017-08-23 10:55:41 +0200 | [diff] [blame] | 378 | * Reset (init or clear) a verify_chain |
| 379 | */ |
| 380 | static void x509_crt_verify_chain_reset( |
| 381 | mbedtls_x509_crt_verify_chain *ver_chain ) |
| 382 | { |
| 383 | size_t i; |
| 384 | |
| 385 | for( i = 0; i < MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE; i++ ) |
| 386 | { |
| 387 | ver_chain->items[i].crt = NULL; |
Hanno Becker | a9375b3 | 2019-01-10 09:19:26 +0000 | [diff] [blame] | 388 | ver_chain->items[i].flags = (uint32_t) -1; |
Manuel Pégourié-Gonnard | 83e923b | 2017-08-23 10:55:41 +0200 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | ver_chain->len = 0; |
Hanno Becker | f53893b | 2019-03-28 13:45:55 +0000 | [diff] [blame] | 392 | |
| 393 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 394 | ver_chain->trust_ca_cb_result = NULL; |
| 395 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
Manuel Pégourié-Gonnard | 83e923b | 2017-08-23 10:55:41 +0200 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | /* |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 399 | * Version ::= INTEGER { v1(0), v2(1), v3(2) } |
| 400 | */ |
| 401 | static int x509_get_version( unsigned char **p, |
| 402 | const unsigned char *end, |
| 403 | int *ver ) |
| 404 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 405 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 406 | size_t len; |
| 407 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 408 | if( ( ret = mbedtls_asn1_get_tag( p, end, &len, |
| 409 | MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) != 0 ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 410 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 411 | if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 412 | { |
| 413 | *ver = 0; |
| 414 | return( 0 ); |
| 415 | } |
| 416 | |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 417 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | end = *p + len; |
| 421 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 422 | if( ( ret = mbedtls_asn1_get_int( p, end, ver ) ) != 0 ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 423 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_VERSION, ret ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 424 | |
| 425 | if( *p != end ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 426 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_VERSION, |
| 427 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 428 | |
| 429 | return( 0 ); |
| 430 | } |
| 431 | |
| 432 | /* |
| 433 | * Validity ::= SEQUENCE { |
| 434 | * notBefore Time, |
| 435 | * notAfter Time } |
| 436 | */ |
| 437 | static int x509_get_dates( unsigned char **p, |
| 438 | const unsigned char *end, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 439 | mbedtls_x509_time *from, |
| 440 | mbedtls_x509_time *to ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 441 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 442 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 443 | size_t len; |
| 444 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 445 | if( ( ret = mbedtls_asn1_get_tag( p, end, &len, |
| 446 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 447 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_DATE, ret ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 448 | |
| 449 | end = *p + len; |
| 450 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 451 | if( ( ret = mbedtls_x509_get_time( p, end, from ) ) != 0 ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 452 | return( ret ); |
| 453 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 454 | if( ( ret = mbedtls_x509_get_time( p, end, to ) ) != 0 ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 455 | return( ret ); |
| 456 | |
| 457 | if( *p != end ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 458 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_DATE, |
| 459 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 460 | |
| 461 | return( 0 ); |
| 462 | } |
| 463 | |
| 464 | /* |
| 465 | * X.509 v2/v3 unique identifier (not parsed) |
| 466 | */ |
| 467 | static int x509_get_uid( unsigned char **p, |
| 468 | const unsigned char *end, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 469 | mbedtls_x509_buf *uid, int n ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 470 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 471 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 472 | |
| 473 | if( *p == end ) |
| 474 | return( 0 ); |
| 475 | |
| 476 | uid->tag = **p; |
| 477 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 478 | if( ( ret = mbedtls_asn1_get_tag( p, end, &uid->len, |
| 479 | MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | n ) ) != 0 ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 480 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 481 | if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 482 | return( 0 ); |
| 483 | |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 484 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | uid->p = *p; |
| 488 | *p += uid->len; |
| 489 | |
| 490 | return( 0 ); |
| 491 | } |
| 492 | |
| 493 | static int x509_get_basic_constraints( unsigned char **p, |
| 494 | const unsigned char *end, |
| 495 | int *ca_istrue, |
| 496 | int *max_pathlen ) |
| 497 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 498 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 499 | size_t len; |
| 500 | |
| 501 | /* |
| 502 | * BasicConstraints ::= SEQUENCE { |
| 503 | * cA BOOLEAN DEFAULT FALSE, |
| 504 | * pathLenConstraint INTEGER (0..MAX) OPTIONAL } |
| 505 | */ |
| 506 | *ca_istrue = 0; /* DEFAULT FALSE */ |
| 507 | *max_pathlen = 0; /* endless */ |
| 508 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 509 | if( ( ret = mbedtls_asn1_get_tag( p, end, &len, |
| 510 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 511 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 512 | |
| 513 | if( *p == end ) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 514 | return( 0 ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 515 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 516 | if( ( ret = mbedtls_asn1_get_bool( p, end, ca_istrue ) ) != 0 ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 517 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 518 | if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) |
| 519 | ret = mbedtls_asn1_get_int( p, end, ca_istrue ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 520 | |
| 521 | if( ret != 0 ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 522 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 523 | |
| 524 | if( *ca_istrue != 0 ) |
| 525 | *ca_istrue = 1; |
| 526 | } |
| 527 | |
| 528 | if( *p == end ) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 529 | return( 0 ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 530 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 531 | if( ( ret = mbedtls_asn1_get_int( p, end, max_pathlen ) ) != 0 ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 532 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 533 | |
| 534 | if( *p != end ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 535 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, |
| 536 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 537 | |
Andrzej Kurek | 1605074 | 2020-04-14 09:49:52 -0400 | [diff] [blame] | 538 | /* Do not accept max_pathlen equal to INT_MAX to avoid a signed integer |
| 539 | * overflow, which is an undefined behavior. */ |
| 540 | if( *max_pathlen == INT_MAX ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 541 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, |
| 542 | MBEDTLS_ERR_ASN1_INVALID_LENGTH ) ); |
Andrzej Kurek | 1605074 | 2020-04-14 09:49:52 -0400 | [diff] [blame] | 543 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 544 | (*max_pathlen)++; |
| 545 | |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 546 | return( 0 ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 547 | } |
| 548 | |
| 549 | static int x509_get_ns_cert_type( unsigned char **p, |
| 550 | const unsigned char *end, |
| 551 | unsigned char *ns_cert_type) |
| 552 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 553 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 554 | mbedtls_x509_bitstring bs = { 0, 0, NULL }; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 555 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 556 | if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 557 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 558 | |
| 559 | if( bs.len != 1 ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 560 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, |
| 561 | MBEDTLS_ERR_ASN1_INVALID_LENGTH ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 562 | |
| 563 | /* Get actual bitstring */ |
| 564 | *ns_cert_type = *bs.p; |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 565 | return( 0 ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | static int x509_get_key_usage( unsigned char **p, |
| 569 | const unsigned char *end, |
Manuel Pégourié-Gonnard | 1d0ca1a | 2015-03-27 16:50:00 +0100 | [diff] [blame] | 570 | unsigned int *key_usage) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 571 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 572 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 9a70225 | 2015-06-23 10:14:36 +0200 | [diff] [blame] | 573 | size_t i; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 574 | mbedtls_x509_bitstring bs = { 0, 0, NULL }; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 575 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 576 | if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 577 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 578 | |
| 579 | if( bs.len < 1 ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 580 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, |
| 581 | MBEDTLS_ERR_ASN1_INVALID_LENGTH ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 582 | |
| 583 | /* Get actual bitstring */ |
Manuel Pégourié-Gonnard | 9a70225 | 2015-06-23 10:14:36 +0200 | [diff] [blame] | 584 | *key_usage = 0; |
| 585 | for( i = 0; i < bs.len && i < sizeof( unsigned int ); i++ ) |
| 586 | { |
| 587 | *key_usage |= (unsigned int) bs.p[i] << (8*i); |
| 588 | } |
| 589 | |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 590 | return( 0 ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | /* |
| 594 | * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId |
| 595 | * |
| 596 | * KeyPurposeId ::= OBJECT IDENTIFIER |
| 597 | */ |
| 598 | static int x509_get_ext_key_usage( unsigned char **p, |
| 599 | const unsigned char *end, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 600 | mbedtls_x509_sequence *ext_key_usage) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 601 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 602 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 603 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 604 | if( ( ret = mbedtls_asn1_get_sequence_of( p, end, ext_key_usage, MBEDTLS_ASN1_OID ) ) != 0 ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 605 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 606 | |
| 607 | /* Sequence length must be >= 1 */ |
| 608 | if( ext_key_usage->buf.p == NULL ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 609 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, |
| 610 | MBEDTLS_ERR_ASN1_INVALID_LENGTH ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 611 | |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 612 | return( 0 ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 613 | } |
| 614 | |
| 615 | /* |
toth92g | 1bbc2fe | 2021-02-12 16:11:17 +0100 | [diff] [blame] | 616 | * SubjectKeyIdentifier ::= KeyIdentifier |
| 617 | * |
| 618 | * KeyIdentifier ::= OCTET STRING |
| 619 | */ |
toth92g | dd12390 | 2021-04-08 10:12:52 +0200 | [diff] [blame^] | 620 | static int x509_get_subject_key_id( unsigned char** p, |
toth92g | 1bbc2fe | 2021-02-12 16:11:17 +0100 | [diff] [blame] | 621 | const unsigned char* end, |
toth92g | dd12390 | 2021-04-08 10:12:52 +0200 | [diff] [blame^] | 622 | mbedtls_x509_buf* subject_key_id ) |
toth92g | 1bbc2fe | 2021-02-12 16:11:17 +0100 | [diff] [blame] | 623 | { |
| 624 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 625 | size_t len = 0u; |
| 626 | |
toth92g | dd12390 | 2021-04-08 10:12:52 +0200 | [diff] [blame^] | 627 | if ( (ret = mbedtls_asn1_get_tag(p, end, &len, |
| 628 | MBEDTLS_ASN1_OCTET_STRING)) != 0 ) |
toth92g | 1bbc2fe | 2021-02-12 16:11:17 +0100 | [diff] [blame] | 629 | { |
toth92g | dd12390 | 2021-04-08 10:12:52 +0200 | [diff] [blame^] | 630 | return( ret ); |
toth92g | 1bbc2fe | 2021-02-12 16:11:17 +0100 | [diff] [blame] | 631 | } |
| 632 | else |
| 633 | { |
| 634 | subject_key_id->len = len; |
| 635 | subject_key_id->tag = MBEDTLS_ASN1_OCTET_STRING; |
| 636 | subject_key_id->p = *p; |
| 637 | *p += len; |
| 638 | } |
| 639 | |
toth92g | dd12390 | 2021-04-08 10:12:52 +0200 | [diff] [blame^] | 640 | return( 0 ); |
toth92g | 1bbc2fe | 2021-02-12 16:11:17 +0100 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | /* |
| 644 | * AuthorityKeyIdentifier ::= SEQUENCE { |
| 645 | * keyIdentifier [0] KeyIdentifier OPTIONAL, |
| 646 | * authorityCertIssuer [1] GeneralNames OPTIONAL, |
| 647 | * authorityCertSerialNumber [2] CertificateSerialNumber OPTIONAL } |
| 648 | * |
| 649 | * KeyIdentifier ::= OCTET STRING |
| 650 | */ |
toth92g | dd12390 | 2021-04-08 10:12:52 +0200 | [diff] [blame^] | 651 | static int x509_get_authority_key_id( unsigned char** p, |
toth92g | 1bbc2fe | 2021-02-12 16:11:17 +0100 | [diff] [blame] | 652 | unsigned char* end, |
toth92g | dd12390 | 2021-04-08 10:12:52 +0200 | [diff] [blame^] | 653 | mbedtls_x509_authority* authority_key_id ) |
toth92g | 1bbc2fe | 2021-02-12 16:11:17 +0100 | [diff] [blame] | 654 | { |
| 655 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 656 | size_t len = 0u; |
| 657 | |
toth92g | dd12390 | 2021-04-08 10:12:52 +0200 | [diff] [blame^] | 658 | if ( (ret = mbedtls_asn1_get_tag(p, end, &len, |
| 659 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0 ) |
toth92g | 1bbc2fe | 2021-02-12 16:11:17 +0100 | [diff] [blame] | 660 | { |
toth92g | dd12390 | 2021-04-08 10:12:52 +0200 | [diff] [blame^] | 661 | return( ret ); |
toth92g | 1bbc2fe | 2021-02-12 16:11:17 +0100 | [diff] [blame] | 662 | } |
| 663 | |
toth92g | dd12390 | 2021-04-08 10:12:52 +0200 | [diff] [blame^] | 664 | if ( (ret = mbedtls_asn1_get_tag(p, end, &len, |
| 665 | MBEDTLS_ASN1_CONTEXT_SPECIFIC)) != 0 ) |
toth92g | 1bbc2fe | 2021-02-12 16:11:17 +0100 | [diff] [blame] | 666 | { |
| 667 | /* KeyIdentifier is an OPTIONAL field */ |
| 668 | } |
| 669 | else |
| 670 | { |
| 671 | authority_key_id->keyIdentifier.len = len; |
| 672 | authority_key_id->keyIdentifier.p = *p; |
| 673 | authority_key_id->keyIdentifier.tag = MBEDTLS_ASN1_OCTET_STRING; |
| 674 | |
| 675 | *p += len; |
| 676 | } |
| 677 | |
| 678 | if ( *p < end ) |
| 679 | { |
toth92g | dd12390 | 2021-04-08 10:12:52 +0200 | [diff] [blame^] | 680 | if ( (ret = mbedtls_asn1_get_tag(p, end, &len, |
| 681 | MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_BOOLEAN)) != 0 ) |
toth92g | 1bbc2fe | 2021-02-12 16:11:17 +0100 | [diff] [blame] | 682 | { |
| 683 | /* authorityCertIssuer is an OPTIONAL field */ |
| 684 | } |
| 685 | else |
| 686 | { |
toth92g | dd12390 | 2021-04-08 10:12:52 +0200 | [diff] [blame^] | 687 | if ( (ret = mbedtls_asn1_get_tag(p, end, &len, |
| 688 | MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_OCTET_STRING)) != 0 ) |
toth92g | 1bbc2fe | 2021-02-12 16:11:17 +0100 | [diff] [blame] | 689 | { |
toth92g | dd12390 | 2021-04-08 10:12:52 +0200 | [diff] [blame^] | 690 | return( ret ); |
toth92g | 1bbc2fe | 2021-02-12 16:11:17 +0100 | [diff] [blame] | 691 | } |
| 692 | else |
| 693 | { |
| 694 | authority_key_id->raw.p = *p; |
| 695 | |
toth92g | dd12390 | 2021-04-08 10:12:52 +0200 | [diff] [blame^] | 696 | if ( (ret = mbedtls_asn1_get_tag(p, end, &len, |
| 697 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0 ) |
toth92g | 1bbc2fe | 2021-02-12 16:11:17 +0100 | [diff] [blame] | 698 | { |
toth92g | dd12390 | 2021-04-08 10:12:52 +0200 | [diff] [blame^] | 699 | return( ret ); |
toth92g | 1bbc2fe | 2021-02-12 16:11:17 +0100 | [diff] [blame] | 700 | } |
| 701 | |
toth92g | dd12390 | 2021-04-08 10:12:52 +0200 | [diff] [blame^] | 702 | if ( (ret = mbedtls_x509_get_name(p, *p + len, &authority_key_id->authorityCertIssuer)) != 0 ) |
toth92g | 1bbc2fe | 2021-02-12 16:11:17 +0100 | [diff] [blame] | 703 | { |
toth92g | dd12390 | 2021-04-08 10:12:52 +0200 | [diff] [blame^] | 704 | return( ret ); |
toth92g | 1bbc2fe | 2021-02-12 16:11:17 +0100 | [diff] [blame] | 705 | } |
| 706 | |
| 707 | authority_key_id->raw.len = *p - authority_key_id->raw.p; |
| 708 | } |
| 709 | } |
| 710 | } |
| 711 | |
| 712 | if ( *p < end ) |
| 713 | { |
toth92g | dd12390 | 2021-04-08 10:12:52 +0200 | [diff] [blame^] | 714 | if ( (ret = mbedtls_asn1_get_tag(p, end, &len, |
| 715 | MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_INTEGER)) != 0 ) |
toth92g | 1bbc2fe | 2021-02-12 16:11:17 +0100 | [diff] [blame] | 716 | { |
| 717 | /* authorityCertSerialNumber is an OPTIONAL field, but if there are still data it must be the serial number */ |
toth92g | dd12390 | 2021-04-08 10:12:52 +0200 | [diff] [blame^] | 718 | return( ret ); |
toth92g | 1bbc2fe | 2021-02-12 16:11:17 +0100 | [diff] [blame] | 719 | } |
| 720 | else |
| 721 | { |
| 722 | authority_key_id->authorityCertSerialNumber.len = len; |
| 723 | authority_key_id->authorityCertSerialNumber.p = *p; |
| 724 | authority_key_id->authorityCertSerialNumber.tag = MBEDTLS_ASN1_OCTET_STRING; |
| 725 | *p += len; |
| 726 | } |
| 727 | } |
| 728 | |
toth92g | dd12390 | 2021-04-08 10:12:52 +0200 | [diff] [blame^] | 729 | if ( *p != end ) |
toth92g | 1bbc2fe | 2021-02-12 16:11:17 +0100 | [diff] [blame] | 730 | { |
toth92g | dd12390 | 2021-04-08 10:12:52 +0200 | [diff] [blame^] | 731 | return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + |
| 732 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ); |
toth92g | 1bbc2fe | 2021-02-12 16:11:17 +0100 | [diff] [blame] | 733 | } |
| 734 | |
toth92g | dd12390 | 2021-04-08 10:12:52 +0200 | [diff] [blame^] | 735 | return( 0 ); |
toth92g | 1bbc2fe | 2021-02-12 16:11:17 +0100 | [diff] [blame] | 736 | } |
| 737 | |
| 738 | /* |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 739 | * SubjectAltName ::= GeneralNames |
| 740 | * |
| 741 | * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName |
| 742 | * |
| 743 | * GeneralName ::= CHOICE { |
| 744 | * otherName [0] OtherName, |
| 745 | * rfc822Name [1] IA5String, |
| 746 | * dNSName [2] IA5String, |
| 747 | * x400Address [3] ORAddress, |
| 748 | * directoryName [4] Name, |
| 749 | * ediPartyName [5] EDIPartyName, |
| 750 | * uniformResourceIdentifier [6] IA5String, |
| 751 | * iPAddress [7] OCTET STRING, |
| 752 | * registeredID [8] OBJECT IDENTIFIER } |
| 753 | * |
| 754 | * OtherName ::= SEQUENCE { |
| 755 | * type-id OBJECT IDENTIFIER, |
| 756 | * value [0] EXPLICIT ANY DEFINED BY type-id } |
| 757 | * |
| 758 | * EDIPartyName ::= SEQUENCE { |
| 759 | * nameAssigner [0] DirectoryString OPTIONAL, |
| 760 | * partyName [1] DirectoryString } |
| 761 | * |
Ron Eldor | c8b5f3f | 2019-05-15 15:15:55 +0300 | [diff] [blame] | 762 | * NOTE: we list all types, but only use dNSName and otherName |
| 763 | * of type HwModuleName, as defined in RFC 4108, at this point. |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 764 | */ |
| 765 | static int x509_get_subject_alt_name( unsigned char **p, |
| 766 | const unsigned char *end, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 767 | mbedtls_x509_sequence *subject_alt_name ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 768 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 769 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 770 | size_t len, tag_len; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 771 | mbedtls_asn1_buf *buf; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 772 | unsigned char tag; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 773 | mbedtls_asn1_sequence *cur = subject_alt_name; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 774 | |
| 775 | /* Get main sequence tag */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 776 | if( ( ret = mbedtls_asn1_get_tag( p, end, &len, |
| 777 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 778 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 779 | |
| 780 | if( *p + len != end ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 781 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, |
| 782 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 783 | |
| 784 | while( *p < end ) |
| 785 | { |
Ron Eldor | dbbd966 | 2019-05-15 15:46:03 +0300 | [diff] [blame] | 786 | mbedtls_x509_subject_alternative_name dummy_san_buf; |
| 787 | memset( &dummy_san_buf, 0, sizeof( dummy_san_buf ) ); |
| 788 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 789 | tag = **p; |
| 790 | (*p)++; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 791 | if( ( ret = mbedtls_asn1_get_len( p, end, &tag_len ) ) != 0 ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 792 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 793 | |
Andres Amaya Garcia | 849bc65 | 2017-08-25 17:13:12 +0100 | [diff] [blame] | 794 | if( ( tag & MBEDTLS_ASN1_TAG_CLASS_MASK ) != |
| 795 | MBEDTLS_ASN1_CONTEXT_SPECIFIC ) |
| 796 | { |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 797 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, |
| 798 | MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) ); |
Andres Amaya Garcia | 849bc65 | 2017-08-25 17:13:12 +0100 | [diff] [blame] | 799 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 800 | |
Ron Eldor | dbbd966 | 2019-05-15 15:46:03 +0300 | [diff] [blame] | 801 | /* |
irwir | 74aee1c | 2019-09-21 18:21:48 +0300 | [diff] [blame] | 802 | * Check that the SAN is structured correctly. |
Ron Eldor | dbbd966 | 2019-05-15 15:46:03 +0300 | [diff] [blame] | 803 | */ |
| 804 | ret = mbedtls_x509_parse_subject_alt_name( &(cur->buf), &dummy_san_buf ); |
| 805 | /* |
| 806 | * In case the extension is malformed, return an error, |
| 807 | * and clear the allocated sequences. |
| 808 | */ |
| 809 | if( ret != 0 && ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE ) |
| 810 | { |
Glenn Strauss | a4b4041 | 2022-06-26 19:32:09 -0400 | [diff] [blame] | 811 | mbedtls_asn1_sequence_free( subject_alt_name->next ); |
Ron Eldor | 5aebeeb | 2019-05-22 16:41:21 +0300 | [diff] [blame] | 812 | subject_alt_name->next = NULL; |
Ron Eldor | dbbd966 | 2019-05-15 15:46:03 +0300 | [diff] [blame] | 813 | return( ret ); |
| 814 | } |
| 815 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 816 | /* Allocate and assign next pointer */ |
Manuel Pégourié-Gonnard | bce2b30 | 2014-04-01 13:43:28 +0200 | [diff] [blame] | 817 | if( cur->buf.p != NULL ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 818 | { |
Manuel Pégourié-Gonnard | b134060 | 2014-11-11 23:11:16 +0100 | [diff] [blame] | 819 | if( cur->next != NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 820 | return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS ); |
Manuel Pégourié-Gonnard | b134060 | 2014-11-11 23:11:16 +0100 | [diff] [blame] | 821 | |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 822 | cur->next = mbedtls_calloc( 1, sizeof( mbedtls_asn1_sequence ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 823 | |
| 824 | if( cur->next == NULL ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 825 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, |
| 826 | MBEDTLS_ERR_ASN1_ALLOC_FAILED ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 827 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 828 | cur = cur->next; |
| 829 | } |
Manuel Pégourié-Gonnard | bce2b30 | 2014-04-01 13:43:28 +0200 | [diff] [blame] | 830 | |
| 831 | buf = &(cur->buf); |
| 832 | buf->tag = tag; |
| 833 | buf->p = *p; |
| 834 | buf->len = tag_len; |
| 835 | *p += buf->len; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 836 | } |
| 837 | |
| 838 | /* Set final sequence entry's next pointer to NULL */ |
| 839 | cur->next = NULL; |
| 840 | |
| 841 | if( *p != end ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 842 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, |
| 843 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 844 | |
| 845 | return( 0 ); |
| 846 | } |
| 847 | |
| 848 | /* |
Ron Eldor | 74d9acc | 2019-03-21 14:00:03 +0200 | [diff] [blame] | 849 | * id-ce-certificatePolicies OBJECT IDENTIFIER ::= { id-ce 32 } |
| 850 | * |
| 851 | * anyPolicy OBJECT IDENTIFIER ::= { id-ce-certificatePolicies 0 } |
| 852 | * |
| 853 | * certificatePolicies ::= SEQUENCE SIZE (1..MAX) OF PolicyInformation |
| 854 | * |
| 855 | * PolicyInformation ::= SEQUENCE { |
| 856 | * policyIdentifier CertPolicyId, |
| 857 | * policyQualifiers SEQUENCE SIZE (1..MAX) OF |
| 858 | * PolicyQualifierInfo OPTIONAL } |
| 859 | * |
| 860 | * CertPolicyId ::= OBJECT IDENTIFIER |
| 861 | * |
| 862 | * PolicyQualifierInfo ::= SEQUENCE { |
| 863 | * policyQualifierId PolicyQualifierId, |
| 864 | * qualifier ANY DEFINED BY policyQualifierId } |
| 865 | * |
| 866 | * -- policyQualifierIds for Internet policy qualifiers |
| 867 | * |
| 868 | * id-qt OBJECT IDENTIFIER ::= { id-pkix 2 } |
| 869 | * id-qt-cps OBJECT IDENTIFIER ::= { id-qt 1 } |
| 870 | * id-qt-unotice OBJECT IDENTIFIER ::= { id-qt 2 } |
| 871 | * |
| 872 | * PolicyQualifierId ::= OBJECT IDENTIFIER ( id-qt-cps | id-qt-unotice ) |
| 873 | * |
| 874 | * Qualifier ::= CHOICE { |
| 875 | * cPSuri CPSuri, |
| 876 | * userNotice UserNotice } |
| 877 | * |
| 878 | * CPSuri ::= IA5String |
| 879 | * |
| 880 | * UserNotice ::= SEQUENCE { |
| 881 | * noticeRef NoticeReference OPTIONAL, |
| 882 | * explicitText DisplayText OPTIONAL } |
| 883 | * |
| 884 | * NoticeReference ::= SEQUENCE { |
| 885 | * organization DisplayText, |
| 886 | * noticeNumbers SEQUENCE OF INTEGER } |
| 887 | * |
| 888 | * DisplayText ::= CHOICE { |
| 889 | * ia5String IA5String (SIZE (1..200)), |
| 890 | * visibleString VisibleString (SIZE (1..200)), |
| 891 | * bmpString BMPString (SIZE (1..200)), |
| 892 | * utf8String UTF8String (SIZE (1..200)) } |
| 893 | * |
| 894 | * NOTE: we only parse and use anyPolicy without qualifiers at this point |
| 895 | * as defined in RFC 5280. |
| 896 | */ |
| 897 | static int x509_get_certificate_policies( unsigned char **p, |
| 898 | const unsigned char *end, |
| 899 | mbedtls_x509_sequence *certificate_policies ) |
| 900 | { |
Ron Eldor | 8b0c3c9 | 2019-05-15 12:20:00 +0300 | [diff] [blame] | 901 | int ret, parse_ret = 0; |
Ron Eldor | 74d9acc | 2019-03-21 14:00:03 +0200 | [diff] [blame] | 902 | size_t len; |
| 903 | mbedtls_asn1_buf *buf; |
| 904 | mbedtls_asn1_sequence *cur = certificate_policies; |
| 905 | |
| 906 | /* Get main sequence tag */ |
| 907 | ret = mbedtls_asn1_get_tag( p, end, &len, |
| 908 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ); |
| 909 | if( ret != 0 ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 910 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) ); |
Ron Eldor | 74d9acc | 2019-03-21 14:00:03 +0200 | [diff] [blame] | 911 | |
| 912 | if( *p + len != end ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 913 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, |
| 914 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) ); |
Ron Eldor | 74d9acc | 2019-03-21 14:00:03 +0200 | [diff] [blame] | 915 | |
| 916 | /* |
| 917 | * Cannot be an empty sequence. |
| 918 | */ |
| 919 | if( len == 0 ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 920 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, |
| 921 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) ); |
Ron Eldor | 74d9acc | 2019-03-21 14:00:03 +0200 | [diff] [blame] | 922 | |
| 923 | while( *p < end ) |
| 924 | { |
| 925 | mbedtls_x509_buf policy_oid; |
| 926 | const unsigned char *policy_end; |
| 927 | |
| 928 | /* |
| 929 | * Get the policy sequence |
| 930 | */ |
| 931 | if( ( ret = mbedtls_asn1_get_tag( p, end, &len, |
| 932 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 933 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) ); |
Ron Eldor | 74d9acc | 2019-03-21 14:00:03 +0200 | [diff] [blame] | 934 | |
| 935 | policy_end = *p + len; |
| 936 | |
Ron Eldor | 0806379 | 2019-05-13 16:38:39 +0300 | [diff] [blame] | 937 | if( ( ret = mbedtls_asn1_get_tag( p, policy_end, &len, |
Ron Eldor | 74d9acc | 2019-03-21 14:00:03 +0200 | [diff] [blame] | 938 | MBEDTLS_ASN1_OID ) ) != 0 ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 939 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) ); |
Ron Eldor | 74d9acc | 2019-03-21 14:00:03 +0200 | [diff] [blame] | 940 | |
| 941 | policy_oid.tag = MBEDTLS_ASN1_OID; |
| 942 | policy_oid.len = len; |
| 943 | policy_oid.p = *p; |
| 944 | |
Ron Eldor | 8b0c3c9 | 2019-05-15 12:20:00 +0300 | [diff] [blame] | 945 | /* |
| 946 | * Only AnyPolicy is currently supported when enforcing policy. |
| 947 | */ |
| 948 | if( MBEDTLS_OID_CMP( MBEDTLS_OID_ANY_POLICY, &policy_oid ) != 0 ) |
| 949 | { |
| 950 | /* |
| 951 | * Set the parsing return code but continue parsing, in case this |
TRodziewicz | 3ecb92e | 2021-05-11 18:22:05 +0200 | [diff] [blame] | 952 | * extension is critical. |
Ron Eldor | 8b0c3c9 | 2019-05-15 12:20:00 +0300 | [diff] [blame] | 953 | */ |
| 954 | parse_ret = MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE; |
| 955 | } |
| 956 | |
Ron Eldor | 74d9acc | 2019-03-21 14:00:03 +0200 | [diff] [blame] | 957 | /* Allocate and assign next pointer */ |
| 958 | if( cur->buf.p != NULL ) |
| 959 | { |
| 960 | if( cur->next != NULL ) |
| 961 | return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS ); |
| 962 | |
| 963 | cur->next = mbedtls_calloc( 1, sizeof( mbedtls_asn1_sequence ) ); |
| 964 | |
| 965 | if( cur->next == NULL ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 966 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, |
| 967 | MBEDTLS_ERR_ASN1_ALLOC_FAILED ) ); |
Ron Eldor | 74d9acc | 2019-03-21 14:00:03 +0200 | [diff] [blame] | 968 | |
| 969 | cur = cur->next; |
| 970 | } |
| 971 | |
| 972 | buf = &( cur->buf ); |
| 973 | buf->tag = policy_oid.tag; |
| 974 | buf->p = policy_oid.p; |
| 975 | buf->len = policy_oid.len; |
Ron Eldor | 0806379 | 2019-05-13 16:38:39 +0300 | [diff] [blame] | 976 | |
| 977 | *p += len; |
| 978 | |
| 979 | /* |
| 980 | * If there is an optional qualifier, then *p < policy_end |
| 981 | * Check the Qualifier len to verify it doesn't exceed policy_end. |
| 982 | */ |
| 983 | if( *p < policy_end ) |
| 984 | { |
| 985 | if( ( ret = mbedtls_asn1_get_tag( p, policy_end, &len, |
| 986 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 987 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) ); |
Ron Eldor | 0806379 | 2019-05-13 16:38:39 +0300 | [diff] [blame] | 988 | /* |
| 989 | * Skip the optional policy qualifiers. |
| 990 | */ |
| 991 | *p += len; |
| 992 | } |
| 993 | |
| 994 | if( *p != policy_end ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 995 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, |
| 996 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) ); |
Ron Eldor | 74d9acc | 2019-03-21 14:00:03 +0200 | [diff] [blame] | 997 | } |
| 998 | |
| 999 | /* Set final sequence entry's next pointer to NULL */ |
| 1000 | cur->next = NULL; |
| 1001 | |
| 1002 | if( *p != end ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 1003 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, |
| 1004 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) ); |
Ron Eldor | 74d9acc | 2019-03-21 14:00:03 +0200 | [diff] [blame] | 1005 | |
Ron Eldor | 8b0c3c9 | 2019-05-15 12:20:00 +0300 | [diff] [blame] | 1006 | return( parse_ret ); |
Ron Eldor | 74d9acc | 2019-03-21 14:00:03 +0200 | [diff] [blame] | 1007 | } |
| 1008 | |
| 1009 | /* |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1010 | * X.509 v3 extensions |
| 1011 | * |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1012 | */ |
| 1013 | static int x509_get_crt_ext( unsigned char **p, |
| 1014 | const unsigned char *end, |
Nicola Di Lieto | 502d4b4 | 2020-04-25 14:41:25 +0200 | [diff] [blame] | 1015 | mbedtls_x509_crt *crt, |
Nicola Di Lieto | 5659e7e | 2020-05-28 23:41:38 +0200 | [diff] [blame] | 1016 | mbedtls_x509_crt_ext_cb_t cb, |
| 1017 | void *p_ctx ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1018 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1019 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1020 | size_t len; |
Nicola Di Lieto | c84b1e6 | 2020-06-13 11:08:16 +0200 | [diff] [blame] | 1021 | unsigned char *end_ext_data, *start_ext_octet, *end_ext_octet; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1022 | |
Hanno Becker | 12f62fb | 2019-02-12 17:22:36 +0000 | [diff] [blame] | 1023 | if( *p == end ) |
| 1024 | return( 0 ); |
| 1025 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1026 | if( ( ret = mbedtls_x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1027 | return( ret ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1028 | |
Hanno Becker | 12f62fb | 2019-02-12 17:22:36 +0000 | [diff] [blame] | 1029 | end = crt->v3_ext.p + crt->v3_ext.len; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1030 | while( *p < end ) |
| 1031 | { |
| 1032 | /* |
| 1033 | * Extension ::= SEQUENCE { |
| 1034 | * extnID OBJECT IDENTIFIER, |
| 1035 | * critical BOOLEAN DEFAULT FALSE, |
| 1036 | * extnValue OCTET STRING } |
| 1037 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1038 | mbedtls_x509_buf extn_oid = {0, 0, NULL}; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1039 | int is_critical = 0; /* DEFAULT FALSE */ |
| 1040 | int ext_type = 0; |
| 1041 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1042 | if( ( ret = mbedtls_asn1_get_tag( p, end, &len, |
| 1043 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 1044 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1045 | |
| 1046 | end_ext_data = *p + len; |
| 1047 | |
| 1048 | /* Get extension ID */ |
k-stachowiak | dcae78a | 2018-06-28 16:32:54 +0200 | [diff] [blame] | 1049 | if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &extn_oid.len, |
k-stachowiak | 463928a | 2018-07-24 12:50:59 +0200 | [diff] [blame] | 1050 | MBEDTLS_ASN1_OID ) ) != 0 ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 1051 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1052 | |
k-stachowiak | 463928a | 2018-07-24 12:50:59 +0200 | [diff] [blame] | 1053 | extn_oid.tag = MBEDTLS_ASN1_OID; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1054 | extn_oid.p = *p; |
| 1055 | *p += extn_oid.len; |
| 1056 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1057 | /* Get optional critical */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1058 | if( ( ret = mbedtls_asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 && |
| 1059 | ( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 1060 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1061 | |
| 1062 | /* Data should be octet string type */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1063 | if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &len, |
| 1064 | MBEDTLS_ASN1_OCTET_STRING ) ) != 0 ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 1065 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1066 | |
Nicola Di Lieto | c84b1e6 | 2020-06-13 11:08:16 +0200 | [diff] [blame] | 1067 | start_ext_octet = *p; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1068 | end_ext_octet = *p + len; |
| 1069 | |
| 1070 | if( end_ext_octet != end_ext_data ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 1071 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, |
| 1072 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1073 | |
| 1074 | /* |
| 1075 | * Detect supported extensions |
| 1076 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1077 | ret = mbedtls_oid_get_x509_ext_type( &extn_oid, &ext_type ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1078 | |
| 1079 | if( ret != 0 ) |
| 1080 | { |
Nicola Di Lieto | 502d4b4 | 2020-04-25 14:41:25 +0200 | [diff] [blame] | 1081 | /* Give the callback (if any) a chance to handle the extension */ |
Nicola Di Lieto | 2c3a917 | 2020-05-28 17:20:42 +0200 | [diff] [blame] | 1082 | if( cb != NULL ) |
| 1083 | { |
Nicola Di Lieto | 5659e7e | 2020-05-28 23:41:38 +0200 | [diff] [blame] | 1084 | ret = cb( p_ctx, crt, &extn_oid, is_critical, *p, end_ext_octet ); |
Nicola Di Lieto | 565b52b | 2020-05-29 22:46:56 +0200 | [diff] [blame] | 1085 | if( ret != 0 && is_critical ) |
| 1086 | return( ret ); |
Nicola Di Lieto | fae25a1 | 2020-05-28 08:55:08 +0200 | [diff] [blame] | 1087 | *p = end_ext_octet; |
Nicola Di Lieto | 502d4b4 | 2020-04-25 14:41:25 +0200 | [diff] [blame] | 1088 | continue; |
Nicola Di Lieto | fae25a1 | 2020-05-28 08:55:08 +0200 | [diff] [blame] | 1089 | } |
Nicola Di Lieto | 502d4b4 | 2020-04-25 14:41:25 +0200 | [diff] [blame] | 1090 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1091 | /* No parser found, skip extension */ |
| 1092 | *p = end_ext_octet; |
| 1093 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1094 | if( is_critical ) |
| 1095 | { |
| 1096 | /* Data is marked as critical: fail */ |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 1097 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, |
| 1098 | MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1099 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1100 | continue; |
| 1101 | } |
| 1102 | |
Manuel Pégourié-Gonnard | 8a5e3d4 | 2014-11-12 17:47:28 +0100 | [diff] [blame] | 1103 | /* Forbid repeated extensions */ |
| 1104 | if( ( crt->ext_types & ext_type ) != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1105 | return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS ); |
Manuel Pégourié-Gonnard | 8a5e3d4 | 2014-11-12 17:47:28 +0100 | [diff] [blame] | 1106 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1107 | crt->ext_types |= ext_type; |
| 1108 | |
| 1109 | switch( ext_type ) |
| 1110 | { |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 1111 | case MBEDTLS_X509_EXT_BASIC_CONSTRAINTS: |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1112 | /* Parse basic constraints */ |
| 1113 | if( ( ret = x509_get_basic_constraints( p, end_ext_octet, |
| 1114 | &crt->ca_istrue, &crt->max_pathlen ) ) != 0 ) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 1115 | return( ret ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1116 | break; |
| 1117 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1118 | case MBEDTLS_X509_EXT_KEY_USAGE: |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1119 | /* Parse key usage */ |
| 1120 | if( ( ret = x509_get_key_usage( p, end_ext_octet, |
| 1121 | &crt->key_usage ) ) != 0 ) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 1122 | return( ret ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1123 | break; |
| 1124 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1125 | case MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE: |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1126 | /* Parse extended key usage */ |
| 1127 | if( ( ret = x509_get_ext_key_usage( p, end_ext_octet, |
| 1128 | &crt->ext_key_usage ) ) != 0 ) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 1129 | return( ret ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1130 | break; |
toth92g | 1bbc2fe | 2021-02-12 16:11:17 +0100 | [diff] [blame] | 1131 | case MBEDTLS_X509_EXT_SUBJECT_KEY_IDENTIFIER: |
| 1132 | /* Parse subject key identifier */ |
| 1133 | if ( ( ret = x509_get_subject_key_id( p, end_ext_data, |
| 1134 | &crt->subject_key_id ) ) != 0 ) |
| 1135 | { |
| 1136 | return ( ret ); |
| 1137 | } |
| 1138 | break; |
| 1139 | case MBEDTLS_X509_EXT_AUTHORITY_KEY_IDENTIFIER: |
| 1140 | /* Parse authority key identifier */ |
| 1141 | if ( (ret = x509_get_authority_key_id(p, end_ext_octet, |
| 1142 | &crt->authority_key_id)) != 0 ) |
| 1143 | { |
toth92g | dd12390 | 2021-04-08 10:12:52 +0200 | [diff] [blame^] | 1144 | return ( ret ); |
toth92g | 1bbc2fe | 2021-02-12 16:11:17 +0100 | [diff] [blame] | 1145 | } |
| 1146 | break; |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 1147 | case MBEDTLS_X509_EXT_SUBJECT_ALT_NAME: |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1148 | /* Parse subject alt name */ |
| 1149 | if( ( ret = x509_get_subject_alt_name( p, end_ext_octet, |
| 1150 | &crt->subject_alt_names ) ) != 0 ) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 1151 | return( ret ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1152 | break; |
| 1153 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1154 | case MBEDTLS_X509_EXT_NS_CERT_TYPE: |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1155 | /* Parse netscape certificate type */ |
| 1156 | if( ( ret = x509_get_ns_cert_type( p, end_ext_octet, |
| 1157 | &crt->ns_cert_type ) ) != 0 ) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 1158 | return( ret ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1159 | break; |
| 1160 | |
Ron Eldor | 74d9acc | 2019-03-21 14:00:03 +0200 | [diff] [blame] | 1161 | case MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES: |
| 1162 | /* Parse certificate policies type */ |
| 1163 | if( ( ret = x509_get_certificate_policies( p, end_ext_octet, |
| 1164 | &crt->certificate_policies ) ) != 0 ) |
Ron Eldor | 8b0c3c9 | 2019-05-15 12:20:00 +0300 | [diff] [blame] | 1165 | { |
Nicola Di Lieto | c84b1e6 | 2020-06-13 11:08:16 +0200 | [diff] [blame] | 1166 | /* Give the callback (if any) a chance to handle the extension |
| 1167 | * if it contains unsupported policies */ |
| 1168 | if( ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE && cb != NULL && |
| 1169 | cb( p_ctx, crt, &extn_oid, is_critical, |
| 1170 | start_ext_octet, end_ext_octet ) == 0 ) |
| 1171 | break; |
| 1172 | |
Ron Eldor | 8b0c3c9 | 2019-05-15 12:20:00 +0300 | [diff] [blame] | 1173 | if( is_critical ) |
| 1174 | return( ret ); |
| 1175 | else |
Ron Eldor | 8b0c3c9 | 2019-05-15 12:20:00 +0300 | [diff] [blame] | 1176 | /* |
Ron Eldor | a291391 | 2019-05-16 16:17:38 +0300 | [diff] [blame] | 1177 | * If MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE is returned, then we |
| 1178 | * cannot interpret or enforce the policy. However, it is up to |
| 1179 | * the user to choose how to enforce the policies, |
Ron Eldor | 8b0c3c9 | 2019-05-15 12:20:00 +0300 | [diff] [blame] | 1180 | * unless the extension is critical. |
| 1181 | */ |
| 1182 | if( ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE ) |
| 1183 | return( ret ); |
| 1184 | } |
Ron Eldor | 74d9acc | 2019-03-21 14:00:03 +0200 | [diff] [blame] | 1185 | break; |
| 1186 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1187 | default: |
Ron Eldor | df48efa | 2019-04-08 13:28:24 +0300 | [diff] [blame] | 1188 | /* |
| 1189 | * If this is a non-critical extension, which the oid layer |
| 1190 | * supports, but there isn't an x509 parser for it, |
| 1191 | * skip the extension. |
| 1192 | */ |
Ron Eldor | df48efa | 2019-04-08 13:28:24 +0300 | [diff] [blame] | 1193 | if( is_critical ) |
| 1194 | return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE ); |
| 1195 | else |
Ron Eldor | df48efa | 2019-04-08 13:28:24 +0300 | [diff] [blame] | 1196 | *p = end_ext_octet; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1197 | } |
| 1198 | } |
| 1199 | |
| 1200 | if( *p != end ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 1201 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, |
| 1202 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1203 | |
| 1204 | return( 0 ); |
| 1205 | } |
| 1206 | |
| 1207 | /* |
| 1208 | * Parse and fill a single X.509 certificate in DER format |
| 1209 | */ |
Hanno Becker | 1a65dcd | 2019-01-31 08:57:44 +0000 | [diff] [blame] | 1210 | static int x509_crt_parse_der_core( mbedtls_x509_crt *crt, |
| 1211 | const unsigned char *buf, |
| 1212 | size_t buflen, |
Nicola Di Lieto | 502d4b4 | 2020-04-25 14:41:25 +0200 | [diff] [blame] | 1213 | int make_copy, |
Nicola Di Lieto | 5659e7e | 2020-05-28 23:41:38 +0200 | [diff] [blame] | 1214 | mbedtls_x509_crt_ext_cb_t cb, |
| 1215 | void *p_ctx ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1216 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1217 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1218 | size_t len; |
| 1219 | unsigned char *p, *end, *crt_end; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1220 | mbedtls_x509_buf sig_params1, sig_params2, sig_oid2; |
Manuel Pégourié-Gonnard | 59a75d5 | 2014-01-22 10:12:57 +0100 | [diff] [blame] | 1221 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1222 | memset( &sig_params1, 0, sizeof( mbedtls_x509_buf ) ); |
| 1223 | memset( &sig_params2, 0, sizeof( mbedtls_x509_buf ) ); |
| 1224 | memset( &sig_oid2, 0, sizeof( mbedtls_x509_buf ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1225 | |
| 1226 | /* |
| 1227 | * Check for valid input |
| 1228 | */ |
| 1229 | if( crt == NULL || buf == NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1230 | return( MBEDTLS_ERR_X509_BAD_INPUT_DATA ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1231 | |
Hanno Becker | 1a65dcd | 2019-01-31 08:57:44 +0000 | [diff] [blame] | 1232 | /* Use the original buffer until we figure out actual length. */ |
Janos Follath | cc0e49d | 2016-02-17 14:34:12 +0000 | [diff] [blame] | 1233 | p = (unsigned char*) buf; |
| 1234 | len = buflen; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1235 | end = p + len; |
| 1236 | |
| 1237 | /* |
| 1238 | * Certificate ::= SEQUENCE { |
| 1239 | * tbsCertificate TBSCertificate, |
| 1240 | * signatureAlgorithm AlgorithmIdentifier, |
| 1241 | * signatureValue BIT STRING } |
| 1242 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1243 | if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, |
| 1244 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1245 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1246 | mbedtls_x509_crt_free( crt ); |
| 1247 | return( MBEDTLS_ERR_X509_INVALID_FORMAT ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1248 | } |
| 1249 | |
Janos Follath | cc0e49d | 2016-02-17 14:34:12 +0000 | [diff] [blame] | 1250 | end = crt_end = p + len; |
Hanno Becker | 1a65dcd | 2019-01-31 08:57:44 +0000 | [diff] [blame] | 1251 | crt->raw.len = crt_end - buf; |
| 1252 | if( make_copy != 0 ) |
| 1253 | { |
| 1254 | /* Create and populate a new buffer for the raw field. */ |
| 1255 | crt->raw.p = p = mbedtls_calloc( 1, crt->raw.len ); |
| 1256 | if( crt->raw.p == NULL ) |
| 1257 | return( MBEDTLS_ERR_X509_ALLOC_FAILED ); |
| 1258 | |
| 1259 | memcpy( crt->raw.p, buf, crt->raw.len ); |
| 1260 | crt->own_buffer = 1; |
| 1261 | |
| 1262 | p += crt->raw.len - len; |
| 1263 | end = crt_end = p + len; |
| 1264 | } |
| 1265 | else |
| 1266 | { |
| 1267 | crt->raw.p = (unsigned char*) buf; |
| 1268 | crt->own_buffer = 0; |
| 1269 | } |
Janos Follath | cc0e49d | 2016-02-17 14:34:12 +0000 | [diff] [blame] | 1270 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1271 | /* |
| 1272 | * TBSCertificate ::= SEQUENCE { |
| 1273 | */ |
| 1274 | crt->tbs.p = p; |
| 1275 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1276 | if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, |
| 1277 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1278 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1279 | mbedtls_x509_crt_free( crt ); |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 1280 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1281 | } |
| 1282 | |
| 1283 | end = p + len; |
| 1284 | crt->tbs.len = end - crt->tbs.p; |
| 1285 | |
| 1286 | /* |
| 1287 | * Version ::= INTEGER { v1(0), v2(1), v3(2) } |
| 1288 | * |
| 1289 | * CertificateSerialNumber ::= INTEGER |
| 1290 | * |
| 1291 | * signature AlgorithmIdentifier |
| 1292 | */ |
| 1293 | if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 || |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1294 | ( ret = mbedtls_x509_get_serial( &p, end, &crt->serial ) ) != 0 || |
| 1295 | ( ret = mbedtls_x509_get_alg( &p, end, &crt->sig_oid, |
Manuel Pégourié-Gonnard | dddbb1d | 2014-06-05 17:02:24 +0200 | [diff] [blame] | 1296 | &sig_params1 ) ) != 0 ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1297 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1298 | mbedtls_x509_crt_free( crt ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1299 | return( ret ); |
| 1300 | } |
| 1301 | |
Andres AG | 7ca4a03 | 2017-03-09 16:16:11 +0000 | [diff] [blame] | 1302 | if( crt->version < 0 || crt->version > 2 ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1303 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1304 | mbedtls_x509_crt_free( crt ); |
| 1305 | return( MBEDTLS_ERR_X509_UNKNOWN_VERSION ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1306 | } |
| 1307 | |
Andres AG | 7ca4a03 | 2017-03-09 16:16:11 +0000 | [diff] [blame] | 1308 | crt->version++; |
| 1309 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1310 | if( ( ret = mbedtls_x509_get_sig_alg( &crt->sig_oid, &sig_params1, |
Manuel Pégourié-Gonnard | f75f2f7 | 2014-06-05 15:14:28 +0200 | [diff] [blame] | 1311 | &crt->sig_md, &crt->sig_pk, |
| 1312 | &crt->sig_opts ) ) != 0 ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1313 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1314 | mbedtls_x509_crt_free( crt ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1315 | return( ret ); |
| 1316 | } |
| 1317 | |
| 1318 | /* |
| 1319 | * issuer Name |
| 1320 | */ |
| 1321 | crt->issuer_raw.p = p; |
| 1322 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1323 | if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, |
| 1324 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1325 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1326 | mbedtls_x509_crt_free( crt ); |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 1327 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1328 | } |
| 1329 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1330 | if( ( ret = mbedtls_x509_get_name( &p, p + len, &crt->issuer ) ) != 0 ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1331 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1332 | mbedtls_x509_crt_free( crt ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1333 | return( ret ); |
| 1334 | } |
| 1335 | |
| 1336 | crt->issuer_raw.len = p - crt->issuer_raw.p; |
| 1337 | |
| 1338 | /* |
| 1339 | * Validity ::= SEQUENCE { |
| 1340 | * notBefore Time, |
| 1341 | * notAfter Time } |
| 1342 | * |
| 1343 | */ |
| 1344 | if( ( ret = x509_get_dates( &p, end, &crt->valid_from, |
| 1345 | &crt->valid_to ) ) != 0 ) |
| 1346 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1347 | mbedtls_x509_crt_free( crt ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1348 | return( ret ); |
| 1349 | } |
| 1350 | |
| 1351 | /* |
| 1352 | * subject Name |
| 1353 | */ |
| 1354 | crt->subject_raw.p = p; |
| 1355 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1356 | if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, |
| 1357 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1358 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1359 | mbedtls_x509_crt_free( crt ); |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 1360 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1361 | } |
| 1362 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1363 | if( len && ( ret = mbedtls_x509_get_name( &p, p + len, &crt->subject ) ) != 0 ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1364 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1365 | mbedtls_x509_crt_free( crt ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1366 | return( ret ); |
| 1367 | } |
| 1368 | |
| 1369 | crt->subject_raw.len = p - crt->subject_raw.p; |
| 1370 | |
| 1371 | /* |
| 1372 | * SubjectPublicKeyInfo |
| 1373 | */ |
Hanno Becker | 494dd7a | 2019-02-06 16:13:41 +0000 | [diff] [blame] | 1374 | crt->pk_raw.p = p; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1375 | if( ( ret = mbedtls_pk_parse_subpubkey( &p, end, &crt->pk ) ) != 0 ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1376 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1377 | mbedtls_x509_crt_free( crt ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1378 | return( ret ); |
| 1379 | } |
Hanno Becker | 494dd7a | 2019-02-06 16:13:41 +0000 | [diff] [blame] | 1380 | crt->pk_raw.len = p - crt->pk_raw.p; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1381 | |
| 1382 | /* |
| 1383 | * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL, |
| 1384 | * -- If present, version shall be v2 or v3 |
| 1385 | * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL, |
| 1386 | * -- If present, version shall be v2 or v3 |
| 1387 | * extensions [3] EXPLICIT Extensions OPTIONAL |
| 1388 | * -- If present, version shall be v3 |
| 1389 | */ |
| 1390 | if( crt->version == 2 || crt->version == 3 ) |
| 1391 | { |
| 1392 | ret = x509_get_uid( &p, end, &crt->issuer_id, 1 ); |
| 1393 | if( ret != 0 ) |
| 1394 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1395 | mbedtls_x509_crt_free( crt ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1396 | return( ret ); |
| 1397 | } |
| 1398 | } |
| 1399 | |
| 1400 | if( crt->version == 2 || crt->version == 3 ) |
| 1401 | { |
| 1402 | ret = x509_get_uid( &p, end, &crt->subject_id, 2 ); |
| 1403 | if( ret != 0 ) |
| 1404 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1405 | mbedtls_x509_crt_free( crt ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1406 | return( ret ); |
| 1407 | } |
| 1408 | } |
| 1409 | |
| 1410 | if( crt->version == 3 ) |
Manuel Pégourié-Gonnard | a252af7 | 2015-03-27 16:15:55 +0100 | [diff] [blame] | 1411 | { |
Nicola Di Lieto | 5659e7e | 2020-05-28 23:41:38 +0200 | [diff] [blame] | 1412 | ret = x509_get_crt_ext( &p, end, crt, cb, p_ctx ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1413 | if( ret != 0 ) |
| 1414 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1415 | mbedtls_x509_crt_free( crt ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1416 | return( ret ); |
| 1417 | } |
| 1418 | } |
| 1419 | |
| 1420 | if( p != end ) |
| 1421 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1422 | mbedtls_x509_crt_free( crt ); |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 1423 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, |
| 1424 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1425 | } |
| 1426 | |
| 1427 | end = crt_end; |
| 1428 | |
| 1429 | /* |
| 1430 | * } |
| 1431 | * -- end of TBSCertificate |
| 1432 | * |
| 1433 | * signatureAlgorithm AlgorithmIdentifier, |
| 1434 | * signatureValue BIT STRING |
| 1435 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1436 | if( ( ret = mbedtls_x509_get_alg( &p, end, &sig_oid2, &sig_params2 ) ) != 0 ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1437 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1438 | mbedtls_x509_crt_free( crt ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1439 | return( ret ); |
| 1440 | } |
| 1441 | |
Manuel Pégourié-Gonnard | 1022fed | 2015-03-27 16:30:47 +0100 | [diff] [blame] | 1442 | if( crt->sig_oid.len != sig_oid2.len || |
| 1443 | memcmp( crt->sig_oid.p, sig_oid2.p, crt->sig_oid.len ) != 0 || |
Paul Elliott | ca17ebf | 2020-11-24 17:30:18 +0000 | [diff] [blame] | 1444 | sig_params1.tag != sig_params2.tag || |
Manuel Pégourié-Gonnard | dddbb1d | 2014-06-05 17:02:24 +0200 | [diff] [blame] | 1445 | sig_params1.len != sig_params2.len || |
Manuel Pégourié-Gonnard | 159c524 | 2015-04-30 11:15:22 +0200 | [diff] [blame] | 1446 | ( sig_params1.len != 0 && |
| 1447 | memcmp( sig_params1.p, sig_params2.p, sig_params1.len ) != 0 ) ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1448 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1449 | mbedtls_x509_crt_free( crt ); |
| 1450 | return( MBEDTLS_ERR_X509_SIG_MISMATCH ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1451 | } |
| 1452 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1453 | if( ( ret = mbedtls_x509_get_sig( &p, end, &crt->sig ) ) != 0 ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1454 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1455 | mbedtls_x509_crt_free( crt ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1456 | return( ret ); |
| 1457 | } |
| 1458 | |
| 1459 | if( p != end ) |
| 1460 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1461 | mbedtls_x509_crt_free( crt ); |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 1462 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, |
| 1463 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1464 | } |
| 1465 | |
| 1466 | return( 0 ); |
| 1467 | } |
| 1468 | |
| 1469 | /* |
| 1470 | * Parse one X.509 certificate in DER format from a buffer and add them to a |
| 1471 | * chained list |
| 1472 | */ |
Hanno Becker | 1a65dcd | 2019-01-31 08:57:44 +0000 | [diff] [blame] | 1473 | static int mbedtls_x509_crt_parse_der_internal( mbedtls_x509_crt *chain, |
| 1474 | const unsigned char *buf, |
| 1475 | size_t buflen, |
Nicola Di Lieto | 502d4b4 | 2020-04-25 14:41:25 +0200 | [diff] [blame] | 1476 | int make_copy, |
Nicola Di Lieto | 5659e7e | 2020-05-28 23:41:38 +0200 | [diff] [blame] | 1477 | mbedtls_x509_crt_ext_cb_t cb, |
| 1478 | void *p_ctx ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1479 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1480 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1481 | mbedtls_x509_crt *crt = chain, *prev = NULL; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1482 | |
| 1483 | /* |
| 1484 | * Check for valid input |
| 1485 | */ |
| 1486 | if( crt == NULL || buf == NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1487 | return( MBEDTLS_ERR_X509_BAD_INPUT_DATA ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1488 | |
| 1489 | while( crt->version != 0 && crt->next != NULL ) |
| 1490 | { |
| 1491 | prev = crt; |
| 1492 | crt = crt->next; |
| 1493 | } |
| 1494 | |
| 1495 | /* |
| 1496 | * Add new certificate on the end of the chain if needed. |
| 1497 | */ |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 1498 | if( crt->version != 0 && crt->next == NULL ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1499 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 1500 | crt->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1501 | |
| 1502 | if( crt->next == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 1503 | return( MBEDTLS_ERR_X509_ALLOC_FAILED ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1504 | |
| 1505 | prev = crt; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1506 | mbedtls_x509_crt_init( crt->next ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1507 | crt = crt->next; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1508 | } |
| 1509 | |
Nicola Di Lieto | 5659e7e | 2020-05-28 23:41:38 +0200 | [diff] [blame] | 1510 | ret = x509_crt_parse_der_core( crt, buf, buflen, make_copy, cb, p_ctx ); |
Nicola Di Lieto | 502d4b4 | 2020-04-25 14:41:25 +0200 | [diff] [blame] | 1511 | if( ret != 0 ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1512 | { |
| 1513 | if( prev ) |
| 1514 | prev->next = NULL; |
| 1515 | |
| 1516 | if( crt != chain ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1517 | mbedtls_free( crt ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1518 | |
| 1519 | return( ret ); |
| 1520 | } |
| 1521 | |
| 1522 | return( 0 ); |
| 1523 | } |
| 1524 | |
Hanno Becker | 1a65dcd | 2019-01-31 08:57:44 +0000 | [diff] [blame] | 1525 | int mbedtls_x509_crt_parse_der_nocopy( mbedtls_x509_crt *chain, |
| 1526 | const unsigned char *buf, |
| 1527 | size_t buflen ) |
| 1528 | { |
Nicola Di Lieto | 5659e7e | 2020-05-28 23:41:38 +0200 | [diff] [blame] | 1529 | return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, 0, NULL, NULL ) ); |
Nicola Di Lieto | 502d4b4 | 2020-04-25 14:41:25 +0200 | [diff] [blame] | 1530 | } |
| 1531 | |
Nicola Di Lieto | fde98f7 | 2020-05-28 08:34:33 +0200 | [diff] [blame] | 1532 | int mbedtls_x509_crt_parse_der_with_ext_cb( mbedtls_x509_crt *chain, |
| 1533 | const unsigned char *buf, |
| 1534 | size_t buflen, |
Nicola Di Lieto | 4dbe567 | 2020-05-28 09:18:42 +0200 | [diff] [blame] | 1535 | int make_copy, |
Nicola Di Lieto | 5659e7e | 2020-05-28 23:41:38 +0200 | [diff] [blame] | 1536 | mbedtls_x509_crt_ext_cb_t cb, |
| 1537 | void *p_ctx ) |
Nicola Di Lieto | 502d4b4 | 2020-04-25 14:41:25 +0200 | [diff] [blame] | 1538 | { |
Nicola Di Lieto | 5659e7e | 2020-05-28 23:41:38 +0200 | [diff] [blame] | 1539 | return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, make_copy, cb, p_ctx ) ); |
Hanno Becker | 1a65dcd | 2019-01-31 08:57:44 +0000 | [diff] [blame] | 1540 | } |
| 1541 | |
| 1542 | int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain, |
| 1543 | const unsigned char *buf, |
| 1544 | size_t buflen ) |
| 1545 | { |
Nicola Di Lieto | 5659e7e | 2020-05-28 23:41:38 +0200 | [diff] [blame] | 1546 | return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, 1, NULL, NULL ) ); |
Hanno Becker | 1a65dcd | 2019-01-31 08:57:44 +0000 | [diff] [blame] | 1547 | } |
| 1548 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1549 | /* |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 1550 | * Parse one or more PEM certificates from a buffer and add them to the chained |
| 1551 | * list |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1552 | */ |
Hanno Becker | 1a65dcd | 2019-01-31 08:57:44 +0000 | [diff] [blame] | 1553 | int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain, |
| 1554 | const unsigned char *buf, |
| 1555 | size_t buflen ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1556 | { |
Janos Follath | 98e28a7 | 2016-05-31 14:03:54 +0100 | [diff] [blame] | 1557 | #if defined(MBEDTLS_PEM_PARSE_C) |
Andres AG | c0db511 | 2016-12-07 15:05:53 +0000 | [diff] [blame] | 1558 | int success = 0, first_error = 0, total_failed = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1559 | int buf_format = MBEDTLS_X509_FORMAT_DER; |
Janos Follath | 98e28a7 | 2016-05-31 14:03:54 +0100 | [diff] [blame] | 1560 | #endif |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1561 | |
| 1562 | /* |
| 1563 | * Check for valid input |
| 1564 | */ |
| 1565 | if( chain == NULL || buf == NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1566 | return( MBEDTLS_ERR_X509_BAD_INPUT_DATA ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1567 | |
| 1568 | /* |
| 1569 | * Determine buffer content. Buffer contains either one DER certificate or |
| 1570 | * one or more PEM certificates. |
| 1571 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1572 | #if defined(MBEDTLS_PEM_PARSE_C) |
Manuel Pégourié-Gonnard | 0ece0f9 | 2015-05-12 12:43:54 +0200 | [diff] [blame] | 1573 | if( buflen != 0 && buf[buflen - 1] == '\0' && |
Manuel Pégourié-Gonnard | 43b37cb | 2015-05-12 11:20:10 +0200 | [diff] [blame] | 1574 | strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL ) |
| 1575 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1576 | buf_format = MBEDTLS_X509_FORMAT_PEM; |
Manuel Pégourié-Gonnard | 43b37cb | 2015-05-12 11:20:10 +0200 | [diff] [blame] | 1577 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1578 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1579 | if( buf_format == MBEDTLS_X509_FORMAT_DER ) |
| 1580 | return mbedtls_x509_crt_parse_der( chain, buf, buflen ); |
Janos Follath | 98e28a7 | 2016-05-31 14:03:54 +0100 | [diff] [blame] | 1581 | #else |
| 1582 | return mbedtls_x509_crt_parse_der( chain, buf, buflen ); |
| 1583 | #endif |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1584 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1585 | #if defined(MBEDTLS_PEM_PARSE_C) |
| 1586 | if( buf_format == MBEDTLS_X509_FORMAT_PEM ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1587 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1588 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1589 | mbedtls_pem_context pem; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1590 | |
Manuel Pégourié-Gonnard | 43b37cb | 2015-05-12 11:20:10 +0200 | [diff] [blame] | 1591 | /* 1 rather than 0 since the terminating NULL byte is counted in */ |
| 1592 | while( buflen > 1 ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1593 | { |
| 1594 | size_t use_len; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1595 | mbedtls_pem_init( &pem ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1596 | |
Manuel Pégourié-Gonnard | 43b37cb | 2015-05-12 11:20:10 +0200 | [diff] [blame] | 1597 | /* If we get there, we know the string is null-terminated */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1598 | ret = mbedtls_pem_read_buffer( &pem, |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1599 | "-----BEGIN CERTIFICATE-----", |
| 1600 | "-----END CERTIFICATE-----", |
| 1601 | buf, NULL, 0, &use_len ); |
| 1602 | |
| 1603 | if( ret == 0 ) |
| 1604 | { |
| 1605 | /* |
| 1606 | * Was PEM encoded |
| 1607 | */ |
| 1608 | buflen -= use_len; |
| 1609 | buf += use_len; |
| 1610 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1611 | else if( ret == MBEDTLS_ERR_PEM_BAD_INPUT_DATA ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1612 | { |
| 1613 | return( ret ); |
| 1614 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1615 | else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1616 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1617 | mbedtls_pem_free( &pem ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1618 | |
| 1619 | /* |
| 1620 | * PEM header and footer were found |
| 1621 | */ |
| 1622 | buflen -= use_len; |
| 1623 | buf += use_len; |
| 1624 | |
| 1625 | if( first_error == 0 ) |
| 1626 | first_error = ret; |
| 1627 | |
Paul Bakker | 5a5fa92 | 2014-09-26 14:53:04 +0200 | [diff] [blame] | 1628 | total_failed++; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1629 | continue; |
| 1630 | } |
| 1631 | else |
| 1632 | break; |
| 1633 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1634 | ret = mbedtls_x509_crt_parse_der( chain, pem.buf, pem.buflen ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1635 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1636 | mbedtls_pem_free( &pem ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1637 | |
| 1638 | if( ret != 0 ) |
| 1639 | { |
| 1640 | /* |
| 1641 | * Quit parsing on a memory error |
| 1642 | */ |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 1643 | if( ret == MBEDTLS_ERR_X509_ALLOC_FAILED ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1644 | return( ret ); |
| 1645 | |
| 1646 | if( first_error == 0 ) |
| 1647 | first_error = ret; |
| 1648 | |
| 1649 | total_failed++; |
| 1650 | continue; |
| 1651 | } |
| 1652 | |
| 1653 | success = 1; |
| 1654 | } |
| 1655 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1656 | |
| 1657 | if( success ) |
| 1658 | return( total_failed ); |
| 1659 | else if( first_error ) |
| 1660 | return( first_error ); |
| 1661 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1662 | return( MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT ); |
Janos Follath | 98e28a7 | 2016-05-31 14:03:54 +0100 | [diff] [blame] | 1663 | #endif /* MBEDTLS_PEM_PARSE_C */ |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1664 | } |
| 1665 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1666 | #if defined(MBEDTLS_FS_IO) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1667 | /* |
| 1668 | * Load one or more certificates and add them to the chained list |
| 1669 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1670 | int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1671 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1672 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1673 | size_t n; |
| 1674 | unsigned char *buf; |
| 1675 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1676 | if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1677 | return( ret ); |
| 1678 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1679 | ret = mbedtls_x509_crt_parse( chain, buf, n ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1680 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 1681 | mbedtls_platform_zeroize( buf, n ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1682 | mbedtls_free( buf ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1683 | |
| 1684 | return( ret ); |
| 1685 | } |
| 1686 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1687 | int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1688 | { |
| 1689 | int ret = 0; |
Paul Bakker | fa6a620 | 2013-10-28 18:48:30 +0100 | [diff] [blame] | 1690 | #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1691 | int w_ret; |
| 1692 | WCHAR szDir[MAX_PATH]; |
| 1693 | char filename[MAX_PATH]; |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 1694 | char *p; |
Manuel Pégourié-Gonnard | 261faed | 2015-10-21 10:16:29 +0200 | [diff] [blame] | 1695 | size_t len = strlen( path ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1696 | |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 1697 | WIN32_FIND_DATAW file_data; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1698 | HANDLE hFind; |
| 1699 | |
| 1700 | if( len > MAX_PATH - 3 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1701 | return( MBEDTLS_ERR_X509_BAD_INPUT_DATA ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1702 | |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 1703 | memset( szDir, 0, sizeof(szDir) ); |
| 1704 | memset( filename, 0, MAX_PATH ); |
| 1705 | memcpy( filename, path, len ); |
| 1706 | filename[len++] = '\\'; |
| 1707 | p = filename + len; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1708 | filename[len++] = '*'; |
| 1709 | |
Simon B | 3c6b18d | 2016-11-03 01:11:37 +0000 | [diff] [blame] | 1710 | w_ret = MultiByteToWideChar( CP_ACP, 0, filename, (int)len, szDir, |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 1711 | MAX_PATH - 3 ); |
Manuel Pégourié-Gonnard | acdb9b9 | 2015-01-23 17:50:34 +0000 | [diff] [blame] | 1712 | if( w_ret == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1713 | return( MBEDTLS_ERR_X509_BAD_INPUT_DATA ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1714 | |
| 1715 | hFind = FindFirstFileW( szDir, &file_data ); |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 1716 | if( hFind == INVALID_HANDLE_VALUE ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1717 | return( MBEDTLS_ERR_X509_FILE_IO_ERROR ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1718 | |
| 1719 | len = MAX_PATH - len; |
| 1720 | do |
| 1721 | { |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 1722 | memset( p, 0, len ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1723 | |
| 1724 | if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) |
| 1725 | continue; |
| 1726 | |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 1727 | w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName, |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 1728 | lstrlenW( file_data.cFileName ), |
Manuel Pégourié-Gonnard | 261faed | 2015-10-21 10:16:29 +0200 | [diff] [blame] | 1729 | p, (int) len - 1, |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 1730 | NULL, NULL ); |
Manuel Pégourié-Gonnard | acdb9b9 | 2015-01-23 17:50:34 +0000 | [diff] [blame] | 1731 | if( w_ret == 0 ) |
Ron Eldor | 36d9042 | 2017-01-09 15:09:16 +0200 | [diff] [blame] | 1732 | { |
| 1733 | ret = MBEDTLS_ERR_X509_FILE_IO_ERROR; |
| 1734 | goto cleanup; |
| 1735 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1736 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1737 | w_ret = mbedtls_x509_crt_parse_file( chain, filename ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1738 | if( w_ret < 0 ) |
| 1739 | ret++; |
| 1740 | else |
| 1741 | ret += w_ret; |
| 1742 | } |
| 1743 | while( FindNextFileW( hFind, &file_data ) != 0 ); |
| 1744 | |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 1745 | if( GetLastError() != ERROR_NO_MORE_FILES ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1746 | ret = MBEDTLS_ERR_X509_FILE_IO_ERROR; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1747 | |
Ron Eldor | 36d9042 | 2017-01-09 15:09:16 +0200 | [diff] [blame] | 1748 | cleanup: |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1749 | FindClose( hFind ); |
Paul Bakker | be089b0 | 2013-10-14 15:51:50 +0200 | [diff] [blame] | 1750 | #else /* _WIN32 */ |
Manuel Pégourié-Gonnard | 964bf9b | 2013-11-26 16:47:11 +0100 | [diff] [blame] | 1751 | int t_ret; |
Andres AG | f911319 | 2016-09-02 14:06:04 +0100 | [diff] [blame] | 1752 | int snp_ret; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1753 | struct stat sb; |
Manuel Pégourié-Gonnard | 964bf9b | 2013-11-26 16:47:11 +0100 | [diff] [blame] | 1754 | struct dirent *entry; |
Andres AG | f911319 | 2016-09-02 14:06:04 +0100 | [diff] [blame] | 1755 | char entry_name[MBEDTLS_X509_MAX_FILE_PATH_LEN]; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1756 | DIR *dir = opendir( path ); |
| 1757 | |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 1758 | if( dir == NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1759 | return( MBEDTLS_ERR_X509_FILE_IO_ERROR ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1760 | |
Ron Eldor | 6314068 | 2017-01-09 19:27:59 +0200 | [diff] [blame] | 1761 | #if defined(MBEDTLS_THREADING_C) |
Manuel Pégourié-Gonnard | 944cfe8 | 2015-05-27 20:07:18 +0200 | [diff] [blame] | 1762 | if( ( ret = mbedtls_mutex_lock( &mbedtls_threading_readdir_mutex ) ) != 0 ) |
Manuel Pégourié-Gonnard | f9b85d9 | 2015-06-22 18:39:57 +0200 | [diff] [blame] | 1763 | { |
| 1764 | closedir( dir ); |
Manuel Pégourié-Gonnard | 5ad68e4 | 2013-11-28 17:11:54 +0100 | [diff] [blame] | 1765 | return( ret ); |
Manuel Pégourié-Gonnard | f9b85d9 | 2015-06-22 18:39:57 +0200 | [diff] [blame] | 1766 | } |
Ron Eldor | 6314068 | 2017-01-09 19:27:59 +0200 | [diff] [blame] | 1767 | #endif /* MBEDTLS_THREADING_C */ |
Manuel Pégourié-Gonnard | 5ad68e4 | 2013-11-28 17:11:54 +0100 | [diff] [blame] | 1768 | |
Paul Elliott | fb91a48 | 2021-03-05 14:17:51 +0000 | [diff] [blame] | 1769 | memset( &sb, 0, sizeof( sb ) ); |
| 1770 | |
Manuel Pégourié-Gonnard | 964bf9b | 2013-11-26 16:47:11 +0100 | [diff] [blame] | 1771 | while( ( entry = readdir( dir ) ) != NULL ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1772 | { |
Andres AG | f911319 | 2016-09-02 14:06:04 +0100 | [diff] [blame] | 1773 | snp_ret = mbedtls_snprintf( entry_name, sizeof entry_name, |
| 1774 | "%s/%s", path, entry->d_name ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1775 | |
Andres AG | f911319 | 2016-09-02 14:06:04 +0100 | [diff] [blame] | 1776 | if( snp_ret < 0 || (size_t)snp_ret >= sizeof entry_name ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1777 | { |
Andres AG | f911319 | 2016-09-02 14:06:04 +0100 | [diff] [blame] | 1778 | ret = MBEDTLS_ERR_X509_BUFFER_TOO_SMALL; |
| 1779 | goto cleanup; |
| 1780 | } |
| 1781 | else if( stat( entry_name, &sb ) == -1 ) |
| 1782 | { |
Dave Rodgman | fa40b02 | 2022-07-20 16:08:00 +0100 | [diff] [blame] | 1783 | if( errno == ENOENT ) |
Eduardo Silva | e1bfffc | 2019-04-25 10:43:26 -0600 | [diff] [blame] | 1784 | { |
Dave Rodgman | fa40b02 | 2022-07-20 16:08:00 +0100 | [diff] [blame] | 1785 | /* Broken symbolic link - ignore this entry. |
| 1786 | stat(2) will return this error for either (a) a dangling |
| 1787 | symlink or (b) a missing file. |
| 1788 | Given that we have just obtained the filename from readdir, |
| 1789 | assume that it does exist and therefore treat this as a |
| 1790 | dangling symlink. */ |
| 1791 | continue; |
| 1792 | } |
| 1793 | else |
| 1794 | { |
| 1795 | /* Some other file error; report the error. */ |
Eduardo Silva | e1bfffc | 2019-04-25 10:43:26 -0600 | [diff] [blame] | 1796 | ret = MBEDTLS_ERR_X509_FILE_IO_ERROR; |
| 1797 | goto cleanup; |
| 1798 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1799 | } |
| 1800 | |
| 1801 | if( !S_ISREG( sb.st_mode ) ) |
| 1802 | continue; |
| 1803 | |
| 1804 | // Ignore parse errors |
| 1805 | // |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1806 | t_ret = mbedtls_x509_crt_parse_file( chain, entry_name ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1807 | if( t_ret < 0 ) |
| 1808 | ret++; |
| 1809 | else |
| 1810 | ret += t_ret; |
| 1811 | } |
Manuel Pégourié-Gonnard | 5ad68e4 | 2013-11-28 17:11:54 +0100 | [diff] [blame] | 1812 | |
| 1813 | cleanup: |
Andres AG | f911319 | 2016-09-02 14:06:04 +0100 | [diff] [blame] | 1814 | closedir( dir ); |
| 1815 | |
Ron Eldor | 6314068 | 2017-01-09 19:27:59 +0200 | [diff] [blame] | 1816 | #if defined(MBEDTLS_THREADING_C) |
Manuel Pégourié-Gonnard | 944cfe8 | 2015-05-27 20:07:18 +0200 | [diff] [blame] | 1817 | if( mbedtls_mutex_unlock( &mbedtls_threading_readdir_mutex ) != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1818 | ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR; |
Ron Eldor | 6314068 | 2017-01-09 19:27:59 +0200 | [diff] [blame] | 1819 | #endif /* MBEDTLS_THREADING_C */ |
Manuel Pégourié-Gonnard | 5ad68e4 | 2013-11-28 17:11:54 +0100 | [diff] [blame] | 1820 | |
Paul Bakker | be089b0 | 2013-10-14 15:51:50 +0200 | [diff] [blame] | 1821 | #endif /* _WIN32 */ |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1822 | |
| 1823 | return( ret ); |
| 1824 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1825 | #endif /* MBEDTLS_FS_IO */ |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1826 | |
Ron Eldor | b2dc3fa | 2019-03-21 13:40:13 +0200 | [diff] [blame] | 1827 | /* |
| 1828 | * OtherName ::= SEQUENCE { |
| 1829 | * type-id OBJECT IDENTIFIER, |
| 1830 | * value [0] EXPLICIT ANY DEFINED BY type-id } |
| 1831 | * |
| 1832 | * HardwareModuleName ::= SEQUENCE { |
| 1833 | * hwType OBJECT IDENTIFIER, |
| 1834 | * hwSerialNum OCTET STRING } |
| 1835 | * |
| 1836 | * NOTE: we currently only parse and use otherName of type HwModuleName, |
| 1837 | * as defined in RFC 4108. |
| 1838 | */ |
| 1839 | static int x509_get_other_name( const mbedtls_x509_buf *subject_alt_name, |
| 1840 | mbedtls_x509_san_other_name *other_name ) |
| 1841 | { |
Janos Follath | ab23cd1 | 2019-05-09 13:53:57 +0100 | [diff] [blame] | 1842 | int ret = 0; |
Ron Eldor | b2dc3fa | 2019-03-21 13:40:13 +0200 | [diff] [blame] | 1843 | size_t len; |
| 1844 | unsigned char *p = subject_alt_name->p; |
| 1845 | const unsigned char *end = p + subject_alt_name->len; |
| 1846 | mbedtls_x509_buf cur_oid; |
| 1847 | |
| 1848 | if( ( subject_alt_name->tag & |
| 1849 | ( MBEDTLS_ASN1_TAG_CLASS_MASK | MBEDTLS_ASN1_TAG_VALUE_MASK ) ) != |
| 1850 | ( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME ) ) |
| 1851 | { |
| 1852 | /* |
| 1853 | * The given subject alternative name is not of type "othername". |
| 1854 | */ |
| 1855 | return( MBEDTLS_ERR_X509_BAD_INPUT_DATA ); |
| 1856 | } |
| 1857 | |
| 1858 | if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, |
| 1859 | MBEDTLS_ASN1_OID ) ) != 0 ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 1860 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) ); |
Ron Eldor | b2dc3fa | 2019-03-21 13:40:13 +0200 | [diff] [blame] | 1861 | |
| 1862 | cur_oid.tag = MBEDTLS_ASN1_OID; |
| 1863 | cur_oid.p = p; |
| 1864 | cur_oid.len = len; |
| 1865 | |
| 1866 | /* |
| 1867 | * Only HwModuleName is currently supported. |
| 1868 | */ |
| 1869 | if( MBEDTLS_OID_CMP( MBEDTLS_OID_ON_HW_MODULE_NAME, &cur_oid ) != 0 ) |
| 1870 | { |
| 1871 | return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE ); |
| 1872 | } |
| 1873 | |
Ron Eldor | 890819a | 2019-05-13 19:03:04 +0300 | [diff] [blame] | 1874 | if( p + len >= end ) |
| 1875 | { |
Sébastien Duquette | 661d725 | 2019-06-23 17:45:26 -0400 | [diff] [blame] | 1876 | mbedtls_platform_zeroize( other_name, sizeof( *other_name ) ); |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 1877 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, |
| 1878 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) ); |
Ron Eldor | 890819a | 2019-05-13 19:03:04 +0300 | [diff] [blame] | 1879 | } |
Ron Eldor | b2dc3fa | 2019-03-21 13:40:13 +0200 | [diff] [blame] | 1880 | p += len; |
| 1881 | if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, |
| 1882 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC ) ) != 0 ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 1883 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) ); |
Ron Eldor | b2dc3fa | 2019-03-21 13:40:13 +0200 | [diff] [blame] | 1884 | |
| 1885 | if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, |
| 1886 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 1887 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) ); |
Ron Eldor | b2dc3fa | 2019-03-21 13:40:13 +0200 | [diff] [blame] | 1888 | |
| 1889 | if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OID ) ) != 0 ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 1890 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) ); |
Ron Eldor | b2dc3fa | 2019-03-21 13:40:13 +0200 | [diff] [blame] | 1891 | |
| 1892 | other_name->value.hardware_module_name.oid.tag = MBEDTLS_ASN1_OID; |
| 1893 | other_name->value.hardware_module_name.oid.p = p; |
| 1894 | other_name->value.hardware_module_name.oid.len = len; |
| 1895 | |
Ron Eldor | 890819a | 2019-05-13 19:03:04 +0300 | [diff] [blame] | 1896 | if( p + len >= end ) |
| 1897 | { |
Sébastien Duquette | 661d725 | 2019-06-23 17:45:26 -0400 | [diff] [blame] | 1898 | mbedtls_platform_zeroize( other_name, sizeof( *other_name ) ); |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 1899 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, |
| 1900 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) ); |
Ron Eldor | 890819a | 2019-05-13 19:03:04 +0300 | [diff] [blame] | 1901 | } |
Ron Eldor | b2dc3fa | 2019-03-21 13:40:13 +0200 | [diff] [blame] | 1902 | p += len; |
| 1903 | if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, |
| 1904 | MBEDTLS_ASN1_OCTET_STRING ) ) != 0 ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 1905 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) ); |
Ron Eldor | b2dc3fa | 2019-03-21 13:40:13 +0200 | [diff] [blame] | 1906 | |
| 1907 | other_name->value.hardware_module_name.val.tag = MBEDTLS_ASN1_OCTET_STRING; |
| 1908 | other_name->value.hardware_module_name.val.p = p; |
| 1909 | other_name->value.hardware_module_name.val.len = len; |
Ron Eldor | b2dc3fa | 2019-03-21 13:40:13 +0200 | [diff] [blame] | 1910 | p += len; |
| 1911 | if( p != end ) |
| 1912 | { |
Ron Eldor | 890819a | 2019-05-13 19:03:04 +0300 | [diff] [blame] | 1913 | mbedtls_platform_zeroize( other_name, |
Sébastien Duquette | 661d725 | 2019-06-23 17:45:26 -0400 | [diff] [blame] | 1914 | sizeof( *other_name ) ); |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 1915 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, |
| 1916 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) ); |
Ron Eldor | b2dc3fa | 2019-03-21 13:40:13 +0200 | [diff] [blame] | 1917 | } |
| 1918 | return( 0 ); |
| 1919 | } |
| 1920 | |
Peter Kolbus | 9a969b6 | 2018-12-11 13:55:56 -0600 | [diff] [blame] | 1921 | int mbedtls_x509_parse_subject_alt_name( const mbedtls_x509_buf *san_buf, |
| 1922 | mbedtls_x509_subject_alternative_name *san ) |
| 1923 | { |
| 1924 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 1925 | switch( san_buf->tag & |
| 1926 | ( MBEDTLS_ASN1_TAG_CLASS_MASK | |
| 1927 | MBEDTLS_ASN1_TAG_VALUE_MASK ) ) |
| 1928 | { |
| 1929 | /* |
| 1930 | * otherName |
| 1931 | */ |
| 1932 | case( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME ): |
| 1933 | { |
| 1934 | mbedtls_x509_san_other_name other_name; |
| 1935 | |
| 1936 | ret = x509_get_other_name( san_buf, &other_name ); |
| 1937 | if( ret != 0 ) |
| 1938 | return( ret ); |
| 1939 | |
| 1940 | memset( san, 0, sizeof( mbedtls_x509_subject_alternative_name ) ); |
| 1941 | san->type = MBEDTLS_X509_SAN_OTHER_NAME; |
| 1942 | memcpy( &san->san.other_name, |
| 1943 | &other_name, sizeof( other_name ) ); |
| 1944 | |
| 1945 | } |
| 1946 | break; |
| 1947 | |
| 1948 | /* |
| 1949 | * dNSName |
| 1950 | */ |
| 1951 | case( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_DNS_NAME ): |
| 1952 | { |
| 1953 | memset( san, 0, sizeof( mbedtls_x509_subject_alternative_name ) ); |
| 1954 | san->type = MBEDTLS_X509_SAN_DNS_NAME; |
| 1955 | |
| 1956 | memcpy( &san->san.unstructured_name, |
| 1957 | san_buf, sizeof( *san_buf ) ); |
| 1958 | |
| 1959 | } |
| 1960 | break; |
| 1961 | |
| 1962 | /* |
| 1963 | * Type not supported |
| 1964 | */ |
| 1965 | default: |
| 1966 | return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE ); |
| 1967 | } |
| 1968 | return( 0 ); |
| 1969 | } |
| 1970 | |
| 1971 | #if !defined(MBEDTLS_X509_REMOVE_INFO) |
Manuel Pégourié-Gonnard | bce2b30 | 2014-04-01 13:43:28 +0200 | [diff] [blame] | 1972 | static int x509_info_subject_alt_name( char **buf, size_t *size, |
Janos Follath | 2f0ec1e | 2019-05-10 11:06:31 +0100 | [diff] [blame] | 1973 | const mbedtls_x509_sequence |
| 1974 | *subject_alt_name, |
Ron Eldor | b2dc3fa | 2019-03-21 13:40:13 +0200 | [diff] [blame] | 1975 | const char *prefix ) |
Manuel Pégourié-Gonnard | bce2b30 | 2014-04-01 13:43:28 +0200 | [diff] [blame] | 1976 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1977 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Victor Barpp Gomes | 47c7a73 | 2022-09-29 11:34:23 -0300 | [diff] [blame] | 1978 | size_t i; |
Manuel Pégourié-Gonnard | bce2b30 | 2014-04-01 13:43:28 +0200 | [diff] [blame] | 1979 | size_t n = *size; |
| 1980 | char *p = *buf; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1981 | const mbedtls_x509_sequence *cur = subject_alt_name; |
Ron Eldor | 890819a | 2019-05-13 19:03:04 +0300 | [diff] [blame] | 1982 | mbedtls_x509_subject_alternative_name san; |
| 1983 | int parse_ret; |
Manuel Pégourié-Gonnard | bce2b30 | 2014-04-01 13:43:28 +0200 | [diff] [blame] | 1984 | |
| 1985 | while( cur != NULL ) |
| 1986 | { |
Ron Eldor | 890819a | 2019-05-13 19:03:04 +0300 | [diff] [blame] | 1987 | memset( &san, 0, sizeof( san ) ); |
| 1988 | parse_ret = mbedtls_x509_parse_subject_alt_name( &cur->buf, &san ); |
| 1989 | if( parse_ret != 0 ) |
| 1990 | { |
| 1991 | if( parse_ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE ) |
| 1992 | { |
| 1993 | ret = mbedtls_snprintf( p, n, "\n%s <unsupported>", prefix ); |
| 1994 | MBEDTLS_X509_SAFE_SNPRINTF; |
| 1995 | } |
| 1996 | else |
| 1997 | { |
| 1998 | ret = mbedtls_snprintf( p, n, "\n%s <malformed>", prefix ); |
| 1999 | MBEDTLS_X509_SAFE_SNPRINTF; |
| 2000 | } |
| 2001 | cur = cur->next; |
| 2002 | continue; |
| 2003 | } |
| 2004 | |
| 2005 | switch( san.type ) |
Manuel Pégourié-Gonnard | bce2b30 | 2014-04-01 13:43:28 +0200 | [diff] [blame] | 2006 | { |
Ron Eldor | b2dc3fa | 2019-03-21 13:40:13 +0200 | [diff] [blame] | 2007 | /* |
| 2008 | * otherName |
| 2009 | */ |
Ron Eldor | a291391 | 2019-05-16 16:17:38 +0300 | [diff] [blame] | 2010 | case MBEDTLS_X509_SAN_OTHER_NAME: |
Ron Eldor | 890819a | 2019-05-13 19:03:04 +0300 | [diff] [blame] | 2011 | { |
| 2012 | mbedtls_x509_san_other_name *other_name = &san.san.other_name; |
| 2013 | |
| 2014 | ret = mbedtls_snprintf( p, n, "\n%s otherName :", prefix ); |
| 2015 | MBEDTLS_X509_SAFE_SNPRINTF; |
| 2016 | |
| 2017 | if( MBEDTLS_OID_CMP( MBEDTLS_OID_ON_HW_MODULE_NAME, |
| 2018 | &other_name->value.hardware_module_name.oid ) != 0 ) |
Janos Follath | 22f605f | 2019-05-10 10:37:17 +0100 | [diff] [blame] | 2019 | { |
Ron Eldor | 890819a | 2019-05-13 19:03:04 +0300 | [diff] [blame] | 2020 | ret = mbedtls_snprintf( p, n, "\n%s hardware module name :", prefix ); |
| 2021 | MBEDTLS_X509_SAFE_SNPRINTF; |
| 2022 | ret = mbedtls_snprintf( p, n, "\n%s hardware type : ", prefix ); |
Janos Follath | 2f0ec1e | 2019-05-10 11:06:31 +0100 | [diff] [blame] | 2023 | MBEDTLS_X509_SAFE_SNPRINTF; |
| 2024 | |
Ron Eldor | 890819a | 2019-05-13 19:03:04 +0300 | [diff] [blame] | 2025 | ret = mbedtls_oid_get_numeric_string( p, n, &other_name->value.hardware_module_name.oid ); |
| 2026 | MBEDTLS_X509_SAFE_SNPRINTF; |
Janos Follath | 2f0ec1e | 2019-05-10 11:06:31 +0100 | [diff] [blame] | 2027 | |
Ron Eldor | 890819a | 2019-05-13 19:03:04 +0300 | [diff] [blame] | 2028 | ret = mbedtls_snprintf( p, n, "\n%s hardware serial number : ", prefix ); |
| 2029 | MBEDTLS_X509_SAFE_SNPRINTF; |
| 2030 | |
Victor Barpp Gomes | 47c7a73 | 2022-09-29 11:34:23 -0300 | [diff] [blame] | 2031 | for( i = 0; i < other_name->value.hardware_module_name.val.len; i++ ) |
Ron Eldor | 890819a | 2019-05-13 19:03:04 +0300 | [diff] [blame] | 2032 | { |
Victor Barpp Gomes | 47c7a73 | 2022-09-29 11:34:23 -0300 | [diff] [blame] | 2033 | ret = mbedtls_snprintf( p, n, "%02X", other_name->value.hardware_module_name.val.p[i] ); |
| 2034 | MBEDTLS_X509_SAFE_SNPRINTF; |
Janos Follath | 22f605f | 2019-05-10 10:37:17 +0100 | [diff] [blame] | 2035 | } |
Ron Eldor | 890819a | 2019-05-13 19:03:04 +0300 | [diff] [blame] | 2036 | }/* MBEDTLS_OID_ON_HW_MODULE_NAME */ |
| 2037 | } |
| 2038 | break; |
Ron Eldor | b2dc3fa | 2019-03-21 13:40:13 +0200 | [diff] [blame] | 2039 | |
| 2040 | /* |
| 2041 | * dNSName |
| 2042 | */ |
Ron Eldor | a291391 | 2019-05-16 16:17:38 +0300 | [diff] [blame] | 2043 | case MBEDTLS_X509_SAN_DNS_NAME: |
Ron Eldor | 890819a | 2019-05-13 19:03:04 +0300 | [diff] [blame] | 2044 | { |
Ron Eldor | b2dc3fa | 2019-03-21 13:40:13 +0200 | [diff] [blame] | 2045 | ret = mbedtls_snprintf( p, n, "\n%s dNSName : ", prefix ); |
| 2046 | MBEDTLS_X509_SAFE_SNPRINTF; |
Ron Eldor | 890819a | 2019-05-13 19:03:04 +0300 | [diff] [blame] | 2047 | if( san.san.unstructured_name.len >= n ) |
Ron Eldor | b2dc3fa | 2019-03-21 13:40:13 +0200 | [diff] [blame] | 2048 | { |
| 2049 | *p = '\0'; |
| 2050 | return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL ); |
| 2051 | } |
Ron Eldor | a291391 | 2019-05-16 16:17:38 +0300 | [diff] [blame] | 2052 | |
| 2053 | memcpy( p, san.san.unstructured_name.p, san.san.unstructured_name.len ); |
| 2054 | p += san.san.unstructured_name.len; |
Ron Eldor | 890819a | 2019-05-13 19:03:04 +0300 | [diff] [blame] | 2055 | n -= san.san.unstructured_name.len; |
Ron Eldor | 890819a | 2019-05-13 19:03:04 +0300 | [diff] [blame] | 2056 | } |
| 2057 | break; |
Ron Eldor | b2dc3fa | 2019-03-21 13:40:13 +0200 | [diff] [blame] | 2058 | |
| 2059 | /* |
Ron Eldor | 890819a | 2019-05-13 19:03:04 +0300 | [diff] [blame] | 2060 | * Type not supported, skip item. |
Ron Eldor | b2dc3fa | 2019-03-21 13:40:13 +0200 | [diff] [blame] | 2061 | */ |
| 2062 | default: |
Janos Follath | 22f605f | 2019-05-10 10:37:17 +0100 | [diff] [blame] | 2063 | ret = mbedtls_snprintf( p, n, "\n%s <unsupported>", prefix ); |
| 2064 | MBEDTLS_X509_SAFE_SNPRINTF; |
Ron Eldor | b2dc3fa | 2019-03-21 13:40:13 +0200 | [diff] [blame] | 2065 | break; |
Manuel Pégourié-Gonnard | bce2b30 | 2014-04-01 13:43:28 +0200 | [diff] [blame] | 2066 | } |
| 2067 | |
Manuel Pégourié-Gonnard | bce2b30 | 2014-04-01 13:43:28 +0200 | [diff] [blame] | 2068 | cur = cur->next; |
| 2069 | } |
| 2070 | |
| 2071 | *p = '\0'; |
| 2072 | |
| 2073 | *size = n; |
| 2074 | *buf = p; |
| 2075 | |
| 2076 | return( 0 ); |
| 2077 | } |
| 2078 | |
Manuel Pégourié-Gonnard | 0db29b0 | 2014-04-01 18:12:24 +0200 | [diff] [blame] | 2079 | #define PRINT_ITEM(i) \ |
| 2080 | { \ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2081 | ret = mbedtls_snprintf( p, n, "%s" i, sep ); \ |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 2082 | MBEDTLS_X509_SAFE_SNPRINTF; \ |
Manuel Pégourié-Gonnard | 0db29b0 | 2014-04-01 18:12:24 +0200 | [diff] [blame] | 2083 | sep = ", "; \ |
| 2084 | } |
| 2085 | |
| 2086 | #define CERT_TYPE(type,name) \ |
Hanno Becker | 1eeca41 | 2018-10-15 12:01:35 +0100 | [diff] [blame] | 2087 | if( ns_cert_type & (type) ) \ |
Manuel Pégourié-Gonnard | 0db29b0 | 2014-04-01 18:12:24 +0200 | [diff] [blame] | 2088 | PRINT_ITEM( name ); |
| 2089 | |
Manuel Pégourié-Gonnard | 919f8f5 | 2014-04-01 13:01:11 +0200 | [diff] [blame] | 2090 | static int x509_info_cert_type( char **buf, size_t *size, |
| 2091 | unsigned char ns_cert_type ) |
| 2092 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2093 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 919f8f5 | 2014-04-01 13:01:11 +0200 | [diff] [blame] | 2094 | size_t n = *size; |
| 2095 | char *p = *buf; |
Manuel Pégourié-Gonnard | 7b30cfc | 2014-04-01 18:00:07 +0200 | [diff] [blame] | 2096 | const char *sep = ""; |
Manuel Pégourié-Gonnard | 919f8f5 | 2014-04-01 13:01:11 +0200 | [diff] [blame] | 2097 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2098 | CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT, "SSL Client" ); |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 2099 | CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER, "SSL Server" ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2100 | CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL, "Email" ); |
| 2101 | CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing" ); |
| 2102 | CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_RESERVED, "Reserved" ); |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 2103 | CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CA, "SSL CA" ); |
| 2104 | CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA, "Email CA" ); |
| 2105 | CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA" ); |
Manuel Pégourié-Gonnard | 919f8f5 | 2014-04-01 13:01:11 +0200 | [diff] [blame] | 2106 | |
| 2107 | *size = n; |
| 2108 | *buf = p; |
| 2109 | |
| 2110 | return( 0 ); |
| 2111 | } |
| 2112 | |
Manuel Pégourié-Gonnard | 0db29b0 | 2014-04-01 18:12:24 +0200 | [diff] [blame] | 2113 | #define KEY_USAGE(code,name) \ |
Hanno Becker | 1eeca41 | 2018-10-15 12:01:35 +0100 | [diff] [blame] | 2114 | if( key_usage & (code) ) \ |
Manuel Pégourié-Gonnard | 0db29b0 | 2014-04-01 18:12:24 +0200 | [diff] [blame] | 2115 | PRINT_ITEM( name ); |
| 2116 | |
Manuel Pégourié-Gonnard | 65c2ddc | 2014-04-01 14:12:11 +0200 | [diff] [blame] | 2117 | static int x509_info_key_usage( char **buf, size_t *size, |
Manuel Pégourié-Gonnard | 9a70225 | 2015-06-23 10:14:36 +0200 | [diff] [blame] | 2118 | unsigned int key_usage ) |
Manuel Pégourié-Gonnard | 65c2ddc | 2014-04-01 14:12:11 +0200 | [diff] [blame] | 2119 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2120 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 65c2ddc | 2014-04-01 14:12:11 +0200 | [diff] [blame] | 2121 | size_t n = *size; |
| 2122 | char *p = *buf; |
Manuel Pégourié-Gonnard | 7b30cfc | 2014-04-01 18:00:07 +0200 | [diff] [blame] | 2123 | const char *sep = ""; |
Manuel Pégourié-Gonnard | 65c2ddc | 2014-04-01 14:12:11 +0200 | [diff] [blame] | 2124 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2125 | KEY_USAGE( MBEDTLS_X509_KU_DIGITAL_SIGNATURE, "Digital Signature" ); |
| 2126 | KEY_USAGE( MBEDTLS_X509_KU_NON_REPUDIATION, "Non Repudiation" ); |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 2127 | KEY_USAGE( MBEDTLS_X509_KU_KEY_ENCIPHERMENT, "Key Encipherment" ); |
| 2128 | KEY_USAGE( MBEDTLS_X509_KU_DATA_ENCIPHERMENT, "Data Encipherment" ); |
| 2129 | KEY_USAGE( MBEDTLS_X509_KU_KEY_AGREEMENT, "Key Agreement" ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2130 | KEY_USAGE( MBEDTLS_X509_KU_KEY_CERT_SIGN, "Key Cert Sign" ); |
| 2131 | KEY_USAGE( MBEDTLS_X509_KU_CRL_SIGN, "CRL Sign" ); |
Manuel Pégourié-Gonnard | 9a70225 | 2015-06-23 10:14:36 +0200 | [diff] [blame] | 2132 | KEY_USAGE( MBEDTLS_X509_KU_ENCIPHER_ONLY, "Encipher Only" ); |
| 2133 | KEY_USAGE( MBEDTLS_X509_KU_DECIPHER_ONLY, "Decipher Only" ); |
Manuel Pégourié-Gonnard | 65c2ddc | 2014-04-01 14:12:11 +0200 | [diff] [blame] | 2134 | |
| 2135 | *size = n; |
| 2136 | *buf = p; |
| 2137 | |
| 2138 | return( 0 ); |
| 2139 | } |
| 2140 | |
Manuel Pégourié-Gonnard | f6f4ab4 | 2014-04-01 17:32:44 +0200 | [diff] [blame] | 2141 | static int x509_info_ext_key_usage( char **buf, size_t *size, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2142 | const mbedtls_x509_sequence *extended_key_usage ) |
Manuel Pégourié-Gonnard | f6f4ab4 | 2014-04-01 17:32:44 +0200 | [diff] [blame] | 2143 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2144 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | f6f4ab4 | 2014-04-01 17:32:44 +0200 | [diff] [blame] | 2145 | const char *desc; |
| 2146 | size_t n = *size; |
| 2147 | char *p = *buf; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2148 | const mbedtls_x509_sequence *cur = extended_key_usage; |
Manuel Pégourié-Gonnard | 7b30cfc | 2014-04-01 18:00:07 +0200 | [diff] [blame] | 2149 | const char *sep = ""; |
Manuel Pégourié-Gonnard | f6f4ab4 | 2014-04-01 17:32:44 +0200 | [diff] [blame] | 2150 | |
| 2151 | while( cur != NULL ) |
| 2152 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2153 | if( mbedtls_oid_get_extended_key_usage( &cur->buf, &desc ) != 0 ) |
Manuel Pégourié-Gonnard | f6f4ab4 | 2014-04-01 17:32:44 +0200 | [diff] [blame] | 2154 | desc = "???"; |
| 2155 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2156 | ret = mbedtls_snprintf( p, n, "%s%s", sep, desc ); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 2157 | MBEDTLS_X509_SAFE_SNPRINTF; |
Manuel Pégourié-Gonnard | f6f4ab4 | 2014-04-01 17:32:44 +0200 | [diff] [blame] | 2158 | |
Manuel Pégourié-Gonnard | 7b30cfc | 2014-04-01 18:00:07 +0200 | [diff] [blame] | 2159 | sep = ", "; |
| 2160 | |
Manuel Pégourié-Gonnard | f6f4ab4 | 2014-04-01 17:32:44 +0200 | [diff] [blame] | 2161 | cur = cur->next; |
| 2162 | } |
| 2163 | |
| 2164 | *size = n; |
| 2165 | *buf = p; |
| 2166 | |
| 2167 | return( 0 ); |
| 2168 | } |
| 2169 | |
Ron Eldor | 74d9acc | 2019-03-21 14:00:03 +0200 | [diff] [blame] | 2170 | static int x509_info_cert_policies( char **buf, size_t *size, |
| 2171 | const mbedtls_x509_sequence *certificate_policies ) |
| 2172 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2173 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Ron Eldor | 74d9acc | 2019-03-21 14:00:03 +0200 | [diff] [blame] | 2174 | const char *desc; |
| 2175 | size_t n = *size; |
| 2176 | char *p = *buf; |
| 2177 | const mbedtls_x509_sequence *cur = certificate_policies; |
| 2178 | const char *sep = ""; |
| 2179 | |
| 2180 | while( cur != NULL ) |
| 2181 | { |
| 2182 | if( mbedtls_oid_get_certificate_policies( &cur->buf, &desc ) != 0 ) |
| 2183 | desc = "???"; |
| 2184 | |
| 2185 | ret = mbedtls_snprintf( p, n, "%s%s", sep, desc ); |
| 2186 | MBEDTLS_X509_SAFE_SNPRINTF; |
| 2187 | |
| 2188 | sep = ", "; |
| 2189 | |
| 2190 | cur = cur->next; |
| 2191 | } |
| 2192 | |
| 2193 | *size = n; |
| 2194 | *buf = p; |
| 2195 | |
| 2196 | return( 0 ); |
| 2197 | } |
| 2198 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 2199 | /* |
| 2200 | * Return an informational string about the certificate. |
| 2201 | */ |
Manuel Pégourié-Gonnard | b28487d | 2014-04-01 12:19:09 +0200 | [diff] [blame] | 2202 | #define BEFORE_COLON 18 |
| 2203 | #define BC "18" |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2204 | int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix, |
| 2205 | const mbedtls_x509_crt *crt ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 2206 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2207 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 2208 | size_t n; |
| 2209 | char *p; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 2210 | char key_size_str[BEFORE_COLON]; |
| 2211 | |
| 2212 | p = buf; |
| 2213 | n = size; |
| 2214 | |
Janos Follath | 98e28a7 | 2016-05-31 14:03:54 +0100 | [diff] [blame] | 2215 | if( NULL == crt ) |
| 2216 | { |
| 2217 | ret = mbedtls_snprintf( p, n, "\nCertificate is uninitialised!\n" ); |
| 2218 | MBEDTLS_X509_SAFE_SNPRINTF; |
| 2219 | |
| 2220 | return( (int) ( size - n ) ); |
| 2221 | } |
| 2222 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2223 | ret = mbedtls_snprintf( p, n, "%scert. version : %d\n", |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 2224 | prefix, crt->version ); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 2225 | MBEDTLS_X509_SAFE_SNPRINTF; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2226 | ret = mbedtls_snprintf( p, n, "%sserial number : ", |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 2227 | prefix ); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 2228 | MBEDTLS_X509_SAFE_SNPRINTF; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 2229 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2230 | ret = mbedtls_x509_serial_gets( p, n, &crt->serial ); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 2231 | MBEDTLS_X509_SAFE_SNPRINTF; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 2232 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2233 | ret = mbedtls_snprintf( p, n, "\n%sissuer name : ", prefix ); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 2234 | MBEDTLS_X509_SAFE_SNPRINTF; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2235 | ret = mbedtls_x509_dn_gets( p, n, &crt->issuer ); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 2236 | MBEDTLS_X509_SAFE_SNPRINTF; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 2237 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2238 | ret = mbedtls_snprintf( p, n, "\n%ssubject name : ", prefix ); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 2239 | MBEDTLS_X509_SAFE_SNPRINTF; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2240 | ret = mbedtls_x509_dn_gets( p, n, &crt->subject ); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 2241 | MBEDTLS_X509_SAFE_SNPRINTF; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 2242 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2243 | ret = mbedtls_snprintf( p, n, "\n%sissued on : " \ |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 2244 | "%04d-%02d-%02d %02d:%02d:%02d", prefix, |
| 2245 | crt->valid_from.year, crt->valid_from.mon, |
| 2246 | crt->valid_from.day, crt->valid_from.hour, |
| 2247 | crt->valid_from.min, crt->valid_from.sec ); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 2248 | MBEDTLS_X509_SAFE_SNPRINTF; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 2249 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2250 | ret = mbedtls_snprintf( p, n, "\n%sexpires on : " \ |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 2251 | "%04d-%02d-%02d %02d:%02d:%02d", prefix, |
| 2252 | crt->valid_to.year, crt->valid_to.mon, |
| 2253 | crt->valid_to.day, crt->valid_to.hour, |
| 2254 | crt->valid_to.min, crt->valid_to.sec ); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 2255 | MBEDTLS_X509_SAFE_SNPRINTF; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 2256 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2257 | ret = mbedtls_snprintf( p, n, "\n%ssigned using : ", prefix ); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 2258 | MBEDTLS_X509_SAFE_SNPRINTF; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 2259 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2260 | ret = mbedtls_x509_sig_alg_gets( p, n, &crt->sig_oid, crt->sig_pk, |
Manuel Pégourié-Gonnard | bf696d0 | 2014-06-05 17:07:30 +0200 | [diff] [blame] | 2261 | crt->sig_md, crt->sig_opts ); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 2262 | MBEDTLS_X509_SAFE_SNPRINTF; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 2263 | |
Manuel Pégourié-Gonnard | b28487d | 2014-04-01 12:19:09 +0200 | [diff] [blame] | 2264 | /* Key size */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2265 | if( ( ret = mbedtls_x509_key_size_helper( key_size_str, BEFORE_COLON, |
| 2266 | mbedtls_pk_get_name( &crt->pk ) ) ) != 0 ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 2267 | { |
| 2268 | return( ret ); |
| 2269 | } |
| 2270 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2271 | ret = mbedtls_snprintf( p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str, |
Manuel Pégourié-Gonnard | 097c7bb | 2015-06-18 16:43:38 +0200 | [diff] [blame] | 2272 | (int) mbedtls_pk_get_bitlen( &crt->pk ) ); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 2273 | MBEDTLS_X509_SAFE_SNPRINTF; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 2274 | |
Manuel Pégourié-Gonnard | b28487d | 2014-04-01 12:19:09 +0200 | [diff] [blame] | 2275 | /* |
| 2276 | * Optional extensions |
| 2277 | */ |
| 2278 | |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 2279 | if( crt->ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS ) |
Manuel Pégourié-Gonnard | b28487d | 2014-04-01 12:19:09 +0200 | [diff] [blame] | 2280 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2281 | ret = mbedtls_snprintf( p, n, "\n%sbasic constraints : CA=%s", prefix, |
Manuel Pégourié-Gonnard | b28487d | 2014-04-01 12:19:09 +0200 | [diff] [blame] | 2282 | crt->ca_istrue ? "true" : "false" ); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 2283 | MBEDTLS_X509_SAFE_SNPRINTF; |
Manuel Pégourié-Gonnard | b28487d | 2014-04-01 12:19:09 +0200 | [diff] [blame] | 2284 | |
| 2285 | if( crt->max_pathlen > 0 ) |
| 2286 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2287 | ret = mbedtls_snprintf( p, n, ", max_pathlen=%d", crt->max_pathlen - 1 ); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 2288 | MBEDTLS_X509_SAFE_SNPRINTF; |
Manuel Pégourié-Gonnard | b28487d | 2014-04-01 12:19:09 +0200 | [diff] [blame] | 2289 | } |
| 2290 | } |
| 2291 | |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 2292 | if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME ) |
Manuel Pégourié-Gonnard | b28487d | 2014-04-01 12:19:09 +0200 | [diff] [blame] | 2293 | { |
Ron Eldor | b2dc3fa | 2019-03-21 13:40:13 +0200 | [diff] [blame] | 2294 | ret = mbedtls_snprintf( p, n, "\n%ssubject alt name :", prefix ); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 2295 | MBEDTLS_X509_SAFE_SNPRINTF; |
Manuel Pégourié-Gonnard | bce2b30 | 2014-04-01 13:43:28 +0200 | [diff] [blame] | 2296 | |
| 2297 | if( ( ret = x509_info_subject_alt_name( &p, &n, |
Ron Eldor | b2dc3fa | 2019-03-21 13:40:13 +0200 | [diff] [blame] | 2298 | &crt->subject_alt_names, |
Ron Eldor | 6aeae9e | 2019-05-20 12:00:36 +0300 | [diff] [blame] | 2299 | prefix ) ) != 0 ) |
Manuel Pégourié-Gonnard | bce2b30 | 2014-04-01 13:43:28 +0200 | [diff] [blame] | 2300 | return( ret ); |
Manuel Pégourié-Gonnard | b28487d | 2014-04-01 12:19:09 +0200 | [diff] [blame] | 2301 | } |
| 2302 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2303 | if( crt->ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE ) |
Manuel Pégourié-Gonnard | b28487d | 2014-04-01 12:19:09 +0200 | [diff] [blame] | 2304 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2305 | ret = mbedtls_snprintf( p, n, "\n%scert. type : ", prefix ); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 2306 | MBEDTLS_X509_SAFE_SNPRINTF; |
Manuel Pégourié-Gonnard | 919f8f5 | 2014-04-01 13:01:11 +0200 | [diff] [blame] | 2307 | |
| 2308 | if( ( ret = x509_info_cert_type( &p, &n, crt->ns_cert_type ) ) != 0 ) |
| 2309 | return( ret ); |
Manuel Pégourié-Gonnard | b28487d | 2014-04-01 12:19:09 +0200 | [diff] [blame] | 2310 | } |
| 2311 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2312 | if( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE ) |
Manuel Pégourié-Gonnard | b28487d | 2014-04-01 12:19:09 +0200 | [diff] [blame] | 2313 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2314 | ret = mbedtls_snprintf( p, n, "\n%skey usage : ", prefix ); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 2315 | MBEDTLS_X509_SAFE_SNPRINTF; |
Manuel Pégourié-Gonnard | 65c2ddc | 2014-04-01 14:12:11 +0200 | [diff] [blame] | 2316 | |
| 2317 | if( ( ret = x509_info_key_usage( &p, &n, crt->key_usage ) ) != 0 ) |
| 2318 | return( ret ); |
Manuel Pégourié-Gonnard | b28487d | 2014-04-01 12:19:09 +0200 | [diff] [blame] | 2319 | } |
| 2320 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2321 | if( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE ) |
Manuel Pégourié-Gonnard | b28487d | 2014-04-01 12:19:09 +0200 | [diff] [blame] | 2322 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2323 | ret = mbedtls_snprintf( p, n, "\n%sext key usage : ", prefix ); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 2324 | MBEDTLS_X509_SAFE_SNPRINTF; |
Manuel Pégourié-Gonnard | f6f4ab4 | 2014-04-01 17:32:44 +0200 | [diff] [blame] | 2325 | |
| 2326 | if( ( ret = x509_info_ext_key_usage( &p, &n, |
| 2327 | &crt->ext_key_usage ) ) != 0 ) |
| 2328 | return( ret ); |
Manuel Pégourié-Gonnard | b28487d | 2014-04-01 12:19:09 +0200 | [diff] [blame] | 2329 | } |
| 2330 | |
Ron Eldor | 74d9acc | 2019-03-21 14:00:03 +0200 | [diff] [blame] | 2331 | if( crt->ext_types & MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES ) |
| 2332 | { |
| 2333 | ret = mbedtls_snprintf( p, n, "\n%scertificate policies : ", prefix ); |
| 2334 | MBEDTLS_X509_SAFE_SNPRINTF; |
| 2335 | |
| 2336 | if( ( ret = x509_info_cert_policies( &p, &n, |
| 2337 | &crt->certificate_policies ) ) != 0 ) |
| 2338 | return( ret ); |
| 2339 | } |
| 2340 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2341 | ret = mbedtls_snprintf( p, n, "\n" ); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 2342 | MBEDTLS_X509_SAFE_SNPRINTF; |
Manuel Pégourié-Gonnard | b28487d | 2014-04-01 12:19:09 +0200 | [diff] [blame] | 2343 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 2344 | return( (int) ( size - n ) ); |
| 2345 | } |
| 2346 | |
Manuel Pégourié-Gonnard | b5f48ad | 2015-04-20 10:38:13 +0100 | [diff] [blame] | 2347 | struct x509_crt_verify_string { |
| 2348 | int code; |
| 2349 | const char *string; |
| 2350 | }; |
| 2351 | |
Hanno Becker | 7ac83f9 | 2020-10-09 10:48:22 +0100 | [diff] [blame] | 2352 | #define X509_CRT_ERROR_INFO( err, err_str, info ) { err, info }, |
Manuel Pégourié-Gonnard | b5f48ad | 2015-04-20 10:38:13 +0100 | [diff] [blame] | 2353 | static const struct x509_crt_verify_string x509_crt_verify_strings[] = { |
Hanno Becker | 7ac83f9 | 2020-10-09 10:48:22 +0100 | [diff] [blame] | 2354 | MBEDTLS_X509_CRT_ERROR_INFO_LIST |
Manuel Pégourié-Gonnard | b5f48ad | 2015-04-20 10:38:13 +0100 | [diff] [blame] | 2355 | { 0, NULL } |
| 2356 | }; |
Hanno Becker | 7ac83f9 | 2020-10-09 10:48:22 +0100 | [diff] [blame] | 2357 | #undef X509_CRT_ERROR_INFO |
Manuel Pégourié-Gonnard | b5f48ad | 2015-04-20 10:38:13 +0100 | [diff] [blame] | 2358 | |
| 2359 | int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix, |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 2360 | uint32_t flags ) |
Manuel Pégourié-Gonnard | b5f48ad | 2015-04-20 10:38:13 +0100 | [diff] [blame] | 2361 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2362 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | b5f48ad | 2015-04-20 10:38:13 +0100 | [diff] [blame] | 2363 | const struct x509_crt_verify_string *cur; |
| 2364 | char *p = buf; |
| 2365 | size_t n = size; |
| 2366 | |
| 2367 | for( cur = x509_crt_verify_strings; cur->string != NULL ; cur++ ) |
| 2368 | { |
| 2369 | if( ( flags & cur->code ) == 0 ) |
| 2370 | continue; |
| 2371 | |
| 2372 | ret = mbedtls_snprintf( p, n, "%s%s\n", prefix, cur->string ); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 2373 | MBEDTLS_X509_SAFE_SNPRINTF; |
Manuel Pégourié-Gonnard | b5f48ad | 2015-04-20 10:38:13 +0100 | [diff] [blame] | 2374 | flags ^= cur->code; |
| 2375 | } |
| 2376 | |
| 2377 | if( flags != 0 ) |
| 2378 | { |
| 2379 | ret = mbedtls_snprintf( p, n, "%sUnknown reason " |
| 2380 | "(this should not happen)\n", prefix ); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 2381 | MBEDTLS_X509_SAFE_SNPRINTF; |
Manuel Pégourié-Gonnard | b5f48ad | 2015-04-20 10:38:13 +0100 | [diff] [blame] | 2382 | } |
| 2383 | |
| 2384 | return( (int) ( size - n ) ); |
| 2385 | } |
Hanno Becker | 612a2f1 | 2020-10-09 09:19:39 +0100 | [diff] [blame] | 2386 | #endif /* MBEDTLS_X509_REMOVE_INFO */ |
Manuel Pégourié-Gonnard | b5f48ad | 2015-04-20 10:38:13 +0100 | [diff] [blame] | 2387 | |
Manuel Pégourié-Gonnard | 655a964 | 2015-06-23 10:48:44 +0200 | [diff] [blame] | 2388 | int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt, |
| 2389 | unsigned int usage ) |
Manuel Pégourié-Gonnard | 603116c | 2014-04-09 09:50:03 +0200 | [diff] [blame] | 2390 | { |
Manuel Pégourié-Gonnard | 655a964 | 2015-06-23 10:48:44 +0200 | [diff] [blame] | 2391 | unsigned int usage_must, usage_may; |
| 2392 | unsigned int may_mask = MBEDTLS_X509_KU_ENCIPHER_ONLY |
| 2393 | | MBEDTLS_X509_KU_DECIPHER_ONLY; |
| 2394 | |
| 2395 | if( ( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE ) == 0 ) |
| 2396 | return( 0 ); |
| 2397 | |
| 2398 | usage_must = usage & ~may_mask; |
| 2399 | |
| 2400 | if( ( ( crt->key_usage & ~may_mask ) & usage_must ) != usage_must ) |
| 2401 | return( MBEDTLS_ERR_X509_BAD_INPUT_DATA ); |
| 2402 | |
| 2403 | usage_may = usage & may_mask; |
| 2404 | |
| 2405 | if( ( ( crt->key_usage & may_mask ) | usage_may ) != usage_may ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2406 | return( MBEDTLS_ERR_X509_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 603116c | 2014-04-09 09:50:03 +0200 | [diff] [blame] | 2407 | |
| 2408 | return( 0 ); |
| 2409 | } |
Manuel Pégourié-Gonnard | 603116c | 2014-04-09 09:50:03 +0200 | [diff] [blame] | 2410 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2411 | int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt, |
Manuel Pégourié-Gonnard | 7afb8a0 | 2014-04-10 17:53:56 +0200 | [diff] [blame] | 2412 | const char *usage_oid, |
| 2413 | size_t usage_len ) |
| 2414 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2415 | const mbedtls_x509_sequence *cur; |
Manuel Pégourié-Gonnard | 7afb8a0 | 2014-04-10 17:53:56 +0200 | [diff] [blame] | 2416 | |
| 2417 | /* Extension is not mandatory, absent means no restriction */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2418 | if( ( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE ) == 0 ) |
Manuel Pégourié-Gonnard | 7afb8a0 | 2014-04-10 17:53:56 +0200 | [diff] [blame] | 2419 | return( 0 ); |
| 2420 | |
| 2421 | /* |
| 2422 | * Look for the requested usage (or wildcard ANY) in our list |
| 2423 | */ |
| 2424 | for( cur = &crt->ext_key_usage; cur != NULL; cur = cur->next ) |
| 2425 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2426 | const mbedtls_x509_buf *cur_oid = &cur->buf; |
Manuel Pégourié-Gonnard | 7afb8a0 | 2014-04-10 17:53:56 +0200 | [diff] [blame] | 2427 | |
| 2428 | if( cur_oid->len == usage_len && |
| 2429 | memcmp( cur_oid->p, usage_oid, usage_len ) == 0 ) |
| 2430 | { |
| 2431 | return( 0 ); |
| 2432 | } |
| 2433 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2434 | if( MBEDTLS_OID_CMP( MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE, cur_oid ) == 0 ) |
Manuel Pégourié-Gonnard | 7afb8a0 | 2014-04-10 17:53:56 +0200 | [diff] [blame] | 2435 | return( 0 ); |
| 2436 | } |
| 2437 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2438 | return( MBEDTLS_ERR_X509_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 7afb8a0 | 2014-04-10 17:53:56 +0200 | [diff] [blame] | 2439 | } |
Manuel Pégourié-Gonnard | 7afb8a0 | 2014-04-10 17:53:56 +0200 | [diff] [blame] | 2440 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2441 | #if defined(MBEDTLS_X509_CRL_PARSE_C) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 2442 | /* |
| 2443 | * Return 1 if the certificate is revoked, or 0 otherwise. |
| 2444 | */ |
Manuel Pégourié-Gonnard | c730ed3 | 2015-06-02 10:38:50 +0100 | [diff] [blame] | 2445 | int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 2446 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2447 | const mbedtls_x509_crl_entry *cur = &crl->entry; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 2448 | |
| 2449 | while( cur != NULL && cur->serial.len != 0 ) |
| 2450 | { |
| 2451 | if( crt->serial.len == cur->serial.len && |
| 2452 | memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 ) |
| 2453 | { |
Raoul Strackx | a4e8614 | 2020-06-15 17:03:13 +0200 | [diff] [blame] | 2454 | return( 1 ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 2455 | } |
| 2456 | |
| 2457 | cur = cur->next; |
| 2458 | } |
| 2459 | |
| 2460 | return( 0 ); |
| 2461 | } |
| 2462 | |
| 2463 | /* |
Manuel Pégourié-Gonnard | eeef947 | 2016-02-22 11:36:55 +0100 | [diff] [blame] | 2464 | * Check that the given certificate is not revoked according to the CRL. |
Manuel Pégourié-Gonnard | 08eacec | 2017-10-18 14:20:24 +0200 | [diff] [blame] | 2465 | * Skip validation if no CRL for the given CA is present. |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 2466 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2467 | static int x509_crt_verifycrl( mbedtls_x509_crt *crt, mbedtls_x509_crt *ca, |
Manuel Pégourié-Gonnard | 9505164 | 2015-06-15 10:39:46 +0200 | [diff] [blame] | 2468 | mbedtls_x509_crl *crl_list, |
| 2469 | const mbedtls_x509_crt_profile *profile ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 2470 | { |
| 2471 | int flags = 0; |
Przemek Stekiel | 40afdd2 | 2022-09-06 13:08:28 +0200 | [diff] [blame] | 2472 | unsigned char hash[MBEDTLS_HASH_MAX_SIZE]; |
pespacek | 7599a77 | 2022-02-07 14:40:55 +0100 | [diff] [blame] | 2473 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
pespacek | 7599a77 | 2022-02-07 14:40:55 +0100 | [diff] [blame] | 2474 | psa_algorithm_t psa_algorithm; |
| 2475 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2476 | const mbedtls_md_info_t *md_info; |
pespacek | 7599a77 | 2022-02-07 14:40:55 +0100 | [diff] [blame] | 2477 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 2478 | size_t hash_length; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 2479 | |
| 2480 | if( ca == NULL ) |
| 2481 | return( flags ); |
| 2482 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 2483 | while( crl_list != NULL ) |
| 2484 | { |
| 2485 | if( crl_list->version == 0 || |
Hanno Becker | b75ffb5 | 2018-11-02 09:19:54 +0000 | [diff] [blame] | 2486 | x509_name_cmp( &crl_list->issuer, &ca->subject ) != 0 ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 2487 | { |
| 2488 | crl_list = crl_list->next; |
| 2489 | continue; |
| 2490 | } |
| 2491 | |
| 2492 | /* |
Manuel Pégourié-Gonnard | 99d4f19 | 2014-04-08 15:10:07 +0200 | [diff] [blame] | 2493 | * Check if the CA is configured to sign CRLs |
| 2494 | */ |
Hanno Becker | b75ffb5 | 2018-11-02 09:19:54 +0000 | [diff] [blame] | 2495 | if( mbedtls_x509_crt_check_key_usage( ca, |
| 2496 | MBEDTLS_X509_KU_CRL_SIGN ) != 0 ) |
Manuel Pégourié-Gonnard | 99d4f19 | 2014-04-08 15:10:07 +0200 | [diff] [blame] | 2497 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2498 | flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED; |
Manuel Pégourié-Gonnard | 99d4f19 | 2014-04-08 15:10:07 +0200 | [diff] [blame] | 2499 | break; |
| 2500 | } |
Manuel Pégourié-Gonnard | 99d4f19 | 2014-04-08 15:10:07 +0200 | [diff] [blame] | 2501 | |
| 2502 | /* |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 2503 | * Check if CRL is correctly signed by the trusted CA |
| 2504 | */ |
Manuel Pégourié-Gonnard | cbb1f6e | 2015-06-15 16:17:55 +0200 | [diff] [blame] | 2505 | if( x509_profile_check_md_alg( profile, crl_list->sig_md ) != 0 ) |
| 2506 | flags |= MBEDTLS_X509_BADCRL_BAD_MD; |
| 2507 | |
| 2508 | if( x509_profile_check_pk_alg( profile, crl_list->sig_pk ) != 0 ) |
| 2509 | flags |= MBEDTLS_X509_BADCRL_BAD_PK; |
| 2510 | |
pespacek | 7599a77 | 2022-02-07 14:40:55 +0100 | [diff] [blame] | 2511 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Manuel Pégourié-Gonnard | abac037 | 2022-07-18 13:41:11 +0200 | [diff] [blame] | 2512 | psa_algorithm = mbedtls_hash_info_psa_from_md( crl_list->sig_md ); |
pespacek | d924e55 | 2022-02-28 11:49:54 +0100 | [diff] [blame] | 2513 | if( psa_hash_compute( psa_algorithm, |
| 2514 | crl_list->tbs.p, |
| 2515 | crl_list->tbs.len, |
| 2516 | hash, |
| 2517 | sizeof( hash ), |
| 2518 | &hash_length ) != PSA_SUCCESS ) |
pespacek | a7a6469 | 2022-02-14 15:18:43 +0100 | [diff] [blame] | 2519 | { |
| 2520 | /* Note: this can't happen except after an internal error */ |
| 2521 | flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED; |
| 2522 | break; |
| 2523 | } |
pespacek | 7599a77 | 2022-02-07 14:40:55 +0100 | [diff] [blame] | 2524 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2525 | md_info = mbedtls_md_info_from_type( crl_list->sig_md ); |
pespacek | 7599a77 | 2022-02-07 14:40:55 +0100 | [diff] [blame] | 2526 | hash_length = mbedtls_md_get_size( md_info ); |
| 2527 | if( mbedtls_md( md_info, |
| 2528 | crl_list->tbs.p, |
| 2529 | crl_list->tbs.len, |
| 2530 | hash ) != 0 ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 2531 | { |
Manuel Pégourié-Gonnard | 329e78c | 2017-06-26 12:22:17 +0200 | [diff] [blame] | 2532 | /* Note: this can't happen except after an internal error */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2533 | flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 2534 | break; |
| 2535 | } |
pespacek | a7a6469 | 2022-02-14 15:18:43 +0100 | [diff] [blame] | 2536 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 2537 | |
Manuel Pégourié-Gonnard | 3f81691 | 2017-10-26 10:24:16 +0200 | [diff] [blame] | 2538 | if( x509_profile_check_key( profile, &ca->pk ) != 0 ) |
Manuel Pégourié-Gonnard | cbb1f6e | 2015-06-15 16:17:55 +0200 | [diff] [blame] | 2539 | flags |= MBEDTLS_X509_BADCERT_BAD_KEY; |
Manuel Pégourié-Gonnard | 9505164 | 2015-06-15 10:39:46 +0200 | [diff] [blame] | 2540 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2541 | if( mbedtls_pk_verify_ext( crl_list->sig_pk, crl_list->sig_opts, &ca->pk, |
pespacek | 7599a77 | 2022-02-07 14:40:55 +0100 | [diff] [blame] | 2542 | crl_list->sig_md, hash, hash_length, |
Manuel Pégourié-Gonnard | 5388202 | 2014-06-05 17:53:52 +0200 | [diff] [blame] | 2543 | crl_list->sig.p, crl_list->sig.len ) != 0 ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 2544 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2545 | flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 2546 | break; |
| 2547 | } |
| 2548 | |
| 2549 | /* |
| 2550 | * Check for validity of CRL (Do not drop out) |
| 2551 | */ |
Manuel Pégourié-Gonnard | c730ed3 | 2015-06-02 10:38:50 +0100 | [diff] [blame] | 2552 | if( mbedtls_x509_time_is_past( &crl_list->next_update ) ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2553 | flags |= MBEDTLS_X509_BADCRL_EXPIRED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 2554 | |
Manuel Pégourié-Gonnard | c730ed3 | 2015-06-02 10:38:50 +0100 | [diff] [blame] | 2555 | if( mbedtls_x509_time_is_future( &crl_list->this_update ) ) |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 2556 | flags |= MBEDTLS_X509_BADCRL_FUTURE; |
Manuel Pégourié-Gonnard | 9533765 | 2014-03-10 13:15:18 +0100 | [diff] [blame] | 2557 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 2558 | /* |
| 2559 | * Check if certificate is revoked |
| 2560 | */ |
Manuel Pégourié-Gonnard | c730ed3 | 2015-06-02 10:38:50 +0100 | [diff] [blame] | 2561 | if( mbedtls_x509_crt_is_revoked( crt, crl_list ) ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 2562 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2563 | flags |= MBEDTLS_X509_BADCERT_REVOKED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 2564 | break; |
| 2565 | } |
| 2566 | |
| 2567 | crl_list = crl_list->next; |
| 2568 | } |
Manuel Pégourié-Gonnard | cbb1f6e | 2015-06-15 16:17:55 +0200 | [diff] [blame] | 2569 | |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 2570 | return( flags ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 2571 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2572 | #endif /* MBEDTLS_X509_CRL_PARSE_C */ |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 2573 | |
Manuel Pégourié-Gonnard | 8842124 | 2014-10-17 11:36:18 +0200 | [diff] [blame] | 2574 | /* |
Manuel Pégourié-Gonnard | f82a4d5 | 2017-07-03 19:26:25 +0200 | [diff] [blame] | 2575 | * Check the signature of a certificate by its parent |
| 2576 | */ |
| 2577 | static int x509_crt_check_signature( const mbedtls_x509_crt *child, |
Manuel Pégourié-Gonnard | a4a5d1d | 2017-07-17 10:26:19 +0200 | [diff] [blame] | 2578 | mbedtls_x509_crt *parent, |
| 2579 | mbedtls_x509_crt_restart_ctx *rs_ctx ) |
Manuel Pégourié-Gonnard | f82a4d5 | 2017-07-03 19:26:25 +0200 | [diff] [blame] | 2580 | { |
Andrzej Kurek | d4a6553 | 2018-10-31 06:18:39 -0400 | [diff] [blame] | 2581 | size_t hash_len; |
Przemek Stekiel | 5166954 | 2022-09-13 12:57:05 +0200 | [diff] [blame] | 2582 | unsigned char hash[MBEDTLS_HASH_MAX_SIZE]; |
Andrzej Kurek | d4a6553 | 2018-10-31 06:18:39 -0400 | [diff] [blame] | 2583 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) |
| 2584 | const mbedtls_md_info_t *md_info; |
Manuel Pégourié-Gonnard | f82a4d5 | 2017-07-03 19:26:25 +0200 | [diff] [blame] | 2585 | md_info = mbedtls_md_info_from_type( child->sig_md ); |
Andrzej Kurek | d4a6553 | 2018-10-31 06:18:39 -0400 | [diff] [blame] | 2586 | hash_len = mbedtls_md_get_size( md_info ); |
Andrzej Kurek | 8b38ff5 | 2018-11-20 03:20:09 -0500 | [diff] [blame] | 2587 | |
Andrzej Kurek | d4a6553 | 2018-10-31 06:18:39 -0400 | [diff] [blame] | 2588 | /* Note: hash errors can happen only after an internal error */ |
Manuel Pégourié-Gonnard | f82a4d5 | 2017-07-03 19:26:25 +0200 | [diff] [blame] | 2589 | if( mbedtls_md( md_info, child->tbs.p, child->tbs.len, hash ) != 0 ) |
Andrzej Kurek | d4a6553 | 2018-10-31 06:18:39 -0400 | [diff] [blame] | 2590 | return( -1 ); |
| 2591 | #else |
Manuel Pégourié-Gonnard | abac037 | 2022-07-18 13:41:11 +0200 | [diff] [blame] | 2592 | psa_algorithm_t hash_alg = mbedtls_hash_info_psa_from_md( child->sig_md ); |
pespacek | 7599a77 | 2022-02-07 14:40:55 +0100 | [diff] [blame] | 2593 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Andrzej Kurek | d4a6553 | 2018-10-31 06:18:39 -0400 | [diff] [blame] | 2594 | |
pespacek | 7599a77 | 2022-02-07 14:40:55 +0100 | [diff] [blame] | 2595 | status = psa_hash_compute( hash_alg, |
| 2596 | child->tbs.p, |
| 2597 | child->tbs.len, |
| 2598 | hash, |
pespacek | b9f07a7 | 2022-02-14 15:13:26 +0100 | [diff] [blame] | 2599 | sizeof( hash ), |
pespacek | 7599a77 | 2022-02-07 14:40:55 +0100 | [diff] [blame] | 2600 | &hash_len ); |
| 2601 | if( status != PSA_SUCCESS ) |
Manuel Pégourié-Gonnard | f82a4d5 | 2017-07-03 19:26:25 +0200 | [diff] [blame] | 2602 | { |
pespacek | 3110c7b | 2022-02-14 15:07:41 +0100 | [diff] [blame] | 2603 | return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED ); |
Manuel Pégourié-Gonnard | f82a4d5 | 2017-07-03 19:26:25 +0200 | [diff] [blame] | 2604 | } |
| 2605 | |
Andrzej Kurek | d4a6553 | 2018-10-31 06:18:39 -0400 | [diff] [blame] | 2606 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | a4a5d1d | 2017-07-17 10:26:19 +0200 | [diff] [blame] | 2607 | /* Skip expensive computation on obvious mismatch */ |
| 2608 | if( ! mbedtls_pk_can_do( &parent->pk, child->sig_pk ) ) |
Manuel Pégourié-Gonnard | f82a4d5 | 2017-07-03 19:26:25 +0200 | [diff] [blame] | 2609 | return( -1 ); |
Manuel Pégourié-Gonnard | f82a4d5 | 2017-07-03 19:26:25 +0200 | [diff] [blame] | 2610 | |
Manuel Pégourié-Gonnard | 8b59049 | 2017-08-14 18:04:19 +0200 | [diff] [blame] | 2611 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
Manuel Pégourié-Gonnard | a4a5d1d | 2017-07-17 10:26:19 +0200 | [diff] [blame] | 2612 | if( rs_ctx != NULL && child->sig_pk == MBEDTLS_PK_ECDSA ) |
| 2613 | { |
| 2614 | return( mbedtls_pk_verify_restartable( &parent->pk, |
Andrzej Kurek | d4a6553 | 2018-10-31 06:18:39 -0400 | [diff] [blame] | 2615 | child->sig_md, hash, hash_len, |
Manuel Pégourié-Gonnard | 15d7df2 | 2017-08-17 14:33:31 +0200 | [diff] [blame] | 2616 | child->sig.p, child->sig.len, &rs_ctx->pk ) ); |
Manuel Pégourié-Gonnard | a4a5d1d | 2017-07-17 10:26:19 +0200 | [diff] [blame] | 2617 | } |
| 2618 | #else |
| 2619 | (void) rs_ctx; |
| 2620 | #endif |
| 2621 | |
| 2622 | return( mbedtls_pk_verify_ext( child->sig_pk, child->sig_opts, &parent->pk, |
Andrzej Kurek | d4a6553 | 2018-10-31 06:18:39 -0400 | [diff] [blame] | 2623 | child->sig_md, hash, hash_len, |
Manuel Pégourié-Gonnard | a4a5d1d | 2017-07-17 10:26:19 +0200 | [diff] [blame] | 2624 | child->sig.p, child->sig.len ) ); |
Manuel Pégourié-Gonnard | f82a4d5 | 2017-07-03 19:26:25 +0200 | [diff] [blame] | 2625 | } |
| 2626 | |
| 2627 | /* |
Manuel Pégourié-Gonnard | 312010e | 2014-04-09 14:30:11 +0200 | [diff] [blame] | 2628 | * Check if 'parent' is a suitable parent (signing CA) for 'child'. |
| 2629 | * Return 0 if yes, -1 if not. |
Manuel Pégourié-Gonnard | d249b7a | 2014-06-24 11:49:16 +0200 | [diff] [blame] | 2630 | * |
| 2631 | * top means parent is a locally-trusted certificate |
Manuel Pégourié-Gonnard | 3fed0b3 | 2014-04-08 13:18:01 +0200 | [diff] [blame] | 2632 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2633 | static int x509_crt_check_parent( const mbedtls_x509_crt *child, |
| 2634 | const mbedtls_x509_crt *parent, |
Manuel Pégourié-Gonnard | 27e9479 | 2017-07-04 00:49:31 +0200 | [diff] [blame] | 2635 | int top ) |
Manuel Pégourié-Gonnard | 3fed0b3 | 2014-04-08 13:18:01 +0200 | [diff] [blame] | 2636 | { |
Manuel Pégourié-Gonnard | d249b7a | 2014-06-24 11:49:16 +0200 | [diff] [blame] | 2637 | int need_ca_bit; |
| 2638 | |
Manuel Pégourié-Gonnard | c4eff16 | 2014-06-19 12:18:08 +0200 | [diff] [blame] | 2639 | /* Parent must be the issuer */ |
Manuel Pégourié-Gonnard | ef9a6ae | 2014-10-17 12:25:12 +0200 | [diff] [blame] | 2640 | if( x509_name_cmp( &child->issuer, &parent->subject ) != 0 ) |
Manuel Pégourié-Gonnard | 312010e | 2014-04-09 14:30:11 +0200 | [diff] [blame] | 2641 | return( -1 ); |
Manuel Pégourié-Gonnard | 3fed0b3 | 2014-04-08 13:18:01 +0200 | [diff] [blame] | 2642 | |
Manuel Pégourié-Gonnard | d249b7a | 2014-06-24 11:49:16 +0200 | [diff] [blame] | 2643 | /* Parent must have the basicConstraints CA bit set as a general rule */ |
| 2644 | need_ca_bit = 1; |
| 2645 | |
| 2646 | /* Exception: v1/v2 certificates that are locally trusted. */ |
| 2647 | if( top && parent->version < 3 ) |
| 2648 | need_ca_bit = 0; |
| 2649 | |
Manuel Pégourié-Gonnard | d249b7a | 2014-06-24 11:49:16 +0200 | [diff] [blame] | 2650 | if( need_ca_bit && ! parent->ca_istrue ) |
| 2651 | return( -1 ); |
| 2652 | |
Manuel Pégourié-Gonnard | d249b7a | 2014-06-24 11:49:16 +0200 | [diff] [blame] | 2653 | if( need_ca_bit && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2654 | mbedtls_x509_crt_check_key_usage( parent, MBEDTLS_X509_KU_KEY_CERT_SIGN ) != 0 ) |
Manuel Pégourié-Gonnard | c4eff16 | 2014-06-19 12:18:08 +0200 | [diff] [blame] | 2655 | { |
| 2656 | return( -1 ); |
| 2657 | } |
Manuel Pégourié-Gonnard | 312010e | 2014-04-09 14:30:11 +0200 | [diff] [blame] | 2658 | |
| 2659 | return( 0 ); |
Manuel Pégourié-Gonnard | 3fed0b3 | 2014-04-08 13:18:01 +0200 | [diff] [blame] | 2660 | } |
| 2661 | |
Manuel Pégourié-Gonnard | 35407c7 | 2017-06-29 10:45:25 +0200 | [diff] [blame] | 2662 | /* |
Manuel Pégourié-Gonnard | 3e329b8 | 2017-06-29 12:55:27 +0200 | [diff] [blame] | 2663 | * Find a suitable parent for child in candidates, or return NULL. |
| 2664 | * |
| 2665 | * Here suitable is defined as: |
Manuel Pégourié-Gonnard | 2f09d59 | 2017-07-03 18:30:43 +0200 | [diff] [blame] | 2666 | * 1. subject name matches child's issuer |
| 2667 | * 2. if necessary, the CA bit is set and key usage allows signing certs |
| 2668 | * 3. for trusted roots, the signature is correct |
Manuel Pégourié-Gonnard | be4ff42 | 2017-07-14 12:04:14 +0200 | [diff] [blame] | 2669 | * (for intermediates, the signature is checked and the result reported) |
Manuel Pégourié-Gonnard | 2f09d59 | 2017-07-03 18:30:43 +0200 | [diff] [blame] | 2670 | * 4. pathlen constraints are satisfied |
Manuel Pégourié-Gonnard | 3e329b8 | 2017-06-29 12:55:27 +0200 | [diff] [blame] | 2671 | * |
Manuel Pégourié-Gonnard | 562df40 | 2017-08-08 18:09:14 +0200 | [diff] [blame] | 2672 | * If there's a suitable candidate which is also time-valid, return the first |
| 2673 | * such. Otherwise, return the first suitable candidate (or NULL if there is |
| 2674 | * none). |
Manuel Pégourié-Gonnard | 3e329b8 | 2017-06-29 12:55:27 +0200 | [diff] [blame] | 2675 | * |
| 2676 | * The rationale for this rule is that someone could have a list of trusted |
| 2677 | * roots with two versions on the same root with different validity periods. |
| 2678 | * (At least one user reported having such a list and wanted it to just work.) |
| 2679 | * The reason we don't just require time-validity is that generally there is |
| 2680 | * only one version, and if it's expired we want the flags to state that |
| 2681 | * rather than NOT_TRUSTED, as would be the case if we required it here. |
Manuel Pégourié-Gonnard | 2f09d59 | 2017-07-03 18:30:43 +0200 | [diff] [blame] | 2682 | * |
| 2683 | * The rationale for rule 3 (signature for trusted roots) is that users might |
| 2684 | * have two versions of the same CA with different keys in their list, and the |
Manuel Pégourié-Gonnard | bdc5440 | 2017-07-04 00:33:39 +0200 | [diff] [blame] | 2685 | * way we select the correct one is by checking the signature (as we don't |
| 2686 | * rely on key identifier extensions). (This is one way users might choose to |
| 2687 | * handle key rollover, another relies on self-issued certs, see [SIRO].) |
Manuel Pégourié-Gonnard | 98a6778 | 2017-08-17 10:52:20 +0200 | [diff] [blame] | 2688 | * |
| 2689 | * Arguments: |
Manuel Pégourié-Gonnard | e57d743 | 2018-03-07 10:00:57 +0100 | [diff] [blame] | 2690 | * - [in] child: certificate for which we're looking for a parent |
| 2691 | * - [in] candidates: chained list of potential parents |
Manuel Pégourié-Gonnard | da19f4c | 2018-06-12 12:40:54 +0200 | [diff] [blame] | 2692 | * - [out] r_parent: parent found (or NULL) |
| 2693 | * - [out] r_signature_is_good: 1 if child signature by parent is valid, or 0 |
Manuel Pégourié-Gonnard | e57d743 | 2018-03-07 10:00:57 +0100 | [diff] [blame] | 2694 | * - [in] top: 1 if candidates consists of trusted roots, ie we're at the top |
| 2695 | * of the chain, 0 otherwise |
| 2696 | * - [in] path_cnt: number of intermediates seen so far |
| 2697 | * - [in] self_cnt: number of self-signed intermediates seen so far |
| 2698 | * (will never be greater than path_cnt) |
Manuel Pégourié-Gonnard | da19f4c | 2018-06-12 12:40:54 +0200 | [diff] [blame] | 2699 | * - [in-out] rs_ctx: context for restarting operations |
Manuel Pégourié-Gonnard | e57d743 | 2018-03-07 10:00:57 +0100 | [diff] [blame] | 2700 | * |
| 2701 | * Return value: |
Manuel Pégourié-Gonnard | da19f4c | 2018-06-12 12:40:54 +0200 | [diff] [blame] | 2702 | * - 0 on success |
| 2703 | * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise |
Manuel Pégourié-Gonnard | 2f1c33d | 2017-06-29 12:27:23 +0200 | [diff] [blame] | 2704 | */ |
Manuel Pégourié-Gonnard | a4a5d1d | 2017-07-17 10:26:19 +0200 | [diff] [blame] | 2705 | static int x509_crt_find_parent_in( |
| 2706 | mbedtls_x509_crt *child, |
| 2707 | mbedtls_x509_crt *candidates, |
| 2708 | mbedtls_x509_crt **r_parent, |
| 2709 | int *r_signature_is_good, |
| 2710 | int top, |
Manuel Pégourié-Gonnard | bb216bd | 2017-08-28 13:25:55 +0200 | [diff] [blame] | 2711 | unsigned path_cnt, |
| 2712 | unsigned self_cnt, |
Manuel Pégourié-Gonnard | a4a5d1d | 2017-07-17 10:26:19 +0200 | [diff] [blame] | 2713 | mbedtls_x509_crt_restart_ctx *rs_ctx ) |
Manuel Pégourié-Gonnard | 2f1c33d | 2017-06-29 12:27:23 +0200 | [diff] [blame] | 2714 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2715 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 8b59049 | 2017-08-14 18:04:19 +0200 | [diff] [blame] | 2716 | mbedtls_x509_crt *parent, *fallback_parent; |
Benjamin Kier | 3605073 | 2019-05-30 14:49:17 -0400 | [diff] [blame] | 2717 | int signature_is_good = 0, fallback_signature_is_good; |
Manuel Pégourié-Gonnard | 8b59049 | 2017-08-14 18:04:19 +0200 | [diff] [blame] | 2718 | |
| 2719 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
Manuel Pégourié-Gonnard | 3627a8b | 2017-08-23 11:20:48 +0200 | [diff] [blame] | 2720 | /* did we have something in progress? */ |
Manuel Pégourié-Gonnard | 8b59049 | 2017-08-14 18:04:19 +0200 | [diff] [blame] | 2721 | if( rs_ctx != NULL && rs_ctx->parent != NULL ) |
| 2722 | { |
Manuel Pégourié-Gonnard | 3627a8b | 2017-08-23 11:20:48 +0200 | [diff] [blame] | 2723 | /* restore saved state */ |
Manuel Pégourié-Gonnard | 8b59049 | 2017-08-14 18:04:19 +0200 | [diff] [blame] | 2724 | parent = rs_ctx->parent; |
| 2725 | fallback_parent = rs_ctx->fallback_parent; |
Manuel Pégourié-Gonnard | 78d7e8c | 2018-07-02 12:33:14 +0200 | [diff] [blame] | 2726 | fallback_signature_is_good = rs_ctx->fallback_signature_is_good; |
Manuel Pégourié-Gonnard | 8b59049 | 2017-08-14 18:04:19 +0200 | [diff] [blame] | 2727 | |
Manuel Pégourié-Gonnard | 3627a8b | 2017-08-23 11:20:48 +0200 | [diff] [blame] | 2728 | /* clear saved state */ |
| 2729 | rs_ctx->parent = NULL; |
| 2730 | rs_ctx->fallback_parent = NULL; |
Manuel Pégourié-Gonnard | 78d7e8c | 2018-07-02 12:33:14 +0200 | [diff] [blame] | 2731 | rs_ctx->fallback_signature_is_good = 0; |
Manuel Pégourié-Gonnard | 3627a8b | 2017-08-23 11:20:48 +0200 | [diff] [blame] | 2732 | |
| 2733 | /* resume where we left */ |
Manuel Pégourié-Gonnard | 8b59049 | 2017-08-14 18:04:19 +0200 | [diff] [blame] | 2734 | goto check_signature; |
| 2735 | } |
| 2736 | #endif |
| 2737 | |
| 2738 | fallback_parent = NULL; |
Manuel Pégourié-Gonnard | 78d7e8c | 2018-07-02 12:33:14 +0200 | [diff] [blame] | 2739 | fallback_signature_is_good = 0; |
Manuel Pégourié-Gonnard | 2f1c33d | 2017-06-29 12:27:23 +0200 | [diff] [blame] | 2740 | |
Manuel Pégourié-Gonnard | 2f1c33d | 2017-06-29 12:27:23 +0200 | [diff] [blame] | 2741 | for( parent = candidates; parent != NULL; parent = parent->next ) |
| 2742 | { |
Manuel Pégourié-Gonnard | 2f09d59 | 2017-07-03 18:30:43 +0200 | [diff] [blame] | 2743 | /* basic parenting skills (name, CA bit, key usage) */ |
Manuel Pégourié-Gonnard | 27e9479 | 2017-07-04 00:49:31 +0200 | [diff] [blame] | 2744 | if( x509_crt_check_parent( child, parent, top ) != 0 ) |
Manuel Pégourié-Gonnard | 2f1c33d | 2017-06-29 12:27:23 +0200 | [diff] [blame] | 2745 | continue; |
| 2746 | |
Manuel Pégourié-Gonnard | 9c6118c | 2017-06-29 12:38:42 +0200 | [diff] [blame] | 2747 | /* +1 because stored max_pathlen is 1 higher that the actual value */ |
| 2748 | if( parent->max_pathlen > 0 && |
Manuel Pégourié-Gonnard | bb216bd | 2017-08-28 13:25:55 +0200 | [diff] [blame] | 2749 | (size_t) parent->max_pathlen < 1 + path_cnt - self_cnt ) |
Manuel Pégourié-Gonnard | 9c6118c | 2017-06-29 12:38:42 +0200 | [diff] [blame] | 2750 | { |
| 2751 | continue; |
| 2752 | } |
| 2753 | |
Manuel Pégourié-Gonnard | 2f09d59 | 2017-07-03 18:30:43 +0200 | [diff] [blame] | 2754 | /* Signature */ |
Manuel Pégourié-Gonnard | 8b59049 | 2017-08-14 18:04:19 +0200 | [diff] [blame] | 2755 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
| 2756 | check_signature: |
| 2757 | #endif |
Manuel Pégourié-Gonnard | a4a5d1d | 2017-07-17 10:26:19 +0200 | [diff] [blame] | 2758 | ret = x509_crt_check_signature( child, parent, rs_ctx ); |
| 2759 | |
| 2760 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
Manuel Pégourié-Gonnard | 8b59049 | 2017-08-14 18:04:19 +0200 | [diff] [blame] | 2761 | if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS ) |
| 2762 | { |
| 2763 | /* save state */ |
| 2764 | rs_ctx->parent = parent; |
| 2765 | rs_ctx->fallback_parent = fallback_parent; |
Manuel Pégourié-Gonnard | 78d7e8c | 2018-07-02 12:33:14 +0200 | [diff] [blame] | 2766 | rs_ctx->fallback_signature_is_good = fallback_signature_is_good; |
Manuel Pégourié-Gonnard | 8b59049 | 2017-08-14 18:04:19 +0200 | [diff] [blame] | 2767 | |
Manuel Pégourié-Gonnard | a4a5d1d | 2017-07-17 10:26:19 +0200 | [diff] [blame] | 2768 | return( ret ); |
| 2769 | } |
Manuel Pégourié-Gonnard | 8b59049 | 2017-08-14 18:04:19 +0200 | [diff] [blame] | 2770 | #else |
| 2771 | (void) ret; |
| 2772 | #endif |
Manuel Pégourié-Gonnard | a4a5d1d | 2017-07-17 10:26:19 +0200 | [diff] [blame] | 2773 | |
| 2774 | signature_is_good = ret == 0; |
| 2775 | if( top && ! signature_is_good ) |
Manuel Pégourié-Gonnard | f82a4d5 | 2017-07-03 19:26:25 +0200 | [diff] [blame] | 2776 | continue; |
Manuel Pégourié-Gonnard | 2f09d59 | 2017-07-03 18:30:43 +0200 | [diff] [blame] | 2777 | |
Manuel Pégourié-Gonnard | 562df40 | 2017-08-08 18:09:14 +0200 | [diff] [blame] | 2778 | /* optional time check */ |
Manuel Pégourié-Gonnard | 3e329b8 | 2017-06-29 12:55:27 +0200 | [diff] [blame] | 2779 | if( mbedtls_x509_time_is_past( &parent->valid_to ) || |
| 2780 | mbedtls_x509_time_is_future( &parent->valid_from ) ) |
| 2781 | { |
Manuel Pégourié-Gonnard | be4ff42 | 2017-07-14 12:04:14 +0200 | [diff] [blame] | 2782 | if( fallback_parent == NULL ) |
| 2783 | { |
| 2784 | fallback_parent = parent; |
Manuel Pégourié-Gonnard | 78d7e8c | 2018-07-02 12:33:14 +0200 | [diff] [blame] | 2785 | fallback_signature_is_good = signature_is_good; |
Manuel Pégourié-Gonnard | be4ff42 | 2017-07-14 12:04:14 +0200 | [diff] [blame] | 2786 | } |
Manuel Pégourié-Gonnard | 3e329b8 | 2017-06-29 12:55:27 +0200 | [diff] [blame] | 2787 | |
| 2788 | continue; |
| 2789 | } |
| 2790 | |
Andy Gross | 1f62714 | 2019-01-30 10:25:53 -0600 | [diff] [blame] | 2791 | *r_parent = parent; |
| 2792 | *r_signature_is_good = signature_is_good; |
| 2793 | |
Manuel Pégourié-Gonnard | 2f1c33d | 2017-06-29 12:27:23 +0200 | [diff] [blame] | 2794 | break; |
| 2795 | } |
| 2796 | |
Andy Gross | 1f62714 | 2019-01-30 10:25:53 -0600 | [diff] [blame] | 2797 | if( parent == NULL ) |
Manuel Pégourié-Gonnard | a4a5d1d | 2017-07-17 10:26:19 +0200 | [diff] [blame] | 2798 | { |
| 2799 | *r_parent = fallback_parent; |
Manuel Pégourié-Gonnard | 78d7e8c | 2018-07-02 12:33:14 +0200 | [diff] [blame] | 2800 | *r_signature_is_good = fallback_signature_is_good; |
Manuel Pégourié-Gonnard | be4ff42 | 2017-07-14 12:04:14 +0200 | [diff] [blame] | 2801 | } |
Manuel Pégourié-Gonnard | 3e329b8 | 2017-06-29 12:55:27 +0200 | [diff] [blame] | 2802 | |
Manuel Pégourié-Gonnard | a4a5d1d | 2017-07-17 10:26:19 +0200 | [diff] [blame] | 2803 | return( 0 ); |
Manuel Pégourié-Gonnard | 2f1c33d | 2017-06-29 12:27:23 +0200 | [diff] [blame] | 2804 | } |
| 2805 | |
| 2806 | /* |
Manuel Pégourié-Gonnard | 6368612 | 2017-07-04 01:01:39 +0200 | [diff] [blame] | 2807 | * Find a parent in trusted CAs or the provided chain, or return NULL. |
| 2808 | * |
| 2809 | * Searches in trusted CAs first, and return the first suitable parent found |
| 2810 | * (see find_parent_in() for definition of suitable). |
Manuel Pégourié-Gonnard | 98a6778 | 2017-08-17 10:52:20 +0200 | [diff] [blame] | 2811 | * |
| 2812 | * Arguments: |
Manuel Pégourié-Gonnard | e57d743 | 2018-03-07 10:00:57 +0100 | [diff] [blame] | 2813 | * - [in] child: certificate for which we're looking for a parent, followed |
| 2814 | * by a chain of possible intermediates |
Manuel Pégourié-Gonnard | da19f4c | 2018-06-12 12:40:54 +0200 | [diff] [blame] | 2815 | * - [in] trust_ca: list of locally trusted certificates |
| 2816 | * - [out] parent: parent found (or NULL) |
| 2817 | * - [out] parent_is_trusted: 1 if returned `parent` is trusted, or 0 |
| 2818 | * - [out] signature_is_good: 1 if child signature by parent is valid, or 0 |
| 2819 | * - [in] path_cnt: number of links in the chain so far (EE -> ... -> child) |
| 2820 | * - [in] self_cnt: number of self-signed certs in the chain so far |
Manuel Pégourié-Gonnard | e57d743 | 2018-03-07 10:00:57 +0100 | [diff] [blame] | 2821 | * (will always be no greater than path_cnt) |
Manuel Pégourié-Gonnard | da19f4c | 2018-06-12 12:40:54 +0200 | [diff] [blame] | 2822 | * - [in-out] rs_ctx: context for restarting operations |
Manuel Pégourié-Gonnard | e57d743 | 2018-03-07 10:00:57 +0100 | [diff] [blame] | 2823 | * |
| 2824 | * Return value: |
Manuel Pégourié-Gonnard | da19f4c | 2018-06-12 12:40:54 +0200 | [diff] [blame] | 2825 | * - 0 on success |
| 2826 | * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise |
Manuel Pégourié-Gonnard | 6368612 | 2017-07-04 01:01:39 +0200 | [diff] [blame] | 2827 | */ |
Manuel Pégourié-Gonnard | a4a5d1d | 2017-07-17 10:26:19 +0200 | [diff] [blame] | 2828 | static int x509_crt_find_parent( |
| 2829 | mbedtls_x509_crt *child, |
| 2830 | mbedtls_x509_crt *trust_ca, |
| 2831 | mbedtls_x509_crt **parent, |
| 2832 | int *parent_is_trusted, |
| 2833 | int *signature_is_good, |
Manuel Pégourié-Gonnard | bb216bd | 2017-08-28 13:25:55 +0200 | [diff] [blame] | 2834 | unsigned path_cnt, |
| 2835 | unsigned self_cnt, |
Manuel Pégourié-Gonnard | a4a5d1d | 2017-07-17 10:26:19 +0200 | [diff] [blame] | 2836 | mbedtls_x509_crt_restart_ctx *rs_ctx ) |
Manuel Pégourié-Gonnard | 6368612 | 2017-07-04 01:01:39 +0200 | [diff] [blame] | 2837 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2838 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 18547b5 | 2017-08-14 16:11:43 +0200 | [diff] [blame] | 2839 | mbedtls_x509_crt *search_list; |
Manuel Pégourié-Gonnard | 6368612 | 2017-07-04 01:01:39 +0200 | [diff] [blame] | 2840 | |
Manuel Pégourié-Gonnard | 6368612 | 2017-07-04 01:01:39 +0200 | [diff] [blame] | 2841 | *parent_is_trusted = 1; |
Manuel Pégourié-Gonnard | 18547b5 | 2017-08-14 16:11:43 +0200 | [diff] [blame] | 2842 | |
Manuel Pégourié-Gonnard | 8b59049 | 2017-08-14 18:04:19 +0200 | [diff] [blame] | 2843 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
Manuel Pégourié-Gonnard | 3627a8b | 2017-08-23 11:20:48 +0200 | [diff] [blame] | 2844 | /* restore then clear saved state if we have some stored */ |
| 2845 | if( rs_ctx != NULL && rs_ctx->parent_is_trusted != -1 ) |
| 2846 | { |
Manuel Pégourié-Gonnard | 8b59049 | 2017-08-14 18:04:19 +0200 | [diff] [blame] | 2847 | *parent_is_trusted = rs_ctx->parent_is_trusted; |
Manuel Pégourié-Gonnard | 3627a8b | 2017-08-23 11:20:48 +0200 | [diff] [blame] | 2848 | rs_ctx->parent_is_trusted = -1; |
| 2849 | } |
Manuel Pégourié-Gonnard | 8b59049 | 2017-08-14 18:04:19 +0200 | [diff] [blame] | 2850 | #endif |
| 2851 | |
Manuel Pégourié-Gonnard | 18547b5 | 2017-08-14 16:11:43 +0200 | [diff] [blame] | 2852 | while( 1 ) { |
| 2853 | search_list = *parent_is_trusted ? trust_ca : child->next; |
| 2854 | |
| 2855 | ret = x509_crt_find_parent_in( child, search_list, |
| 2856 | parent, signature_is_good, |
| 2857 | *parent_is_trusted, |
| 2858 | path_cnt, self_cnt, rs_ctx ); |
Manuel Pégourié-Gonnard | 6368612 | 2017-07-04 01:01:39 +0200 | [diff] [blame] | 2859 | |
Manuel Pégourié-Gonnard | a4a5d1d | 2017-07-17 10:26:19 +0200 | [diff] [blame] | 2860 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
Manuel Pégourié-Gonnard | 8b59049 | 2017-08-14 18:04:19 +0200 | [diff] [blame] | 2861 | if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS ) |
| 2862 | { |
| 2863 | /* save state */ |
| 2864 | rs_ctx->parent_is_trusted = *parent_is_trusted; |
Manuel Pégourié-Gonnard | 18547b5 | 2017-08-14 16:11:43 +0200 | [diff] [blame] | 2865 | return( ret ); |
| 2866 | } |
Manuel Pégourié-Gonnard | 8b59049 | 2017-08-14 18:04:19 +0200 | [diff] [blame] | 2867 | #else |
| 2868 | (void) ret; |
| 2869 | #endif |
Manuel Pégourié-Gonnard | a4a5d1d | 2017-07-17 10:26:19 +0200 | [diff] [blame] | 2870 | |
Manuel Pégourié-Gonnard | 18547b5 | 2017-08-14 16:11:43 +0200 | [diff] [blame] | 2871 | /* stop here if found or already in second iteration */ |
| 2872 | if( *parent != NULL || *parent_is_trusted == 0 ) |
| 2873 | break; |
Manuel Pégourié-Gonnard | 6368612 | 2017-07-04 01:01:39 +0200 | [diff] [blame] | 2874 | |
Manuel Pégourié-Gonnard | 18547b5 | 2017-08-14 16:11:43 +0200 | [diff] [blame] | 2875 | /* prepare second iteration */ |
| 2876 | *parent_is_trusted = 0; |
Manuel Pégourié-Gonnard | a4a5d1d | 2017-07-17 10:26:19 +0200 | [diff] [blame] | 2877 | } |
Manuel Pégourié-Gonnard | 18547b5 | 2017-08-14 16:11:43 +0200 | [diff] [blame] | 2878 | |
| 2879 | /* extra precaution against mistakes in the caller */ |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 2880 | if( *parent == NULL ) |
Manuel Pégourié-Gonnard | 18547b5 | 2017-08-14 16:11:43 +0200 | [diff] [blame] | 2881 | { |
Manuel Pégourié-Gonnard | a5a3e40 | 2018-10-16 11:27:23 +0200 | [diff] [blame] | 2882 | *parent_is_trusted = 0; |
| 2883 | *signature_is_good = 0; |
Manuel Pégourié-Gonnard | 18547b5 | 2017-08-14 16:11:43 +0200 | [diff] [blame] | 2884 | } |
Manuel Pégourié-Gonnard | a4a5d1d | 2017-07-17 10:26:19 +0200 | [diff] [blame] | 2885 | |
| 2886 | return( 0 ); |
Manuel Pégourié-Gonnard | 6368612 | 2017-07-04 01:01:39 +0200 | [diff] [blame] | 2887 | } |
| 2888 | |
| 2889 | /* |
Manuel Pégourié-Gonnard | 27e9479 | 2017-07-04 00:49:31 +0200 | [diff] [blame] | 2890 | * Check if an end-entity certificate is locally trusted |
| 2891 | * |
| 2892 | * Currently we require such certificates to be self-signed (actually only |
| 2893 | * check for self-issued as self-signatures are not checked) |
| 2894 | */ |
| 2895 | static int x509_crt_check_ee_locally_trusted( |
| 2896 | mbedtls_x509_crt *crt, |
| 2897 | mbedtls_x509_crt *trust_ca ) |
| 2898 | { |
| 2899 | mbedtls_x509_crt *cur; |
| 2900 | |
| 2901 | /* must be self-issued */ |
| 2902 | if( x509_name_cmp( &crt->issuer, &crt->subject ) != 0 ) |
| 2903 | return( -1 ); |
| 2904 | |
| 2905 | /* look for an exact match with trusted cert */ |
| 2906 | for( cur = trust_ca; cur != NULL; cur = cur->next ) |
| 2907 | { |
| 2908 | if( crt->raw.len == cur->raw.len && |
| 2909 | memcmp( crt->raw.p, cur->raw.p, crt->raw.len ) == 0 ) |
| 2910 | { |
| 2911 | return( 0 ); |
| 2912 | } |
| 2913 | } |
| 2914 | |
| 2915 | /* too bad */ |
| 2916 | return( -1 ); |
| 2917 | } |
| 2918 | |
| 2919 | /* |
Manuel Pégourié-Gonnard | f86f491 | 2017-07-05 16:43:44 +0200 | [diff] [blame] | 2920 | * Build and verify a certificate chain |
Manuel Pégourié-Gonnard | 35407c7 | 2017-06-29 10:45:25 +0200 | [diff] [blame] | 2921 | * |
Manuel Pégourié-Gonnard | f86f491 | 2017-07-05 16:43:44 +0200 | [diff] [blame] | 2922 | * Given a peer-provided list of certificates EE, C1, ..., Cn and |
| 2923 | * a list of trusted certs R1, ... Rp, try to build and verify a chain |
Manuel Pégourié-Gonnard | 562df40 | 2017-08-08 18:09:14 +0200 | [diff] [blame] | 2924 | * EE, Ci1, ... Ciq [, Rj] |
Manuel Pégourié-Gonnard | f86f491 | 2017-07-05 16:43:44 +0200 | [diff] [blame] | 2925 | * such that every cert in the chain is a child of the next one, |
| 2926 | * jumping to a trusted root as early as possible. |
Manuel Pégourié-Gonnard | bdc5440 | 2017-07-04 00:33:39 +0200 | [diff] [blame] | 2927 | * |
Manuel Pégourié-Gonnard | f86f491 | 2017-07-05 16:43:44 +0200 | [diff] [blame] | 2928 | * Verify that chain and return it with flags for all issues found. |
| 2929 | * |
| 2930 | * Special cases: |
| 2931 | * - EE == Rj -> return a one-element list containing it |
| 2932 | * - EE, Ci1, ..., Ciq cannot be continued with a trusted root |
| 2933 | * -> return that chain with NOT_TRUSTED set on Ciq |
Manuel Pégourié-Gonnard | bdc5440 | 2017-07-04 00:33:39 +0200 | [diff] [blame] | 2934 | * |
Manuel Pégourié-Gonnard | d19a41d | 2017-07-14 11:05:59 +0200 | [diff] [blame] | 2935 | * Tests for (aspects of) this function should include at least: |
| 2936 | * - trusted EE |
| 2937 | * - EE -> trusted root |
Antonin Décimo | 36e89b5 | 2019-01-23 15:24:37 +0100 | [diff] [blame] | 2938 | * - EE -> intermediate CA -> trusted root |
Manuel Pégourié-Gonnard | d19a41d | 2017-07-14 11:05:59 +0200 | [diff] [blame] | 2939 | * - if relevant: EE untrusted |
| 2940 | * - if relevant: EE -> intermediate, untrusted |
| 2941 | * with the aspect under test checked at each relevant level (EE, int, root). |
| 2942 | * For some aspects longer chains are required, but usually length 2 is |
| 2943 | * enough (but length 1 is not in general). |
| 2944 | * |
Manuel Pégourié-Gonnard | bdc5440 | 2017-07-04 00:33:39 +0200 | [diff] [blame] | 2945 | * Arguments: |
Manuel Pégourié-Gonnard | ce6e52f | 2017-07-05 17:05:03 +0200 | [diff] [blame] | 2946 | * - [in] crt: the cert list EE, C1, ..., Cn |
| 2947 | * - [in] trust_ca: the trusted list R1, ..., Rp |
| 2948 | * - [in] ca_crl, profile: as in verify_with_profile() |
Manuel Pégourié-Gonnard | c11e4ba | 2017-08-14 17:17:14 +0200 | [diff] [blame] | 2949 | * - [out] ver_chain: the built and verified chain |
Manuel Pégourié-Gonnard | a968843 | 2017-08-23 11:23:59 +0200 | [diff] [blame] | 2950 | * Only valid when return value is 0, may contain garbage otherwise! |
| 2951 | * Restart note: need not be the same when calling again to resume. |
Manuel Pégourié-Gonnard | 98a6778 | 2017-08-17 10:52:20 +0200 | [diff] [blame] | 2952 | * - [in-out] rs_ctx: context for restarting operations |
Manuel Pégourié-Gonnard | f86f491 | 2017-07-05 16:43:44 +0200 | [diff] [blame] | 2953 | * |
| 2954 | * Return value: |
| 2955 | * - non-zero if the chain could not be fully built and examined |
| 2956 | * - 0 is the chain was successfully built and examined, |
| 2957 | * even if it was found to be invalid |
Manuel Pégourié-Gonnard | 35407c7 | 2017-06-29 10:45:25 +0200 | [diff] [blame] | 2958 | */ |
Manuel Pégourié-Gonnard | bdc5440 | 2017-07-04 00:33:39 +0200 | [diff] [blame] | 2959 | static int x509_crt_verify_chain( |
Manuel Pégourié-Gonnard | ce6e52f | 2017-07-05 17:05:03 +0200 | [diff] [blame] | 2960 | mbedtls_x509_crt *crt, |
| 2961 | mbedtls_x509_crt *trust_ca, |
| 2962 | mbedtls_x509_crl *ca_crl, |
Hanno Becker | 3116fb3 | 2019-03-28 13:34:42 +0000 | [diff] [blame] | 2963 | mbedtls_x509_crt_ca_cb_t f_ca_cb, |
| 2964 | void *p_ca_cb, |
Manuel Pégourié-Gonnard | 9505164 | 2015-06-15 10:39:46 +0200 | [diff] [blame] | 2965 | const mbedtls_x509_crt_profile *profile, |
Manuel Pégourié-Gonnard | c11e4ba | 2017-08-14 17:17:14 +0200 | [diff] [blame] | 2966 | mbedtls_x509_crt_verify_chain *ver_chain, |
Manuel Pégourié-Gonnard | a4a5d1d | 2017-07-17 10:26:19 +0200 | [diff] [blame] | 2967 | mbedtls_x509_crt_restart_ctx *rs_ctx ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 2968 | { |
Manuel Pégourié-Gonnard | a968843 | 2017-08-23 11:23:59 +0200 | [diff] [blame] | 2969 | /* Don't initialize any of those variables here, so that the compiler can |
| 2970 | * catch potential issues with jumping ahead when restarting */ |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2971 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | f86f491 | 2017-07-05 16:43:44 +0200 | [diff] [blame] | 2972 | uint32_t *flags; |
Manuel Pégourié-Gonnard | c11e4ba | 2017-08-14 17:17:14 +0200 | [diff] [blame] | 2973 | mbedtls_x509_crt_verify_chain_item *cur; |
Manuel Pégourié-Gonnard | ce6e52f | 2017-07-05 17:05:03 +0200 | [diff] [blame] | 2974 | mbedtls_x509_crt *child; |
Manuel Pégourié-Gonnard | 58dcd2d | 2017-07-03 21:35:04 +0200 | [diff] [blame] | 2975 | mbedtls_x509_crt *parent; |
Manuel Pégourié-Gonnard | 8b59049 | 2017-08-14 18:04:19 +0200 | [diff] [blame] | 2976 | int parent_is_trusted; |
| 2977 | int child_is_trusted; |
| 2978 | int signature_is_good; |
Manuel Pégourié-Gonnard | bb216bd | 2017-08-28 13:25:55 +0200 | [diff] [blame] | 2979 | unsigned self_cnt; |
Hanno Becker | f53893b | 2019-03-28 13:45:55 +0000 | [diff] [blame] | 2980 | mbedtls_x509_crt *cur_trust_ca = NULL; |
Manuel Pégourié-Gonnard | 8b59049 | 2017-08-14 18:04:19 +0200 | [diff] [blame] | 2981 | |
| 2982 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
| 2983 | /* resume if we had an operation in progress */ |
Manuel Pégourié-Gonnard | daf0491 | 2017-08-23 12:32:19 +0200 | [diff] [blame] | 2984 | if( rs_ctx != NULL && rs_ctx->in_progress == x509_crt_rs_find_parent ) |
Manuel Pégourié-Gonnard | 8b59049 | 2017-08-14 18:04:19 +0200 | [diff] [blame] | 2985 | { |
Manuel Pégourié-Gonnard | 3627a8b | 2017-08-23 11:20:48 +0200 | [diff] [blame] | 2986 | /* restore saved state */ |
Manuel Pégourié-Gonnard | a968843 | 2017-08-23 11:23:59 +0200 | [diff] [blame] | 2987 | *ver_chain = rs_ctx->ver_chain; /* struct copy */ |
Manuel Pégourié-Gonnard | daf0491 | 2017-08-23 12:32:19 +0200 | [diff] [blame] | 2988 | self_cnt = rs_ctx->self_cnt; |
Manuel Pégourié-Gonnard | 8b59049 | 2017-08-14 18:04:19 +0200 | [diff] [blame] | 2989 | |
Manuel Pégourié-Gonnard | daf0491 | 2017-08-23 12:32:19 +0200 | [diff] [blame] | 2990 | /* restore derived state */ |
Manuel Pégourié-Gonnard | 8b59049 | 2017-08-14 18:04:19 +0200 | [diff] [blame] | 2991 | cur = &ver_chain->items[ver_chain->len - 1]; |
Manuel Pégourié-Gonnard | daf0491 | 2017-08-23 12:32:19 +0200 | [diff] [blame] | 2992 | child = cur->crt; |
Manuel Pégourié-Gonnard | 8b59049 | 2017-08-14 18:04:19 +0200 | [diff] [blame] | 2993 | flags = &cur->flags; |
| 2994 | |
| 2995 | goto find_parent; |
| 2996 | } |
| 2997 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */ |
Manuel Pégourié-Gonnard | 8f8c282 | 2017-07-03 21:25:10 +0200 | [diff] [blame] | 2998 | |
Manuel Pégourié-Gonnard | ce6e52f | 2017-07-05 17:05:03 +0200 | [diff] [blame] | 2999 | child = crt; |
Manuel Pégourié-Gonnard | 8b59049 | 2017-08-14 18:04:19 +0200 | [diff] [blame] | 3000 | self_cnt = 0; |
| 3001 | parent_is_trusted = 0; |
| 3002 | child_is_trusted = 0; |
Manuel Pégourié-Gonnard | f86f491 | 2017-07-05 16:43:44 +0200 | [diff] [blame] | 3003 | |
Manuel Pégourié-Gonnard | ce6e52f | 2017-07-05 17:05:03 +0200 | [diff] [blame] | 3004 | while( 1 ) { |
| 3005 | /* Add certificate to the verification chain */ |
Manuel Pégourié-Gonnard | c11e4ba | 2017-08-14 17:17:14 +0200 | [diff] [blame] | 3006 | cur = &ver_chain->items[ver_chain->len]; |
| 3007 | cur->crt = child; |
Manuel Pégourié-Gonnard | 83e923b | 2017-08-23 10:55:41 +0200 | [diff] [blame] | 3008 | cur->flags = 0; |
Manuel Pégourié-Gonnard | c11e4ba | 2017-08-14 17:17:14 +0200 | [diff] [blame] | 3009 | ver_chain->len++; |
Manuel Pégourié-Gonnard | 83e923b | 2017-08-23 10:55:41 +0200 | [diff] [blame] | 3010 | flags = &cur->flags; |
Manuel Pégourié-Gonnard | 66fac75 | 2017-07-03 21:39:21 +0200 | [diff] [blame] | 3011 | |
Manuel Pégourié-Gonnard | ce6e52f | 2017-07-05 17:05:03 +0200 | [diff] [blame] | 3012 | /* Check time-validity (all certificates) */ |
| 3013 | if( mbedtls_x509_time_is_past( &child->valid_to ) ) |
| 3014 | *flags |= MBEDTLS_X509_BADCERT_EXPIRED; |
Manuel Pégourié-Gonnard | 66fac75 | 2017-07-03 21:39:21 +0200 | [diff] [blame] | 3015 | |
Manuel Pégourié-Gonnard | ce6e52f | 2017-07-05 17:05:03 +0200 | [diff] [blame] | 3016 | if( mbedtls_x509_time_is_future( &child->valid_from ) ) |
| 3017 | *flags |= MBEDTLS_X509_BADCERT_FUTURE; |
Manuel Pégourié-Gonnard | cb39610 | 2017-07-04 00:00:24 +0200 | [diff] [blame] | 3018 | |
Manuel Pégourié-Gonnard | ce6e52f | 2017-07-05 17:05:03 +0200 | [diff] [blame] | 3019 | /* Stop here for trusted roots (but not for trusted EE certs) */ |
| 3020 | if( child_is_trusted ) |
| 3021 | return( 0 ); |
Manuel Pégourié-Gonnard | 66fac75 | 2017-07-03 21:39:21 +0200 | [diff] [blame] | 3022 | |
Manuel Pégourié-Gonnard | ce6e52f | 2017-07-05 17:05:03 +0200 | [diff] [blame] | 3023 | /* Check signature algorithm: MD & PK algs */ |
| 3024 | if( x509_profile_check_md_alg( profile, child->sig_md ) != 0 ) |
| 3025 | *flags |= MBEDTLS_X509_BADCERT_BAD_MD; |
Manuel Pégourié-Gonnard | 66fac75 | 2017-07-03 21:39:21 +0200 | [diff] [blame] | 3026 | |
Manuel Pégourié-Gonnard | ce6e52f | 2017-07-05 17:05:03 +0200 | [diff] [blame] | 3027 | if( x509_profile_check_pk_alg( profile, child->sig_pk ) != 0 ) |
| 3028 | *flags |= MBEDTLS_X509_BADCERT_BAD_PK; |
Manuel Pégourié-Gonnard | 27e9479 | 2017-07-04 00:49:31 +0200 | [diff] [blame] | 3029 | |
Manuel Pégourié-Gonnard | ce6e52f | 2017-07-05 17:05:03 +0200 | [diff] [blame] | 3030 | /* Special case: EE certs that are locally trusted */ |
Manuel Pégourié-Gonnard | c11e4ba | 2017-08-14 17:17:14 +0200 | [diff] [blame] | 3031 | if( ver_chain->len == 1 && |
Manuel Pégourié-Gonnard | ce6e52f | 2017-07-05 17:05:03 +0200 | [diff] [blame] | 3032 | x509_crt_check_ee_locally_trusted( child, trust_ca ) == 0 ) |
| 3033 | { |
| 3034 | return( 0 ); |
| 3035 | } |
Manuel Pégourié-Gonnard | 8f8c282 | 2017-07-03 21:25:10 +0200 | [diff] [blame] | 3036 | |
Manuel Pégourié-Gonnard | 8b59049 | 2017-08-14 18:04:19 +0200 | [diff] [blame] | 3037 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
| 3038 | find_parent: |
| 3039 | #endif |
Hanno Becker | f53893b | 2019-03-28 13:45:55 +0000 | [diff] [blame] | 3040 | |
| 3041 | /* Obtain list of potential trusted signers from CA callback, |
| 3042 | * or use statically provided list. */ |
| 3043 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 3044 | if( f_ca_cb != NULL ) |
| 3045 | { |
| 3046 | mbedtls_x509_crt_free( ver_chain->trust_ca_cb_result ); |
| 3047 | mbedtls_free( ver_chain->trust_ca_cb_result ); |
| 3048 | ver_chain->trust_ca_cb_result = NULL; |
| 3049 | |
| 3050 | ret = f_ca_cb( p_ca_cb, child, &ver_chain->trust_ca_cb_result ); |
| 3051 | if( ret != 0 ) |
| 3052 | return( MBEDTLS_ERR_X509_FATAL_ERROR ); |
| 3053 | |
| 3054 | cur_trust_ca = ver_chain->trust_ca_cb_result; |
| 3055 | } |
| 3056 | else |
| 3057 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
| 3058 | { |
| 3059 | ((void) f_ca_cb); |
| 3060 | ((void) p_ca_cb); |
| 3061 | cur_trust_ca = trust_ca; |
| 3062 | } |
| 3063 | |
Manuel Pégourié-Gonnard | ce6e52f | 2017-07-05 17:05:03 +0200 | [diff] [blame] | 3064 | /* Look for a parent in trusted CAs or up the chain */ |
Hanno Becker | f53893b | 2019-03-28 13:45:55 +0000 | [diff] [blame] | 3065 | ret = x509_crt_find_parent( child, cur_trust_ca, &parent, |
Manuel Pégourié-Gonnard | be4ff42 | 2017-07-14 12:04:14 +0200 | [diff] [blame] | 3066 | &parent_is_trusted, &signature_is_good, |
Manuel Pégourié-Gonnard | c11e4ba | 2017-08-14 17:17:14 +0200 | [diff] [blame] | 3067 | ver_chain->len - 1, self_cnt, rs_ctx ); |
Manuel Pégourié-Gonnard | a4a5d1d | 2017-07-17 10:26:19 +0200 | [diff] [blame] | 3068 | |
| 3069 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
Manuel Pégourié-Gonnard | 8b59049 | 2017-08-14 18:04:19 +0200 | [diff] [blame] | 3070 | if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS ) |
| 3071 | { |
| 3072 | /* save state */ |
Manuel Pégourié-Gonnard | daf0491 | 2017-08-23 12:32:19 +0200 | [diff] [blame] | 3073 | rs_ctx->in_progress = x509_crt_rs_find_parent; |
Manuel Pégourié-Gonnard | 8b59049 | 2017-08-14 18:04:19 +0200 | [diff] [blame] | 3074 | rs_ctx->self_cnt = self_cnt; |
Manuel Pégourié-Gonnard | a968843 | 2017-08-23 11:23:59 +0200 | [diff] [blame] | 3075 | rs_ctx->ver_chain = *ver_chain; /* struct copy */ |
Manuel Pégourié-Gonnard | 8b59049 | 2017-08-14 18:04:19 +0200 | [diff] [blame] | 3076 | |
Manuel Pégourié-Gonnard | a4a5d1d | 2017-07-17 10:26:19 +0200 | [diff] [blame] | 3077 | return( ret ); |
Manuel Pégourié-Gonnard | a4a5d1d | 2017-07-17 10:26:19 +0200 | [diff] [blame] | 3078 | } |
Manuel Pégourié-Gonnard | 8b59049 | 2017-08-14 18:04:19 +0200 | [diff] [blame] | 3079 | #else |
| 3080 | (void) ret; |
| 3081 | #endif |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 3082 | |
Manuel Pégourié-Gonnard | ce6e52f | 2017-07-05 17:05:03 +0200 | [diff] [blame] | 3083 | /* No parent? We're done here */ |
| 3084 | if( parent == NULL ) |
| 3085 | { |
| 3086 | *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED; |
| 3087 | return( 0 ); |
| 3088 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 3089 | |
Manuel Pégourié-Gonnard | ce6e52f | 2017-07-05 17:05:03 +0200 | [diff] [blame] | 3090 | /* Count intermediate self-issued (not necessarily self-signed) certs. |
| 3091 | * These can occur with some strategies for key rollover, see [SIRO], |
| 3092 | * and should be excluded from max_pathlen checks. */ |
Manuel Pégourié-Gonnard | c11e4ba | 2017-08-14 17:17:14 +0200 | [diff] [blame] | 3093 | if( ver_chain->len != 1 && |
Manuel Pégourié-Gonnard | 24611f9 | 2017-08-09 10:28:07 +0200 | [diff] [blame] | 3094 | x509_name_cmp( &child->issuer, &child->subject ) == 0 ) |
| 3095 | { |
Manuel Pégourié-Gonnard | ce6e52f | 2017-07-05 17:05:03 +0200 | [diff] [blame] | 3096 | self_cnt++; |
Manuel Pégourié-Gonnard | 24611f9 | 2017-08-09 10:28:07 +0200 | [diff] [blame] | 3097 | } |
Manuel Pégourié-Gonnard | fd6c85c | 2014-11-20 16:34:20 +0100 | [diff] [blame] | 3098 | |
Manuel Pégourié-Gonnard | ce6e52f | 2017-07-05 17:05:03 +0200 | [diff] [blame] | 3099 | /* path_cnt is 0 for the first intermediate CA, |
| 3100 | * and if parent is trusted it's not an intermediate CA */ |
| 3101 | if( ! parent_is_trusted && |
Manuel Pégourié-Gonnard | c11e4ba | 2017-08-14 17:17:14 +0200 | [diff] [blame] | 3102 | ver_chain->len > MBEDTLS_X509_MAX_INTERMEDIATE_CA ) |
Manuel Pégourié-Gonnard | ce6e52f | 2017-07-05 17:05:03 +0200 | [diff] [blame] | 3103 | { |
| 3104 | /* return immediately to avoid overflow the chain array */ |
| 3105 | return( MBEDTLS_ERR_X509_FATAL_ERROR ); |
| 3106 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 3107 | |
Manuel Pégourié-Gonnard | 98a6778 | 2017-08-17 10:52:20 +0200 | [diff] [blame] | 3108 | /* signature was checked while searching parent */ |
Manuel Pégourié-Gonnard | be4ff42 | 2017-07-14 12:04:14 +0200 | [diff] [blame] | 3109 | if( ! signature_is_good ) |
Manuel Pégourié-Gonnard | ce6e52f | 2017-07-05 17:05:03 +0200 | [diff] [blame] | 3110 | *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED; |
| 3111 | |
| 3112 | /* check size of signing key */ |
Manuel Pégourié-Gonnard | 3f81691 | 2017-10-26 10:24:16 +0200 | [diff] [blame] | 3113 | if( x509_profile_check_key( profile, &parent->pk ) != 0 ) |
Manuel Pégourié-Gonnard | ce6e52f | 2017-07-05 17:05:03 +0200 | [diff] [blame] | 3114 | *flags |= MBEDTLS_X509_BADCERT_BAD_KEY; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 3115 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3116 | #if defined(MBEDTLS_X509_CRL_PARSE_C) |
Manuel Pégourié-Gonnard | ce6e52f | 2017-07-05 17:05:03 +0200 | [diff] [blame] | 3117 | /* Check trusted CA's CRL for the given crt */ |
Manuel Pégourié-Gonnard | 562df40 | 2017-08-08 18:09:14 +0200 | [diff] [blame] | 3118 | *flags |= x509_crt_verifycrl( child, parent, ca_crl, profile ); |
Manuel Pégourié-Gonnard | ce6e52f | 2017-07-05 17:05:03 +0200 | [diff] [blame] | 3119 | #else |
| 3120 | (void) ca_crl; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 3121 | #endif |
| 3122 | |
Manuel Pégourié-Gonnard | ce6e52f | 2017-07-05 17:05:03 +0200 | [diff] [blame] | 3123 | /* prepare for next iteration */ |
| 3124 | child = parent; |
| 3125 | parent = NULL; |
| 3126 | child_is_trusted = parent_is_trusted; |
Manuel Pégourié-Gonnard | be4ff42 | 2017-07-14 12:04:14 +0200 | [diff] [blame] | 3127 | signature_is_good = 0; |
Manuel Pégourié-Gonnard | ce6e52f | 2017-07-05 17:05:03 +0200 | [diff] [blame] | 3128 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 3129 | } |
| 3130 | |
| 3131 | /* |
Manuel Pégourié-Gonnard | a468eb1 | 2017-07-04 01:31:59 +0200 | [diff] [blame] | 3132 | * Check for CN match |
| 3133 | */ |
| 3134 | static int x509_crt_check_cn( const mbedtls_x509_buf *name, |
| 3135 | const char *cn, size_t cn_len ) |
| 3136 | { |
| 3137 | /* try exact match */ |
| 3138 | if( name->len == cn_len && |
| 3139 | x509_memcasecmp( cn, name->p, cn_len ) == 0 ) |
| 3140 | { |
| 3141 | return( 0 ); |
| 3142 | } |
| 3143 | |
| 3144 | /* try wildcard match */ |
Manuel Pégourié-Gonnard | 900fba6 | 2017-10-18 14:28:11 +0200 | [diff] [blame] | 3145 | if( x509_check_wildcard( cn, name ) == 0 ) |
Manuel Pégourié-Gonnard | a468eb1 | 2017-07-04 01:31:59 +0200 | [diff] [blame] | 3146 | { |
| 3147 | return( 0 ); |
| 3148 | } |
| 3149 | |
| 3150 | return( -1 ); |
| 3151 | } |
| 3152 | |
| 3153 | /* |
Manuel Pégourié-Gonnard | f3e4bd8 | 2020-07-21 13:22:41 +0200 | [diff] [blame] | 3154 | * Check for SAN match, see RFC 5280 Section 4.2.1.6 |
| 3155 | */ |
| 3156 | static int x509_crt_check_san( const mbedtls_x509_buf *name, |
| 3157 | const char *cn, size_t cn_len ) |
| 3158 | { |
| 3159 | const unsigned char san_type = (unsigned char) name->tag & |
| 3160 | MBEDTLS_ASN1_TAG_VALUE_MASK; |
| 3161 | |
| 3162 | /* dNSName */ |
| 3163 | if( san_type == MBEDTLS_X509_SAN_DNS_NAME ) |
| 3164 | return( x509_crt_check_cn( name, cn, cn_len ) ); |
| 3165 | |
| 3166 | /* (We may handle other types here later.) */ |
| 3167 | |
| 3168 | /* Unrecognized type */ |
| 3169 | return( -1 ); |
| 3170 | } |
| 3171 | |
| 3172 | /* |
Manuel Pégourié-Gonnard | 1300e99 | 2017-07-04 01:13:44 +0200 | [diff] [blame] | 3173 | * Verify the requested CN - only call this if cn is not NULL! |
| 3174 | */ |
Manuel Pégourié-Gonnard | a468eb1 | 2017-07-04 01:31:59 +0200 | [diff] [blame] | 3175 | static void x509_crt_verify_name( const mbedtls_x509_crt *crt, |
Manuel Pégourié-Gonnard | 1300e99 | 2017-07-04 01:13:44 +0200 | [diff] [blame] | 3176 | const char *cn, |
| 3177 | uint32_t *flags ) |
| 3178 | { |
Manuel Pégourié-Gonnard | a468eb1 | 2017-07-04 01:31:59 +0200 | [diff] [blame] | 3179 | const mbedtls_x509_name *name; |
| 3180 | const mbedtls_x509_sequence *cur; |
| 3181 | size_t cn_len = strlen( cn ); |
Manuel Pégourié-Gonnard | 1300e99 | 2017-07-04 01:13:44 +0200 | [diff] [blame] | 3182 | |
| 3183 | if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME ) |
| 3184 | { |
Manuel Pégourié-Gonnard | a468eb1 | 2017-07-04 01:31:59 +0200 | [diff] [blame] | 3185 | for( cur = &crt->subject_alt_names; cur != NULL; cur = cur->next ) |
Manuel Pégourié-Gonnard | 1300e99 | 2017-07-04 01:13:44 +0200 | [diff] [blame] | 3186 | { |
Manuel Pégourié-Gonnard | f3e4bd8 | 2020-07-21 13:22:41 +0200 | [diff] [blame] | 3187 | if( x509_crt_check_san( &cur->buf, cn, cn_len ) == 0 ) |
Manuel Pégourié-Gonnard | 1300e99 | 2017-07-04 01:13:44 +0200 | [diff] [blame] | 3188 | break; |
Manuel Pégourié-Gonnard | 1300e99 | 2017-07-04 01:13:44 +0200 | [diff] [blame] | 3189 | } |
| 3190 | |
| 3191 | if( cur == NULL ) |
| 3192 | *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH; |
| 3193 | } |
| 3194 | else |
| 3195 | { |
Manuel Pégourié-Gonnard | 08eacec | 2017-10-18 14:20:24 +0200 | [diff] [blame] | 3196 | for( name = &crt->subject; name != NULL; name = name->next ) |
Manuel Pégourié-Gonnard | 1300e99 | 2017-07-04 01:13:44 +0200 | [diff] [blame] | 3197 | { |
Manuel Pégourié-Gonnard | a468eb1 | 2017-07-04 01:31:59 +0200 | [diff] [blame] | 3198 | if( MBEDTLS_OID_CMP( MBEDTLS_OID_AT_CN, &name->oid ) == 0 && |
| 3199 | x509_crt_check_cn( &name->val, cn, cn_len ) == 0 ) |
Manuel Pégourié-Gonnard | 1300e99 | 2017-07-04 01:13:44 +0200 | [diff] [blame] | 3200 | { |
Manuel Pégourié-Gonnard | a468eb1 | 2017-07-04 01:31:59 +0200 | [diff] [blame] | 3201 | break; |
Manuel Pégourié-Gonnard | 1300e99 | 2017-07-04 01:13:44 +0200 | [diff] [blame] | 3202 | } |
Manuel Pégourié-Gonnard | 1300e99 | 2017-07-04 01:13:44 +0200 | [diff] [blame] | 3203 | } |
| 3204 | |
| 3205 | if( name == NULL ) |
| 3206 | *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH; |
| 3207 | } |
| 3208 | } |
| 3209 | |
| 3210 | /* |
Manuel Pégourié-Gonnard | a707e1d | 2017-07-05 17:18:42 +0200 | [diff] [blame] | 3211 | * Merge the flags for all certs in the chain, after calling callback |
| 3212 | */ |
| 3213 | static int x509_crt_merge_flags_with_cb( |
| 3214 | uint32_t *flags, |
Manuel Pégourié-Gonnard | c11e4ba | 2017-08-14 17:17:14 +0200 | [diff] [blame] | 3215 | const mbedtls_x509_crt_verify_chain *ver_chain, |
Manuel Pégourié-Gonnard | a707e1d | 2017-07-05 17:18:42 +0200 | [diff] [blame] | 3216 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), |
| 3217 | void *p_vrfy ) |
| 3218 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3219 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | bb216bd | 2017-08-28 13:25:55 +0200 | [diff] [blame] | 3220 | unsigned i; |
Manuel Pégourié-Gonnard | a707e1d | 2017-07-05 17:18:42 +0200 | [diff] [blame] | 3221 | uint32_t cur_flags; |
Manuel Pégourié-Gonnard | c11e4ba | 2017-08-14 17:17:14 +0200 | [diff] [blame] | 3222 | const mbedtls_x509_crt_verify_chain_item *cur; |
Manuel Pégourié-Gonnard | a707e1d | 2017-07-05 17:18:42 +0200 | [diff] [blame] | 3223 | |
Manuel Pégourié-Gonnard | c11e4ba | 2017-08-14 17:17:14 +0200 | [diff] [blame] | 3224 | for( i = ver_chain->len; i != 0; --i ) |
Manuel Pégourié-Gonnard | a707e1d | 2017-07-05 17:18:42 +0200 | [diff] [blame] | 3225 | { |
Manuel Pégourié-Gonnard | c11e4ba | 2017-08-14 17:17:14 +0200 | [diff] [blame] | 3226 | cur = &ver_chain->items[i-1]; |
| 3227 | cur_flags = cur->flags; |
Manuel Pégourié-Gonnard | a707e1d | 2017-07-05 17:18:42 +0200 | [diff] [blame] | 3228 | |
| 3229 | if( NULL != f_vrfy ) |
Manuel Pégourié-Gonnard | bb216bd | 2017-08-28 13:25:55 +0200 | [diff] [blame] | 3230 | if( ( ret = f_vrfy( p_vrfy, cur->crt, (int) i-1, &cur_flags ) ) != 0 ) |
Manuel Pégourié-Gonnard | a707e1d | 2017-07-05 17:18:42 +0200 | [diff] [blame] | 3231 | return( ret ); |
| 3232 | |
| 3233 | *flags |= cur_flags; |
| 3234 | } |
| 3235 | |
| 3236 | return( 0 ); |
| 3237 | } |
| 3238 | |
| 3239 | /* |
Manuel Pégourié-Gonnard | bc3f44a | 2017-07-11 11:02:20 +0200 | [diff] [blame] | 3240 | * Verify the certificate validity, with profile, restartable version |
| 3241 | * |
| 3242 | * This function: |
| 3243 | * - checks the requested CN (if any) |
| 3244 | * - checks the type and size of the EE cert's key, |
| 3245 | * as that isn't done as part of chain building/verification currently |
| 3246 | * - builds and verifies the chain |
| 3247 | * - then calls the callback and merges the flags |
Hanno Becker | 3116fb3 | 2019-03-28 13:34:42 +0000 | [diff] [blame] | 3248 | * |
| 3249 | * The parameters pairs `trust_ca`, `ca_crl` and `f_ca_cb`, `p_ca_cb` |
| 3250 | * are mutually exclusive: If `f_ca_cb != NULL`, it will be used by the |
| 3251 | * verification routine to search for trusted signers, and CRLs will |
| 3252 | * be disabled. Otherwise, `trust_ca` will be used as the static list |
| 3253 | * of trusted signers, and `ca_crl` will be use as the static list |
| 3254 | * of CRLs. |
Manuel Pégourié-Gonnard | bc3f44a | 2017-07-11 11:02:20 +0200 | [diff] [blame] | 3255 | */ |
Jarno Lamsa | 2ee67a6 | 2019-04-01 14:59:33 +0300 | [diff] [blame] | 3256 | static int x509_crt_verify_restartable_ca_cb( mbedtls_x509_crt *crt, |
Manuel Pégourié-Gonnard | bc3f44a | 2017-07-11 11:02:20 +0200 | [diff] [blame] | 3257 | mbedtls_x509_crt *trust_ca, |
| 3258 | mbedtls_x509_crl *ca_crl, |
Hanno Becker | 3116fb3 | 2019-03-28 13:34:42 +0000 | [diff] [blame] | 3259 | mbedtls_x509_crt_ca_cb_t f_ca_cb, |
| 3260 | void *p_ca_cb, |
Manuel Pégourié-Gonnard | bc3f44a | 2017-07-11 11:02:20 +0200 | [diff] [blame] | 3261 | const mbedtls_x509_crt_profile *profile, |
| 3262 | const char *cn, uint32_t *flags, |
| 3263 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), |
| 3264 | void *p_vrfy, |
| 3265 | mbedtls_x509_crt_restart_ctx *rs_ctx ) |
| 3266 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3267 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 65eefc8 | 2015-10-23 14:08:48 +0200 | [diff] [blame] | 3268 | mbedtls_pk_type_t pk_type; |
Manuel Pégourié-Gonnard | c11e4ba | 2017-08-14 17:17:14 +0200 | [diff] [blame] | 3269 | mbedtls_x509_crt_verify_chain ver_chain; |
Manuel Pégourié-Gonnard | 83e923b | 2017-08-23 10:55:41 +0200 | [diff] [blame] | 3270 | uint32_t ee_flags; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 3271 | |
| 3272 | *flags = 0; |
Manuel Pégourié-Gonnard | 83e923b | 2017-08-23 10:55:41 +0200 | [diff] [blame] | 3273 | ee_flags = 0; |
| 3274 | x509_crt_verify_chain_reset( &ver_chain ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 3275 | |
Manuel Pégourié-Gonnard | d15795a | 2017-06-22 12:19:27 +0200 | [diff] [blame] | 3276 | if( profile == NULL ) |
| 3277 | { |
| 3278 | ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA; |
| 3279 | goto exit; |
| 3280 | } |
| 3281 | |
Manuel Pégourié-Gonnard | 1300e99 | 2017-07-04 01:13:44 +0200 | [diff] [blame] | 3282 | /* check name if requested */ |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 3283 | if( cn != NULL ) |
Manuel Pégourié-Gonnard | 83e923b | 2017-08-23 10:55:41 +0200 | [diff] [blame] | 3284 | x509_crt_verify_name( crt, cn, &ee_flags ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 3285 | |
Manuel Pégourié-Gonnard | 65eefc8 | 2015-10-23 14:08:48 +0200 | [diff] [blame] | 3286 | /* Check the type and size of the key */ |
| 3287 | pk_type = mbedtls_pk_get_type( &crt->pk ); |
| 3288 | |
| 3289 | if( x509_profile_check_pk_alg( profile, pk_type ) != 0 ) |
Manuel Pégourié-Gonnard | 83e923b | 2017-08-23 10:55:41 +0200 | [diff] [blame] | 3290 | ee_flags |= MBEDTLS_X509_BADCERT_BAD_PK; |
Manuel Pégourié-Gonnard | 65eefc8 | 2015-10-23 14:08:48 +0200 | [diff] [blame] | 3291 | |
Manuel Pégourié-Gonnard | 3f81691 | 2017-10-26 10:24:16 +0200 | [diff] [blame] | 3292 | if( x509_profile_check_key( profile, &crt->pk ) != 0 ) |
Manuel Pégourié-Gonnard | 83e923b | 2017-08-23 10:55:41 +0200 | [diff] [blame] | 3293 | ee_flags |= MBEDTLS_X509_BADCERT_BAD_KEY; |
Manuel Pégourié-Gonnard | 65eefc8 | 2015-10-23 14:08:48 +0200 | [diff] [blame] | 3294 | |
Manuel Pégourié-Gonnard | bdc5440 | 2017-07-04 00:33:39 +0200 | [diff] [blame] | 3295 | /* Check the chain */ |
Hanno Becker | 3116fb3 | 2019-03-28 13:34:42 +0000 | [diff] [blame] | 3296 | ret = x509_crt_verify_chain( crt, trust_ca, ca_crl, |
| 3297 | f_ca_cb, p_ca_cb, profile, |
Manuel Pégourié-Gonnard | c11e4ba | 2017-08-14 17:17:14 +0200 | [diff] [blame] | 3298 | &ver_chain, rs_ctx ); |
Manuel Pégourié-Gonnard | a4a5d1d | 2017-07-17 10:26:19 +0200 | [diff] [blame] | 3299 | |
Manuel Pégourié-Gonnard | c547d1a | 2017-07-05 13:28:45 +0200 | [diff] [blame] | 3300 | if( ret != 0 ) |
| 3301 | goto exit; |
| 3302 | |
Manuel Pégourié-Gonnard | 83e923b | 2017-08-23 10:55:41 +0200 | [diff] [blame] | 3303 | /* Merge end-entity flags */ |
| 3304 | ver_chain.items[0].flags |= ee_flags; |
| 3305 | |
Manuel Pégourié-Gonnard | a707e1d | 2017-07-05 17:18:42 +0200 | [diff] [blame] | 3306 | /* Build final flags, calling callback on the way if any */ |
Manuel Pégourié-Gonnard | c11e4ba | 2017-08-14 17:17:14 +0200 | [diff] [blame] | 3307 | ret = x509_crt_merge_flags_with_cb( flags, &ver_chain, f_vrfy, p_vrfy ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 3308 | |
Manuel Pégourié-Gonnard | d15795a | 2017-06-22 12:19:27 +0200 | [diff] [blame] | 3309 | exit: |
Hanno Becker | f53893b | 2019-03-28 13:45:55 +0000 | [diff] [blame] | 3310 | |
| 3311 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 3312 | mbedtls_x509_crt_free( ver_chain.trust_ca_cb_result ); |
| 3313 | mbedtls_free( ver_chain.trust_ca_cb_result ); |
| 3314 | ver_chain.trust_ca_cb_result = NULL; |
| 3315 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
| 3316 | |
Manuel Pégourié-Gonnard | 8b59049 | 2017-08-14 18:04:19 +0200 | [diff] [blame] | 3317 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
| 3318 | if( rs_ctx != NULL && ret != MBEDTLS_ERR_ECP_IN_PROGRESS ) |
| 3319 | mbedtls_x509_crt_restart_free( rs_ctx ); |
| 3320 | #endif |
| 3321 | |
Manuel Pégourié-Gonnard | 9107b5f | 2017-07-06 12:16:25 +0200 | [diff] [blame] | 3322 | /* prevent misuse of the vrfy callback - VERIFY_FAILED would be ignored by |
| 3323 | * the SSL module for authmode optional, but non-zero return from the |
| 3324 | * callback means a fatal error so it shouldn't be ignored */ |
Manuel Pégourié-Gonnard | 31458a1 | 2017-06-26 10:11:49 +0200 | [diff] [blame] | 3325 | if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ) |
| 3326 | ret = MBEDTLS_ERR_X509_FATAL_ERROR; |
| 3327 | |
Manuel Pégourié-Gonnard | d15795a | 2017-06-22 12:19:27 +0200 | [diff] [blame] | 3328 | if( ret != 0 ) |
| 3329 | { |
| 3330 | *flags = (uint32_t) -1; |
| 3331 | return( ret ); |
| 3332 | } |
| 3333 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 3334 | if( *flags != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3335 | return( MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 3336 | |
| 3337 | return( 0 ); |
| 3338 | } |
| 3339 | |
Hanno Becker | 3116fb3 | 2019-03-28 13:34:42 +0000 | [diff] [blame] | 3340 | |
| 3341 | /* |
| 3342 | * Verify the certificate validity (default profile, not restartable) |
| 3343 | */ |
| 3344 | int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt, |
| 3345 | mbedtls_x509_crt *trust_ca, |
| 3346 | mbedtls_x509_crl *ca_crl, |
| 3347 | const char *cn, uint32_t *flags, |
| 3348 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), |
| 3349 | void *p_vrfy ) |
| 3350 | { |
Jarno Lamsa | 2ee67a6 | 2019-04-01 14:59:33 +0300 | [diff] [blame] | 3351 | return( x509_crt_verify_restartable_ca_cb( crt, trust_ca, ca_crl, |
Hanno Becker | 3116fb3 | 2019-03-28 13:34:42 +0000 | [diff] [blame] | 3352 | NULL, NULL, |
| 3353 | &mbedtls_x509_crt_profile_default, |
| 3354 | cn, flags, |
| 3355 | f_vrfy, p_vrfy, NULL ) ); |
| 3356 | } |
| 3357 | |
| 3358 | /* |
| 3359 | * Verify the certificate validity (user-chosen profile, not restartable) |
| 3360 | */ |
| 3361 | int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt, |
| 3362 | mbedtls_x509_crt *trust_ca, |
| 3363 | mbedtls_x509_crl *ca_crl, |
| 3364 | const mbedtls_x509_crt_profile *profile, |
| 3365 | const char *cn, uint32_t *flags, |
| 3366 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), |
| 3367 | void *p_vrfy ) |
| 3368 | { |
Jarno Lamsa | 2ee67a6 | 2019-04-01 14:59:33 +0300 | [diff] [blame] | 3369 | return( x509_crt_verify_restartable_ca_cb( crt, trust_ca, ca_crl, |
Hanno Becker | 3116fb3 | 2019-03-28 13:34:42 +0000 | [diff] [blame] | 3370 | NULL, NULL, |
| 3371 | profile, cn, flags, |
| 3372 | f_vrfy, p_vrfy, NULL ) ); |
| 3373 | } |
| 3374 | |
| 3375 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 3376 | /* |
| 3377 | * Verify the certificate validity (user-chosen profile, CA callback, |
| 3378 | * not restartable). |
| 3379 | */ |
Jarno Lamsa | 31d9db6 | 2019-04-01 14:33:49 +0300 | [diff] [blame] | 3380 | int mbedtls_x509_crt_verify_with_ca_cb( mbedtls_x509_crt *crt, |
Hanno Becker | 3116fb3 | 2019-03-28 13:34:42 +0000 | [diff] [blame] | 3381 | mbedtls_x509_crt_ca_cb_t f_ca_cb, |
| 3382 | void *p_ca_cb, |
| 3383 | const mbedtls_x509_crt_profile *profile, |
| 3384 | const char *cn, uint32_t *flags, |
| 3385 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), |
| 3386 | void *p_vrfy ) |
| 3387 | { |
Jarno Lamsa | 2ee67a6 | 2019-04-01 14:59:33 +0300 | [diff] [blame] | 3388 | return( x509_crt_verify_restartable_ca_cb( crt, NULL, NULL, |
Hanno Becker | 3116fb3 | 2019-03-28 13:34:42 +0000 | [diff] [blame] | 3389 | f_ca_cb, p_ca_cb, |
| 3390 | profile, cn, flags, |
| 3391 | f_vrfy, p_vrfy, NULL ) ); |
| 3392 | } |
| 3393 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
| 3394 | |
| 3395 | int mbedtls_x509_crt_verify_restartable( mbedtls_x509_crt *crt, |
| 3396 | mbedtls_x509_crt *trust_ca, |
| 3397 | mbedtls_x509_crl *ca_crl, |
| 3398 | const mbedtls_x509_crt_profile *profile, |
| 3399 | const char *cn, uint32_t *flags, |
| 3400 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), |
| 3401 | void *p_vrfy, |
| 3402 | mbedtls_x509_crt_restart_ctx *rs_ctx ) |
| 3403 | { |
Jarno Lamsa | 2ee67a6 | 2019-04-01 14:59:33 +0300 | [diff] [blame] | 3404 | return( x509_crt_verify_restartable_ca_cb( crt, trust_ca, ca_crl, |
Hanno Becker | 3116fb3 | 2019-03-28 13:34:42 +0000 | [diff] [blame] | 3405 | NULL, NULL, |
| 3406 | profile, cn, flags, |
| 3407 | f_vrfy, p_vrfy, rs_ctx ) ); |
| 3408 | } |
| 3409 | |
| 3410 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 3411 | /* |
Paul Bakker | 369d2eb | 2013-09-18 11:58:25 +0200 | [diff] [blame] | 3412 | * Initialize a certificate chain |
| 3413 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3414 | void mbedtls_x509_crt_init( mbedtls_x509_crt *crt ) |
Paul Bakker | 369d2eb | 2013-09-18 11:58:25 +0200 | [diff] [blame] | 3415 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3416 | memset( crt, 0, sizeof(mbedtls_x509_crt) ); |
Paul Bakker | 369d2eb | 2013-09-18 11:58:25 +0200 | [diff] [blame] | 3417 | } |
| 3418 | |
| 3419 | /* |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 3420 | * Unallocate all certificate data |
| 3421 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3422 | void mbedtls_x509_crt_free( mbedtls_x509_crt *crt ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 3423 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3424 | mbedtls_x509_crt *cert_cur = crt; |
| 3425 | mbedtls_x509_crt *cert_prv; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 3426 | |
Glenn Strauss | a4b4041 | 2022-06-26 19:32:09 -0400 | [diff] [blame] | 3427 | while( cert_cur != NULL ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 3428 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3429 | mbedtls_pk_free( &cert_cur->pk ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 3430 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3431 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) |
| 3432 | mbedtls_free( cert_cur->sig_opts ); |
Manuel Pégourié-Gonnard | f75f2f7 | 2014-06-05 15:14:28 +0200 | [diff] [blame] | 3433 | #endif |
| 3434 | |
Glenn Strauss | a4b4041 | 2022-06-26 19:32:09 -0400 | [diff] [blame] | 3435 | mbedtls_asn1_free_named_data_list_shallow( cert_cur->issuer.next ); |
| 3436 | mbedtls_asn1_free_named_data_list_shallow( cert_cur->subject.next ); |
| 3437 | mbedtls_asn1_sequence_free( cert_cur->ext_key_usage.next ); |
| 3438 | mbedtls_asn1_sequence_free( cert_cur->subject_alt_names.next ); |
| 3439 | mbedtls_asn1_sequence_free( cert_cur->certificate_policies.next ); |
Ron Eldor | 74d9acc | 2019-03-21 14:00:03 +0200 | [diff] [blame] | 3440 | |
toth92g | 1bbc2fe | 2021-02-12 16:11:17 +0100 | [diff] [blame] | 3441 | name_cur = cert_cur->authority_key_id.authorityCertIssuer.next; |
toth92g | dd12390 | 2021-04-08 10:12:52 +0200 | [diff] [blame^] | 3442 | while ( name_cur != NULL ) |
toth92g | 1bbc2fe | 2021-02-12 16:11:17 +0100 | [diff] [blame] | 3443 | { |
| 3444 | name_prv = name_cur; |
| 3445 | name_cur = name_cur->next; |
toth92g | dd12390 | 2021-04-08 10:12:52 +0200 | [diff] [blame^] | 3446 | mbedtls_platform_zeroize( name_prv, sizeof(mbedtls_x509_name) ); |
| 3447 | mbedtls_free( name_prv ); |
toth92g | 1bbc2fe | 2021-02-12 16:11:17 +0100 | [diff] [blame] | 3448 | } |
| 3449 | |
Hanno Becker | 1a65dcd | 2019-01-31 08:57:44 +0000 | [diff] [blame] | 3450 | if( cert_cur->raw.p != NULL && cert_cur->own_buffer ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 3451 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 3452 | mbedtls_platform_zeroize( cert_cur->raw.p, cert_cur->raw.len ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3453 | mbedtls_free( cert_cur->raw.p ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 3454 | } |
| 3455 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 3456 | cert_prv = cert_cur; |
| 3457 | cert_cur = cert_cur->next; |
| 3458 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 3459 | mbedtls_platform_zeroize( cert_prv, sizeof( mbedtls_x509_crt ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 3460 | if( cert_prv != crt ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3461 | mbedtls_free( cert_prv ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 3462 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 3463 | } |
| 3464 | |
Manuel Pégourié-Gonnard | bc3f44a | 2017-07-11 11:02:20 +0200 | [diff] [blame] | 3465 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
| 3466 | /* |
| 3467 | * Initialize a restart context |
| 3468 | */ |
| 3469 | void mbedtls_x509_crt_restart_init( mbedtls_x509_crt_restart_ctx *ctx ) |
| 3470 | { |
Manuel Pégourié-Gonnard | 15d7df2 | 2017-08-17 14:33:31 +0200 | [diff] [blame] | 3471 | mbedtls_pk_restart_init( &ctx->pk ); |
Manuel Pégourié-Gonnard | 8b59049 | 2017-08-14 18:04:19 +0200 | [diff] [blame] | 3472 | |
| 3473 | ctx->parent = NULL; |
| 3474 | ctx->fallback_parent = NULL; |
Manuel Pégourié-Gonnard | 78d7e8c | 2018-07-02 12:33:14 +0200 | [diff] [blame] | 3475 | ctx->fallback_signature_is_good = 0; |
Manuel Pégourié-Gonnard | 8b59049 | 2017-08-14 18:04:19 +0200 | [diff] [blame] | 3476 | |
| 3477 | ctx->parent_is_trusted = -1; |
| 3478 | |
Manuel Pégourié-Gonnard | daf0491 | 2017-08-23 12:32:19 +0200 | [diff] [blame] | 3479 | ctx->in_progress = x509_crt_rs_none; |
Manuel Pégourié-Gonnard | 8b59049 | 2017-08-14 18:04:19 +0200 | [diff] [blame] | 3480 | ctx->self_cnt = 0; |
Manuel Pégourié-Gonnard | 83e923b | 2017-08-23 10:55:41 +0200 | [diff] [blame] | 3481 | x509_crt_verify_chain_reset( &ctx->ver_chain ); |
Manuel Pégourié-Gonnard | bc3f44a | 2017-07-11 11:02:20 +0200 | [diff] [blame] | 3482 | } |
| 3483 | |
| 3484 | /* |
| 3485 | * Free the components of a restart context |
| 3486 | */ |
| 3487 | void mbedtls_x509_crt_restart_free( mbedtls_x509_crt_restart_ctx *ctx ) |
| 3488 | { |
| 3489 | if( ctx == NULL ) |
| 3490 | return; |
| 3491 | |
Manuel Pégourié-Gonnard | 15d7df2 | 2017-08-17 14:33:31 +0200 | [diff] [blame] | 3492 | mbedtls_pk_restart_free( &ctx->pk ); |
Manuel Pégourié-Gonnard | 8b59049 | 2017-08-14 18:04:19 +0200 | [diff] [blame] | 3493 | mbedtls_x509_crt_restart_init( ctx ); |
Manuel Pégourié-Gonnard | bc3f44a | 2017-07-11 11:02:20 +0200 | [diff] [blame] | 3494 | } |
| 3495 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */ |
| 3496 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3497 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |