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