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