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