Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /* |
Mateusz Starzyk | 06b07fb | 2021-02-18 13:55:21 +0100 | [diff] [blame] | 2 | * TLS shared functions |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3 | * |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 4 | * Copyright The Mbed TLS Contributors |
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 | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 18 | */ |
| 19 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 20 | * http://www.ietf.org/rfc/rfc2246.txt |
| 21 | * http://www.ietf.org/rfc/rfc4346.txt |
| 22 | */ |
| 23 | |
Gilles Peskine | db09ef6 | 2020-06-03 01:43:33 +0200 | [diff] [blame] | 24 | #include "common.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 25 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 26 | #if defined(MBEDTLS_SSL_TLS_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 27 | |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 28 | #include <assert.h> |
| 29 | |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 30 | #include "mbedtls/platform.h" |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 31 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 32 | #include "mbedtls/ssl.h" |
Ronald Cron | 9f0fba3 | 2022-02-10 16:45:15 +0100 | [diff] [blame] | 33 | #include "ssl_client.h" |
Ronald Cron | 27c85e7 | 2022-03-08 11:37:55 +0100 | [diff] [blame] | 34 | #include "ssl_debug_helpers.h" |
Chris Jones | 84a773f | 2021-03-05 18:38:47 +0000 | [diff] [blame] | 35 | #include "ssl_misc.h" |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 36 | |
Janos Follath | 73c616b | 2019-12-18 15:07:04 +0000 | [diff] [blame] | 37 | #include "mbedtls/debug.h" |
| 38 | #include "mbedtls/error.h" |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 39 | #include "mbedtls/platform_util.h" |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 40 | #include "mbedtls/version.h" |
Gabor Mezei | 765862c | 2021-10-19 12:22:25 +0200 | [diff] [blame] | 41 | #include "mbedtls/constant_time.h" |
Paul Bakker | 0be444a | 2013-08-27 21:55:01 +0200 | [diff] [blame] | 42 | |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 43 | #include <string.h> |
| 44 | |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 45 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 46 | #include "mbedtls/psa_util.h" |
| 47 | #include "psa/crypto.h" |
| 48 | #endif |
Manuel Pégourié-Gonnard | 07018f9 | 2022-09-15 11:29:35 +0200 | [diff] [blame] | 49 | #include "mbedtls/legacy_or_psa.h" |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 50 | |
Janos Follath | 23bdca0 | 2016-10-07 14:47:14 +0100 | [diff] [blame] | 51 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 52 | #include "mbedtls/oid.h" |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 53 | #endif |
| 54 | |
Ronald Cron | ad8c17b | 2022-06-10 17:18:09 +0200 | [diff] [blame] | 55 | #if defined(MBEDTLS_TEST_HOOKS) |
| 56 | static mbedtls_ssl_chk_buf_ptr_args chk_buf_ptr_fail_args; |
| 57 | |
| 58 | void mbedtls_ssl_set_chk_buf_ptr_fail_args( |
| 59 | const uint8_t *cur, const uint8_t *end, size_t need ) |
| 60 | { |
| 61 | chk_buf_ptr_fail_args.cur = cur; |
| 62 | chk_buf_ptr_fail_args.end = end; |
| 63 | chk_buf_ptr_fail_args.need = need; |
| 64 | } |
| 65 | |
| 66 | void mbedtls_ssl_reset_chk_buf_ptr_fail_args( void ) |
| 67 | { |
| 68 | memset( &chk_buf_ptr_fail_args, 0, sizeof( chk_buf_ptr_fail_args ) ); |
| 69 | } |
| 70 | |
| 71 | int mbedtls_ssl_cmp_chk_buf_ptr_fail_args( mbedtls_ssl_chk_buf_ptr_args *args ) |
| 72 | { |
| 73 | return( ( chk_buf_ptr_fail_args.cur != args->cur ) || |
| 74 | ( chk_buf_ptr_fail_args.end != args->end ) || |
| 75 | ( chk_buf_ptr_fail_args.need != args->need ) ); |
| 76 | } |
| 77 | #endif /* MBEDTLS_TEST_HOOKS */ |
| 78 | |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 79 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 80 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 81 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 82 | /* Top-level Connection ID API */ |
| 83 | |
Hanno Becker | 8367ccc | 2019-05-14 11:30:10 +0100 | [diff] [blame] | 84 | int mbedtls_ssl_conf_cid( mbedtls_ssl_config *conf, |
| 85 | size_t len, |
| 86 | int ignore_other_cid ) |
Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 87 | { |
| 88 | if( len > MBEDTLS_SSL_CID_IN_LEN_MAX ) |
| 89 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 90 | |
Hanno Becker | 611ac77 | 2019-05-14 11:45:26 +0100 | [diff] [blame] | 91 | if( ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL && |
| 92 | ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE ) |
| 93 | { |
| 94 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 95 | } |
| 96 | |
| 97 | conf->ignore_unexpected_cid = ignore_other_cid; |
Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 98 | conf->cid_len = len; |
| 99 | return( 0 ); |
| 100 | } |
| 101 | |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 102 | int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl, |
| 103 | int enable, |
| 104 | unsigned char const *own_cid, |
| 105 | size_t own_cid_len ) |
| 106 | { |
Hanno Becker | 76a79ab | 2019-05-03 14:38:32 +0100 | [diff] [blame] | 107 | if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 108 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 109 | |
Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 110 | ssl->negotiate_cid = enable; |
| 111 | if( enable == MBEDTLS_SSL_CID_DISABLED ) |
| 112 | { |
| 113 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) ); |
| 114 | return( 0 ); |
| 115 | } |
| 116 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) ); |
Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 117 | MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len ); |
Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 118 | |
Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 119 | if( own_cid_len != ssl->conf->cid_len ) |
Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 120 | { |
Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 121 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID length %u does not match CID length %u in config", |
| 122 | (unsigned) own_cid_len, |
| 123 | (unsigned) ssl->conf->cid_len ) ); |
Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 124 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 125 | } |
| 126 | |
| 127 | memcpy( ssl->own_cid, own_cid, own_cid_len ); |
Hanno Becker | b7ee0cf | 2019-04-30 14:07:31 +0100 | [diff] [blame] | 128 | /* Truncation is not an issue here because |
| 129 | * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */ |
| 130 | ssl->own_cid_len = (uint8_t) own_cid_len; |
Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 131 | |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 132 | return( 0 ); |
| 133 | } |
| 134 | |
Paul Elliott | 0113cf1 | 2022-03-11 20:26:47 +0000 | [diff] [blame] | 135 | int mbedtls_ssl_get_own_cid( mbedtls_ssl_context *ssl, |
| 136 | int *enabled, |
| 137 | unsigned char own_cid[MBEDTLS_SSL_CID_OUT_LEN_MAX], |
| 138 | size_t *own_cid_len ) |
| 139 | { |
| 140 | *enabled = MBEDTLS_SSL_CID_DISABLED; |
| 141 | |
| 142 | if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 143 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 144 | |
| 145 | /* We report MBEDTLS_SSL_CID_DISABLED in case the CID length is |
| 146 | * zero as this is indistinguishable from not requesting to use |
| 147 | * the CID extension. */ |
| 148 | if( ssl->own_cid_len == 0 || ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED ) |
| 149 | return( 0 ); |
| 150 | |
| 151 | if( own_cid_len != NULL ) |
| 152 | { |
| 153 | *own_cid_len = ssl->own_cid_len; |
| 154 | if( own_cid != NULL ) |
| 155 | memcpy( own_cid, ssl->own_cid, ssl->own_cid_len ); |
| 156 | } |
| 157 | |
| 158 | *enabled = MBEDTLS_SSL_CID_ENABLED; |
| 159 | |
| 160 | return( 0 ); |
| 161 | } |
| 162 | |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 163 | int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl, |
| 164 | int *enabled, |
| 165 | unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ], |
| 166 | size_t *peer_cid_len ) |
| 167 | { |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 168 | *enabled = MBEDTLS_SSL_CID_DISABLED; |
Hanno Becker | b1f89cd | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 169 | |
Hanno Becker | 76a79ab | 2019-05-03 14:38:32 +0100 | [diff] [blame] | 170 | if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM || |
Paul Elliott | 27b0d94 | 2022-03-18 21:55:32 +0000 | [diff] [blame] | 171 | mbedtls_ssl_is_handshake_over( ssl ) == 0 ) |
Hanno Becker | 76a79ab | 2019-05-03 14:38:32 +0100 | [diff] [blame] | 172 | { |
Hanno Becker | b1f89cd | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 173 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Hanno Becker | 76a79ab | 2019-05-03 14:38:32 +0100 | [diff] [blame] | 174 | } |
Hanno Becker | b1f89cd | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 175 | |
Hanno Becker | c5f2422 | 2019-05-03 12:54:52 +0100 | [diff] [blame] | 176 | /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions |
| 177 | * were used, but client and server requested the empty CID. |
| 178 | * This is indistinguishable from not using the CID extension |
| 179 | * in the first place. */ |
Hanno Becker | b1f89cd | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 180 | if( ssl->transform_in->in_cid_len == 0 && |
| 181 | ssl->transform_in->out_cid_len == 0 ) |
| 182 | { |
| 183 | return( 0 ); |
| 184 | } |
| 185 | |
Hanno Becker | 615ef17 | 2019-05-22 16:50:35 +0100 | [diff] [blame] | 186 | if( peer_cid_len != NULL ) |
| 187 | { |
| 188 | *peer_cid_len = ssl->transform_in->out_cid_len; |
| 189 | if( peer_cid != NULL ) |
| 190 | { |
| 191 | memcpy( peer_cid, ssl->transform_in->out_cid, |
| 192 | ssl->transform_in->out_cid_len ); |
| 193 | } |
| 194 | } |
Hanno Becker | b1f89cd | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 195 | |
| 196 | *enabled = MBEDTLS_SSL_CID_ENABLED; |
| 197 | |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 198 | return( 0 ); |
| 199 | } |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 200 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 201 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 202 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 203 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 204 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Manuel Pégourié-Gonnard | 581e6b6 | 2013-07-18 12:32:27 +0200 | [diff] [blame] | 205 | /* |
| 206 | * Convert max_fragment_length codes to length. |
| 207 | * RFC 6066 says: |
| 208 | * enum{ |
| 209 | * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255) |
| 210 | * } MaxFragmentLength; |
| 211 | * and we add 0 -> extension unused |
| 212 | */ |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 213 | static unsigned int ssl_mfl_code_to_length( int mfl ) |
Manuel Pégourié-Gonnard | 581e6b6 | 2013-07-18 12:32:27 +0200 | [diff] [blame] | 214 | { |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 215 | switch( mfl ) |
| 216 | { |
| 217 | case MBEDTLS_SSL_MAX_FRAG_LEN_NONE: |
| 218 | return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN ); |
| 219 | case MBEDTLS_SSL_MAX_FRAG_LEN_512: |
| 220 | return 512; |
| 221 | case MBEDTLS_SSL_MAX_FRAG_LEN_1024: |
| 222 | return 1024; |
| 223 | case MBEDTLS_SSL_MAX_FRAG_LEN_2048: |
| 224 | return 2048; |
| 225 | case MBEDTLS_SSL_MAX_FRAG_LEN_4096: |
| 226 | return 4096; |
| 227 | default: |
| 228 | return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN ); |
| 229 | } |
| 230 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 231 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
Manuel Pégourié-Gonnard | 581e6b6 | 2013-07-18 12:32:27 +0200 | [diff] [blame] | 232 | |
Hanno Becker | 52055ae | 2019-02-06 14:30:46 +0000 | [diff] [blame] | 233 | int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst, |
| 234 | const mbedtls_ssl_session *src ) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 235 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 236 | mbedtls_ssl_session_free( dst ); |
| 237 | memcpy( dst, src, sizeof( mbedtls_ssl_session ) ); |
吴敬辉 | 0b71611 | 2021-11-29 10:46:35 +0800 | [diff] [blame] | 238 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
| 239 | dst->ticket = NULL; |
Xiaokang Qian | 8730644 | 2022-10-12 09:47:38 +0000 | [diff] [blame] | 240 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \ |
| 241 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Xiaokang Qian | 126bf8e | 2022-10-13 02:22:40 +0000 | [diff] [blame] | 242 | dst->hostname = NULL; |
吴敬辉 | 0b71611 | 2021-11-29 10:46:35 +0800 | [diff] [blame] | 243 | #endif |
Xiaokang Qian | 8730644 | 2022-10-12 09:47:38 +0000 | [diff] [blame] | 244 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
吴敬辉 | 0b71611 | 2021-11-29 10:46:35 +0800 | [diff] [blame] | 245 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 246 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Hanno Becker | 6d1986e | 2019-02-07 12:27:42 +0000 | [diff] [blame] | 247 | |
| 248 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 249 | if( src->peer_cert != NULL ) |
| 250 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 251 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 2292d1f | 2013-09-15 17:06:49 +0200 | [diff] [blame] | 252 | |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 253 | dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 254 | if( dst->peer_cert == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 255 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 256 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 257 | mbedtls_x509_crt_init( dst->peer_cert ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 258 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 259 | if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p, |
Manuel Pégourié-Gonnard | 4d2a8eb | 2014-06-13 20:33:27 +0200 | [diff] [blame] | 260 | src->peer_cert->raw.len ) ) != 0 ) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 261 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 262 | mbedtls_free( dst->peer_cert ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 263 | dst->peer_cert = NULL; |
| 264 | return( ret ); |
| 265 | } |
| 266 | } |
Hanno Becker | 6d1986e | 2019-02-07 12:27:42 +0000 | [diff] [blame] | 267 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Hanno Becker | 9198ad1 | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 268 | if( src->peer_cert_digest != NULL ) |
| 269 | { |
Hanno Becker | 9198ad1 | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 270 | dst->peer_cert_digest = |
Hanno Becker | accc599 | 2019-02-25 10:06:59 +0000 | [diff] [blame] | 271 | mbedtls_calloc( 1, src->peer_cert_digest_len ); |
Hanno Becker | 9198ad1 | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 272 | if( dst->peer_cert_digest == NULL ) |
| 273 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 274 | |
| 275 | memcpy( dst->peer_cert_digest, src->peer_cert_digest, |
| 276 | src->peer_cert_digest_len ); |
| 277 | dst->peer_cert_digest_type = src->peer_cert_digest_type; |
Hanno Becker | accc599 | 2019-02-25 10:06:59 +0000 | [diff] [blame] | 278 | dst->peer_cert_digest_len = src->peer_cert_digest_len; |
Hanno Becker | 9198ad1 | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 279 | } |
| 280 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 281 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 282 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 283 | |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 284 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 285 | if( src->ticket != NULL ) |
| 286 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 287 | dst->ticket = mbedtls_calloc( 1, src->ticket_len ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 288 | if( dst->ticket == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 289 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 290 | |
| 291 | memcpy( dst->ticket, src->ticket, src->ticket_len ); |
| 292 | } |
Xiaokang Qian | 126bf8e | 2022-10-13 02:22:40 +0000 | [diff] [blame] | 293 | |
| 294 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \ |
| 295 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 296 | if( src->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 297 | { |
| 298 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 299 | ret = mbedtls_ssl_session_set_hostname( dst, src->hostname ); |
| 300 | if( ret != 0 ) |
| 301 | return ( ret ); |
| 302 | } |
| 303 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 && |
| 304 | MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 305 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 306 | |
| 307 | return( 0 ); |
| 308 | } |
| 309 | |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 310 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 311 | MBEDTLS_CHECK_RETURN_CRITICAL |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 312 | static int resize_buffer( unsigned char **buffer, size_t len_new, size_t *len_old ) |
| 313 | { |
| 314 | unsigned char* resized_buffer = mbedtls_calloc( 1, len_new ); |
| 315 | if( resized_buffer == NULL ) |
| 316 | return -1; |
| 317 | |
| 318 | /* We want to copy len_new bytes when downsizing the buffer, and |
| 319 | * len_old bytes when upsizing, so we choose the smaller of two sizes, |
| 320 | * to fit one buffer into another. Size checks, ensuring that no data is |
| 321 | * lost, are done outside of this function. */ |
| 322 | memcpy( resized_buffer, *buffer, |
| 323 | ( len_new < *len_old ) ? len_new : *len_old ); |
| 324 | mbedtls_platform_zeroize( *buffer, *len_old ); |
| 325 | mbedtls_free( *buffer ); |
| 326 | |
| 327 | *buffer = resized_buffer; |
| 328 | *len_old = len_new; |
| 329 | |
| 330 | return 0; |
| 331 | } |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 332 | |
| 333 | static void handle_buffer_resizing( mbedtls_ssl_context *ssl, int downsizing, |
Andrzej Kurek | 069fa96 | 2021-01-07 08:02:15 -0500 | [diff] [blame] | 334 | size_t in_buf_new_len, |
| 335 | size_t out_buf_new_len ) |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 336 | { |
| 337 | int modified = 0; |
| 338 | size_t written_in = 0, iv_offset_in = 0, len_offset_in = 0; |
| 339 | size_t written_out = 0, iv_offset_out = 0, len_offset_out = 0; |
| 340 | if( ssl->in_buf != NULL ) |
| 341 | { |
| 342 | written_in = ssl->in_msg - ssl->in_buf; |
| 343 | iv_offset_in = ssl->in_iv - ssl->in_buf; |
| 344 | len_offset_in = ssl->in_len - ssl->in_buf; |
| 345 | if( downsizing ? |
| 346 | ssl->in_buf_len > in_buf_new_len && ssl->in_left < in_buf_new_len : |
| 347 | ssl->in_buf_len < in_buf_new_len ) |
| 348 | { |
| 349 | if( resize_buffer( &ssl->in_buf, in_buf_new_len, &ssl->in_buf_len ) != 0 ) |
| 350 | { |
| 351 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "input buffer resizing failed - out of memory" ) ); |
| 352 | } |
| 353 | else |
| 354 | { |
Paul Elliott | b744990 | 2021-03-10 18:14:58 +0000 | [diff] [blame] | 355 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reallocating in_buf to %" MBEDTLS_PRINTF_SIZET, |
| 356 | in_buf_new_len ) ); |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 357 | modified = 1; |
| 358 | } |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | if( ssl->out_buf != NULL ) |
| 363 | { |
| 364 | written_out = ssl->out_msg - ssl->out_buf; |
| 365 | iv_offset_out = ssl->out_iv - ssl->out_buf; |
| 366 | len_offset_out = ssl->out_len - ssl->out_buf; |
| 367 | if( downsizing ? |
| 368 | ssl->out_buf_len > out_buf_new_len && ssl->out_left < out_buf_new_len : |
| 369 | ssl->out_buf_len < out_buf_new_len ) |
| 370 | { |
| 371 | if( resize_buffer( &ssl->out_buf, out_buf_new_len, &ssl->out_buf_len ) != 0 ) |
| 372 | { |
| 373 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "output buffer resizing failed - out of memory" ) ); |
| 374 | } |
| 375 | else |
| 376 | { |
Paul Elliott | b744990 | 2021-03-10 18:14:58 +0000 | [diff] [blame] | 377 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reallocating out_buf to %" MBEDTLS_PRINTF_SIZET, |
| 378 | out_buf_new_len ) ); |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 379 | modified = 1; |
| 380 | } |
| 381 | } |
| 382 | } |
| 383 | if( modified ) |
| 384 | { |
| 385 | /* Update pointers here to avoid doing it twice. */ |
| 386 | mbedtls_ssl_reset_in_out_pointers( ssl ); |
| 387 | /* Fields below might not be properly updated with record |
| 388 | * splitting or with CID, so they are manually updated here. */ |
| 389 | ssl->out_msg = ssl->out_buf + written_out; |
| 390 | ssl->out_len = ssl->out_buf + len_offset_out; |
| 391 | ssl->out_iv = ssl->out_buf + iv_offset_out; |
| 392 | |
| 393 | ssl->in_msg = ssl->in_buf + written_in; |
| 394 | ssl->in_len = ssl->in_buf + len_offset_in; |
| 395 | ssl->in_iv = ssl->in_buf + iv_offset_in; |
| 396 | } |
| 397 | } |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 398 | #endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */ |
| 399 | |
Jerry Yu | db8c48a | 2022-01-27 14:54:54 +0800 | [diff] [blame] | 400 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Jerry Yu | ed14c93 | 2022-02-17 13:40:45 +0800 | [diff] [blame] | 401 | |
| 402 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
| 403 | typedef int (*tls_prf_fn)( const unsigned char *secret, size_t slen, |
| 404 | const char *label, |
| 405 | const unsigned char *random, size_t rlen, |
| 406 | unsigned char *dstbuf, size_t dlen ); |
| 407 | |
| 408 | static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id ); |
| 409 | |
| 410 | #endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */ |
| 411 | |
| 412 | /* Type for the TLS PRF */ |
| 413 | typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *, |
| 414 | const unsigned char *, size_t, |
| 415 | unsigned char *, size_t); |
| 416 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 417 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | ed14c93 | 2022-02-17 13:40:45 +0800 | [diff] [blame] | 418 | static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform, |
| 419 | int ciphersuite, |
| 420 | const unsigned char master[48], |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 421 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Jerry Yu | ed14c93 | 2022-02-17 13:40:45 +0800 | [diff] [blame] | 422 | int encrypt_then_mac, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 423 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Jerry Yu | ed14c93 | 2022-02-17 13:40:45 +0800 | [diff] [blame] | 424 | ssl_tls_prf_t tls_prf, |
| 425 | const unsigned char randbytes[64], |
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 426 | mbedtls_ssl_protocol_version tls_version, |
Jerry Yu | ed14c93 | 2022-02-17 13:40:45 +0800 | [diff] [blame] | 427 | unsigned endpoint, |
| 428 | const mbedtls_ssl_context *ssl ); |
| 429 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 430 | #if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 431 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | ed14c93 | 2022-02-17 13:40:45 +0800 | [diff] [blame] | 432 | static int tls_prf_sha256( const unsigned char *secret, size_t slen, |
| 433 | const char *label, |
| 434 | const unsigned char *random, size_t rlen, |
| 435 | unsigned char *dstbuf, size_t dlen ); |
| 436 | static void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *,unsigned char*, size_t * ); |
| 437 | static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int ); |
| 438 | |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 439 | #endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/ |
Jerry Yu | ed14c93 | 2022-02-17 13:40:45 +0800 | [diff] [blame] | 440 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 441 | #if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 442 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | ed14c93 | 2022-02-17 13:40:45 +0800 | [diff] [blame] | 443 | static int tls_prf_sha384( const unsigned char *secret, size_t slen, |
| 444 | const char *label, |
| 445 | const unsigned char *random, size_t rlen, |
| 446 | unsigned char *dstbuf, size_t dlen ); |
| 447 | |
| 448 | static void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *, unsigned char*, size_t * ); |
| 449 | static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int ); |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 450 | #endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/ |
Jerry Yu | ed14c93 | 2022-02-17 13:40:45 +0800 | [diff] [blame] | 451 | |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 452 | static size_t ssl_tls12_session_save( const mbedtls_ssl_session *session, |
Jerry Yu | ed14c93 | 2022-02-17 13:40:45 +0800 | [diff] [blame] | 453 | unsigned char *buf, |
| 454 | size_t buf_len ); |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 455 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 456 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 457 | static int ssl_tls12_session_load( mbedtls_ssl_session *session, |
Jerry Yu | ed14c93 | 2022-02-17 13:40:45 +0800 | [diff] [blame] | 458 | const unsigned char *buf, |
| 459 | size_t len ); |
| 460 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 461 | |
Jerry Yu | 53d23e2 | 2022-02-09 16:25:09 +0800 | [diff] [blame] | 462 | static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t ); |
| 463 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 464 | #if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Jerry Yu | db8c48a | 2022-01-27 14:54:54 +0800 | [diff] [blame] | 465 | static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t ); |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 466 | #endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/ |
Jerry Yu | db8c48a | 2022-01-27 14:54:54 +0800 | [diff] [blame] | 467 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 468 | #if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Jerry Yu | db8c48a | 2022-01-27 14:54:54 +0800 | [diff] [blame] | 469 | static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t ); |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 470 | #endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/ |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 471 | |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 472 | int mbedtls_ssl_tls_prf( const mbedtls_tls_prf_types prf, |
| 473 | const unsigned char *secret, size_t slen, |
| 474 | const char *label, |
| 475 | const unsigned char *random, size_t rlen, |
| 476 | unsigned char *dstbuf, size_t dlen ) |
| 477 | { |
| 478 | mbedtls_ssl_tls_prf_cb *tls_prf = NULL; |
| 479 | |
| 480 | switch( prf ) |
| 481 | { |
Ron Eldor | d2f25f7 | 2019-05-15 14:54:22 +0300 | [diff] [blame] | 482 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 483 | #if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 484 | case MBEDTLS_SSL_TLS_PRF_SHA384: |
| 485 | tls_prf = tls_prf_sha384; |
| 486 | break; |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 487 | #endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/ |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 488 | #if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 489 | case MBEDTLS_SSL_TLS_PRF_SHA256: |
| 490 | tls_prf = tls_prf_sha256; |
| 491 | break; |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 492 | #endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/ |
Ron Eldor | d2f25f7 | 2019-05-15 14:54:22 +0300 | [diff] [blame] | 493 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 494 | default: |
| 495 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 496 | } |
| 497 | |
| 498 | return( tls_prf( secret, slen, label, random, rlen, dstbuf, dlen ) ); |
| 499 | } |
| 500 | |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 501 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 502 | static void ssl_clear_peer_cert( mbedtls_ssl_session *session ) |
| 503 | { |
| 504 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 505 | if( session->peer_cert != NULL ) |
| 506 | { |
| 507 | mbedtls_x509_crt_free( session->peer_cert ); |
| 508 | mbedtls_free( session->peer_cert ); |
| 509 | session->peer_cert = NULL; |
| 510 | } |
| 511 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 512 | if( session->peer_cert_digest != NULL ) |
| 513 | { |
| 514 | /* Zeroization is not necessary. */ |
| 515 | mbedtls_free( session->peer_cert_digest ); |
| 516 | session->peer_cert_digest = NULL; |
| 517 | session->peer_cert_digest_type = MBEDTLS_MD_NONE; |
| 518 | session->peer_cert_digest_len = 0; |
| 519 | } |
| 520 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 521 | } |
| 522 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 523 | |
| 524 | void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl, |
| 525 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info ) |
| 526 | { |
| 527 | ((void) ciphersuite_info); |
| 528 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 529 | #if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 530 | if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 ) |
| 531 | ssl->handshake->update_checksum = ssl_update_checksum_sha384; |
| 532 | else |
| 533 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 534 | #if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 535 | if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 ) |
| 536 | ssl->handshake->update_checksum = ssl_update_checksum_sha256; |
| 537 | else |
| 538 | #endif |
| 539 | { |
| 540 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 541 | return; |
| 542 | } |
| 543 | } |
| 544 | |
XiaokangQian | adab9a6 | 2022-07-18 07:41:26 +0000 | [diff] [blame] | 545 | void mbedtls_ssl_add_hs_hdr_to_checksum( mbedtls_ssl_context *ssl, |
| 546 | unsigned hs_type, |
| 547 | size_t total_hs_len ) |
Ronald Cron | 8f6d39a | 2022-03-10 18:56:50 +0100 | [diff] [blame] | 548 | { |
| 549 | unsigned char hs_hdr[4]; |
| 550 | |
| 551 | /* Build HS header for checksum update. */ |
| 552 | hs_hdr[0] = MBEDTLS_BYTE_0( hs_type ); |
| 553 | hs_hdr[1] = MBEDTLS_BYTE_2( total_hs_len ); |
| 554 | hs_hdr[2] = MBEDTLS_BYTE_1( total_hs_len ); |
| 555 | hs_hdr[3] = MBEDTLS_BYTE_0( total_hs_len ); |
| 556 | |
| 557 | ssl->handshake->update_checksum( ssl, hs_hdr, sizeof( hs_hdr ) ); |
| 558 | } |
| 559 | |
| 560 | void mbedtls_ssl_add_hs_msg_to_checksum( mbedtls_ssl_context *ssl, |
| 561 | unsigned hs_type, |
| 562 | unsigned char const *msg, |
| 563 | size_t msg_len ) |
| 564 | { |
| 565 | mbedtls_ssl_add_hs_hdr_to_checksum( ssl, hs_type, msg_len ); |
| 566 | ssl->handshake->update_checksum( ssl, msg, msg_len ); |
| 567 | } |
| 568 | |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 569 | void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl ) |
| 570 | { |
| 571 | ((void) ssl); |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 572 | #if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 573 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 574 | psa_hash_abort( &ssl->handshake->fin_sha256_psa ); |
| 575 | psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 ); |
| 576 | #else |
| 577 | mbedtls_sha256_starts( &ssl->handshake->fin_sha256, 0 ); |
| 578 | #endif |
| 579 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 580 | #if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 581 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 582 | psa_hash_abort( &ssl->handshake->fin_sha384_psa ); |
| 583 | psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 ); |
| 584 | #else |
Andrzej Kurek | a242e83 | 2022-08-11 10:03:14 -0400 | [diff] [blame] | 585 | mbedtls_sha512_starts( &ssl->handshake->fin_sha384, 1 ); |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 586 | #endif |
| 587 | #endif |
| 588 | } |
| 589 | |
| 590 | static void ssl_update_checksum_start( mbedtls_ssl_context *ssl, |
| 591 | const unsigned char *buf, size_t len ) |
| 592 | { |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 593 | #if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 594 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 595 | psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len ); |
| 596 | #else |
| 597 | mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len ); |
| 598 | #endif |
| 599 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 600 | #if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 601 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 602 | psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len ); |
| 603 | #else |
Andrzej Kurek | a242e83 | 2022-08-11 10:03:14 -0400 | [diff] [blame] | 604 | mbedtls_sha512_update( &ssl->handshake->fin_sha384, buf, len ); |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 605 | #endif |
| 606 | #endif |
| 607 | } |
| 608 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 609 | #if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 610 | static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl, |
| 611 | const unsigned char *buf, size_t len ) |
| 612 | { |
| 613 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 614 | psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len ); |
| 615 | #else |
| 616 | mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len ); |
| 617 | #endif |
| 618 | } |
| 619 | #endif |
| 620 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 621 | #if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 622 | static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl, |
| 623 | const unsigned char *buf, size_t len ) |
| 624 | { |
| 625 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 626 | psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len ); |
| 627 | #else |
Andrzej Kurek | a242e83 | 2022-08-11 10:03:14 -0400 | [diff] [blame] | 628 | mbedtls_sha512_update( &ssl->handshake->fin_sha384, buf, len ); |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 629 | #endif |
| 630 | } |
| 631 | #endif |
| 632 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 633 | static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake ) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 634 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 635 | memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 636 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 637 | #if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 638 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 639 | handshake->fin_sha256_psa = psa_hash_operation_init(); |
| 640 | psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 ); |
| 641 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 642 | mbedtls_sha256_init( &handshake->fin_sha256 ); |
TRodziewicz | 26371e4 | 2021-06-08 16:45:41 +0200 | [diff] [blame] | 643 | mbedtls_sha256_starts( &handshake->fin_sha256, 0 ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 644 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 645 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 646 | #if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 647 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 648 | handshake->fin_sha384_psa = psa_hash_operation_init(); |
| 649 | psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 650 | #else |
Andrzej Kurek | a242e83 | 2022-08-11 10:03:14 -0400 | [diff] [blame] | 651 | mbedtls_sha512_init( &handshake->fin_sha384 ); |
| 652 | mbedtls_sha512_starts( &handshake->fin_sha384, 1 ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 653 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 654 | #endif |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 655 | |
| 656 | handshake->update_checksum = ssl_update_checksum_start; |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 657 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 658 | #if defined(MBEDTLS_DHM_C) |
| 659 | mbedtls_dhm_init( &handshake->dhm_ctx ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 660 | #endif |
Neil Armstrong | f3f4641 | 2022-04-12 14:43:39 +0200 | [diff] [blame] | 661 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 662 | mbedtls_ecdh_init( &handshake->ecdh_ctx ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 663 | #endif |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 664 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 665 | mbedtls_ecjpake_init( &handshake->ecjpake_ctx ); |
Manuel Pégourié-Gonnard | 77c0646 | 2015-09-17 13:59:49 +0200 | [diff] [blame] | 666 | #if defined(MBEDTLS_SSL_CLI_C) |
| 667 | handshake->ecjpake_cache = NULL; |
| 668 | handshake->ecjpake_cache_len = 0; |
| 669 | #endif |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 670 | #endif |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 671 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 672 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Manuel Pégourié-Gonnard | 6b7301c | 2017-08-15 12:08:45 +0200 | [diff] [blame] | 673 | mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx ); |
Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 674 | #endif |
| 675 | |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 676 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 677 | handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET; |
| 678 | #endif |
Hanno Becker | 7517312 | 2019-02-06 16:18:31 +0000 | [diff] [blame] | 679 | |
| 680 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 681 | !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 682 | mbedtls_pk_init( &handshake->peer_pubkey ); |
| 683 | #endif |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 684 | } |
| 685 | |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 686 | void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform ) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 687 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 688 | memset( transform, 0, sizeof(mbedtls_ssl_transform) ); |
Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 689 | |
Przemyslaw Stekiel | 8f80fb9 | 2022-01-11 08:28:13 +0100 | [diff] [blame] | 690 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 691 | transform->psa_key_enc = MBEDTLS_SVC_KEY_ID_INIT; |
| 692 | transform->psa_key_dec = MBEDTLS_SVC_KEY_ID_INIT; |
Przemyslaw Stekiel | 6be9cf5 | 2022-01-19 16:00:22 +0100 | [diff] [blame] | 693 | #else |
| 694 | mbedtls_cipher_init( &transform->cipher_ctx_enc ); |
| 695 | mbedtls_cipher_init( &transform->cipher_ctx_dec ); |
Przemyslaw Stekiel | 8f80fb9 | 2022-01-11 08:28:13 +0100 | [diff] [blame] | 696 | #endif |
| 697 | |
Hanno Becker | fd86ca8 | 2020-11-30 08:54:23 +0000 | [diff] [blame] | 698 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
Neil Armstrong | 39b8e7d | 2022-02-23 09:24:45 +0100 | [diff] [blame] | 699 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 700 | transform->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT; |
| 701 | transform->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT; |
Neil Armstrong | cf8841a | 2022-02-24 11:17:45 +0100 | [diff] [blame] | 702 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 703 | mbedtls_md_init( &transform->md_ctx_enc ); |
| 704 | mbedtls_md_init( &transform->md_ctx_dec ); |
Hanno Becker | d56ed24 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 705 | #endif |
Neil Armstrong | cf8841a | 2022-02-24 11:17:45 +0100 | [diff] [blame] | 706 | #endif |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 707 | } |
| 708 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 709 | void mbedtls_ssl_session_init( mbedtls_ssl_session *session ) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 710 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 711 | memset( session, 0, sizeof(mbedtls_ssl_session) ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 712 | } |
| 713 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 714 | MBEDTLS_CHECK_RETURN_CRITICAL |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 715 | static int ssl_handshake_init( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 716 | { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 717 | /* Clear old handshake information if present */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 718 | if( ssl->transform_negotiate ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 719 | mbedtls_ssl_transform_free( ssl->transform_negotiate ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 720 | if( ssl->session_negotiate ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 721 | mbedtls_ssl_session_free( ssl->session_negotiate ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 722 | if( ssl->handshake ) |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 723 | mbedtls_ssl_handshake_free( ssl ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 724 | |
| 725 | /* |
| 726 | * Either the pointers are now NULL or cleared properly and can be freed. |
| 727 | * Now allocate missing structures. |
| 728 | */ |
| 729 | if( ssl->transform_negotiate == NULL ) |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 730 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 731 | ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 732 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 733 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 734 | if( ssl->session_negotiate == NULL ) |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 735 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 736 | ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 737 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 738 | |
Paul Bakker | 82788fb | 2014-10-20 13:59:19 +0200 | [diff] [blame] | 739 | if( ssl->handshake == NULL ) |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 740 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 741 | ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 742 | } |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 743 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 744 | /* If the buffers are too small - reallocate */ |
Andrzej Kurek | 8ea6872 | 2020-04-03 06:40:47 -0400 | [diff] [blame] | 745 | |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 746 | handle_buffer_resizing( ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN, |
| 747 | MBEDTLS_SSL_OUT_BUFFER_LEN ); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 748 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 749 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 750 | /* All pointers should exist and can be directly freed without issue */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 751 | if( ssl->handshake == NULL || |
| 752 | ssl->transform_negotiate == NULL || |
| 753 | ssl->session_negotiate == NULL ) |
| 754 | { |
Manuel Pégourié-Gonnard | b2a18a2 | 2015-05-27 16:29:56 +0200 | [diff] [blame] | 755 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 756 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 757 | mbedtls_free( ssl->handshake ); |
| 758 | mbedtls_free( ssl->transform_negotiate ); |
| 759 | mbedtls_free( ssl->session_negotiate ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 760 | |
| 761 | ssl->handshake = NULL; |
| 762 | ssl->transform_negotiate = NULL; |
| 763 | ssl->session_negotiate = NULL; |
| 764 | |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 765 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 766 | } |
| 767 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 768 | /* Initialize structures */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 769 | mbedtls_ssl_session_init( ssl->session_negotiate ); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 770 | mbedtls_ssl_transform_init( ssl->transform_negotiate ); |
Paul Bakker | 968afaa | 2014-07-09 11:09:24 +0200 | [diff] [blame] | 771 | ssl_handshake_params_init( ssl->handshake ); |
| 772 | |
Jerry Yu | d0766ec | 2022-09-22 10:46:57 +0800 | [diff] [blame] | 773 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \ |
| 774 | defined(MBEDTLS_SSL_SRV_C) && \ |
| 775 | defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 776 | ssl->handshake->new_session_tickets_count = |
| 777 | ssl->conf->new_session_tickets_count ; |
| 778 | #endif |
| 779 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 780 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 781 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 782 | { |
| 783 | ssl->handshake->alt_transform_out = ssl->transform_out; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 784 | |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 785 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 786 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING; |
| 787 | else |
| 788 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING; |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 789 | |
Hanno Becker | 0f57a65 | 2020-02-05 10:37:26 +0000 | [diff] [blame] | 790 | mbedtls_ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 791 | } |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 792 | #endif |
| 793 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 794 | /* |
| 795 | * curve_list is translated to IANA TLS group identifiers here because |
| 796 | * mbedtls_ssl_conf_curves returns void and so can't return |
| 797 | * any error codes. |
| 798 | */ |
| 799 | #if defined(MBEDTLS_ECP_C) |
| 800 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 801 | /* Heap allocate and translate curve_list from internal to IANA group ids */ |
| 802 | if ( ssl->conf->curve_list != NULL ) |
| 803 | { |
| 804 | size_t length; |
| 805 | const mbedtls_ecp_group_id *curve_list = ssl->conf->curve_list; |
| 806 | |
| 807 | for( length = 0; ( curve_list[length] != MBEDTLS_ECP_DP_NONE ) && |
| 808 | ( length < MBEDTLS_ECP_DP_MAX ); length++ ) {} |
| 809 | |
| 810 | /* Leave room for zero termination */ |
| 811 | uint16_t *group_list = mbedtls_calloc( length + 1, sizeof(uint16_t) ); |
| 812 | if ( group_list == NULL ) |
| 813 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 814 | |
| 815 | for( size_t i = 0; i < length; i++ ) |
| 816 | { |
| 817 | const mbedtls_ecp_curve_info *info = |
| 818 | mbedtls_ecp_curve_info_from_grp_id( curve_list[i] ); |
| 819 | if ( info == NULL ) |
| 820 | { |
| 821 | mbedtls_free( group_list ); |
| 822 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); |
| 823 | } |
| 824 | group_list[i] = info->tls_id; |
| 825 | } |
| 826 | |
| 827 | group_list[length] = 0; |
| 828 | |
| 829 | ssl->handshake->group_list = group_list; |
| 830 | ssl->handshake->group_list_heap_allocated = 1; |
| 831 | } |
| 832 | else |
| 833 | { |
| 834 | ssl->handshake->group_list = ssl->conf->group_list; |
| 835 | ssl->handshake->group_list_heap_allocated = 0; |
| 836 | } |
| 837 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
| 838 | #endif /* MBEDTLS_ECP_C */ |
| 839 | |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 840 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
| 841 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Jerry Yu | a69269a | 2022-01-17 21:06:01 +0800 | [diff] [blame] | 842 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Jerry Yu | 713013f | 2022-01-17 18:16:35 +0800 | [diff] [blame] | 843 | /* Heap allocate and translate sig_hashes from internal hash identifiers to |
| 844 | signature algorithms IANA identifiers. */ |
| 845 | if ( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) && |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 846 | ssl->conf->sig_hashes != NULL ) |
| 847 | { |
| 848 | const int *md; |
| 849 | const int *sig_hashes = ssl->conf->sig_hashes; |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 850 | size_t sig_algs_len = 0; |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 851 | uint16_t *p; |
| 852 | |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 853 | #if defined(static_assert) |
| 854 | static_assert( MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN |
| 855 | <= ( SIZE_MAX - ( 2 * sizeof(uint16_t) ) ), |
| 856 | "MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN too big" ); |
Jerry Yu | a68dca2 | 2022-01-20 16:28:27 +0800 | [diff] [blame] | 857 | #endif |
| 858 | |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 859 | for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ ) |
| 860 | { |
| 861 | if( mbedtls_ssl_hash_from_md_alg( *md ) == MBEDTLS_SSL_HASH_NONE ) |
| 862 | continue; |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 863 | #if defined(MBEDTLS_ECDSA_C) |
| 864 | sig_algs_len += sizeof( uint16_t ); |
| 865 | #endif |
Jerry Yu | a68dca2 | 2022-01-20 16:28:27 +0800 | [diff] [blame] | 866 | |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 867 | #if defined(MBEDTLS_RSA_C) |
| 868 | sig_algs_len += sizeof( uint16_t ); |
| 869 | #endif |
| 870 | if( sig_algs_len > MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN ) |
| 871 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 872 | } |
| 873 | |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 874 | if( sig_algs_len < MBEDTLS_SSL_MIN_SIG_ALG_LIST_LEN ) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 875 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); |
| 876 | |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 877 | ssl->handshake->sig_algs = mbedtls_calloc( 1, sig_algs_len + |
| 878 | sizeof( uint16_t )); |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 879 | if( ssl->handshake->sig_algs == NULL ) |
| 880 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 881 | |
| 882 | p = (uint16_t *)ssl->handshake->sig_algs; |
| 883 | for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ ) |
| 884 | { |
| 885 | unsigned char hash = mbedtls_ssl_hash_from_md_alg( *md ); |
| 886 | if( hash == MBEDTLS_SSL_HASH_NONE ) |
| 887 | continue; |
Jerry Yu | 6106fdc | 2022-01-12 16:36:14 +0800 | [diff] [blame] | 888 | #if defined(MBEDTLS_ECDSA_C) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 889 | *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_ECDSA); |
| 890 | p++; |
Jerry Yu | 6106fdc | 2022-01-12 16:36:14 +0800 | [diff] [blame] | 891 | #endif |
| 892 | #if defined(MBEDTLS_RSA_C) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 893 | *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_RSA); |
| 894 | p++; |
Jerry Yu | 6106fdc | 2022-01-12 16:36:14 +0800 | [diff] [blame] | 895 | #endif |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 896 | } |
Gabor Mezei | 15b95a6 | 2022-05-09 16:37:58 +0200 | [diff] [blame] | 897 | *p = MBEDTLS_TLS_SIG_NONE; |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 898 | ssl->handshake->sig_algs_heap_allocated = 1; |
| 899 | } |
| 900 | else |
Jerry Yu | a69269a | 2022-01-17 21:06:01 +0800 | [diff] [blame] | 901 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 902 | { |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 903 | ssl->handshake->sig_algs_heap_allocated = 0; |
| 904 | } |
Jerry Yu | cc53910 | 2022-06-27 16:27:35 +0800 | [diff] [blame] | 905 | #endif /* !MBEDTLS_DEPRECATED_REMOVED */ |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 906 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 907 | return( 0 ); |
| 908 | } |
| 909 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 910 | #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 911 | /* Dummy cookie callbacks for defaults */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 912 | MBEDTLS_CHECK_RETURN_CRITICAL |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 913 | static int ssl_cookie_write_dummy( void *ctx, |
| 914 | unsigned char **p, unsigned char *end, |
| 915 | const unsigned char *cli_id, size_t cli_id_len ) |
| 916 | { |
| 917 | ((void) ctx); |
| 918 | ((void) p); |
| 919 | ((void) end); |
| 920 | ((void) cli_id); |
| 921 | ((void) cli_id_len); |
| 922 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 923 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 924 | } |
| 925 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 926 | MBEDTLS_CHECK_RETURN_CRITICAL |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 927 | static int ssl_cookie_check_dummy( void *ctx, |
| 928 | const unsigned char *cookie, size_t cookie_len, |
| 929 | const unsigned char *cli_id, size_t cli_id_len ) |
| 930 | { |
| 931 | ((void) ctx); |
| 932 | ((void) cookie); |
| 933 | ((void) cookie_len); |
| 934 | ((void) cli_id); |
| 935 | ((void) cli_id_len); |
| 936 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 937 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 938 | } |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 939 | #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 940 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 941 | /* |
| 942 | * Initialize an SSL context |
| 943 | */ |
Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 944 | void mbedtls_ssl_init( mbedtls_ssl_context *ssl ) |
| 945 | { |
| 946 | memset( ssl, 0, sizeof( mbedtls_ssl_context ) ); |
| 947 | } |
| 948 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 949 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 950 | static int ssl_conf_version_check( const mbedtls_ssl_context *ssl ) |
| 951 | { |
Ronald Cron | 086ee0b | 2022-03-15 15:18:51 +0100 | [diff] [blame] | 952 | const mbedtls_ssl_config *conf = ssl->conf; |
| 953 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 954 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Ronald Cron | 086ee0b | 2022-03-15 15:18:51 +0100 | [diff] [blame] | 955 | if( mbedtls_ssl_conf_is_tls13_only( conf ) ) |
| 956 | { |
XiaokangQian | ed582dd | 2022-04-13 08:21:05 +0000 | [diff] [blame] | 957 | if( conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 958 | { |
| 959 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS 1.3 is not yet supported." ) ); |
| 960 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 961 | } |
| 962 | |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 963 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls13 only." ) ); |
| 964 | return( 0 ); |
| 965 | } |
| 966 | #endif |
| 967 | |
| 968 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Ronald Cron | 086ee0b | 2022-03-15 15:18:51 +0100 | [diff] [blame] | 969 | if( mbedtls_ssl_conf_is_tls12_only( conf ) ) |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 970 | { |
| 971 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls12 only." ) ); |
| 972 | return( 0 ); |
| 973 | } |
| 974 | #endif |
| 975 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 976 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Ronald Cron | 086ee0b | 2022-03-15 15:18:51 +0100 | [diff] [blame] | 977 | if( mbedtls_ssl_conf_is_hybrid_tls12_tls13( conf ) ) |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 978 | { |
XiaokangQian | ed582dd | 2022-04-13 08:21:05 +0000 | [diff] [blame] | 979 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 980 | { |
| 981 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS not yet supported in Hybrid TLS 1.3 + TLS 1.2" ) ); |
| 982 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 983 | } |
| 984 | |
XiaokangQian | 8f9dfe4 | 2022-04-15 02:52:39 +0000 | [diff] [blame] | 985 | if( conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 986 | { |
| 987 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS 1.3 server is not supported yet." ) ); |
| 988 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 989 | } |
| 990 | |
| 991 | |
Ronald Cron | e1d3f06 | 2022-02-10 14:50:54 +0100 | [diff] [blame] | 992 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is TLS 1.3 or TLS 1.2." ) ); |
| 993 | return( 0 ); |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 994 | } |
| 995 | #endif |
| 996 | |
| 997 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "The SSL configuration is invalid." ) ); |
| 998 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); |
| 999 | } |
| 1000 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 1001 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1002 | static int ssl_conf_check(const mbedtls_ssl_context *ssl) |
| 1003 | { |
| 1004 | int ret; |
| 1005 | ret = ssl_conf_version_check( ssl ); |
| 1006 | if( ret != 0 ) |
| 1007 | return( ret ); |
| 1008 | |
| 1009 | /* Space for further checks */ |
| 1010 | |
| 1011 | return( 0 ); |
| 1012 | } |
| 1013 | |
Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 1014 | /* |
| 1015 | * Setup an SSL context |
| 1016 | */ |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 1017 | |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 1018 | int mbedtls_ssl_setup( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 1897af9 | 2015-05-10 23:27:38 +0200 | [diff] [blame] | 1019 | const mbedtls_ssl_config *conf ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1020 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1021 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1022 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; |
| 1023 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1024 | |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 1025 | ssl->conf = conf; |
Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 1026 | |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1027 | if( ( ret = ssl_conf_check( ssl ) ) != 0 ) |
| 1028 | return( ret ); |
| 1029 | |
Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 1030 | /* |
Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 1031 | * Prepare base structures |
Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 1032 | */ |
k-stachowiak | c9a5f02 | 2018-07-24 13:53:31 +0200 | [diff] [blame] | 1033 | |
| 1034 | /* Set to NULL in case of an error condition */ |
| 1035 | ssl->out_buf = NULL; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1036 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1037 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 1038 | ssl->in_buf_len = in_buf_len; |
| 1039 | #endif |
| 1040 | ssl->in_buf = mbedtls_calloc( 1, in_buf_len ); |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 1041 | if( ssl->in_buf == NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1042 | { |
Paul Elliott | d48d5c6 | 2021-01-07 14:47:05 +0000 | [diff] [blame] | 1043 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", in_buf_len ) ); |
k-stachowiak | 9f7798e | 2018-07-31 16:52:32 +0200 | [diff] [blame] | 1044 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1045 | goto error; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 1046 | } |
| 1047 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1048 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 1049 | ssl->out_buf_len = out_buf_len; |
| 1050 | #endif |
| 1051 | ssl->out_buf = mbedtls_calloc( 1, out_buf_len ); |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 1052 | if( ssl->out_buf == NULL ) |
| 1053 | { |
Paul Elliott | d48d5c6 | 2021-01-07 14:47:05 +0000 | [diff] [blame] | 1054 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", out_buf_len ) ); |
k-stachowiak | 9f7798e | 2018-07-31 16:52:32 +0200 | [diff] [blame] | 1055 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1056 | goto error; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1057 | } |
| 1058 | |
Hanno Becker | 3e6f8ab | 2020-02-05 10:40:57 +0000 | [diff] [blame] | 1059 | mbedtls_ssl_reset_in_out_pointers( ssl ); |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 1060 | |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 1061 | #if defined(MBEDTLS_SSL_DTLS_SRTP) |
Ron Eldor | 3adb992 | 2017-12-21 10:15:08 +0200 | [diff] [blame] | 1062 | memset( &ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info) ); |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 1063 | #endif |
| 1064 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1065 | if( ( ret = ssl_handshake_init( ssl ) ) != 0 ) |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1066 | goto error; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1067 | |
| 1068 | return( 0 ); |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1069 | |
| 1070 | error: |
| 1071 | mbedtls_free( ssl->in_buf ); |
| 1072 | mbedtls_free( ssl->out_buf ); |
| 1073 | |
| 1074 | ssl->conf = NULL; |
| 1075 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1076 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 1077 | ssl->in_buf_len = 0; |
| 1078 | ssl->out_buf_len = 0; |
| 1079 | #endif |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1080 | ssl->in_buf = NULL; |
| 1081 | ssl->out_buf = NULL; |
| 1082 | |
| 1083 | ssl->in_hdr = NULL; |
| 1084 | ssl->in_ctr = NULL; |
| 1085 | ssl->in_len = NULL; |
| 1086 | ssl->in_iv = NULL; |
| 1087 | ssl->in_msg = NULL; |
| 1088 | |
| 1089 | ssl->out_hdr = NULL; |
| 1090 | ssl->out_ctr = NULL; |
| 1091 | ssl->out_len = NULL; |
| 1092 | ssl->out_iv = NULL; |
| 1093 | ssl->out_msg = NULL; |
| 1094 | |
k-stachowiak | 9f7798e | 2018-07-31 16:52:32 +0200 | [diff] [blame] | 1095 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1096 | } |
| 1097 | |
| 1098 | /* |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 1099 | * Reset an initialized and used SSL context for re-use while retaining |
| 1100 | * all application-set variables, function pointers and data. |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 1101 | * |
| 1102 | * If partial is non-zero, keep data in the input buffer and client ID. |
| 1103 | * (Use when a DTLS client reconnects from the same port.) |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 1104 | */ |
XiaokangQian | 78b1fa7 | 2022-01-19 06:56:30 +0000 | [diff] [blame] | 1105 | void mbedtls_ssl_session_reset_msg_layer( mbedtls_ssl_context *ssl, |
| 1106 | int partial ) |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 1107 | { |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1108 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 1109 | size_t in_buf_len = ssl->in_buf_len; |
| 1110 | size_t out_buf_len = ssl->out_buf_len; |
| 1111 | #else |
| 1112 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; |
| 1113 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; |
| 1114 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1115 | |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1116 | #if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || !defined(MBEDTLS_SSL_SRV_C) |
| 1117 | partial = 0; |
Hanno Becker | 7e77213 | 2018-08-10 12:38:21 +0100 | [diff] [blame] | 1118 | #endif |
| 1119 | |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 1120 | /* Cancel any possibly running timer */ |
Hanno Becker | 0f57a65 | 2020-02-05 10:37:26 +0000 | [diff] [blame] | 1121 | mbedtls_ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 1122 | |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1123 | mbedtls_ssl_reset_in_out_pointers( ssl ); |
| 1124 | |
| 1125 | /* Reset incoming message parsing */ |
| 1126 | ssl->in_offt = NULL; |
| 1127 | ssl->nb_zero = 0; |
| 1128 | ssl->in_msgtype = 0; |
| 1129 | ssl->in_msglen = 0; |
| 1130 | ssl->in_hslen = 0; |
| 1131 | ssl->keep_current_message = 0; |
| 1132 | ssl->transform_in = NULL; |
| 1133 | |
| 1134 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 1135 | ssl->next_record_offset = 0; |
| 1136 | ssl->in_epoch = 0; |
| 1137 | #endif |
| 1138 | |
| 1139 | /* Keep current datagram if partial == 1 */ |
| 1140 | if( partial == 0 ) |
| 1141 | { |
| 1142 | ssl->in_left = 0; |
| 1143 | memset( ssl->in_buf, 0, in_buf_len ); |
| 1144 | } |
| 1145 | |
Ronald Cron | ad8c17b | 2022-06-10 17:18:09 +0200 | [diff] [blame] | 1146 | ssl->send_alert = 0; |
| 1147 | |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1148 | /* Reset outgoing message writing */ |
| 1149 | ssl->out_msgtype = 0; |
| 1150 | ssl->out_msglen = 0; |
| 1151 | ssl->out_left = 0; |
| 1152 | memset( ssl->out_buf, 0, out_buf_len ); |
| 1153 | memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) ); |
| 1154 | ssl->transform_out = NULL; |
| 1155 | |
| 1156 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 1157 | mbedtls_ssl_dtls_replay_reset( ssl ); |
| 1158 | #endif |
| 1159 | |
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1160 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1161 | if( ssl->transform ) |
| 1162 | { |
| 1163 | mbedtls_ssl_transform_free( ssl->transform ); |
| 1164 | mbedtls_free( ssl->transform ); |
| 1165 | ssl->transform = NULL; |
| 1166 | } |
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1167 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 1168 | |
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1169 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 1170 | mbedtls_ssl_transform_free( ssl->transform_application ); |
| 1171 | mbedtls_free( ssl->transform_application ); |
| 1172 | ssl->transform_application = NULL; |
| 1173 | |
| 1174 | if( ssl->handshake != NULL ) |
| 1175 | { |
| 1176 | mbedtls_ssl_transform_free( ssl->handshake->transform_earlydata ); |
| 1177 | mbedtls_free( ssl->handshake->transform_earlydata ); |
| 1178 | ssl->handshake->transform_earlydata = NULL; |
| 1179 | |
| 1180 | mbedtls_ssl_transform_free( ssl->handshake->transform_handshake ); |
| 1181 | mbedtls_free( ssl->handshake->transform_handshake ); |
| 1182 | ssl->handshake->transform_handshake = NULL; |
| 1183 | } |
| 1184 | |
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1185 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1186 | } |
| 1187 | |
| 1188 | int mbedtls_ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial ) |
| 1189 | { |
| 1190 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 1191 | |
| 1192 | ssl->state = MBEDTLS_SSL_HELLO_REQUEST; |
| 1193 | |
XiaokangQian | 78b1fa7 | 2022-01-19 06:56:30 +0000 | [diff] [blame] | 1194 | mbedtls_ssl_session_reset_msg_layer( ssl, partial ); |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1195 | |
| 1196 | /* Reset renegotiation state */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1197 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 1198 | ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE; |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 1199 | ssl->renego_records_seen = 0; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1200 | |
| 1201 | ssl->verify_data_len = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1202 | memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN ); |
| 1203 | memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN ); |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 1204 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1205 | ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1206 | |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1207 | ssl->session_in = NULL; |
Hanno Becker | 7864090 | 2018-08-13 16:35:15 +0100 | [diff] [blame] | 1208 | ssl->session_out = NULL; |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 1209 | if( ssl->session ) |
| 1210 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1211 | mbedtls_ssl_session_free( ssl->session ); |
| 1212 | mbedtls_free( ssl->session ); |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 1213 | ssl->session = NULL; |
| 1214 | } |
| 1215 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1216 | #if defined(MBEDTLS_SSL_ALPN) |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 1217 | ssl->alpn_chosen = NULL; |
| 1218 | #endif |
| 1219 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 1220 | #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C) |
Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 1221 | #if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 1222 | if( partial == 0 ) |
Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 1223 | #endif |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 1224 | { |
| 1225 | mbedtls_free( ssl->cli_id ); |
| 1226 | ssl->cli_id = NULL; |
| 1227 | ssl->cli_id_len = 0; |
| 1228 | } |
Manuel Pégourié-Gonnard | 43c0218 | 2014-07-22 17:32:01 +0200 | [diff] [blame] | 1229 | #endif |
| 1230 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1231 | if( ( ret = ssl_handshake_init( ssl ) ) != 0 ) |
| 1232 | return( ret ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1233 | |
| 1234 | return( 0 ); |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 1235 | } |
| 1236 | |
Manuel Pégourié-Gonnard | 779e429 | 2013-08-03 13:50:48 +0200 | [diff] [blame] | 1237 | /* |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 1238 | * Reset an initialized and used SSL context for re-use while retaining |
| 1239 | * all application-set variables, function pointers and data. |
| 1240 | */ |
| 1241 | int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl ) |
| 1242 | { |
Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 1243 | return( mbedtls_ssl_session_reset_int( ssl, 0 ) ); |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 1244 | } |
| 1245 | |
| 1246 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1247 | * SSL set accessors |
| 1248 | */ |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1249 | void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1250 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1251 | conf->endpoint = endpoint; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1252 | } |
| 1253 | |
Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 1254 | void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport ) |
Manuel Pégourié-Gonnard | 0b1ff29 | 2014-02-06 13:04:16 +0100 | [diff] [blame] | 1255 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1256 | conf->transport = transport; |
Manuel Pégourié-Gonnard | 0b1ff29 | 2014-02-06 13:04:16 +0100 | [diff] [blame] | 1257 | } |
| 1258 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1259 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1260 | void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode ) |
Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 1261 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1262 | conf->anti_replay = mode; |
Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 1263 | } |
| 1264 | #endif |
| 1265 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1266 | void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit ) |
Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 1267 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1268 | conf->badmac_limit = limit; |
Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 1269 | } |
Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 1270 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1271 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 1272 | |
Hanno Becker | 1841b0a | 2018-08-24 11:13:57 +0100 | [diff] [blame] | 1273 | void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl, |
| 1274 | unsigned allow_packing ) |
Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 1275 | { |
| 1276 | ssl->disable_datagram_packing = !allow_packing; |
| 1277 | } |
| 1278 | |
| 1279 | void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf, |
| 1280 | uint32_t min, uint32_t max ) |
Manuel Pégourié-Gonnard | 905dd24 | 2014-10-01 12:03:55 +0200 | [diff] [blame] | 1281 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1282 | conf->hs_timeout_min = min; |
| 1283 | conf->hs_timeout_max = max; |
Manuel Pégourié-Gonnard | 905dd24 | 2014-10-01 12:03:55 +0200 | [diff] [blame] | 1284 | } |
| 1285 | #endif |
| 1286 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1287 | void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1288 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1289 | conf->authmode = authmode; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1290 | } |
| 1291 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1292 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1293 | void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 1294 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 1295 | void *p_vrfy ) |
| 1296 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1297 | conf->f_vrfy = f_vrfy; |
| 1298 | conf->p_vrfy = p_vrfy; |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 1299 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1300 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 1301 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1302 | void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf, |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 1303 | int (*f_rng)(void *, unsigned char *, size_t), |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1304 | void *p_rng ) |
| 1305 | { |
Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 1306 | conf->f_rng = f_rng; |
| 1307 | conf->p_rng = p_rng; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1308 | } |
| 1309 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1310 | void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | fd47423 | 2015-06-23 16:34:24 +0200 | [diff] [blame] | 1311 | void (*f_dbg)(void *, int, const char *, int, const char *), |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1312 | void *p_dbg ) |
| 1313 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1314 | conf->f_dbg = f_dbg; |
| 1315 | conf->p_dbg = p_dbg; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1316 | } |
| 1317 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1318 | void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 1319 | void *p_bio, |
Simon Butcher | e846b51 | 2016-03-01 17:31:49 +0000 | [diff] [blame] | 1320 | mbedtls_ssl_send_t *f_send, |
| 1321 | mbedtls_ssl_recv_t *f_recv, |
| 1322 | mbedtls_ssl_recv_timeout_t *f_recv_timeout ) |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 1323 | { |
| 1324 | ssl->p_bio = p_bio; |
| 1325 | ssl->f_send = f_send; |
| 1326 | ssl->f_recv = f_recv; |
| 1327 | ssl->f_recv_timeout = f_recv_timeout; |
Manuel Pégourié-Gonnard | 97fd52c | 2015-05-06 15:38:52 +0100 | [diff] [blame] | 1328 | } |
| 1329 | |
Manuel Pégourié-Gonnard | 6e7aaca | 2018-08-20 10:37:23 +0200 | [diff] [blame] | 1330 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 1331 | void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu ) |
| 1332 | { |
| 1333 | ssl->mtu = mtu; |
| 1334 | } |
| 1335 | #endif |
| 1336 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1337 | void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout ) |
Manuel Pégourié-Gonnard | 97fd52c | 2015-05-06 15:38:52 +0100 | [diff] [blame] | 1338 | { |
| 1339 | conf->read_timeout = timeout; |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 1340 | } |
| 1341 | |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 1342 | void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl, |
| 1343 | void *p_timer, |
Simon Butcher | e846b51 | 2016-03-01 17:31:49 +0000 | [diff] [blame] | 1344 | mbedtls_ssl_set_timer_t *f_set_timer, |
| 1345 | mbedtls_ssl_get_timer_t *f_get_timer ) |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 1346 | { |
| 1347 | ssl->p_timer = p_timer; |
| 1348 | ssl->f_set_timer = f_set_timer; |
| 1349 | ssl->f_get_timer = f_get_timer; |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 1350 | |
| 1351 | /* Make sure we start with no timer running */ |
Hanno Becker | 0f57a65 | 2020-02-05 10:37:26 +0000 | [diff] [blame] | 1352 | mbedtls_ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 1353 | } |
| 1354 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1355 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1356 | void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf, |
Hanno Becker | a637ff6 | 2021-04-15 08:42:48 +0100 | [diff] [blame] | 1357 | void *p_cache, |
| 1358 | mbedtls_ssl_cache_get_t *f_get_cache, |
| 1359 | mbedtls_ssl_cache_set_t *f_set_cache ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1360 | { |
Manuel Pégourié-Gonnard | 5cb3308 | 2015-05-06 18:06:26 +0100 | [diff] [blame] | 1361 | conf->p_cache = p_cache; |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1362 | conf->f_get_cache = f_get_cache; |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1363 | conf->f_set_cache = f_set_cache; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1364 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1365 | #endif /* MBEDTLS_SSL_SRV_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1366 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1367 | #if defined(MBEDTLS_SSL_CLI_C) |
| 1368 | int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1369 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1370 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1371 | |
| 1372 | if( ssl == NULL || |
| 1373 | session == NULL || |
| 1374 | ssl->session_negotiate == NULL || |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1375 | ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT ) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1376 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1377 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1378 | } |
| 1379 | |
Hanno Becker | e810bbc | 2021-05-14 16:01:05 +0100 | [diff] [blame] | 1380 | if( ssl->handshake->resume == 1 ) |
| 1381 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 1382 | |
Jerry Yu | 2109206 | 2022-10-10 21:21:31 +0800 | [diff] [blame] | 1383 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 1384 | if( session->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 ) |
Jerry Yu | 40afab6 | 2022-10-08 10:42:13 +0800 | [diff] [blame] | 1385 | { |
Jerry Yu | 2109206 | 2022-10-10 21:21:31 +0800 | [diff] [blame] | 1386 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 1387 | mbedtls_ssl_ciphersuite_from_id( session->ciphersuite ); |
| 1388 | |
| 1389 | if( mbedtls_ssl_validate_ciphersuite( |
| 1390 | ssl, ciphersuite_info, MBEDTLS_SSL_VERSION_TLS1_3, |
| 1391 | MBEDTLS_SSL_VERSION_TLS1_3 ) != 0 ) |
| 1392 | { |
| 1393 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "%d is not a valid TLS 1.3 ciphersuite.", |
| 1394 | session->ciphersuite ) ); |
| 1395 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1396 | } |
Jerry Yu | 40afab6 | 2022-10-08 10:42:13 +0800 | [diff] [blame] | 1397 | } |
Jerry Yu | 2109206 | 2022-10-10 21:21:31 +0800 | [diff] [blame] | 1398 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Jerry Yu | 40afab6 | 2022-10-08 10:42:13 +0800 | [diff] [blame] | 1399 | |
Hanno Becker | 52055ae | 2019-02-06 14:30:46 +0000 | [diff] [blame] | 1400 | if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate, |
| 1401 | session ) ) != 0 ) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1402 | return( ret ); |
| 1403 | |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 1404 | ssl->handshake->resume = 1; |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1405 | |
| 1406 | return( 0 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1407 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1408 | #endif /* MBEDTLS_SSL_CLI_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1409 | |
Mateusz Starzyk | 06b07fb | 2021-02-18 13:55:21 +0100 | [diff] [blame] | 1410 | void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf, |
| 1411 | const int *ciphersuites ) |
| 1412 | { |
Hanno Becker | d60b6c6 | 2021-04-29 12:04:11 +0100 | [diff] [blame] | 1413 | conf->ciphersuite_list = ciphersuites; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1414 | } |
| 1415 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 1416 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | cadebe5 | 2021-08-24 10:36:45 +0800 | [diff] [blame] | 1417 | void mbedtls_ssl_conf_tls13_key_exchange_modes( mbedtls_ssl_config *conf, |
Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 1418 | const int kex_modes ) |
| 1419 | { |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 1420 | conf->tls13_kex_modes = kex_modes & MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL; |
Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 1421 | } |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 1422 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 1423 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1424 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 6e3ee3a | 2015-06-17 10:58:20 +0200 | [diff] [blame] | 1425 | void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf, |
Nicholas Wilson | 2088e2e | 2015-09-08 16:53:18 +0100 | [diff] [blame] | 1426 | const mbedtls_x509_crt_profile *profile ) |
Manuel Pégourié-Gonnard | 6e3ee3a | 2015-06-17 10:58:20 +0200 | [diff] [blame] | 1427 | { |
| 1428 | conf->cert_profile = profile; |
| 1429 | } |
| 1430 | |
Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 1431 | static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert ) |
| 1432 | { |
| 1433 | mbedtls_ssl_key_cert *cur = key_cert, *next; |
| 1434 | |
| 1435 | while( cur != NULL ) |
| 1436 | { |
| 1437 | next = cur->next; |
| 1438 | mbedtls_free( cur ); |
| 1439 | cur = next; |
| 1440 | } |
| 1441 | } |
| 1442 | |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1443 | /* Append a new keycert entry to a (possibly empty) list */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 1444 | MBEDTLS_CHECK_RETURN_CRITICAL |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1445 | static int ssl_append_key_cert( mbedtls_ssl_key_cert **head, |
| 1446 | mbedtls_x509_crt *cert, |
| 1447 | mbedtls_pk_context *key ) |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1448 | { |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1449 | mbedtls_ssl_key_cert *new_cert; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1450 | |
Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 1451 | if( cert == NULL ) |
| 1452 | { |
| 1453 | /* Free list if cert is null */ |
| 1454 | ssl_key_cert_free( *head ); |
| 1455 | *head = NULL; |
| 1456 | return( 0 ); |
| 1457 | } |
| 1458 | |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1459 | new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) ); |
| 1460 | if( new_cert == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 1461 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1462 | |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1463 | new_cert->cert = cert; |
| 1464 | new_cert->key = key; |
| 1465 | new_cert->next = NULL; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1466 | |
Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 1467 | /* Update head if the list was null, else add to the end */ |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1468 | if( *head == NULL ) |
Paul Bakker | 0333b97 | 2013-11-04 17:08:28 +0100 | [diff] [blame] | 1469 | { |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1470 | *head = new_cert; |
Paul Bakker | 0333b97 | 2013-11-04 17:08:28 +0100 | [diff] [blame] | 1471 | } |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1472 | else |
| 1473 | { |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1474 | mbedtls_ssl_key_cert *cur = *head; |
| 1475 | while( cur->next != NULL ) |
| 1476 | cur = cur->next; |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1477 | cur->next = new_cert; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1478 | } |
| 1479 | |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1480 | return( 0 ); |
| 1481 | } |
| 1482 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1483 | int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1484 | mbedtls_x509_crt *own_cert, |
| 1485 | mbedtls_pk_context *pk_key ) |
| 1486 | { |
Manuel Pégourié-Gonnard | 17a40cd | 2015-05-10 23:17:17 +0200 | [diff] [blame] | 1487 | return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) ); |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1488 | } |
| 1489 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1490 | void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 1491 | mbedtls_x509_crt *ca_chain, |
| 1492 | mbedtls_x509_crl *ca_crl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1493 | { |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 1494 | conf->ca_chain = ca_chain; |
| 1495 | conf->ca_crl = ca_crl; |
Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 1496 | |
| 1497 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 1498 | /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb() |
| 1499 | * cannot be used together. */ |
| 1500 | conf->f_ca_cb = NULL; |
| 1501 | conf->p_ca_cb = NULL; |
| 1502 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1503 | } |
Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 1504 | |
| 1505 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 1506 | void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf, |
Hanno Becker | afd0b0a | 2019-03-27 16:55:01 +0000 | [diff] [blame] | 1507 | mbedtls_x509_crt_ca_cb_t f_ca_cb, |
Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 1508 | void *p_ca_cb ) |
| 1509 | { |
| 1510 | conf->f_ca_cb = f_ca_cb; |
| 1511 | conf->p_ca_cb = p_ca_cb; |
| 1512 | |
| 1513 | /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb() |
| 1514 | * cannot be used together. */ |
| 1515 | conf->ca_chain = NULL; |
| 1516 | conf->ca_crl = NULL; |
| 1517 | } |
| 1518 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1519 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | eb2c658 | 2012-09-27 19:15:01 +0000 | [diff] [blame] | 1520 | |
Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 1521 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Glenn Strauss | 6989407 | 2022-01-24 12:58:00 -0500 | [diff] [blame] | 1522 | const unsigned char *mbedtls_ssl_get_hs_sni( mbedtls_ssl_context *ssl, |
| 1523 | size_t *name_len ) |
| 1524 | { |
| 1525 | *name_len = ssl->handshake->sni_name_len; |
| 1526 | return( ssl->handshake->sni_name ); |
| 1527 | } |
| 1528 | |
Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 1529 | int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl, |
| 1530 | mbedtls_x509_crt *own_cert, |
| 1531 | mbedtls_pk_context *pk_key ) |
| 1532 | { |
| 1533 | return( ssl_append_key_cert( &ssl->handshake->sni_key_cert, |
| 1534 | own_cert, pk_key ) ); |
| 1535 | } |
Manuel Pégourié-Gonnard | 22bfa4b | 2015-05-11 08:46:37 +0200 | [diff] [blame] | 1536 | |
| 1537 | void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl, |
| 1538 | mbedtls_x509_crt *ca_chain, |
| 1539 | mbedtls_x509_crl *ca_crl ) |
| 1540 | { |
| 1541 | ssl->handshake->sni_ca_chain = ca_chain; |
| 1542 | ssl->handshake->sni_ca_crl = ca_crl; |
| 1543 | } |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 1544 | |
Glenn Strauss | 999ef70 | 2022-03-11 01:37:23 -0500 | [diff] [blame] | 1545 | #if defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED) |
| 1546 | void mbedtls_ssl_set_hs_dn_hints( mbedtls_ssl_context *ssl, |
| 1547 | const mbedtls_x509_crt *crt) |
| 1548 | { |
| 1549 | ssl->handshake->dn_hints = crt; |
| 1550 | } |
| 1551 | #endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */ |
| 1552 | |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 1553 | void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl, |
| 1554 | int authmode ) |
| 1555 | { |
| 1556 | ssl->handshake->sni_authmode = authmode; |
| 1557 | } |
Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 1558 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
| 1559 | |
Hanno Becker | 8927c83 | 2019-04-03 12:52:50 +0100 | [diff] [blame] | 1560 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 1561 | void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl, |
| 1562 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), |
| 1563 | void *p_vrfy ) |
| 1564 | { |
| 1565 | ssl->f_vrfy = f_vrfy; |
| 1566 | ssl->p_vrfy = p_vrfy; |
| 1567 | } |
| 1568 | #endif |
| 1569 | |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 1570 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 1571 | /* |
| 1572 | * Set EC J-PAKE password for current handshake |
| 1573 | */ |
| 1574 | int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl, |
| 1575 | const unsigned char *pw, |
| 1576 | size_t pw_len ) |
| 1577 | { |
| 1578 | mbedtls_ecjpake_role role; |
| 1579 | |
Janos Follath | 8eb6413 | 2016-06-03 15:40:57 +0100 | [diff] [blame] | 1580 | if( ssl->handshake == NULL || ssl->conf == NULL ) |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 1581 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1582 | |
| 1583 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 1584 | role = MBEDTLS_ECJPAKE_SERVER; |
| 1585 | else |
| 1586 | role = MBEDTLS_ECJPAKE_CLIENT; |
| 1587 | |
| 1588 | return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx, |
| 1589 | role, |
| 1590 | MBEDTLS_MD_SHA256, |
| 1591 | MBEDTLS_ECP_DP_SECP256R1, |
| 1592 | pw, pw_len ) ); |
| 1593 | } |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 1594 | #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 1595 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 1596 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1597 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 1598 | MBEDTLS_CHECK_RETURN_CRITICAL |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 1599 | static int ssl_conf_psk_is_configured( mbedtls_ssl_config const *conf ) |
| 1600 | { |
| 1601 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1602 | if( !mbedtls_svc_key_id_is_null( conf->psk_opaque ) ) |
| 1603 | return( 1 ); |
| 1604 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 1605 | if( conf->psk != NULL ) |
| 1606 | return( 1 ); |
| 1607 | |
| 1608 | return( 0 ); |
| 1609 | } |
| 1610 | |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1611 | static void ssl_conf_remove_psk( mbedtls_ssl_config *conf ) |
| 1612 | { |
| 1613 | /* Remove reference to existing PSK, if any. */ |
| 1614 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1615 | if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) ) |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1616 | { |
| 1617 | /* The maintenance of the PSK key slot is the |
| 1618 | * user's responsibility. */ |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1619 | conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT; |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1620 | } |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1621 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 1622 | if( conf->psk != NULL ) |
| 1623 | { |
| 1624 | mbedtls_platform_zeroize( conf->psk, conf->psk_len ); |
| 1625 | |
| 1626 | mbedtls_free( conf->psk ); |
| 1627 | conf->psk = NULL; |
| 1628 | conf->psk_len = 0; |
| 1629 | } |
| 1630 | |
| 1631 | /* Remove reference to PSK identity, if any. */ |
| 1632 | if( conf->psk_identity != NULL ) |
| 1633 | { |
| 1634 | mbedtls_free( conf->psk_identity ); |
| 1635 | conf->psk_identity = NULL; |
| 1636 | conf->psk_identity_len = 0; |
| 1637 | } |
| 1638 | } |
| 1639 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1640 | /* This function assumes that PSK identity in the SSL config is unset. |
| 1641 | * It checks that the provided identity is well-formed and attempts |
| 1642 | * to make a copy of it in the SSL config. |
| 1643 | * On failure, the PSK identity in the config remains unset. */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 1644 | MBEDTLS_CHECK_RETURN_CRITICAL |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1645 | static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf, |
| 1646 | unsigned char const *psk_identity, |
| 1647 | size_t psk_identity_len ) |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 1648 | { |
Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 1649 | /* Identity len will be encoded on two bytes */ |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1650 | if( psk_identity == NULL || |
| 1651 | ( psk_identity_len >> 16 ) != 0 || |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 1652 | psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN ) |
Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 1653 | { |
| 1654 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1655 | } |
| 1656 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1657 | conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ); |
| 1658 | if( conf->psk_identity == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 1659 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 1660 | |
Manuel Pégourié-Gonnard | 120fdbd | 2015-05-07 17:07:50 +0100 | [diff] [blame] | 1661 | conf->psk_identity_len = psk_identity_len; |
Manuel Pégourié-Gonnard | 120fdbd | 2015-05-07 17:07:50 +0100 | [diff] [blame] | 1662 | memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len ); |
Paul Bakker | 5ad403f | 2013-09-18 21:21:30 +0200 | [diff] [blame] | 1663 | |
| 1664 | return( 0 ); |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 1665 | } |
| 1666 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1667 | int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf, |
| 1668 | const unsigned char *psk, size_t psk_len, |
| 1669 | const unsigned char *psk_identity, size_t psk_identity_len ) |
| 1670 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1671 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 1672 | |
| 1673 | /* We currently only support one PSK, raw or opaque. */ |
| 1674 | if( ssl_conf_psk_is_configured( conf ) ) |
| 1675 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1676 | |
| 1677 | /* Check and set raw PSK */ |
Piotr Nowicki | 9926eaf | 2019-11-20 14:54:36 +0100 | [diff] [blame] | 1678 | if( psk == NULL ) |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1679 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Piotr Nowicki | 9926eaf | 2019-11-20 14:54:36 +0100 | [diff] [blame] | 1680 | if( psk_len == 0 ) |
| 1681 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1682 | if( psk_len > MBEDTLS_PSK_MAX_LEN ) |
| 1683 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1684 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1685 | if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ) |
| 1686 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 1687 | conf->psk_len = psk_len; |
| 1688 | memcpy( conf->psk, psk, conf->psk_len ); |
| 1689 | |
| 1690 | /* Check and set PSK Identity */ |
| 1691 | ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len ); |
| 1692 | if( ret != 0 ) |
| 1693 | ssl_conf_remove_psk( conf ); |
| 1694 | |
| 1695 | return( ret ); |
| 1696 | } |
| 1697 | |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1698 | static void ssl_remove_psk( mbedtls_ssl_context *ssl ) |
| 1699 | { |
| 1700 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1701 | if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) ) |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1702 | { |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 1703 | /* The maintenance of the external PSK key slot is the |
| 1704 | * user's responsibility. */ |
| 1705 | if( ssl->handshake->psk_opaque_is_internal ) |
| 1706 | { |
| 1707 | psa_destroy_key( ssl->handshake->psk_opaque ); |
| 1708 | ssl->handshake->psk_opaque_is_internal = 0; |
| 1709 | } |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1710 | ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT; |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1711 | } |
Neil Armstrong | e952a30 | 2022-05-03 10:22:14 +0200 | [diff] [blame] | 1712 | #else |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1713 | if( ssl->handshake->psk != NULL ) |
| 1714 | { |
| 1715 | mbedtls_platform_zeroize( ssl->handshake->psk, |
| 1716 | ssl->handshake->psk_len ); |
| 1717 | mbedtls_free( ssl->handshake->psk ); |
| 1718 | ssl->handshake->psk_len = 0; |
| 1719 | } |
Neil Armstrong | e952a30 | 2022-05-03 10:22:14 +0200 | [diff] [blame] | 1720 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1721 | } |
| 1722 | |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1723 | int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl, |
| 1724 | const unsigned char *psk, size_t psk_len ) |
| 1725 | { |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 1726 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1727 | psa_key_attributes_t key_attributes = psa_key_attributes_init(); |
Jerry Yu | 5d01c05 | 2022-08-17 10:18:10 +0800 | [diff] [blame] | 1728 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Jerry Yu | 24b8c81 | 2022-08-20 19:06:56 +0800 | [diff] [blame] | 1729 | psa_algorithm_t alg = PSA_ALG_NONE; |
Jerry Yu | 5d01c05 | 2022-08-17 10:18:10 +0800 | [diff] [blame] | 1730 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 1731 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 1732 | |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1733 | if( psk == NULL || ssl->handshake == NULL ) |
| 1734 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1735 | |
| 1736 | if( psk_len > MBEDTLS_PSK_MAX_LEN ) |
| 1737 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1738 | |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1739 | ssl_remove_psk( ssl ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1740 | |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 1741 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Jerry Yu | ccc68a4 | 2022-07-26 16:39:20 +0800 | [diff] [blame] | 1742 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 1743 | if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 ) |
| 1744 | { |
Jerry Yu | 6cf6b47 | 2022-08-16 14:50:28 +0800 | [diff] [blame] | 1745 | if( ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384 ) |
Jerry Yu | ccc68a4 | 2022-07-26 16:39:20 +0800 | [diff] [blame] | 1746 | alg = PSA_ALG_TLS12_PSK_TO_MS( PSA_ALG_SHA_384 ); |
| 1747 | else |
| 1748 | alg = PSA_ALG_TLS12_PSK_TO_MS( PSA_ALG_SHA_256 ); |
| 1749 | psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE ); |
| 1750 | } |
| 1751 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 1752 | |
Jerry Yu | 568ec25 | 2022-07-22 21:27:34 +0800 | [diff] [blame] | 1753 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | ccc68a4 | 2022-07-26 16:39:20 +0800 | [diff] [blame] | 1754 | if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 ) |
| 1755 | { |
| 1756 | alg = PSA_ALG_HKDF_EXTRACT( PSA_ALG_ANY_HASH ); |
| 1757 | psa_set_key_usage_flags( &key_attributes, |
| 1758 | PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT ); |
| 1759 | } |
| 1760 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 1761 | |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 1762 | psa_set_key_algorithm( &key_attributes, alg ); |
| 1763 | psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE ); |
| 1764 | |
| 1765 | status = psa_import_key( &key_attributes, psk, psk_len, &key ); |
| 1766 | if( status != PSA_SUCCESS ) |
| 1767 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 1768 | |
| 1769 | /* Allow calling psa_destroy_key() on psk remove */ |
| 1770 | ssl->handshake->psk_opaque_is_internal = 1; |
| 1771 | return mbedtls_ssl_set_hs_psk_opaque( ssl, key ); |
| 1772 | #else |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 1773 | if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 1774 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1775 | |
| 1776 | ssl->handshake->psk_len = psk_len; |
| 1777 | memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len ); |
| 1778 | |
| 1779 | return( 0 ); |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 1780 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1781 | } |
| 1782 | |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1783 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1784 | int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf, |
Andrzej Kurek | 03e0146 | 2022-01-03 12:53:24 +0100 | [diff] [blame] | 1785 | mbedtls_svc_key_id_t psk, |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1786 | const unsigned char *psk_identity, |
| 1787 | size_t psk_identity_len ) |
| 1788 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1789 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 1790 | |
| 1791 | /* We currently only support one PSK, raw or opaque. */ |
| 1792 | if( ssl_conf_psk_is_configured( conf ) ) |
| 1793 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1794 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1795 | /* Check and set opaque PSK */ |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1796 | if( mbedtls_svc_key_id_is_null( psk ) ) |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1797 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1798 | conf->psk_opaque = psk; |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1799 | |
| 1800 | /* Check and set PSK Identity */ |
| 1801 | ret = ssl_conf_set_psk_identity( conf, psk_identity, |
| 1802 | psk_identity_len ); |
| 1803 | if( ret != 0 ) |
| 1804 | ssl_conf_remove_psk( conf ); |
| 1805 | |
| 1806 | return( ret ); |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1807 | } |
| 1808 | |
Przemyslaw Stekiel | 6928a51 | 2022-02-03 13:50:35 +0100 | [diff] [blame] | 1809 | int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl, |
| 1810 | mbedtls_svc_key_id_t psk ) |
| 1811 | { |
| 1812 | if( ( mbedtls_svc_key_id_is_null( psk ) ) || |
| 1813 | ( ssl->handshake == NULL ) ) |
| 1814 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1815 | |
| 1816 | ssl_remove_psk( ssl ); |
| 1817 | ssl->handshake->psk_opaque = psk; |
| 1818 | return( 0 ); |
| 1819 | } |
| 1820 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 1821 | |
Jerry Yu | 8897c07 | 2022-08-12 13:56:53 +0800 | [diff] [blame] | 1822 | #if defined(MBEDTLS_SSL_SRV_C) |
Przemyslaw Stekiel | 6928a51 | 2022-02-03 13:50:35 +0100 | [diff] [blame] | 1823 | void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf, |
| 1824 | int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *, |
| 1825 | size_t), |
| 1826 | void *p_psk ) |
| 1827 | { |
| 1828 | conf->f_psk = f_psk; |
| 1829 | conf->p_psk = p_psk; |
| 1830 | } |
Jerry Yu | 8897c07 | 2022-08-12 13:56:53 +0800 | [diff] [blame] | 1831 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 1832 | |
Przemyslaw Stekiel | 6928a51 | 2022-02-03 13:50:35 +0100 | [diff] [blame] | 1833 | #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ |
| 1834 | |
| 1835 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 301711e | 2022-04-26 16:57:05 +0200 | [diff] [blame] | 1836 | static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode( |
| 1837 | psa_algorithm_t alg ) |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 1838 | { |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 1839 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1840 | if( alg == PSA_ALG_CBC_NO_PADDING ) |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 1841 | return( MBEDTLS_SSL_MODE_CBC ); |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 1842 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1843 | if( PSA_ALG_IS_AEAD( alg ) ) |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 1844 | return( MBEDTLS_SSL_MODE_AEAD ); |
Gilles Peskine | 301711e | 2022-04-26 16:57:05 +0200 | [diff] [blame] | 1845 | return( MBEDTLS_SSL_MODE_STREAM ); |
| 1846 | } |
| 1847 | |
| 1848 | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
| 1849 | |
| 1850 | static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode( |
| 1851 | mbedtls_cipher_mode_t mode ) |
| 1852 | { |
| 1853 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
| 1854 | if( mode == MBEDTLS_MODE_CBC ) |
| 1855 | return( MBEDTLS_SSL_MODE_CBC ); |
| 1856 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
| 1857 | |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 1858 | #if defined(MBEDTLS_GCM_C) || \ |
| 1859 | defined(MBEDTLS_CCM_C) || \ |
| 1860 | defined(MBEDTLS_CHACHAPOLY_C) |
| 1861 | if( mode == MBEDTLS_MODE_GCM || |
| 1862 | mode == MBEDTLS_MODE_CCM || |
| 1863 | mode == MBEDTLS_MODE_CHACHAPOLY ) |
| 1864 | return( MBEDTLS_SSL_MODE_AEAD ); |
| 1865 | #endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */ |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 1866 | |
| 1867 | return( MBEDTLS_SSL_MODE_STREAM ); |
| 1868 | } |
Gilles Peskine | 301711e | 2022-04-26 16:57:05 +0200 | [diff] [blame] | 1869 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 1870 | |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 1871 | static mbedtls_ssl_mode_t mbedtls_ssl_get_actual_mode( |
| 1872 | mbedtls_ssl_mode_t base_mode, |
| 1873 | int encrypt_then_mac ) |
| 1874 | { |
| 1875 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
| 1876 | if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED && |
| 1877 | base_mode == MBEDTLS_SSL_MODE_CBC ) |
| 1878 | { |
| 1879 | return( MBEDTLS_SSL_MODE_CBC_ETM ); |
| 1880 | } |
| 1881 | #else |
| 1882 | (void) encrypt_then_mac; |
| 1883 | #endif |
| 1884 | return( base_mode ); |
| 1885 | } |
| 1886 | |
Neil Armstrong | ab555e0 | 2022-04-04 11:07:59 +0200 | [diff] [blame] | 1887 | mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_transform( |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1888 | const mbedtls_ssl_transform *transform ) |
| 1889 | { |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 1890 | mbedtls_ssl_mode_t base_mode = mbedtls_ssl_get_base_mode( |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1891 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 1892 | transform->psa_alg |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1893 | #else |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 1894 | mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) |
| 1895 | #endif |
| 1896 | ); |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1897 | |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 1898 | int encrypt_then_mac = 0; |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 1899 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 1900 | encrypt_then_mac = transform->encrypt_then_mac; |
| 1901 | #endif |
| 1902 | return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) ); |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1903 | } |
| 1904 | |
Neil Armstrong | ab555e0 | 2022-04-04 11:07:59 +0200 | [diff] [blame] | 1905 | mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_ciphersuite( |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 1906 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1907 | int encrypt_then_mac, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 1908 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1909 | const mbedtls_ssl_ciphersuite_t *suite ) |
| 1910 | { |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 1911 | mbedtls_ssl_mode_t base_mode = MBEDTLS_SSL_MODE_STREAM; |
| 1912 | |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1913 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1914 | psa_status_t status; |
| 1915 | psa_algorithm_t alg; |
| 1916 | psa_key_type_t type; |
| 1917 | size_t size; |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1918 | status = mbedtls_ssl_cipher_to_psa( suite->cipher, 0, &alg, &type, &size ); |
| 1919 | if( status == PSA_SUCCESS ) |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 1920 | base_mode = mbedtls_ssl_get_base_mode( alg ); |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1921 | #else |
| 1922 | const mbedtls_cipher_info_t *cipher = |
| 1923 | mbedtls_cipher_info_from_type( suite->cipher ); |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1924 | if( cipher != NULL ) |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 1925 | { |
| 1926 | base_mode = |
| 1927 | mbedtls_ssl_get_base_mode( |
| 1928 | mbedtls_cipher_info_get_mode( cipher ) ); |
| 1929 | } |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1930 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 1931 | |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 1932 | #if !defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
| 1933 | int encrypt_then_mac = 0; |
| 1934 | #endif |
| 1935 | return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) ); |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1936 | } |
| 1937 | |
Neil Armstrong | 8395d7a | 2022-05-18 11:44:56 +0200 | [diff] [blame] | 1938 | #if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 1939 | |
| 1940 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 1941 | /* Serialization of TLS 1.3 sessions: |
| 1942 | * |
| 1943 | * struct { |
Xiaokang Qian | 126bf8e | 2022-10-13 02:22:40 +0000 | [diff] [blame] | 1944 | * opaque hostname<0..2^16-1>; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 1945 | * uint64 ticket_received; |
| 1946 | * uint32 ticket_lifetime; |
Jerry Yu | 3419107 | 2022-08-18 10:32:09 +0800 | [diff] [blame] | 1947 | * opaque ticket<1..2^16-1>; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 1948 | * } ClientOnlyData; |
| 1949 | * |
| 1950 | * struct { |
| 1951 | * uint8 endpoint; |
| 1952 | * uint8 ciphersuite[2]; |
| 1953 | * uint32 ticket_age_add; |
| 1954 | * uint8 ticket_flags; |
| 1955 | * opaque resumption_key<0..255>; |
| 1956 | * select ( endpoint ) { |
| 1957 | * case client: ClientOnlyData; |
| 1958 | * case server: uint64 start_time; |
| 1959 | * }; |
| 1960 | * } serialized_session_tls13; |
| 1961 | * |
| 1962 | */ |
| 1963 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 1964 | MBEDTLS_CHECK_RETURN_CRITICAL |
| 1965 | static int ssl_tls13_session_save( const mbedtls_ssl_session *session, |
| 1966 | unsigned char *buf, |
| 1967 | size_t buf_len, |
| 1968 | size_t *olen ) |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 1969 | { |
| 1970 | unsigned char *p = buf; |
Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 1971 | #if defined(MBEDTLS_SSL_CLI_C) && \ |
| 1972 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 1973 | size_t hostname_len = ( session->hostname == NULL ) ? |
Xiaokang Qian | ed0620c | 2022-10-12 06:58:13 +0000 | [diff] [blame] | 1974 | 0 : strlen( session->hostname ) + 1; |
Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 1975 | #endif |
Jerry Yu | bc7c1a4 | 2022-07-21 22:57:37 +0800 | [diff] [blame] | 1976 | size_t needed = 1 /* endpoint */ |
| 1977 | + 2 /* ciphersuite */ |
| 1978 | + 4 /* ticket_age_add */ |
Jerry Yu | 3419107 | 2022-08-18 10:32:09 +0800 | [diff] [blame] | 1979 | + 1 /* ticket_flags */ |
| 1980 | + 1; /* resumption_key length */ |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 1981 | *olen = 0; |
Jerry Yu | 3419107 | 2022-08-18 10:32:09 +0800 | [diff] [blame] | 1982 | |
| 1983 | if( session->resumption_key_len > MBEDTLS_SSL_TLS1_3_TICKET_RESUMPTION_KEY_LEN ) |
| 1984 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1985 | needed += session->resumption_key_len; /* resumption_key */ |
| 1986 | |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 1987 | #if defined(MBEDTLS_HAVE_TIME) |
| 1988 | needed += 8; /* start_time or ticket_received */ |
| 1989 | #endif |
| 1990 | |
| 1991 | #if defined(MBEDTLS_SSL_CLI_C) |
| 1992 | if( session->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 1993 | { |
Xiaokang Qian | 126bf8e | 2022-10-13 02:22:40 +0000 | [diff] [blame] | 1994 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Xiaokang Qian | bc663a0 | 2022-10-09 11:14:39 +0000 | [diff] [blame] | 1995 | needed += 2 /* hostname_len */ |
Xiaokang Qian | 2f9efd3 | 2022-10-10 11:24:08 +0000 | [diff] [blame] | 1996 | + hostname_len; /* hostname */ |
Xiaokang Qian | 281fd1b | 2022-09-20 11:35:41 +0000 | [diff] [blame] | 1997 | #endif |
| 1998 | |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 1999 | needed += 4 /* ticket_lifetime */ |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 2000 | + 2; /* ticket_len */ |
Jerry Yu | 3419107 | 2022-08-18 10:32:09 +0800 | [diff] [blame] | 2001 | |
| 2002 | /* Check size_t overflow */ |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 2003 | if( session->ticket_len > SIZE_MAX - needed ) |
| 2004 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jerry Yu | 3419107 | 2022-08-18 10:32:09 +0800 | [diff] [blame] | 2005 | |
Jerry Yu | e28d974 | 2022-08-18 15:44:03 +0800 | [diff] [blame] | 2006 | needed += session->ticket_len; /* ticket */ |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2007 | } |
| 2008 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 2009 | |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 2010 | *olen = needed; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2011 | if( needed > buf_len ) |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 2012 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2013 | |
| 2014 | p[0] = session->endpoint; |
| 2015 | MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 1 ); |
| 2016 | MBEDTLS_PUT_UINT32_BE( session->ticket_age_add, p, 3 ); |
| 2017 | p[7] = session->ticket_flags; |
| 2018 | |
| 2019 | /* save resumption_key */ |
Jerry Yu | bc7c1a4 | 2022-07-21 22:57:37 +0800 | [diff] [blame] | 2020 | p[8] = session->resumption_key_len; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2021 | p += 9; |
Jerry Yu | bc7c1a4 | 2022-07-21 22:57:37 +0800 | [diff] [blame] | 2022 | memcpy( p, session->resumption_key, session->resumption_key_len ); |
| 2023 | p += session->resumption_key_len; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2024 | |
| 2025 | #if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C) |
| 2026 | if( session->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 2027 | { |
| 2028 | MBEDTLS_PUT_UINT64_BE( (uint64_t) session->start, p, 0 ); |
| 2029 | p += 8; |
| 2030 | } |
| 2031 | #endif /* MBEDTLS_HAVE_TIME */ |
| 2032 | |
| 2033 | #if defined(MBEDTLS_SSL_CLI_C) |
| 2034 | if( session->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 2035 | { |
Xiaokang Qian | ed0620c | 2022-10-12 06:58:13 +0000 | [diff] [blame] | 2036 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 2037 | MBEDTLS_PUT_UINT16_BE( hostname_len, p, 0 ); |
| 2038 | p += 2; |
Xiaokang Qian | 126bf8e | 2022-10-13 02:22:40 +0000 | [diff] [blame] | 2039 | if( hostname_len > 0 ) |
Xiaokang Qian | ed0620c | 2022-10-12 06:58:13 +0000 | [diff] [blame] | 2040 | { |
| 2041 | /* save host name */ |
| 2042 | memcpy( p, session->hostname, hostname_len ); |
| 2043 | p += hostname_len; |
| 2044 | } |
| 2045 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
| 2046 | |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2047 | #if defined(MBEDTLS_HAVE_TIME) |
| 2048 | MBEDTLS_PUT_UINT64_BE( (uint64_t) session->ticket_received, p, 0 ); |
| 2049 | p += 8; |
| 2050 | #endif |
| 2051 | MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 ); |
| 2052 | p += 4; |
| 2053 | |
| 2054 | MBEDTLS_PUT_UINT16_BE( session->ticket_len, p, 0 ); |
| 2055 | p += 2; |
Jerry Yu | 3419107 | 2022-08-18 10:32:09 +0800 | [diff] [blame] | 2056 | |
| 2057 | if( session->ticket != NULL && session->ticket_len > 0 ) |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2058 | { |
| 2059 | memcpy( p, session->ticket, session->ticket_len ); |
| 2060 | p += session->ticket_len; |
| 2061 | } |
| 2062 | } |
| 2063 | #endif /* MBEDTLS_SSL_CLI_C */ |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 2064 | return( 0 ); |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2065 | } |
| 2066 | |
| 2067 | MBEDTLS_CHECK_RETURN_CRITICAL |
| 2068 | static int ssl_tls13_session_load( mbedtls_ssl_session *session, |
| 2069 | const unsigned char *buf, |
| 2070 | size_t len ) |
| 2071 | { |
| 2072 | const unsigned char *p = buf; |
| 2073 | const unsigned char *end = buf + len; |
| 2074 | |
| 2075 | if( end - p < 9 ) |
| 2076 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2077 | session->endpoint = p[0]; |
| 2078 | session->ciphersuite = MBEDTLS_GET_UINT16_BE( p, 1 ); |
| 2079 | session->ticket_age_add = MBEDTLS_GET_UINT32_BE( p, 3 ); |
| 2080 | session->ticket_flags = p[7]; |
| 2081 | |
| 2082 | /* load resumption_key */ |
Jerry Yu | bc7c1a4 | 2022-07-21 22:57:37 +0800 | [diff] [blame] | 2083 | session->resumption_key_len = p[8]; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2084 | p += 9; |
| 2085 | |
Jerry Yu | bc7c1a4 | 2022-07-21 22:57:37 +0800 | [diff] [blame] | 2086 | if( end - p < session->resumption_key_len ) |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2087 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2088 | |
Jerry Yu | bc7c1a4 | 2022-07-21 22:57:37 +0800 | [diff] [blame] | 2089 | if( sizeof( session->resumption_key ) < session->resumption_key_len ) |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2090 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jerry Yu | bc7c1a4 | 2022-07-21 22:57:37 +0800 | [diff] [blame] | 2091 | memcpy( session->resumption_key, p, session->resumption_key_len ); |
| 2092 | p += session->resumption_key_len; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2093 | |
| 2094 | #if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C) |
| 2095 | if( session->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 2096 | { |
| 2097 | if( end - p < 8 ) |
| 2098 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2099 | session->start = MBEDTLS_GET_UINT64_BE( p, 0 ); |
| 2100 | p += 8; |
| 2101 | } |
| 2102 | #endif /* MBEDTLS_HAVE_TIME */ |
| 2103 | |
| 2104 | #if defined(MBEDTLS_SSL_CLI_C) |
| 2105 | if( session->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 2106 | { |
Xiaokang Qian | ed0620c | 2022-10-12 06:58:13 +0000 | [diff] [blame] | 2107 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \ |
| 2108 | defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 2109 | size_t hostname_len; |
| 2110 | /* load host name */ |
| 2111 | if( end - p < 2 ) |
| 2112 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2113 | hostname_len = MBEDTLS_GET_UINT16_BE( p, 0 ); |
| 2114 | p += 2; |
| 2115 | |
| 2116 | if( end - p < ( long int )hostname_len ) |
| 2117 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2118 | if( hostname_len > 0 ) |
| 2119 | { |
| 2120 | session->hostname = mbedtls_calloc( 1, hostname_len ); |
| 2121 | if( session->hostname == NULL ) |
| 2122 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 2123 | memcpy( session->hostname, p, hostname_len ); |
| 2124 | p += hostname_len; |
| 2125 | } |
| 2126 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION && |
| 2127 | MBEDTLS_SSL_SESSION_TICKETS */ |
| 2128 | |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2129 | #if defined(MBEDTLS_HAVE_TIME) |
| 2130 | if( end - p < 8 ) |
| 2131 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2132 | session->ticket_received = MBEDTLS_GET_UINT64_BE( p, 0 ); |
| 2133 | p += 8; |
| 2134 | #endif |
| 2135 | if( end - p < 4 ) |
| 2136 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2137 | session->ticket_lifetime = MBEDTLS_GET_UINT32_BE( p, 0 ); |
| 2138 | p += 4; |
| 2139 | |
| 2140 | if( end - p < 2 ) |
| 2141 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2142 | session->ticket_len = MBEDTLS_GET_UINT16_BE( p, 0 ); |
| 2143 | p += 2; |
| 2144 | |
| 2145 | if( end - p < ( long int )session->ticket_len ) |
| 2146 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2147 | if( session->ticket_len > 0 ) |
| 2148 | { |
| 2149 | session->ticket = mbedtls_calloc( 1, session->ticket_len ); |
| 2150 | if( session->ticket == NULL ) |
| 2151 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 2152 | memcpy( session->ticket, p, session->ticket_len ); |
| 2153 | p += session->ticket_len; |
| 2154 | } |
| 2155 | } |
| 2156 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 2157 | |
| 2158 | return( 0 ); |
| 2159 | |
| 2160 | } |
| 2161 | #else /* MBEDTLS_SSL_SESSION_TICKETS */ |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 2162 | MBEDTLS_CHECK_RETURN_CRITICAL |
| 2163 | static int ssl_tls13_session_save( const mbedtls_ssl_session *session, |
| 2164 | unsigned char *buf, |
| 2165 | size_t buf_len, |
| 2166 | size_t *olen ) |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 2167 | { |
| 2168 | ((void) session); |
| 2169 | ((void) buf); |
| 2170 | ((void) buf_len); |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 2171 | *olen = 0; |
| 2172 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 2173 | } |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2174 | |
| 2175 | static int ssl_tls13_session_load( const mbedtls_ssl_session *session, |
| 2176 | unsigned char *buf, |
| 2177 | size_t buf_len ) |
| 2178 | { |
| 2179 | ((void) session); |
| 2180 | ((void) buf); |
| 2181 | ((void) buf_len); |
| 2182 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 2183 | } |
| 2184 | #endif /* !MBEDTLS_SSL_SESSION_TICKETS */ |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 2185 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 2186 | |
Przemyslaw Stekiel | f57b456 | 2022-01-25 00:04:18 +0100 | [diff] [blame] | 2187 | psa_status_t mbedtls_ssl_cipher_to_psa( mbedtls_cipher_type_t mbedtls_cipher_type, |
Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2188 | size_t taglen, |
| 2189 | psa_algorithm_t *alg, |
| 2190 | psa_key_type_t *key_type, |
| 2191 | size_t *key_size ) |
| 2192 | { |
| 2193 | switch ( mbedtls_cipher_type ) |
| 2194 | { |
| 2195 | case MBEDTLS_CIPHER_AES_128_CBC: |
| 2196 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 2197 | *key_type = PSA_KEY_TYPE_AES; |
| 2198 | *key_size = 128; |
| 2199 | break; |
| 2200 | case MBEDTLS_CIPHER_AES_128_CCM: |
| 2201 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 2202 | *key_type = PSA_KEY_TYPE_AES; |
| 2203 | *key_size = 128; |
| 2204 | break; |
| 2205 | case MBEDTLS_CIPHER_AES_128_GCM: |
| 2206 | *alg = PSA_ALG_GCM; |
| 2207 | *key_type = PSA_KEY_TYPE_AES; |
| 2208 | *key_size = 128; |
| 2209 | break; |
| 2210 | case MBEDTLS_CIPHER_AES_192_CCM: |
| 2211 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 2212 | *key_type = PSA_KEY_TYPE_AES; |
| 2213 | *key_size = 192; |
| 2214 | break; |
| 2215 | case MBEDTLS_CIPHER_AES_192_GCM: |
| 2216 | *alg = PSA_ALG_GCM; |
| 2217 | *key_type = PSA_KEY_TYPE_AES; |
| 2218 | *key_size = 192; |
| 2219 | break; |
| 2220 | case MBEDTLS_CIPHER_AES_256_CBC: |
| 2221 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 2222 | *key_type = PSA_KEY_TYPE_AES; |
| 2223 | *key_size = 256; |
| 2224 | break; |
| 2225 | case MBEDTLS_CIPHER_AES_256_CCM: |
| 2226 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 2227 | *key_type = PSA_KEY_TYPE_AES; |
| 2228 | *key_size = 256; |
| 2229 | break; |
| 2230 | case MBEDTLS_CIPHER_AES_256_GCM: |
| 2231 | *alg = PSA_ALG_GCM; |
| 2232 | *key_type = PSA_KEY_TYPE_AES; |
| 2233 | *key_size = 256; |
| 2234 | break; |
| 2235 | case MBEDTLS_CIPHER_ARIA_128_CBC: |
| 2236 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 2237 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2238 | *key_size = 128; |
| 2239 | break; |
| 2240 | case MBEDTLS_CIPHER_ARIA_128_CCM: |
| 2241 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 2242 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2243 | *key_size = 128; |
| 2244 | break; |
| 2245 | case MBEDTLS_CIPHER_ARIA_128_GCM: |
| 2246 | *alg = PSA_ALG_GCM; |
| 2247 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2248 | *key_size = 128; |
| 2249 | break; |
| 2250 | case MBEDTLS_CIPHER_ARIA_192_CCM: |
| 2251 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 2252 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2253 | *key_size = 192; |
| 2254 | break; |
| 2255 | case MBEDTLS_CIPHER_ARIA_192_GCM: |
| 2256 | *alg = PSA_ALG_GCM; |
| 2257 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2258 | *key_size = 192; |
| 2259 | break; |
| 2260 | case MBEDTLS_CIPHER_ARIA_256_CBC: |
| 2261 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 2262 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2263 | *key_size = 256; |
| 2264 | break; |
| 2265 | case MBEDTLS_CIPHER_ARIA_256_CCM: |
| 2266 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 2267 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2268 | *key_size = 256; |
| 2269 | break; |
| 2270 | case MBEDTLS_CIPHER_ARIA_256_GCM: |
| 2271 | *alg = PSA_ALG_GCM; |
| 2272 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2273 | *key_size = 256; |
| 2274 | break; |
| 2275 | case MBEDTLS_CIPHER_CAMELLIA_128_CBC: |
| 2276 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 2277 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2278 | *key_size = 128; |
| 2279 | break; |
| 2280 | case MBEDTLS_CIPHER_CAMELLIA_128_CCM: |
| 2281 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 2282 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2283 | *key_size = 128; |
| 2284 | break; |
| 2285 | case MBEDTLS_CIPHER_CAMELLIA_128_GCM: |
| 2286 | *alg = PSA_ALG_GCM; |
| 2287 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2288 | *key_size = 128; |
| 2289 | break; |
| 2290 | case MBEDTLS_CIPHER_CAMELLIA_192_CCM: |
| 2291 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 2292 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2293 | *key_size = 192; |
| 2294 | break; |
| 2295 | case MBEDTLS_CIPHER_CAMELLIA_192_GCM: |
| 2296 | *alg = PSA_ALG_GCM; |
| 2297 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2298 | *key_size = 192; |
| 2299 | break; |
| 2300 | case MBEDTLS_CIPHER_CAMELLIA_256_CBC: |
| 2301 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 2302 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2303 | *key_size = 256; |
| 2304 | break; |
| 2305 | case MBEDTLS_CIPHER_CAMELLIA_256_CCM: |
| 2306 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 2307 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2308 | *key_size = 256; |
| 2309 | break; |
| 2310 | case MBEDTLS_CIPHER_CAMELLIA_256_GCM: |
| 2311 | *alg = PSA_ALG_GCM; |
| 2312 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2313 | *key_size = 256; |
| 2314 | break; |
| 2315 | case MBEDTLS_CIPHER_CHACHA20_POLY1305: |
| 2316 | *alg = PSA_ALG_CHACHA20_POLY1305; |
| 2317 | *key_type = PSA_KEY_TYPE_CHACHA20; |
| 2318 | *key_size = 256; |
| 2319 | break; |
| 2320 | case MBEDTLS_CIPHER_NULL: |
| 2321 | *alg = MBEDTLS_SSL_NULL_CIPHER; |
| 2322 | *key_type = 0; |
| 2323 | *key_size = 0; |
| 2324 | break; |
| 2325 | default: |
| 2326 | return PSA_ERROR_NOT_SUPPORTED; |
| 2327 | } |
| 2328 | |
| 2329 | return PSA_SUCCESS; |
| 2330 | } |
Neil Armstrong | 8395d7a | 2022-05-18 11:44:56 +0200 | [diff] [blame] | 2331 | #endif /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2332 | |
Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 2333 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 2334 | int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf, |
| 2335 | const unsigned char *dhm_P, size_t P_len, |
| 2336 | const unsigned char *dhm_G, size_t G_len ) |
| 2337 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2338 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 2339 | |
Glenn Strauss | cee1129 | 2021-12-20 01:43:17 -0500 | [diff] [blame] | 2340 | mbedtls_mpi_free( &conf->dhm_P ); |
| 2341 | mbedtls_mpi_free( &conf->dhm_G ); |
| 2342 | |
Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 2343 | if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 || |
| 2344 | ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 ) |
| 2345 | { |
| 2346 | mbedtls_mpi_free( &conf->dhm_P ); |
| 2347 | mbedtls_mpi_free( &conf->dhm_G ); |
| 2348 | return( ret ); |
| 2349 | } |
| 2350 | |
| 2351 | return( 0 ); |
| 2352 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2353 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2354 | int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx ) |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 2355 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2356 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 2357 | |
Glenn Strauss | cee1129 | 2021-12-20 01:43:17 -0500 | [diff] [blame] | 2358 | mbedtls_mpi_free( &conf->dhm_P ); |
| 2359 | mbedtls_mpi_free( &conf->dhm_G ); |
| 2360 | |
Gilles Peskine | e570248 | 2021-06-11 21:59:08 +0200 | [diff] [blame] | 2361 | if( ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_P, |
| 2362 | &conf->dhm_P ) ) != 0 || |
| 2363 | ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_G, |
| 2364 | &conf->dhm_G ) ) != 0 ) |
Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 2365 | { |
| 2366 | mbedtls_mpi_free( &conf->dhm_P ); |
| 2367 | mbedtls_mpi_free( &conf->dhm_G ); |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 2368 | return( ret ); |
Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 2369 | } |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 2370 | |
| 2371 | return( 0 ); |
| 2372 | } |
Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 2373 | #endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */ |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 2374 | |
Manuel Pégourié-Gonnard | bd990d6 | 2015-06-11 14:49:42 +0200 | [diff] [blame] | 2375 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C) |
| 2376 | /* |
| 2377 | * Set the minimum length for Diffie-Hellman parameters |
| 2378 | */ |
| 2379 | void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf, |
| 2380 | unsigned int bitlen ) |
| 2381 | { |
| 2382 | conf->dhm_min_bitlen = bitlen; |
| 2383 | } |
| 2384 | #endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */ |
| 2385 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 2386 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Jerry Yu | 7ddc38c | 2022-01-19 11:08:05 +0800 | [diff] [blame] | 2387 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) && defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Manuel Pégourié-Gonnard | 36a8b57 | 2015-06-17 12:43:26 +0200 | [diff] [blame] | 2388 | /* |
| 2389 | * Set allowed/preferred hashes for handshake signatures |
| 2390 | */ |
| 2391 | void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf, |
| 2392 | const int *hashes ) |
| 2393 | { |
| 2394 | conf->sig_hashes = hashes; |
| 2395 | } |
Jerry Yu | 7ddc38c | 2022-01-19 11:08:05 +0800 | [diff] [blame] | 2396 | #endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */ |
Hanno Becker | 1cd6e00 | 2021-08-10 13:27:10 +0100 | [diff] [blame] | 2397 | |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 2398 | /* Configure allowed signature algorithms for handshake */ |
Hanno Becker | 1cd6e00 | 2021-08-10 13:27:10 +0100 | [diff] [blame] | 2399 | void mbedtls_ssl_conf_sig_algs( mbedtls_ssl_config *conf, |
| 2400 | const uint16_t* sig_algs ) |
| 2401 | { |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 2402 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 2403 | conf->sig_hashes = NULL; |
| 2404 | #endif /* !MBEDTLS_DEPRECATED_REMOVED */ |
| 2405 | conf->sig_algs = sig_algs; |
Hanno Becker | 1cd6e00 | 2021-08-10 13:27:10 +0100 | [diff] [blame] | 2406 | } |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 2407 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
Manuel Pégourié-Gonnard | 36a8b57 | 2015-06-17 12:43:26 +0200 | [diff] [blame] | 2408 | |
Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 2409 | #if defined(MBEDTLS_ECP_C) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 2410 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 2411 | /* |
| 2412 | * Set the allowed elliptic curves |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 2413 | * |
| 2414 | * mbedtls_ssl_setup() takes the provided list |
| 2415 | * and translates it to a list of IANA TLS group identifiers, |
| 2416 | * stored in ssl->handshake->group_list. |
| 2417 | * |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 2418 | */ |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2419 | void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2420 | const mbedtls_ecp_group_id *curve_list ) |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 2421 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2422 | conf->curve_list = curve_list; |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 2423 | conf->group_list = NULL; |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 2424 | } |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 2425 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2426 | #endif /* MBEDTLS_ECP_C */ |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 2427 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 2428 | /* |
| 2429 | * Set the allowed groups |
| 2430 | */ |
| 2431 | void mbedtls_ssl_conf_groups( mbedtls_ssl_config *conf, |
| 2432 | const uint16_t *group_list ) |
| 2433 | { |
| 2434 | #if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 2435 | conf->curve_list = NULL; |
| 2436 | #endif |
| 2437 | conf->group_list = group_list; |
| 2438 | } |
| 2439 | |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 2440 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2441 | int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2442 | { |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2443 | /* Initialize to suppress unnecessary compiler warning */ |
| 2444 | size_t hostname_len = 0; |
| 2445 | |
| 2446 | /* Check if new hostname is valid before |
| 2447 | * making any change to current one */ |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2448 | if( hostname != NULL ) |
| 2449 | { |
| 2450 | hostname_len = strlen( hostname ); |
| 2451 | |
| 2452 | if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN ) |
| 2453 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2454 | } |
| 2455 | |
| 2456 | /* Now it's clear that we will overwrite the old hostname, |
| 2457 | * so we can free it safely */ |
| 2458 | |
| 2459 | if( ssl->hostname != NULL ) |
| 2460 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 2461 | mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) ); |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2462 | mbedtls_free( ssl->hostname ); |
| 2463 | } |
| 2464 | |
| 2465 | /* Passing NULL as hostname shall clear the old one */ |
Manuel Pégourié-Gonnard | ba26c24 | 2015-05-06 10:47:06 +0100 | [diff] [blame] | 2466 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2467 | if( hostname == NULL ) |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2468 | { |
| 2469 | ssl->hostname = NULL; |
| 2470 | } |
| 2471 | else |
| 2472 | { |
| 2473 | ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 ); |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2474 | if( ssl->hostname == NULL ) |
| 2475 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Paul Bakker | 75c1a6f | 2013-08-19 14:25:29 +0200 | [diff] [blame] | 2476 | |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2477 | memcpy( ssl->hostname, hostname, hostname_len ); |
Paul Bakker | 75c1a6f | 2013-08-19 14:25:29 +0200 | [diff] [blame] | 2478 | |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2479 | ssl->hostname[hostname_len] = '\0'; |
| 2480 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2481 | |
| 2482 | return( 0 ); |
| 2483 | } |
Hanno Becker | 1a9a51c | 2017-04-07 13:02:16 +0100 | [diff] [blame] | 2484 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2485 | |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 2486 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2487 | void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2488 | int (*f_sni)(void *, mbedtls_ssl_context *, |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 2489 | const unsigned char *, size_t), |
| 2490 | void *p_sni ) |
| 2491 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2492 | conf->f_sni = f_sni; |
| 2493 | conf->p_sni = p_sni; |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 2494 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2495 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 2496 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2497 | #if defined(MBEDTLS_SSL_ALPN) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2498 | int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos ) |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 2499 | { |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 2500 | size_t cur_len, tot_len; |
| 2501 | const char **p; |
| 2502 | |
| 2503 | /* |
Brian J Murray | 1903fb3 | 2016-11-06 04:45:15 -0800 | [diff] [blame] | 2504 | * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings |
| 2505 | * MUST NOT be truncated." |
| 2506 | * We check lengths now rather than later. |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 2507 | */ |
| 2508 | tot_len = 0; |
| 2509 | for( p = protos; *p != NULL; p++ ) |
| 2510 | { |
| 2511 | cur_len = strlen( *p ); |
| 2512 | tot_len += cur_len; |
| 2513 | |
Ronald Cron | 8216dd3 | 2020-04-23 16:41:44 +0200 | [diff] [blame] | 2514 | if( ( cur_len == 0 ) || |
| 2515 | ( cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN ) || |
| 2516 | ( tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN ) ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2517 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 2518 | } |
| 2519 | |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2520 | conf->alpn_list = protos; |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 2521 | |
| 2522 | return( 0 ); |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 2523 | } |
| 2524 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2525 | const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 2526 | { |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 2527 | return( ssl->alpn_chosen ); |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 2528 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2529 | #endif /* MBEDTLS_SSL_ALPN */ |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 2530 | |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2531 | #if defined(MBEDTLS_SSL_DTLS_SRTP) |
Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 2532 | void mbedtls_ssl_conf_srtp_mki_value_supported( mbedtls_ssl_config *conf, |
| 2533 | int support_mki_value ) |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 2534 | { |
| 2535 | conf->dtls_srtp_mki_support = support_mki_value; |
| 2536 | } |
| 2537 | |
Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 2538 | int mbedtls_ssl_dtls_srtp_set_mki_value( mbedtls_ssl_context *ssl, |
| 2539 | unsigned char *mki_value, |
Johan Pascal | f6417ec | 2020-09-22 15:15:19 +0200 | [diff] [blame] | 2540 | uint16_t mki_len ) |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 2541 | { |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 2542 | if( mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH ) |
Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 2543 | { |
Johan Pascal | d576fdb | 2020-09-22 10:39:53 +0200 | [diff] [blame] | 2544 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 2545 | } |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 2546 | |
| 2547 | if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED ) |
Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 2548 | { |
Johan Pascal | d576fdb | 2020-09-22 10:39:53 +0200 | [diff] [blame] | 2549 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 2550 | } |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 2551 | |
| 2552 | memcpy( ssl->dtls_srtp_info.mki_value, mki_value, mki_len ); |
| 2553 | ssl->dtls_srtp_info.mki_len = mki_len; |
Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 2554 | return( 0 ); |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 2555 | } |
| 2556 | |
Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 2557 | int mbedtls_ssl_conf_dtls_srtp_protection_profiles( mbedtls_ssl_config *conf, |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 2558 | const mbedtls_ssl_srtp_profile *profiles ) |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2559 | { |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 2560 | const mbedtls_ssl_srtp_profile *p; |
| 2561 | size_t list_size = 0; |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2562 | |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 2563 | /* check the profiles list: all entry must be valid, |
| 2564 | * its size cannot be more than the total number of supported profiles, currently 4 */ |
Johan Pascal | d387aa0 | 2020-09-23 18:47:56 +0200 | [diff] [blame] | 2565 | for( p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET && |
| 2566 | list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH; |
| 2567 | p++ ) |
Johan Pascal | d576fdb | 2020-09-22 10:39:53 +0200 | [diff] [blame] | 2568 | { |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 2569 | if( mbedtls_ssl_check_srtp_profile_value( *p ) != MBEDTLS_TLS_SRTP_UNSET ) |
Johan Pascal | d576fdb | 2020-09-22 10:39:53 +0200 | [diff] [blame] | 2570 | { |
Johan Pascal | 76fdf1d | 2020-10-22 23:31:00 +0200 | [diff] [blame] | 2571 | list_size++; |
| 2572 | } |
| 2573 | else |
| 2574 | { |
| 2575 | /* unsupported value, stop parsing and set the size to an error value */ |
| 2576 | list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1; |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2577 | } |
| 2578 | } |
| 2579 | |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 2580 | if( list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH ) |
Johan Pascal | d387aa0 | 2020-09-23 18:47:56 +0200 | [diff] [blame] | 2581 | { |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 2582 | conf->dtls_srtp_profile_list = NULL; |
| 2583 | conf->dtls_srtp_profile_list_len = 0; |
| 2584 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2585 | } |
| 2586 | |
Johan Pascal | 9bc97ca | 2020-09-21 23:44:45 +0200 | [diff] [blame] | 2587 | conf->dtls_srtp_profile_list = profiles; |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 2588 | conf->dtls_srtp_profile_list_len = list_size; |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2589 | |
| 2590 | return( 0 ); |
| 2591 | } |
| 2592 | |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 2593 | void mbedtls_ssl_get_dtls_srtp_negotiation_result( const mbedtls_ssl_context *ssl, |
| 2594 | mbedtls_dtls_srtp_info *dtls_srtp_info ) |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2595 | { |
Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 2596 | dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile; |
| 2597 | /* do not copy the mki value if there is no chosen profile */ |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 2598 | if( dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET ) |
Johan Pascal | 0dbcd1d | 2020-10-28 11:03:07 +0100 | [diff] [blame] | 2599 | { |
Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 2600 | dtls_srtp_info->mki_len = 0; |
Johan Pascal | 0dbcd1d | 2020-10-28 11:03:07 +0100 | [diff] [blame] | 2601 | } |
Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 2602 | else |
| 2603 | { |
| 2604 | dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len; |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 2605 | memcpy( dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value, |
| 2606 | ssl->dtls_srtp_info.mki_len ); |
Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 2607 | } |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2608 | } |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2609 | #endif /* MBEDTLS_SSL_DTLS_SRTP */ |
| 2610 | |
Aditya Patwardhan | 3096f33 | 2022-07-26 14:31:46 +0530 | [diff] [blame] | 2611 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 2612 | void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor ) |
Paul Bakker | 490ecc8 | 2011-10-06 13:04:09 +0000 | [diff] [blame] | 2613 | { |
Glenn Strauss | 2dfcea2 | 2022-03-14 17:26:42 -0400 | [diff] [blame] | 2614 | conf->max_tls_version = (major << 8) | minor; |
Paul Bakker | 490ecc8 | 2011-10-06 13:04:09 +0000 | [diff] [blame] | 2615 | } |
| 2616 | |
Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 2617 | void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor ) |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 2618 | { |
Glenn Strauss | 2dfcea2 | 2022-03-14 17:26:42 -0400 | [diff] [blame] | 2619 | conf->min_tls_version = (major << 8) | minor; |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 2620 | } |
Aditya Patwardhan | 3096f33 | 2022-07-26 14:31:46 +0530 | [diff] [blame] | 2621 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 2622 | |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 2623 | #if defined(MBEDTLS_SSL_SRV_C) |
| 2624 | void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf, |
| 2625 | char cert_req_ca_list ) |
| 2626 | { |
| 2627 | conf->cert_req_ca_list = cert_req_ca_list; |
| 2628 | } |
| 2629 | #endif |
| 2630 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2631 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2632 | void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm ) |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 2633 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2634 | conf->encrypt_then_mac = etm; |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 2635 | } |
| 2636 | #endif |
| 2637 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2638 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2639 | void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems ) |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 2640 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2641 | conf->extended_ms = ems; |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 2642 | } |
| 2643 | #endif |
| 2644 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2645 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2646 | int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code ) |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 2647 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2648 | if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID || |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 2649 | ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN ) |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 2650 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2651 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 2652 | } |
| 2653 | |
Manuel Pégourié-Gonnard | 6bf89d6 | 2015-05-05 17:01:57 +0100 | [diff] [blame] | 2654 | conf->mfl_code = mfl_code; |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 2655 | |
| 2656 | return( 0 ); |
| 2657 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2658 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 2659 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2660 | void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2661 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2662 | conf->allow_legacy_renegotiation = allow_legacy; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2663 | } |
| 2664 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2665 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2666 | void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation ) |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 2667 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2668 | conf->disable_renegotiation = renegotiation; |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 2669 | } |
| 2670 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2671 | void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records ) |
Manuel Pégourié-Gonnard | a9964db | 2014-07-03 19:29:16 +0200 | [diff] [blame] | 2672 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2673 | conf->renego_max_records = max_records; |
Manuel Pégourié-Gonnard | a9964db | 2014-07-03 19:29:16 +0200 | [diff] [blame] | 2674 | } |
| 2675 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2676 | void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 837f0fe | 2014-11-05 13:58:53 +0100 | [diff] [blame] | 2677 | const unsigned char period[8] ) |
| 2678 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2679 | memcpy( conf->renego_period, period, 8 ); |
Manuel Pégourié-Gonnard | 837f0fe | 2014-11-05 13:58:53 +0100 | [diff] [blame] | 2680 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2681 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2682 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2683 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 2684 | #if defined(MBEDTLS_SSL_CLI_C) |
| 2685 | void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets ) |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 2686 | { |
Manuel Pégourié-Gonnard | 2b49445 | 2015-05-06 10:05:11 +0100 | [diff] [blame] | 2687 | conf->session_tickets = use_tickets; |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 2688 | } |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 2689 | #endif |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 2690 | |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 2691 | #if defined(MBEDTLS_SSL_SRV_C) |
Jerry Yu | 1ad7ace | 2022-08-09 13:28:39 +0800 | [diff] [blame] | 2692 | |
Jerry Yu | d0766ec | 2022-09-22 10:46:57 +0800 | [diff] [blame] | 2693 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS) |
Jerry Yu | 1ad7ace | 2022-08-09 13:28:39 +0800 | [diff] [blame] | 2694 | void mbedtls_ssl_conf_new_session_tickets( mbedtls_ssl_config *conf, |
| 2695 | uint16_t num_tickets ) |
| 2696 | { |
Jerry Yu | d0766ec | 2022-09-22 10:46:57 +0800 | [diff] [blame] | 2697 | conf->new_session_tickets_count = num_tickets; |
Jerry Yu | 1ad7ace | 2022-08-09 13:28:39 +0800 | [diff] [blame] | 2698 | } |
| 2699 | #endif |
| 2700 | |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 2701 | void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf, |
| 2702 | mbedtls_ssl_ticket_write_t *f_ticket_write, |
| 2703 | mbedtls_ssl_ticket_parse_t *f_ticket_parse, |
| 2704 | void *p_ticket ) |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 2705 | { |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 2706 | conf->f_ticket_write = f_ticket_write; |
| 2707 | conf->f_ticket_parse = f_ticket_parse; |
| 2708 | conf->p_ticket = p_ticket; |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 2709 | } |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 2710 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2711 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 2712 | |
Hanno Becker | 7e6c178 | 2021-06-08 09:24:55 +0100 | [diff] [blame] | 2713 | void mbedtls_ssl_set_export_keys_cb( mbedtls_ssl_context *ssl, |
| 2714 | mbedtls_ssl_export_keys_t *f_export_keys, |
| 2715 | void *p_export_keys ) |
Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 2716 | { |
Hanno Becker | 7e6c178 | 2021-06-08 09:24:55 +0100 | [diff] [blame] | 2717 | ssl->f_export_keys = f_export_keys; |
| 2718 | ssl->p_export_keys = p_export_keys; |
Ron Eldor | f5cc10d | 2019-05-07 18:33:40 +0300 | [diff] [blame] | 2719 | } |
Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 2720 | |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 2721 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 2722 | void mbedtls_ssl_conf_async_private_cb( |
| 2723 | mbedtls_ssl_config *conf, |
| 2724 | mbedtls_ssl_async_sign_t *f_async_sign, |
| 2725 | mbedtls_ssl_async_decrypt_t *f_async_decrypt, |
| 2726 | mbedtls_ssl_async_resume_t *f_async_resume, |
| 2727 | mbedtls_ssl_async_cancel_t *f_async_cancel, |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 2728 | void *async_config_data ) |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 2729 | { |
| 2730 | conf->f_async_sign_start = f_async_sign; |
| 2731 | conf->f_async_decrypt_start = f_async_decrypt; |
| 2732 | conf->f_async_resume = f_async_resume; |
| 2733 | conf->f_async_cancel = f_async_cancel; |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 2734 | conf->p_async_config_data = async_config_data; |
| 2735 | } |
| 2736 | |
Gilles Peskine | 8f97af7 | 2018-04-26 11:46:10 +0200 | [diff] [blame] | 2737 | void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf ) |
| 2738 | { |
| 2739 | return( conf->p_async_config_data ); |
| 2740 | } |
| 2741 | |
Gilles Peskine | 1febfef | 2018-04-30 11:54:39 +0200 | [diff] [blame] | 2742 | void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl ) |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 2743 | { |
| 2744 | if( ssl->handshake == NULL ) |
| 2745 | return( NULL ); |
| 2746 | else |
| 2747 | return( ssl->handshake->user_async_ctx ); |
| 2748 | } |
| 2749 | |
Gilles Peskine | 1febfef | 2018-04-30 11:54:39 +0200 | [diff] [blame] | 2750 | void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl, |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 2751 | void *ctx ) |
| 2752 | { |
| 2753 | if( ssl->handshake != NULL ) |
| 2754 | ssl->handshake->user_async_ctx = ctx; |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 2755 | } |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 2756 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 2757 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2758 | /* |
| 2759 | * SSL get accessors |
| 2760 | */ |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 2761 | uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2762 | { |
Manuel Pégourié-Gonnard | e89163c | 2015-01-23 14:30:57 +0000 | [diff] [blame] | 2763 | if( ssl->session != NULL ) |
| 2764 | return( ssl->session->verify_result ); |
| 2765 | |
| 2766 | if( ssl->session_negotiate != NULL ) |
| 2767 | return( ssl->session_negotiate->verify_result ); |
| 2768 | |
Manuel Pégourié-Gonnard | 6ab9b00 | 2015-05-14 11:25:04 +0200 | [diff] [blame] | 2769 | return( 0xFFFFFFFF ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2770 | } |
| 2771 | |
Glenn Strauss | 8f52690 | 2022-01-13 00:04:49 -0500 | [diff] [blame] | 2772 | int mbedtls_ssl_get_ciphersuite_id_from_ssl( const mbedtls_ssl_context *ssl ) |
| 2773 | { |
| 2774 | if( ssl == NULL || ssl->session == NULL ) |
| 2775 | return( 0 ); |
| 2776 | |
| 2777 | return( ssl->session->ciphersuite ); |
| 2778 | } |
| 2779 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2780 | const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl ) |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 2781 | { |
Paul Bakker | 926c8e4 | 2013-03-06 10:23:34 +0100 | [diff] [blame] | 2782 | if( ssl == NULL || ssl->session == NULL ) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 2783 | return( NULL ); |
Paul Bakker | 926c8e4 | 2013-03-06 10:23:34 +0100 | [diff] [blame] | 2784 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2785 | return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite ); |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 2786 | } |
| 2787 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2788 | const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl ) |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 2789 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2790 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2791 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 2792 | { |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 2793 | switch( ssl->tls_version ) |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 2794 | { |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 2795 | case MBEDTLS_SSL_VERSION_TLS1_2: |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 2796 | return( "DTLSv1.2" ); |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 2797 | default: |
| 2798 | return( "unknown (DTLS)" ); |
| 2799 | } |
| 2800 | } |
| 2801 | #endif |
| 2802 | |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 2803 | switch( ssl->tls_version ) |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 2804 | { |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 2805 | case MBEDTLS_SSL_VERSION_TLS1_2: |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2806 | return( "TLSv1.2" ); |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 2807 | case MBEDTLS_SSL_VERSION_TLS1_3: |
Gilles Peskine | c63a1e0 | 2022-01-13 01:10:24 +0100 | [diff] [blame] | 2808 | return( "TLSv1.3" ); |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 2809 | default: |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 2810 | return( "unknown" ); |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 2811 | } |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 2812 | } |
| 2813 | |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 2814 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 2815 | size_t mbedtls_ssl_get_input_max_frag_len( const mbedtls_ssl_context *ssl ) |
| 2816 | { |
David Horstmann | 95d516f | 2021-05-04 18:36:56 +0100 | [diff] [blame] | 2817 | size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN; |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 2818 | size_t read_mfl; |
| 2819 | |
| 2820 | /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */ |
| 2821 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
| 2822 | ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE ) |
| 2823 | { |
| 2824 | return ssl_mfl_code_to_length( ssl->conf->mfl_code ); |
| 2825 | } |
| 2826 | |
| 2827 | /* Check if a smaller max length was negotiated */ |
| 2828 | if( ssl->session_out != NULL ) |
| 2829 | { |
| 2830 | read_mfl = ssl_mfl_code_to_length( ssl->session_out->mfl_code ); |
| 2831 | if( read_mfl < max_len ) |
| 2832 | { |
| 2833 | max_len = read_mfl; |
| 2834 | } |
| 2835 | } |
| 2836 | |
| 2837 | // During a handshake, use the value being negotiated |
| 2838 | if( ssl->session_negotiate != NULL ) |
| 2839 | { |
| 2840 | read_mfl = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ); |
| 2841 | if( read_mfl < max_len ) |
| 2842 | { |
| 2843 | max_len = read_mfl; |
| 2844 | } |
| 2845 | } |
| 2846 | |
| 2847 | return( max_len ); |
| 2848 | } |
| 2849 | |
| 2850 | size_t mbedtls_ssl_get_output_max_frag_len( const mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 2851 | { |
| 2852 | size_t max_len; |
| 2853 | |
| 2854 | /* |
| 2855 | * Assume mfl_code is correct since it was checked when set |
| 2856 | */ |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 2857 | max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code ); |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 2858 | |
Manuel Pégourié-Gonnard | 2cb17e2 | 2017-09-19 13:00:47 +0200 | [diff] [blame] | 2859 | /* Check if a smaller max length was negotiated */ |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 2860 | if( ssl->session_out != NULL && |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 2861 | ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len ) |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 2862 | { |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 2863 | max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code ); |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 2864 | } |
| 2865 | |
Manuel Pégourié-Gonnard | 2cb17e2 | 2017-09-19 13:00:47 +0200 | [diff] [blame] | 2866 | /* During a handshake, use the value being negotiated */ |
| 2867 | if( ssl->session_negotiate != NULL && |
| 2868 | ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len ) |
| 2869 | { |
| 2870 | max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ); |
| 2871 | } |
| 2872 | |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 2873 | return( max_len ); |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 2874 | } |
| 2875 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
| 2876 | |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 2877 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 8949071 | 2020-02-05 10:50:12 +0000 | [diff] [blame] | 2878 | size_t mbedtls_ssl_get_current_mtu( const mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 2879 | { |
Andrzej Kurek | ef43ce6 | 2018-10-09 08:24:12 -0400 | [diff] [blame] | 2880 | /* Return unlimited mtu for client hello messages to avoid fragmentation. */ |
| 2881 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
| 2882 | ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO || |
| 2883 | ssl->state == MBEDTLS_SSL_SERVER_HELLO ) ) |
| 2884 | return ( 0 ); |
| 2885 | |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 2886 | if( ssl->handshake == NULL || ssl->handshake->mtu == 0 ) |
| 2887 | return( ssl->mtu ); |
| 2888 | |
| 2889 | if( ssl->mtu == 0 ) |
| 2890 | return( ssl->handshake->mtu ); |
| 2891 | |
| 2892 | return( ssl->mtu < ssl->handshake->mtu ? |
| 2893 | ssl->mtu : ssl->handshake->mtu ); |
| 2894 | } |
| 2895 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 2896 | |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 2897 | int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl ) |
| 2898 | { |
| 2899 | size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN; |
| 2900 | |
Manuel Pégourié-Gonnard | 000281e | 2018-08-21 11:20:58 +0200 | [diff] [blame] | 2901 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \ |
| 2902 | !defined(MBEDTLS_SSL_PROTO_DTLS) |
| 2903 | (void) ssl; |
| 2904 | #endif |
| 2905 | |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 2906 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 2907 | const size_t mfl = mbedtls_ssl_get_output_max_frag_len( ssl ); |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 2908 | |
| 2909 | if( max_len > mfl ) |
| 2910 | max_len = mfl; |
| 2911 | #endif |
| 2912 | |
| 2913 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 8949071 | 2020-02-05 10:50:12 +0000 | [diff] [blame] | 2914 | if( mbedtls_ssl_get_current_mtu( ssl ) != 0 ) |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 2915 | { |
Hanno Becker | 8949071 | 2020-02-05 10:50:12 +0000 | [diff] [blame] | 2916 | const size_t mtu = mbedtls_ssl_get_current_mtu( ssl ); |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 2917 | const int ret = mbedtls_ssl_get_record_expansion( ssl ); |
| 2918 | const size_t overhead = (size_t) ret; |
| 2919 | |
| 2920 | if( ret < 0 ) |
| 2921 | return( ret ); |
| 2922 | |
| 2923 | if( mtu <= overhead ) |
| 2924 | { |
| 2925 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) ); |
| 2926 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 2927 | } |
| 2928 | |
| 2929 | if( max_len > mtu - overhead ) |
| 2930 | max_len = mtu - overhead; |
| 2931 | } |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 2932 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 2933 | |
Hanno Becker | 0defedb | 2018-08-10 12:35:02 +0100 | [diff] [blame] | 2934 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \ |
| 2935 | !defined(MBEDTLS_SSL_PROTO_DTLS) |
| 2936 | ((void) ssl); |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 2937 | #endif |
| 2938 | |
| 2939 | return( (int) max_len ); |
| 2940 | } |
| 2941 | |
Hanno Becker | 2d8e99b | 2021-04-21 06:19:50 +0100 | [diff] [blame] | 2942 | int mbedtls_ssl_get_max_in_record_payload( const mbedtls_ssl_context *ssl ) |
| 2943 | { |
| 2944 | size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN; |
| 2945 | |
| 2946 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 2947 | (void) ssl; |
| 2948 | #endif |
| 2949 | |
| 2950 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 2951 | const size_t mfl = mbedtls_ssl_get_input_max_frag_len( ssl ); |
| 2952 | |
| 2953 | if( max_len > mfl ) |
| 2954 | max_len = mfl; |
| 2955 | #endif |
| 2956 | |
| 2957 | return( (int) max_len ); |
| 2958 | } |
| 2959 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2960 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 2961 | const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl ) |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 2962 | { |
| 2963 | if( ssl == NULL || ssl->session == NULL ) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 2964 | return( NULL ); |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 2965 | |
Hanno Becker | e682457 | 2019-02-07 13:18:46 +0000 | [diff] [blame] | 2966 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 2967 | return( ssl->session->peer_cert ); |
Hanno Becker | e682457 | 2019-02-07 13:18:46 +0000 | [diff] [blame] | 2968 | #else |
| 2969 | return( NULL ); |
| 2970 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 2971 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2972 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 2973 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2974 | #if defined(MBEDTLS_SSL_CLI_C) |
Hanno Becker | f852b1c | 2019-02-05 11:42:30 +0000 | [diff] [blame] | 2975 | int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, |
| 2976 | mbedtls_ssl_session *dst ) |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 2977 | { |
Hanno Becker | e810bbc | 2021-05-14 16:01:05 +0100 | [diff] [blame] | 2978 | int ret; |
| 2979 | |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 2980 | if( ssl == NULL || |
| 2981 | dst == NULL || |
| 2982 | ssl->session == NULL || |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2983 | ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT ) |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 2984 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2985 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 2986 | } |
| 2987 | |
Hanno Becker | e810bbc | 2021-05-14 16:01:05 +0100 | [diff] [blame] | 2988 | /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer |
| 2989 | * idempotent: Each session can only be exported once. |
| 2990 | * |
| 2991 | * (This is in preparation for TLS 1.3 support where we will |
| 2992 | * need the ability to export multiple sessions (aka tickets), |
| 2993 | * which will be achieved by calling mbedtls_ssl_get_session() |
| 2994 | * multiple times until it fails.) |
| 2995 | * |
| 2996 | * Check whether we have already exported the current session, |
| 2997 | * and fail if so. |
| 2998 | */ |
| 2999 | if( ssl->session->exported == 1 ) |
| 3000 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 3001 | |
| 3002 | ret = mbedtls_ssl_session_copy( dst, ssl->session ); |
| 3003 | if( ret != 0 ) |
| 3004 | return( ret ); |
| 3005 | |
| 3006 | /* Remember that we've exported the session. */ |
| 3007 | ssl->session->exported = 1; |
| 3008 | return( 0 ); |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 3009 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3010 | #endif /* MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 3011 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3012 | /* |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 3013 | * Define ticket header determining Mbed TLS version |
| 3014 | * and structure of the ticket. |
| 3015 | */ |
| 3016 | |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3017 | /* |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 3018 | * Define bitflag determining compile-time settings influencing |
| 3019 | * structure of serialized SSL sessions. |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3020 | */ |
| 3021 | |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 3022 | #if defined(MBEDTLS_HAVE_TIME) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3023 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME 1 |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 3024 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3025 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3026 | #endif /* MBEDTLS_HAVE_TIME */ |
| 3027 | |
| 3028 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3029 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3030 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3031 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3032 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 3033 | |
| 3034 | #if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3035 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3036 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3037 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3038 | #endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */ |
| 3039 | |
| 3040 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3041 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3042 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3043 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3044 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
| 3045 | |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3046 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3047 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3048 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3049 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3050 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ |
| 3051 | |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3052 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 3053 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1 |
| 3054 | #else |
| 3055 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0 |
| 3056 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
| 3057 | |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3058 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0 |
| 3059 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1 |
| 3060 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2 |
| 3061 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3 |
Hanno Becker | 37bdbe6 | 2021-08-01 05:38:58 +0100 | [diff] [blame] | 3062 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4 |
| 3063 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5 |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3064 | |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 3065 | #define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \ |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3066 | ( (uint16_t) ( \ |
| 3067 | ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \ |
| 3068 | ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \ |
| 3069 | ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \ |
| 3070 | ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \ |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3071 | ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \ |
Hanno Becker | be34e8e | 2019-06-04 09:43:16 +0100 | [diff] [blame] | 3072 | ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) ) ) |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3073 | |
Hanno Becker | f878707 | 2019-05-16 12:41:07 +0100 | [diff] [blame] | 3074 | static unsigned char ssl_serialized_session_header[] = { |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3075 | MBEDTLS_VERSION_MAJOR, |
| 3076 | MBEDTLS_VERSION_MINOR, |
| 3077 | MBEDTLS_VERSION_PATCH, |
Joe Subbiani | 2194dc4 | 2021-07-14 12:31:31 +0100 | [diff] [blame] | 3078 | MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ), |
| 3079 | MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ), |
Hanno Becker | f878707 | 2019-05-16 12:41:07 +0100 | [diff] [blame] | 3080 | }; |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 3081 | |
| 3082 | /* |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 3083 | * Serialize a session in the following format: |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 3084 | * (in the presentation language of TLS, RFC 8446 section 3) |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 3085 | * |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3086 | * struct { |
Hanno Becker | dc28b6c | 2019-05-29 11:08:00 +0100 | [diff] [blame] | 3087 | * |
Hanno Becker | dce5097 | 2021-08-01 05:39:23 +0100 | [diff] [blame] | 3088 | * opaque mbedtls_version[3]; // library version: major, minor, patch |
| 3089 | * opaque session_format[2]; // library-version specific 16-bit field |
| 3090 | * // determining the format of the remaining |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3091 | * // serialized data. |
Hanno Becker | dc28b6c | 2019-05-29 11:08:00 +0100 | [diff] [blame] | 3092 | * |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3093 | * Note: When updating the format, remember to keep |
| 3094 | * these version+format bytes. |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 3095 | * |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3096 | * // In this version, `session_format` determines |
| 3097 | * // the setting of those compile-time |
| 3098 | * // configuration options which influence |
| 3099 | * // the structure of mbedtls_ssl_session. |
| 3100 | * |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 3101 | * uint8_t minor_ver; // Protocol minor version. Possible values: |
| 3102 | * // - TLS 1.2 (3) |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3103 | * |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 3104 | * select (serialized_session.tls_version) { |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3105 | * |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 3106 | * case MBEDTLS_SSL_VERSION_TLS1_2: |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3107 | * serialized_session_tls12 data; |
| 3108 | * |
| 3109 | * }; |
| 3110 | * |
| 3111 | * } serialized_session; |
| 3112 | * |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 3113 | */ |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3114 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 3115 | MBEDTLS_CHECK_RETURN_CRITICAL |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3116 | static int ssl_session_save( const mbedtls_ssl_session *session, |
| 3117 | unsigned char omit_header, |
| 3118 | unsigned char *buf, |
| 3119 | size_t buf_len, |
| 3120 | size_t *olen ) |
| 3121 | { |
| 3122 | unsigned char *p = buf; |
| 3123 | size_t used = 0; |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 3124 | size_t remaining_len; |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 3125 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 3126 | size_t out_len; |
| 3127 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 3128 | #endif |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 3129 | if( session == NULL ) |
| 3130 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 3131 | |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3132 | if( !omit_header ) |
| 3133 | { |
| 3134 | /* |
| 3135 | * Add Mbed TLS version identifier |
| 3136 | */ |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3137 | used += sizeof( ssl_serialized_session_header ); |
| 3138 | |
| 3139 | if( used <= buf_len ) |
| 3140 | { |
| 3141 | memcpy( p, ssl_serialized_session_header, |
| 3142 | sizeof( ssl_serialized_session_header ) ); |
| 3143 | p += sizeof( ssl_serialized_session_header ); |
| 3144 | } |
| 3145 | } |
| 3146 | |
| 3147 | /* |
| 3148 | * TLS version identifier |
| 3149 | */ |
| 3150 | used += 1; |
| 3151 | if( used <= buf_len ) |
| 3152 | { |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 3153 | *p++ = MBEDTLS_BYTE_0( session->tls_version ); |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3154 | } |
| 3155 | |
| 3156 | /* Forward to version-specific serialization routine. */ |
Jerry Yu | fca4d57 | 2022-07-21 10:37:48 +0800 | [diff] [blame] | 3157 | remaining_len = (buf_len >= used) ? buf_len - used : 0; |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 3158 | switch( session->tls_version ) |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3159 | { |
| 3160 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 3161 | case MBEDTLS_SSL_VERSION_TLS1_2: |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 3162 | used += ssl_tls12_session_save( session, p, remaining_len ); |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3163 | break; |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3164 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 3165 | |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 3166 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 3167 | case MBEDTLS_SSL_VERSION_TLS1_3: |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 3168 | ret = ssl_tls13_session_save( session, p, remaining_len, &out_len ); |
| 3169 | if( ret != 0 && ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ) |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 3170 | return( ret ); |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 3171 | used += out_len; |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 3172 | break; |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 3173 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 3174 | |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3175 | default: |
| 3176 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 3177 | } |
| 3178 | |
| 3179 | *olen = used; |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 3180 | if( used > buf_len ) |
| 3181 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 3182 | |
| 3183 | return( 0 ); |
| 3184 | } |
| 3185 | |
| 3186 | /* |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 3187 | * Public wrapper for ssl_session_save() |
| 3188 | */ |
| 3189 | int mbedtls_ssl_session_save( const mbedtls_ssl_session *session, |
| 3190 | unsigned char *buf, |
| 3191 | size_t buf_len, |
| 3192 | size_t *olen ) |
| 3193 | { |
| 3194 | return( ssl_session_save( session, 0, buf, buf_len, olen ) ); |
| 3195 | } |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 3196 | |
Jerry Yu | f1b23ca | 2022-02-18 11:48:47 +0800 | [diff] [blame] | 3197 | /* |
| 3198 | * Deserialize session, see mbedtls_ssl_session_save() for format. |
| 3199 | * |
| 3200 | * This internal version is wrapped by a public function that cleans up in |
| 3201 | * case of error, and has an extra option omit_header. |
| 3202 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 3203 | MBEDTLS_CHECK_RETURN_CRITICAL |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3204 | static int ssl_session_load( mbedtls_ssl_session *session, |
| 3205 | unsigned char omit_header, |
| 3206 | const unsigned char *buf, |
| 3207 | size_t len ) |
| 3208 | { |
| 3209 | const unsigned char *p = buf; |
| 3210 | const unsigned char * const end = buf + len; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 3211 | size_t remaining_len; |
| 3212 | |
| 3213 | |
| 3214 | if( session == NULL ) |
| 3215 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3216 | |
| 3217 | if( !omit_header ) |
| 3218 | { |
| 3219 | /* |
| 3220 | * Check Mbed TLS version identifier |
| 3221 | */ |
| 3222 | |
| 3223 | if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) ) |
| 3224 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3225 | |
| 3226 | if( memcmp( p, ssl_serialized_session_header, |
| 3227 | sizeof( ssl_serialized_session_header ) ) != 0 ) |
| 3228 | { |
| 3229 | return( MBEDTLS_ERR_SSL_VERSION_MISMATCH ); |
| 3230 | } |
| 3231 | p += sizeof( ssl_serialized_session_header ); |
| 3232 | } |
| 3233 | |
| 3234 | /* |
| 3235 | * TLS version identifier |
| 3236 | */ |
| 3237 | if( 1 > (size_t)( end - p ) ) |
| 3238 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 3239 | session->tls_version = 0x0300 | *p++; |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3240 | |
| 3241 | /* Dispatch according to TLS version. */ |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 3242 | remaining_len = ( end - p ); |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 3243 | switch( session->tls_version ) |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3244 | { |
| 3245 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 3246 | case MBEDTLS_SSL_VERSION_TLS1_2: |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 3247 | return( ssl_tls12_session_load( session, p, remaining_len ) ); |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3248 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 3249 | |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 3250 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 3251 | case MBEDTLS_SSL_VERSION_TLS1_3: |
| 3252 | return( ssl_tls13_session_load( session, p, remaining_len ) ); |
| 3253 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 3254 | |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3255 | default: |
| 3256 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3257 | } |
| 3258 | } |
| 3259 | |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 3260 | /* |
Manuel Pégourié-Gonnard | b9dfc9f | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 3261 | * Deserialize session: public wrapper for error cleaning |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 3262 | */ |
| 3263 | int mbedtls_ssl_session_load( mbedtls_ssl_session *session, |
| 3264 | const unsigned char *buf, |
| 3265 | size_t len ) |
| 3266 | { |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 3267 | int ret = ssl_session_load( session, 0, buf, len ); |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 3268 | |
| 3269 | if( ret != 0 ) |
| 3270 | mbedtls_ssl_session_free( session ); |
| 3271 | |
| 3272 | return( ret ); |
| 3273 | } |
| 3274 | |
| 3275 | /* |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3276 | * Perform a single step of the SSL handshake |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3277 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 3278 | MBEDTLS_CHECK_RETURN_CRITICAL |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 3279 | static int ssl_prepare_handshake_step( mbedtls_ssl_context *ssl ) |
| 3280 | { |
| 3281 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 3282 | |
Ronald Cron | 66dbf91 | 2022-02-02 15:33:46 +0100 | [diff] [blame] | 3283 | /* |
| 3284 | * We may have not been able to send to the peer all the handshake data |
Ronald Cron | 3f20b77 | 2022-03-08 16:00:02 +0100 | [diff] [blame] | 3285 | * that were written into the output buffer by the previous handshake step, |
| 3286 | * if the write to the network callback returned with the |
Ronald Cron | 66dbf91 | 2022-02-02 15:33:46 +0100 | [diff] [blame] | 3287 | * #MBEDTLS_ERR_SSL_WANT_WRITE error code. |
| 3288 | * We proceed to the next handshake step only when all data from the |
| 3289 | * previous one have been sent to the peer, thus we make sure that this is |
| 3290 | * the case here by calling `mbedtls_ssl_flush_output()`. The function may |
| 3291 | * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case |
| 3292 | * we have to wait before to go ahead. |
| 3293 | * In the case of TLS 1.3, handshake step handlers do not send data to the |
| 3294 | * peer. Data are only sent here and through |
| 3295 | * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an |
Andrzej Kurek | 5c65c57 | 2022-04-13 14:28:52 -0400 | [diff] [blame] | 3296 | * alert occurred. |
Ronald Cron | 66dbf91 | 2022-02-02 15:33:46 +0100 | [diff] [blame] | 3297 | */ |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 3298 | if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) |
| 3299 | return( ret ); |
| 3300 | |
| 3301 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3302 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 3303 | ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING ) |
| 3304 | { |
| 3305 | if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 ) |
| 3306 | return( ret ); |
| 3307 | } |
| 3308 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 3309 | |
| 3310 | return( ret ); |
| 3311 | } |
| 3312 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3313 | int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3314 | { |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 3315 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3316 | |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 3317 | if( ssl == NULL || |
| 3318 | ssl->conf == NULL || |
| 3319 | ssl->handshake == NULL || |
Paul Elliott | 27b0d94 | 2022-03-18 21:55:32 +0000 | [diff] [blame] | 3320 | mbedtls_ssl_is_handshake_over( ssl ) == 1 ) |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 3321 | { |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 3322 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 3323 | } |
| 3324 | |
| 3325 | ret = ssl_prepare_handshake_step( ssl ); |
| 3326 | if( ret != 0 ) |
| 3327 | return( ret ); |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 3328 | |
Jerry Yu | e704781 | 2021-09-13 19:26:39 +0800 | [diff] [blame] | 3329 | ret = mbedtls_ssl_handle_pending_alert( ssl ); |
| 3330 | if( ret != 0 ) |
| 3331 | goto cleanup; |
| 3332 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3333 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3334 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 3335 | { |
Ronald Cron | 27c85e7 | 2022-03-08 11:37:55 +0100 | [diff] [blame] | 3336 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "client state: %s", |
| 3337 | mbedtls_ssl_states_str( ssl->state ) ) ); |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 3338 | |
Ronald Cron | 9f0fba3 | 2022-02-10 16:45:15 +0100 | [diff] [blame] | 3339 | switch( ssl->state ) |
| 3340 | { |
| 3341 | case MBEDTLS_SSL_HELLO_REQUEST: |
| 3342 | ssl->state = MBEDTLS_SSL_CLIENT_HELLO; |
| 3343 | break; |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 3344 | |
Ronald Cron | 9f0fba3 | 2022-02-10 16:45:15 +0100 | [diff] [blame] | 3345 | case MBEDTLS_SSL_CLIENT_HELLO: |
| 3346 | ret = mbedtls_ssl_write_client_hello( ssl ); |
| 3347 | break; |
| 3348 | |
| 3349 | default: |
| 3350 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 3351 | if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 ) |
Ronald Cron | 9f0fba3 | 2022-02-10 16:45:15 +0100 | [diff] [blame] | 3352 | ret = mbedtls_ssl_tls13_handshake_client_step( ssl ); |
| 3353 | else |
| 3354 | ret = mbedtls_ssl_handshake_client_step( ssl ); |
| 3355 | #elif defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 3356 | ret = mbedtls_ssl_handshake_client_step( ssl ); |
| 3357 | #else |
| 3358 | ret = mbedtls_ssl_tls13_handshake_client_step( ssl ); |
| 3359 | #endif |
| 3360 | } |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 3361 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3362 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3363 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3364 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 3365 | { |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3366 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 3367 | if( mbedtls_ssl_conf_is_tls13_only( ssl->conf ) ) |
Jerry Yu | 2756193 | 2021-08-27 17:07:38 +0800 | [diff] [blame] | 3368 | ret = mbedtls_ssl_tls13_handshake_server_step( ssl ); |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3369 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 3370 | |
| 3371 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 3372 | if( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) ) |
| 3373 | ret = mbedtls_ssl_handshake_server_step( ssl ); |
| 3374 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 3375 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3376 | #endif |
| 3377 | |
Jerry Yu | e704781 | 2021-09-13 19:26:39 +0800 | [diff] [blame] | 3378 | if( ret != 0 ) |
| 3379 | { |
Jerry Yu | bbd5a3f | 2021-09-18 20:50:22 +0800 | [diff] [blame] | 3380 | /* handshake_step return error. And it is same |
| 3381 | * with alert_reason. |
| 3382 | */ |
Jerry Yu | 3bf1f97 | 2021-09-22 21:37:18 +0800 | [diff] [blame] | 3383 | if( ssl->send_alert ) |
Jerry Yu | e704781 | 2021-09-13 19:26:39 +0800 | [diff] [blame] | 3384 | { |
Jerry Yu | 3bf1f97 | 2021-09-22 21:37:18 +0800 | [diff] [blame] | 3385 | ret = mbedtls_ssl_handle_pending_alert( ssl ); |
Jerry Yu | e704781 | 2021-09-13 19:26:39 +0800 | [diff] [blame] | 3386 | goto cleanup; |
| 3387 | } |
| 3388 | } |
| 3389 | |
| 3390 | cleanup: |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3391 | return( ret ); |
| 3392 | } |
| 3393 | |
| 3394 | /* |
| 3395 | * Perform the SSL handshake |
| 3396 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3397 | int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl ) |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3398 | { |
| 3399 | int ret = 0; |
| 3400 | |
Hanno Becker | a817ea4 | 2020-10-20 15:20:23 +0100 | [diff] [blame] | 3401 | /* Sanity checks */ |
| 3402 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 3403 | if( ssl == NULL || ssl->conf == NULL ) |
| 3404 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3405 | |
Hanno Becker | a817ea4 | 2020-10-20 15:20:23 +0100 | [diff] [blame] | 3406 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3407 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 3408 | ( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL ) ) |
| 3409 | { |
| 3410 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use " |
| 3411 | "mbedtls_ssl_set_timer_cb() for DTLS" ) ); |
| 3412 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3413 | } |
| 3414 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 3415 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3416 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3417 | |
Hanno Becker | a817ea4 | 2020-10-20 15:20:23 +0100 | [diff] [blame] | 3418 | /* Main handshake loop */ |
Paul Elliott | 27b0d94 | 2022-03-18 21:55:32 +0000 | [diff] [blame] | 3419 | while( mbedtls_ssl_is_handshake_over( ssl ) == 0 ) |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3420 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3421 | ret = mbedtls_ssl_handshake_step( ssl ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3422 | |
| 3423 | if( ret != 0 ) |
| 3424 | break; |
| 3425 | } |
| 3426 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3427 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3428 | |
| 3429 | return( ret ); |
| 3430 | } |
| 3431 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3432 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 3433 | #if defined(MBEDTLS_SSL_SRV_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3434 | /* |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3435 | * Write HelloRequest to request renegotiation on server |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3436 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 3437 | MBEDTLS_CHECK_RETURN_CRITICAL |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3438 | static int ssl_write_hello_request( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3439 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3440 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3441 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3442 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) ); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3443 | |
| 3444 | ssl->out_msglen = 4; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3445 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 3446 | ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST; |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3447 | |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3448 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3449 | { |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3450 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3451 | return( ret ); |
| 3452 | } |
| 3453 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3454 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) ); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3455 | |
| 3456 | return( 0 ); |
| 3457 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3458 | #endif /* MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3459 | |
| 3460 | /* |
| 3461 | * Actually renegotiate current connection, triggered by either: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3462 | * - any side: calling mbedtls_ssl_renegotiate(), |
| 3463 | * - client: receiving a HelloRequest during mbedtls_ssl_read(), |
| 3464 | * - server: receiving any handshake message on server during mbedtls_ssl_read() after |
Manuel Pégourié-Gonnard | 55e4ff2 | 2014-08-19 11:16:35 +0200 | [diff] [blame] | 3465 | * the initial handshake is completed. |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3466 | * If the handshake doesn't complete due to waiting for I/O, it will continue |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3467 | * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively. |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3468 | */ |
Hanno Becker | 40cdaa1 | 2020-02-05 10:48:27 +0000 | [diff] [blame] | 3469 | int mbedtls_ssl_start_renegotiation( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3470 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3471 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3472 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3473 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3474 | |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3475 | if( ( ret = ssl_handshake_init( ssl ) ) != 0 ) |
| 3476 | return( ret ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3477 | |
Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 3478 | /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and |
| 3479 | * the ServerHello will have message_seq = 1" */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3480 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3481 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3482 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ) |
Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 3483 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3484 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 3485 | ssl->handshake->out_msg_seq = 1; |
| 3486 | else |
| 3487 | ssl->handshake->in_msg_seq = 1; |
Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 3488 | } |
| 3489 | #endif |
| 3490 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3491 | ssl->state = MBEDTLS_SSL_HELLO_REQUEST; |
| 3492 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3493 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3494 | if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3495 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3496 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3497 | return( ret ); |
| 3498 | } |
| 3499 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3500 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3501 | |
| 3502 | return( 0 ); |
| 3503 | } |
| 3504 | |
| 3505 | /* |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3506 | * Renegotiate current connection on client, |
| 3507 | * or request renegotiation on server |
| 3508 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3509 | int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3510 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3511 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3512 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 3513 | if( ssl == NULL || ssl->conf == NULL ) |
| 3514 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3515 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3516 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3517 | /* On server, just send the request */ |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3518 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3519 | { |
Paul Elliott | 27b0d94 | 2022-03-18 21:55:32 +0000 | [diff] [blame] | 3520 | if( mbedtls_ssl_is_handshake_over( ssl ) == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3521 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3522 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3523 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING; |
Manuel Pégourié-Gonnard | f07f421 | 2014-08-15 19:04:47 +0200 | [diff] [blame] | 3524 | |
| 3525 | /* Did we already try/start sending HelloRequest? */ |
| 3526 | if( ssl->out_left != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3527 | return( mbedtls_ssl_flush_output( ssl ) ); |
Manuel Pégourié-Gonnard | f07f421 | 2014-08-15 19:04:47 +0200 | [diff] [blame] | 3528 | |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3529 | return( ssl_write_hello_request( ssl ) ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3530 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3531 | #endif /* MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3532 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3533 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3534 | /* |
| 3535 | * On client, either start the renegotiation process or, |
| 3536 | * if already in progress, continue the handshake |
| 3537 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3538 | if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3539 | { |
Paul Elliott | 27b0d94 | 2022-03-18 21:55:32 +0000 | [diff] [blame] | 3540 | if( mbedtls_ssl_is_handshake_over( ssl ) == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3541 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3542 | |
Hanno Becker | 40cdaa1 | 2020-02-05 10:48:27 +0000 | [diff] [blame] | 3543 | if( ( ret = mbedtls_ssl_start_renegotiation( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3544 | { |
Hanno Becker | 40cdaa1 | 2020-02-05 10:48:27 +0000 | [diff] [blame] | 3545 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_start_renegotiation", ret ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3546 | return( ret ); |
| 3547 | } |
| 3548 | } |
| 3549 | else |
| 3550 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3551 | if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3552 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3553 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3554 | return( ret ); |
| 3555 | } |
| 3556 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3557 | #endif /* MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3558 | |
Paul Bakker | 37ce0ff | 2013-10-31 14:32:04 +0100 | [diff] [blame] | 3559 | return( ret ); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3560 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3561 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3562 | |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 3563 | void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3564 | { |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 3565 | mbedtls_ssl_handshake_params *handshake = ssl->handshake; |
| 3566 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3567 | if( handshake == NULL ) |
| 3568 | return; |
| 3569 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 3570 | #if defined(MBEDTLS_ECP_C) |
| 3571 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 3572 | if ( ssl->handshake->group_list_heap_allocated ) |
| 3573 | mbedtls_free( (void*) handshake->group_list ); |
| 3574 | handshake->group_list = NULL; |
| 3575 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
| 3576 | #endif /* MBEDTLS_ECP_C */ |
| 3577 | |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 3578 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
| 3579 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 3580 | if ( ssl->handshake->sig_algs_heap_allocated ) |
| 3581 | mbedtls_free( (void*) handshake->sig_algs ); |
| 3582 | handshake->sig_algs = NULL; |
| 3583 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
Xiaofei Bai | c234ecf | 2022-02-08 09:59:23 +0000 | [diff] [blame] | 3584 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 3585 | if( ssl->handshake->certificate_request_context ) |
| 3586 | { |
| 3587 | mbedtls_free( (void*) handshake->certificate_request_context ); |
| 3588 | } |
| 3589 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 3590 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 3591 | |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3592 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
| 3593 | if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 ) |
| 3594 | { |
Gilles Peskine | 8f97af7 | 2018-04-26 11:46:10 +0200 | [diff] [blame] | 3595 | ssl->conf->f_async_cancel( ssl ); |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3596 | handshake->async_in_progress = 0; |
| 3597 | } |
| 3598 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
| 3599 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 3600 | #if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3601 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 3602 | psa_hash_abort( &handshake->fin_sha256_psa ); |
| 3603 | #else |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 3604 | mbedtls_sha256_free( &handshake->fin_sha256 ); |
| 3605 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3606 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 3607 | #if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3608 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 3609 | psa_hash_abort( &handshake->fin_sha384_psa ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3610 | #else |
Andrzej Kurek | a242e83 | 2022-08-11 10:03:14 -0400 | [diff] [blame] | 3611 | mbedtls_sha512_free( &handshake->fin_sha384 ); |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 3612 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3613 | #endif |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 3614 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3615 | #if defined(MBEDTLS_DHM_C) |
| 3616 | mbedtls_dhm_free( &handshake->dhm_ctx ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3617 | #endif |
Neil Armstrong | f3f4641 | 2022-04-12 14:43:39 +0200 | [diff] [blame] | 3618 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3619 | mbedtls_ecdh_free( &handshake->ecdh_ctx ); |
Paul Bakker | 61d113b | 2013-07-04 11:51:43 +0200 | [diff] [blame] | 3620 | #endif |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 3621 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 3622 | mbedtls_ecjpake_free( &handshake->ecjpake_ctx ); |
Manuel Pégourié-Gonnard | 77c0646 | 2015-09-17 13:59:49 +0200 | [diff] [blame] | 3623 | #if defined(MBEDTLS_SSL_CLI_C) |
| 3624 | mbedtls_free( handshake->ecjpake_cache ); |
| 3625 | handshake->ecjpake_cache = NULL; |
| 3626 | handshake->ecjpake_cache_len = 0; |
| 3627 | #endif |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 3628 | #endif |
Paul Bakker | 61d113b | 2013-07-04 11:51:43 +0200 | [diff] [blame] | 3629 | |
Janos Follath | 4ae5c29 | 2016-02-10 11:27:43 +0000 | [diff] [blame] | 3630 | #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ |
| 3631 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 3632 | /* explicit void pointer cast for buggy MS compiler */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3633 | mbedtls_free( (void *) handshake->curves ); |
Manuel Pégourié-Gonnard | d09453c | 2013-09-23 19:11:32 +0200 | [diff] [blame] | 3634 | #endif |
| 3635 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3636 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 3637 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 3638 | if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) ) |
| 3639 | { |
| 3640 | /* The maintenance of the external PSK key slot is the |
| 3641 | * user's responsibility. */ |
| 3642 | if( ssl->handshake->psk_opaque_is_internal ) |
| 3643 | { |
| 3644 | psa_destroy_key( ssl->handshake->psk_opaque ); |
| 3645 | ssl->handshake->psk_opaque_is_internal = 0; |
| 3646 | } |
| 3647 | ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT; |
| 3648 | } |
Neil Armstrong | e952a30 | 2022-05-03 10:22:14 +0200 | [diff] [blame] | 3649 | #else |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 3650 | if( handshake->psk != NULL ) |
| 3651 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 3652 | mbedtls_platform_zeroize( handshake->psk, handshake->psk_len ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 3653 | mbedtls_free( handshake->psk ); |
| 3654 | } |
Neil Armstrong | e952a30 | 2022-05-03 10:22:14 +0200 | [diff] [blame] | 3655 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 3656 | #endif |
| 3657 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3658 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 3659 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Manuel Pégourié-Gonnard | 8372454 | 2013-09-24 22:30:56 +0200 | [diff] [blame] | 3660 | /* |
| 3661 | * Free only the linked list wrapper, not the keys themselves |
| 3662 | * since the belong to the SNI callback |
| 3663 | */ |
Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 3664 | ssl_key_cert_free( handshake->sni_key_cert ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3665 | #endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
Manuel Pégourié-Gonnard | 705fcca | 2013-09-23 20:04:20 +0200 | [diff] [blame] | 3666 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3667 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Manuel Pégourié-Gonnard | 6b7301c | 2017-08-15 12:08:45 +0200 | [diff] [blame] | 3668 | mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx ); |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 3669 | if( handshake->ecrs_peer_cert != NULL ) |
| 3670 | { |
| 3671 | mbedtls_x509_crt_free( handshake->ecrs_peer_cert ); |
| 3672 | mbedtls_free( handshake->ecrs_peer_cert ); |
| 3673 | } |
Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 3674 | #endif |
| 3675 | |
Hanno Becker | 7517312 | 2019-02-06 16:18:31 +0000 | [diff] [blame] | 3676 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 3677 | !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 3678 | mbedtls_pk_free( &handshake->peer_pubkey ); |
| 3679 | #endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 3680 | |
XiaokangQian | 9b93c0d | 2022-02-09 06:02:25 +0000 | [diff] [blame] | 3681 | #if defined(MBEDTLS_SSL_CLI_C) && \ |
| 3682 | ( defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3) ) |
| 3683 | mbedtls_free( handshake->cookie ); |
| 3684 | #endif /* MBEDTLS_SSL_CLI_C && |
| 3685 | ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */ |
XiaokangQian | 8499b6c | 2022-01-27 09:00:11 +0000 | [diff] [blame] | 3686 | |
| 3687 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 533ab5f | 2020-02-05 10:49:13 +0000 | [diff] [blame] | 3688 | mbedtls_ssl_flight_free( handshake->flight ); |
| 3689 | mbedtls_ssl_buffering_free( ssl ); |
XiaokangQian | 8499b6c | 2022-01-27 09:00:11 +0000 | [diff] [blame] | 3690 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 3691 | |
Ronald Cron | f12b81d | 2022-03-15 10:42:41 +0100 | [diff] [blame] | 3692 | #if defined(MBEDTLS_ECDH_C) && \ |
| 3693 | ( defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3) ) |
Neil Armstrong | f716a70 | 2022-04-04 11:23:46 +0200 | [diff] [blame] | 3694 | if( handshake->ecdh_psa_privkey_is_external == 0 ) |
Neil Armstrong | 8113d25 | 2022-03-23 10:57:04 +0100 | [diff] [blame] | 3695 | psa_destroy_key( handshake->ecdh_psa_privkey ); |
Hanno Becker | 4a63ed4 | 2019-01-08 11:39:35 +0000 | [diff] [blame] | 3696 | #endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */ |
| 3697 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3698 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | a1a568c | 2021-11-09 10:17:21 +0800 | [diff] [blame] | 3699 | mbedtls_ssl_transform_free( handshake->transform_handshake ); |
| 3700 | mbedtls_ssl_transform_free( handshake->transform_earlydata ); |
Jerry Yu | ba9c727 | 2021-10-30 11:54:10 +0800 | [diff] [blame] | 3701 | mbedtls_free( handshake->transform_earlydata ); |
| 3702 | mbedtls_free( handshake->transform_handshake ); |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3703 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Jerry Yu | ba9c727 | 2021-10-30 11:54:10 +0800 | [diff] [blame] | 3704 | |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 3705 | |
| 3706 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 3707 | /* If the buffers are too big - reallocate. Because of the way Mbed TLS |
| 3708 | * processes datagrams and the fact that a datagram is allowed to have |
| 3709 | * several records in it, it is possible that the I/O buffers are not |
| 3710 | * empty at this stage */ |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 3711 | handle_buffer_resizing( ssl, 1, mbedtls_ssl_get_input_buflen( ssl ), |
| 3712 | mbedtls_ssl_get_output_buflen( ssl ) ); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 3713 | #endif |
Hanno Becker | 3aa186f | 2021-08-10 09:24:19 +0100 | [diff] [blame] | 3714 | |
Jerry Yu | ba9c727 | 2021-10-30 11:54:10 +0800 | [diff] [blame] | 3715 | /* mbedtls_platform_zeroize MUST be last one in this function */ |
| 3716 | mbedtls_platform_zeroize( handshake, |
| 3717 | sizeof( mbedtls_ssl_handshake_params ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3718 | } |
| 3719 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3720 | void mbedtls_ssl_session_free( mbedtls_ssl_session *session ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3721 | { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3722 | if( session == NULL ) |
| 3723 | return; |
| 3724 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3725 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Hanno Becker | 1294a0b | 2019-02-05 12:38:15 +0000 | [diff] [blame] | 3726 | ssl_clear_peer_cert( session ); |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 3727 | #endif |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 3728 | |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 3729 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
Xiaokang Qian | bc663a0 | 2022-10-09 11:14:39 +0000 | [diff] [blame] | 3730 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \ |
| 3731 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Xiaokang Qian | 281fd1b | 2022-09-20 11:35:41 +0000 | [diff] [blame] | 3732 | mbedtls_free( session->hostname ); |
| 3733 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3734 | mbedtls_free( session->ticket ); |
Paul Bakker | a503a63 | 2013-08-14 13:48:06 +0200 | [diff] [blame] | 3735 | #endif |
Manuel Pégourié-Gonnard | 75d4401 | 2013-08-02 14:44:04 +0200 | [diff] [blame] | 3736 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 3737 | mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3738 | } |
| 3739 | |
Manuel Pégourié-Gonnard | 5c0e377 | 2019-07-23 16:13:17 +0200 | [diff] [blame] | 3740 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 3741 | |
| 3742 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 3743 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u |
| 3744 | #else |
| 3745 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u |
| 3746 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 3747 | |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 3748 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 3749 | |
| 3750 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 3751 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u |
| 3752 | #else |
| 3753 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u |
| 3754 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
| 3755 | |
| 3756 | #if defined(MBEDTLS_SSL_ALPN) |
| 3757 | #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u |
| 3758 | #else |
| 3759 | #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u |
| 3760 | #endif /* MBEDTLS_SSL_ALPN */ |
| 3761 | |
| 3762 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0 |
| 3763 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1 |
| 3764 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2 |
| 3765 | #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3 |
| 3766 | |
| 3767 | #define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \ |
| 3768 | ( (uint32_t) ( \ |
| 3769 | ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \ |
| 3770 | ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \ |
| 3771 | ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \ |
| 3772 | ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \ |
| 3773 | 0u ) ) |
| 3774 | |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3775 | static unsigned char ssl_serialized_context_header[] = { |
| 3776 | MBEDTLS_VERSION_MAJOR, |
| 3777 | MBEDTLS_VERSION_MINOR, |
| 3778 | MBEDTLS_VERSION_PATCH, |
Joe Subbiani | 2194dc4 | 2021-07-14 12:31:31 +0100 | [diff] [blame] | 3779 | MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ), |
| 3780 | MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ), |
| 3781 | MBEDTLS_BYTE_2( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ), |
| 3782 | MBEDTLS_BYTE_1( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ), |
| 3783 | MBEDTLS_BYTE_0( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ), |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3784 | }; |
| 3785 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3786 | /* |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 3787 | * Serialize a full SSL context |
Manuel Pégourié-Gonnard | 00400c2 | 2019-07-10 14:58:45 +0200 | [diff] [blame] | 3788 | * |
| 3789 | * The format of the serialized data is: |
| 3790 | * (in the presentation language of TLS, RFC 8446 section 3) |
| 3791 | * |
| 3792 | * // header |
| 3793 | * opaque mbedtls_version[3]; // major, minor, patch |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3794 | * opaque context_format[5]; // version-specific field determining |
Manuel Pégourié-Gonnard | 00400c2 | 2019-07-10 14:58:45 +0200 | [diff] [blame] | 3795 | * // the format of the remaining |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3796 | * // serialized data. |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 3797 | * Note: When updating the format, remember to keep these |
| 3798 | * version+format bytes. (We may make their size part of the API.) |
Manuel Pégourié-Gonnard | 00400c2 | 2019-07-10 14:58:45 +0200 | [diff] [blame] | 3799 | * |
| 3800 | * // session sub-structure |
| 3801 | * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save() |
| 3802 | * // transform sub-structure |
| 3803 | * uint8 random[64]; // ServerHello.random+ClientHello.random |
| 3804 | * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value |
| 3805 | * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use |
| 3806 | * // fields from ssl_context |
| 3807 | * uint32 badmac_seen; // DTLS: number of records with failing MAC |
| 3808 | * uint64 in_window_top; // DTLS: last validated record seq_num |
| 3809 | * uint64 in_window; // DTLS: bitmask for replay protection |
| 3810 | * uint8 disable_datagram_packing; // DTLS: only one record per datagram |
| 3811 | * uint64 cur_out_ctr; // Record layer: outgoing sequence number |
| 3812 | * uint16 mtu; // DTLS: path mtu (max outgoing fragment size) |
| 3813 | * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol |
| 3814 | * |
| 3815 | * Note that many fields of the ssl_context or sub-structures are not |
| 3816 | * serialized, as they fall in one of the following categories: |
| 3817 | * |
| 3818 | * 1. forced value (eg in_left must be 0) |
| 3819 | * 2. pointer to dynamically-allocated memory (eg session, transform) |
| 3820 | * 3. value can be re-derived from other data (eg session keys from MS) |
| 3821 | * 4. value was temporary (eg content of input buffer) |
| 3822 | * 5. value will be provided by the user again (eg I/O callbacks and context) |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 3823 | */ |
| 3824 | int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl, |
| 3825 | unsigned char *buf, |
| 3826 | size_t buf_len, |
| 3827 | size_t *olen ) |
| 3828 | { |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3829 | unsigned char *p = buf; |
| 3830 | size_t used = 0; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3831 | size_t session_len; |
| 3832 | int ret = 0; |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3833 | |
Manuel Pégourié-Gonnard | 1aaf669 | 2019-07-10 14:14:05 +0200 | [diff] [blame] | 3834 | /* |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3835 | * Enforce usage restrictions, see "return BAD_INPUT_DATA" in |
| 3836 | * this function's documentation. |
| 3837 | * |
| 3838 | * These are due to assumptions/limitations in the implementation. Some of |
| 3839 | * them are likely to stay (no handshake in progress) some might go away |
| 3840 | * (only DTLS) but are currently used to simplify the implementation. |
Manuel Pégourié-Gonnard | 1aaf669 | 2019-07-10 14:14:05 +0200 | [diff] [blame] | 3841 | */ |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3842 | /* The initial handshake must be over */ |
Paul Elliott | 27b0d94 | 2022-03-18 21:55:32 +0000 | [diff] [blame] | 3843 | if( mbedtls_ssl_is_handshake_over( ssl ) == 0 ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3844 | { |
| 3845 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Initial handshake isn't over" ) ); |
Manuel Pégourié-Gonnard | 1aaf669 | 2019-07-10 14:14:05 +0200 | [diff] [blame] | 3846 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3847 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3848 | if( ssl->handshake != NULL ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3849 | { |
| 3850 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Handshake isn't completed" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3851 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3852 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3853 | /* Double-check that sub-structures are indeed ready */ |
| 3854 | if( ssl->transform == NULL || ssl->session == NULL ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3855 | { |
| 3856 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Serialised structures aren't ready" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3857 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3858 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3859 | /* There must be no pending incoming or outgoing data */ |
| 3860 | if( mbedtls_ssl_check_pending( ssl ) != 0 ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3861 | { |
| 3862 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending incoming data" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3863 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3864 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3865 | if( ssl->out_left != 0 ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3866 | { |
| 3867 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending outgoing data" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3868 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3869 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3870 | /* Protocol must be DLTS, not TLS */ |
| 3871 | if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3872 | { |
| 3873 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only DTLS is supported" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3874 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3875 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3876 | /* Version must be 1.2 */ |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 3877 | if( ssl->tls_version != MBEDTLS_SSL_VERSION_TLS1_2 ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3878 | { |
| 3879 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only version 1.2 supported" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3880 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3881 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3882 | /* We must be using an AEAD ciphersuite */ |
| 3883 | if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3884 | { |
| 3885 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only AEAD ciphersuites supported" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3886 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3887 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3888 | /* Renegotiation must not be enabled */ |
| 3889 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 3890 | if( ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3891 | { |
| 3892 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Renegotiation must not be enabled" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3893 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3894 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3895 | #endif |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 3896 | |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3897 | /* |
| 3898 | * Version and format identifier |
| 3899 | */ |
| 3900 | used += sizeof( ssl_serialized_context_header ); |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 3901 | |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3902 | if( used <= buf_len ) |
| 3903 | { |
| 3904 | memcpy( p, ssl_serialized_context_header, |
| 3905 | sizeof( ssl_serialized_context_header ) ); |
| 3906 | p += sizeof( ssl_serialized_context_header ); |
| 3907 | } |
| 3908 | |
| 3909 | /* |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3910 | * Session (length + data) |
| 3911 | */ |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 3912 | ret = ssl_session_save( ssl->session, 1, NULL, 0, &session_len ); |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3913 | if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ) |
| 3914 | return( ret ); |
| 3915 | |
| 3916 | used += 4 + session_len; |
| 3917 | if( used <= buf_len ) |
| 3918 | { |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 3919 | MBEDTLS_PUT_UINT32_BE( session_len, p, 0 ); |
| 3920 | p += 4; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3921 | |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 3922 | ret = ssl_session_save( ssl->session, 1, |
| 3923 | p, session_len, &session_len ); |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3924 | if( ret != 0 ) |
| 3925 | return( ret ); |
| 3926 | |
| 3927 | p += session_len; |
| 3928 | } |
| 3929 | |
| 3930 | /* |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 3931 | * Transform |
| 3932 | */ |
| 3933 | used += sizeof( ssl->transform->randbytes ); |
| 3934 | if( used <= buf_len ) |
| 3935 | { |
| 3936 | memcpy( p, ssl->transform->randbytes, |
| 3937 | sizeof( ssl->transform->randbytes ) ); |
| 3938 | p += sizeof( ssl->transform->randbytes ); |
| 3939 | } |
| 3940 | |
| 3941 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 3942 | used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len; |
| 3943 | if( used <= buf_len ) |
| 3944 | { |
| 3945 | *p++ = ssl->transform->in_cid_len; |
| 3946 | memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len ); |
| 3947 | p += ssl->transform->in_cid_len; |
| 3948 | |
| 3949 | *p++ = ssl->transform->out_cid_len; |
| 3950 | memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len ); |
| 3951 | p += ssl->transform->out_cid_len; |
| 3952 | } |
| 3953 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 3954 | |
| 3955 | /* |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3956 | * Saved fields from top-level ssl_context structure |
| 3957 | */ |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3958 | used += 4; |
| 3959 | if( used <= buf_len ) |
| 3960 | { |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 3961 | MBEDTLS_PUT_UINT32_BE( ssl->badmac_seen, p, 0 ); |
| 3962 | p += 4; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3963 | } |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3964 | |
| 3965 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 3966 | used += 16; |
| 3967 | if( used <= buf_len ) |
| 3968 | { |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 3969 | MBEDTLS_PUT_UINT64_BE( ssl->in_window_top, p, 0 ); |
| 3970 | p += 8; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3971 | |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 3972 | MBEDTLS_PUT_UINT64_BE( ssl->in_window, p, 0 ); |
| 3973 | p += 8; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3974 | } |
| 3975 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
| 3976 | |
| 3977 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3978 | used += 1; |
| 3979 | if( used <= buf_len ) |
| 3980 | { |
| 3981 | *p++ = ssl->disable_datagram_packing; |
| 3982 | } |
| 3983 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 3984 | |
Jerry Yu | ae0b2e2 | 2021-10-08 15:21:19 +0800 | [diff] [blame] | 3985 | used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3986 | if( used <= buf_len ) |
| 3987 | { |
Jerry Yu | ae0b2e2 | 2021-10-08 15:21:19 +0800 | [diff] [blame] | 3988 | memcpy( p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN ); |
| 3989 | p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3990 | } |
| 3991 | |
| 3992 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3993 | used += 2; |
| 3994 | if( used <= buf_len ) |
| 3995 | { |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 3996 | MBEDTLS_PUT_UINT16_BE( ssl->mtu, p, 0 ); |
| 3997 | p += 2; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3998 | } |
| 3999 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 4000 | |
| 4001 | #if defined(MBEDTLS_SSL_ALPN) |
| 4002 | { |
| 4003 | const uint8_t alpn_len = ssl->alpn_chosen |
Manuel Pégourié-Gonnard | f041f4e | 2019-07-24 00:58:27 +0200 | [diff] [blame] | 4004 | ? (uint8_t) strlen( ssl->alpn_chosen ) |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4005 | : 0; |
| 4006 | |
| 4007 | used += 1 + alpn_len; |
| 4008 | if( used <= buf_len ) |
| 4009 | { |
| 4010 | *p++ = alpn_len; |
| 4011 | |
| 4012 | if( ssl->alpn_chosen != NULL ) |
| 4013 | { |
| 4014 | memcpy( p, ssl->alpn_chosen, alpn_len ); |
| 4015 | p += alpn_len; |
| 4016 | } |
| 4017 | } |
| 4018 | } |
| 4019 | #endif /* MBEDTLS_SSL_ALPN */ |
| 4020 | |
| 4021 | /* |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4022 | * Done |
| 4023 | */ |
| 4024 | *olen = used; |
| 4025 | |
| 4026 | if( used > buf_len ) |
| 4027 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 4028 | |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4029 | MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used ); |
| 4030 | |
Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 4031 | return( mbedtls_ssl_session_reset_int( ssl, 0 ) ); |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 4032 | } |
| 4033 | |
| 4034 | /* |
Manuel Pégourié-Gonnard | b9dfc9f | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 4035 | * Deserialize context, see mbedtls_ssl_context_save() for format. |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4036 | * |
| 4037 | * This internal version is wrapped by a public function that cleans up in |
| 4038 | * case of error. |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 4039 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 4040 | MBEDTLS_CHECK_RETURN_CRITICAL |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4041 | static int ssl_context_load( mbedtls_ssl_context *ssl, |
| 4042 | const unsigned char *buf, |
| 4043 | size_t len ) |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 4044 | { |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4045 | const unsigned char *p = buf; |
| 4046 | const unsigned char * const end = buf + len; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4047 | size_t session_len; |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 4048 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Andrzej Kurek | 2d59dbc | 2022-10-13 08:34:38 -0400 | [diff] [blame^] | 4049 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Andrzej Kurek | 894edde | 2022-09-29 06:31:14 -0400 | [diff] [blame] | 4050 | tls_prf_fn prf_func = NULL; |
Andrzej Kurek | 2d59dbc | 2022-10-13 08:34:38 -0400 | [diff] [blame^] | 4051 | #endif |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4052 | |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 4053 | /* |
| 4054 | * The context should have been freshly setup or reset. |
| 4055 | * Give the user an error in case of obvious misuse. |
Manuel Pégourié-Gonnard | 4ca930f | 2019-07-26 16:31:53 +0200 | [diff] [blame] | 4056 | * (Checking session is useful because it won't be NULL if we're |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 4057 | * renegotiating, or if the user mistakenly loaded a session first.) |
| 4058 | */ |
| 4059 | if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST || |
| 4060 | ssl->session != NULL ) |
| 4061 | { |
| 4062 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4063 | } |
| 4064 | |
| 4065 | /* |
| 4066 | * We can't check that the config matches the initial one, but we can at |
| 4067 | * least check it matches the requirements for serializing. |
| 4068 | */ |
| 4069 | if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM || |
Glenn Strauss | 2dfcea2 | 2022-03-14 17:26:42 -0400 | [diff] [blame] | 4070 | ssl->conf->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2 || |
| 4071 | ssl->conf->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 || |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 4072 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | 9a96fd7 | 2019-07-23 17:11:24 +0200 | [diff] [blame] | 4073 | ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED || |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 4074 | #endif |
Manuel Pégourié-Gonnard | 9a96fd7 | 2019-07-23 17:11:24 +0200 | [diff] [blame] | 4075 | 0 ) |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 4076 | { |
| 4077 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4078 | } |
| 4079 | |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4080 | MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len ); |
| 4081 | |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4082 | /* |
| 4083 | * Check version identifier |
| 4084 | */ |
| 4085 | if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) ) |
| 4086 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4087 | |
| 4088 | if( memcmp( p, ssl_serialized_context_header, |
| 4089 | sizeof( ssl_serialized_context_header ) ) != 0 ) |
| 4090 | { |
| 4091 | return( MBEDTLS_ERR_SSL_VERSION_MISMATCH ); |
| 4092 | } |
| 4093 | p += sizeof( ssl_serialized_context_header ); |
| 4094 | |
| 4095 | /* |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4096 | * Session |
| 4097 | */ |
| 4098 | if( (size_t)( end - p ) < 4 ) |
| 4099 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4100 | |
| 4101 | session_len = ( (size_t) p[0] << 24 ) | |
| 4102 | ( (size_t) p[1] << 16 ) | |
| 4103 | ( (size_t) p[2] << 8 ) | |
| 4104 | ( (size_t) p[3] ); |
| 4105 | p += 4; |
| 4106 | |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 4107 | /* This has been allocated by ssl_handshake_init(), called by |
Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 4108 | * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */ |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 4109 | ssl->session = ssl->session_negotiate; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4110 | ssl->session_in = ssl->session; |
| 4111 | ssl->session_out = ssl->session; |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 4112 | ssl->session_negotiate = NULL; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4113 | |
| 4114 | if( (size_t)( end - p ) < session_len ) |
| 4115 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4116 | |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 4117 | ret = ssl_session_load( ssl->session, 1, p, session_len ); |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4118 | if( ret != 0 ) |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 4119 | { |
| 4120 | mbedtls_ssl_session_free( ssl->session ); |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4121 | return( ret ); |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 4122 | } |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4123 | |
| 4124 | p += session_len; |
| 4125 | |
| 4126 | /* |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4127 | * Transform |
| 4128 | */ |
| 4129 | |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 4130 | /* This has been allocated by ssl_handshake_init(), called by |
Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 4131 | * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */ |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 4132 | ssl->transform = ssl->transform_negotiate; |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4133 | ssl->transform_in = ssl->transform; |
| 4134 | ssl->transform_out = ssl->transform; |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 4135 | ssl->transform_negotiate = NULL; |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4136 | |
Andrzej Kurek | 2d59dbc | 2022-10-13 08:34:38 -0400 | [diff] [blame^] | 4137 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Andrzej Kurek | 894edde | 2022-09-29 06:31:14 -0400 | [diff] [blame] | 4138 | prf_func = ssl_tls12prf_from_cs( ssl->session->ciphersuite ); |
| 4139 | if( prf_func == NULL ) |
| 4140 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4141 | |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4142 | /* Read random bytes and populate structure */ |
| 4143 | if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) ) |
| 4144 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Andrzej Kurek | 2d59dbc | 2022-10-13 08:34:38 -0400 | [diff] [blame^] | 4145 | |
Hanno Becker | bd25755 | 2021-03-22 06:59:27 +0000 | [diff] [blame] | 4146 | ret = ssl_tls12_populate_transform( ssl->transform, |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4147 | ssl->session->ciphersuite, |
| 4148 | ssl->session->master, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 4149 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4150 | ssl->session->encrypt_then_mac, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 4151 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Andrzej Kurek | 894edde | 2022-09-29 06:31:14 -0400 | [diff] [blame] | 4152 | prf_func, |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4153 | p, /* currently pointing to randbytes */ |
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 4154 | MBEDTLS_SSL_VERSION_TLS1_2, /* (D)TLS 1.2 is forced */ |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4155 | ssl->conf->endpoint, |
| 4156 | ssl ); |
| 4157 | if( ret != 0 ) |
| 4158 | return( ret ); |
Jerry Yu | 840fbb2 | 2022-02-17 14:59:29 +0800 | [diff] [blame] | 4159 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4160 | p += sizeof( ssl->transform->randbytes ); |
| 4161 | |
| 4162 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 4163 | /* Read connection IDs and store them */ |
| 4164 | if( (size_t)( end - p ) < 1 ) |
| 4165 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4166 | |
| 4167 | ssl->transform->in_cid_len = *p++; |
| 4168 | |
Manuel Pégourié-Gonnard | 5ea13b8 | 2019-07-23 15:02:54 +0200 | [diff] [blame] | 4169 | if( (size_t)( end - p ) < ssl->transform->in_cid_len + 1u ) |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4170 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4171 | |
| 4172 | memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len ); |
| 4173 | p += ssl->transform->in_cid_len; |
| 4174 | |
| 4175 | ssl->transform->out_cid_len = *p++; |
| 4176 | |
| 4177 | if( (size_t)( end - p ) < ssl->transform->out_cid_len ) |
| 4178 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4179 | |
| 4180 | memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len ); |
| 4181 | p += ssl->transform->out_cid_len; |
| 4182 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 4183 | |
| 4184 | /* |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4185 | * Saved fields from top-level ssl_context structure |
| 4186 | */ |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4187 | if( (size_t)( end - p ) < 4 ) |
| 4188 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4189 | |
| 4190 | ssl->badmac_seen = ( (uint32_t) p[0] << 24 ) | |
| 4191 | ( (uint32_t) p[1] << 16 ) | |
| 4192 | ( (uint32_t) p[2] << 8 ) | |
| 4193 | ( (uint32_t) p[3] ); |
| 4194 | p += 4; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4195 | |
| 4196 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 4197 | if( (size_t)( end - p ) < 16 ) |
| 4198 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4199 | |
| 4200 | ssl->in_window_top = ( (uint64_t) p[0] << 56 ) | |
| 4201 | ( (uint64_t) p[1] << 48 ) | |
| 4202 | ( (uint64_t) p[2] << 40 ) | |
| 4203 | ( (uint64_t) p[3] << 32 ) | |
| 4204 | ( (uint64_t) p[4] << 24 ) | |
| 4205 | ( (uint64_t) p[5] << 16 ) | |
| 4206 | ( (uint64_t) p[6] << 8 ) | |
| 4207 | ( (uint64_t) p[7] ); |
| 4208 | p += 8; |
| 4209 | |
| 4210 | ssl->in_window = ( (uint64_t) p[0] << 56 ) | |
| 4211 | ( (uint64_t) p[1] << 48 ) | |
| 4212 | ( (uint64_t) p[2] << 40 ) | |
| 4213 | ( (uint64_t) p[3] << 32 ) | |
| 4214 | ( (uint64_t) p[4] << 24 ) | |
| 4215 | ( (uint64_t) p[5] << 16 ) | |
| 4216 | ( (uint64_t) p[6] << 8 ) | |
| 4217 | ( (uint64_t) p[7] ); |
| 4218 | p += 8; |
| 4219 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
| 4220 | |
| 4221 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4222 | if( (size_t)( end - p ) < 1 ) |
| 4223 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4224 | |
| 4225 | ssl->disable_datagram_packing = *p++; |
| 4226 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 4227 | |
Jerry Yu | d9a94fe | 2021-09-28 18:58:59 +0800 | [diff] [blame] | 4228 | if( (size_t)( end - p ) < sizeof( ssl->cur_out_ctr ) ) |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4229 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jerry Yu | d9a94fe | 2021-09-28 18:58:59 +0800 | [diff] [blame] | 4230 | memcpy( ssl->cur_out_ctr, p, sizeof( ssl->cur_out_ctr ) ); |
| 4231 | p += sizeof( ssl->cur_out_ctr ); |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4232 | |
| 4233 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4234 | if( (size_t)( end - p ) < 2 ) |
| 4235 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4236 | |
| 4237 | ssl->mtu = ( p[0] << 8 ) | p[1]; |
| 4238 | p += 2; |
| 4239 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 4240 | |
| 4241 | #if defined(MBEDTLS_SSL_ALPN) |
| 4242 | { |
| 4243 | uint8_t alpn_len; |
| 4244 | const char **cur; |
| 4245 | |
| 4246 | if( (size_t)( end - p ) < 1 ) |
| 4247 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4248 | |
| 4249 | alpn_len = *p++; |
| 4250 | |
| 4251 | if( alpn_len != 0 && ssl->conf->alpn_list != NULL ) |
| 4252 | { |
| 4253 | /* alpn_chosen should point to an item in the configured list */ |
| 4254 | for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ ) |
| 4255 | { |
| 4256 | if( strlen( *cur ) == alpn_len && |
| 4257 | memcmp( p, cur, alpn_len ) == 0 ) |
| 4258 | { |
| 4259 | ssl->alpn_chosen = *cur; |
| 4260 | break; |
| 4261 | } |
| 4262 | } |
| 4263 | } |
| 4264 | |
| 4265 | /* can only happen on conf mismatch */ |
| 4266 | if( alpn_len != 0 && ssl->alpn_chosen == NULL ) |
| 4267 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4268 | |
| 4269 | p += alpn_len; |
| 4270 | } |
| 4271 | #endif /* MBEDTLS_SSL_ALPN */ |
| 4272 | |
| 4273 | /* |
Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 4274 | * Forced fields from top-level ssl_context structure |
| 4275 | * |
| 4276 | * Most of them already set to the correct value by mbedtls_ssl_init() and |
| 4277 | * mbedtls_ssl_reset(), so we only need to set the remaining ones. |
| 4278 | */ |
| 4279 | ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER; |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 4280 | ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 4281 | |
Hanno Becker | 361b10d | 2019-08-30 10:42:49 +0100 | [diff] [blame] | 4282 | /* Adjust pointers for header fields of outgoing records to |
| 4283 | * the given transform, accounting for explicit IV and CID. */ |
Hanno Becker | 3e6f8ab | 2020-02-05 10:40:57 +0000 | [diff] [blame] | 4284 | mbedtls_ssl_update_out_pointers( ssl, ssl->transform ); |
Hanno Becker | 361b10d | 2019-08-30 10:42:49 +0100 | [diff] [blame] | 4285 | |
Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 4286 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4287 | ssl->in_epoch = 1; |
| 4288 | #endif |
| 4289 | |
| 4290 | /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated, |
| 4291 | * which we don't want - otherwise we'd end up freeing the wrong transform |
Hanno Becker | ce5f5fd | 2020-02-05 10:47:44 +0000 | [diff] [blame] | 4292 | * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform() |
| 4293 | * inappropriately. */ |
Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 4294 | if( ssl->handshake != NULL ) |
| 4295 | { |
| 4296 | mbedtls_ssl_handshake_free( ssl ); |
| 4297 | mbedtls_free( ssl->handshake ); |
| 4298 | ssl->handshake = NULL; |
| 4299 | } |
| 4300 | |
| 4301 | /* |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4302 | * Done - should have consumed entire buffer |
| 4303 | */ |
| 4304 | if( p != end ) |
| 4305 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 4306 | |
| 4307 | return( 0 ); |
| 4308 | } |
| 4309 | |
| 4310 | /* |
Manuel Pégourié-Gonnard | b9dfc9f | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 4311 | * Deserialize context: public wrapper for error cleaning |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4312 | */ |
| 4313 | int mbedtls_ssl_context_load( mbedtls_ssl_context *context, |
| 4314 | const unsigned char *buf, |
| 4315 | size_t len ) |
| 4316 | { |
| 4317 | int ret = ssl_context_load( context, buf, len ); |
| 4318 | |
| 4319 | if( ret != 0 ) |
| 4320 | mbedtls_ssl_free( context ); |
| 4321 | |
| 4322 | return( ret ); |
| 4323 | } |
Manuel Pégourié-Gonnard | 5c0e377 | 2019-07-23 16:13:17 +0200 | [diff] [blame] | 4324 | #endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */ |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4325 | |
| 4326 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4327 | * Free an SSL context |
| 4328 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4329 | void mbedtls_ssl_free( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4330 | { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 4331 | if( ssl == NULL ) |
| 4332 | return; |
| 4333 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4334 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4335 | |
Manuel Pégourié-Gonnard | 7ee6f0e | 2014-02-13 10:54:07 +0100 | [diff] [blame] | 4336 | if( ssl->out_buf != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4337 | { |
sander-visser | b8aa207 | 2020-05-06 22:05:13 +0200 | [diff] [blame] | 4338 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 4339 | size_t out_buf_len = ssl->out_buf_len; |
| 4340 | #else |
| 4341 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; |
| 4342 | #endif |
| 4343 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 4344 | mbedtls_platform_zeroize( ssl->out_buf, out_buf_len ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4345 | mbedtls_free( ssl->out_buf ); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 4346 | ssl->out_buf = NULL; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4347 | } |
| 4348 | |
Manuel Pégourié-Gonnard | 7ee6f0e | 2014-02-13 10:54:07 +0100 | [diff] [blame] | 4349 | if( ssl->in_buf != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4350 | { |
sander-visser | b8aa207 | 2020-05-06 22:05:13 +0200 | [diff] [blame] | 4351 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 4352 | size_t in_buf_len = ssl->in_buf_len; |
| 4353 | #else |
| 4354 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; |
| 4355 | #endif |
| 4356 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 4357 | mbedtls_platform_zeroize( ssl->in_buf, in_buf_len ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4358 | mbedtls_free( ssl->in_buf ); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 4359 | ssl->in_buf = NULL; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4360 | } |
| 4361 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4362 | if( ssl->transform ) |
| 4363 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4364 | mbedtls_ssl_transform_free( ssl->transform ); |
| 4365 | mbedtls_free( ssl->transform ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4366 | } |
| 4367 | |
| 4368 | if( ssl->handshake ) |
| 4369 | { |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 4370 | mbedtls_ssl_handshake_free( ssl ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4371 | mbedtls_ssl_transform_free( ssl->transform_negotiate ); |
| 4372 | mbedtls_ssl_session_free( ssl->session_negotiate ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4373 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4374 | mbedtls_free( ssl->handshake ); |
| 4375 | mbedtls_free( ssl->transform_negotiate ); |
| 4376 | mbedtls_free( ssl->session_negotiate ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4377 | } |
| 4378 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4379 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Hanno Becker | 3aa186f | 2021-08-10 09:24:19 +0100 | [diff] [blame] | 4380 | mbedtls_ssl_transform_free( ssl->transform_application ); |
| 4381 | mbedtls_free( ssl->transform_application ); |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4382 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | 3aa186f | 2021-08-10 09:24:19 +0100 | [diff] [blame] | 4383 | |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 4384 | if( ssl->session ) |
| 4385 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4386 | mbedtls_ssl_session_free( ssl->session ); |
| 4387 | mbedtls_free( ssl->session ); |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 4388 | } |
| 4389 | |
Manuel Pégourié-Gonnard | 55fab2d | 2015-05-11 16:15:19 +0200 | [diff] [blame] | 4390 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 4391 | if( ssl->hostname != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4392 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 4393 | mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4394 | mbedtls_free( ssl->hostname ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4395 | } |
Paul Bakker | 0be444a | 2013-08-27 21:55:01 +0200 | [diff] [blame] | 4396 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4397 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 4398 | #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4399 | mbedtls_free( ssl->cli_id ); |
Manuel Pégourié-Gonnard | 43c0218 | 2014-07-22 17:32:01 +0200 | [diff] [blame] | 4400 | #endif |
| 4401 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4402 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) ); |
Paul Bakker | 2da561c | 2009-02-05 18:00:28 +0000 | [diff] [blame] | 4403 | |
Paul Bakker | 86f04f4 | 2013-02-14 11:20:09 +0100 | [diff] [blame] | 4404 | /* Actually clear after last debug message */ |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 4405 | mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4406 | } |
| 4407 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4408 | /* |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 4409 | * Initialize mbedtls_ssl_config |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4410 | */ |
| 4411 | void mbedtls_ssl_config_init( mbedtls_ssl_config *conf ) |
| 4412 | { |
| 4413 | memset( conf, 0, sizeof( mbedtls_ssl_config ) ); |
| 4414 | } |
| 4415 | |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4416 | /* The selection should be the same as mbedtls_x509_crt_profile_default in |
| 4417 | * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters: |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4418 | * curves with a lower resource usage come first. |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4419 | * See the documentation of mbedtls_ssl_conf_curves() for what we promise |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4420 | * about this list. |
| 4421 | */ |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4422 | static uint16_t ssl_preset_default_groups[] = { |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4423 | #if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4424 | MBEDTLS_SSL_IANA_TLS_GROUP_X25519, |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4425 | #endif |
| 4426 | #if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4427 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1, |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4428 | #endif |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4429 | #if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4430 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4431 | #endif |
| 4432 | #if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4433 | MBEDTLS_SSL_IANA_TLS_GROUP_X448, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4434 | #endif |
| 4435 | #if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4436 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4437 | #endif |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4438 | #if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4439 | MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1, |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4440 | #endif |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4441 | #if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4442 | MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4443 | #endif |
| 4444 | #if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4445 | MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4446 | #endif |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4447 | MBEDTLS_SSL_IANA_TLS_GROUP_NONE |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4448 | }; |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4449 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4450 | static int ssl_preset_suiteb_ciphersuites[] = { |
| 4451 | MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, |
| 4452 | MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, |
| 4453 | 0 |
| 4454 | }; |
| 4455 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 4456 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Hanno Becker | 9c6aa7b | 2021-08-10 13:50:43 +0100 | [diff] [blame] | 4457 | |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4458 | /* NOTICE: |
Jerry Yu | 0b994b8 | 2022-01-25 17:22:12 +0800 | [diff] [blame] | 4459 | * For ssl_preset_*_sig_algs and ssl_tls12_preset_*_sig_algs, the following |
Jerry Yu | 370e146 | 2022-01-25 10:36:53 +0800 | [diff] [blame] | 4460 | * rules SHOULD be upheld. |
| 4461 | * - No duplicate entries. |
| 4462 | * - But if there is a good reason, do not change the order of the algorithms. |
Jerry Yu | 09a99fc | 2022-07-28 14:22:17 +0800 | [diff] [blame] | 4463 | * - ssl_tls12_preset* is for TLS 1.2 use only. |
Jerry Yu | 370e146 | 2022-01-25 10:36:53 +0800 | [diff] [blame] | 4464 | * - ssl_preset_* is for TLS 1.3 only or hybrid TLS 1.3/1.2 handshakes. |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4465 | */ |
Hanno Becker | 9c6aa7b | 2021-08-10 13:50:43 +0100 | [diff] [blame] | 4466 | static uint16_t ssl_preset_default_sig_algs[] = { |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4467 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4468 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \ |
Jerry Yu | ed5e9f4 | 2022-01-26 11:21:34 +0800 | [diff] [blame] | 4469 | defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) |
| 4470 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256, |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4471 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA && |
Jerry Yu | ed5e9f4 | 2022-01-26 11:21:34 +0800 | [diff] [blame] | 4472 | MBEDTLS_ECP_DP_SECP256R1_ENABLED */ |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4473 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4474 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \ |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4475 | defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) |
| 4476 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384, |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4477 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&& |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4478 | MBEDTLS_ECP_DP_SECP384R1_ENABLED */ |
| 4479 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4480 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \ |
Jerry Yu | ed5e9f4 | 2022-01-26 11:21:34 +0800 | [diff] [blame] | 4481 | defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) |
| 4482 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512, |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4483 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&& |
Jerry Yu | ed5e9f4 | 2022-01-26 11:21:34 +0800 | [diff] [blame] | 4484 | MBEDTLS_ECP_DP_SECP521R1_ENABLED */ |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4485 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4486 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Jerry Yu | 9bb3ee4 | 2022-06-23 10:16:33 +0800 | [diff] [blame] | 4487 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512, |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4488 | #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT && MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA */ |
Jerry Yu | 9bb3ee4 | 2022-06-23 10:16:33 +0800 | [diff] [blame] | 4489 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4490 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Jerry Yu | 9bb3ee4 | 2022-06-23 10:16:33 +0800 | [diff] [blame] | 4491 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384, |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4492 | #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */ |
Jerry Yu | 9bb3ee4 | 2022-06-23 10:16:33 +0800 | [diff] [blame] | 4493 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4494 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Jerry Yu | 9bb3ee4 | 2022-06-23 10:16:33 +0800 | [diff] [blame] | 4495 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256, |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4496 | #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */ |
Jerry Yu | 9bb3ee4 | 2022-06-23 10:16:33 +0800 | [diff] [blame] | 4497 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4498 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Jerry Yu | 693a47a | 2022-06-23 14:02:28 +0800 | [diff] [blame] | 4499 | MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512, |
| 4500 | #endif /* MBEDTLS_RSA_C && MBEDTLS_SHA512_C */ |
| 4501 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4502 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Jerry Yu | 693a47a | 2022-06-23 14:02:28 +0800 | [diff] [blame] | 4503 | MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384, |
| 4504 | #endif /* MBEDTLS_RSA_C && MBEDTLS_SHA384_C */ |
| 4505 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4506 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Jerry Yu | 693a47a | 2022-06-23 14:02:28 +0800 | [diff] [blame] | 4507 | MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256, |
| 4508 | #endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */ |
| 4509 | |
Gabor Mezei | 15b95a6 | 2022-05-09 16:37:58 +0200 | [diff] [blame] | 4510 | MBEDTLS_TLS_SIG_NONE |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4511 | }; |
| 4512 | |
| 4513 | /* NOTICE: see above */ |
| 4514 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 4515 | static uint16_t ssl_tls12_preset_default_sig_algs[] = { |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4516 | #if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 4517 | #if defined(MBEDTLS_ECDSA_C) |
| 4518 | MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512 ), |
Jerry Yu | 713013f | 2022-01-17 18:16:35 +0800 | [diff] [blame] | 4519 | #endif |
Jerry Yu | 09a99fc | 2022-07-28 14:22:17 +0800 | [diff] [blame] | 4520 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) |
| 4521 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512, |
| 4522 | #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */ |
Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 4523 | #if defined(MBEDTLS_RSA_C) |
| 4524 | MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA512 ), |
| 4525 | #endif |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4526 | #endif /* MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/ |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4527 | #if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 4528 | #if defined(MBEDTLS_ECDSA_C) |
| 4529 | MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384 ), |
Jerry Yu | 11f0a9c | 2022-01-12 18:43:08 +0800 | [diff] [blame] | 4530 | #endif |
Jerry Yu | 09a99fc | 2022-07-28 14:22:17 +0800 | [diff] [blame] | 4531 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) |
| 4532 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384, |
| 4533 | #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */ |
Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 4534 | #if defined(MBEDTLS_RSA_C) |
| 4535 | MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ), |
| 4536 | #endif |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4537 | #endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/ |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4538 | #if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 4539 | #if defined(MBEDTLS_ECDSA_C) |
| 4540 | MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256 ), |
Jerry Yu | 11f0a9c | 2022-01-12 18:43:08 +0800 | [diff] [blame] | 4541 | #endif |
Jerry Yu | 09a99fc | 2022-07-28 14:22:17 +0800 | [diff] [blame] | 4542 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) |
| 4543 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256, |
| 4544 | #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */ |
Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 4545 | #if defined(MBEDTLS_RSA_C) |
| 4546 | MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ), |
| 4547 | #endif |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4548 | #endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/ |
Gabor Mezei | 15b95a6 | 2022-05-09 16:37:58 +0200 | [diff] [blame] | 4549 | MBEDTLS_TLS_SIG_NONE |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4550 | }; |
| 4551 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 4552 | /* NOTICE: see above */ |
| 4553 | static uint16_t ssl_preset_suiteb_sig_algs[] = { |
| 4554 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4555 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \ |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4556 | defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) |
| 4557 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256, |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4558 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA&& |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4559 | MBEDTLS_ECP_DP_SECP256R1_ENABLED */ |
| 4560 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4561 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \ |
Jerry Yu | 5303789 | 2022-01-25 11:02:06 +0800 | [diff] [blame] | 4562 | defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) |
| 4563 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384, |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4564 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&& |
Jerry Yu | 5303789 | 2022-01-25 11:02:06 +0800 | [diff] [blame] | 4565 | MBEDTLS_ECP_DP_SECP384R1_ENABLED */ |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4566 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4567 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4568 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256, |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4569 | #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/ |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4570 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4571 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Jerry Yu | 5303789 | 2022-01-25 11:02:06 +0800 | [diff] [blame] | 4572 | MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256, |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4573 | #endif /* MBEDTLS_RSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/ |
Jerry Yu | 5303789 | 2022-01-25 11:02:06 +0800 | [diff] [blame] | 4574 | |
Gabor Mezei | 15b95a6 | 2022-05-09 16:37:58 +0200 | [diff] [blame] | 4575 | MBEDTLS_TLS_SIG_NONE |
Jerry Yu | 713013f | 2022-01-17 18:16:35 +0800 | [diff] [blame] | 4576 | }; |
Jerry Yu | 6106fdc | 2022-01-12 16:36:14 +0800 | [diff] [blame] | 4577 | |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4578 | /* NOTICE: see above */ |
| 4579 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 4580 | static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = { |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4581 | #if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 4582 | #if defined(MBEDTLS_ECDSA_C) |
| 4583 | MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256 ), |
Jerry Yu | 713013f | 2022-01-17 18:16:35 +0800 | [diff] [blame] | 4584 | #endif |
Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 4585 | #if defined(MBEDTLS_RSA_C) |
| 4586 | MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ), |
| 4587 | #endif |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4588 | #endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/ |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4589 | #if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 4590 | #if defined(MBEDTLS_ECDSA_C) |
| 4591 | MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384 ), |
Jerry Yu | 18c833e | 2022-01-25 10:55:47 +0800 | [diff] [blame] | 4592 | #endif |
Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 4593 | #if defined(MBEDTLS_RSA_C) |
| 4594 | MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ), |
| 4595 | #endif |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4596 | #endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/ |
Gabor Mezei | 15b95a6 | 2022-05-09 16:37:58 +0200 | [diff] [blame] | 4597 | MBEDTLS_TLS_SIG_NONE |
Hanno Becker | 9c6aa7b | 2021-08-10 13:50:43 +0100 | [diff] [blame] | 4598 | }; |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4599 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 4600 | |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4601 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4602 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4603 | static uint16_t ssl_preset_suiteb_groups[] = { |
Jaeden Amero | d431104 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 4604 | #if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4605 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1, |
Jaeden Amero | d431104 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 4606 | #endif |
| 4607 | #if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4608 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1, |
Jaeden Amero | d431104 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 4609 | #endif |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4610 | MBEDTLS_SSL_IANA_TLS_GROUP_NONE |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4611 | }; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4612 | |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4613 | #if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4614 | /* Function for checking `ssl_preset_*_sig_algs` and `ssl_tls12_preset_*_sig_algs` |
Jerry Yu | 370e146 | 2022-01-25 10:36:53 +0800 | [diff] [blame] | 4615 | * to make sure there are no duplicated signature algorithm entries. */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 4616 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 4617 | static int ssl_check_no_sig_alg_duplication( uint16_t * sig_algs ) |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4618 | { |
| 4619 | size_t i, j; |
| 4620 | int ret = 0; |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4621 | |
Gabor Mezei | 15b95a6 | 2022-05-09 16:37:58 +0200 | [diff] [blame] | 4622 | for( i = 0; sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ ) |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4623 | { |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 4624 | for( j = 0; j < i; j++ ) |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4625 | { |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 4626 | if( sig_algs[i] != sig_algs[j] ) |
| 4627 | continue; |
| 4628 | mbedtls_printf( " entry(%04x,%" MBEDTLS_PRINTF_SIZET |
| 4629 | ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n", |
| 4630 | sig_algs[i], j, i ); |
| 4631 | ret = -1; |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4632 | } |
| 4633 | } |
| 4634 | return( ret ); |
| 4635 | } |
| 4636 | |
| 4637 | #endif /* MBEDTLS_DEBUG_C && MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 4638 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4639 | /* |
Tillmann Karras | 588ad50 | 2015-09-25 04:27:22 +0200 | [diff] [blame] | 4640 | * Load default in mbedtls_ssl_config |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4641 | */ |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 4642 | int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4643 | int endpoint, int transport, int preset ) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4644 | { |
Manuel Pégourié-Gonnard | 8b431fb | 2015-05-11 12:54:52 +0200 | [diff] [blame] | 4645 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 4646 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 8b431fb | 2015-05-11 12:54:52 +0200 | [diff] [blame] | 4647 | #endif |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4648 | |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4649 | #if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 4650 | if( ssl_check_no_sig_alg_duplication( ssl_preset_suiteb_sig_algs ) ) |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4651 | { |
| 4652 | mbedtls_printf( "ssl_preset_suiteb_sig_algs has duplicated entries\n" ); |
| 4653 | return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED ); |
| 4654 | } |
| 4655 | |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 4656 | if( ssl_check_no_sig_alg_duplication( ssl_preset_default_sig_algs ) ) |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4657 | { |
| 4658 | mbedtls_printf( "ssl_preset_default_sig_algs has duplicated entries\n" ); |
| 4659 | return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED ); |
| 4660 | } |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4661 | |
| 4662 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 4663 | if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_suiteb_sig_algs ) ) |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4664 | { |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4665 | mbedtls_printf( "ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n" ); |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4666 | return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED ); |
| 4667 | } |
| 4668 | |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 4669 | if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_default_sig_algs ) ) |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4670 | { |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4671 | mbedtls_printf( "ssl_tls12_preset_default_sig_algs has duplicated entries\n" ); |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4672 | return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED ); |
| 4673 | } |
| 4674 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4675 | #endif /* MBEDTLS_DEBUG_C && MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 4676 | |
Manuel Pégourié-Gonnard | 0de074f | 2015-05-14 12:58:01 +0200 | [diff] [blame] | 4677 | /* Use the functions here so that they are covered in tests, |
| 4678 | * but otherwise access member directly for efficiency */ |
| 4679 | mbedtls_ssl_conf_endpoint( conf, endpoint ); |
| 4680 | mbedtls_ssl_conf_transport( conf, transport ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4681 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4682 | /* |
| 4683 | * Things that are common to all presets |
| 4684 | */ |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 4685 | #if defined(MBEDTLS_SSL_CLI_C) |
| 4686 | if( endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 4687 | { |
| 4688 | conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED; |
| 4689 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 4690 | conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED; |
| 4691 | #endif |
| 4692 | } |
| 4693 | #endif |
| 4694 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4695 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 4696 | conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED; |
| 4697 | #endif |
| 4698 | |
| 4699 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 4700 | conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED; |
| 4701 | #endif |
| 4702 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 4703 | #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4704 | conf->f_cookie_write = ssl_cookie_write_dummy; |
| 4705 | conf->f_cookie_check = ssl_cookie_check_dummy; |
| 4706 | #endif |
| 4707 | |
| 4708 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 4709 | conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED; |
| 4710 | #endif |
| 4711 | |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 4712 | #if defined(MBEDTLS_SSL_SRV_C) |
| 4713 | conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED; |
TRodziewicz | 3946f79 | 2021-06-14 12:11:18 +0200 | [diff] [blame] | 4714 | conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER; |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 4715 | #endif |
| 4716 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4717 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4718 | conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN; |
| 4719 | conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX; |
| 4720 | #endif |
| 4721 | |
| 4722 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 4723 | conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT; |
Andres AG | 2196c7f | 2016-12-15 17:01:16 +0000 | [diff] [blame] | 4724 | memset( conf->renego_period, 0x00, 2 ); |
| 4725 | memset( conf->renego_period + 2, 0xFF, 6 ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4726 | #endif |
| 4727 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4728 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
Hanno Becker | e2defad | 2021-07-24 05:59:17 +0100 | [diff] [blame] | 4729 | if( endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 4730 | { |
| 4731 | const unsigned char dhm_p[] = |
| 4732 | MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN; |
| 4733 | const unsigned char dhm_g[] = |
| 4734 | MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN; |
Hanno Becker | 00d0a68 | 2017-10-04 13:14:29 +0100 | [diff] [blame] | 4735 | |
Hanno Becker | e2defad | 2021-07-24 05:59:17 +0100 | [diff] [blame] | 4736 | if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf, |
| 4737 | dhm_p, sizeof( dhm_p ), |
| 4738 | dhm_g, sizeof( dhm_g ) ) ) != 0 ) |
| 4739 | { |
| 4740 | return( ret ); |
| 4741 | } |
| 4742 | } |
Manuel Pégourié-Gonnard | bd990d6 | 2015-06-11 14:49:42 +0200 | [diff] [blame] | 4743 | #endif |
| 4744 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4745 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | d0766ec | 2022-09-22 10:46:57 +0800 | [diff] [blame] | 4746 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SESSION_TICKETS) |
Jerry Yu | 1ad7ace | 2022-08-09 13:28:39 +0800 | [diff] [blame] | 4747 | mbedtls_ssl_conf_new_session_tickets( |
| 4748 | conf, MBEDTLS_SSL_TLS1_3_DEFAULT_NEW_SESSION_TICKETS ); |
| 4749 | #endif |
Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 4750 | /* |
| 4751 | * Allow all TLS 1.3 key exchange modes by default. |
| 4752 | */ |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 4753 | conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL; |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4754 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 4755 | |
XiaokangQian | 4d3a604 | 2022-04-21 13:46:17 +0000 | [diff] [blame] | 4756 | if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 4757 | { |
Glenn Strauss | 2dfcea2 | 2022-03-14 17:26:42 -0400 | [diff] [blame] | 4758 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
XiaokangQian | 4d3a604 | 2022-04-21 13:46:17 +0000 | [diff] [blame] | 4759 | conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
XiaokangQian | 060d867 | 2022-04-21 09:24:56 +0000 | [diff] [blame] | 4760 | conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
XiaokangQian | 4d3a604 | 2022-04-21 13:46:17 +0000 | [diff] [blame] | 4761 | #else |
XiaokangQian | 060d867 | 2022-04-21 09:24:56 +0000 | [diff] [blame] | 4762 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
XiaokangQian | 060d867 | 2022-04-21 09:24:56 +0000 | [diff] [blame] | 4763 | #endif |
XiaokangQian | 4d3a604 | 2022-04-21 13:46:17 +0000 | [diff] [blame] | 4764 | } |
| 4765 | else |
| 4766 | { |
| 4767 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 4768 | if( endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 4769 | { |
| 4770 | conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 4771 | conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3; |
| 4772 | } |
| 4773 | else |
| 4774 | /* Hybrid TLS 1.2 / 1.3 is not supported on server side yet */ |
| 4775 | { |
| 4776 | conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 4777 | conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 4778 | } |
| 4779 | #elif defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 4780 | conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_3; |
| 4781 | conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3; |
| 4782 | #elif defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 4783 | conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 4784 | conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 4785 | #else |
| 4786 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 4787 | #endif |
| 4788 | } |
Glenn Strauss | 2dfcea2 | 2022-03-14 17:26:42 -0400 | [diff] [blame] | 4789 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4790 | /* |
| 4791 | * Preset-specific defaults |
| 4792 | */ |
| 4793 | switch( preset ) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4794 | { |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4795 | /* |
| 4796 | * NSA Suite B |
| 4797 | */ |
| 4798 | case MBEDTLS_SSL_PRESET_SUITEB: |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4799 | |
Hanno Becker | d60b6c6 | 2021-04-29 12:04:11 +0100 | [diff] [blame] | 4800 | conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4801 | |
| 4802 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 4803 | conf->cert_profile = &mbedtls_x509_crt_profile_suiteb; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4804 | #endif |
| 4805 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 4806 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4807 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 4808 | if( mbedtls_ssl_conf_is_tls12_only( conf ) ) |
| 4809 | conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs; |
| 4810 | else |
| 4811 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 4812 | conf->sig_algs = ssl_preset_suiteb_sig_algs; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4813 | #endif |
| 4814 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4815 | #if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 4816 | conf->curve_list = NULL; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4817 | #endif |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4818 | conf->group_list = ssl_preset_suiteb_groups; |
Manuel Pégourié-Gonnard | c98204e | 2015-08-11 04:21:01 +0200 | [diff] [blame] | 4819 | break; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4820 | |
| 4821 | /* |
| 4822 | * Default |
| 4823 | */ |
| 4824 | default: |
Ronald Cron | f660655 | 2022-03-15 11:23:25 +0100 | [diff] [blame] | 4825 | |
Hanno Becker | d60b6c6 | 2021-04-29 12:04:11 +0100 | [diff] [blame] | 4826 | conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites(); |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4827 | |
| 4828 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 4829 | conf->cert_profile = &mbedtls_x509_crt_profile_default; |
| 4830 | #endif |
| 4831 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 4832 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4833 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 4834 | if( mbedtls_ssl_conf_is_tls12_only( conf ) ) |
| 4835 | conf->sig_algs = ssl_tls12_preset_default_sig_algs; |
| 4836 | else |
| 4837 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 4838 | conf->sig_algs = ssl_preset_default_sig_algs; |
Hanno Becker | deb68ce | 2021-08-10 16:04:05 +0100 | [diff] [blame] | 4839 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4840 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4841 | #if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 4842 | conf->curve_list = NULL; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4843 | #endif |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4844 | conf->group_list = ssl_preset_default_groups; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4845 | |
| 4846 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C) |
| 4847 | conf->dhm_min_bitlen = 1024; |
| 4848 | #endif |
| 4849 | } |
| 4850 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4851 | return( 0 ); |
| 4852 | } |
| 4853 | |
| 4854 | /* |
| 4855 | * Free mbedtls_ssl_config |
| 4856 | */ |
| 4857 | void mbedtls_ssl_config_free( mbedtls_ssl_config *conf ) |
| 4858 | { |
| 4859 | #if defined(MBEDTLS_DHM_C) |
| 4860 | mbedtls_mpi_free( &conf->dhm_P ); |
| 4861 | mbedtls_mpi_free( &conf->dhm_G ); |
| 4862 | #endif |
| 4863 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 4864 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 4865 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 4866 | if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) ) |
| 4867 | { |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 4868 | conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT; |
| 4869 | } |
Neil Armstrong | 8ecd668 | 2022-05-05 11:40:35 +0200 | [diff] [blame] | 4870 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4871 | if( conf->psk != NULL ) |
| 4872 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 4873 | mbedtls_platform_zeroize( conf->psk, conf->psk_len ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4874 | mbedtls_free( conf->psk ); |
Azim Khan | 27e8a12 | 2018-03-21 14:24:11 +0000 | [diff] [blame] | 4875 | conf->psk = NULL; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4876 | conf->psk_len = 0; |
junyeonLEE | 316b162 | 2017-12-20 16:29:30 +0900 | [diff] [blame] | 4877 | } |
| 4878 | |
| 4879 | if( conf->psk_identity != NULL ) |
| 4880 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 4881 | mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len ); |
junyeonLEE | 316b162 | 2017-12-20 16:29:30 +0900 | [diff] [blame] | 4882 | mbedtls_free( conf->psk_identity ); |
Azim Khan | 27e8a12 | 2018-03-21 14:24:11 +0000 | [diff] [blame] | 4883 | conf->psk_identity = NULL; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4884 | conf->psk_identity_len = 0; |
| 4885 | } |
| 4886 | #endif |
| 4887 | |
| 4888 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 4889 | ssl_key_cert_free( conf->key_cert ); |
| 4890 | #endif |
| 4891 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 4892 | mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4893 | } |
| 4894 | |
Manuel Pégourié-Gonnard | 5674a97 | 2015-10-19 15:14:03 +0200 | [diff] [blame] | 4895 | #if defined(MBEDTLS_PK_C) && \ |
| 4896 | ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) ) |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 4897 | /* |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4898 | * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 4899 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4900 | unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk ) |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 4901 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4902 | #if defined(MBEDTLS_RSA_C) |
| 4903 | if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) ) |
| 4904 | return( MBEDTLS_SSL_SIG_RSA ); |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 4905 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4906 | #if defined(MBEDTLS_ECDSA_C) |
| 4907 | if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) ) |
| 4908 | return( MBEDTLS_SSL_SIG_ECDSA ); |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 4909 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4910 | return( MBEDTLS_SSL_SIG_ANON ); |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 4911 | } |
| 4912 | |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 4913 | unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type ) |
| 4914 | { |
| 4915 | switch( type ) { |
| 4916 | case MBEDTLS_PK_RSA: |
| 4917 | return( MBEDTLS_SSL_SIG_RSA ); |
| 4918 | case MBEDTLS_PK_ECDSA: |
| 4919 | case MBEDTLS_PK_ECKEY: |
| 4920 | return( MBEDTLS_SSL_SIG_ECDSA ); |
| 4921 | default: |
| 4922 | return( MBEDTLS_SSL_SIG_ANON ); |
| 4923 | } |
| 4924 | } |
| 4925 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4926 | mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig ) |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4927 | { |
| 4928 | switch( sig ) |
| 4929 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4930 | #if defined(MBEDTLS_RSA_C) |
| 4931 | case MBEDTLS_SSL_SIG_RSA: |
| 4932 | return( MBEDTLS_PK_RSA ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4933 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4934 | #if defined(MBEDTLS_ECDSA_C) |
| 4935 | case MBEDTLS_SSL_SIG_ECDSA: |
| 4936 | return( MBEDTLS_PK_ECDSA ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4937 | #endif |
| 4938 | default: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4939 | return( MBEDTLS_PK_NONE ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4940 | } |
| 4941 | } |
Manuel Pégourié-Gonnard | 5674a97 | 2015-10-19 15:14:03 +0200 | [diff] [blame] | 4942 | #endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */ |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4943 | |
Manuel Pégourié-Gonnard | 1a48383 | 2013-09-20 12:29:15 +0200 | [diff] [blame] | 4944 | /* |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 4945 | * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX |
Manuel Pégourié-Gonnard | 1a48383 | 2013-09-20 12:29:15 +0200 | [diff] [blame] | 4946 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4947 | mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash ) |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4948 | { |
| 4949 | switch( hash ) |
| 4950 | { |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4951 | #if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4952 | case MBEDTLS_SSL_HASH_MD5: |
| 4953 | return( MBEDTLS_MD_MD5 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4954 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4955 | #if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4956 | case MBEDTLS_SSL_HASH_SHA1: |
| 4957 | return( MBEDTLS_MD_SHA1 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4958 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4959 | #if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4960 | case MBEDTLS_SSL_HASH_SHA224: |
| 4961 | return( MBEDTLS_MD_SHA224 ); |
Mateusz Starzyk | e3c48b4 | 2021-04-19 16:46:28 +0200 | [diff] [blame] | 4962 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4963 | #if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4964 | case MBEDTLS_SSL_HASH_SHA256: |
| 4965 | return( MBEDTLS_MD_SHA256 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4966 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4967 | #if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4968 | case MBEDTLS_SSL_HASH_SHA384: |
| 4969 | return( MBEDTLS_MD_SHA384 ); |
Mateusz Starzyk | 3352a53 | 2021-04-06 14:28:22 +0200 | [diff] [blame] | 4970 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4971 | #if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4972 | case MBEDTLS_SSL_HASH_SHA512: |
| 4973 | return( MBEDTLS_MD_SHA512 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4974 | #endif |
| 4975 | default: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4976 | return( MBEDTLS_MD_NONE ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4977 | } |
| 4978 | } |
| 4979 | |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 4980 | /* |
| 4981 | * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX |
| 4982 | */ |
| 4983 | unsigned char mbedtls_ssl_hash_from_md_alg( int md ) |
| 4984 | { |
| 4985 | switch( md ) |
| 4986 | { |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4987 | #if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 4988 | case MBEDTLS_MD_MD5: |
| 4989 | return( MBEDTLS_SSL_HASH_MD5 ); |
| 4990 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4991 | #if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 4992 | case MBEDTLS_MD_SHA1: |
| 4993 | return( MBEDTLS_SSL_HASH_SHA1 ); |
| 4994 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4995 | #if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 4996 | case MBEDTLS_MD_SHA224: |
| 4997 | return( MBEDTLS_SSL_HASH_SHA224 ); |
Mateusz Starzyk | e3c48b4 | 2021-04-19 16:46:28 +0200 | [diff] [blame] | 4998 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4999 | #if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 5000 | case MBEDTLS_MD_SHA256: |
| 5001 | return( MBEDTLS_SSL_HASH_SHA256 ); |
| 5002 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5003 | #if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 5004 | case MBEDTLS_MD_SHA384: |
| 5005 | return( MBEDTLS_SSL_HASH_SHA384 ); |
Mateusz Starzyk | 3352a53 | 2021-04-06 14:28:22 +0200 | [diff] [blame] | 5006 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5007 | #if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 5008 | case MBEDTLS_MD_SHA512: |
| 5009 | return( MBEDTLS_SSL_HASH_SHA512 ); |
| 5010 | #endif |
| 5011 | default: |
| 5012 | return( MBEDTLS_SSL_HASH_NONE ); |
| 5013 | } |
| 5014 | } |
| 5015 | |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 5016 | /* |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 5017 | * Check if a curve proposed by the peer is in our list. |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 5018 | * Return 0 if we're willing to use it, -1 otherwise. |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 5019 | */ |
Manuel Pégourié-Gonnard | 0d63b84 | 2022-01-18 13:10:56 +0100 | [diff] [blame] | 5020 | int mbedtls_ssl_check_curve_tls_id( const mbedtls_ssl_context *ssl, uint16_t tls_id ) |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 5021 | { |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5022 | const uint16_t *group_list = mbedtls_ssl_get_groups( ssl ); |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 5023 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5024 | if( group_list == NULL ) |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 5025 | return( -1 ); |
| 5026 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5027 | for( ; *group_list != 0; group_list++ ) |
| 5028 | { |
| 5029 | if( *group_list == tls_id ) |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 5030 | return( 0 ); |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5031 | } |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 5032 | |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 5033 | return( -1 ); |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 5034 | } |
Manuel Pégourié-Gonnard | 0d63b84 | 2022-01-18 13:10:56 +0100 | [diff] [blame] | 5035 | |
| 5036 | #if defined(MBEDTLS_ECP_C) |
| 5037 | /* |
| 5038 | * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id. |
| 5039 | */ |
| 5040 | int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id ) |
| 5041 | { |
Leonid Rozenboim | 19e5973 | 2022-08-08 16:52:38 -0700 | [diff] [blame] | 5042 | const mbedtls_ecp_curve_info *grp_info = |
Tom Cosgrove | bcc13c9 | 2022-08-24 15:08:16 +0100 | [diff] [blame] | 5043 | mbedtls_ecp_curve_info_from_grp_id( grp_id ); |
Leonid Rozenboim | 19e5973 | 2022-08-08 16:52:38 -0700 | [diff] [blame] | 5044 | |
Tom Cosgrove | bcc13c9 | 2022-08-24 15:08:16 +0100 | [diff] [blame] | 5045 | if ( grp_info == NULL ) |
Leonid Rozenboim | 19e5973 | 2022-08-08 16:52:38 -0700 | [diff] [blame] | 5046 | return -1; |
| 5047 | |
| 5048 | uint16_t tls_id = grp_info->tls_id; |
| 5049 | |
Manuel Pégourié-Gonnard | 0d63b84 | 2022-01-18 13:10:56 +0100 | [diff] [blame] | 5050 | return mbedtls_ssl_check_curve_tls_id( ssl, tls_id ); |
| 5051 | } |
Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 5052 | #endif /* MBEDTLS_ECP_C */ |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5053 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5054 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 5055 | int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert, |
| 5056 | const mbedtls_ssl_ciphersuite_t *ciphersuite, |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 5057 | int cert_endpoint, |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 5058 | uint32_t *flags ) |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5059 | { |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 5060 | int ret = 0; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5061 | int usage = 0; |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 5062 | const char *ext_oid; |
| 5063 | size_t ext_len; |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 5064 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5065 | if( cert_endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5066 | { |
| 5067 | /* Server part of the key exchange */ |
| 5068 | switch( ciphersuite->key_exchange ) |
| 5069 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5070 | case MBEDTLS_KEY_EXCHANGE_RSA: |
| 5071 | case MBEDTLS_KEY_EXCHANGE_RSA_PSK: |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 5072 | usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5073 | break; |
| 5074 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5075 | case MBEDTLS_KEY_EXCHANGE_DHE_RSA: |
| 5076 | case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA: |
| 5077 | case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA: |
| 5078 | usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5079 | break; |
| 5080 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5081 | case MBEDTLS_KEY_EXCHANGE_ECDH_RSA: |
| 5082 | case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA: |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 5083 | usage = MBEDTLS_X509_KU_KEY_AGREEMENT; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5084 | break; |
| 5085 | |
| 5086 | /* Don't use default: we want warnings when adding new values */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5087 | case MBEDTLS_KEY_EXCHANGE_NONE: |
| 5088 | case MBEDTLS_KEY_EXCHANGE_PSK: |
| 5089 | case MBEDTLS_KEY_EXCHANGE_DHE_PSK: |
| 5090 | case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK: |
Manuel Pégourié-Gonnard | 557535d | 2015-09-15 17:53:32 +0200 | [diff] [blame] | 5091 | case MBEDTLS_KEY_EXCHANGE_ECJPAKE: |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5092 | usage = 0; |
| 5093 | } |
| 5094 | } |
| 5095 | else |
| 5096 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5097 | /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */ |
| 5098 | usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5099 | } |
| 5100 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5101 | if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 ) |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 5102 | { |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 5103 | *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE; |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 5104 | ret = -1; |
| 5105 | } |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5106 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5107 | if( cert_endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 5108 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5109 | ext_oid = MBEDTLS_OID_SERVER_AUTH; |
| 5110 | ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH ); |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 5111 | } |
| 5112 | else |
| 5113 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5114 | ext_oid = MBEDTLS_OID_CLIENT_AUTH; |
| 5115 | ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH ); |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 5116 | } |
| 5117 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5118 | if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 ) |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 5119 | { |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 5120 | *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE; |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 5121 | ret = -1; |
| 5122 | } |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 5123 | |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 5124 | return( ret ); |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5125 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5126 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Manuel Pégourié-Gonnard | 3a306b9 | 2014-04-29 15:11:17 +0200 | [diff] [blame] | 5127 | |
Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 5128 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 5129 | int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl, |
| 5130 | const mbedtls_md_type_t md, |
| 5131 | unsigned char *dst, |
| 5132 | size_t dst_len, |
| 5133 | size_t *olen ) |
| 5134 | { |
Ronald Cron | f6893e1 | 2022-01-07 22:09:01 +0100 | [diff] [blame] | 5135 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 5136 | psa_hash_operation_t *hash_operation_to_clone; |
| 5137 | psa_hash_operation_t hash_operation = psa_hash_operation_init(); |
| 5138 | |
Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 5139 | *olen = 0; |
Ronald Cron | f6893e1 | 2022-01-07 22:09:01 +0100 | [diff] [blame] | 5140 | |
| 5141 | switch( md ) |
| 5142 | { |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5143 | #if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Ronald Cron | f6893e1 | 2022-01-07 22:09:01 +0100 | [diff] [blame] | 5144 | case MBEDTLS_MD_SHA384: |
| 5145 | hash_operation_to_clone = &ssl->handshake->fin_sha384_psa; |
| 5146 | break; |
| 5147 | #endif |
| 5148 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5149 | #if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Ronald Cron | f6893e1 | 2022-01-07 22:09:01 +0100 | [diff] [blame] | 5150 | case MBEDTLS_MD_SHA256: |
| 5151 | hash_operation_to_clone = &ssl->handshake->fin_sha256_psa; |
| 5152 | break; |
| 5153 | #endif |
| 5154 | |
| 5155 | default: |
| 5156 | goto exit; |
| 5157 | } |
| 5158 | |
| 5159 | status = psa_hash_clone( hash_operation_to_clone, &hash_operation ); |
| 5160 | if( status != PSA_SUCCESS ) |
| 5161 | goto exit; |
| 5162 | |
| 5163 | status = psa_hash_finish( &hash_operation, dst, dst_len, olen ); |
| 5164 | if( status != PSA_SUCCESS ) |
| 5165 | goto exit; |
| 5166 | |
| 5167 | exit: |
Ronald Cron | b788c04 | 2022-02-15 09:14:15 +0100 | [diff] [blame] | 5168 | return( psa_ssl_status_to_mbedtls( status ) ); |
Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 5169 | } |
| 5170 | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
| 5171 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5172 | #if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 5173 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | 000f976 | 2021-09-14 11:12:51 +0800 | [diff] [blame] | 5174 | static int ssl_get_handshake_transcript_sha384( mbedtls_ssl_context *ssl, |
| 5175 | unsigned char *dst, |
| 5176 | size_t dst_len, |
| 5177 | size_t *olen ) |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5178 | { |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5179 | int ret; |
| 5180 | mbedtls_sha512_context sha512; |
| 5181 | |
| 5182 | if( dst_len < 48 ) |
| 5183 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 5184 | |
| 5185 | mbedtls_sha512_init( &sha512 ); |
Andrzej Kurek | a242e83 | 2022-08-11 10:03:14 -0400 | [diff] [blame] | 5186 | mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 ); |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5187 | |
| 5188 | if( ( ret = mbedtls_sha512_finish( &sha512, dst ) ) != 0 ) |
| 5189 | { |
| 5190 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha512_finish", ret ); |
| 5191 | goto exit; |
| 5192 | } |
| 5193 | |
| 5194 | *olen = 48; |
| 5195 | |
| 5196 | exit: |
| 5197 | |
| 5198 | mbedtls_sha512_free( &sha512 ); |
| 5199 | return( ret ); |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5200 | } |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5201 | #endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */ |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5202 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5203 | #if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 5204 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | 000f976 | 2021-09-14 11:12:51 +0800 | [diff] [blame] | 5205 | static int ssl_get_handshake_transcript_sha256( mbedtls_ssl_context *ssl, |
| 5206 | unsigned char *dst, |
| 5207 | size_t dst_len, |
| 5208 | size_t *olen ) |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5209 | { |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5210 | int ret; |
| 5211 | mbedtls_sha256_context sha256; |
| 5212 | |
| 5213 | if( dst_len < 32 ) |
| 5214 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 5215 | |
| 5216 | mbedtls_sha256_init( &sha256 ); |
| 5217 | mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 ); |
Jerry Yu | c5aef88 | 2021-12-23 20:15:02 +0800 | [diff] [blame] | 5218 | |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5219 | if( ( ret = mbedtls_sha256_finish( &sha256, dst ) ) != 0 ) |
| 5220 | { |
| 5221 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha256_finish", ret ); |
| 5222 | goto exit; |
| 5223 | } |
| 5224 | |
| 5225 | *olen = 32; |
| 5226 | |
| 5227 | exit: |
| 5228 | |
| 5229 | mbedtls_sha256_free( &sha256 ); |
| 5230 | return( ret ); |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5231 | } |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5232 | #endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */ |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5233 | |
Jerry Yu | 000f976 | 2021-09-14 11:12:51 +0800 | [diff] [blame] | 5234 | int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl, |
| 5235 | const mbedtls_md_type_t md, |
| 5236 | unsigned char *dst, |
| 5237 | size_t dst_len, |
| 5238 | size_t *olen ) |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5239 | { |
Jerry Yu | c1ddeef | 2021-10-08 15:14:45 +0800 | [diff] [blame] | 5240 | switch( md ) |
| 5241 | { |
| 5242 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5243 | #if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Jerry Yu | c1ddeef | 2021-10-08 15:14:45 +0800 | [diff] [blame] | 5244 | case MBEDTLS_MD_SHA384: |
Jerry Yu | c5aef88 | 2021-12-23 20:15:02 +0800 | [diff] [blame] | 5245 | return( ssl_get_handshake_transcript_sha384( ssl, dst, dst_len, olen ) ); |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 5246 | #endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/ |
Jerry Yu | c1ddeef | 2021-10-08 15:14:45 +0800 | [diff] [blame] | 5247 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5248 | #if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Jerry Yu | c1ddeef | 2021-10-08 15:14:45 +0800 | [diff] [blame] | 5249 | case MBEDTLS_MD_SHA256: |
Jerry Yu | c5aef88 | 2021-12-23 20:15:02 +0800 | [diff] [blame] | 5250 | return( ssl_get_handshake_transcript_sha256( ssl, dst, dst_len, olen ) ); |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 5251 | #endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/ |
Jerry Yu | c1ddeef | 2021-10-08 15:14:45 +0800 | [diff] [blame] | 5252 | |
| 5253 | default: |
| 5254 | break; |
| 5255 | } |
Jerry Yu | c5aef88 | 2021-12-23 20:15:02 +0800 | [diff] [blame] | 5256 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5257 | } |
XiaokangQian | 647719a | 2021-12-07 09:16:29 +0000 | [diff] [blame] | 5258 | |
Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 5259 | #endif /* !MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5260 | |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 5261 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
| 5262 | /* mbedtls_ssl_parse_sig_alg_ext() |
| 5263 | * |
| 5264 | * The `extension_data` field of signature algorithm contains a `SignatureSchemeList` |
| 5265 | * value (TLS 1.3 RFC8446): |
| 5266 | * enum { |
| 5267 | * .... |
| 5268 | * ecdsa_secp256r1_sha256( 0x0403 ), |
| 5269 | * ecdsa_secp384r1_sha384( 0x0503 ), |
| 5270 | * ecdsa_secp521r1_sha512( 0x0603 ), |
| 5271 | * .... |
| 5272 | * } SignatureScheme; |
| 5273 | * |
| 5274 | * struct { |
| 5275 | * SignatureScheme supported_signature_algorithms<2..2^16-2>; |
| 5276 | * } SignatureSchemeList; |
| 5277 | * |
| 5278 | * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm` |
| 5279 | * value (TLS 1.2 RFC5246): |
| 5280 | * enum { |
| 5281 | * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5), |
| 5282 | * sha512(6), (255) |
| 5283 | * } HashAlgorithm; |
| 5284 | * |
| 5285 | * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) } |
| 5286 | * SignatureAlgorithm; |
| 5287 | * |
| 5288 | * struct { |
| 5289 | * HashAlgorithm hash; |
| 5290 | * SignatureAlgorithm signature; |
| 5291 | * } SignatureAndHashAlgorithm; |
| 5292 | * |
| 5293 | * SignatureAndHashAlgorithm |
| 5294 | * supported_signature_algorithms<2..2^16-2>; |
| 5295 | * |
| 5296 | * The TLS 1.3 signature algorithm extension was defined to be a compatible |
| 5297 | * generalization of the TLS 1.2 signature algorithm extension. |
| 5298 | * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by |
| 5299 | * `SignatureScheme` field of TLS 1.3 |
| 5300 | * |
| 5301 | */ |
| 5302 | int mbedtls_ssl_parse_sig_alg_ext( mbedtls_ssl_context *ssl, |
| 5303 | const unsigned char *buf, |
| 5304 | const unsigned char *end ) |
| 5305 | { |
| 5306 | const unsigned char *p = buf; |
| 5307 | size_t supported_sig_algs_len = 0; |
| 5308 | const unsigned char *supported_sig_algs_end; |
| 5309 | uint16_t sig_alg; |
| 5310 | uint32_t common_idx = 0; |
| 5311 | |
| 5312 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 ); |
| 5313 | supported_sig_algs_len = MBEDTLS_GET_UINT16_BE( p, 0 ); |
| 5314 | p += 2; |
| 5315 | |
| 5316 | memset( ssl->handshake->received_sig_algs, 0, |
| 5317 | sizeof(ssl->handshake->received_sig_algs) ); |
| 5318 | |
| 5319 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, supported_sig_algs_len ); |
| 5320 | supported_sig_algs_end = p + supported_sig_algs_len; |
| 5321 | while( p < supported_sig_algs_end ) |
| 5322 | { |
| 5323 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, supported_sig_algs_end, 2 ); |
| 5324 | sig_alg = MBEDTLS_GET_UINT16_BE( p, 0 ); |
| 5325 | p += 2; |
Jerry Yu | f3b46b5 | 2022-06-19 16:52:27 +0800 | [diff] [blame] | 5326 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "received signature algorithm: 0x%x %s", |
| 5327 | sig_alg, |
| 5328 | mbedtls_ssl_sig_alg_to_str( sig_alg ) ) ); |
Jerry Yu | 2fe6c63 | 2022-06-29 10:02:38 +0800 | [diff] [blame] | 5329 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Jerry Yu | 52b7d92 | 2022-07-01 18:03:31 +0800 | [diff] [blame] | 5330 | if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 && |
Jerry Yu | 2fe6c63 | 2022-06-29 10:02:38 +0800 | [diff] [blame] | 5331 | ( ! ( mbedtls_ssl_sig_alg_is_supported( ssl, sig_alg ) && |
| 5332 | mbedtls_ssl_sig_alg_is_offered( ssl, sig_alg ) ) ) ) |
| 5333 | { |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 5334 | continue; |
Jerry Yu | 2fe6c63 | 2022-06-29 10:02:38 +0800 | [diff] [blame] | 5335 | } |
| 5336 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 5337 | |
Jerry Yu | f3b46b5 | 2022-06-19 16:52:27 +0800 | [diff] [blame] | 5338 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "valid signature algorithm: %s", |
| 5339 | mbedtls_ssl_sig_alg_to_str( sig_alg ) ) ); |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 5340 | |
| 5341 | if( common_idx + 1 < MBEDTLS_RECEIVED_SIG_ALGS_SIZE ) |
| 5342 | { |
| 5343 | ssl->handshake->received_sig_algs[common_idx] = sig_alg; |
| 5344 | common_idx += 1; |
| 5345 | } |
| 5346 | } |
| 5347 | /* Check that we consumed all the message. */ |
| 5348 | if( p != end ) |
| 5349 | { |
| 5350 | MBEDTLS_SSL_DEBUG_MSG( 1, |
| 5351 | ( "Signature algorithms extension length misaligned" ) ); |
| 5352 | MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR, |
| 5353 | MBEDTLS_ERR_SSL_DECODE_ERROR ); |
| 5354 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
| 5355 | } |
| 5356 | |
| 5357 | if( common_idx == 0 ) |
| 5358 | { |
| 5359 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "no signature algorithm in common" ) ); |
| 5360 | MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE, |
| 5361 | MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE ); |
| 5362 | return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE ); |
| 5363 | } |
| 5364 | |
Gabor Mezei | 15b95a6 | 2022-05-09 16:37:58 +0200 | [diff] [blame] | 5365 | ssl->handshake->received_sig_algs[common_idx] = MBEDTLS_TLS_SIG_NONE; |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 5366 | return( 0 ); |
| 5367 | } |
| 5368 | |
| 5369 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 5370 | |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5371 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 5372 | |
| 5373 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 5374 | |
| 5375 | static psa_status_t setup_psa_key_derivation( psa_key_derivation_operation_t* derivation, |
| 5376 | mbedtls_svc_key_id_t key, |
| 5377 | psa_algorithm_t alg, |
Neil Armstrong | 8ecd668 | 2022-05-05 11:40:35 +0200 | [diff] [blame] | 5378 | const unsigned char* raw_psk, size_t raw_psk_length, |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5379 | const unsigned char* seed, size_t seed_length, |
| 5380 | const unsigned char* label, size_t label_length, |
Przemek Stekiel | 51a1f36 | 2022-04-13 08:57:06 +0200 | [diff] [blame] | 5381 | const unsigned char* other_secret, |
| 5382 | size_t other_secret_length, |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5383 | size_t capacity ) |
| 5384 | { |
| 5385 | psa_status_t status; |
| 5386 | |
| 5387 | status = psa_key_derivation_setup( derivation, alg ); |
| 5388 | if( status != PSA_SUCCESS ) |
| 5389 | return( status ); |
| 5390 | |
| 5391 | if( PSA_ALG_IS_TLS12_PRF( alg ) || PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) ) |
| 5392 | { |
| 5393 | status = psa_key_derivation_input_bytes( derivation, |
| 5394 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 5395 | seed, seed_length ); |
| 5396 | if( status != PSA_SUCCESS ) |
| 5397 | return( status ); |
| 5398 | |
Przemek Stekiel | 51a1f36 | 2022-04-13 08:57:06 +0200 | [diff] [blame] | 5399 | if ( other_secret != NULL ) |
Przemek Stekiel | 1f02703 | 2022-04-05 17:12:11 +0200 | [diff] [blame] | 5400 | { |
| 5401 | status = psa_key_derivation_input_bytes( derivation, |
Przemek Stekiel | 51a1f36 | 2022-04-13 08:57:06 +0200 | [diff] [blame] | 5402 | PSA_KEY_DERIVATION_INPUT_OTHER_SECRET, |
| 5403 | other_secret, other_secret_length ); |
Przemek Stekiel | 1f02703 | 2022-04-05 17:12:11 +0200 | [diff] [blame] | 5404 | if( status != PSA_SUCCESS ) |
| 5405 | return( status ); |
| 5406 | } |
| 5407 | |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5408 | if( mbedtls_svc_key_id_is_null( key ) ) |
| 5409 | { |
| 5410 | status = psa_key_derivation_input_bytes( |
| 5411 | derivation, PSA_KEY_DERIVATION_INPUT_SECRET, |
Neil Armstrong | 8ecd668 | 2022-05-05 11:40:35 +0200 | [diff] [blame] | 5412 | raw_psk, raw_psk_length ); |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5413 | } |
| 5414 | else |
| 5415 | { |
| 5416 | status = psa_key_derivation_input_key( |
| 5417 | derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key ); |
| 5418 | } |
| 5419 | if( status != PSA_SUCCESS ) |
| 5420 | return( status ); |
| 5421 | |
| 5422 | status = psa_key_derivation_input_bytes( derivation, |
| 5423 | PSA_KEY_DERIVATION_INPUT_LABEL, |
| 5424 | label, label_length ); |
| 5425 | if( status != PSA_SUCCESS ) |
| 5426 | return( status ); |
| 5427 | } |
| 5428 | else |
| 5429 | { |
| 5430 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5431 | } |
| 5432 | |
| 5433 | status = psa_key_derivation_set_capacity( derivation, capacity ); |
| 5434 | if( status != PSA_SUCCESS ) |
| 5435 | return( status ); |
| 5436 | |
| 5437 | return( PSA_SUCCESS ); |
| 5438 | } |
| 5439 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 5440 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5441 | static int tls_prf_generic( mbedtls_md_type_t md_type, |
| 5442 | const unsigned char *secret, size_t slen, |
| 5443 | const char *label, |
| 5444 | const unsigned char *random, size_t rlen, |
| 5445 | unsigned char *dstbuf, size_t dlen ) |
| 5446 | { |
| 5447 | psa_status_t status; |
| 5448 | psa_algorithm_t alg; |
| 5449 | mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5450 | psa_key_derivation_operation_t derivation = |
| 5451 | PSA_KEY_DERIVATION_OPERATION_INIT; |
| 5452 | |
| 5453 | if( md_type == MBEDTLS_MD_SHA384 ) |
| 5454 | alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384); |
| 5455 | else |
| 5456 | alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256); |
| 5457 | |
| 5458 | /* Normally a "secret" should be long enough to be impossible to |
| 5459 | * find by brute force, and in particular should not be empty. But |
| 5460 | * this PRF is also used to derive an IV, in particular in EAP-TLS, |
| 5461 | * and for this use case it makes sense to have a 0-length "secret". |
| 5462 | * Since the key API doesn't allow importing a key of length 0, |
| 5463 | * keep master_key=0, which setup_psa_key_derivation() understands |
| 5464 | * to mean a 0-length "secret" input. */ |
| 5465 | if( slen != 0 ) |
| 5466 | { |
| 5467 | psa_key_attributes_t key_attributes = psa_key_attributes_init(); |
| 5468 | psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE ); |
| 5469 | psa_set_key_algorithm( &key_attributes, alg ); |
| 5470 | psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE ); |
| 5471 | |
| 5472 | status = psa_import_key( &key_attributes, secret, slen, &master_key ); |
| 5473 | if( status != PSA_SUCCESS ) |
| 5474 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 5475 | } |
| 5476 | |
| 5477 | status = setup_psa_key_derivation( &derivation, |
| 5478 | master_key, alg, |
Neil Armstrong | 8ecd668 | 2022-05-05 11:40:35 +0200 | [diff] [blame] | 5479 | NULL, 0, |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5480 | random, rlen, |
| 5481 | (unsigned char const *) label, |
| 5482 | (size_t) strlen( label ), |
Przemek Stekiel | 1f02703 | 2022-04-05 17:12:11 +0200 | [diff] [blame] | 5483 | NULL, 0, |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5484 | dlen ); |
| 5485 | if( status != PSA_SUCCESS ) |
| 5486 | { |
| 5487 | psa_key_derivation_abort( &derivation ); |
| 5488 | psa_destroy_key( master_key ); |
| 5489 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 5490 | } |
| 5491 | |
| 5492 | status = psa_key_derivation_output_bytes( &derivation, dstbuf, dlen ); |
| 5493 | if( status != PSA_SUCCESS ) |
| 5494 | { |
| 5495 | psa_key_derivation_abort( &derivation ); |
| 5496 | psa_destroy_key( master_key ); |
| 5497 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 5498 | } |
| 5499 | |
| 5500 | status = psa_key_derivation_abort( &derivation ); |
| 5501 | if( status != PSA_SUCCESS ) |
| 5502 | { |
| 5503 | psa_destroy_key( master_key ); |
| 5504 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 5505 | } |
| 5506 | |
| 5507 | if( ! mbedtls_svc_key_id_is_null( master_key ) ) |
| 5508 | status = psa_destroy_key( master_key ); |
| 5509 | if( status != PSA_SUCCESS ) |
| 5510 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 5511 | |
| 5512 | return( 0 ); |
| 5513 | } |
| 5514 | |
| 5515 | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
| 5516 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 5517 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5518 | static int tls_prf_generic( mbedtls_md_type_t md_type, |
| 5519 | const unsigned char *secret, size_t slen, |
| 5520 | const char *label, |
| 5521 | const unsigned char *random, size_t rlen, |
| 5522 | unsigned char *dstbuf, size_t dlen ) |
| 5523 | { |
| 5524 | size_t nb; |
| 5525 | size_t i, j, k, md_len; |
| 5526 | unsigned char *tmp; |
| 5527 | size_t tmp_len = 0; |
| 5528 | unsigned char h_i[MBEDTLS_MD_MAX_SIZE]; |
| 5529 | const mbedtls_md_info_t *md_info; |
| 5530 | mbedtls_md_context_t md_ctx; |
| 5531 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 5532 | |
| 5533 | mbedtls_md_init( &md_ctx ); |
| 5534 | |
| 5535 | if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL ) |
| 5536 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 5537 | |
| 5538 | md_len = mbedtls_md_get_size( md_info ); |
| 5539 | |
| 5540 | tmp_len = md_len + strlen( label ) + rlen; |
| 5541 | tmp = mbedtls_calloc( 1, tmp_len ); |
| 5542 | if( tmp == NULL ) |
| 5543 | { |
| 5544 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 5545 | goto exit; |
| 5546 | } |
| 5547 | |
| 5548 | nb = strlen( label ); |
| 5549 | memcpy( tmp + md_len, label, nb ); |
| 5550 | memcpy( tmp + md_len + nb, random, rlen ); |
| 5551 | nb += rlen; |
| 5552 | |
| 5553 | /* |
| 5554 | * Compute P_<hash>(secret, label + random)[0..dlen] |
| 5555 | */ |
| 5556 | if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 ) |
| 5557 | goto exit; |
| 5558 | |
| 5559 | ret = mbedtls_md_hmac_starts( &md_ctx, secret, slen ); |
| 5560 | if( ret != 0 ) |
| 5561 | goto exit; |
| 5562 | ret = mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb ); |
| 5563 | if( ret != 0 ) |
| 5564 | goto exit; |
| 5565 | ret = mbedtls_md_hmac_finish( &md_ctx, tmp ); |
| 5566 | if( ret != 0 ) |
| 5567 | goto exit; |
| 5568 | |
| 5569 | for( i = 0; i < dlen; i += md_len ) |
| 5570 | { |
| 5571 | ret = mbedtls_md_hmac_reset ( &md_ctx ); |
| 5572 | if( ret != 0 ) |
| 5573 | goto exit; |
| 5574 | ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb ); |
| 5575 | if( ret != 0 ) |
| 5576 | goto exit; |
| 5577 | ret = mbedtls_md_hmac_finish( &md_ctx, h_i ); |
| 5578 | if( ret != 0 ) |
| 5579 | goto exit; |
| 5580 | |
| 5581 | ret = mbedtls_md_hmac_reset ( &md_ctx ); |
| 5582 | if( ret != 0 ) |
| 5583 | goto exit; |
| 5584 | ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len ); |
| 5585 | if( ret != 0 ) |
| 5586 | goto exit; |
| 5587 | ret = mbedtls_md_hmac_finish( &md_ctx, tmp ); |
| 5588 | if( ret != 0 ) |
| 5589 | goto exit; |
| 5590 | |
| 5591 | k = ( i + md_len > dlen ) ? dlen % md_len : md_len; |
| 5592 | |
| 5593 | for( j = 0; j < k; j++ ) |
| 5594 | dstbuf[i + j] = h_i[j]; |
| 5595 | } |
| 5596 | |
| 5597 | exit: |
| 5598 | mbedtls_md_free( &md_ctx ); |
| 5599 | |
| 5600 | mbedtls_platform_zeroize( tmp, tmp_len ); |
| 5601 | mbedtls_platform_zeroize( h_i, sizeof( h_i ) ); |
| 5602 | |
| 5603 | mbedtls_free( tmp ); |
| 5604 | |
| 5605 | return( ret ); |
| 5606 | } |
| 5607 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 5608 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5609 | #if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 5610 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5611 | static int tls_prf_sha256( const unsigned char *secret, size_t slen, |
| 5612 | const char *label, |
| 5613 | const unsigned char *random, size_t rlen, |
| 5614 | unsigned char *dstbuf, size_t dlen ) |
| 5615 | { |
| 5616 | return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen, |
| 5617 | label, random, rlen, dstbuf, dlen ) ); |
| 5618 | } |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 5619 | #endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/ |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5620 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5621 | #if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 5622 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5623 | static int tls_prf_sha384( const unsigned char *secret, size_t slen, |
| 5624 | const char *label, |
| 5625 | const unsigned char *random, size_t rlen, |
| 5626 | unsigned char *dstbuf, size_t dlen ) |
| 5627 | { |
| 5628 | return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen, |
| 5629 | label, random, rlen, dstbuf, dlen ) ); |
| 5630 | } |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 5631 | #endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/ |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5632 | |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5633 | /* |
| 5634 | * Set appropriate PRF function and other SSL / TLS1.2 functions |
| 5635 | * |
| 5636 | * Inputs: |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5637 | * - hash associated with the ciphersuite (only used by TLS 1.2) |
| 5638 | * |
| 5639 | * Outputs: |
| 5640 | * - the tls_prf, calc_verify and calc_finished members of handshake structure |
| 5641 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 5642 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5643 | static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake, |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5644 | mbedtls_md_type_t hash ) |
| 5645 | { |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5646 | #if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Ronald Cron | 81591aa | 2022-03-07 09:05:51 +0100 | [diff] [blame] | 5647 | if( hash == MBEDTLS_MD_SHA384 ) |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5648 | { |
| 5649 | handshake->tls_prf = tls_prf_sha384; |
| 5650 | handshake->calc_verify = ssl_calc_verify_tls_sha384; |
| 5651 | handshake->calc_finished = ssl_calc_finished_tls_sha384; |
| 5652 | } |
| 5653 | else |
| 5654 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5655 | #if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5656 | { |
Ronald Cron | 81591aa | 2022-03-07 09:05:51 +0100 | [diff] [blame] | 5657 | (void) hash; |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5658 | handshake->tls_prf = tls_prf_sha256; |
| 5659 | handshake->calc_verify = ssl_calc_verify_tls_sha256; |
| 5660 | handshake->calc_finished = ssl_calc_finished_tls_sha256; |
| 5661 | } |
Ronald Cron | 81591aa | 2022-03-07 09:05:51 +0100 | [diff] [blame] | 5662 | #else |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5663 | { |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5664 | (void) handshake; |
Ronald Cron | 81591aa | 2022-03-07 09:05:51 +0100 | [diff] [blame] | 5665 | (void) hash; |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5666 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 5667 | } |
Ronald Cron | 81591aa | 2022-03-07 09:05:51 +0100 | [diff] [blame] | 5668 | #endif |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5669 | |
| 5670 | return( 0 ); |
| 5671 | } |
Jerry Yu | d6ab235 | 2022-02-17 14:03:43 +0800 | [diff] [blame] | 5672 | |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5673 | /* |
| 5674 | * Compute master secret if needed |
| 5675 | * |
| 5676 | * Parameters: |
| 5677 | * [in/out] handshake |
| 5678 | * [in] resume, premaster, extended_ms, calc_verify, tls_prf |
| 5679 | * (PSA-PSK) ciphersuite_info, psk_opaque |
| 5680 | * [out] premaster (cleared) |
| 5681 | * [out] master |
| 5682 | * [in] ssl: optionally used for debugging, EMS and PSA-PSK |
| 5683 | * debug: conf->f_dbg, conf->p_dbg |
| 5684 | * EMS: passed to calc_verify (debug + session_negotiate) |
Ronald Cron | a25cf58 | 2022-03-07 11:10:36 +0100 | [diff] [blame] | 5685 | * PSA-PSA: conf |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5686 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 5687 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5688 | static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake, |
| 5689 | unsigned char *master, |
| 5690 | const mbedtls_ssl_context *ssl ) |
| 5691 | { |
| 5692 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 5693 | |
| 5694 | /* cf. RFC 5246, Section 8.1: |
| 5695 | * "The master secret is always exactly 48 bytes in length." */ |
| 5696 | size_t const master_secret_len = 48; |
| 5697 | |
| 5698 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 5699 | unsigned char session_hash[48]; |
| 5700 | #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */ |
| 5701 | |
| 5702 | /* The label for the KDF used for key expansion. |
| 5703 | * This is either "master secret" or "extended master secret" |
| 5704 | * depending on whether the Extended Master Secret extension |
| 5705 | * is used. */ |
| 5706 | char const *lbl = "master secret"; |
| 5707 | |
Przemek Stekiel | ae4ed30 | 2022-04-05 17:15:55 +0200 | [diff] [blame] | 5708 | /* The seed for the KDF used for key expansion. |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5709 | * - If the Extended Master Secret extension is not used, |
| 5710 | * this is ClientHello.Random + ServerHello.Random |
| 5711 | * (see Sect. 8.1 in RFC 5246). |
| 5712 | * - If the Extended Master Secret extension is used, |
| 5713 | * this is the transcript of the handshake so far. |
| 5714 | * (see Sect. 4 in RFC 7627). */ |
Przemek Stekiel | ae4ed30 | 2022-04-05 17:15:55 +0200 | [diff] [blame] | 5715 | unsigned char const *seed = handshake->randbytes; |
| 5716 | size_t seed_len = 64; |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5717 | |
| 5718 | #if !defined(MBEDTLS_DEBUG_C) && \ |
| 5719 | !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \ |
| 5720 | !(defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
| 5721 | defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)) |
| 5722 | ssl = NULL; /* make sure we don't use it except for those cases */ |
| 5723 | (void) ssl; |
| 5724 | #endif |
| 5725 | |
| 5726 | if( handshake->resume != 0 ) |
| 5727 | { |
| 5728 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) ); |
| 5729 | return( 0 ); |
| 5730 | } |
| 5731 | |
| 5732 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 5733 | if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED ) |
| 5734 | { |
| 5735 | lbl = "extended master secret"; |
Przemek Stekiel | ae4ed30 | 2022-04-05 17:15:55 +0200 | [diff] [blame] | 5736 | seed = session_hash; |
| 5737 | handshake->calc_verify( ssl, session_hash, &seed_len ); |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5738 | |
| 5739 | MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret", |
Przemek Stekiel | ae4ed30 | 2022-04-05 17:15:55 +0200 | [diff] [blame] | 5740 | session_hash, seed_len ); |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5741 | } |
Przemek Stekiel | 169bf0b | 2022-04-29 07:53:29 +0200 | [diff] [blame] | 5742 | #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */ |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5743 | |
Przemek Stekiel | 99114f3 | 2022-04-22 11:20:09 +0200 | [diff] [blame] | 5744 | #if defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
Przemek Stekiel | 8a4b7fd | 2022-04-28 09:22:22 +0200 | [diff] [blame] | 5745 | defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Neil Armstrong | e952a30 | 2022-05-03 10:22:14 +0200 | [diff] [blame] | 5746 | if( mbedtls_ssl_ciphersuite_uses_psk( handshake->ciphersuite_info ) == 1 ) |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5747 | { |
| 5748 | /* Perform PSK-to-MS expansion in a single step. */ |
| 5749 | psa_status_t status; |
| 5750 | psa_algorithm_t alg; |
| 5751 | mbedtls_svc_key_id_t psk; |
| 5752 | psa_key_derivation_operation_t derivation = |
| 5753 | PSA_KEY_DERIVATION_OPERATION_INIT; |
| 5754 | mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac; |
| 5755 | |
| 5756 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) ); |
| 5757 | |
| 5758 | psk = mbedtls_ssl_get_opaque_psk( ssl ); |
| 5759 | |
| 5760 | if( hash_alg == MBEDTLS_MD_SHA384 ) |
| 5761 | alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384); |
| 5762 | else |
| 5763 | alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256); |
| 5764 | |
Przemek Stekiel | 51a1f36 | 2022-04-13 08:57:06 +0200 | [diff] [blame] | 5765 | size_t other_secret_len = 0; |
| 5766 | unsigned char* other_secret = NULL; |
Przemek Stekiel | c203340 | 2022-04-05 17:19:41 +0200 | [diff] [blame] | 5767 | |
Przemek Stekiel | 19b80f8 | 2022-04-14 08:29:31 +0200 | [diff] [blame] | 5768 | switch( handshake->ciphersuite_info->key_exchange ) |
Przemek Stekiel | c203340 | 2022-04-05 17:19:41 +0200 | [diff] [blame] | 5769 | { |
Przemek Stekiel | 19b80f8 | 2022-04-14 08:29:31 +0200 | [diff] [blame] | 5770 | /* Provide other secret. |
Przemek Stekiel | 51a1f36 | 2022-04-13 08:57:06 +0200 | [diff] [blame] | 5771 | * Other secret is stored in premaster, where first 2 bytes hold the |
Przemek Stekiel | 19b80f8 | 2022-04-14 08:29:31 +0200 | [diff] [blame] | 5772 | * length of the other key. |
Przemek Stekiel | c203340 | 2022-04-05 17:19:41 +0200 | [diff] [blame] | 5773 | */ |
Przemek Stekiel | 19b80f8 | 2022-04-14 08:29:31 +0200 | [diff] [blame] | 5774 | case MBEDTLS_KEY_EXCHANGE_RSA_PSK: |
Przemek Stekiel | 8abcee9 | 2022-04-28 09:16:28 +0200 | [diff] [blame] | 5775 | /* For RSA-PSK other key length is always 48 bytes. */ |
Przemek Stekiel | 19b80f8 | 2022-04-14 08:29:31 +0200 | [diff] [blame] | 5776 | other_secret_len = 48; |
| 5777 | other_secret = handshake->premaster + 2; |
| 5778 | break; |
| 5779 | case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK: |
Przemek Stekiel | b293aaa | 2022-04-19 12:22:38 +0200 | [diff] [blame] | 5780 | case MBEDTLS_KEY_EXCHANGE_DHE_PSK: |
Przemek Stekiel | 19b80f8 | 2022-04-14 08:29:31 +0200 | [diff] [blame] | 5781 | other_secret_len = MBEDTLS_GET_UINT16_BE(handshake->premaster, 0); |
| 5782 | other_secret = handshake->premaster + 2; |
| 5783 | break; |
| 5784 | default: |
| 5785 | break; |
Przemek Stekiel | c203340 | 2022-04-05 17:19:41 +0200 | [diff] [blame] | 5786 | } |
| 5787 | |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5788 | status = setup_psa_key_derivation( &derivation, psk, alg, |
Neil Armstrong | 8ecd668 | 2022-05-05 11:40:35 +0200 | [diff] [blame] | 5789 | ssl->conf->psk, ssl->conf->psk_len, |
Przemek Stekiel | ae4ed30 | 2022-04-05 17:15:55 +0200 | [diff] [blame] | 5790 | seed, seed_len, |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5791 | (unsigned char const *) lbl, |
| 5792 | (size_t) strlen( lbl ), |
Przemek Stekiel | 51a1f36 | 2022-04-13 08:57:06 +0200 | [diff] [blame] | 5793 | other_secret, other_secret_len, |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5794 | master_secret_len ); |
| 5795 | if( status != PSA_SUCCESS ) |
| 5796 | { |
| 5797 | psa_key_derivation_abort( &derivation ); |
| 5798 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 5799 | } |
| 5800 | |
| 5801 | status = psa_key_derivation_output_bytes( &derivation, |
| 5802 | master, |
| 5803 | master_secret_len ); |
| 5804 | if( status != PSA_SUCCESS ) |
| 5805 | { |
| 5806 | psa_key_derivation_abort( &derivation ); |
| 5807 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 5808 | } |
| 5809 | |
| 5810 | status = psa_key_derivation_abort( &derivation ); |
| 5811 | if( status != PSA_SUCCESS ) |
| 5812 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 5813 | } |
| 5814 | else |
| 5815 | #endif |
| 5816 | { |
| 5817 | ret = handshake->tls_prf( handshake->premaster, handshake->pmslen, |
Przemek Stekiel | ae4ed30 | 2022-04-05 17:15:55 +0200 | [diff] [blame] | 5818 | lbl, seed, seed_len, |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5819 | master, |
| 5820 | master_secret_len ); |
| 5821 | if( ret != 0 ) |
| 5822 | { |
| 5823 | MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret ); |
| 5824 | return( ret ); |
| 5825 | } |
| 5826 | |
| 5827 | MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", |
| 5828 | handshake->premaster, |
| 5829 | handshake->pmslen ); |
| 5830 | |
| 5831 | mbedtls_platform_zeroize( handshake->premaster, |
| 5832 | sizeof(handshake->premaster) ); |
| 5833 | } |
| 5834 | |
| 5835 | return( 0 ); |
| 5836 | } |
| 5837 | |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 5838 | int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl ) |
| 5839 | { |
| 5840 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 5841 | const mbedtls_ssl_ciphersuite_t * const ciphersuite_info = |
| 5842 | ssl->handshake->ciphersuite_info; |
| 5843 | |
| 5844 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) ); |
| 5845 | |
| 5846 | /* Set PRF, calc_verify and calc_finished function pointers */ |
| 5847 | ret = ssl_set_handshake_prfs( ssl->handshake, |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 5848 | ciphersuite_info->mac ); |
| 5849 | if( ret != 0 ) |
| 5850 | { |
| 5851 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret ); |
| 5852 | return( ret ); |
| 5853 | } |
| 5854 | |
| 5855 | /* Compute master secret if needed */ |
| 5856 | ret = ssl_compute_master( ssl->handshake, |
| 5857 | ssl->session_negotiate->master, |
| 5858 | ssl ); |
| 5859 | if( ret != 0 ) |
| 5860 | { |
| 5861 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret ); |
| 5862 | return( ret ); |
| 5863 | } |
| 5864 | |
| 5865 | /* Swap the client and server random values: |
| 5866 | * - MS derivation wanted client+server (RFC 5246 8.1) |
| 5867 | * - key derivation wants server+client (RFC 5246 6.3) */ |
| 5868 | { |
| 5869 | unsigned char tmp[64]; |
| 5870 | memcpy( tmp, ssl->handshake->randbytes, 64 ); |
| 5871 | memcpy( ssl->handshake->randbytes, tmp + 32, 32 ); |
| 5872 | memcpy( ssl->handshake->randbytes + 32, tmp, 32 ); |
| 5873 | mbedtls_platform_zeroize( tmp, sizeof( tmp ) ); |
| 5874 | } |
| 5875 | |
| 5876 | /* Populate transform structure */ |
| 5877 | ret = ssl_tls12_populate_transform( ssl->transform_negotiate, |
| 5878 | ssl->session_negotiate->ciphersuite, |
| 5879 | ssl->session_negotiate->master, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 5880 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 5881 | ssl->session_negotiate->encrypt_then_mac, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 5882 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 5883 | ssl->handshake->tls_prf, |
| 5884 | ssl->handshake->randbytes, |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 5885 | ssl->tls_version, |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 5886 | ssl->conf->endpoint, |
| 5887 | ssl ); |
| 5888 | if( ret != 0 ) |
| 5889 | { |
| 5890 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls12_populate_transform", ret ); |
| 5891 | return( ret ); |
| 5892 | } |
| 5893 | |
| 5894 | /* We no longer need Server/ClientHello.random values */ |
| 5895 | mbedtls_platform_zeroize( ssl->handshake->randbytes, |
| 5896 | sizeof( ssl->handshake->randbytes ) ); |
| 5897 | |
| 5898 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) ); |
| 5899 | |
| 5900 | return( 0 ); |
| 5901 | } |
Jerry Yu | 8392e0d | 2022-02-17 14:10:24 +0800 | [diff] [blame] | 5902 | |
Ronald Cron | 4dcbca9 | 2022-03-07 10:21:40 +0100 | [diff] [blame] | 5903 | int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md ) |
| 5904 | { |
Ronald Cron | 4dcbca9 | 2022-03-07 10:21:40 +0100 | [diff] [blame] | 5905 | switch( md ) |
| 5906 | { |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5907 | #if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Ronald Cron | 4dcbca9 | 2022-03-07 10:21:40 +0100 | [diff] [blame] | 5908 | case MBEDTLS_SSL_HASH_SHA384: |
| 5909 | ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384; |
| 5910 | break; |
| 5911 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5912 | #if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Ronald Cron | 4dcbca9 | 2022-03-07 10:21:40 +0100 | [diff] [blame] | 5913 | case MBEDTLS_SSL_HASH_SHA256: |
| 5914 | ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256; |
| 5915 | break; |
| 5916 | #endif |
| 5917 | default: |
| 5918 | return( -1 ); |
| 5919 | } |
| 5920 | |
Ronald Cron | c2f13a0 | 2022-03-07 10:25:24 +0100 | [diff] [blame] | 5921 | return( 0 ); |
Ronald Cron | 4dcbca9 | 2022-03-07 10:21:40 +0100 | [diff] [blame] | 5922 | } |
| 5923 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5924 | #if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Jerry Yu | 8392e0d | 2022-02-17 14:10:24 +0800 | [diff] [blame] | 5925 | void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl, |
| 5926 | unsigned char *hash, |
| 5927 | size_t *hlen ) |
| 5928 | { |
| 5929 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 5930 | size_t hash_size; |
| 5931 | psa_status_t status; |
| 5932 | psa_hash_operation_t sha256_psa = psa_hash_operation_init(); |
| 5933 | |
| 5934 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) ); |
| 5935 | status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa ); |
| 5936 | if( status != PSA_SUCCESS ) |
| 5937 | { |
| 5938 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); |
| 5939 | return; |
| 5940 | } |
| 5941 | |
| 5942 | status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size ); |
| 5943 | if( status != PSA_SUCCESS ) |
| 5944 | { |
| 5945 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); |
| 5946 | return; |
| 5947 | } |
| 5948 | |
| 5949 | *hlen = 32; |
| 5950 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen ); |
| 5951 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) ); |
| 5952 | #else |
| 5953 | mbedtls_sha256_context sha256; |
| 5954 | |
| 5955 | mbedtls_sha256_init( &sha256 ); |
| 5956 | |
| 5957 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) ); |
| 5958 | |
| 5959 | mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 ); |
| 5960 | mbedtls_sha256_finish( &sha256, hash ); |
| 5961 | |
| 5962 | *hlen = 32; |
| 5963 | |
| 5964 | MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen ); |
| 5965 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) ); |
| 5966 | |
| 5967 | mbedtls_sha256_free( &sha256 ); |
| 5968 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 5969 | return; |
| 5970 | } |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5971 | #endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */ |
Jerry Yu | 8392e0d | 2022-02-17 14:10:24 +0800 | [diff] [blame] | 5972 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5973 | #if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Jerry Yu | c1cb384 | 2022-02-17 14:13:48 +0800 | [diff] [blame] | 5974 | void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl, |
| 5975 | unsigned char *hash, |
| 5976 | size_t *hlen ) |
| 5977 | { |
| 5978 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 5979 | size_t hash_size; |
| 5980 | psa_status_t status; |
| 5981 | psa_hash_operation_t sha384_psa = psa_hash_operation_init(); |
| 5982 | |
| 5983 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) ); |
| 5984 | status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa ); |
| 5985 | if( status != PSA_SUCCESS ) |
| 5986 | { |
| 5987 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); |
| 5988 | return; |
| 5989 | } |
| 5990 | |
| 5991 | status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size ); |
| 5992 | if( status != PSA_SUCCESS ) |
| 5993 | { |
| 5994 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); |
| 5995 | return; |
| 5996 | } |
| 5997 | |
| 5998 | *hlen = 48; |
| 5999 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen ); |
| 6000 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) ); |
| 6001 | #else |
| 6002 | mbedtls_sha512_context sha512; |
| 6003 | |
| 6004 | mbedtls_sha512_init( &sha512 ); |
| 6005 | |
| 6006 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) ); |
| 6007 | |
Andrzej Kurek | a242e83 | 2022-08-11 10:03:14 -0400 | [diff] [blame] | 6008 | mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 ); |
Jerry Yu | c1cb384 | 2022-02-17 14:13:48 +0800 | [diff] [blame] | 6009 | mbedtls_sha512_finish( &sha512, hash ); |
| 6010 | |
| 6011 | *hlen = 48; |
| 6012 | |
| 6013 | MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen ); |
| 6014 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) ); |
| 6015 | |
| 6016 | mbedtls_sha512_free( &sha512 ); |
| 6017 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 6018 | return; |
| 6019 | } |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 6020 | #endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */ |
Jerry Yu | c1cb384 | 2022-02-17 14:13:48 +0800 | [diff] [blame] | 6021 | |
Neil Armstrong | 80f6f32 | 2022-05-03 17:56:38 +0200 | [diff] [blame] | 6022 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
| 6023 | defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6024 | int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex ) |
| 6025 | { |
| 6026 | unsigned char *p = ssl->handshake->premaster; |
| 6027 | unsigned char *end = p + sizeof( ssl->handshake->premaster ); |
| 6028 | const unsigned char *psk = NULL; |
| 6029 | size_t psk_len = 0; |
Przemek Stekiel | b293aaa | 2022-04-19 12:22:38 +0200 | [diff] [blame] | 6030 | int psk_ret = mbedtls_ssl_get_psk( ssl, &psk, &psk_len ); |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6031 | |
Przemek Stekiel | b293aaa | 2022-04-19 12:22:38 +0200 | [diff] [blame] | 6032 | if( psk_ret == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED ) |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6033 | { |
| 6034 | /* |
| 6035 | * This should never happen because the existence of a PSK is always |
Przemek Stekiel | b293aaa | 2022-04-19 12:22:38 +0200 | [diff] [blame] | 6036 | * checked before calling this function. |
| 6037 | * |
| 6038 | * The exception is opaque DHE-PSK. For DHE-PSK fill premaster with |
Przemek Stekiel | 8abcee9 | 2022-04-28 09:16:28 +0200 | [diff] [blame] | 6039 | * the shared secret without PSK. |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6040 | */ |
Przemek Stekiel | b293aaa | 2022-04-19 12:22:38 +0200 | [diff] [blame] | 6041 | if ( key_ex != MBEDTLS_KEY_EXCHANGE_DHE_PSK ) |
| 6042 | { |
| 6043 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 6044 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 6045 | } |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6046 | } |
| 6047 | |
| 6048 | /* |
| 6049 | * PMS = struct { |
| 6050 | * opaque other_secret<0..2^16-1>; |
| 6051 | * opaque psk<0..2^16-1>; |
| 6052 | * }; |
| 6053 | * with "other_secret" depending on the particular key exchange |
| 6054 | */ |
| 6055 | #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) |
| 6056 | if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK ) |
| 6057 | { |
| 6058 | if( end - p < 2 ) |
| 6059 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6060 | |
| 6061 | MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 ); |
| 6062 | p += 2; |
| 6063 | |
| 6064 | if( end < p || (size_t)( end - p ) < psk_len ) |
| 6065 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6066 | |
| 6067 | memset( p, 0, psk_len ); |
| 6068 | p += psk_len; |
| 6069 | } |
| 6070 | else |
| 6071 | #endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */ |
| 6072 | #if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) |
| 6073 | if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK ) |
| 6074 | { |
| 6075 | /* |
| 6076 | * other_secret already set by the ClientKeyExchange message, |
| 6077 | * and is 48 bytes long |
| 6078 | */ |
| 6079 | if( end - p < 2 ) |
| 6080 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6081 | |
| 6082 | *p++ = 0; |
| 6083 | *p++ = 48; |
| 6084 | p += 48; |
| 6085 | } |
| 6086 | else |
| 6087 | #endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */ |
| 6088 | #if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) |
| 6089 | if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK ) |
| 6090 | { |
| 6091 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 6092 | size_t len; |
| 6093 | |
| 6094 | /* Write length only when we know the actual value */ |
| 6095 | if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx, |
| 6096 | p + 2, end - ( p + 2 ), &len, |
| 6097 | ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 ) |
| 6098 | { |
| 6099 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret ); |
| 6100 | return( ret ); |
| 6101 | } |
| 6102 | MBEDTLS_PUT_UINT16_BE( len, p, 0 ); |
| 6103 | p += 2 + len; |
| 6104 | |
| 6105 | MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K ); |
| 6106 | } |
| 6107 | else |
| 6108 | #endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */ |
Neil Armstrong | 80f6f32 | 2022-05-03 17:56:38 +0200 | [diff] [blame] | 6109 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6110 | if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ) |
| 6111 | { |
| 6112 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 6113 | size_t zlen; |
| 6114 | |
| 6115 | if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen, |
| 6116 | p + 2, end - ( p + 2 ), |
| 6117 | ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 ) |
| 6118 | { |
| 6119 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret ); |
| 6120 | return( ret ); |
| 6121 | } |
| 6122 | |
| 6123 | MBEDTLS_PUT_UINT16_BE( zlen, p, 0 ); |
| 6124 | p += 2 + zlen; |
| 6125 | |
| 6126 | MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx, |
| 6127 | MBEDTLS_DEBUG_ECDH_Z ); |
| 6128 | } |
| 6129 | else |
Neil Armstrong | 80f6f32 | 2022-05-03 17:56:38 +0200 | [diff] [blame] | 6130 | #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */ |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6131 | { |
| 6132 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 6133 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 6134 | } |
| 6135 | |
| 6136 | /* opaque psk<0..2^16-1>; */ |
| 6137 | if( end - p < 2 ) |
| 6138 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6139 | |
| 6140 | MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 ); |
| 6141 | p += 2; |
| 6142 | |
| 6143 | if( end < p || (size_t)( end - p ) < psk_len ) |
| 6144 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6145 | |
| 6146 | memcpy( p, psk, psk_len ); |
| 6147 | p += psk_len; |
| 6148 | |
| 6149 | ssl->handshake->pmslen = p - ssl->handshake->premaster; |
| 6150 | |
| 6151 | return( 0 ); |
| 6152 | } |
Neil Armstrong | 80f6f32 | 2022-05-03 17:56:38 +0200 | [diff] [blame] | 6153 | #endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ |
Jerry Yu | c2c673d | 2022-02-17 14:20:39 +0800 | [diff] [blame] | 6154 | |
| 6155 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6156 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | c2c673d | 2022-02-17 14:20:39 +0800 | [diff] [blame] | 6157 | static int ssl_write_hello_request( mbedtls_ssl_context *ssl ); |
| 6158 | |
| 6159 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 6160 | int mbedtls_ssl_resend_hello_request( mbedtls_ssl_context *ssl ) |
| 6161 | { |
| 6162 | /* If renegotiation is not enforced, retransmit until we would reach max |
| 6163 | * timeout if we were using the usual handshake doubling scheme */ |
| 6164 | if( ssl->conf->renego_max_records < 0 ) |
| 6165 | { |
| 6166 | uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1; |
| 6167 | unsigned char doublings = 1; |
| 6168 | |
| 6169 | while( ratio != 0 ) |
| 6170 | { |
| 6171 | ++doublings; |
| 6172 | ratio >>= 1; |
| 6173 | } |
| 6174 | |
| 6175 | if( ++ssl->renego_records_seen > doublings ) |
| 6176 | { |
| 6177 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) ); |
| 6178 | return( 0 ); |
| 6179 | } |
| 6180 | } |
| 6181 | |
| 6182 | return( ssl_write_hello_request( ssl ) ); |
| 6183 | } |
| 6184 | #endif |
| 6185 | #endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */ |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6186 | |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6187 | /* |
| 6188 | * Handshake functions |
| 6189 | */ |
| 6190 | #if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
| 6191 | /* No certificate support -> dummy functions */ |
| 6192 | int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl ) |
| 6193 | { |
| 6194 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 6195 | ssl->handshake->ciphersuite_info; |
| 6196 | |
| 6197 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) ); |
| 6198 | |
| 6199 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) |
| 6200 | { |
| 6201 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) ); |
| 6202 | ssl->state++; |
| 6203 | return( 0 ); |
| 6204 | } |
| 6205 | |
| 6206 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 6207 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 6208 | } |
| 6209 | |
| 6210 | int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl ) |
| 6211 | { |
| 6212 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 6213 | ssl->handshake->ciphersuite_info; |
| 6214 | |
| 6215 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) ); |
| 6216 | |
| 6217 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) |
| 6218 | { |
| 6219 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) ); |
| 6220 | ssl->state++; |
| 6221 | return( 0 ); |
| 6222 | } |
| 6223 | |
| 6224 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 6225 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 6226 | } |
| 6227 | |
| 6228 | #else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 6229 | /* Some certificate support -> implement write and parse */ |
| 6230 | |
| 6231 | int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl ) |
| 6232 | { |
| 6233 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
| 6234 | size_t i, n; |
| 6235 | const mbedtls_x509_crt *crt; |
| 6236 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 6237 | ssl->handshake->ciphersuite_info; |
| 6238 | |
| 6239 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) ); |
| 6240 | |
| 6241 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) |
| 6242 | { |
| 6243 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) ); |
| 6244 | ssl->state++; |
| 6245 | return( 0 ); |
| 6246 | } |
| 6247 | |
| 6248 | #if defined(MBEDTLS_SSL_CLI_C) |
| 6249 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 6250 | { |
| 6251 | if( ssl->handshake->client_auth == 0 ) |
| 6252 | { |
| 6253 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) ); |
| 6254 | ssl->state++; |
| 6255 | return( 0 ); |
| 6256 | } |
| 6257 | } |
| 6258 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 6259 | #if defined(MBEDTLS_SSL_SRV_C) |
| 6260 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 6261 | { |
| 6262 | if( mbedtls_ssl_own_cert( ssl ) == NULL ) |
| 6263 | { |
| 6264 | /* Should never happen because we shouldn't have picked the |
| 6265 | * ciphersuite if we don't have a certificate. */ |
| 6266 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 6267 | } |
| 6268 | } |
| 6269 | #endif |
| 6270 | |
| 6271 | MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) ); |
| 6272 | |
| 6273 | /* |
| 6274 | * 0 . 0 handshake type |
| 6275 | * 1 . 3 handshake length |
| 6276 | * 4 . 6 length of all certs |
| 6277 | * 7 . 9 length of cert. 1 |
| 6278 | * 10 . n-1 peer certificate |
| 6279 | * n . n+2 length of cert. 2 |
| 6280 | * n+3 . ... upper level cert, etc. |
| 6281 | */ |
| 6282 | i = 7; |
| 6283 | crt = mbedtls_ssl_own_cert( ssl ); |
| 6284 | |
| 6285 | while( crt != NULL ) |
| 6286 | { |
| 6287 | n = crt->raw.len; |
| 6288 | if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i ) |
| 6289 | { |
| 6290 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %" MBEDTLS_PRINTF_SIZET |
| 6291 | " > %" MBEDTLS_PRINTF_SIZET, |
| 6292 | i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN ) ); |
| 6293 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 6294 | } |
| 6295 | |
| 6296 | ssl->out_msg[i ] = MBEDTLS_BYTE_2( n ); |
| 6297 | ssl->out_msg[i + 1] = MBEDTLS_BYTE_1( n ); |
| 6298 | ssl->out_msg[i + 2] = MBEDTLS_BYTE_0( n ); |
| 6299 | |
| 6300 | i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n ); |
| 6301 | i += n; crt = crt->next; |
| 6302 | } |
| 6303 | |
| 6304 | ssl->out_msg[4] = MBEDTLS_BYTE_2( i - 7 ); |
| 6305 | ssl->out_msg[5] = MBEDTLS_BYTE_1( i - 7 ); |
| 6306 | ssl->out_msg[6] = MBEDTLS_BYTE_0( i - 7 ); |
| 6307 | |
| 6308 | ssl->out_msglen = i; |
| 6309 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 6310 | ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE; |
| 6311 | |
| 6312 | ssl->state++; |
| 6313 | |
| 6314 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
| 6315 | { |
| 6316 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); |
| 6317 | return( ret ); |
| 6318 | } |
| 6319 | |
| 6320 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) ); |
| 6321 | |
| 6322 | return( ret ); |
| 6323 | } |
| 6324 | |
| 6325 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) |
| 6326 | |
| 6327 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6328 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6329 | static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl, |
| 6330 | unsigned char *crt_buf, |
| 6331 | size_t crt_buf_len ) |
| 6332 | { |
| 6333 | mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert; |
| 6334 | |
| 6335 | if( peer_crt == NULL ) |
| 6336 | return( -1 ); |
| 6337 | |
| 6338 | if( peer_crt->raw.len != crt_buf_len ) |
| 6339 | return( -1 ); |
| 6340 | |
| 6341 | return( memcmp( peer_crt->raw.p, crt_buf, peer_crt->raw.len ) ); |
| 6342 | } |
| 6343 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6344 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6345 | static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl, |
| 6346 | unsigned char *crt_buf, |
| 6347 | size_t crt_buf_len ) |
| 6348 | { |
| 6349 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 6350 | unsigned char const * const peer_cert_digest = |
| 6351 | ssl->session->peer_cert_digest; |
| 6352 | mbedtls_md_type_t const peer_cert_digest_type = |
| 6353 | ssl->session->peer_cert_digest_type; |
| 6354 | mbedtls_md_info_t const * const digest_info = |
| 6355 | mbedtls_md_info_from_type( peer_cert_digest_type ); |
| 6356 | unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN]; |
| 6357 | size_t digest_len; |
| 6358 | |
| 6359 | if( peer_cert_digest == NULL || digest_info == NULL ) |
| 6360 | return( -1 ); |
| 6361 | |
| 6362 | digest_len = mbedtls_md_get_size( digest_info ); |
| 6363 | if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN ) |
| 6364 | return( -1 ); |
| 6365 | |
| 6366 | ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest ); |
| 6367 | if( ret != 0 ) |
| 6368 | return( -1 ); |
| 6369 | |
| 6370 | return( memcmp( tmp_digest, peer_cert_digest, digest_len ) ); |
| 6371 | } |
| 6372 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 6373 | #endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */ |
| 6374 | |
| 6375 | /* |
| 6376 | * Once the certificate message is read, parse it into a cert chain and |
| 6377 | * perform basic checks, but leave actual verification to the caller |
| 6378 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6379 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6380 | static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl, |
| 6381 | mbedtls_x509_crt *chain ) |
| 6382 | { |
| 6383 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 6384 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) |
| 6385 | int crt_cnt=0; |
| 6386 | #endif |
| 6387 | size_t i, n; |
| 6388 | uint8_t alert; |
| 6389 | |
| 6390 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) |
| 6391 | { |
| 6392 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
| 6393 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6394 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
| 6395 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
| 6396 | } |
| 6397 | |
| 6398 | if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ) |
| 6399 | { |
| 6400 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6401 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
| 6402 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
| 6403 | } |
| 6404 | |
| 6405 | if( ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 ) |
| 6406 | { |
| 6407 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
| 6408 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6409 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
| 6410 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
| 6411 | } |
| 6412 | |
| 6413 | i = mbedtls_ssl_hs_hdr_len( ssl ); |
| 6414 | |
| 6415 | /* |
| 6416 | * Same message structure as in mbedtls_ssl_write_certificate() |
| 6417 | */ |
| 6418 | n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2]; |
| 6419 | |
| 6420 | if( ssl->in_msg[i] != 0 || |
| 6421 | ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) ) |
| 6422 | { |
| 6423 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
| 6424 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6425 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
| 6426 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
| 6427 | } |
| 6428 | |
| 6429 | /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */ |
| 6430 | i += 3; |
| 6431 | |
| 6432 | /* Iterate through and parse the CRTs in the provided chain. */ |
| 6433 | while( i < ssl->in_hslen ) |
| 6434 | { |
| 6435 | /* Check that there's room for the next CRT's length fields. */ |
| 6436 | if ( i + 3 > ssl->in_hslen ) { |
| 6437 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
| 6438 | mbedtls_ssl_send_alert_message( ssl, |
| 6439 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6440 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
| 6441 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
| 6442 | } |
| 6443 | /* In theory, the CRT can be up to 2**24 Bytes, but we don't support |
| 6444 | * anything beyond 2**16 ~ 64K. */ |
| 6445 | if( ssl->in_msg[i] != 0 ) |
| 6446 | { |
| 6447 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
| 6448 | mbedtls_ssl_send_alert_message( ssl, |
| 6449 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6450 | MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT ); |
| 6451 | return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE ); |
| 6452 | } |
| 6453 | |
| 6454 | /* Read length of the next CRT in the chain. */ |
| 6455 | n = ( (unsigned int) ssl->in_msg[i + 1] << 8 ) |
| 6456 | | (unsigned int) ssl->in_msg[i + 2]; |
| 6457 | i += 3; |
| 6458 | |
| 6459 | if( n < 128 || i + n > ssl->in_hslen ) |
| 6460 | { |
| 6461 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
| 6462 | mbedtls_ssl_send_alert_message( ssl, |
| 6463 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6464 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
| 6465 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
| 6466 | } |
| 6467 | |
| 6468 | /* Check if we're handling the first CRT in the chain. */ |
| 6469 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) |
| 6470 | if( crt_cnt++ == 0 && |
| 6471 | ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
| 6472 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
| 6473 | { |
| 6474 | /* During client-side renegotiation, check that the server's |
| 6475 | * end-CRTs hasn't changed compared to the initial handshake, |
| 6476 | * mitigating the triple handshake attack. On success, reuse |
| 6477 | * the original end-CRT instead of parsing it again. */ |
| 6478 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) ); |
| 6479 | if( ssl_check_peer_crt_unchanged( ssl, |
| 6480 | &ssl->in_msg[i], |
| 6481 | n ) != 0 ) |
| 6482 | { |
| 6483 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) ); |
| 6484 | mbedtls_ssl_send_alert_message( ssl, |
| 6485 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6486 | MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED ); |
| 6487 | return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE ); |
| 6488 | } |
| 6489 | |
| 6490 | /* Now we can safely free the original chain. */ |
| 6491 | ssl_clear_peer_cert( ssl->session ); |
| 6492 | } |
| 6493 | #endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */ |
| 6494 | |
| 6495 | /* Parse the next certificate in the chain. */ |
| 6496 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 6497 | ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n ); |
| 6498 | #else |
| 6499 | /* If we don't need to store the CRT chain permanently, parse |
| 6500 | * it in-place from the input buffer instead of making a copy. */ |
| 6501 | ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n ); |
| 6502 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 6503 | switch( ret ) |
| 6504 | { |
| 6505 | case 0: /*ok*/ |
| 6506 | case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND: |
| 6507 | /* Ignore certificate with an unknown algorithm: maybe a |
| 6508 | prior certificate was already trusted. */ |
| 6509 | break; |
| 6510 | |
| 6511 | case MBEDTLS_ERR_X509_ALLOC_FAILED: |
| 6512 | alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR; |
| 6513 | goto crt_parse_der_failed; |
| 6514 | |
| 6515 | case MBEDTLS_ERR_X509_UNKNOWN_VERSION: |
| 6516 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 6517 | goto crt_parse_der_failed; |
| 6518 | |
| 6519 | default: |
| 6520 | alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT; |
| 6521 | crt_parse_der_failed: |
| 6522 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert ); |
| 6523 | MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret ); |
| 6524 | return( ret ); |
| 6525 | } |
| 6526 | |
| 6527 | i += n; |
| 6528 | } |
| 6529 | |
| 6530 | MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain ); |
| 6531 | return( 0 ); |
| 6532 | } |
| 6533 | |
| 6534 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6535 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6536 | static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl ) |
| 6537 | { |
| 6538 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 6539 | return( -1 ); |
| 6540 | |
| 6541 | if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) && |
| 6542 | ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 6543 | ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE && |
| 6544 | memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 ) |
| 6545 | { |
Ronald Cron | 1938588 | 2022-06-15 16:26:13 +0200 | [diff] [blame] | 6546 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "peer has no certificate" ) ); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6547 | return( 0 ); |
| 6548 | } |
| 6549 | return( -1 ); |
| 6550 | } |
| 6551 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 6552 | |
| 6553 | /* Check if a certificate message is expected. |
| 6554 | * Return either |
| 6555 | * - SSL_CERTIFICATE_EXPECTED, or |
| 6556 | * - SSL_CERTIFICATE_SKIP |
| 6557 | * indicating whether a Certificate message is expected or not. |
| 6558 | */ |
| 6559 | #define SSL_CERTIFICATE_EXPECTED 0 |
| 6560 | #define SSL_CERTIFICATE_SKIP 1 |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6561 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6562 | static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl, |
| 6563 | int authmode ) |
| 6564 | { |
| 6565 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 6566 | ssl->handshake->ciphersuite_info; |
| 6567 | |
| 6568 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) |
| 6569 | return( SSL_CERTIFICATE_SKIP ); |
| 6570 | |
| 6571 | #if defined(MBEDTLS_SSL_SRV_C) |
| 6572 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 6573 | { |
| 6574 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ) |
| 6575 | return( SSL_CERTIFICATE_SKIP ); |
| 6576 | |
| 6577 | if( authmode == MBEDTLS_SSL_VERIFY_NONE ) |
| 6578 | { |
| 6579 | ssl->session_negotiate->verify_result = |
| 6580 | MBEDTLS_X509_BADCERT_SKIP_VERIFY; |
| 6581 | return( SSL_CERTIFICATE_SKIP ); |
| 6582 | } |
| 6583 | } |
| 6584 | #else |
| 6585 | ((void) authmode); |
| 6586 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 6587 | |
| 6588 | return( SSL_CERTIFICATE_EXPECTED ); |
| 6589 | } |
| 6590 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6591 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6592 | static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl, |
| 6593 | int authmode, |
| 6594 | mbedtls_x509_crt *chain, |
| 6595 | void *rs_ctx ) |
| 6596 | { |
| 6597 | int ret = 0; |
| 6598 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 6599 | ssl->handshake->ciphersuite_info; |
| 6600 | int have_ca_chain = 0; |
| 6601 | |
| 6602 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *); |
| 6603 | void *p_vrfy; |
| 6604 | |
| 6605 | if( authmode == MBEDTLS_SSL_VERIFY_NONE ) |
| 6606 | return( 0 ); |
| 6607 | |
| 6608 | if( ssl->f_vrfy != NULL ) |
| 6609 | { |
| 6610 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) ); |
| 6611 | f_vrfy = ssl->f_vrfy; |
| 6612 | p_vrfy = ssl->p_vrfy; |
| 6613 | } |
| 6614 | else |
| 6615 | { |
| 6616 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) ); |
| 6617 | f_vrfy = ssl->conf->f_vrfy; |
| 6618 | p_vrfy = ssl->conf->p_vrfy; |
| 6619 | } |
| 6620 | |
| 6621 | /* |
| 6622 | * Main check: verify certificate |
| 6623 | */ |
| 6624 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 6625 | if( ssl->conf->f_ca_cb != NULL ) |
| 6626 | { |
| 6627 | ((void) rs_ctx); |
| 6628 | have_ca_chain = 1; |
| 6629 | |
| 6630 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) ); |
| 6631 | ret = mbedtls_x509_crt_verify_with_ca_cb( |
| 6632 | chain, |
| 6633 | ssl->conf->f_ca_cb, |
| 6634 | ssl->conf->p_ca_cb, |
| 6635 | ssl->conf->cert_profile, |
| 6636 | ssl->hostname, |
| 6637 | &ssl->session_negotiate->verify_result, |
| 6638 | f_vrfy, p_vrfy ); |
| 6639 | } |
| 6640 | else |
| 6641 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
| 6642 | { |
| 6643 | mbedtls_x509_crt *ca_chain; |
| 6644 | mbedtls_x509_crl *ca_crl; |
| 6645 | |
| 6646 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 6647 | if( ssl->handshake->sni_ca_chain != NULL ) |
| 6648 | { |
| 6649 | ca_chain = ssl->handshake->sni_ca_chain; |
| 6650 | ca_crl = ssl->handshake->sni_ca_crl; |
| 6651 | } |
| 6652 | else |
| 6653 | #endif |
| 6654 | { |
| 6655 | ca_chain = ssl->conf->ca_chain; |
| 6656 | ca_crl = ssl->conf->ca_crl; |
| 6657 | } |
| 6658 | |
| 6659 | if( ca_chain != NULL ) |
| 6660 | have_ca_chain = 1; |
| 6661 | |
| 6662 | ret = mbedtls_x509_crt_verify_restartable( |
| 6663 | chain, |
| 6664 | ca_chain, ca_crl, |
| 6665 | ssl->conf->cert_profile, |
| 6666 | ssl->hostname, |
| 6667 | &ssl->session_negotiate->verify_result, |
| 6668 | f_vrfy, p_vrfy, rs_ctx ); |
| 6669 | } |
| 6670 | |
| 6671 | if( ret != 0 ) |
| 6672 | { |
| 6673 | MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret ); |
| 6674 | } |
| 6675 | |
| 6676 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
| 6677 | if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS ) |
| 6678 | return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS ); |
| 6679 | #endif |
| 6680 | |
| 6681 | /* |
| 6682 | * Secondary checks: always done, but change 'ret' only if it was 0 |
| 6683 | */ |
| 6684 | |
| 6685 | #if defined(MBEDTLS_ECP_C) |
| 6686 | { |
| 6687 | const mbedtls_pk_context *pk = &chain->pk; |
| 6688 | |
Manuel Pégourié-Gonnard | 66b0d61 | 2022-06-17 10:49:29 +0200 | [diff] [blame] | 6689 | /* If certificate uses an EC key, make sure the curve is OK. |
| 6690 | * This is a public key, so it can't be opaque, so can_do() is a good |
| 6691 | * enough check to ensure pk_ec() is safe to use here. */ |
Leonid Rozenboim | 19e5973 | 2022-08-08 16:52:38 -0700 | [diff] [blame] | 6692 | if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) ) |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6693 | { |
Leonid Rozenboim | 19e5973 | 2022-08-08 16:52:38 -0700 | [diff] [blame] | 6694 | /* and in the unlikely case the above assumption no longer holds |
| 6695 | * we are making sure that pk_ec() here does not return a NULL |
| 6696 | */ |
| 6697 | const mbedtls_ecp_keypair *ec = mbedtls_pk_ec( *pk ); |
| 6698 | if( ec == NULL ) |
| 6699 | { |
Tom Cosgrove | 20c1137 | 2022-08-24 15:06:13 +0100 | [diff] [blame] | 6700 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_pk_ec() returned NULL" ) ); |
Leonid Rozenboim | 19e5973 | 2022-08-08 16:52:38 -0700 | [diff] [blame] | 6701 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 6702 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6703 | |
Leonid Rozenboim | 19e5973 | 2022-08-08 16:52:38 -0700 | [diff] [blame] | 6704 | if( mbedtls_ssl_check_curve( ssl, ec->grp.id ) != 0 ) |
| 6705 | { |
| 6706 | ssl->session_negotiate->verify_result |= |
| 6707 | MBEDTLS_X509_BADCERT_BAD_KEY; |
| 6708 | |
| 6709 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) ); |
| 6710 | if( ret == 0 ) |
| 6711 | ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE; |
| 6712 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6713 | } |
| 6714 | } |
| 6715 | #endif /* MBEDTLS_ECP_C */ |
| 6716 | |
| 6717 | if( mbedtls_ssl_check_cert_usage( chain, |
| 6718 | ciphersuite_info, |
| 6719 | ! ssl->conf->endpoint, |
| 6720 | &ssl->session_negotiate->verify_result ) != 0 ) |
| 6721 | { |
| 6722 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) ); |
| 6723 | if( ret == 0 ) |
| 6724 | ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE; |
| 6725 | } |
| 6726 | |
| 6727 | /* mbedtls_x509_crt_verify_with_profile is supposed to report a |
| 6728 | * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED, |
| 6729 | * with details encoded in the verification flags. All other kinds |
| 6730 | * of error codes, including those from the user provided f_vrfy |
| 6731 | * functions, are treated as fatal and lead to a failure of |
| 6732 | * ssl_parse_certificate even if verification was optional. */ |
| 6733 | if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL && |
| 6734 | ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED || |
| 6735 | ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE ) ) |
| 6736 | { |
| 6737 | ret = 0; |
| 6738 | } |
| 6739 | |
| 6740 | if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED ) |
| 6741 | { |
| 6742 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) ); |
| 6743 | ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED; |
| 6744 | } |
| 6745 | |
| 6746 | if( ret != 0 ) |
| 6747 | { |
| 6748 | uint8_t alert; |
| 6749 | |
| 6750 | /* The certificate may have been rejected for several reasons. |
| 6751 | Pick one and send the corresponding alert. Which alert to send |
| 6752 | may be a subject of debate in some cases. */ |
| 6753 | if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER ) |
| 6754 | alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED; |
| 6755 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH ) |
| 6756 | alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT; |
| 6757 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE ) |
| 6758 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 6759 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE ) |
| 6760 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 6761 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE ) |
| 6762 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 6763 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK ) |
| 6764 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 6765 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY ) |
| 6766 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 6767 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED ) |
| 6768 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED; |
| 6769 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED ) |
| 6770 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED; |
| 6771 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED ) |
| 6772 | alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA; |
| 6773 | else |
| 6774 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN; |
| 6775 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6776 | alert ); |
| 6777 | } |
| 6778 | |
| 6779 | #if defined(MBEDTLS_DEBUG_C) |
| 6780 | if( ssl->session_negotiate->verify_result != 0 ) |
| 6781 | { |
| 6782 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %08x", |
| 6783 | (unsigned int) ssl->session_negotiate->verify_result ) ); |
| 6784 | } |
| 6785 | else |
| 6786 | { |
| 6787 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) ); |
| 6788 | } |
| 6789 | #endif /* MBEDTLS_DEBUG_C */ |
| 6790 | |
| 6791 | return( ret ); |
| 6792 | } |
| 6793 | |
| 6794 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6795 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6796 | static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl, |
| 6797 | unsigned char *start, size_t len ) |
| 6798 | { |
| 6799 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 6800 | /* Remember digest of the peer's end-CRT. */ |
| 6801 | ssl->session_negotiate->peer_cert_digest = |
| 6802 | mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ); |
| 6803 | if( ssl->session_negotiate->peer_cert_digest == NULL ) |
| 6804 | { |
| 6805 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", |
| 6806 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ); |
| 6807 | mbedtls_ssl_send_alert_message( ssl, |
| 6808 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6809 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
| 6810 | |
| 6811 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 6812 | } |
| 6813 | |
| 6814 | ret = mbedtls_md( mbedtls_md_info_from_type( |
| 6815 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ), |
| 6816 | start, len, |
| 6817 | ssl->session_negotiate->peer_cert_digest ); |
| 6818 | |
| 6819 | ssl->session_negotiate->peer_cert_digest_type = |
| 6820 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE; |
| 6821 | ssl->session_negotiate->peer_cert_digest_len = |
| 6822 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN; |
| 6823 | |
| 6824 | return( ret ); |
| 6825 | } |
| 6826 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6827 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6828 | static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl, |
| 6829 | unsigned char *start, size_t len ) |
| 6830 | { |
| 6831 | unsigned char *end = start + len; |
| 6832 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 6833 | |
| 6834 | /* Make a copy of the peer's raw public key. */ |
| 6835 | mbedtls_pk_init( &ssl->handshake->peer_pubkey ); |
| 6836 | ret = mbedtls_pk_parse_subpubkey( &start, end, |
| 6837 | &ssl->handshake->peer_pubkey ); |
| 6838 | if( ret != 0 ) |
| 6839 | { |
| 6840 | /* We should have parsed the public key before. */ |
| 6841 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 6842 | } |
| 6843 | |
| 6844 | return( 0 ); |
| 6845 | } |
| 6846 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 6847 | |
| 6848 | int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl ) |
| 6849 | { |
| 6850 | int ret = 0; |
| 6851 | int crt_expected; |
| 6852 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 6853 | const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET |
| 6854 | ? ssl->handshake->sni_authmode |
| 6855 | : ssl->conf->authmode; |
| 6856 | #else |
| 6857 | const int authmode = ssl->conf->authmode; |
| 6858 | #endif |
| 6859 | void *rs_ctx = NULL; |
| 6860 | mbedtls_x509_crt *chain = NULL; |
| 6861 | |
| 6862 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) ); |
| 6863 | |
| 6864 | crt_expected = ssl_parse_certificate_coordinate( ssl, authmode ); |
| 6865 | if( crt_expected == SSL_CERTIFICATE_SKIP ) |
| 6866 | { |
| 6867 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) ); |
| 6868 | goto exit; |
| 6869 | } |
| 6870 | |
| 6871 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
| 6872 | if( ssl->handshake->ecrs_enabled && |
| 6873 | ssl->handshake->ecrs_state == ssl_ecrs_crt_verify ) |
| 6874 | { |
| 6875 | chain = ssl->handshake->ecrs_peer_cert; |
| 6876 | ssl->handshake->ecrs_peer_cert = NULL; |
| 6877 | goto crt_verify; |
| 6878 | } |
| 6879 | #endif |
| 6880 | |
| 6881 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) |
| 6882 | { |
| 6883 | /* mbedtls_ssl_read_record may have sent an alert already. We |
| 6884 | let it decide whether to alert. */ |
| 6885 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
| 6886 | goto exit; |
| 6887 | } |
| 6888 | |
| 6889 | #if defined(MBEDTLS_SSL_SRV_C) |
| 6890 | if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 ) |
| 6891 | { |
| 6892 | ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING; |
| 6893 | |
| 6894 | if( authmode != MBEDTLS_SSL_VERIFY_OPTIONAL ) |
| 6895 | ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE; |
| 6896 | |
| 6897 | goto exit; |
| 6898 | } |
| 6899 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 6900 | |
| 6901 | /* Clear existing peer CRT structure in case we tried to |
| 6902 | * reuse a session but it failed, and allocate a new one. */ |
| 6903 | ssl_clear_peer_cert( ssl->session_negotiate ); |
| 6904 | |
| 6905 | chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ); |
| 6906 | if( chain == NULL ) |
| 6907 | { |
| 6908 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", |
| 6909 | sizeof( mbedtls_x509_crt ) ) ); |
| 6910 | mbedtls_ssl_send_alert_message( ssl, |
| 6911 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6912 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
| 6913 | |
| 6914 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 6915 | goto exit; |
| 6916 | } |
| 6917 | mbedtls_x509_crt_init( chain ); |
| 6918 | |
| 6919 | ret = ssl_parse_certificate_chain( ssl, chain ); |
| 6920 | if( ret != 0 ) |
| 6921 | goto exit; |
| 6922 | |
| 6923 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
| 6924 | if( ssl->handshake->ecrs_enabled) |
| 6925 | ssl->handshake->ecrs_state = ssl_ecrs_crt_verify; |
| 6926 | |
| 6927 | crt_verify: |
| 6928 | if( ssl->handshake->ecrs_enabled) |
| 6929 | rs_ctx = &ssl->handshake->ecrs_ctx; |
| 6930 | #endif |
| 6931 | |
| 6932 | ret = ssl_parse_certificate_verify( ssl, authmode, |
| 6933 | chain, rs_ctx ); |
| 6934 | if( ret != 0 ) |
| 6935 | goto exit; |
| 6936 | |
| 6937 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 6938 | { |
| 6939 | unsigned char *crt_start, *pk_start; |
| 6940 | size_t crt_len, pk_len; |
| 6941 | |
| 6942 | /* We parse the CRT chain without copying, so |
| 6943 | * these pointers point into the input buffer, |
| 6944 | * and are hence still valid after freeing the |
| 6945 | * CRT chain. */ |
| 6946 | |
| 6947 | crt_start = chain->raw.p; |
| 6948 | crt_len = chain->raw.len; |
| 6949 | |
| 6950 | pk_start = chain->pk_raw.p; |
| 6951 | pk_len = chain->pk_raw.len; |
| 6952 | |
| 6953 | /* Free the CRT structures before computing |
| 6954 | * digest and copying the peer's public key. */ |
| 6955 | mbedtls_x509_crt_free( chain ); |
| 6956 | mbedtls_free( chain ); |
| 6957 | chain = NULL; |
| 6958 | |
| 6959 | ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len ); |
| 6960 | if( ret != 0 ) |
| 6961 | goto exit; |
| 6962 | |
| 6963 | ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len ); |
| 6964 | if( ret != 0 ) |
| 6965 | goto exit; |
| 6966 | } |
| 6967 | #else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 6968 | /* Pass ownership to session structure. */ |
| 6969 | ssl->session_negotiate->peer_cert = chain; |
| 6970 | chain = NULL; |
| 6971 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 6972 | |
| 6973 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) ); |
| 6974 | |
| 6975 | exit: |
| 6976 | |
| 6977 | if( ret == 0 ) |
| 6978 | ssl->state++; |
| 6979 | |
| 6980 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
| 6981 | if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS ) |
| 6982 | { |
| 6983 | ssl->handshake->ecrs_peer_cert = chain; |
| 6984 | chain = NULL; |
| 6985 | } |
| 6986 | #endif |
| 6987 | |
| 6988 | if( chain != NULL ) |
| 6989 | { |
| 6990 | mbedtls_x509_crt_free( chain ); |
| 6991 | mbedtls_free( chain ); |
| 6992 | } |
| 6993 | |
| 6994 | return( ret ); |
| 6995 | } |
| 6996 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 6997 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 6998 | #if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 6999 | static void ssl_calc_finished_tls_sha256( |
| 7000 | mbedtls_ssl_context *ssl, unsigned char *buf, int from ) |
| 7001 | { |
| 7002 | int len = 12; |
| 7003 | const char *sender; |
| 7004 | unsigned char padbuf[32]; |
| 7005 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7006 | size_t hash_size; |
| 7007 | psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT; |
| 7008 | psa_status_t status; |
| 7009 | #else |
| 7010 | mbedtls_sha256_context sha256; |
| 7011 | #endif |
| 7012 | |
| 7013 | mbedtls_ssl_session *session = ssl->session_negotiate; |
| 7014 | if( !session ) |
| 7015 | session = ssl->session; |
| 7016 | |
| 7017 | sender = ( from == MBEDTLS_SSL_IS_CLIENT ) |
| 7018 | ? "client finished" |
| 7019 | : "server finished"; |
| 7020 | |
| 7021 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7022 | sha256_psa = psa_hash_operation_init(); |
| 7023 | |
| 7024 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) ); |
| 7025 | |
| 7026 | status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa ); |
| 7027 | if( status != PSA_SUCCESS ) |
| 7028 | { |
| 7029 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); |
| 7030 | return; |
| 7031 | } |
| 7032 | |
| 7033 | status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size ); |
| 7034 | if( status != PSA_SUCCESS ) |
| 7035 | { |
| 7036 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); |
| 7037 | return; |
| 7038 | } |
| 7039 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 ); |
| 7040 | #else |
| 7041 | |
| 7042 | mbedtls_sha256_init( &sha256 ); |
| 7043 | |
| 7044 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) ); |
| 7045 | |
| 7046 | mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 ); |
| 7047 | |
| 7048 | /* |
| 7049 | * TLSv1.2: |
| 7050 | * hash = PRF( master, finished_label, |
| 7051 | * Hash( handshake ) )[0.11] |
| 7052 | */ |
| 7053 | |
| 7054 | #if !defined(MBEDTLS_SHA256_ALT) |
| 7055 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *) |
| 7056 | sha256.state, sizeof( sha256.state ) ); |
| 7057 | #endif |
| 7058 | |
| 7059 | mbedtls_sha256_finish( &sha256, padbuf ); |
| 7060 | mbedtls_sha256_free( &sha256 ); |
| 7061 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 7062 | |
| 7063 | ssl->handshake->tls_prf( session->master, 48, sender, |
| 7064 | padbuf, 32, buf, len ); |
| 7065 | |
| 7066 | MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len ); |
| 7067 | |
| 7068 | mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) ); |
| 7069 | |
| 7070 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) ); |
| 7071 | } |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 7072 | #endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/ |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 7073 | |
Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 7074 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 7075 | #if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 7076 | static void ssl_calc_finished_tls_sha384( |
| 7077 | mbedtls_ssl_context *ssl, unsigned char *buf, int from ) |
| 7078 | { |
| 7079 | int len = 12; |
| 7080 | const char *sender; |
| 7081 | unsigned char padbuf[48]; |
| 7082 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7083 | size_t hash_size; |
| 7084 | psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT; |
| 7085 | psa_status_t status; |
| 7086 | #else |
| 7087 | mbedtls_sha512_context sha512; |
| 7088 | #endif |
| 7089 | |
| 7090 | mbedtls_ssl_session *session = ssl->session_negotiate; |
| 7091 | if( !session ) |
| 7092 | session = ssl->session; |
| 7093 | |
| 7094 | sender = ( from == MBEDTLS_SSL_IS_CLIENT ) |
| 7095 | ? "client finished" |
| 7096 | : "server finished"; |
| 7097 | |
| 7098 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7099 | sha384_psa = psa_hash_operation_init(); |
| 7100 | |
| 7101 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) ); |
| 7102 | |
| 7103 | status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa ); |
| 7104 | if( status != PSA_SUCCESS ) |
| 7105 | { |
| 7106 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); |
| 7107 | return; |
| 7108 | } |
| 7109 | |
| 7110 | status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size ); |
| 7111 | if( status != PSA_SUCCESS ) |
| 7112 | { |
| 7113 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); |
| 7114 | return; |
| 7115 | } |
| 7116 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 ); |
| 7117 | #else |
| 7118 | mbedtls_sha512_init( &sha512 ); |
| 7119 | |
| 7120 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) ); |
| 7121 | |
Andrzej Kurek | a242e83 | 2022-08-11 10:03:14 -0400 | [diff] [blame] | 7122 | mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 ); |
Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 7123 | |
| 7124 | /* |
| 7125 | * TLSv1.2: |
| 7126 | * hash = PRF( master, finished_label, |
| 7127 | * Hash( handshake ) )[0.11] |
| 7128 | */ |
| 7129 | |
| 7130 | #if !defined(MBEDTLS_SHA512_ALT) |
| 7131 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *) |
| 7132 | sha512.state, sizeof( sha512.state ) ); |
| 7133 | #endif |
| 7134 | mbedtls_sha512_finish( &sha512, padbuf ); |
| 7135 | |
| 7136 | mbedtls_sha512_free( &sha512 ); |
| 7137 | #endif |
| 7138 | |
| 7139 | ssl->handshake->tls_prf( session->master, 48, sender, |
| 7140 | padbuf, 48, buf, len ); |
| 7141 | |
| 7142 | MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len ); |
| 7143 | |
| 7144 | mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) ); |
| 7145 | |
| 7146 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) ); |
| 7147 | } |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 7148 | #endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/ |
Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 7149 | |
Jerry Yu | aef0015 | 2022-02-17 14:27:31 +0800 | [diff] [blame] | 7150 | void mbedtls_ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl ) |
| 7151 | { |
| 7152 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) ); |
| 7153 | |
| 7154 | /* |
| 7155 | * Free our handshake params |
| 7156 | */ |
| 7157 | mbedtls_ssl_handshake_free( ssl ); |
| 7158 | mbedtls_free( ssl->handshake ); |
| 7159 | ssl->handshake = NULL; |
| 7160 | |
| 7161 | /* |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 7162 | * Free the previous transform and switch in the current one |
Jerry Yu | aef0015 | 2022-02-17 14:27:31 +0800 | [diff] [blame] | 7163 | */ |
| 7164 | if( ssl->transform ) |
| 7165 | { |
| 7166 | mbedtls_ssl_transform_free( ssl->transform ); |
| 7167 | mbedtls_free( ssl->transform ); |
| 7168 | } |
| 7169 | ssl->transform = ssl->transform_negotiate; |
| 7170 | ssl->transform_negotiate = NULL; |
| 7171 | |
| 7172 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) ); |
| 7173 | } |
| 7174 | |
Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 7175 | void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl ) |
| 7176 | { |
| 7177 | int resume = ssl->handshake->resume; |
| 7178 | |
| 7179 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) ); |
| 7180 | |
| 7181 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 7182 | if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
| 7183 | { |
| 7184 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE; |
| 7185 | ssl->renego_records_seen = 0; |
| 7186 | } |
| 7187 | #endif |
| 7188 | |
| 7189 | /* |
| 7190 | * Free the previous session and switch in the current one |
| 7191 | */ |
| 7192 | if( ssl->session ) |
| 7193 | { |
| 7194 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 7195 | /* RFC 7366 3.1: keep the EtM state */ |
| 7196 | ssl->session_negotiate->encrypt_then_mac = |
| 7197 | ssl->session->encrypt_then_mac; |
| 7198 | #endif |
| 7199 | |
| 7200 | mbedtls_ssl_session_free( ssl->session ); |
| 7201 | mbedtls_free( ssl->session ); |
| 7202 | } |
| 7203 | ssl->session = ssl->session_negotiate; |
| 7204 | ssl->session_negotiate = NULL; |
| 7205 | |
| 7206 | /* |
| 7207 | * Add cache entry |
| 7208 | */ |
| 7209 | if( ssl->conf->f_set_cache != NULL && |
| 7210 | ssl->session->id_len != 0 && |
| 7211 | resume == 0 ) |
| 7212 | { |
| 7213 | if( ssl->conf->f_set_cache( ssl->conf->p_cache, |
| 7214 | ssl->session->id, |
| 7215 | ssl->session->id_len, |
| 7216 | ssl->session ) != 0 ) |
| 7217 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) ); |
| 7218 | } |
| 7219 | |
| 7220 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 7221 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 7222 | ssl->handshake->flight != NULL ) |
| 7223 | { |
| 7224 | /* Cancel handshake timer */ |
| 7225 | mbedtls_ssl_set_timer( ssl, 0 ); |
| 7226 | |
| 7227 | /* Keep last flight around in case we need to resend it: |
| 7228 | * we need the handshake and transform structures for that */ |
| 7229 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) ); |
| 7230 | } |
| 7231 | else |
| 7232 | #endif |
| 7233 | mbedtls_ssl_handshake_wrapup_free_hs_transform( ssl ); |
| 7234 | |
| 7235 | ssl->state++; |
| 7236 | |
| 7237 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) ); |
| 7238 | } |
| 7239 | |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 7240 | int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl ) |
| 7241 | { |
| 7242 | int ret, hash_len; |
| 7243 | |
| 7244 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) ); |
| 7245 | |
| 7246 | mbedtls_ssl_update_out_pointers( ssl, ssl->transform_negotiate ); |
| 7247 | |
| 7248 | ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint ); |
| 7249 | |
| 7250 | /* |
| 7251 | * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites |
| 7252 | * may define some other value. Currently (early 2016), no defined |
| 7253 | * ciphersuite does this (and this is unlikely to change as activity has |
| 7254 | * moved to TLS 1.3 now) so we can keep the hardcoded 12 here. |
| 7255 | */ |
| 7256 | hash_len = 12; |
| 7257 | |
| 7258 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 7259 | ssl->verify_data_len = hash_len; |
| 7260 | memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len ); |
| 7261 | #endif |
| 7262 | |
| 7263 | ssl->out_msglen = 4 + hash_len; |
| 7264 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 7265 | ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED; |
| 7266 | |
| 7267 | /* |
| 7268 | * In case of session resuming, invert the client and server |
| 7269 | * ChangeCipherSpec messages order. |
| 7270 | */ |
| 7271 | if( ssl->handshake->resume != 0 ) |
| 7272 | { |
| 7273 | #if defined(MBEDTLS_SSL_CLI_C) |
| 7274 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 7275 | ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP; |
| 7276 | #endif |
| 7277 | #if defined(MBEDTLS_SSL_SRV_C) |
| 7278 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 7279 | ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC; |
| 7280 | #endif |
| 7281 | } |
| 7282 | else |
| 7283 | ssl->state++; |
| 7284 | |
| 7285 | /* |
| 7286 | * Switch to our negotiated transform and session parameters for outbound |
| 7287 | * data. |
| 7288 | */ |
| 7289 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) ); |
| 7290 | |
| 7291 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 7292 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 7293 | { |
| 7294 | unsigned char i; |
| 7295 | |
| 7296 | /* Remember current epoch settings for resending */ |
| 7297 | ssl->handshake->alt_transform_out = ssl->transform_out; |
| 7298 | memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, |
| 7299 | sizeof( ssl->handshake->alt_out_ctr ) ); |
| 7300 | |
| 7301 | /* Set sequence_number to zero */ |
| 7302 | memset( &ssl->cur_out_ctr[2], 0, sizeof( ssl->cur_out_ctr ) - 2 ); |
| 7303 | |
| 7304 | |
| 7305 | /* Increment epoch */ |
| 7306 | for( i = 2; i > 0; i-- ) |
| 7307 | if( ++ssl->cur_out_ctr[i - 1] != 0 ) |
| 7308 | break; |
| 7309 | |
| 7310 | /* The loop goes to its end iff the counter is wrapping */ |
| 7311 | if( i == 0 ) |
| 7312 | { |
| 7313 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) ); |
| 7314 | return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING ); |
| 7315 | } |
| 7316 | } |
| 7317 | else |
| 7318 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 7319 | memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) ); |
| 7320 | |
| 7321 | ssl->transform_out = ssl->transform_negotiate; |
| 7322 | ssl->session_out = ssl->session_negotiate; |
| 7323 | |
| 7324 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 7325 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 7326 | mbedtls_ssl_send_flight_completed( ssl ); |
| 7327 | #endif |
| 7328 | |
| 7329 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
| 7330 | { |
| 7331 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); |
| 7332 | return( ret ); |
| 7333 | } |
| 7334 | |
| 7335 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 7336 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 7337 | ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 ) |
| 7338 | { |
| 7339 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret ); |
| 7340 | return( ret ); |
| 7341 | } |
| 7342 | #endif |
| 7343 | |
| 7344 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) ); |
| 7345 | |
| 7346 | return( 0 ); |
| 7347 | } |
| 7348 | |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 7349 | #define SSL_MAX_HASH_LEN 12 |
| 7350 | |
| 7351 | int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl ) |
| 7352 | { |
| 7353 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 7354 | unsigned int hash_len = 12; |
| 7355 | unsigned char buf[SSL_MAX_HASH_LEN]; |
| 7356 | |
| 7357 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) ); |
| 7358 | |
| 7359 | ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 ); |
| 7360 | |
| 7361 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) |
| 7362 | { |
| 7363 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
| 7364 | goto exit; |
| 7365 | } |
| 7366 | |
| 7367 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) |
| 7368 | { |
| 7369 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) ); |
| 7370 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7371 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
| 7372 | ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; |
| 7373 | goto exit; |
| 7374 | } |
| 7375 | |
| 7376 | if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ) |
| 7377 | { |
| 7378 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7379 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
| 7380 | ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; |
| 7381 | goto exit; |
| 7382 | } |
| 7383 | |
| 7384 | if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len ) |
| 7385 | { |
| 7386 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) ); |
| 7387 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7388 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
| 7389 | ret = MBEDTLS_ERR_SSL_DECODE_ERROR; |
| 7390 | goto exit; |
| 7391 | } |
| 7392 | |
| 7393 | if( mbedtls_ct_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), |
| 7394 | buf, hash_len ) != 0 ) |
| 7395 | { |
| 7396 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) ); |
| 7397 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7398 | MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR ); |
| 7399 | ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE; |
| 7400 | goto exit; |
| 7401 | } |
| 7402 | |
| 7403 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 7404 | ssl->verify_data_len = hash_len; |
| 7405 | memcpy( ssl->peer_verify_data, buf, hash_len ); |
| 7406 | #endif |
| 7407 | |
| 7408 | if( ssl->handshake->resume != 0 ) |
| 7409 | { |
| 7410 | #if defined(MBEDTLS_SSL_CLI_C) |
| 7411 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 7412 | ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC; |
| 7413 | #endif |
| 7414 | #if defined(MBEDTLS_SSL_SRV_C) |
| 7415 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 7416 | ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP; |
| 7417 | #endif |
| 7418 | } |
| 7419 | else |
| 7420 | ssl->state++; |
| 7421 | |
| 7422 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 7423 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 7424 | mbedtls_ssl_recv_flight_completed( ssl ); |
| 7425 | #endif |
| 7426 | |
| 7427 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) ); |
| 7428 | |
| 7429 | exit: |
| 7430 | mbedtls_platform_zeroize( buf, hash_len ); |
| 7431 | return( ret ); |
| 7432 | } |
| 7433 | |
Jerry Yu | 392112c | 2022-02-17 14:34:10 +0800 | [diff] [blame] | 7434 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
| 7435 | /* |
| 7436 | * Helper to get TLS 1.2 PRF from ciphersuite |
| 7437 | * (Duplicates bits of logic from ssl_set_handshake_prfs().) |
| 7438 | */ |
Andrzej Kurek | 894edde | 2022-09-29 06:31:14 -0400 | [diff] [blame] | 7439 | #if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) || \ |
| 7440 | defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Jerry Yu | 392112c | 2022-02-17 14:34:10 +0800 | [diff] [blame] | 7441 | static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id ) |
| 7442 | { |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 7443 | #if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Jerry Yu | 392112c | 2022-02-17 14:34:10 +0800 | [diff] [blame] | 7444 | const mbedtls_ssl_ciphersuite_t * const ciphersuite_info = |
| 7445 | mbedtls_ssl_ciphersuite_from_id( ciphersuite_id ); |
| 7446 | |
Leonid Rozenboim | e9d8dcd | 2022-08-08 15:57:48 -0700 | [diff] [blame] | 7447 | if( ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA384 ) |
Jerry Yu | 392112c | 2022-02-17 14:34:10 +0800 | [diff] [blame] | 7448 | return( tls_prf_sha384 ); |
Andrzej Kurek | 894edde | 2022-09-29 06:31:14 -0400 | [diff] [blame] | 7449 | else |
Jerry Yu | 392112c | 2022-02-17 14:34:10 +0800 | [diff] [blame] | 7450 | #endif |
Andrzej Kurek | 894edde | 2022-09-29 06:31:14 -0400 | [diff] [blame] | 7451 | #if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
| 7452 | { |
| 7453 | if( ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA256 ) |
| 7454 | return( tls_prf_sha256 ); |
| 7455 | } |
| 7456 | #endif |
| 7457 | #if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \ |
| 7458 | !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
| 7459 | (void) ciphersuite_info; |
| 7460 | #endif |
| 7461 | return( NULL ); |
Jerry Yu | 392112c | 2022-02-17 14:34:10 +0800 | [diff] [blame] | 7462 | } |
Andrzej Kurek | 894edde | 2022-09-29 06:31:14 -0400 | [diff] [blame] | 7463 | #endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA || |
| 7464 | MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */ |
Jerry Yu | 392112c | 2022-02-17 14:34:10 +0800 | [diff] [blame] | 7465 | #endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */ |
Jerry Yu | e93ffcd | 2022-02-17 14:37:06 +0800 | [diff] [blame] | 7466 | |
| 7467 | static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf ) |
| 7468 | { |
| 7469 | ((void) tls_prf); |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 7470 | #if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Jerry Yu | e93ffcd | 2022-02-17 14:37:06 +0800 | [diff] [blame] | 7471 | if( tls_prf == tls_prf_sha384 ) |
| 7472 | { |
| 7473 | return( MBEDTLS_SSL_TLS_PRF_SHA384 ); |
| 7474 | } |
| 7475 | else |
| 7476 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 7477 | #if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Jerry Yu | e93ffcd | 2022-02-17 14:37:06 +0800 | [diff] [blame] | 7478 | if( tls_prf == tls_prf_sha256 ) |
| 7479 | { |
| 7480 | return( MBEDTLS_SSL_TLS_PRF_SHA256 ); |
| 7481 | } |
| 7482 | else |
| 7483 | #endif |
| 7484 | return( MBEDTLS_SSL_TLS_PRF_NONE ); |
| 7485 | } |
| 7486 | |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7487 | /* |
| 7488 | * Populate a transform structure with session keys and all the other |
| 7489 | * necessary information. |
| 7490 | * |
| 7491 | * Parameters: |
| 7492 | * - [in/out]: transform: structure to populate |
| 7493 | * [in] must be just initialised with mbedtls_ssl_transform_init() |
| 7494 | * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf() |
| 7495 | * - [in] ciphersuite |
| 7496 | * - [in] master |
| 7497 | * - [in] encrypt_then_mac |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7498 | * - [in] tls_prf: pointer to PRF to use for key derivation |
| 7499 | * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random |
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 7500 | * - [in] tls_version: TLS version |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7501 | * - [in] endpoint: client or server |
| 7502 | * - [in] ssl: used for: |
| 7503 | * - ssl->conf->{f,p}_export_keys |
| 7504 | * [in] optionally used for: |
| 7505 | * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg |
| 7506 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 7507 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7508 | static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform, |
| 7509 | int ciphersuite, |
| 7510 | const unsigned char master[48], |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 7511 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7512 | int encrypt_then_mac, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 7513 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7514 | ssl_tls_prf_t tls_prf, |
| 7515 | const unsigned char randbytes[64], |
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 7516 | mbedtls_ssl_protocol_version tls_version, |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7517 | unsigned endpoint, |
| 7518 | const mbedtls_ssl_context *ssl ) |
| 7519 | { |
| 7520 | int ret = 0; |
| 7521 | unsigned char keyblk[256]; |
| 7522 | unsigned char *key1; |
| 7523 | unsigned char *key2; |
| 7524 | unsigned char *mac_enc; |
| 7525 | unsigned char *mac_dec; |
| 7526 | size_t mac_key_len = 0; |
| 7527 | size_t iv_copy_len; |
| 7528 | size_t keylen; |
| 7529 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info; |
Neil Armstrong | 7fea33e | 2022-04-01 15:40:25 +0200 | [diff] [blame] | 7530 | mbedtls_ssl_mode_t ssl_mode; |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7531 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7532 | const mbedtls_cipher_info_t *cipher_info; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7533 | const mbedtls_md_info_t *md_info; |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7534 | #endif /* !MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7535 | |
| 7536 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7537 | psa_key_type_t key_type; |
| 7538 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7539 | psa_algorithm_t alg; |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7540 | psa_algorithm_t mac_alg = 0; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7541 | size_t key_bits; |
| 7542 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 7543 | #endif |
| 7544 | |
| 7545 | #if !defined(MBEDTLS_DEBUG_C) && \ |
| 7546 | !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 7547 | if( ssl->f_export_keys == NULL ) |
| 7548 | { |
| 7549 | ssl = NULL; /* make sure we don't use it except for these cases */ |
| 7550 | (void) ssl; |
| 7551 | } |
| 7552 | #endif |
| 7553 | |
| 7554 | /* |
| 7555 | * Some data just needs copying into the structure |
| 7556 | */ |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 7557 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7558 | transform->encrypt_then_mac = encrypt_then_mac; |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 7559 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 7560 | transform->tls_version = tls_version; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7561 | |
| 7562 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
| 7563 | memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) ); |
| 7564 | #endif |
| 7565 | |
| 7566 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 7567 | if( tls_version == MBEDTLS_SSL_VERSION_TLS1_3 ) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7568 | { |
| 7569 | /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform |
| 7570 | * generation separate. This should never happen. */ |
| 7571 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 7572 | } |
| 7573 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 7574 | |
| 7575 | /* |
| 7576 | * Get various info structures |
| 7577 | */ |
| 7578 | ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite ); |
| 7579 | if( ciphersuite_info == NULL ) |
| 7580 | { |
| 7581 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found", |
| 7582 | ciphersuite ) ); |
| 7583 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 7584 | } |
| 7585 | |
Neil Armstrong | ab555e0 | 2022-04-04 11:07:59 +0200 | [diff] [blame] | 7586 | ssl_mode = mbedtls_ssl_get_mode_from_ciphersuite( |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 7587 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Neil Armstrong | 7fea33e | 2022-04-01 15:40:25 +0200 | [diff] [blame] | 7588 | encrypt_then_mac, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 7589 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Neil Armstrong | 7fea33e | 2022-04-01 15:40:25 +0200 | [diff] [blame] | 7590 | ciphersuite_info ); |
| 7591 | |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7592 | if( ssl_mode == MBEDTLS_SSL_MODE_AEAD ) |
| 7593 | transform->taglen = |
| 7594 | ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16; |
| 7595 | |
| 7596 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7597 | if( ( status = mbedtls_ssl_cipher_to_psa( ciphersuite_info->cipher, |
| 7598 | transform->taglen, |
| 7599 | &alg, |
| 7600 | &key_type, |
| 7601 | &key_bits ) ) != PSA_SUCCESS ) |
| 7602 | { |
| 7603 | ret = psa_ssl_status_to_mbedtls( status ); |
| 7604 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_cipher_to_psa", ret ); |
| 7605 | goto end; |
| 7606 | } |
| 7607 | #else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7608 | cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher ); |
| 7609 | if( cipher_info == NULL ) |
| 7610 | { |
| 7611 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %u not found", |
| 7612 | ciphersuite_info->cipher ) ); |
| 7613 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 7614 | } |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7615 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7616 | |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7617 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Manuel Pégourié-Gonnard | abac037 | 2022-07-18 13:41:11 +0200 | [diff] [blame] | 7618 | mac_alg = mbedtls_hash_info_psa_from_md( ciphersuite_info->mac ); |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7619 | if( mac_alg == 0 ) |
| 7620 | { |
Manuel Pégourié-Gonnard | abac037 | 2022-07-18 13:41:11 +0200 | [diff] [blame] | 7621 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_hash_info_psa_from_md for %u not found", |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7622 | (unsigned) ciphersuite_info->mac ) ); |
| 7623 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 7624 | } |
| 7625 | #else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7626 | md_info = mbedtls_md_info_from_type( ciphersuite_info->mac ); |
| 7627 | if( md_info == NULL ) |
| 7628 | { |
| 7629 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %u not found", |
| 7630 | (unsigned) ciphersuite_info->mac ) ); |
| 7631 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 7632 | } |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7633 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7634 | |
| 7635 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 7636 | /* Copy own and peer's CID if the use of the CID |
| 7637 | * extension has been negotiated. */ |
| 7638 | if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED ) |
| 7639 | { |
| 7640 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) ); |
| 7641 | |
| 7642 | transform->in_cid_len = ssl->own_cid_len; |
| 7643 | memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len ); |
| 7644 | MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid, |
| 7645 | transform->in_cid_len ); |
| 7646 | |
| 7647 | transform->out_cid_len = ssl->handshake->peer_cid_len; |
| 7648 | memcpy( transform->out_cid, ssl->handshake->peer_cid, |
| 7649 | ssl->handshake->peer_cid_len ); |
| 7650 | MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid, |
| 7651 | transform->out_cid_len ); |
| 7652 | } |
| 7653 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 7654 | |
| 7655 | /* |
| 7656 | * Compute key block using the PRF |
| 7657 | */ |
| 7658 | ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 ); |
| 7659 | if( ret != 0 ) |
| 7660 | { |
| 7661 | MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret ); |
| 7662 | return( ret ); |
| 7663 | } |
| 7664 | |
| 7665 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s", |
| 7666 | mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) ); |
| 7667 | MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 ); |
| 7668 | MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 ); |
| 7669 | MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 ); |
| 7670 | |
| 7671 | /* |
| 7672 | * Determine the appropriate key, IV and MAC length. |
| 7673 | */ |
| 7674 | |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7675 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7676 | keylen = PSA_BITS_TO_BYTES(key_bits); |
| 7677 | #else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7678 | keylen = mbedtls_cipher_info_get_key_bitlen( cipher_info ) / 8; |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7679 | #endif |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7680 | |
| 7681 | #if defined(MBEDTLS_GCM_C) || \ |
| 7682 | defined(MBEDTLS_CCM_C) || \ |
| 7683 | defined(MBEDTLS_CHACHAPOLY_C) |
Neil Armstrong | 7fea33e | 2022-04-01 15:40:25 +0200 | [diff] [blame] | 7684 | if( ssl_mode == MBEDTLS_SSL_MODE_AEAD ) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7685 | { |
| 7686 | size_t explicit_ivlen; |
| 7687 | |
| 7688 | transform->maclen = 0; |
| 7689 | mac_key_len = 0; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7690 | |
| 7691 | /* All modes haves 96-bit IVs, but the length of the static parts vary |
| 7692 | * with mode and version: |
| 7693 | * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes |
| 7694 | * (to be concatenated with a dynamically chosen IV of 8 Bytes) |
| 7695 | * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's |
| 7696 | * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record |
| 7697 | * sequence number). |
| 7698 | */ |
| 7699 | transform->ivlen = 12; |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7700 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7701 | if( key_type == PSA_KEY_TYPE_CHACHA20 ) |
| 7702 | #else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7703 | if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CHACHAPOLY ) |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7704 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7705 | transform->fixed_ivlen = 12; |
| 7706 | else |
| 7707 | transform->fixed_ivlen = 4; |
| 7708 | |
| 7709 | /* Minimum length of encrypted record */ |
| 7710 | explicit_ivlen = transform->ivlen - transform->fixed_ivlen; |
| 7711 | transform->minlen = explicit_ivlen + transform->taglen; |
| 7712 | } |
| 7713 | else |
| 7714 | #endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */ |
| 7715 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
Neil Armstrong | 7fea33e | 2022-04-01 15:40:25 +0200 | [diff] [blame] | 7716 | if( ssl_mode == MBEDTLS_SSL_MODE_STREAM || |
| 7717 | ssl_mode == MBEDTLS_SSL_MODE_CBC || |
| 7718 | ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM ) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7719 | { |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7720 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Neil Armstrong | d1be767 | 2022-04-04 11:21:41 +0200 | [diff] [blame] | 7721 | size_t block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ); |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7722 | #else |
| 7723 | size_t block_size = cipher_info->block_size; |
| 7724 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 7725 | |
| 7726 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7727 | /* Get MAC length */ |
| 7728 | mac_key_len = PSA_HASH_LENGTH(mac_alg); |
| 7729 | #else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7730 | /* Initialize HMAC contexts */ |
| 7731 | if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 || |
| 7732 | ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 ) |
| 7733 | { |
| 7734 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret ); |
| 7735 | goto end; |
| 7736 | } |
| 7737 | |
| 7738 | /* Get MAC length */ |
| 7739 | mac_key_len = mbedtls_md_get_size( md_info ); |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7740 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7741 | transform->maclen = mac_key_len; |
| 7742 | |
| 7743 | /* IV length */ |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7744 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Neil Armstrong | 2230e6c | 2022-04-27 10:36:14 +0200 | [diff] [blame] | 7745 | transform->ivlen = PSA_CIPHER_IV_LENGTH( key_type, alg ); |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7746 | #else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7747 | transform->ivlen = cipher_info->iv_size; |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7748 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7749 | |
| 7750 | /* Minimum length */ |
Neil Armstrong | 7fea33e | 2022-04-01 15:40:25 +0200 | [diff] [blame] | 7751 | if( ssl_mode == MBEDTLS_SSL_MODE_STREAM ) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7752 | transform->minlen = transform->maclen; |
| 7753 | else |
| 7754 | { |
| 7755 | /* |
| 7756 | * GenericBlockCipher: |
| 7757 | * 1. if EtM is in use: one block plus MAC |
| 7758 | * otherwise: * first multiple of blocklen greater than maclen |
| 7759 | * 2. IV |
| 7760 | */ |
| 7761 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Neil Armstrong | 7fea33e | 2022-04-01 15:40:25 +0200 | [diff] [blame] | 7762 | if( ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM ) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7763 | { |
| 7764 | transform->minlen = transform->maclen |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7765 | + block_size; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7766 | } |
| 7767 | else |
| 7768 | #endif |
| 7769 | { |
| 7770 | transform->minlen = transform->maclen |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7771 | + block_size |
| 7772 | - transform->maclen % block_size; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7773 | } |
| 7774 | |
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 7775 | if( tls_version == MBEDTLS_SSL_VERSION_TLS1_2 ) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7776 | { |
| 7777 | transform->minlen += transform->ivlen; |
| 7778 | } |
| 7779 | else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7780 | { |
| 7781 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 7782 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 7783 | goto end; |
| 7784 | } |
| 7785 | } |
| 7786 | } |
| 7787 | else |
| 7788 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
| 7789 | { |
| 7790 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 7791 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 7792 | } |
| 7793 | |
| 7794 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u", |
| 7795 | (unsigned) keylen, |
| 7796 | (unsigned) transform->minlen, |
| 7797 | (unsigned) transform->ivlen, |
| 7798 | (unsigned) transform->maclen ) ); |
| 7799 | |
| 7800 | /* |
| 7801 | * Finally setup the cipher contexts, IVs and MAC secrets. |
| 7802 | */ |
| 7803 | #if defined(MBEDTLS_SSL_CLI_C) |
| 7804 | if( endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 7805 | { |
| 7806 | key1 = keyblk + mac_key_len * 2; |
| 7807 | key2 = keyblk + mac_key_len * 2 + keylen; |
| 7808 | |
| 7809 | mac_enc = keyblk; |
| 7810 | mac_dec = keyblk + mac_key_len; |
| 7811 | |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7812 | iv_copy_len = ( transform->fixed_ivlen ) ? |
| 7813 | transform->fixed_ivlen : transform->ivlen; |
| 7814 | memcpy( transform->iv_enc, key2 + keylen, iv_copy_len ); |
| 7815 | memcpy( transform->iv_dec, key2 + keylen + iv_copy_len, |
| 7816 | iv_copy_len ); |
| 7817 | } |
| 7818 | else |
| 7819 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 7820 | #if defined(MBEDTLS_SSL_SRV_C) |
| 7821 | if( endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 7822 | { |
| 7823 | key1 = keyblk + mac_key_len * 2 + keylen; |
| 7824 | key2 = keyblk + mac_key_len * 2; |
| 7825 | |
| 7826 | mac_enc = keyblk + mac_key_len; |
| 7827 | mac_dec = keyblk; |
| 7828 | |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7829 | iv_copy_len = ( transform->fixed_ivlen ) ? |
| 7830 | transform->fixed_ivlen : transform->ivlen; |
| 7831 | memcpy( transform->iv_dec, key1 + keylen, iv_copy_len ); |
| 7832 | memcpy( transform->iv_enc, key1 + keylen + iv_copy_len, |
| 7833 | iv_copy_len ); |
| 7834 | } |
| 7835 | else |
| 7836 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 7837 | { |
| 7838 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 7839 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 7840 | goto end; |
| 7841 | } |
| 7842 | |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7843 | if( ssl != NULL && ssl->f_export_keys != NULL ) |
| 7844 | { |
| 7845 | ssl->f_export_keys( ssl->p_export_keys, |
| 7846 | MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET, |
| 7847 | master, 48, |
| 7848 | randbytes + 32, |
| 7849 | randbytes, |
| 7850 | tls_prf_get_type( tls_prf ) ); |
| 7851 | } |
| 7852 | |
| 7853 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7854 | transform->psa_alg = alg; |
| 7855 | |
| 7856 | if ( alg != MBEDTLS_SSL_NULL_CIPHER ) |
| 7857 | { |
| 7858 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 7859 | psa_set_key_algorithm( &attributes, alg ); |
| 7860 | psa_set_key_type( &attributes, key_type ); |
| 7861 | |
| 7862 | if( ( status = psa_import_key( &attributes, |
| 7863 | key1, |
| 7864 | PSA_BITS_TO_BYTES( key_bits ), |
| 7865 | &transform->psa_key_enc ) ) != PSA_SUCCESS ) |
| 7866 | { |
| 7867 | MBEDTLS_SSL_DEBUG_RET( 3, "psa_import_key", (int)status ); |
| 7868 | ret = psa_ssl_status_to_mbedtls( status ); |
| 7869 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret ); |
| 7870 | goto end; |
| 7871 | } |
| 7872 | |
| 7873 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 7874 | |
| 7875 | if( ( status = psa_import_key( &attributes, |
| 7876 | key2, |
| 7877 | PSA_BITS_TO_BYTES( key_bits ), |
| 7878 | &transform->psa_key_dec ) ) != PSA_SUCCESS ) |
| 7879 | { |
| 7880 | ret = psa_ssl_status_to_mbedtls( status ); |
| 7881 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret ); |
| 7882 | goto end; |
| 7883 | } |
| 7884 | } |
| 7885 | #else |
| 7886 | if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc, |
| 7887 | cipher_info ) ) != 0 ) |
| 7888 | { |
| 7889 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret ); |
| 7890 | goto end; |
| 7891 | } |
| 7892 | |
| 7893 | if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec, |
| 7894 | cipher_info ) ) != 0 ) |
| 7895 | { |
| 7896 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret ); |
| 7897 | goto end; |
| 7898 | } |
| 7899 | |
| 7900 | if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1, |
| 7901 | (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ), |
| 7902 | MBEDTLS_ENCRYPT ) ) != 0 ) |
| 7903 | { |
| 7904 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret ); |
| 7905 | goto end; |
| 7906 | } |
| 7907 | |
| 7908 | if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2, |
| 7909 | (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ), |
| 7910 | MBEDTLS_DECRYPT ) ) != 0 ) |
| 7911 | { |
| 7912 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret ); |
| 7913 | goto end; |
| 7914 | } |
| 7915 | |
| 7916 | #if defined(MBEDTLS_CIPHER_MODE_CBC) |
| 7917 | if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CBC ) |
| 7918 | { |
| 7919 | if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc, |
| 7920 | MBEDTLS_PADDING_NONE ) ) != 0 ) |
| 7921 | { |
| 7922 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret ); |
| 7923 | goto end; |
| 7924 | } |
| 7925 | |
| 7926 | if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec, |
| 7927 | MBEDTLS_PADDING_NONE ) ) != 0 ) |
| 7928 | { |
| 7929 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret ); |
| 7930 | goto end; |
| 7931 | } |
| 7932 | } |
| 7933 | #endif /* MBEDTLS_CIPHER_MODE_CBC */ |
| 7934 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 7935 | |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 7936 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
| 7937 | /* For HMAC-based ciphersuites, initialize the HMAC transforms. |
| 7938 | For AEAD-based ciphersuites, there is nothing to do here. */ |
| 7939 | if( mac_key_len != 0 ) |
| 7940 | { |
| 7941 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7942 | transform->psa_mac_alg = PSA_ALG_HMAC( mac_alg ); |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 7943 | |
| 7944 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE ); |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7945 | psa_set_key_algorithm( &attributes, PSA_ALG_HMAC( mac_alg ) ); |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 7946 | psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC ); |
| 7947 | |
| 7948 | if( ( status = psa_import_key( &attributes, |
| 7949 | mac_enc, mac_key_len, |
| 7950 | &transform->psa_mac_enc ) ) != PSA_SUCCESS ) |
| 7951 | { |
| 7952 | ret = psa_ssl_status_to_mbedtls( status ); |
| 7953 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret ); |
| 7954 | goto end; |
| 7955 | } |
| 7956 | |
Ronald Cron | fb39f15 | 2022-03-25 14:36:28 +0100 | [diff] [blame] | 7957 | if( ( transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER ) || |
Andrzej Kurek | 8c95ac4 | 2022-08-17 16:17:00 -0400 | [diff] [blame] | 7958 | ( ( transform->psa_alg == PSA_ALG_CBC_NO_PADDING ) |
| 7959 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
| 7960 | && ( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED ) |
| 7961 | #endif |
| 7962 | ) ) |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 7963 | /* mbedtls_ct_hmac() requires the key to be exportable */ |
| 7964 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT | |
| 7965 | PSA_KEY_USAGE_VERIFY_HASH ); |
| 7966 | else |
| 7967 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH ); |
| 7968 | |
| 7969 | if( ( status = psa_import_key( &attributes, |
| 7970 | mac_dec, mac_key_len, |
| 7971 | &transform->psa_mac_dec ) ) != PSA_SUCCESS ) |
| 7972 | { |
| 7973 | ret = psa_ssl_status_to_mbedtls( status ); |
| 7974 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret ); |
| 7975 | goto end; |
| 7976 | } |
| 7977 | #else |
| 7978 | ret = mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len ); |
| 7979 | if( ret != 0 ) |
| 7980 | goto end; |
| 7981 | ret = mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len ); |
| 7982 | if( ret != 0 ) |
| 7983 | goto end; |
| 7984 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 7985 | } |
| 7986 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
| 7987 | |
| 7988 | ((void) mac_dec); |
| 7989 | ((void) mac_enc); |
| 7990 | |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7991 | end: |
| 7992 | mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) ); |
| 7993 | return( ret ); |
| 7994 | } |
| 7995 | |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 7996 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7997 | int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl, |
| 7998 | unsigned char *hash, size_t *hashlen, |
| 7999 | unsigned char *data, size_t data_len, |
| 8000 | mbedtls_md_type_t md_alg ) |
| 8001 | { |
| 8002 | psa_status_t status; |
| 8003 | psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT; |
Manuel Pégourié-Gonnard | abac037 | 2022-07-18 13:41:11 +0200 | [diff] [blame] | 8004 | psa_algorithm_t hash_alg = mbedtls_hash_info_psa_from_md( md_alg ); |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 8005 | |
| 8006 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) ); |
| 8007 | |
| 8008 | if( ( status = psa_hash_setup( &hash_operation, |
| 8009 | hash_alg ) ) != PSA_SUCCESS ) |
| 8010 | { |
| 8011 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status ); |
| 8012 | goto exit; |
| 8013 | } |
| 8014 | |
| 8015 | if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes, |
| 8016 | 64 ) ) != PSA_SUCCESS ) |
| 8017 | { |
| 8018 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status ); |
| 8019 | goto exit; |
| 8020 | } |
| 8021 | |
| 8022 | if( ( status = psa_hash_update( &hash_operation, |
| 8023 | data, data_len ) ) != PSA_SUCCESS ) |
| 8024 | { |
| 8025 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status ); |
| 8026 | goto exit; |
| 8027 | } |
| 8028 | |
| 8029 | if( ( status = psa_hash_finish( &hash_operation, hash, PSA_HASH_MAX_SIZE, |
| 8030 | hashlen ) ) != PSA_SUCCESS ) |
| 8031 | { |
| 8032 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status ); |
| 8033 | goto exit; |
| 8034 | } |
| 8035 | |
| 8036 | exit: |
| 8037 | if( status != PSA_SUCCESS ) |
| 8038 | { |
| 8039 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 8040 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
| 8041 | switch( status ) |
| 8042 | { |
| 8043 | case PSA_ERROR_NOT_SUPPORTED: |
| 8044 | return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE ); |
| 8045 | case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */ |
| 8046 | case PSA_ERROR_BUFFER_TOO_SMALL: |
| 8047 | return( MBEDTLS_ERR_MD_BAD_INPUT_DATA ); |
| 8048 | case PSA_ERROR_INSUFFICIENT_MEMORY: |
| 8049 | return( MBEDTLS_ERR_MD_ALLOC_FAILED ); |
| 8050 | default: |
| 8051 | return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED ); |
| 8052 | } |
| 8053 | } |
| 8054 | return( 0 ); |
| 8055 | } |
| 8056 | |
| 8057 | #else |
| 8058 | |
| 8059 | int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl, |
| 8060 | unsigned char *hash, size_t *hashlen, |
| 8061 | unsigned char *data, size_t data_len, |
| 8062 | mbedtls_md_type_t md_alg ) |
| 8063 | { |
| 8064 | int ret = 0; |
| 8065 | mbedtls_md_context_t ctx; |
| 8066 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg ); |
| 8067 | *hashlen = mbedtls_md_get_size( md_info ); |
| 8068 | |
| 8069 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) ); |
| 8070 | |
| 8071 | mbedtls_md_init( &ctx ); |
| 8072 | |
| 8073 | /* |
| 8074 | * digitally-signed struct { |
| 8075 | * opaque client_random[32]; |
| 8076 | * opaque server_random[32]; |
| 8077 | * ServerDHParams params; |
| 8078 | * }; |
| 8079 | */ |
| 8080 | if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 ) |
| 8081 | { |
| 8082 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret ); |
| 8083 | goto exit; |
| 8084 | } |
| 8085 | if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 ) |
| 8086 | { |
| 8087 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret ); |
| 8088 | goto exit; |
| 8089 | } |
| 8090 | if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 ) |
| 8091 | { |
| 8092 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret ); |
| 8093 | goto exit; |
| 8094 | } |
| 8095 | if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 ) |
| 8096 | { |
| 8097 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret ); |
| 8098 | goto exit; |
| 8099 | } |
| 8100 | if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 ) |
| 8101 | { |
| 8102 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret ); |
| 8103 | goto exit; |
| 8104 | } |
| 8105 | |
| 8106 | exit: |
| 8107 | mbedtls_md_free( &ctx ); |
| 8108 | |
| 8109 | if( ret != 0 ) |
| 8110 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 8111 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
| 8112 | |
| 8113 | return( ret ); |
| 8114 | } |
| 8115 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 8116 | |
Jerry Yu | d9d91da | 2022-02-17 14:57:06 +0800 | [diff] [blame] | 8117 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
| 8118 | |
Gabor Mezei | a3d016c | 2022-05-10 12:44:09 +0200 | [diff] [blame] | 8119 | /* Find the preferred hash for a given signature algorithm. */ |
| 8120 | unsigned int mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg( |
| 8121 | mbedtls_ssl_context *ssl, |
| 8122 | unsigned int sig_alg ) |
Jerry Yu | d9d91da | 2022-02-17 14:57:06 +0800 | [diff] [blame] | 8123 | { |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 8124 | unsigned int i; |
Gabor Mezei | a3d016c | 2022-05-10 12:44:09 +0200 | [diff] [blame] | 8125 | uint16_t *received_sig_algs = ssl->handshake->received_sig_algs; |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 8126 | |
| 8127 | if( sig_alg == MBEDTLS_SSL_SIG_ANON ) |
Gabor Mezei | a3d016c | 2022-05-10 12:44:09 +0200 | [diff] [blame] | 8128 | return( MBEDTLS_SSL_HASH_NONE ); |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 8129 | |
Gabor Mezei | a3d016c | 2022-05-10 12:44:09 +0200 | [diff] [blame] | 8130 | for( i = 0; received_sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ ) |
Jerry Yu | d9d91da | 2022-02-17 14:57:06 +0800 | [diff] [blame] | 8131 | { |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 8132 | unsigned int hash_alg_received = |
| 8133 | MBEDTLS_SSL_TLS12_HASH_ALG_FROM_SIG_AND_HASH_ALG( |
| 8134 | received_sig_algs[i] ); |
| 8135 | unsigned int sig_alg_received = |
| 8136 | MBEDTLS_SSL_TLS12_SIG_ALG_FROM_SIG_AND_HASH_ALG( |
| 8137 | received_sig_algs[i] ); |
| 8138 | |
| 8139 | if( sig_alg == sig_alg_received ) |
| 8140 | { |
| 8141 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 8142 | if( ssl->handshake->key_cert && ssl->handshake->key_cert->key ) |
| 8143 | { |
Neil Armstrong | 96eceb8 | 2022-06-30 18:05:05 +0200 | [diff] [blame] | 8144 | psa_algorithm_t psa_hash_alg = |
Manuel Pégourié-Gonnard | abac037 | 2022-07-18 13:41:11 +0200 | [diff] [blame] | 8145 | mbedtls_hash_info_psa_from_md( hash_alg_received ); |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 8146 | |
Neil Armstrong | 96eceb8 | 2022-06-30 18:05:05 +0200 | [diff] [blame] | 8147 | if( sig_alg_received == MBEDTLS_SSL_SIG_ECDSA && |
| 8148 | ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key, |
| 8149 | PSA_ALG_ECDSA( psa_hash_alg ), |
| 8150 | PSA_KEY_USAGE_SIGN_HASH ) ) |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 8151 | continue; |
| 8152 | |
Neil Armstrong | 96eceb8 | 2022-06-30 18:05:05 +0200 | [diff] [blame] | 8153 | if( sig_alg_received == MBEDTLS_SSL_SIG_RSA && |
Neil Armstrong | 971f30d | 2022-07-01 16:23:50 +0200 | [diff] [blame] | 8154 | ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key, |
| 8155 | PSA_ALG_RSA_PKCS1V15_SIGN( |
| 8156 | psa_hash_alg ), |
| 8157 | PSA_KEY_USAGE_SIGN_HASH ) ) |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 8158 | continue; |
| 8159 | } |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 8160 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Neil Armstrong | 96eceb8 | 2022-06-30 18:05:05 +0200 | [diff] [blame] | 8161 | |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 8162 | return( hash_alg_received ); |
| 8163 | } |
Jerry Yu | d9d91da | 2022-02-17 14:57:06 +0800 | [diff] [blame] | 8164 | } |
Jerry Yu | d9d91da | 2022-02-17 14:57:06 +0800 | [diff] [blame] | 8165 | |
Gabor Mezei | a3d016c | 2022-05-10 12:44:09 +0200 | [diff] [blame] | 8166 | return( MBEDTLS_SSL_HASH_NONE ); |
Jerry Yu | d9d91da | 2022-02-17 14:57:06 +0800 | [diff] [blame] | 8167 | } |
| 8168 | |
| 8169 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 8170 | |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8171 | /* Serialization of TLS 1.2 sessions: |
| 8172 | * |
| 8173 | * struct { |
| 8174 | * uint64 start_time; |
| 8175 | * uint8 ciphersuite[2]; // defined by the standard |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8176 | * uint8 session_id_len; // at most 32 |
| 8177 | * opaque session_id[32]; |
| 8178 | * opaque master[48]; // fixed length in the standard |
| 8179 | * uint32 verify_result; |
| 8180 | * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert |
| 8181 | * opaque ticket<0..2^24-1>; // length 0 means no ticket |
| 8182 | * uint32 ticket_lifetime; |
| 8183 | * uint8 mfl_code; // up to 255 according to standard |
| 8184 | * uint8 encrypt_then_mac; // 0 or 1 |
| 8185 | * } serialized_session_tls12; |
| 8186 | * |
| 8187 | */ |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 8188 | static size_t ssl_tls12_session_save( const mbedtls_ssl_session *session, |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8189 | unsigned char *buf, |
| 8190 | size_t buf_len ) |
| 8191 | { |
| 8192 | unsigned char *p = buf; |
| 8193 | size_t used = 0; |
| 8194 | |
| 8195 | #if defined(MBEDTLS_HAVE_TIME) |
| 8196 | uint64_t start; |
| 8197 | #endif |
| 8198 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 8199 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 8200 | size_t cert_len; |
| 8201 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 8202 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 8203 | |
| 8204 | /* |
| 8205 | * Time |
| 8206 | */ |
| 8207 | #if defined(MBEDTLS_HAVE_TIME) |
| 8208 | used += 8; |
| 8209 | |
| 8210 | if( used <= buf_len ) |
| 8211 | { |
| 8212 | start = (uint64_t) session->start; |
| 8213 | |
| 8214 | MBEDTLS_PUT_UINT64_BE( start, p, 0 ); |
| 8215 | p += 8; |
| 8216 | } |
| 8217 | #endif /* MBEDTLS_HAVE_TIME */ |
| 8218 | |
| 8219 | /* |
| 8220 | * Basic mandatory fields |
| 8221 | */ |
| 8222 | used += 2 /* ciphersuite */ |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8223 | + 1 /* id_len */ |
| 8224 | + sizeof( session->id ) |
| 8225 | + sizeof( session->master ) |
| 8226 | + 4; /* verify_result */ |
| 8227 | |
| 8228 | if( used <= buf_len ) |
| 8229 | { |
| 8230 | MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 0 ); |
| 8231 | p += 2; |
| 8232 | |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8233 | *p++ = MBEDTLS_BYTE_0( session->id_len ); |
| 8234 | memcpy( p, session->id, 32 ); |
| 8235 | p += 32; |
| 8236 | |
| 8237 | memcpy( p, session->master, 48 ); |
| 8238 | p += 48; |
| 8239 | |
| 8240 | MBEDTLS_PUT_UINT32_BE( session->verify_result, p, 0 ); |
| 8241 | p += 4; |
| 8242 | } |
| 8243 | |
| 8244 | /* |
| 8245 | * Peer's end-entity certificate |
| 8246 | */ |
| 8247 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 8248 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 8249 | if( session->peer_cert == NULL ) |
| 8250 | cert_len = 0; |
| 8251 | else |
| 8252 | cert_len = session->peer_cert->raw.len; |
| 8253 | |
| 8254 | used += 3 + cert_len; |
| 8255 | |
| 8256 | if( used <= buf_len ) |
| 8257 | { |
| 8258 | *p++ = MBEDTLS_BYTE_2( cert_len ); |
| 8259 | *p++ = MBEDTLS_BYTE_1( cert_len ); |
| 8260 | *p++ = MBEDTLS_BYTE_0( cert_len ); |
| 8261 | |
| 8262 | if( session->peer_cert != NULL ) |
| 8263 | { |
| 8264 | memcpy( p, session->peer_cert->raw.p, cert_len ); |
| 8265 | p += cert_len; |
| 8266 | } |
| 8267 | } |
| 8268 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 8269 | if( session->peer_cert_digest != NULL ) |
| 8270 | { |
| 8271 | used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len; |
| 8272 | if( used <= buf_len ) |
| 8273 | { |
| 8274 | *p++ = (unsigned char) session->peer_cert_digest_type; |
| 8275 | *p++ = (unsigned char) session->peer_cert_digest_len; |
| 8276 | memcpy( p, session->peer_cert_digest, |
| 8277 | session->peer_cert_digest_len ); |
| 8278 | p += session->peer_cert_digest_len; |
| 8279 | } |
| 8280 | } |
| 8281 | else |
| 8282 | { |
| 8283 | used += 2; |
| 8284 | if( used <= buf_len ) |
| 8285 | { |
| 8286 | *p++ = (unsigned char) MBEDTLS_MD_NONE; |
| 8287 | *p++ = 0; |
| 8288 | } |
| 8289 | } |
| 8290 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 8291 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 8292 | |
| 8293 | /* |
| 8294 | * Session ticket if any, plus associated data |
| 8295 | */ |
| 8296 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
| 8297 | used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */ |
| 8298 | |
| 8299 | if( used <= buf_len ) |
| 8300 | { |
| 8301 | *p++ = MBEDTLS_BYTE_2( session->ticket_len ); |
| 8302 | *p++ = MBEDTLS_BYTE_1( session->ticket_len ); |
| 8303 | *p++ = MBEDTLS_BYTE_0( session->ticket_len ); |
| 8304 | |
| 8305 | if( session->ticket != NULL ) |
| 8306 | { |
| 8307 | memcpy( p, session->ticket, session->ticket_len ); |
| 8308 | p += session->ticket_len; |
| 8309 | } |
| 8310 | |
| 8311 | MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 ); |
| 8312 | p += 4; |
| 8313 | } |
| 8314 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
| 8315 | |
| 8316 | /* |
| 8317 | * Misc extension-related info |
| 8318 | */ |
| 8319 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 8320 | used += 1; |
| 8321 | |
| 8322 | if( used <= buf_len ) |
| 8323 | *p++ = session->mfl_code; |
| 8324 | #endif |
| 8325 | |
| 8326 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 8327 | used += 1; |
| 8328 | |
| 8329 | if( used <= buf_len ) |
| 8330 | *p++ = MBEDTLS_BYTE_0( session->encrypt_then_mac ); |
| 8331 | #endif |
| 8332 | |
| 8333 | return( used ); |
| 8334 | } |
| 8335 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 8336 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 8337 | static int ssl_tls12_session_load( mbedtls_ssl_session *session, |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8338 | const unsigned char *buf, |
| 8339 | size_t len ) |
| 8340 | { |
| 8341 | #if defined(MBEDTLS_HAVE_TIME) |
| 8342 | uint64_t start; |
| 8343 | #endif |
| 8344 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 8345 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 8346 | size_t cert_len; |
| 8347 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 8348 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 8349 | |
| 8350 | const unsigned char *p = buf; |
| 8351 | const unsigned char * const end = buf + len; |
| 8352 | |
| 8353 | /* |
| 8354 | * Time |
| 8355 | */ |
| 8356 | #if defined(MBEDTLS_HAVE_TIME) |
| 8357 | if( 8 > (size_t)( end - p ) ) |
| 8358 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8359 | |
| 8360 | start = ( (uint64_t) p[0] << 56 ) | |
| 8361 | ( (uint64_t) p[1] << 48 ) | |
| 8362 | ( (uint64_t) p[2] << 40 ) | |
| 8363 | ( (uint64_t) p[3] << 32 ) | |
| 8364 | ( (uint64_t) p[4] << 24 ) | |
| 8365 | ( (uint64_t) p[5] << 16 ) | |
| 8366 | ( (uint64_t) p[6] << 8 ) | |
| 8367 | ( (uint64_t) p[7] ); |
| 8368 | p += 8; |
| 8369 | |
| 8370 | session->start = (time_t) start; |
| 8371 | #endif /* MBEDTLS_HAVE_TIME */ |
| 8372 | |
| 8373 | /* |
| 8374 | * Basic mandatory fields |
| 8375 | */ |
Thomas Daubney | 20f89a9 | 2022-06-20 15:12:19 +0100 | [diff] [blame] | 8376 | if( 2 + 1 + 32 + 48 + 4 > (size_t)( end - p ) ) |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8377 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8378 | |
| 8379 | session->ciphersuite = ( p[0] << 8 ) | p[1]; |
| 8380 | p += 2; |
| 8381 | |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8382 | session->id_len = *p++; |
| 8383 | memcpy( session->id, p, 32 ); |
| 8384 | p += 32; |
| 8385 | |
| 8386 | memcpy( session->master, p, 48 ); |
| 8387 | p += 48; |
| 8388 | |
| 8389 | session->verify_result = ( (uint32_t) p[0] << 24 ) | |
| 8390 | ( (uint32_t) p[1] << 16 ) | |
| 8391 | ( (uint32_t) p[2] << 8 ) | |
| 8392 | ( (uint32_t) p[3] ); |
| 8393 | p += 4; |
| 8394 | |
| 8395 | /* Immediately clear invalid pointer values that have been read, in case |
| 8396 | * we exit early before we replaced them with valid ones. */ |
| 8397 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 8398 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 8399 | session->peer_cert = NULL; |
| 8400 | #else |
| 8401 | session->peer_cert_digest = NULL; |
| 8402 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 8403 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 8404 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
| 8405 | session->ticket = NULL; |
| 8406 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
| 8407 | |
| 8408 | /* |
| 8409 | * Peer certificate |
| 8410 | */ |
| 8411 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 8412 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 8413 | /* Deserialize CRT from the end of the ticket. */ |
| 8414 | if( 3 > (size_t)( end - p ) ) |
| 8415 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8416 | |
| 8417 | cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2]; |
| 8418 | p += 3; |
| 8419 | |
| 8420 | if( cert_len != 0 ) |
| 8421 | { |
| 8422 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 8423 | |
| 8424 | if( cert_len > (size_t)( end - p ) ) |
| 8425 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8426 | |
| 8427 | session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ); |
| 8428 | |
| 8429 | if( session->peer_cert == NULL ) |
| 8430 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 8431 | |
| 8432 | mbedtls_x509_crt_init( session->peer_cert ); |
| 8433 | |
| 8434 | if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert, |
| 8435 | p, cert_len ) ) != 0 ) |
| 8436 | { |
| 8437 | mbedtls_x509_crt_free( session->peer_cert ); |
| 8438 | mbedtls_free( session->peer_cert ); |
| 8439 | session->peer_cert = NULL; |
| 8440 | return( ret ); |
| 8441 | } |
| 8442 | |
| 8443 | p += cert_len; |
| 8444 | } |
| 8445 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 8446 | /* Deserialize CRT digest from the end of the ticket. */ |
| 8447 | if( 2 > (size_t)( end - p ) ) |
| 8448 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8449 | |
| 8450 | session->peer_cert_digest_type = (mbedtls_md_type_t) *p++; |
| 8451 | session->peer_cert_digest_len = (size_t) *p++; |
| 8452 | |
| 8453 | if( session->peer_cert_digest_len != 0 ) |
| 8454 | { |
| 8455 | const mbedtls_md_info_t *md_info = |
| 8456 | mbedtls_md_info_from_type( session->peer_cert_digest_type ); |
| 8457 | if( md_info == NULL ) |
| 8458 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8459 | if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) ) |
| 8460 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8461 | |
| 8462 | if( session->peer_cert_digest_len > (size_t)( end - p ) ) |
| 8463 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8464 | |
| 8465 | session->peer_cert_digest = |
| 8466 | mbedtls_calloc( 1, session->peer_cert_digest_len ); |
| 8467 | if( session->peer_cert_digest == NULL ) |
| 8468 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 8469 | |
| 8470 | memcpy( session->peer_cert_digest, p, |
| 8471 | session->peer_cert_digest_len ); |
| 8472 | p += session->peer_cert_digest_len; |
| 8473 | } |
| 8474 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 8475 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 8476 | |
| 8477 | /* |
| 8478 | * Session ticket and associated data |
| 8479 | */ |
| 8480 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
| 8481 | if( 3 > (size_t)( end - p ) ) |
| 8482 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8483 | |
| 8484 | session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2]; |
| 8485 | p += 3; |
| 8486 | |
| 8487 | if( session->ticket_len != 0 ) |
| 8488 | { |
| 8489 | if( session->ticket_len > (size_t)( end - p ) ) |
| 8490 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8491 | |
| 8492 | session->ticket = mbedtls_calloc( 1, session->ticket_len ); |
| 8493 | if( session->ticket == NULL ) |
| 8494 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 8495 | |
| 8496 | memcpy( session->ticket, p, session->ticket_len ); |
| 8497 | p += session->ticket_len; |
| 8498 | } |
| 8499 | |
| 8500 | if( 4 > (size_t)( end - p ) ) |
| 8501 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8502 | |
| 8503 | session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) | |
| 8504 | ( (uint32_t) p[1] << 16 ) | |
| 8505 | ( (uint32_t) p[2] << 8 ) | |
| 8506 | ( (uint32_t) p[3] ); |
| 8507 | p += 4; |
| 8508 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
| 8509 | |
| 8510 | /* |
| 8511 | * Misc extension-related info |
| 8512 | */ |
| 8513 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 8514 | if( 1 > (size_t)( end - p ) ) |
| 8515 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8516 | |
| 8517 | session->mfl_code = *p++; |
| 8518 | #endif |
| 8519 | |
| 8520 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 8521 | if( 1 > (size_t)( end - p ) ) |
| 8522 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8523 | |
| 8524 | session->encrypt_then_mac = *p++; |
| 8525 | #endif |
| 8526 | |
| 8527 | /* Done, should have consumed entire buffer */ |
| 8528 | if( p != end ) |
| 8529 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8530 | |
| 8531 | return( 0 ); |
| 8532 | } |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 8533 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 8534 | |
XiaokangQian | 75d40ef | 2022-04-20 11:05:24 +0000 | [diff] [blame] | 8535 | int mbedtls_ssl_validate_ciphersuite( |
| 8536 | const mbedtls_ssl_context *ssl, |
| 8537 | const mbedtls_ssl_ciphersuite_t *suite_info, |
| 8538 | mbedtls_ssl_protocol_version min_tls_version, |
| 8539 | mbedtls_ssl_protocol_version max_tls_version ) |
| 8540 | { |
| 8541 | (void) ssl; |
| 8542 | |
| 8543 | if( suite_info == NULL ) |
| 8544 | return( -1 ); |
| 8545 | |
| 8546 | if( ( suite_info->min_tls_version > max_tls_version ) || |
| 8547 | ( suite_info->max_tls_version < min_tls_version ) ) |
| 8548 | { |
| 8549 | return( -1 ); |
| 8550 | } |
| 8551 | |
XiaokangQian | 060d867 | 2022-04-21 09:24:56 +0000 | [diff] [blame] | 8552 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_CLI_C) |
XiaokangQian | 75d40ef | 2022-04-20 11:05:24 +0000 | [diff] [blame] | 8553 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
| 8554 | if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE && |
| 8555 | mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 ) |
| 8556 | { |
| 8557 | return( -1 ); |
| 8558 | } |
| 8559 | #endif |
| 8560 | |
| 8561 | /* Don't suggest PSK-based ciphersuite if no PSK is available. */ |
| 8562 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
| 8563 | if( mbedtls_ssl_ciphersuite_uses_psk( suite_info ) && |
| 8564 | mbedtls_ssl_conf_has_static_psk( ssl->conf ) == 0 ) |
| 8565 | { |
| 8566 | return( -1 ); |
| 8567 | } |
| 8568 | #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ |
| 8569 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 8570 | |
| 8571 | return( 0 ); |
| 8572 | } |
| 8573 | |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 8574 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
| 8575 | /* |
| 8576 | * Function for writing a signature algorithm extension. |
| 8577 | * |
| 8578 | * The `extension_data` field of signature algorithm contains a `SignatureSchemeList` |
| 8579 | * value (TLS 1.3 RFC8446): |
| 8580 | * enum { |
| 8581 | * .... |
| 8582 | * ecdsa_secp256r1_sha256( 0x0403 ), |
| 8583 | * ecdsa_secp384r1_sha384( 0x0503 ), |
| 8584 | * ecdsa_secp521r1_sha512( 0x0603 ), |
| 8585 | * .... |
| 8586 | * } SignatureScheme; |
| 8587 | * |
| 8588 | * struct { |
| 8589 | * SignatureScheme supported_signature_algorithms<2..2^16-2>; |
| 8590 | * } SignatureSchemeList; |
| 8591 | * |
| 8592 | * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm` |
| 8593 | * value (TLS 1.2 RFC5246): |
| 8594 | * enum { |
| 8595 | * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5), |
| 8596 | * sha512(6), (255) |
| 8597 | * } HashAlgorithm; |
| 8598 | * |
| 8599 | * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) } |
| 8600 | * SignatureAlgorithm; |
| 8601 | * |
| 8602 | * struct { |
| 8603 | * HashAlgorithm hash; |
| 8604 | * SignatureAlgorithm signature; |
| 8605 | * } SignatureAndHashAlgorithm; |
| 8606 | * |
| 8607 | * SignatureAndHashAlgorithm |
| 8608 | * supported_signature_algorithms<2..2^16-2>; |
| 8609 | * |
| 8610 | * The TLS 1.3 signature algorithm extension was defined to be a compatible |
| 8611 | * generalization of the TLS 1.2 signature algorithm extension. |
| 8612 | * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by |
| 8613 | * `SignatureScheme` field of TLS 1.3 |
| 8614 | * |
| 8615 | */ |
| 8616 | int mbedtls_ssl_write_sig_alg_ext( mbedtls_ssl_context *ssl, unsigned char *buf, |
| 8617 | const unsigned char *end, size_t *out_len ) |
| 8618 | { |
| 8619 | unsigned char *p = buf; |
| 8620 | unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */ |
| 8621 | size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */ |
| 8622 | |
| 8623 | *out_len = 0; |
| 8624 | |
| 8625 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding signature_algorithms extension" ) ); |
| 8626 | |
| 8627 | /* Check if we have space for header and length field: |
| 8628 | * - extension_type (2 bytes) |
| 8629 | * - extension_data_length (2 bytes) |
| 8630 | * - supported_signature_algorithms_length (2 bytes) |
| 8631 | */ |
| 8632 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 ); |
| 8633 | p += 6; |
| 8634 | |
| 8635 | /* |
| 8636 | * Write supported_signature_algorithms |
| 8637 | */ |
| 8638 | supported_sig_alg = p; |
| 8639 | const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs( ssl ); |
| 8640 | if( sig_alg == NULL ) |
| 8641 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); |
| 8642 | |
| 8643 | for( ; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++ ) |
| 8644 | { |
Jerry Yu | 53f5c15 | 2022-06-22 20:24:38 +0800 | [diff] [blame] | 8645 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "got signature scheme [%x] %s", |
| 8646 | *sig_alg, |
| 8647 | mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) ); |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 8648 | if( ! mbedtls_ssl_sig_alg_is_supported( ssl, *sig_alg ) ) |
| 8649 | continue; |
| 8650 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 ); |
| 8651 | MBEDTLS_PUT_UINT16_BE( *sig_alg, p, 0 ); |
| 8652 | p += 2; |
Jerry Yu | 80dd5db | 2022-06-22 19:30:32 +0800 | [diff] [blame] | 8653 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "sent signature scheme [%x] %s", |
Jerry Yu | f3b46b5 | 2022-06-19 16:52:27 +0800 | [diff] [blame] | 8654 | *sig_alg, |
| 8655 | mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) ); |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 8656 | } |
| 8657 | |
| 8658 | /* Length of supported_signature_algorithms */ |
| 8659 | supported_sig_alg_len = p - supported_sig_alg; |
| 8660 | if( supported_sig_alg_len == 0 ) |
| 8661 | { |
| 8662 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "No signature algorithms defined." ) ); |
| 8663 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 8664 | } |
| 8665 | |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 8666 | MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SIG_ALG, buf, 0 ); |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 8667 | MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len + 2, buf, 2 ); |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 8668 | MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len, buf, 4 ); |
| 8669 | |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 8670 | *out_len = p - buf; |
| 8671 | |
| 8672 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 8673 | ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SIG_ALG; |
| 8674 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 8675 | return( 0 ); |
| 8676 | } |
| 8677 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 8678 | |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 8679 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 8680 | /* |
| 8681 | * mbedtls_ssl_parse_server_name_ext |
| 8682 | * |
| 8683 | * Structure of server_name extension: |
| 8684 | * |
| 8685 | * enum { |
| 8686 | * host_name(0), (255) |
| 8687 | * } NameType; |
| 8688 | * opaque HostName<1..2^16-1>; |
| 8689 | * |
| 8690 | * struct { |
| 8691 | * NameType name_type; |
| 8692 | * select (name_type) { |
| 8693 | * case host_name: HostName; |
| 8694 | * } name; |
| 8695 | * } ServerName; |
| 8696 | * struct { |
| 8697 | * ServerName server_name_list<1..2^16-1> |
| 8698 | * } ServerNameList; |
| 8699 | */ |
Ronald Cron | ce7d76e | 2022-07-08 18:56:49 +0200 | [diff] [blame] | 8700 | MBEDTLS_CHECK_RETURN_CRITICAL |
XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 8701 | int mbedtls_ssl_parse_server_name_ext( mbedtls_ssl_context *ssl, |
| 8702 | const unsigned char *buf, |
| 8703 | const unsigned char *end ) |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 8704 | { |
| 8705 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 8706 | const unsigned char *p = buf; |
XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 8707 | size_t server_name_list_len, hostname_len; |
| 8708 | const unsigned char *server_name_list_end; |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 8709 | |
XiaokangQian | f2a9420 | 2022-05-20 06:44:24 +0000 | [diff] [blame] | 8710 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "parse ServerName extension" ) ); |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 8711 | |
| 8712 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 ); |
XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 8713 | server_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 ); |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 8714 | p += 2; |
| 8715 | |
XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 8716 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, server_name_list_len ); |
| 8717 | server_name_list_end = p + server_name_list_len; |
XiaokangQian | 75fe8c7 | 2022-06-15 09:42:45 +0000 | [diff] [blame] | 8718 | while( p < server_name_list_end ) |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 8719 | { |
XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 8720 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end, 3 ); |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 8721 | hostname_len = MBEDTLS_GET_UINT16_BE( p, 1 ); |
XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 8722 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end, |
| 8723 | hostname_len + 3 ); |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 8724 | |
| 8725 | if( p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME ) |
| 8726 | { |
XiaokangQian | 75fe8c7 | 2022-06-15 09:42:45 +0000 | [diff] [blame] | 8727 | /* sni_name is intended to be used only during the parsing of the |
| 8728 | * ClientHello message (it is reset to NULL before the end of |
| 8729 | * the message parsing). Thus it is ok to just point to the |
| 8730 | * reception buffer and not make a copy of it. |
| 8731 | */ |
XiaokangQian | f2a9420 | 2022-05-20 06:44:24 +0000 | [diff] [blame] | 8732 | ssl->handshake->sni_name = p + 3; |
| 8733 | ssl->handshake->sni_name_len = hostname_len; |
| 8734 | if( ssl->conf->f_sni == NULL ) |
| 8735 | return( 0 ); |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 8736 | ret = ssl->conf->f_sni( ssl->conf->p_sni, |
XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 8737 | ssl, p + 3, hostname_len ); |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 8738 | if( ret != 0 ) |
| 8739 | { |
XiaokangQian | f2a9420 | 2022-05-20 06:44:24 +0000 | [diff] [blame] | 8740 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_sni_wrapper", ret ); |
XiaokangQian | 129aeb9 | 2022-06-02 09:29:18 +0000 | [diff] [blame] | 8741 | MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME, |
| 8742 | MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME ); |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 8743 | return( MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME ); |
| 8744 | } |
| 8745 | return( 0 ); |
| 8746 | } |
| 8747 | |
| 8748 | p += hostname_len + 3; |
| 8749 | } |
| 8750 | |
| 8751 | return( 0 ); |
| 8752 | } |
| 8753 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
| 8754 | |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8755 | #if defined(MBEDTLS_SSL_ALPN) |
Ronald Cron | ce7d76e | 2022-07-08 18:56:49 +0200 | [diff] [blame] | 8756 | MBEDTLS_CHECK_RETURN_CRITICAL |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8757 | int mbedtls_ssl_parse_alpn_ext( mbedtls_ssl_context *ssl, |
| 8758 | const unsigned char *buf, |
| 8759 | const unsigned char *end ) |
| 8760 | { |
| 8761 | const unsigned char *p = buf; |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 8762 | size_t protocol_name_list_len; |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 8763 | const unsigned char *protocol_name_list; |
| 8764 | const unsigned char *protocol_name_list_end; |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 8765 | size_t protocol_name_len; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8766 | |
| 8767 | /* If ALPN not configured, just ignore the extension */ |
| 8768 | if( ssl->conf->alpn_list == NULL ) |
| 8769 | return( 0 ); |
| 8770 | |
| 8771 | /* |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 8772 | * RFC7301, section 3.1 |
| 8773 | * opaque ProtocolName<1..2^8-1>; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8774 | * |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 8775 | * struct { |
| 8776 | * ProtocolName protocol_name_list<2..2^16-1> |
| 8777 | * } ProtocolNameList; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8778 | */ |
| 8779 | |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 8780 | /* |
XiaokangQian | 0b776e2 | 2022-06-24 09:04:59 +0000 | [diff] [blame] | 8781 | * protocol_name_list_len 2 bytes |
| 8782 | * protocol_name_len 1 bytes |
| 8783 | * protocol_name >=1 byte |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 8784 | */ |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8785 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 ); |
| 8786 | |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 8787 | protocol_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 ); |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8788 | p += 2; |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 8789 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, protocol_name_list_len ); |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 8790 | protocol_name_list = p; |
| 8791 | protocol_name_list_end = p + protocol_name_list_len; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8792 | |
| 8793 | /* Validate peer's list (lengths) */ |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 8794 | while( p < protocol_name_list_end ) |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8795 | { |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 8796 | protocol_name_len = *p++; |
| 8797 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, protocol_name_list_end, |
| 8798 | protocol_name_len ); |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 8799 | if( protocol_name_len == 0 ) |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 8800 | { |
| 8801 | MBEDTLS_SSL_PEND_FATAL_ALERT( |
| 8802 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER, |
| 8803 | MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER ); |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8804 | return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER ); |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 8805 | } |
| 8806 | |
| 8807 | p += protocol_name_len; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8808 | } |
| 8809 | |
| 8810 | /* Use our order of preference */ |
| 8811 | for( const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++ ) |
| 8812 | { |
| 8813 | size_t const alpn_len = strlen( *alpn ); |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 8814 | p = protocol_name_list; |
| 8815 | while( p < protocol_name_list_end ) |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8816 | { |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 8817 | protocol_name_len = *p++; |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 8818 | if( protocol_name_len == alpn_len && |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 8819 | memcmp( p, *alpn, alpn_len ) == 0 ) |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8820 | { |
| 8821 | ssl->alpn_chosen = *alpn; |
| 8822 | return( 0 ); |
| 8823 | } |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 8824 | |
| 8825 | p += protocol_name_len; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8826 | } |
| 8827 | } |
| 8828 | |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 8829 | /* If we get here, no match was found */ |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8830 | MBEDTLS_SSL_PEND_FATAL_ALERT( |
| 8831 | MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL, |
| 8832 | MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL ); |
| 8833 | return( MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL ); |
| 8834 | } |
| 8835 | |
| 8836 | int mbedtls_ssl_write_alpn_ext( mbedtls_ssl_context *ssl, |
| 8837 | unsigned char *buf, |
| 8838 | unsigned char *end, |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 8839 | size_t *out_len ) |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8840 | { |
| 8841 | unsigned char *p = buf; |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 8842 | size_t protocol_name_len; |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 8843 | *out_len = 0; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8844 | |
| 8845 | if( ssl->alpn_chosen == NULL ) |
| 8846 | { |
| 8847 | return( 0 ); |
| 8848 | } |
| 8849 | |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 8850 | protocol_name_len = strlen( ssl->alpn_chosen ); |
| 8851 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 7 + protocol_name_len ); |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8852 | |
| 8853 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "server side, adding alpn extension" ) ); |
| 8854 | /* |
| 8855 | * 0 . 1 ext identifier |
| 8856 | * 2 . 3 ext length |
| 8857 | * 4 . 5 protocol list length |
| 8858 | * 6 . 6 protocol name length |
| 8859 | * 7 . 7+n protocol name |
| 8860 | */ |
| 8861 | MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ALPN, p, 0 ); |
| 8862 | |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 8863 | *out_len = 7 + protocol_name_len; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8864 | |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 8865 | MBEDTLS_PUT_UINT16_BE( protocol_name_len + 3, p, 2 ); |
| 8866 | MBEDTLS_PUT_UINT16_BE( protocol_name_len + 1, p, 4 ); |
XiaokangQian | 0b776e2 | 2022-06-24 09:04:59 +0000 | [diff] [blame] | 8867 | /* Note: the length of the chosen protocol has been checked to be less |
| 8868 | * than 255 bytes in `mbedtls_ssl_conf_alpn_protocols`. |
| 8869 | */ |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 8870 | p[6] = MBEDTLS_BYTE_0( protocol_name_len ); |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8871 | |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 8872 | memcpy( p + 7, ssl->alpn_chosen, protocol_name_len ); |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8873 | return ( 0 ); |
| 8874 | } |
| 8875 | #endif /* MBEDTLS_SSL_ALPN */ |
| 8876 | |
Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 8877 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \ |
Xiaokang Qian | 0340929 | 2022-10-12 02:49:52 +0000 | [diff] [blame] | 8878 | defined(MBEDTLS_SSL_SESSION_TICKETS) && \ |
Xiaokang Qian | ed0620c | 2022-10-12 06:58:13 +0000 | [diff] [blame] | 8879 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \ |
Xiaokang Qian | ed3afcd | 2022-10-12 08:31:11 +0000 | [diff] [blame] | 8880 | defined(MBEDTLS_SSL_CLI_C) |
Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 8881 | int mbedtls_ssl_session_set_hostname( mbedtls_ssl_session *session, |
| 8882 | const char *hostname ) |
| 8883 | { |
| 8884 | /* Initialize to suppress unnecessary compiler warning */ |
| 8885 | size_t hostname_len = 0; |
| 8886 | |
| 8887 | /* Check if new hostname is valid before |
| 8888 | * making any change to current one */ |
| 8889 | if( hostname != NULL ) |
| 8890 | { |
| 8891 | hostname_len = strlen( hostname ); |
| 8892 | |
| 8893 | if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN ) |
| 8894 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8895 | } |
| 8896 | |
| 8897 | /* Now it's clear that we will overwrite the old hostname, |
| 8898 | * so we can free it safely */ |
| 8899 | if( session->hostname != NULL ) |
| 8900 | { |
| 8901 | mbedtls_platform_zeroize( session->hostname, |
| 8902 | strlen( session->hostname ) ); |
| 8903 | mbedtls_free( session->hostname ); |
| 8904 | } |
| 8905 | |
| 8906 | /* Passing NULL as hostname shall clear the old one */ |
| 8907 | if( hostname == NULL ) |
| 8908 | { |
| 8909 | session->hostname = NULL; |
| 8910 | } |
| 8911 | else |
| 8912 | { |
| 8913 | session->hostname = mbedtls_calloc( 1, hostname_len + 1 ); |
| 8914 | if( session->hostname == NULL ) |
| 8915 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 8916 | |
| 8917 | memcpy( session->hostname, hostname, hostname_len ); |
| 8918 | } |
| 8919 | |
| 8920 | return( 0 ); |
| 8921 | } |
Xiaokang Qian | 0340929 | 2022-10-12 02:49:52 +0000 | [diff] [blame] | 8922 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 && |
| 8923 | MBEDTLS_SSL_SESSION_TICKETS && |
Xiaokang Qian | ed0620c | 2022-10-12 06:58:13 +0000 | [diff] [blame] | 8924 | MBEDTLS_SSL_SERVER_NAME_INDICATION && |
Xiaokang Qian | ed3afcd | 2022-10-12 08:31:11 +0000 | [diff] [blame] | 8925 | MBEDTLS_SSL_CLI_C */ |
Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 8926 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8927 | #endif /* MBEDTLS_SSL_TLS_C */ |