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