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