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