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