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