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