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