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