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