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