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