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 |
Andrzej Kurek | eabeb30 | 2022-10-17 07:52:51 -0400 | [diff] [blame] | 607 | #if !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \ |
| 608 | !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
| 609 | (void) ssl; |
| 610 | (void) buf; |
| 611 | (void) len; |
| 612 | #endif |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 613 | } |
| 614 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 615 | #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] | 616 | static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl, |
| 617 | const unsigned char *buf, size_t len ) |
| 618 | { |
| 619 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 620 | psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len ); |
| 621 | #else |
| 622 | mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len ); |
| 623 | #endif |
| 624 | } |
| 625 | #endif |
| 626 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 627 | #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] | 628 | static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl, |
| 629 | const unsigned char *buf, size_t len ) |
| 630 | { |
| 631 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 632 | psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len ); |
| 633 | #else |
Andrzej Kurek | a242e83 | 2022-08-11 10:03:14 -0400 | [diff] [blame] | 634 | mbedtls_sha512_update( &ssl->handshake->fin_sha384, buf, len ); |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 635 | #endif |
| 636 | } |
| 637 | #endif |
| 638 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 639 | static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake ) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 640 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 641 | memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 642 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 643 | #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] | 644 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 645 | handshake->fin_sha256_psa = psa_hash_operation_init(); |
| 646 | psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 ); |
| 647 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 648 | mbedtls_sha256_init( &handshake->fin_sha256 ); |
TRodziewicz | 26371e4 | 2021-06-08 16:45:41 +0200 | [diff] [blame] | 649 | mbedtls_sha256_starts( &handshake->fin_sha256, 0 ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 650 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 651 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 652 | #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] | 653 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 654 | handshake->fin_sha384_psa = psa_hash_operation_init(); |
| 655 | psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 656 | #else |
Andrzej Kurek | a242e83 | 2022-08-11 10:03:14 -0400 | [diff] [blame] | 657 | mbedtls_sha512_init( &handshake->fin_sha384 ); |
| 658 | mbedtls_sha512_starts( &handshake->fin_sha384, 1 ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 659 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 660 | #endif |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 661 | |
| 662 | handshake->update_checksum = ssl_update_checksum_start; |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 663 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 664 | #if defined(MBEDTLS_DHM_C) |
| 665 | mbedtls_dhm_init( &handshake->dhm_ctx ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 666 | #endif |
Neil Armstrong | f3f4641 | 2022-04-12 14:43:39 +0200 | [diff] [blame] | 667 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 668 | mbedtls_ecdh_init( &handshake->ecdh_ctx ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 669 | #endif |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 670 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 671 | mbedtls_ecjpake_init( &handshake->ecjpake_ctx ); |
Manuel Pégourié-Gonnard | 77c0646 | 2015-09-17 13:59:49 +0200 | [diff] [blame] | 672 | #if defined(MBEDTLS_SSL_CLI_C) |
| 673 | handshake->ecjpake_cache = NULL; |
| 674 | handshake->ecjpake_cache_len = 0; |
| 675 | #endif |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 676 | #endif |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 677 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 678 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Manuel Pégourié-Gonnard | 6b7301c | 2017-08-15 12:08:45 +0200 | [diff] [blame] | 679 | mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx ); |
Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 680 | #endif |
| 681 | |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 682 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 683 | handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET; |
| 684 | #endif |
Hanno Becker | 7517312 | 2019-02-06 16:18:31 +0000 | [diff] [blame] | 685 | |
| 686 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 687 | !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 688 | mbedtls_pk_init( &handshake->peer_pubkey ); |
| 689 | #endif |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 690 | } |
| 691 | |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 692 | void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform ) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 693 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 694 | memset( transform, 0, sizeof(mbedtls_ssl_transform) ); |
Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 695 | |
Przemyslaw Stekiel | 8f80fb9 | 2022-01-11 08:28:13 +0100 | [diff] [blame] | 696 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 697 | transform->psa_key_enc = MBEDTLS_SVC_KEY_ID_INIT; |
| 698 | transform->psa_key_dec = MBEDTLS_SVC_KEY_ID_INIT; |
Przemyslaw Stekiel | 6be9cf5 | 2022-01-19 16:00:22 +0100 | [diff] [blame] | 699 | #else |
| 700 | mbedtls_cipher_init( &transform->cipher_ctx_enc ); |
| 701 | mbedtls_cipher_init( &transform->cipher_ctx_dec ); |
Przemyslaw Stekiel | 8f80fb9 | 2022-01-11 08:28:13 +0100 | [diff] [blame] | 702 | #endif |
| 703 | |
Hanno Becker | fd86ca8 | 2020-11-30 08:54:23 +0000 | [diff] [blame] | 704 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
Neil Armstrong | 39b8e7d | 2022-02-23 09:24:45 +0100 | [diff] [blame] | 705 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 706 | transform->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT; |
| 707 | transform->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT; |
Neil Armstrong | cf8841a | 2022-02-24 11:17:45 +0100 | [diff] [blame] | 708 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 709 | mbedtls_md_init( &transform->md_ctx_enc ); |
| 710 | mbedtls_md_init( &transform->md_ctx_dec ); |
Hanno Becker | d56ed24 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 711 | #endif |
Neil Armstrong | cf8841a | 2022-02-24 11:17:45 +0100 | [diff] [blame] | 712 | #endif |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 713 | } |
| 714 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 715 | void mbedtls_ssl_session_init( mbedtls_ssl_session *session ) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 716 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 717 | memset( session, 0, sizeof(mbedtls_ssl_session) ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 718 | } |
| 719 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 720 | MBEDTLS_CHECK_RETURN_CRITICAL |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 721 | static int ssl_handshake_init( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 722 | { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 723 | /* Clear old handshake information if present */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 724 | if( ssl->transform_negotiate ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 725 | mbedtls_ssl_transform_free( ssl->transform_negotiate ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 726 | if( ssl->session_negotiate ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 727 | mbedtls_ssl_session_free( ssl->session_negotiate ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 728 | if( ssl->handshake ) |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 729 | mbedtls_ssl_handshake_free( ssl ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 730 | |
| 731 | /* |
| 732 | * Either the pointers are now NULL or cleared properly and can be freed. |
| 733 | * Now allocate missing structures. |
| 734 | */ |
| 735 | if( ssl->transform_negotiate == NULL ) |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 736 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 737 | ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 738 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 739 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 740 | if( ssl->session_negotiate == NULL ) |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 741 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 742 | ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 743 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 744 | |
Paul Bakker | 82788fb | 2014-10-20 13:59:19 +0200 | [diff] [blame] | 745 | if( ssl->handshake == NULL ) |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 746 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 747 | ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 748 | } |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 749 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 750 | /* If the buffers are too small - reallocate */ |
Andrzej Kurek | 8ea6872 | 2020-04-03 06:40:47 -0400 | [diff] [blame] | 751 | |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 752 | handle_buffer_resizing( ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN, |
| 753 | MBEDTLS_SSL_OUT_BUFFER_LEN ); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 754 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 755 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 756 | /* All pointers should exist and can be directly freed without issue */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 757 | if( ssl->handshake == NULL || |
| 758 | ssl->transform_negotiate == NULL || |
| 759 | ssl->session_negotiate == NULL ) |
| 760 | { |
Manuel Pégourié-Gonnard | b2a18a2 | 2015-05-27 16:29:56 +0200 | [diff] [blame] | 761 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 762 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 763 | mbedtls_free( ssl->handshake ); |
| 764 | mbedtls_free( ssl->transform_negotiate ); |
| 765 | mbedtls_free( ssl->session_negotiate ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 766 | |
| 767 | ssl->handshake = NULL; |
| 768 | ssl->transform_negotiate = NULL; |
| 769 | ssl->session_negotiate = NULL; |
| 770 | |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 771 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 772 | } |
| 773 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 774 | /* Initialize structures */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 775 | mbedtls_ssl_session_init( ssl->session_negotiate ); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 776 | mbedtls_ssl_transform_init( ssl->transform_negotiate ); |
Paul Bakker | 968afaa | 2014-07-09 11:09:24 +0200 | [diff] [blame] | 777 | ssl_handshake_params_init( ssl->handshake ); |
| 778 | |
Jerry Yu | d0766ec | 2022-09-22 10:46:57 +0800 | [diff] [blame] | 779 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \ |
| 780 | defined(MBEDTLS_SSL_SRV_C) && \ |
| 781 | defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 782 | ssl->handshake->new_session_tickets_count = |
| 783 | ssl->conf->new_session_tickets_count ; |
| 784 | #endif |
| 785 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 786 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 787 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 788 | { |
| 789 | ssl->handshake->alt_transform_out = ssl->transform_out; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 790 | |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 791 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 792 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING; |
| 793 | else |
| 794 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING; |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 795 | |
Hanno Becker | 0f57a65 | 2020-02-05 10:37:26 +0000 | [diff] [blame] | 796 | mbedtls_ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 797 | } |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 798 | #endif |
| 799 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 800 | /* |
| 801 | * curve_list is translated to IANA TLS group identifiers here because |
| 802 | * mbedtls_ssl_conf_curves returns void and so can't return |
| 803 | * any error codes. |
| 804 | */ |
| 805 | #if defined(MBEDTLS_ECP_C) |
| 806 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 807 | /* Heap allocate and translate curve_list from internal to IANA group ids */ |
| 808 | if ( ssl->conf->curve_list != NULL ) |
| 809 | { |
| 810 | size_t length; |
| 811 | const mbedtls_ecp_group_id *curve_list = ssl->conf->curve_list; |
| 812 | |
| 813 | for( length = 0; ( curve_list[length] != MBEDTLS_ECP_DP_NONE ) && |
| 814 | ( length < MBEDTLS_ECP_DP_MAX ); length++ ) {} |
| 815 | |
| 816 | /* Leave room for zero termination */ |
| 817 | uint16_t *group_list = mbedtls_calloc( length + 1, sizeof(uint16_t) ); |
| 818 | if ( group_list == NULL ) |
| 819 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 820 | |
| 821 | for( size_t i = 0; i < length; i++ ) |
| 822 | { |
| 823 | const mbedtls_ecp_curve_info *info = |
| 824 | mbedtls_ecp_curve_info_from_grp_id( curve_list[i] ); |
| 825 | if ( info == NULL ) |
| 826 | { |
| 827 | mbedtls_free( group_list ); |
| 828 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); |
| 829 | } |
| 830 | group_list[i] = info->tls_id; |
| 831 | } |
| 832 | |
| 833 | group_list[length] = 0; |
| 834 | |
| 835 | ssl->handshake->group_list = group_list; |
| 836 | ssl->handshake->group_list_heap_allocated = 1; |
| 837 | } |
| 838 | else |
| 839 | { |
| 840 | ssl->handshake->group_list = ssl->conf->group_list; |
| 841 | ssl->handshake->group_list_heap_allocated = 0; |
| 842 | } |
| 843 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
| 844 | #endif /* MBEDTLS_ECP_C */ |
| 845 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 846 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 847 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Jerry Yu | a69269a | 2022-01-17 21:06:01 +0800 | [diff] [blame] | 848 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Jerry Yu | 713013f | 2022-01-17 18:16:35 +0800 | [diff] [blame] | 849 | /* Heap allocate and translate sig_hashes from internal hash identifiers to |
| 850 | signature algorithms IANA identifiers. */ |
| 851 | if ( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) && |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 852 | ssl->conf->sig_hashes != NULL ) |
| 853 | { |
| 854 | const int *md; |
| 855 | const int *sig_hashes = ssl->conf->sig_hashes; |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 856 | size_t sig_algs_len = 0; |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 857 | uint16_t *p; |
| 858 | |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 859 | #if defined(static_assert) |
| 860 | static_assert( MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN |
| 861 | <= ( SIZE_MAX - ( 2 * sizeof(uint16_t) ) ), |
| 862 | "MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN too big" ); |
Jerry Yu | a68dca2 | 2022-01-20 16:28:27 +0800 | [diff] [blame] | 863 | #endif |
| 864 | |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 865 | for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ ) |
| 866 | { |
| 867 | if( mbedtls_ssl_hash_from_md_alg( *md ) == MBEDTLS_SSL_HASH_NONE ) |
| 868 | continue; |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 869 | #if defined(MBEDTLS_ECDSA_C) |
| 870 | sig_algs_len += sizeof( uint16_t ); |
| 871 | #endif |
Jerry Yu | a68dca2 | 2022-01-20 16:28:27 +0800 | [diff] [blame] | 872 | |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 873 | #if defined(MBEDTLS_RSA_C) |
| 874 | sig_algs_len += sizeof( uint16_t ); |
| 875 | #endif |
| 876 | if( sig_algs_len > MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN ) |
| 877 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 878 | } |
| 879 | |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 880 | if( sig_algs_len < MBEDTLS_SSL_MIN_SIG_ALG_LIST_LEN ) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 881 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); |
| 882 | |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 883 | ssl->handshake->sig_algs = mbedtls_calloc( 1, sig_algs_len + |
| 884 | sizeof( uint16_t )); |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 885 | if( ssl->handshake->sig_algs == NULL ) |
| 886 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 887 | |
| 888 | p = (uint16_t *)ssl->handshake->sig_algs; |
| 889 | for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ ) |
| 890 | { |
| 891 | unsigned char hash = mbedtls_ssl_hash_from_md_alg( *md ); |
| 892 | if( hash == MBEDTLS_SSL_HASH_NONE ) |
| 893 | continue; |
Jerry Yu | 6106fdc | 2022-01-12 16:36:14 +0800 | [diff] [blame] | 894 | #if defined(MBEDTLS_ECDSA_C) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 895 | *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_ECDSA); |
| 896 | p++; |
Jerry Yu | 6106fdc | 2022-01-12 16:36:14 +0800 | [diff] [blame] | 897 | #endif |
| 898 | #if defined(MBEDTLS_RSA_C) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 899 | *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_RSA); |
| 900 | p++; |
Jerry Yu | 6106fdc | 2022-01-12 16:36:14 +0800 | [diff] [blame] | 901 | #endif |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 902 | } |
Gabor Mezei | 15b95a6 | 2022-05-09 16:37:58 +0200 | [diff] [blame] | 903 | *p = MBEDTLS_TLS_SIG_NONE; |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 904 | ssl->handshake->sig_algs_heap_allocated = 1; |
| 905 | } |
| 906 | else |
Jerry Yu | a69269a | 2022-01-17 21:06:01 +0800 | [diff] [blame] | 907 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 908 | { |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 909 | ssl->handshake->sig_algs_heap_allocated = 0; |
| 910 | } |
Jerry Yu | cc53910 | 2022-06-27 16:27:35 +0800 | [diff] [blame] | 911 | #endif /* !MBEDTLS_DEPRECATED_REMOVED */ |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 912 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 913 | return( 0 ); |
| 914 | } |
| 915 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 916 | #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] | 917 | /* Dummy cookie callbacks for defaults */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 918 | MBEDTLS_CHECK_RETURN_CRITICAL |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 919 | static int ssl_cookie_write_dummy( void *ctx, |
| 920 | unsigned char **p, unsigned char *end, |
| 921 | const unsigned char *cli_id, size_t cli_id_len ) |
| 922 | { |
| 923 | ((void) ctx); |
| 924 | ((void) p); |
| 925 | ((void) end); |
| 926 | ((void) cli_id); |
| 927 | ((void) cli_id_len); |
| 928 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 929 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 930 | } |
| 931 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 932 | MBEDTLS_CHECK_RETURN_CRITICAL |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 933 | static int ssl_cookie_check_dummy( void *ctx, |
| 934 | const unsigned char *cookie, size_t cookie_len, |
| 935 | const unsigned char *cli_id, size_t cli_id_len ) |
| 936 | { |
| 937 | ((void) ctx); |
| 938 | ((void) cookie); |
| 939 | ((void) cookie_len); |
| 940 | ((void) cli_id); |
| 941 | ((void) cli_id_len); |
| 942 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 943 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 944 | } |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 945 | #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 946 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 947 | /* |
| 948 | * Initialize an SSL context |
| 949 | */ |
Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 950 | void mbedtls_ssl_init( mbedtls_ssl_context *ssl ) |
| 951 | { |
| 952 | memset( ssl, 0, sizeof( mbedtls_ssl_context ) ); |
| 953 | } |
| 954 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 955 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 956 | static int ssl_conf_version_check( const mbedtls_ssl_context *ssl ) |
| 957 | { |
Ronald Cron | 086ee0b | 2022-03-15 15:18:51 +0100 | [diff] [blame] | 958 | const mbedtls_ssl_config *conf = ssl->conf; |
| 959 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 960 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Ronald Cron | 086ee0b | 2022-03-15 15:18:51 +0100 | [diff] [blame] | 961 | if( mbedtls_ssl_conf_is_tls13_only( conf ) ) |
| 962 | { |
XiaokangQian | ed582dd | 2022-04-13 08:21:05 +0000 | [diff] [blame] | 963 | if( conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 964 | { |
| 965 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS 1.3 is not yet supported." ) ); |
| 966 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 967 | } |
| 968 | |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 969 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls13 only." ) ); |
| 970 | return( 0 ); |
| 971 | } |
| 972 | #endif |
| 973 | |
| 974 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Ronald Cron | 086ee0b | 2022-03-15 15:18:51 +0100 | [diff] [blame] | 975 | if( mbedtls_ssl_conf_is_tls12_only( conf ) ) |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 976 | { |
| 977 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls12 only." ) ); |
| 978 | return( 0 ); |
| 979 | } |
| 980 | #endif |
| 981 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 982 | #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] | 983 | if( mbedtls_ssl_conf_is_hybrid_tls12_tls13( conf ) ) |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 984 | { |
XiaokangQian | ed582dd | 2022-04-13 08:21:05 +0000 | [diff] [blame] | 985 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 986 | { |
| 987 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS not yet supported in Hybrid TLS 1.3 + TLS 1.2" ) ); |
| 988 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 989 | } |
| 990 | |
XiaokangQian | 8f9dfe4 | 2022-04-15 02:52:39 +0000 | [diff] [blame] | 991 | if( conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 992 | { |
| 993 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS 1.3 server is not supported yet." ) ); |
| 994 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 995 | } |
| 996 | |
| 997 | |
Ronald Cron | e1d3f06 | 2022-02-10 14:50:54 +0100 | [diff] [blame] | 998 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is TLS 1.3 or TLS 1.2." ) ); |
| 999 | return( 0 ); |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1000 | } |
| 1001 | #endif |
| 1002 | |
| 1003 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "The SSL configuration is invalid." ) ); |
| 1004 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); |
| 1005 | } |
| 1006 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 1007 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1008 | static int ssl_conf_check(const mbedtls_ssl_context *ssl) |
| 1009 | { |
| 1010 | int ret; |
| 1011 | ret = ssl_conf_version_check( ssl ); |
| 1012 | if( ret != 0 ) |
| 1013 | return( ret ); |
| 1014 | |
| 1015 | /* Space for further checks */ |
| 1016 | |
| 1017 | return( 0 ); |
| 1018 | } |
| 1019 | |
Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 1020 | /* |
| 1021 | * Setup an SSL context |
| 1022 | */ |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 1023 | |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 1024 | int mbedtls_ssl_setup( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 1897af9 | 2015-05-10 23:27:38 +0200 | [diff] [blame] | 1025 | const mbedtls_ssl_config *conf ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1026 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1027 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1028 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; |
| 1029 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1030 | |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 1031 | ssl->conf = conf; |
Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 1032 | |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1033 | if( ( ret = ssl_conf_check( ssl ) ) != 0 ) |
| 1034 | return( ret ); |
| 1035 | |
Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 1036 | /* |
Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 1037 | * Prepare base structures |
Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 1038 | */ |
k-stachowiak | c9a5f02 | 2018-07-24 13:53:31 +0200 | [diff] [blame] | 1039 | |
| 1040 | /* Set to NULL in case of an error condition */ |
| 1041 | ssl->out_buf = NULL; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1042 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1043 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 1044 | ssl->in_buf_len = in_buf_len; |
| 1045 | #endif |
| 1046 | ssl->in_buf = mbedtls_calloc( 1, in_buf_len ); |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 1047 | if( ssl->in_buf == NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1048 | { |
Paul Elliott | d48d5c6 | 2021-01-07 14:47:05 +0000 | [diff] [blame] | 1049 | 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] | 1050 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1051 | goto error; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 1052 | } |
| 1053 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1054 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 1055 | ssl->out_buf_len = out_buf_len; |
| 1056 | #endif |
| 1057 | ssl->out_buf = mbedtls_calloc( 1, out_buf_len ); |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 1058 | if( ssl->out_buf == NULL ) |
| 1059 | { |
Paul Elliott | d48d5c6 | 2021-01-07 14:47:05 +0000 | [diff] [blame] | 1060 | 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] | 1061 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1062 | goto error; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1063 | } |
| 1064 | |
Hanno Becker | 3e6f8ab | 2020-02-05 10:40:57 +0000 | [diff] [blame] | 1065 | mbedtls_ssl_reset_in_out_pointers( ssl ); |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 1066 | |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 1067 | #if defined(MBEDTLS_SSL_DTLS_SRTP) |
Ron Eldor | 3adb992 | 2017-12-21 10:15:08 +0200 | [diff] [blame] | 1068 | memset( &ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info) ); |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 1069 | #endif |
| 1070 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1071 | if( ( ret = ssl_handshake_init( ssl ) ) != 0 ) |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1072 | goto error; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1073 | |
| 1074 | return( 0 ); |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1075 | |
| 1076 | error: |
| 1077 | mbedtls_free( ssl->in_buf ); |
| 1078 | mbedtls_free( ssl->out_buf ); |
| 1079 | |
| 1080 | ssl->conf = NULL; |
| 1081 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1082 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 1083 | ssl->in_buf_len = 0; |
| 1084 | ssl->out_buf_len = 0; |
| 1085 | #endif |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1086 | ssl->in_buf = NULL; |
| 1087 | ssl->out_buf = NULL; |
| 1088 | |
| 1089 | ssl->in_hdr = NULL; |
| 1090 | ssl->in_ctr = NULL; |
| 1091 | ssl->in_len = NULL; |
| 1092 | ssl->in_iv = NULL; |
| 1093 | ssl->in_msg = NULL; |
| 1094 | |
| 1095 | ssl->out_hdr = NULL; |
| 1096 | ssl->out_ctr = NULL; |
| 1097 | ssl->out_len = NULL; |
| 1098 | ssl->out_iv = NULL; |
| 1099 | ssl->out_msg = NULL; |
| 1100 | |
k-stachowiak | 9f7798e | 2018-07-31 16:52:32 +0200 | [diff] [blame] | 1101 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1102 | } |
| 1103 | |
| 1104 | /* |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 1105 | * Reset an initialized and used SSL context for re-use while retaining |
| 1106 | * all application-set variables, function pointers and data. |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 1107 | * |
| 1108 | * If partial is non-zero, keep data in the input buffer and client ID. |
| 1109 | * (Use when a DTLS client reconnects from the same port.) |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 1110 | */ |
XiaokangQian | 78b1fa7 | 2022-01-19 06:56:30 +0000 | [diff] [blame] | 1111 | void mbedtls_ssl_session_reset_msg_layer( mbedtls_ssl_context *ssl, |
| 1112 | int partial ) |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 1113 | { |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1114 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 1115 | size_t in_buf_len = ssl->in_buf_len; |
| 1116 | size_t out_buf_len = ssl->out_buf_len; |
| 1117 | #else |
| 1118 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; |
| 1119 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; |
| 1120 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1121 | |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1122 | #if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || !defined(MBEDTLS_SSL_SRV_C) |
| 1123 | partial = 0; |
Hanno Becker | 7e77213 | 2018-08-10 12:38:21 +0100 | [diff] [blame] | 1124 | #endif |
| 1125 | |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 1126 | /* Cancel any possibly running timer */ |
Hanno Becker | 0f57a65 | 2020-02-05 10:37:26 +0000 | [diff] [blame] | 1127 | mbedtls_ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 1128 | |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1129 | mbedtls_ssl_reset_in_out_pointers( ssl ); |
| 1130 | |
| 1131 | /* Reset incoming message parsing */ |
| 1132 | ssl->in_offt = NULL; |
| 1133 | ssl->nb_zero = 0; |
| 1134 | ssl->in_msgtype = 0; |
| 1135 | ssl->in_msglen = 0; |
| 1136 | ssl->in_hslen = 0; |
| 1137 | ssl->keep_current_message = 0; |
| 1138 | ssl->transform_in = NULL; |
| 1139 | |
| 1140 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 1141 | ssl->next_record_offset = 0; |
| 1142 | ssl->in_epoch = 0; |
| 1143 | #endif |
| 1144 | |
| 1145 | /* Keep current datagram if partial == 1 */ |
| 1146 | if( partial == 0 ) |
| 1147 | { |
| 1148 | ssl->in_left = 0; |
| 1149 | memset( ssl->in_buf, 0, in_buf_len ); |
| 1150 | } |
| 1151 | |
Ronald Cron | ad8c17b | 2022-06-10 17:18:09 +0200 | [diff] [blame] | 1152 | ssl->send_alert = 0; |
| 1153 | |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1154 | /* Reset outgoing message writing */ |
| 1155 | ssl->out_msgtype = 0; |
| 1156 | ssl->out_msglen = 0; |
| 1157 | ssl->out_left = 0; |
| 1158 | memset( ssl->out_buf, 0, out_buf_len ); |
| 1159 | memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) ); |
| 1160 | ssl->transform_out = NULL; |
| 1161 | |
| 1162 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 1163 | mbedtls_ssl_dtls_replay_reset( ssl ); |
| 1164 | #endif |
| 1165 | |
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1166 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1167 | if( ssl->transform ) |
| 1168 | { |
| 1169 | mbedtls_ssl_transform_free( ssl->transform ); |
| 1170 | mbedtls_free( ssl->transform ); |
| 1171 | ssl->transform = NULL; |
| 1172 | } |
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1173 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 1174 | |
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1175 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 1176 | mbedtls_ssl_transform_free( ssl->transform_application ); |
| 1177 | mbedtls_free( ssl->transform_application ); |
| 1178 | ssl->transform_application = NULL; |
| 1179 | |
| 1180 | if( ssl->handshake != NULL ) |
| 1181 | { |
| 1182 | mbedtls_ssl_transform_free( ssl->handshake->transform_earlydata ); |
| 1183 | mbedtls_free( ssl->handshake->transform_earlydata ); |
| 1184 | ssl->handshake->transform_earlydata = NULL; |
| 1185 | |
| 1186 | mbedtls_ssl_transform_free( ssl->handshake->transform_handshake ); |
| 1187 | mbedtls_free( ssl->handshake->transform_handshake ); |
| 1188 | ssl->handshake->transform_handshake = NULL; |
| 1189 | } |
| 1190 | |
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1191 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1192 | } |
| 1193 | |
| 1194 | int mbedtls_ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial ) |
| 1195 | { |
| 1196 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 1197 | |
| 1198 | ssl->state = MBEDTLS_SSL_HELLO_REQUEST; |
| 1199 | |
XiaokangQian | 78b1fa7 | 2022-01-19 06:56:30 +0000 | [diff] [blame] | 1200 | mbedtls_ssl_session_reset_msg_layer( ssl, partial ); |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1201 | |
| 1202 | /* Reset renegotiation state */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1203 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 1204 | ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE; |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 1205 | ssl->renego_records_seen = 0; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1206 | |
| 1207 | ssl->verify_data_len = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1208 | memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN ); |
| 1209 | 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] | 1210 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1211 | ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1212 | |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1213 | ssl->session_in = NULL; |
Hanno Becker | 7864090 | 2018-08-13 16:35:15 +0100 | [diff] [blame] | 1214 | ssl->session_out = NULL; |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 1215 | if( ssl->session ) |
| 1216 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1217 | mbedtls_ssl_session_free( ssl->session ); |
| 1218 | mbedtls_free( ssl->session ); |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 1219 | ssl->session = NULL; |
| 1220 | } |
| 1221 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1222 | #if defined(MBEDTLS_SSL_ALPN) |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 1223 | ssl->alpn_chosen = NULL; |
| 1224 | #endif |
| 1225 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 1226 | #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C) |
David Horstmann | 3b2276a | 2022-10-06 14:49:08 +0100 | [diff] [blame] | 1227 | int free_cli_id = 1; |
Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 1228 | #if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) |
David Horstmann | 3b2276a | 2022-10-06 14:49:08 +0100 | [diff] [blame] | 1229 | free_cli_id = ( partial == 0 ); |
Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 1230 | #endif |
David Horstmann | 3b2276a | 2022-10-06 14:49:08 +0100 | [diff] [blame] | 1231 | if( free_cli_id ) |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 1232 | { |
| 1233 | mbedtls_free( ssl->cli_id ); |
| 1234 | ssl->cli_id = NULL; |
| 1235 | ssl->cli_id_len = 0; |
| 1236 | } |
Manuel Pégourié-Gonnard | 43c0218 | 2014-07-22 17:32:01 +0200 | [diff] [blame] | 1237 | #endif |
| 1238 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1239 | if( ( ret = ssl_handshake_init( ssl ) ) != 0 ) |
| 1240 | return( ret ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1241 | |
| 1242 | return( 0 ); |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 1243 | } |
| 1244 | |
Manuel Pégourié-Gonnard | 779e429 | 2013-08-03 13:50:48 +0200 | [diff] [blame] | 1245 | /* |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 1246 | * Reset an initialized and used SSL context for re-use while retaining |
| 1247 | * all application-set variables, function pointers and data. |
| 1248 | */ |
| 1249 | int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl ) |
| 1250 | { |
Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 1251 | return( mbedtls_ssl_session_reset_int( ssl, 0 ) ); |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 1252 | } |
| 1253 | |
| 1254 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1255 | * SSL set accessors |
| 1256 | */ |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1257 | void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1258 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1259 | conf->endpoint = endpoint; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1260 | } |
| 1261 | |
Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 1262 | 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] | 1263 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1264 | conf->transport = transport; |
Manuel Pégourié-Gonnard | 0b1ff29 | 2014-02-06 13:04:16 +0100 | [diff] [blame] | 1265 | } |
| 1266 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1267 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1268 | 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] | 1269 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1270 | conf->anti_replay = mode; |
Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 1271 | } |
| 1272 | #endif |
| 1273 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1274 | 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] | 1275 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1276 | conf->badmac_limit = limit; |
Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 1277 | } |
Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 1278 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1279 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 1280 | |
Hanno Becker | 1841b0a | 2018-08-24 11:13:57 +0100 | [diff] [blame] | 1281 | void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl, |
| 1282 | unsigned allow_packing ) |
Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 1283 | { |
| 1284 | ssl->disable_datagram_packing = !allow_packing; |
| 1285 | } |
| 1286 | |
| 1287 | void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf, |
| 1288 | uint32_t min, uint32_t max ) |
Manuel Pégourié-Gonnard | 905dd24 | 2014-10-01 12:03:55 +0200 | [diff] [blame] | 1289 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1290 | conf->hs_timeout_min = min; |
| 1291 | conf->hs_timeout_max = max; |
Manuel Pégourié-Gonnard | 905dd24 | 2014-10-01 12:03:55 +0200 | [diff] [blame] | 1292 | } |
| 1293 | #endif |
| 1294 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1295 | void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1296 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1297 | conf->authmode = authmode; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1298 | } |
| 1299 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1300 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1301 | void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 1302 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 1303 | void *p_vrfy ) |
| 1304 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1305 | conf->f_vrfy = f_vrfy; |
| 1306 | conf->p_vrfy = p_vrfy; |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 1307 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1308 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 1309 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1310 | void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf, |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 1311 | int (*f_rng)(void *, unsigned char *, size_t), |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1312 | void *p_rng ) |
| 1313 | { |
Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 1314 | conf->f_rng = f_rng; |
| 1315 | conf->p_rng = p_rng; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1316 | } |
| 1317 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1318 | void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | fd47423 | 2015-06-23 16:34:24 +0200 | [diff] [blame] | 1319 | void (*f_dbg)(void *, int, const char *, int, const char *), |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1320 | void *p_dbg ) |
| 1321 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1322 | conf->f_dbg = f_dbg; |
| 1323 | conf->p_dbg = p_dbg; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1324 | } |
| 1325 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1326 | void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 1327 | void *p_bio, |
Simon Butcher | e846b51 | 2016-03-01 17:31:49 +0000 | [diff] [blame] | 1328 | mbedtls_ssl_send_t *f_send, |
| 1329 | mbedtls_ssl_recv_t *f_recv, |
| 1330 | mbedtls_ssl_recv_timeout_t *f_recv_timeout ) |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 1331 | { |
| 1332 | ssl->p_bio = p_bio; |
| 1333 | ssl->f_send = f_send; |
| 1334 | ssl->f_recv = f_recv; |
| 1335 | ssl->f_recv_timeout = f_recv_timeout; |
Manuel Pégourié-Gonnard | 97fd52c | 2015-05-06 15:38:52 +0100 | [diff] [blame] | 1336 | } |
| 1337 | |
Manuel Pégourié-Gonnard | 6e7aaca | 2018-08-20 10:37:23 +0200 | [diff] [blame] | 1338 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 1339 | void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu ) |
| 1340 | { |
| 1341 | ssl->mtu = mtu; |
| 1342 | } |
| 1343 | #endif |
| 1344 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1345 | 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] | 1346 | { |
| 1347 | conf->read_timeout = timeout; |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 1348 | } |
| 1349 | |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 1350 | void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl, |
| 1351 | void *p_timer, |
Simon Butcher | e846b51 | 2016-03-01 17:31:49 +0000 | [diff] [blame] | 1352 | mbedtls_ssl_set_timer_t *f_set_timer, |
| 1353 | mbedtls_ssl_get_timer_t *f_get_timer ) |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 1354 | { |
| 1355 | ssl->p_timer = p_timer; |
| 1356 | ssl->f_set_timer = f_set_timer; |
| 1357 | ssl->f_get_timer = f_get_timer; |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 1358 | |
| 1359 | /* Make sure we start with no timer running */ |
Hanno Becker | 0f57a65 | 2020-02-05 10:37:26 +0000 | [diff] [blame] | 1360 | mbedtls_ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 1361 | } |
| 1362 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1363 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1364 | void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf, |
Hanno Becker | a637ff6 | 2021-04-15 08:42:48 +0100 | [diff] [blame] | 1365 | void *p_cache, |
| 1366 | mbedtls_ssl_cache_get_t *f_get_cache, |
| 1367 | mbedtls_ssl_cache_set_t *f_set_cache ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1368 | { |
Manuel Pégourié-Gonnard | 5cb3308 | 2015-05-06 18:06:26 +0100 | [diff] [blame] | 1369 | conf->p_cache = p_cache; |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1370 | conf->f_get_cache = f_get_cache; |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1371 | conf->f_set_cache = f_set_cache; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1372 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1373 | #endif /* MBEDTLS_SSL_SRV_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1374 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1375 | #if defined(MBEDTLS_SSL_CLI_C) |
| 1376 | 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] | 1377 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1378 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1379 | |
| 1380 | if( ssl == NULL || |
| 1381 | session == NULL || |
| 1382 | ssl->session_negotiate == NULL || |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1383 | ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT ) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1384 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1385 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1386 | } |
| 1387 | |
Hanno Becker | e810bbc | 2021-05-14 16:01:05 +0100 | [diff] [blame] | 1388 | if( ssl->handshake->resume == 1 ) |
| 1389 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 1390 | |
Jerry Yu | 2109206 | 2022-10-10 21:21:31 +0800 | [diff] [blame] | 1391 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 1392 | if( session->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 ) |
Jerry Yu | 40afab6 | 2022-10-08 10:42:13 +0800 | [diff] [blame] | 1393 | { |
Jerry Yu | 2109206 | 2022-10-10 21:21:31 +0800 | [diff] [blame] | 1394 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 1395 | mbedtls_ssl_ciphersuite_from_id( session->ciphersuite ); |
| 1396 | |
| 1397 | if( mbedtls_ssl_validate_ciphersuite( |
| 1398 | ssl, ciphersuite_info, MBEDTLS_SSL_VERSION_TLS1_3, |
| 1399 | MBEDTLS_SSL_VERSION_TLS1_3 ) != 0 ) |
| 1400 | { |
| 1401 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "%d is not a valid TLS 1.3 ciphersuite.", |
| 1402 | session->ciphersuite ) ); |
| 1403 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1404 | } |
Jerry Yu | 40afab6 | 2022-10-08 10:42:13 +0800 | [diff] [blame] | 1405 | } |
Jerry Yu | 2109206 | 2022-10-10 21:21:31 +0800 | [diff] [blame] | 1406 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Jerry Yu | 40afab6 | 2022-10-08 10:42:13 +0800 | [diff] [blame] | 1407 | |
Hanno Becker | 52055ae | 2019-02-06 14:30:46 +0000 | [diff] [blame] | 1408 | if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate, |
| 1409 | session ) ) != 0 ) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1410 | return( ret ); |
| 1411 | |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 1412 | ssl->handshake->resume = 1; |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1413 | |
| 1414 | return( 0 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1415 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1416 | #endif /* MBEDTLS_SSL_CLI_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1417 | |
Mateusz Starzyk | 06b07fb | 2021-02-18 13:55:21 +0100 | [diff] [blame] | 1418 | void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf, |
| 1419 | const int *ciphersuites ) |
| 1420 | { |
Hanno Becker | d60b6c6 | 2021-04-29 12:04:11 +0100 | [diff] [blame] | 1421 | conf->ciphersuite_list = ciphersuites; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1422 | } |
| 1423 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 1424 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | cadebe5 | 2021-08-24 10:36:45 +0800 | [diff] [blame] | 1425 | void mbedtls_ssl_conf_tls13_key_exchange_modes( mbedtls_ssl_config *conf, |
Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 1426 | const int kex_modes ) |
| 1427 | { |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 1428 | 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] | 1429 | } |
Xiaokang Qian | 72de95d | 2022-10-25 02:54:33 +0000 | [diff] [blame] | 1430 | |
| 1431 | #if defined(MBEDTLS_SSL_EARLY_DATA) |
| 1432 | void mbedtls_ssl_tls13_conf_early_data( mbedtls_ssl_config *conf, |
Xiaokang Qian | 72dbfef | 2022-10-26 06:33:57 +0000 | [diff] [blame] | 1433 | int early_data_enabled ) |
Xiaokang Qian | 72de95d | 2022-10-25 02:54:33 +0000 | [diff] [blame] | 1434 | { |
| 1435 | conf->early_data_enabled = early_data_enabled; |
| 1436 | } |
| 1437 | #endif /* MBEDTLS_SSL_EARLY_DATA */ |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 1438 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 1439 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1440 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 6e3ee3a | 2015-06-17 10:58:20 +0200 | [diff] [blame] | 1441 | void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf, |
Nicholas Wilson | 2088e2e | 2015-09-08 16:53:18 +0100 | [diff] [blame] | 1442 | const mbedtls_x509_crt_profile *profile ) |
Manuel Pégourié-Gonnard | 6e3ee3a | 2015-06-17 10:58:20 +0200 | [diff] [blame] | 1443 | { |
| 1444 | conf->cert_profile = profile; |
| 1445 | } |
| 1446 | |
Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 1447 | static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert ) |
| 1448 | { |
| 1449 | mbedtls_ssl_key_cert *cur = key_cert, *next; |
| 1450 | |
| 1451 | while( cur != NULL ) |
| 1452 | { |
| 1453 | next = cur->next; |
| 1454 | mbedtls_free( cur ); |
| 1455 | cur = next; |
| 1456 | } |
| 1457 | } |
| 1458 | |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1459 | /* Append a new keycert entry to a (possibly empty) list */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 1460 | MBEDTLS_CHECK_RETURN_CRITICAL |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1461 | static int ssl_append_key_cert( mbedtls_ssl_key_cert **head, |
| 1462 | mbedtls_x509_crt *cert, |
| 1463 | mbedtls_pk_context *key ) |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1464 | { |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1465 | mbedtls_ssl_key_cert *new_cert; |
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 | if( cert == NULL ) |
| 1468 | { |
| 1469 | /* Free list if cert is null */ |
| 1470 | ssl_key_cert_free( *head ); |
| 1471 | *head = NULL; |
| 1472 | return( 0 ); |
| 1473 | } |
| 1474 | |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1475 | new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) ); |
| 1476 | if( new_cert == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 1477 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1478 | |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1479 | new_cert->cert = cert; |
| 1480 | new_cert->key = key; |
| 1481 | new_cert->next = NULL; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1482 | |
Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 1483 | /* 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] | 1484 | if( *head == NULL ) |
Paul Bakker | 0333b97 | 2013-11-04 17:08:28 +0100 | [diff] [blame] | 1485 | { |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1486 | *head = new_cert; |
Paul Bakker | 0333b97 | 2013-11-04 17:08:28 +0100 | [diff] [blame] | 1487 | } |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1488 | else |
| 1489 | { |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1490 | mbedtls_ssl_key_cert *cur = *head; |
| 1491 | while( cur->next != NULL ) |
| 1492 | cur = cur->next; |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1493 | cur->next = new_cert; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1494 | } |
| 1495 | |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1496 | return( 0 ); |
| 1497 | } |
| 1498 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1499 | int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1500 | mbedtls_x509_crt *own_cert, |
| 1501 | mbedtls_pk_context *pk_key ) |
| 1502 | { |
Manuel Pégourié-Gonnard | 17a40cd | 2015-05-10 23:17:17 +0200 | [diff] [blame] | 1503 | 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] | 1504 | } |
| 1505 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1506 | void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 1507 | mbedtls_x509_crt *ca_chain, |
| 1508 | mbedtls_x509_crl *ca_crl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1509 | { |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 1510 | conf->ca_chain = ca_chain; |
| 1511 | conf->ca_crl = ca_crl; |
Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 1512 | |
| 1513 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 1514 | /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb() |
| 1515 | * cannot be used together. */ |
| 1516 | conf->f_ca_cb = NULL; |
| 1517 | conf->p_ca_cb = NULL; |
| 1518 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1519 | } |
Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 1520 | |
| 1521 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 1522 | void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf, |
Hanno Becker | afd0b0a | 2019-03-27 16:55:01 +0000 | [diff] [blame] | 1523 | mbedtls_x509_crt_ca_cb_t f_ca_cb, |
Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 1524 | void *p_ca_cb ) |
| 1525 | { |
| 1526 | conf->f_ca_cb = f_ca_cb; |
| 1527 | conf->p_ca_cb = p_ca_cb; |
| 1528 | |
| 1529 | /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb() |
| 1530 | * cannot be used together. */ |
| 1531 | conf->ca_chain = NULL; |
| 1532 | conf->ca_crl = NULL; |
| 1533 | } |
| 1534 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1535 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | eb2c658 | 2012-09-27 19:15:01 +0000 | [diff] [blame] | 1536 | |
Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 1537 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Glenn Strauss | 6989407 | 2022-01-24 12:58:00 -0500 | [diff] [blame] | 1538 | const unsigned char *mbedtls_ssl_get_hs_sni( mbedtls_ssl_context *ssl, |
| 1539 | size_t *name_len ) |
| 1540 | { |
| 1541 | *name_len = ssl->handshake->sni_name_len; |
| 1542 | return( ssl->handshake->sni_name ); |
| 1543 | } |
| 1544 | |
Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 1545 | int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl, |
| 1546 | mbedtls_x509_crt *own_cert, |
| 1547 | mbedtls_pk_context *pk_key ) |
| 1548 | { |
| 1549 | return( ssl_append_key_cert( &ssl->handshake->sni_key_cert, |
| 1550 | own_cert, pk_key ) ); |
| 1551 | } |
Manuel Pégourié-Gonnard | 22bfa4b | 2015-05-11 08:46:37 +0200 | [diff] [blame] | 1552 | |
| 1553 | void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl, |
| 1554 | mbedtls_x509_crt *ca_chain, |
| 1555 | mbedtls_x509_crl *ca_crl ) |
| 1556 | { |
| 1557 | ssl->handshake->sni_ca_chain = ca_chain; |
| 1558 | ssl->handshake->sni_ca_crl = ca_crl; |
| 1559 | } |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 1560 | |
Glenn Strauss | 999ef70 | 2022-03-11 01:37:23 -0500 | [diff] [blame] | 1561 | #if defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED) |
| 1562 | void mbedtls_ssl_set_hs_dn_hints( mbedtls_ssl_context *ssl, |
| 1563 | const mbedtls_x509_crt *crt) |
| 1564 | { |
| 1565 | ssl->handshake->dn_hints = crt; |
| 1566 | } |
| 1567 | #endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */ |
| 1568 | |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 1569 | void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl, |
| 1570 | int authmode ) |
| 1571 | { |
| 1572 | ssl->handshake->sni_authmode = authmode; |
| 1573 | } |
Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 1574 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
| 1575 | |
Hanno Becker | 8927c83 | 2019-04-03 12:52:50 +0100 | [diff] [blame] | 1576 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 1577 | void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl, |
| 1578 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), |
| 1579 | void *p_vrfy ) |
| 1580 | { |
| 1581 | ssl->f_vrfy = f_vrfy; |
| 1582 | ssl->p_vrfy = p_vrfy; |
| 1583 | } |
| 1584 | #endif |
| 1585 | |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 1586 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 1587 | /* |
| 1588 | * Set EC J-PAKE password for current handshake |
| 1589 | */ |
| 1590 | int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl, |
| 1591 | const unsigned char *pw, |
| 1592 | size_t pw_len ) |
| 1593 | { |
| 1594 | mbedtls_ecjpake_role role; |
| 1595 | |
Janos Follath | 8eb6413 | 2016-06-03 15:40:57 +0100 | [diff] [blame] | 1596 | if( ssl->handshake == NULL || ssl->conf == NULL ) |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 1597 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1598 | |
| 1599 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 1600 | role = MBEDTLS_ECJPAKE_SERVER; |
| 1601 | else |
| 1602 | role = MBEDTLS_ECJPAKE_CLIENT; |
| 1603 | |
| 1604 | return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx, |
| 1605 | role, |
| 1606 | MBEDTLS_MD_SHA256, |
| 1607 | MBEDTLS_ECP_DP_SECP256R1, |
| 1608 | pw, pw_len ) ); |
| 1609 | } |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 1610 | #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 1611 | |
Ronald Cron | 73fe8df | 2022-10-05 14:31:43 +0200 | [diff] [blame] | 1612 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED) |
Ronald Cron | d29e13e | 2022-10-19 10:33:48 +0200 | [diff] [blame] | 1613 | int mbedtls_ssl_conf_has_static_psk( mbedtls_ssl_config const *conf ) |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 1614 | { |
Ronald Cron | d29e13e | 2022-10-19 10:33:48 +0200 | [diff] [blame] | 1615 | if( conf->psk_identity == NULL || |
| 1616 | conf->psk_identity_len == 0 ) |
| 1617 | { |
| 1618 | return( 0 ); |
| 1619 | } |
| 1620 | |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 1621 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Ronald Cron | d29e13e | 2022-10-19 10:33:48 +0200 | [diff] [blame] | 1622 | if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) ) |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 1623 | return( 1 ); |
| 1624 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Ronald Cron | d29e13e | 2022-10-19 10:33:48 +0200 | [diff] [blame] | 1625 | |
| 1626 | if( conf->psk != NULL && conf->psk_len != 0 ) |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 1627 | return( 1 ); |
| 1628 | |
| 1629 | return( 0 ); |
| 1630 | } |
| 1631 | |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1632 | static void ssl_conf_remove_psk( mbedtls_ssl_config *conf ) |
| 1633 | { |
| 1634 | /* Remove reference to existing PSK, if any. */ |
| 1635 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1636 | if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) ) |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1637 | { |
| 1638 | /* The maintenance of the PSK key slot is the |
| 1639 | * user's responsibility. */ |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1640 | conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT; |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1641 | } |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1642 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 1643 | if( conf->psk != NULL ) |
| 1644 | { |
| 1645 | mbedtls_platform_zeroize( conf->psk, conf->psk_len ); |
| 1646 | |
| 1647 | mbedtls_free( conf->psk ); |
| 1648 | conf->psk = NULL; |
| 1649 | conf->psk_len = 0; |
| 1650 | } |
| 1651 | |
| 1652 | /* Remove reference to PSK identity, if any. */ |
| 1653 | if( conf->psk_identity != NULL ) |
| 1654 | { |
| 1655 | mbedtls_free( conf->psk_identity ); |
| 1656 | conf->psk_identity = NULL; |
| 1657 | conf->psk_identity_len = 0; |
| 1658 | } |
| 1659 | } |
| 1660 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1661 | /* This function assumes that PSK identity in the SSL config is unset. |
| 1662 | * It checks that the provided identity is well-formed and attempts |
| 1663 | * to make a copy of it in the SSL config. |
| 1664 | * On failure, the PSK identity in the config remains unset. */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 1665 | MBEDTLS_CHECK_RETURN_CRITICAL |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1666 | static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf, |
| 1667 | unsigned char const *psk_identity, |
| 1668 | size_t psk_identity_len ) |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 1669 | { |
Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 1670 | /* Identity len will be encoded on two bytes */ |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1671 | if( psk_identity == NULL || |
Ronald Cron | 2a87e9b | 2022-10-19 10:55:26 +0200 | [diff] [blame] | 1672 | psk_identity_len == 0 || |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1673 | ( psk_identity_len >> 16 ) != 0 || |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 1674 | psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN ) |
Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 1675 | { |
| 1676 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1677 | } |
| 1678 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1679 | conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ); |
| 1680 | if( conf->psk_identity == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 1681 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 1682 | |
Manuel Pégourié-Gonnard | 120fdbd | 2015-05-07 17:07:50 +0100 | [diff] [blame] | 1683 | conf->psk_identity_len = psk_identity_len; |
Manuel Pégourié-Gonnard | 120fdbd | 2015-05-07 17:07:50 +0100 | [diff] [blame] | 1684 | memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len ); |
Paul Bakker | 5ad403f | 2013-09-18 21:21:30 +0200 | [diff] [blame] | 1685 | |
| 1686 | return( 0 ); |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 1687 | } |
| 1688 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1689 | int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf, |
| 1690 | const unsigned char *psk, size_t psk_len, |
| 1691 | const unsigned char *psk_identity, size_t psk_identity_len ) |
| 1692 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1693 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 1694 | |
| 1695 | /* We currently only support one PSK, raw or opaque. */ |
Ronald Cron | d29e13e | 2022-10-19 10:33:48 +0200 | [diff] [blame] | 1696 | if( mbedtls_ssl_conf_has_static_psk( conf ) ) |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 1697 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1698 | |
| 1699 | /* Check and set raw PSK */ |
Piotr Nowicki | 9926eaf | 2019-11-20 14:54:36 +0100 | [diff] [blame] | 1700 | if( psk == NULL ) |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1701 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Piotr Nowicki | 9926eaf | 2019-11-20 14:54:36 +0100 | [diff] [blame] | 1702 | if( psk_len == 0 ) |
| 1703 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1704 | if( psk_len > MBEDTLS_PSK_MAX_LEN ) |
| 1705 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1706 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1707 | if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ) |
| 1708 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 1709 | conf->psk_len = psk_len; |
| 1710 | memcpy( conf->psk, psk, conf->psk_len ); |
| 1711 | |
| 1712 | /* Check and set PSK Identity */ |
| 1713 | ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len ); |
| 1714 | if( ret != 0 ) |
| 1715 | ssl_conf_remove_psk( conf ); |
| 1716 | |
| 1717 | return( ret ); |
| 1718 | } |
| 1719 | |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1720 | static void ssl_remove_psk( mbedtls_ssl_context *ssl ) |
| 1721 | { |
| 1722 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1723 | if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) ) |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1724 | { |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 1725 | /* The maintenance of the external PSK key slot is the |
| 1726 | * user's responsibility. */ |
| 1727 | if( ssl->handshake->psk_opaque_is_internal ) |
| 1728 | { |
| 1729 | psa_destroy_key( ssl->handshake->psk_opaque ); |
| 1730 | ssl->handshake->psk_opaque_is_internal = 0; |
| 1731 | } |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1732 | ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT; |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1733 | } |
Neil Armstrong | e952a30 | 2022-05-03 10:22:14 +0200 | [diff] [blame] | 1734 | #else |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1735 | if( ssl->handshake->psk != NULL ) |
| 1736 | { |
| 1737 | mbedtls_platform_zeroize( ssl->handshake->psk, |
| 1738 | ssl->handshake->psk_len ); |
| 1739 | mbedtls_free( ssl->handshake->psk ); |
| 1740 | ssl->handshake->psk_len = 0; |
| 1741 | } |
Neil Armstrong | e952a30 | 2022-05-03 10:22:14 +0200 | [diff] [blame] | 1742 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1743 | } |
| 1744 | |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1745 | int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl, |
| 1746 | const unsigned char *psk, size_t psk_len ) |
| 1747 | { |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 1748 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1749 | psa_key_attributes_t key_attributes = psa_key_attributes_init(); |
Jerry Yu | 5d01c05 | 2022-08-17 10:18:10 +0800 | [diff] [blame] | 1750 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Jerry Yu | 24b8c81 | 2022-08-20 19:06:56 +0800 | [diff] [blame] | 1751 | psa_algorithm_t alg = PSA_ALG_NONE; |
Jerry Yu | 5d01c05 | 2022-08-17 10:18:10 +0800 | [diff] [blame] | 1752 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 1753 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 1754 | |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1755 | if( psk == NULL || ssl->handshake == NULL ) |
| 1756 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1757 | |
| 1758 | if( psk_len > MBEDTLS_PSK_MAX_LEN ) |
| 1759 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1760 | |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1761 | ssl_remove_psk( ssl ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1762 | |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 1763 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Jerry Yu | ccc68a4 | 2022-07-26 16:39:20 +0800 | [diff] [blame] | 1764 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 1765 | if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 ) |
| 1766 | { |
Jerry Yu | 6cf6b47 | 2022-08-16 14:50:28 +0800 | [diff] [blame] | 1767 | if( ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384 ) |
Jerry Yu | ccc68a4 | 2022-07-26 16:39:20 +0800 | [diff] [blame] | 1768 | alg = PSA_ALG_TLS12_PSK_TO_MS( PSA_ALG_SHA_384 ); |
| 1769 | else |
| 1770 | alg = PSA_ALG_TLS12_PSK_TO_MS( PSA_ALG_SHA_256 ); |
| 1771 | psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE ); |
| 1772 | } |
| 1773 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 1774 | |
Jerry Yu | 568ec25 | 2022-07-22 21:27:34 +0800 | [diff] [blame] | 1775 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | ccc68a4 | 2022-07-26 16:39:20 +0800 | [diff] [blame] | 1776 | if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 ) |
| 1777 | { |
| 1778 | alg = PSA_ALG_HKDF_EXTRACT( PSA_ALG_ANY_HASH ); |
| 1779 | psa_set_key_usage_flags( &key_attributes, |
| 1780 | PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT ); |
| 1781 | } |
| 1782 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 1783 | |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 1784 | psa_set_key_algorithm( &key_attributes, alg ); |
| 1785 | psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE ); |
| 1786 | |
| 1787 | status = psa_import_key( &key_attributes, psk, psk_len, &key ); |
| 1788 | if( status != PSA_SUCCESS ) |
| 1789 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 1790 | |
| 1791 | /* Allow calling psa_destroy_key() on psk remove */ |
| 1792 | ssl->handshake->psk_opaque_is_internal = 1; |
| 1793 | return mbedtls_ssl_set_hs_psk_opaque( ssl, key ); |
| 1794 | #else |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 1795 | if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 1796 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1797 | |
| 1798 | ssl->handshake->psk_len = psk_len; |
| 1799 | memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len ); |
| 1800 | |
| 1801 | return( 0 ); |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 1802 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1803 | } |
| 1804 | |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1805 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1806 | int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf, |
Andrzej Kurek | 03e0146 | 2022-01-03 12:53:24 +0100 | [diff] [blame] | 1807 | mbedtls_svc_key_id_t psk, |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1808 | const unsigned char *psk_identity, |
| 1809 | size_t psk_identity_len ) |
| 1810 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1811 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 1812 | |
| 1813 | /* We currently only support one PSK, raw or opaque. */ |
Ronald Cron | d29e13e | 2022-10-19 10:33:48 +0200 | [diff] [blame] | 1814 | if( mbedtls_ssl_conf_has_static_psk( conf ) ) |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 1815 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1816 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1817 | /* Check and set opaque PSK */ |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1818 | if( mbedtls_svc_key_id_is_null( psk ) ) |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1819 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1820 | conf->psk_opaque = psk; |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1821 | |
| 1822 | /* Check and set PSK Identity */ |
| 1823 | ret = ssl_conf_set_psk_identity( conf, psk_identity, |
| 1824 | psk_identity_len ); |
| 1825 | if( ret != 0 ) |
| 1826 | ssl_conf_remove_psk( conf ); |
| 1827 | |
| 1828 | return( ret ); |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1829 | } |
| 1830 | |
Przemyslaw Stekiel | 6928a51 | 2022-02-03 13:50:35 +0100 | [diff] [blame] | 1831 | int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl, |
| 1832 | mbedtls_svc_key_id_t psk ) |
| 1833 | { |
| 1834 | if( ( mbedtls_svc_key_id_is_null( psk ) ) || |
| 1835 | ( ssl->handshake == NULL ) ) |
| 1836 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1837 | |
| 1838 | ssl_remove_psk( ssl ); |
| 1839 | ssl->handshake->psk_opaque = psk; |
| 1840 | return( 0 ); |
| 1841 | } |
| 1842 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 1843 | |
Jerry Yu | 8897c07 | 2022-08-12 13:56:53 +0800 | [diff] [blame] | 1844 | #if defined(MBEDTLS_SSL_SRV_C) |
Przemyslaw Stekiel | 6928a51 | 2022-02-03 13:50:35 +0100 | [diff] [blame] | 1845 | void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf, |
| 1846 | int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *, |
| 1847 | size_t), |
| 1848 | void *p_psk ) |
| 1849 | { |
| 1850 | conf->f_psk = f_psk; |
| 1851 | conf->p_psk = p_psk; |
| 1852 | } |
Jerry Yu | 8897c07 | 2022-08-12 13:56:53 +0800 | [diff] [blame] | 1853 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 1854 | |
Ronald Cron | 73fe8df | 2022-10-05 14:31:43 +0200 | [diff] [blame] | 1855 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */ |
Przemyslaw Stekiel | 6928a51 | 2022-02-03 13:50:35 +0100 | [diff] [blame] | 1856 | |
| 1857 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 301711e | 2022-04-26 16:57:05 +0200 | [diff] [blame] | 1858 | static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode( |
| 1859 | psa_algorithm_t alg ) |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 1860 | { |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 1861 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1862 | if( alg == PSA_ALG_CBC_NO_PADDING ) |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 1863 | return( MBEDTLS_SSL_MODE_CBC ); |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 1864 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1865 | if( PSA_ALG_IS_AEAD( alg ) ) |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 1866 | return( MBEDTLS_SSL_MODE_AEAD ); |
Gilles Peskine | 301711e | 2022-04-26 16:57:05 +0200 | [diff] [blame] | 1867 | return( MBEDTLS_SSL_MODE_STREAM ); |
| 1868 | } |
| 1869 | |
| 1870 | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
| 1871 | |
| 1872 | static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode( |
| 1873 | mbedtls_cipher_mode_t mode ) |
| 1874 | { |
| 1875 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
| 1876 | if( mode == MBEDTLS_MODE_CBC ) |
| 1877 | return( MBEDTLS_SSL_MODE_CBC ); |
| 1878 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
| 1879 | |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 1880 | #if defined(MBEDTLS_GCM_C) || \ |
| 1881 | defined(MBEDTLS_CCM_C) || \ |
| 1882 | defined(MBEDTLS_CHACHAPOLY_C) |
| 1883 | if( mode == MBEDTLS_MODE_GCM || |
| 1884 | mode == MBEDTLS_MODE_CCM || |
| 1885 | mode == MBEDTLS_MODE_CHACHAPOLY ) |
| 1886 | return( MBEDTLS_SSL_MODE_AEAD ); |
| 1887 | #endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */ |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 1888 | |
| 1889 | return( MBEDTLS_SSL_MODE_STREAM ); |
| 1890 | } |
Gilles Peskine | 301711e | 2022-04-26 16:57:05 +0200 | [diff] [blame] | 1891 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 1892 | |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 1893 | static mbedtls_ssl_mode_t mbedtls_ssl_get_actual_mode( |
| 1894 | mbedtls_ssl_mode_t base_mode, |
| 1895 | int encrypt_then_mac ) |
| 1896 | { |
| 1897 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
| 1898 | if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED && |
| 1899 | base_mode == MBEDTLS_SSL_MODE_CBC ) |
| 1900 | { |
| 1901 | return( MBEDTLS_SSL_MODE_CBC_ETM ); |
| 1902 | } |
| 1903 | #else |
| 1904 | (void) encrypt_then_mac; |
| 1905 | #endif |
| 1906 | return( base_mode ); |
| 1907 | } |
| 1908 | |
Neil Armstrong | ab555e0 | 2022-04-04 11:07:59 +0200 | [diff] [blame] | 1909 | mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_transform( |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1910 | const mbedtls_ssl_transform *transform ) |
| 1911 | { |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 1912 | mbedtls_ssl_mode_t base_mode = mbedtls_ssl_get_base_mode( |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1913 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 1914 | transform->psa_alg |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1915 | #else |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 1916 | mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) |
| 1917 | #endif |
| 1918 | ); |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1919 | |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 1920 | int encrypt_then_mac = 0; |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 1921 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 1922 | encrypt_then_mac = transform->encrypt_then_mac; |
| 1923 | #endif |
| 1924 | return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) ); |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1925 | } |
| 1926 | |
Neil Armstrong | ab555e0 | 2022-04-04 11:07:59 +0200 | [diff] [blame] | 1927 | mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_ciphersuite( |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 1928 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1929 | int encrypt_then_mac, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 1930 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1931 | const mbedtls_ssl_ciphersuite_t *suite ) |
| 1932 | { |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 1933 | mbedtls_ssl_mode_t base_mode = MBEDTLS_SSL_MODE_STREAM; |
| 1934 | |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1935 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1936 | psa_status_t status; |
| 1937 | psa_algorithm_t alg; |
| 1938 | psa_key_type_t type; |
| 1939 | size_t size; |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1940 | status = mbedtls_ssl_cipher_to_psa( suite->cipher, 0, &alg, &type, &size ); |
| 1941 | if( status == PSA_SUCCESS ) |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 1942 | base_mode = mbedtls_ssl_get_base_mode( alg ); |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1943 | #else |
| 1944 | const mbedtls_cipher_info_t *cipher = |
| 1945 | mbedtls_cipher_info_from_type( suite->cipher ); |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1946 | if( cipher != NULL ) |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 1947 | { |
| 1948 | base_mode = |
| 1949 | mbedtls_ssl_get_base_mode( |
| 1950 | mbedtls_cipher_info_get_mode( cipher ) ); |
| 1951 | } |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1952 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 1953 | |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 1954 | #if !defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
| 1955 | int encrypt_then_mac = 0; |
| 1956 | #endif |
| 1957 | return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) ); |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1958 | } |
| 1959 | |
Neil Armstrong | 8395d7a | 2022-05-18 11:44:56 +0200 | [diff] [blame] | 1960 | #if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 1961 | |
| 1962 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 1963 | /* Serialization of TLS 1.3 sessions: |
| 1964 | * |
| 1965 | * struct { |
Xiaokang Qian | 126bf8e | 2022-10-13 02:22:40 +0000 | [diff] [blame] | 1966 | * opaque hostname<0..2^16-1>; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 1967 | * uint64 ticket_received; |
| 1968 | * uint32 ticket_lifetime; |
Jerry Yu | 3419107 | 2022-08-18 10:32:09 +0800 | [diff] [blame] | 1969 | * opaque ticket<1..2^16-1>; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 1970 | * } ClientOnlyData; |
| 1971 | * |
| 1972 | * struct { |
| 1973 | * uint8 endpoint; |
| 1974 | * uint8 ciphersuite[2]; |
| 1975 | * uint32 ticket_age_add; |
| 1976 | * uint8 ticket_flags; |
| 1977 | * opaque resumption_key<0..255>; |
| 1978 | * select ( endpoint ) { |
| 1979 | * case client: ClientOnlyData; |
| 1980 | * case server: uint64 start_time; |
| 1981 | * }; |
| 1982 | * } serialized_session_tls13; |
| 1983 | * |
| 1984 | */ |
| 1985 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 1986 | MBEDTLS_CHECK_RETURN_CRITICAL |
| 1987 | static int ssl_tls13_session_save( const mbedtls_ssl_session *session, |
| 1988 | unsigned char *buf, |
| 1989 | size_t buf_len, |
| 1990 | size_t *olen ) |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 1991 | { |
| 1992 | unsigned char *p = buf; |
Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 1993 | #if defined(MBEDTLS_SSL_CLI_C) && \ |
| 1994 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 1995 | size_t hostname_len = ( session->hostname == NULL ) ? |
Xiaokang Qian | ed0620c | 2022-10-12 06:58:13 +0000 | [diff] [blame] | 1996 | 0 : strlen( session->hostname ) + 1; |
Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 1997 | #endif |
Jerry Yu | bc7c1a4 | 2022-07-21 22:57:37 +0800 | [diff] [blame] | 1998 | size_t needed = 1 /* endpoint */ |
| 1999 | + 2 /* ciphersuite */ |
| 2000 | + 4 /* ticket_age_add */ |
Jerry Yu | 3419107 | 2022-08-18 10:32:09 +0800 | [diff] [blame] | 2001 | + 1 /* ticket_flags */ |
| 2002 | + 1; /* resumption_key length */ |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 2003 | *olen = 0; |
Jerry Yu | 3419107 | 2022-08-18 10:32:09 +0800 | [diff] [blame] | 2004 | |
| 2005 | if( session->resumption_key_len > MBEDTLS_SSL_TLS1_3_TICKET_RESUMPTION_KEY_LEN ) |
| 2006 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2007 | needed += session->resumption_key_len; /* resumption_key */ |
| 2008 | |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2009 | #if defined(MBEDTLS_HAVE_TIME) |
| 2010 | needed += 8; /* start_time or ticket_received */ |
| 2011 | #endif |
| 2012 | |
| 2013 | #if defined(MBEDTLS_SSL_CLI_C) |
| 2014 | if( session->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 2015 | { |
Xiaokang Qian | 126bf8e | 2022-10-13 02:22:40 +0000 | [diff] [blame] | 2016 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Xiaokang Qian | bc663a0 | 2022-10-09 11:14:39 +0000 | [diff] [blame] | 2017 | needed += 2 /* hostname_len */ |
Xiaokang Qian | 2f9efd3 | 2022-10-10 11:24:08 +0000 | [diff] [blame] | 2018 | + hostname_len; /* hostname */ |
Xiaokang Qian | 281fd1b | 2022-09-20 11:35:41 +0000 | [diff] [blame] | 2019 | #endif |
| 2020 | |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2021 | needed += 4 /* ticket_lifetime */ |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 2022 | + 2; /* ticket_len */ |
Jerry Yu | 3419107 | 2022-08-18 10:32:09 +0800 | [diff] [blame] | 2023 | |
| 2024 | /* Check size_t overflow */ |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 2025 | if( session->ticket_len > SIZE_MAX - needed ) |
| 2026 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jerry Yu | 3419107 | 2022-08-18 10:32:09 +0800 | [diff] [blame] | 2027 | |
Jerry Yu | e28d974 | 2022-08-18 15:44:03 +0800 | [diff] [blame] | 2028 | needed += session->ticket_len; /* ticket */ |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2029 | } |
| 2030 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 2031 | |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 2032 | *olen = needed; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2033 | if( needed > buf_len ) |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 2034 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2035 | |
| 2036 | p[0] = session->endpoint; |
| 2037 | MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 1 ); |
| 2038 | MBEDTLS_PUT_UINT32_BE( session->ticket_age_add, p, 3 ); |
| 2039 | p[7] = session->ticket_flags; |
| 2040 | |
| 2041 | /* save resumption_key */ |
Jerry Yu | bc7c1a4 | 2022-07-21 22:57:37 +0800 | [diff] [blame] | 2042 | p[8] = session->resumption_key_len; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2043 | p += 9; |
Jerry Yu | bc7c1a4 | 2022-07-21 22:57:37 +0800 | [diff] [blame] | 2044 | memcpy( p, session->resumption_key, session->resumption_key_len ); |
| 2045 | p += session->resumption_key_len; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2046 | |
| 2047 | #if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C) |
| 2048 | if( session->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 2049 | { |
| 2050 | MBEDTLS_PUT_UINT64_BE( (uint64_t) session->start, p, 0 ); |
| 2051 | p += 8; |
| 2052 | } |
| 2053 | #endif /* MBEDTLS_HAVE_TIME */ |
| 2054 | |
| 2055 | #if defined(MBEDTLS_SSL_CLI_C) |
| 2056 | if( session->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 2057 | { |
Xiaokang Qian | ed0620c | 2022-10-12 06:58:13 +0000 | [diff] [blame] | 2058 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 2059 | MBEDTLS_PUT_UINT16_BE( hostname_len, p, 0 ); |
| 2060 | p += 2; |
Xiaokang Qian | 126bf8e | 2022-10-13 02:22:40 +0000 | [diff] [blame] | 2061 | if( hostname_len > 0 ) |
Xiaokang Qian | ed0620c | 2022-10-12 06:58:13 +0000 | [diff] [blame] | 2062 | { |
| 2063 | /* save host name */ |
| 2064 | memcpy( p, session->hostname, hostname_len ); |
| 2065 | p += hostname_len; |
| 2066 | } |
| 2067 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
| 2068 | |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2069 | #if defined(MBEDTLS_HAVE_TIME) |
| 2070 | MBEDTLS_PUT_UINT64_BE( (uint64_t) session->ticket_received, p, 0 ); |
| 2071 | p += 8; |
| 2072 | #endif |
| 2073 | MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 ); |
| 2074 | p += 4; |
| 2075 | |
| 2076 | MBEDTLS_PUT_UINT16_BE( session->ticket_len, p, 0 ); |
| 2077 | p += 2; |
Jerry Yu | 3419107 | 2022-08-18 10:32:09 +0800 | [diff] [blame] | 2078 | |
| 2079 | if( session->ticket != NULL && session->ticket_len > 0 ) |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2080 | { |
| 2081 | memcpy( p, session->ticket, session->ticket_len ); |
| 2082 | p += session->ticket_len; |
| 2083 | } |
| 2084 | } |
| 2085 | #endif /* MBEDTLS_SSL_CLI_C */ |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 2086 | return( 0 ); |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2087 | } |
| 2088 | |
| 2089 | MBEDTLS_CHECK_RETURN_CRITICAL |
| 2090 | static int ssl_tls13_session_load( mbedtls_ssl_session *session, |
| 2091 | const unsigned char *buf, |
| 2092 | size_t len ) |
| 2093 | { |
| 2094 | const unsigned char *p = buf; |
| 2095 | const unsigned char *end = buf + len; |
| 2096 | |
| 2097 | if( end - p < 9 ) |
| 2098 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2099 | session->endpoint = p[0]; |
| 2100 | session->ciphersuite = MBEDTLS_GET_UINT16_BE( p, 1 ); |
| 2101 | session->ticket_age_add = MBEDTLS_GET_UINT32_BE( p, 3 ); |
| 2102 | session->ticket_flags = p[7]; |
| 2103 | |
| 2104 | /* load resumption_key */ |
Jerry Yu | bc7c1a4 | 2022-07-21 22:57:37 +0800 | [diff] [blame] | 2105 | session->resumption_key_len = p[8]; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2106 | p += 9; |
| 2107 | |
Jerry Yu | bc7c1a4 | 2022-07-21 22:57:37 +0800 | [diff] [blame] | 2108 | if( end - p < session->resumption_key_len ) |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2109 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2110 | |
Jerry Yu | bc7c1a4 | 2022-07-21 22:57:37 +0800 | [diff] [blame] | 2111 | if( sizeof( session->resumption_key ) < session->resumption_key_len ) |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2112 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jerry Yu | bc7c1a4 | 2022-07-21 22:57:37 +0800 | [diff] [blame] | 2113 | memcpy( session->resumption_key, p, session->resumption_key_len ); |
| 2114 | p += session->resumption_key_len; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2115 | |
| 2116 | #if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C) |
| 2117 | if( session->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 2118 | { |
| 2119 | if( end - p < 8 ) |
| 2120 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2121 | session->start = MBEDTLS_GET_UINT64_BE( p, 0 ); |
| 2122 | p += 8; |
| 2123 | } |
| 2124 | #endif /* MBEDTLS_HAVE_TIME */ |
| 2125 | |
| 2126 | #if defined(MBEDTLS_SSL_CLI_C) |
| 2127 | if( session->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 2128 | { |
Xiaokang Qian | ed0620c | 2022-10-12 06:58:13 +0000 | [diff] [blame] | 2129 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \ |
| 2130 | defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 2131 | size_t hostname_len; |
| 2132 | /* load host name */ |
| 2133 | if( end - p < 2 ) |
| 2134 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2135 | hostname_len = MBEDTLS_GET_UINT16_BE( p, 0 ); |
| 2136 | p += 2; |
| 2137 | |
| 2138 | if( end - p < ( long int )hostname_len ) |
| 2139 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2140 | if( hostname_len > 0 ) |
| 2141 | { |
| 2142 | session->hostname = mbedtls_calloc( 1, hostname_len ); |
| 2143 | if( session->hostname == NULL ) |
| 2144 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 2145 | memcpy( session->hostname, p, hostname_len ); |
| 2146 | p += hostname_len; |
| 2147 | } |
| 2148 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION && |
| 2149 | MBEDTLS_SSL_SESSION_TICKETS */ |
| 2150 | |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2151 | #if defined(MBEDTLS_HAVE_TIME) |
| 2152 | if( end - p < 8 ) |
| 2153 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2154 | session->ticket_received = MBEDTLS_GET_UINT64_BE( p, 0 ); |
| 2155 | p += 8; |
| 2156 | #endif |
| 2157 | if( end - p < 4 ) |
| 2158 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2159 | session->ticket_lifetime = MBEDTLS_GET_UINT32_BE( p, 0 ); |
| 2160 | p += 4; |
| 2161 | |
| 2162 | if( end - p < 2 ) |
| 2163 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2164 | session->ticket_len = MBEDTLS_GET_UINT16_BE( p, 0 ); |
| 2165 | p += 2; |
| 2166 | |
| 2167 | if( end - p < ( long int )session->ticket_len ) |
| 2168 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2169 | if( session->ticket_len > 0 ) |
| 2170 | { |
| 2171 | session->ticket = mbedtls_calloc( 1, session->ticket_len ); |
| 2172 | if( session->ticket == NULL ) |
| 2173 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 2174 | memcpy( session->ticket, p, session->ticket_len ); |
| 2175 | p += session->ticket_len; |
| 2176 | } |
| 2177 | } |
| 2178 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 2179 | |
| 2180 | return( 0 ); |
| 2181 | |
| 2182 | } |
| 2183 | #else /* MBEDTLS_SSL_SESSION_TICKETS */ |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 2184 | MBEDTLS_CHECK_RETURN_CRITICAL |
| 2185 | static int ssl_tls13_session_save( const mbedtls_ssl_session *session, |
| 2186 | unsigned char *buf, |
| 2187 | size_t buf_len, |
| 2188 | size_t *olen ) |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 2189 | { |
| 2190 | ((void) session); |
| 2191 | ((void) buf); |
| 2192 | ((void) buf_len); |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 2193 | *olen = 0; |
| 2194 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 2195 | } |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2196 | |
| 2197 | static int ssl_tls13_session_load( const mbedtls_ssl_session *session, |
| 2198 | unsigned char *buf, |
| 2199 | size_t buf_len ) |
| 2200 | { |
| 2201 | ((void) session); |
| 2202 | ((void) buf); |
| 2203 | ((void) buf_len); |
| 2204 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 2205 | } |
| 2206 | #endif /* !MBEDTLS_SSL_SESSION_TICKETS */ |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 2207 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 2208 | |
Przemyslaw Stekiel | f57b456 | 2022-01-25 00:04:18 +0100 | [diff] [blame] | 2209 | 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] | 2210 | size_t taglen, |
| 2211 | psa_algorithm_t *alg, |
| 2212 | psa_key_type_t *key_type, |
| 2213 | size_t *key_size ) |
| 2214 | { |
| 2215 | switch ( mbedtls_cipher_type ) |
| 2216 | { |
| 2217 | case MBEDTLS_CIPHER_AES_128_CBC: |
| 2218 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 2219 | *key_type = PSA_KEY_TYPE_AES; |
| 2220 | *key_size = 128; |
| 2221 | break; |
| 2222 | case MBEDTLS_CIPHER_AES_128_CCM: |
| 2223 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 2224 | *key_type = PSA_KEY_TYPE_AES; |
| 2225 | *key_size = 128; |
| 2226 | break; |
| 2227 | case MBEDTLS_CIPHER_AES_128_GCM: |
| 2228 | *alg = PSA_ALG_GCM; |
| 2229 | *key_type = PSA_KEY_TYPE_AES; |
| 2230 | *key_size = 128; |
| 2231 | break; |
| 2232 | case MBEDTLS_CIPHER_AES_192_CCM: |
| 2233 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 2234 | *key_type = PSA_KEY_TYPE_AES; |
| 2235 | *key_size = 192; |
| 2236 | break; |
| 2237 | case MBEDTLS_CIPHER_AES_192_GCM: |
| 2238 | *alg = PSA_ALG_GCM; |
| 2239 | *key_type = PSA_KEY_TYPE_AES; |
| 2240 | *key_size = 192; |
| 2241 | break; |
| 2242 | case MBEDTLS_CIPHER_AES_256_CBC: |
| 2243 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 2244 | *key_type = PSA_KEY_TYPE_AES; |
| 2245 | *key_size = 256; |
| 2246 | break; |
| 2247 | case MBEDTLS_CIPHER_AES_256_CCM: |
| 2248 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 2249 | *key_type = PSA_KEY_TYPE_AES; |
| 2250 | *key_size = 256; |
| 2251 | break; |
| 2252 | case MBEDTLS_CIPHER_AES_256_GCM: |
| 2253 | *alg = PSA_ALG_GCM; |
| 2254 | *key_type = PSA_KEY_TYPE_AES; |
| 2255 | *key_size = 256; |
| 2256 | break; |
| 2257 | case MBEDTLS_CIPHER_ARIA_128_CBC: |
| 2258 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 2259 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2260 | *key_size = 128; |
| 2261 | break; |
| 2262 | case MBEDTLS_CIPHER_ARIA_128_CCM: |
| 2263 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 2264 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2265 | *key_size = 128; |
| 2266 | break; |
| 2267 | case MBEDTLS_CIPHER_ARIA_128_GCM: |
| 2268 | *alg = PSA_ALG_GCM; |
| 2269 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2270 | *key_size = 128; |
| 2271 | break; |
| 2272 | case MBEDTLS_CIPHER_ARIA_192_CCM: |
| 2273 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 2274 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2275 | *key_size = 192; |
| 2276 | break; |
| 2277 | case MBEDTLS_CIPHER_ARIA_192_GCM: |
| 2278 | *alg = PSA_ALG_GCM; |
| 2279 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2280 | *key_size = 192; |
| 2281 | break; |
| 2282 | case MBEDTLS_CIPHER_ARIA_256_CBC: |
| 2283 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 2284 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2285 | *key_size = 256; |
| 2286 | break; |
| 2287 | case MBEDTLS_CIPHER_ARIA_256_CCM: |
| 2288 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 2289 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2290 | *key_size = 256; |
| 2291 | break; |
| 2292 | case MBEDTLS_CIPHER_ARIA_256_GCM: |
| 2293 | *alg = PSA_ALG_GCM; |
| 2294 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2295 | *key_size = 256; |
| 2296 | break; |
| 2297 | case MBEDTLS_CIPHER_CAMELLIA_128_CBC: |
| 2298 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 2299 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2300 | *key_size = 128; |
| 2301 | break; |
| 2302 | case MBEDTLS_CIPHER_CAMELLIA_128_CCM: |
| 2303 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 2304 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2305 | *key_size = 128; |
| 2306 | break; |
| 2307 | case MBEDTLS_CIPHER_CAMELLIA_128_GCM: |
| 2308 | *alg = PSA_ALG_GCM; |
| 2309 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2310 | *key_size = 128; |
| 2311 | break; |
| 2312 | case MBEDTLS_CIPHER_CAMELLIA_192_CCM: |
| 2313 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 2314 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2315 | *key_size = 192; |
| 2316 | break; |
| 2317 | case MBEDTLS_CIPHER_CAMELLIA_192_GCM: |
| 2318 | *alg = PSA_ALG_GCM; |
| 2319 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2320 | *key_size = 192; |
| 2321 | break; |
| 2322 | case MBEDTLS_CIPHER_CAMELLIA_256_CBC: |
| 2323 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 2324 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2325 | *key_size = 256; |
| 2326 | break; |
| 2327 | case MBEDTLS_CIPHER_CAMELLIA_256_CCM: |
| 2328 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 2329 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2330 | *key_size = 256; |
| 2331 | break; |
| 2332 | case MBEDTLS_CIPHER_CAMELLIA_256_GCM: |
| 2333 | *alg = PSA_ALG_GCM; |
| 2334 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2335 | *key_size = 256; |
| 2336 | break; |
| 2337 | case MBEDTLS_CIPHER_CHACHA20_POLY1305: |
| 2338 | *alg = PSA_ALG_CHACHA20_POLY1305; |
| 2339 | *key_type = PSA_KEY_TYPE_CHACHA20; |
| 2340 | *key_size = 256; |
| 2341 | break; |
| 2342 | case MBEDTLS_CIPHER_NULL: |
| 2343 | *alg = MBEDTLS_SSL_NULL_CIPHER; |
| 2344 | *key_type = 0; |
| 2345 | *key_size = 0; |
| 2346 | break; |
| 2347 | default: |
| 2348 | return PSA_ERROR_NOT_SUPPORTED; |
| 2349 | } |
| 2350 | |
| 2351 | return PSA_SUCCESS; |
| 2352 | } |
Neil Armstrong | 8395d7a | 2022-05-18 11:44:56 +0200 | [diff] [blame] | 2353 | #endif /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2354 | |
Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 2355 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 2356 | int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf, |
| 2357 | const unsigned char *dhm_P, size_t P_len, |
| 2358 | const unsigned char *dhm_G, size_t G_len ) |
| 2359 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2360 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 2361 | |
Glenn Strauss | cee1129 | 2021-12-20 01:43:17 -0500 | [diff] [blame] | 2362 | mbedtls_mpi_free( &conf->dhm_P ); |
| 2363 | mbedtls_mpi_free( &conf->dhm_G ); |
| 2364 | |
Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 2365 | if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 || |
| 2366 | ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 ) |
| 2367 | { |
| 2368 | mbedtls_mpi_free( &conf->dhm_P ); |
| 2369 | mbedtls_mpi_free( &conf->dhm_G ); |
| 2370 | return( ret ); |
| 2371 | } |
| 2372 | |
| 2373 | return( 0 ); |
| 2374 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2375 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2376 | 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] | 2377 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2378 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 2379 | |
Glenn Strauss | cee1129 | 2021-12-20 01:43:17 -0500 | [diff] [blame] | 2380 | mbedtls_mpi_free( &conf->dhm_P ); |
| 2381 | mbedtls_mpi_free( &conf->dhm_G ); |
| 2382 | |
Gilles Peskine | e570248 | 2021-06-11 21:59:08 +0200 | [diff] [blame] | 2383 | if( ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_P, |
| 2384 | &conf->dhm_P ) ) != 0 || |
| 2385 | ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_G, |
| 2386 | &conf->dhm_G ) ) != 0 ) |
Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 2387 | { |
| 2388 | mbedtls_mpi_free( &conf->dhm_P ); |
| 2389 | mbedtls_mpi_free( &conf->dhm_G ); |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 2390 | return( ret ); |
Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 2391 | } |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 2392 | |
| 2393 | return( 0 ); |
| 2394 | } |
Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 2395 | #endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */ |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 2396 | |
Manuel Pégourié-Gonnard | bd990d6 | 2015-06-11 14:49:42 +0200 | [diff] [blame] | 2397 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C) |
| 2398 | /* |
| 2399 | * Set the minimum length for Diffie-Hellman parameters |
| 2400 | */ |
| 2401 | void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf, |
| 2402 | unsigned int bitlen ) |
| 2403 | { |
| 2404 | conf->dhm_min_bitlen = bitlen; |
| 2405 | } |
| 2406 | #endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */ |
| 2407 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 2408 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Jerry Yu | 7ddc38c | 2022-01-19 11:08:05 +0800 | [diff] [blame] | 2409 | #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] | 2410 | /* |
| 2411 | * Set allowed/preferred hashes for handshake signatures |
| 2412 | */ |
| 2413 | void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf, |
| 2414 | const int *hashes ) |
| 2415 | { |
| 2416 | conf->sig_hashes = hashes; |
| 2417 | } |
Jerry Yu | 7ddc38c | 2022-01-19 11:08:05 +0800 | [diff] [blame] | 2418 | #endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */ |
Hanno Becker | 1cd6e00 | 2021-08-10 13:27:10 +0100 | [diff] [blame] | 2419 | |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 2420 | /* Configure allowed signature algorithms for handshake */ |
Hanno Becker | 1cd6e00 | 2021-08-10 13:27:10 +0100 | [diff] [blame] | 2421 | void mbedtls_ssl_conf_sig_algs( mbedtls_ssl_config *conf, |
| 2422 | const uint16_t* sig_algs ) |
| 2423 | { |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 2424 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 2425 | conf->sig_hashes = NULL; |
| 2426 | #endif /* !MBEDTLS_DEPRECATED_REMOVED */ |
| 2427 | conf->sig_algs = sig_algs; |
Hanno Becker | 1cd6e00 | 2021-08-10 13:27:10 +0100 | [diff] [blame] | 2428 | } |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 2429 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Manuel Pégourié-Gonnard | 36a8b57 | 2015-06-17 12:43:26 +0200 | [diff] [blame] | 2430 | |
Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 2431 | #if defined(MBEDTLS_ECP_C) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 2432 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 2433 | /* |
| 2434 | * Set the allowed elliptic curves |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 2435 | * |
| 2436 | * mbedtls_ssl_setup() takes the provided list |
| 2437 | * and translates it to a list of IANA TLS group identifiers, |
| 2438 | * stored in ssl->handshake->group_list. |
| 2439 | * |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 2440 | */ |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2441 | void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2442 | const mbedtls_ecp_group_id *curve_list ) |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 2443 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2444 | conf->curve_list = curve_list; |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 2445 | conf->group_list = NULL; |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 2446 | } |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 2447 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2448 | #endif /* MBEDTLS_ECP_C */ |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 2449 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 2450 | /* |
| 2451 | * Set the allowed groups |
| 2452 | */ |
| 2453 | void mbedtls_ssl_conf_groups( mbedtls_ssl_config *conf, |
| 2454 | const uint16_t *group_list ) |
| 2455 | { |
| 2456 | #if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 2457 | conf->curve_list = NULL; |
| 2458 | #endif |
| 2459 | conf->group_list = group_list; |
| 2460 | } |
| 2461 | |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 2462 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2463 | int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2464 | { |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2465 | /* Initialize to suppress unnecessary compiler warning */ |
| 2466 | size_t hostname_len = 0; |
| 2467 | |
| 2468 | /* Check if new hostname is valid before |
| 2469 | * making any change to current one */ |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2470 | if( hostname != NULL ) |
| 2471 | { |
| 2472 | hostname_len = strlen( hostname ); |
| 2473 | |
| 2474 | if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN ) |
| 2475 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2476 | } |
| 2477 | |
| 2478 | /* Now it's clear that we will overwrite the old hostname, |
| 2479 | * so we can free it safely */ |
| 2480 | |
| 2481 | if( ssl->hostname != NULL ) |
| 2482 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 2483 | mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) ); |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2484 | mbedtls_free( ssl->hostname ); |
| 2485 | } |
| 2486 | |
| 2487 | /* Passing NULL as hostname shall clear the old one */ |
Manuel Pégourié-Gonnard | ba26c24 | 2015-05-06 10:47:06 +0100 | [diff] [blame] | 2488 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2489 | if( hostname == NULL ) |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2490 | { |
| 2491 | ssl->hostname = NULL; |
| 2492 | } |
| 2493 | else |
| 2494 | { |
| 2495 | ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 ); |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2496 | if( ssl->hostname == NULL ) |
| 2497 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Paul Bakker | 75c1a6f | 2013-08-19 14:25:29 +0200 | [diff] [blame] | 2498 | |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2499 | memcpy( ssl->hostname, hostname, hostname_len ); |
Paul Bakker | 75c1a6f | 2013-08-19 14:25:29 +0200 | [diff] [blame] | 2500 | |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2501 | ssl->hostname[hostname_len] = '\0'; |
| 2502 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2503 | |
| 2504 | return( 0 ); |
| 2505 | } |
Hanno Becker | 1a9a51c | 2017-04-07 13:02:16 +0100 | [diff] [blame] | 2506 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2507 | |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 2508 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2509 | void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2510 | int (*f_sni)(void *, mbedtls_ssl_context *, |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 2511 | const unsigned char *, size_t), |
| 2512 | void *p_sni ) |
| 2513 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2514 | conf->f_sni = f_sni; |
| 2515 | conf->p_sni = p_sni; |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 2516 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2517 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 2518 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2519 | #if defined(MBEDTLS_SSL_ALPN) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2520 | 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] | 2521 | { |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 2522 | size_t cur_len, tot_len; |
| 2523 | const char **p; |
| 2524 | |
| 2525 | /* |
Brian J Murray | 1903fb3 | 2016-11-06 04:45:15 -0800 | [diff] [blame] | 2526 | * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings |
| 2527 | * MUST NOT be truncated." |
| 2528 | * We check lengths now rather than later. |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 2529 | */ |
| 2530 | tot_len = 0; |
| 2531 | for( p = protos; *p != NULL; p++ ) |
| 2532 | { |
| 2533 | cur_len = strlen( *p ); |
| 2534 | tot_len += cur_len; |
| 2535 | |
Ronald Cron | 8216dd3 | 2020-04-23 16:41:44 +0200 | [diff] [blame] | 2536 | if( ( cur_len == 0 ) || |
| 2537 | ( cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN ) || |
| 2538 | ( tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN ) ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2539 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 2540 | } |
| 2541 | |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2542 | conf->alpn_list = protos; |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 2543 | |
| 2544 | return( 0 ); |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 2545 | } |
| 2546 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2547 | 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] | 2548 | { |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 2549 | return( ssl->alpn_chosen ); |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 2550 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2551 | #endif /* MBEDTLS_SSL_ALPN */ |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 2552 | |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2553 | #if defined(MBEDTLS_SSL_DTLS_SRTP) |
Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 2554 | void mbedtls_ssl_conf_srtp_mki_value_supported( mbedtls_ssl_config *conf, |
| 2555 | int support_mki_value ) |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 2556 | { |
| 2557 | conf->dtls_srtp_mki_support = support_mki_value; |
| 2558 | } |
| 2559 | |
Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 2560 | int mbedtls_ssl_dtls_srtp_set_mki_value( mbedtls_ssl_context *ssl, |
| 2561 | unsigned char *mki_value, |
Johan Pascal | f6417ec | 2020-09-22 15:15:19 +0200 | [diff] [blame] | 2562 | uint16_t mki_len ) |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 2563 | { |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 2564 | if( mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH ) |
Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 2565 | { |
Johan Pascal | d576fdb | 2020-09-22 10:39:53 +0200 | [diff] [blame] | 2566 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 2567 | } |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 2568 | |
| 2569 | 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] | 2570 | { |
Johan Pascal | d576fdb | 2020-09-22 10:39:53 +0200 | [diff] [blame] | 2571 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 2572 | } |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 2573 | |
| 2574 | memcpy( ssl->dtls_srtp_info.mki_value, mki_value, mki_len ); |
| 2575 | ssl->dtls_srtp_info.mki_len = mki_len; |
Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 2576 | return( 0 ); |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 2577 | } |
| 2578 | |
Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 2579 | int mbedtls_ssl_conf_dtls_srtp_protection_profiles( mbedtls_ssl_config *conf, |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 2580 | const mbedtls_ssl_srtp_profile *profiles ) |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2581 | { |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 2582 | const mbedtls_ssl_srtp_profile *p; |
| 2583 | size_t list_size = 0; |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2584 | |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 2585 | /* check the profiles list: all entry must be valid, |
| 2586 | * 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] | 2587 | for( p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET && |
| 2588 | list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH; |
| 2589 | p++ ) |
Johan Pascal | d576fdb | 2020-09-22 10:39:53 +0200 | [diff] [blame] | 2590 | { |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 2591 | if( mbedtls_ssl_check_srtp_profile_value( *p ) != MBEDTLS_TLS_SRTP_UNSET ) |
Johan Pascal | d576fdb | 2020-09-22 10:39:53 +0200 | [diff] [blame] | 2592 | { |
Johan Pascal | 76fdf1d | 2020-10-22 23:31:00 +0200 | [diff] [blame] | 2593 | list_size++; |
| 2594 | } |
| 2595 | else |
| 2596 | { |
| 2597 | /* unsupported value, stop parsing and set the size to an error value */ |
| 2598 | list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1; |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2599 | } |
| 2600 | } |
| 2601 | |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 2602 | if( list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH ) |
Johan Pascal | d387aa0 | 2020-09-23 18:47:56 +0200 | [diff] [blame] | 2603 | { |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 2604 | conf->dtls_srtp_profile_list = NULL; |
| 2605 | conf->dtls_srtp_profile_list_len = 0; |
| 2606 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2607 | } |
| 2608 | |
Johan Pascal | 9bc97ca | 2020-09-21 23:44:45 +0200 | [diff] [blame] | 2609 | conf->dtls_srtp_profile_list = profiles; |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 2610 | conf->dtls_srtp_profile_list_len = list_size; |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2611 | |
| 2612 | return( 0 ); |
| 2613 | } |
| 2614 | |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 2615 | void mbedtls_ssl_get_dtls_srtp_negotiation_result( const mbedtls_ssl_context *ssl, |
| 2616 | mbedtls_dtls_srtp_info *dtls_srtp_info ) |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2617 | { |
Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 2618 | dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile; |
| 2619 | /* do not copy the mki value if there is no chosen profile */ |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 2620 | if( dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET ) |
Johan Pascal | 0dbcd1d | 2020-10-28 11:03:07 +0100 | [diff] [blame] | 2621 | { |
Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 2622 | dtls_srtp_info->mki_len = 0; |
Johan Pascal | 0dbcd1d | 2020-10-28 11:03:07 +0100 | [diff] [blame] | 2623 | } |
Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 2624 | else |
| 2625 | { |
| 2626 | dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len; |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 2627 | memcpy( dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value, |
| 2628 | ssl->dtls_srtp_info.mki_len ); |
Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 2629 | } |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2630 | } |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2631 | #endif /* MBEDTLS_SSL_DTLS_SRTP */ |
| 2632 | |
Aditya Patwardhan | 3096f33 | 2022-07-26 14:31:46 +0530 | [diff] [blame] | 2633 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 2634 | 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] | 2635 | { |
Glenn Strauss | 2dfcea2 | 2022-03-14 17:26:42 -0400 | [diff] [blame] | 2636 | conf->max_tls_version = (major << 8) | minor; |
Paul Bakker | 490ecc8 | 2011-10-06 13:04:09 +0000 | [diff] [blame] | 2637 | } |
| 2638 | |
Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 2639 | 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] | 2640 | { |
Glenn Strauss | 2dfcea2 | 2022-03-14 17:26:42 -0400 | [diff] [blame] | 2641 | conf->min_tls_version = (major << 8) | minor; |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 2642 | } |
Aditya Patwardhan | 3096f33 | 2022-07-26 14:31:46 +0530 | [diff] [blame] | 2643 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 2644 | |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 2645 | #if defined(MBEDTLS_SSL_SRV_C) |
| 2646 | void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf, |
| 2647 | char cert_req_ca_list ) |
| 2648 | { |
| 2649 | conf->cert_req_ca_list = cert_req_ca_list; |
| 2650 | } |
| 2651 | #endif |
| 2652 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2653 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2654 | 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] | 2655 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2656 | conf->encrypt_then_mac = etm; |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 2657 | } |
| 2658 | #endif |
| 2659 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2660 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2661 | 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] | 2662 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2663 | conf->extended_ms = ems; |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 2664 | } |
| 2665 | #endif |
| 2666 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2667 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2668 | 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] | 2669 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2670 | if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID || |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 2671 | 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] | 2672 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2673 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 2674 | } |
| 2675 | |
Manuel Pégourié-Gonnard | 6bf89d6 | 2015-05-05 17:01:57 +0100 | [diff] [blame] | 2676 | conf->mfl_code = mfl_code; |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 2677 | |
| 2678 | return( 0 ); |
| 2679 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2680 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 2681 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2682 | 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] | 2683 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2684 | conf->allow_legacy_renegotiation = allow_legacy; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2685 | } |
| 2686 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2687 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2688 | 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] | 2689 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2690 | conf->disable_renegotiation = renegotiation; |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 2691 | } |
| 2692 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2693 | 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] | 2694 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2695 | conf->renego_max_records = max_records; |
Manuel Pégourié-Gonnard | a9964db | 2014-07-03 19:29:16 +0200 | [diff] [blame] | 2696 | } |
| 2697 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2698 | void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 837f0fe | 2014-11-05 13:58:53 +0100 | [diff] [blame] | 2699 | const unsigned char period[8] ) |
| 2700 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2701 | memcpy( conf->renego_period, period, 8 ); |
Manuel Pégourié-Gonnard | 837f0fe | 2014-11-05 13:58:53 +0100 | [diff] [blame] | 2702 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2703 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2704 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2705 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 2706 | #if defined(MBEDTLS_SSL_CLI_C) |
| 2707 | 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] | 2708 | { |
Manuel Pégourié-Gonnard | 2b49445 | 2015-05-06 10:05:11 +0100 | [diff] [blame] | 2709 | conf->session_tickets = use_tickets; |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 2710 | } |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 2711 | #endif |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 2712 | |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 2713 | #if defined(MBEDTLS_SSL_SRV_C) |
Jerry Yu | 1ad7ace | 2022-08-09 13:28:39 +0800 | [diff] [blame] | 2714 | |
Jerry Yu | d0766ec | 2022-09-22 10:46:57 +0800 | [diff] [blame] | 2715 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS) |
Jerry Yu | 1ad7ace | 2022-08-09 13:28:39 +0800 | [diff] [blame] | 2716 | void mbedtls_ssl_conf_new_session_tickets( mbedtls_ssl_config *conf, |
| 2717 | uint16_t num_tickets ) |
| 2718 | { |
Jerry Yu | d0766ec | 2022-09-22 10:46:57 +0800 | [diff] [blame] | 2719 | conf->new_session_tickets_count = num_tickets; |
Jerry Yu | 1ad7ace | 2022-08-09 13:28:39 +0800 | [diff] [blame] | 2720 | } |
| 2721 | #endif |
| 2722 | |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 2723 | void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf, |
| 2724 | mbedtls_ssl_ticket_write_t *f_ticket_write, |
| 2725 | mbedtls_ssl_ticket_parse_t *f_ticket_parse, |
| 2726 | void *p_ticket ) |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 2727 | { |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 2728 | conf->f_ticket_write = f_ticket_write; |
| 2729 | conf->f_ticket_parse = f_ticket_parse; |
| 2730 | conf->p_ticket = p_ticket; |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 2731 | } |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 2732 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2733 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 2734 | |
Hanno Becker | 7e6c178 | 2021-06-08 09:24:55 +0100 | [diff] [blame] | 2735 | void mbedtls_ssl_set_export_keys_cb( mbedtls_ssl_context *ssl, |
| 2736 | mbedtls_ssl_export_keys_t *f_export_keys, |
| 2737 | void *p_export_keys ) |
Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 2738 | { |
Hanno Becker | 7e6c178 | 2021-06-08 09:24:55 +0100 | [diff] [blame] | 2739 | ssl->f_export_keys = f_export_keys; |
| 2740 | ssl->p_export_keys = p_export_keys; |
Ron Eldor | f5cc10d | 2019-05-07 18:33:40 +0300 | [diff] [blame] | 2741 | } |
Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 2742 | |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 2743 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 2744 | void mbedtls_ssl_conf_async_private_cb( |
| 2745 | mbedtls_ssl_config *conf, |
| 2746 | mbedtls_ssl_async_sign_t *f_async_sign, |
| 2747 | mbedtls_ssl_async_decrypt_t *f_async_decrypt, |
| 2748 | mbedtls_ssl_async_resume_t *f_async_resume, |
| 2749 | mbedtls_ssl_async_cancel_t *f_async_cancel, |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 2750 | void *async_config_data ) |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 2751 | { |
| 2752 | conf->f_async_sign_start = f_async_sign; |
| 2753 | conf->f_async_decrypt_start = f_async_decrypt; |
| 2754 | conf->f_async_resume = f_async_resume; |
| 2755 | conf->f_async_cancel = f_async_cancel; |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 2756 | conf->p_async_config_data = async_config_data; |
| 2757 | } |
| 2758 | |
Gilles Peskine | 8f97af7 | 2018-04-26 11:46:10 +0200 | [diff] [blame] | 2759 | void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf ) |
| 2760 | { |
| 2761 | return( conf->p_async_config_data ); |
| 2762 | } |
| 2763 | |
Gilles Peskine | 1febfef | 2018-04-30 11:54:39 +0200 | [diff] [blame] | 2764 | void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl ) |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 2765 | { |
| 2766 | if( ssl->handshake == NULL ) |
| 2767 | return( NULL ); |
| 2768 | else |
| 2769 | return( ssl->handshake->user_async_ctx ); |
| 2770 | } |
| 2771 | |
Gilles Peskine | 1febfef | 2018-04-30 11:54:39 +0200 | [diff] [blame] | 2772 | void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl, |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 2773 | void *ctx ) |
| 2774 | { |
| 2775 | if( ssl->handshake != NULL ) |
| 2776 | ssl->handshake->user_async_ctx = ctx; |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 2777 | } |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 2778 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 2779 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2780 | /* |
| 2781 | * SSL get accessors |
| 2782 | */ |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 2783 | uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2784 | { |
Manuel Pégourié-Gonnard | e89163c | 2015-01-23 14:30:57 +0000 | [diff] [blame] | 2785 | if( ssl->session != NULL ) |
| 2786 | return( ssl->session->verify_result ); |
| 2787 | |
| 2788 | if( ssl->session_negotiate != NULL ) |
| 2789 | return( ssl->session_negotiate->verify_result ); |
| 2790 | |
Manuel Pégourié-Gonnard | 6ab9b00 | 2015-05-14 11:25:04 +0200 | [diff] [blame] | 2791 | return( 0xFFFFFFFF ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2792 | } |
| 2793 | |
Glenn Strauss | 8f52690 | 2022-01-13 00:04:49 -0500 | [diff] [blame] | 2794 | int mbedtls_ssl_get_ciphersuite_id_from_ssl( const mbedtls_ssl_context *ssl ) |
| 2795 | { |
| 2796 | if( ssl == NULL || ssl->session == NULL ) |
| 2797 | return( 0 ); |
| 2798 | |
| 2799 | return( ssl->session->ciphersuite ); |
| 2800 | } |
| 2801 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2802 | const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl ) |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 2803 | { |
Paul Bakker | 926c8e4 | 2013-03-06 10:23:34 +0100 | [diff] [blame] | 2804 | if( ssl == NULL || ssl->session == NULL ) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 2805 | return( NULL ); |
Paul Bakker | 926c8e4 | 2013-03-06 10:23:34 +0100 | [diff] [blame] | 2806 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2807 | return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite ); |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 2808 | } |
| 2809 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2810 | const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl ) |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 2811 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2812 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2813 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 2814 | { |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 2815 | switch( ssl->tls_version ) |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 2816 | { |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 2817 | case MBEDTLS_SSL_VERSION_TLS1_2: |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 2818 | return( "DTLSv1.2" ); |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 2819 | default: |
| 2820 | return( "unknown (DTLS)" ); |
| 2821 | } |
| 2822 | } |
| 2823 | #endif |
| 2824 | |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 2825 | switch( ssl->tls_version ) |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 2826 | { |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 2827 | case MBEDTLS_SSL_VERSION_TLS1_2: |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2828 | return( "TLSv1.2" ); |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 2829 | case MBEDTLS_SSL_VERSION_TLS1_3: |
Gilles Peskine | c63a1e0 | 2022-01-13 01:10:24 +0100 | [diff] [blame] | 2830 | return( "TLSv1.3" ); |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 2831 | default: |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 2832 | return( "unknown" ); |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 2833 | } |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 2834 | } |
| 2835 | |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 2836 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 2837 | size_t mbedtls_ssl_get_input_max_frag_len( const mbedtls_ssl_context *ssl ) |
| 2838 | { |
David Horstmann | 95d516f | 2021-05-04 18:36:56 +0100 | [diff] [blame] | 2839 | size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN; |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 2840 | size_t read_mfl; |
| 2841 | |
| 2842 | /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */ |
| 2843 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
| 2844 | ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE ) |
| 2845 | { |
| 2846 | return ssl_mfl_code_to_length( ssl->conf->mfl_code ); |
| 2847 | } |
| 2848 | |
| 2849 | /* Check if a smaller max length was negotiated */ |
| 2850 | if( ssl->session_out != NULL ) |
| 2851 | { |
| 2852 | read_mfl = ssl_mfl_code_to_length( ssl->session_out->mfl_code ); |
| 2853 | if( read_mfl < max_len ) |
| 2854 | { |
| 2855 | max_len = read_mfl; |
| 2856 | } |
| 2857 | } |
| 2858 | |
| 2859 | // During a handshake, use the value being negotiated |
| 2860 | if( ssl->session_negotiate != NULL ) |
| 2861 | { |
| 2862 | read_mfl = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ); |
| 2863 | if( read_mfl < max_len ) |
| 2864 | { |
| 2865 | max_len = read_mfl; |
| 2866 | } |
| 2867 | } |
| 2868 | |
| 2869 | return( max_len ); |
| 2870 | } |
| 2871 | |
| 2872 | 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] | 2873 | { |
| 2874 | size_t max_len; |
| 2875 | |
| 2876 | /* |
| 2877 | * Assume mfl_code is correct since it was checked when set |
| 2878 | */ |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 2879 | 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] | 2880 | |
Manuel Pégourié-Gonnard | 2cb17e2 | 2017-09-19 13:00:47 +0200 | [diff] [blame] | 2881 | /* Check if a smaller max length was negotiated */ |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 2882 | if( ssl->session_out != NULL && |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 2883 | 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] | 2884 | { |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 2885 | 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] | 2886 | } |
| 2887 | |
Manuel Pégourié-Gonnard | 2cb17e2 | 2017-09-19 13:00:47 +0200 | [diff] [blame] | 2888 | /* During a handshake, use the value being negotiated */ |
| 2889 | if( ssl->session_negotiate != NULL && |
| 2890 | ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len ) |
| 2891 | { |
| 2892 | max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ); |
| 2893 | } |
| 2894 | |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 2895 | return( max_len ); |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 2896 | } |
| 2897 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
| 2898 | |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 2899 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 8949071 | 2020-02-05 10:50:12 +0000 | [diff] [blame] | 2900 | 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] | 2901 | { |
Andrzej Kurek | ef43ce6 | 2018-10-09 08:24:12 -0400 | [diff] [blame] | 2902 | /* Return unlimited mtu for client hello messages to avoid fragmentation. */ |
| 2903 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
| 2904 | ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO || |
| 2905 | ssl->state == MBEDTLS_SSL_SERVER_HELLO ) ) |
| 2906 | return ( 0 ); |
| 2907 | |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 2908 | if( ssl->handshake == NULL || ssl->handshake->mtu == 0 ) |
| 2909 | return( ssl->mtu ); |
| 2910 | |
| 2911 | if( ssl->mtu == 0 ) |
| 2912 | return( ssl->handshake->mtu ); |
| 2913 | |
| 2914 | return( ssl->mtu < ssl->handshake->mtu ? |
| 2915 | ssl->mtu : ssl->handshake->mtu ); |
| 2916 | } |
| 2917 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 2918 | |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 2919 | int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl ) |
| 2920 | { |
| 2921 | size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN; |
| 2922 | |
Manuel Pégourié-Gonnard | 000281e | 2018-08-21 11:20:58 +0200 | [diff] [blame] | 2923 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \ |
| 2924 | !defined(MBEDTLS_SSL_PROTO_DTLS) |
| 2925 | (void) ssl; |
| 2926 | #endif |
| 2927 | |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 2928 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 2929 | 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] | 2930 | |
| 2931 | if( max_len > mfl ) |
| 2932 | max_len = mfl; |
| 2933 | #endif |
| 2934 | |
| 2935 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 8949071 | 2020-02-05 10:50:12 +0000 | [diff] [blame] | 2936 | if( mbedtls_ssl_get_current_mtu( ssl ) != 0 ) |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 2937 | { |
Hanno Becker | 8949071 | 2020-02-05 10:50:12 +0000 | [diff] [blame] | 2938 | const size_t mtu = mbedtls_ssl_get_current_mtu( ssl ); |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 2939 | const int ret = mbedtls_ssl_get_record_expansion( ssl ); |
| 2940 | const size_t overhead = (size_t) ret; |
| 2941 | |
| 2942 | if( ret < 0 ) |
| 2943 | return( ret ); |
| 2944 | |
| 2945 | if( mtu <= overhead ) |
| 2946 | { |
| 2947 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) ); |
| 2948 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 2949 | } |
| 2950 | |
| 2951 | if( max_len > mtu - overhead ) |
| 2952 | max_len = mtu - overhead; |
| 2953 | } |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 2954 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 2955 | |
Hanno Becker | 0defedb | 2018-08-10 12:35:02 +0100 | [diff] [blame] | 2956 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \ |
| 2957 | !defined(MBEDTLS_SSL_PROTO_DTLS) |
| 2958 | ((void) ssl); |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 2959 | #endif |
| 2960 | |
| 2961 | return( (int) max_len ); |
| 2962 | } |
| 2963 | |
Hanno Becker | 2d8e99b | 2021-04-21 06:19:50 +0100 | [diff] [blame] | 2964 | int mbedtls_ssl_get_max_in_record_payload( const mbedtls_ssl_context *ssl ) |
| 2965 | { |
| 2966 | size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN; |
| 2967 | |
| 2968 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 2969 | (void) ssl; |
| 2970 | #endif |
| 2971 | |
| 2972 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 2973 | const size_t mfl = mbedtls_ssl_get_input_max_frag_len( ssl ); |
| 2974 | |
| 2975 | if( max_len > mfl ) |
| 2976 | max_len = mfl; |
| 2977 | #endif |
| 2978 | |
| 2979 | return( (int) max_len ); |
| 2980 | } |
| 2981 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2982 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 2983 | 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] | 2984 | { |
| 2985 | if( ssl == NULL || ssl->session == NULL ) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 2986 | return( NULL ); |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 2987 | |
Hanno Becker | e682457 | 2019-02-07 13:18:46 +0000 | [diff] [blame] | 2988 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 2989 | return( ssl->session->peer_cert ); |
Hanno Becker | e682457 | 2019-02-07 13:18:46 +0000 | [diff] [blame] | 2990 | #else |
| 2991 | return( NULL ); |
| 2992 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 2993 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2994 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 2995 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2996 | #if defined(MBEDTLS_SSL_CLI_C) |
Hanno Becker | f852b1c | 2019-02-05 11:42:30 +0000 | [diff] [blame] | 2997 | int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, |
| 2998 | mbedtls_ssl_session *dst ) |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 2999 | { |
Hanno Becker | e810bbc | 2021-05-14 16:01:05 +0100 | [diff] [blame] | 3000 | int ret; |
| 3001 | |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 3002 | if( ssl == NULL || |
| 3003 | dst == NULL || |
| 3004 | ssl->session == NULL || |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3005 | ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT ) |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 3006 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3007 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 3008 | } |
| 3009 | |
Hanno Becker | e810bbc | 2021-05-14 16:01:05 +0100 | [diff] [blame] | 3010 | /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer |
| 3011 | * idempotent: Each session can only be exported once. |
| 3012 | * |
| 3013 | * (This is in preparation for TLS 1.3 support where we will |
| 3014 | * need the ability to export multiple sessions (aka tickets), |
| 3015 | * which will be achieved by calling mbedtls_ssl_get_session() |
| 3016 | * multiple times until it fails.) |
| 3017 | * |
| 3018 | * Check whether we have already exported the current session, |
| 3019 | * and fail if so. |
| 3020 | */ |
| 3021 | if( ssl->session->exported == 1 ) |
| 3022 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 3023 | |
| 3024 | ret = mbedtls_ssl_session_copy( dst, ssl->session ); |
| 3025 | if( ret != 0 ) |
| 3026 | return( ret ); |
| 3027 | |
| 3028 | /* Remember that we've exported the session. */ |
| 3029 | ssl->session->exported = 1; |
| 3030 | return( 0 ); |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 3031 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3032 | #endif /* MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 3033 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3034 | /* |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 3035 | * Define ticket header determining Mbed TLS version |
| 3036 | * and structure of the ticket. |
| 3037 | */ |
| 3038 | |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3039 | /* |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 3040 | * Define bitflag determining compile-time settings influencing |
| 3041 | * structure of serialized SSL sessions. |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3042 | */ |
| 3043 | |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 3044 | #if defined(MBEDTLS_HAVE_TIME) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3045 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME 1 |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 3046 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3047 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3048 | #endif /* MBEDTLS_HAVE_TIME */ |
| 3049 | |
| 3050 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3051 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3052 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3053 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3054 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 3055 | |
| 3056 | #if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3057 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3058 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3059 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3060 | #endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */ |
| 3061 | |
| 3062 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3063 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3064 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3065 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3066 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
| 3067 | |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3068 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3069 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3070 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3071 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3072 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ |
| 3073 | |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3074 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 3075 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1 |
| 3076 | #else |
| 3077 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0 |
| 3078 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
| 3079 | |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3080 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0 |
| 3081 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1 |
| 3082 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2 |
| 3083 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3 |
Hanno Becker | 37bdbe6 | 2021-08-01 05:38:58 +0100 | [diff] [blame] | 3084 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4 |
| 3085 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5 |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3086 | |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 3087 | #define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \ |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3088 | ( (uint16_t) ( \ |
| 3089 | ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \ |
| 3090 | ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \ |
| 3091 | ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \ |
| 3092 | ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \ |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3093 | ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \ |
Hanno Becker | be34e8e | 2019-06-04 09:43:16 +0100 | [diff] [blame] | 3094 | ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) ) ) |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3095 | |
Hanno Becker | f878707 | 2019-05-16 12:41:07 +0100 | [diff] [blame] | 3096 | static unsigned char ssl_serialized_session_header[] = { |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3097 | MBEDTLS_VERSION_MAJOR, |
| 3098 | MBEDTLS_VERSION_MINOR, |
| 3099 | MBEDTLS_VERSION_PATCH, |
Joe Subbiani | 2194dc4 | 2021-07-14 12:31:31 +0100 | [diff] [blame] | 3100 | MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ), |
| 3101 | MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ), |
Hanno Becker | f878707 | 2019-05-16 12:41:07 +0100 | [diff] [blame] | 3102 | }; |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 3103 | |
| 3104 | /* |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 3105 | * Serialize a session in the following format: |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 3106 | * (in the presentation language of TLS, RFC 8446 section 3) |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 3107 | * |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3108 | * struct { |
Hanno Becker | dc28b6c | 2019-05-29 11:08:00 +0100 | [diff] [blame] | 3109 | * |
Hanno Becker | dce5097 | 2021-08-01 05:39:23 +0100 | [diff] [blame] | 3110 | * opaque mbedtls_version[3]; // library version: major, minor, patch |
| 3111 | * opaque session_format[2]; // library-version specific 16-bit field |
| 3112 | * // determining the format of the remaining |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3113 | * // serialized data. |
Hanno Becker | dc28b6c | 2019-05-29 11:08:00 +0100 | [diff] [blame] | 3114 | * |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3115 | * Note: When updating the format, remember to keep |
| 3116 | * these version+format bytes. |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 3117 | * |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3118 | * // In this version, `session_format` determines |
| 3119 | * // the setting of those compile-time |
| 3120 | * // configuration options which influence |
| 3121 | * // the structure of mbedtls_ssl_session. |
| 3122 | * |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 3123 | * uint8_t minor_ver; // Protocol minor version. Possible values: |
| 3124 | * // - TLS 1.2 (3) |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3125 | * |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 3126 | * select (serialized_session.tls_version) { |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3127 | * |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 3128 | * case MBEDTLS_SSL_VERSION_TLS1_2: |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3129 | * serialized_session_tls12 data; |
| 3130 | * |
| 3131 | * }; |
| 3132 | * |
| 3133 | * } serialized_session; |
| 3134 | * |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 3135 | */ |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3136 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 3137 | MBEDTLS_CHECK_RETURN_CRITICAL |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3138 | static int ssl_session_save( const mbedtls_ssl_session *session, |
| 3139 | unsigned char omit_header, |
| 3140 | unsigned char *buf, |
| 3141 | size_t buf_len, |
| 3142 | size_t *olen ) |
| 3143 | { |
| 3144 | unsigned char *p = buf; |
| 3145 | size_t used = 0; |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 3146 | size_t remaining_len; |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 3147 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 3148 | size_t out_len; |
| 3149 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 3150 | #endif |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 3151 | if( session == NULL ) |
| 3152 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 3153 | |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3154 | if( !omit_header ) |
| 3155 | { |
| 3156 | /* |
| 3157 | * Add Mbed TLS version identifier |
| 3158 | */ |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3159 | used += sizeof( ssl_serialized_session_header ); |
| 3160 | |
| 3161 | if( used <= buf_len ) |
| 3162 | { |
| 3163 | memcpy( p, ssl_serialized_session_header, |
| 3164 | sizeof( ssl_serialized_session_header ) ); |
| 3165 | p += sizeof( ssl_serialized_session_header ); |
| 3166 | } |
| 3167 | } |
| 3168 | |
| 3169 | /* |
| 3170 | * TLS version identifier |
| 3171 | */ |
| 3172 | used += 1; |
| 3173 | if( used <= buf_len ) |
| 3174 | { |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 3175 | *p++ = MBEDTLS_BYTE_0( session->tls_version ); |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3176 | } |
| 3177 | |
| 3178 | /* Forward to version-specific serialization routine. */ |
Jerry Yu | fca4d57 | 2022-07-21 10:37:48 +0800 | [diff] [blame] | 3179 | remaining_len = (buf_len >= used) ? buf_len - used : 0; |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 3180 | switch( session->tls_version ) |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3181 | { |
| 3182 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 3183 | case MBEDTLS_SSL_VERSION_TLS1_2: |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 3184 | used += ssl_tls12_session_save( session, p, remaining_len ); |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3185 | break; |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3186 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 3187 | |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 3188 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 3189 | case MBEDTLS_SSL_VERSION_TLS1_3: |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 3190 | ret = ssl_tls13_session_save( session, p, remaining_len, &out_len ); |
| 3191 | if( ret != 0 && ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ) |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 3192 | return( ret ); |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 3193 | used += out_len; |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 3194 | break; |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 3195 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 3196 | |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3197 | default: |
| 3198 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 3199 | } |
| 3200 | |
| 3201 | *olen = used; |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 3202 | if( used > buf_len ) |
| 3203 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 3204 | |
| 3205 | return( 0 ); |
| 3206 | } |
| 3207 | |
| 3208 | /* |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 3209 | * Public wrapper for ssl_session_save() |
| 3210 | */ |
| 3211 | int mbedtls_ssl_session_save( const mbedtls_ssl_session *session, |
| 3212 | unsigned char *buf, |
| 3213 | size_t buf_len, |
| 3214 | size_t *olen ) |
| 3215 | { |
| 3216 | return( ssl_session_save( session, 0, buf, buf_len, olen ) ); |
| 3217 | } |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 3218 | |
Jerry Yu | f1b23ca | 2022-02-18 11:48:47 +0800 | [diff] [blame] | 3219 | /* |
| 3220 | * Deserialize session, see mbedtls_ssl_session_save() for format. |
| 3221 | * |
| 3222 | * This internal version is wrapped by a public function that cleans up in |
| 3223 | * case of error, and has an extra option omit_header. |
| 3224 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 3225 | MBEDTLS_CHECK_RETURN_CRITICAL |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3226 | static int ssl_session_load( mbedtls_ssl_session *session, |
| 3227 | unsigned char omit_header, |
| 3228 | const unsigned char *buf, |
| 3229 | size_t len ) |
| 3230 | { |
| 3231 | const unsigned char *p = buf; |
| 3232 | const unsigned char * const end = buf + len; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 3233 | size_t remaining_len; |
| 3234 | |
| 3235 | |
| 3236 | if( session == NULL ) |
| 3237 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3238 | |
| 3239 | if( !omit_header ) |
| 3240 | { |
| 3241 | /* |
| 3242 | * Check Mbed TLS version identifier |
| 3243 | */ |
| 3244 | |
| 3245 | if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) ) |
| 3246 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3247 | |
| 3248 | if( memcmp( p, ssl_serialized_session_header, |
| 3249 | sizeof( ssl_serialized_session_header ) ) != 0 ) |
| 3250 | { |
| 3251 | return( MBEDTLS_ERR_SSL_VERSION_MISMATCH ); |
| 3252 | } |
| 3253 | p += sizeof( ssl_serialized_session_header ); |
| 3254 | } |
| 3255 | |
| 3256 | /* |
| 3257 | * TLS version identifier |
| 3258 | */ |
| 3259 | if( 1 > (size_t)( end - p ) ) |
| 3260 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 3261 | session->tls_version = 0x0300 | *p++; |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3262 | |
| 3263 | /* Dispatch according to TLS version. */ |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 3264 | remaining_len = ( end - p ); |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 3265 | switch( session->tls_version ) |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3266 | { |
| 3267 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 3268 | case MBEDTLS_SSL_VERSION_TLS1_2: |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 3269 | return( ssl_tls12_session_load( session, p, remaining_len ) ); |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3270 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 3271 | |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 3272 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 3273 | case MBEDTLS_SSL_VERSION_TLS1_3: |
| 3274 | return( ssl_tls13_session_load( session, p, remaining_len ) ); |
| 3275 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 3276 | |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3277 | default: |
| 3278 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3279 | } |
| 3280 | } |
| 3281 | |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 3282 | /* |
Manuel Pégourié-Gonnard | b9dfc9f | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 3283 | * Deserialize session: public wrapper for error cleaning |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 3284 | */ |
| 3285 | int mbedtls_ssl_session_load( mbedtls_ssl_session *session, |
| 3286 | const unsigned char *buf, |
| 3287 | size_t len ) |
| 3288 | { |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 3289 | int ret = ssl_session_load( session, 0, buf, len ); |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 3290 | |
| 3291 | if( ret != 0 ) |
| 3292 | mbedtls_ssl_session_free( session ); |
| 3293 | |
| 3294 | return( ret ); |
| 3295 | } |
| 3296 | |
| 3297 | /* |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3298 | * Perform a single step of the SSL handshake |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3299 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 3300 | MBEDTLS_CHECK_RETURN_CRITICAL |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 3301 | static int ssl_prepare_handshake_step( mbedtls_ssl_context *ssl ) |
| 3302 | { |
| 3303 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 3304 | |
Ronald Cron | 66dbf91 | 2022-02-02 15:33:46 +0100 | [diff] [blame] | 3305 | /* |
| 3306 | * 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] | 3307 | * that were written into the output buffer by the previous handshake step, |
| 3308 | * if the write to the network callback returned with the |
Ronald Cron | 66dbf91 | 2022-02-02 15:33:46 +0100 | [diff] [blame] | 3309 | * #MBEDTLS_ERR_SSL_WANT_WRITE error code. |
| 3310 | * We proceed to the next handshake step only when all data from the |
| 3311 | * previous one have been sent to the peer, thus we make sure that this is |
| 3312 | * the case here by calling `mbedtls_ssl_flush_output()`. The function may |
| 3313 | * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case |
| 3314 | * we have to wait before to go ahead. |
| 3315 | * In the case of TLS 1.3, handshake step handlers do not send data to the |
| 3316 | * peer. Data are only sent here and through |
| 3317 | * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an |
Andrzej Kurek | 5c65c57 | 2022-04-13 14:28:52 -0400 | [diff] [blame] | 3318 | * alert occurred. |
Ronald Cron | 66dbf91 | 2022-02-02 15:33:46 +0100 | [diff] [blame] | 3319 | */ |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 3320 | if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) |
| 3321 | return( ret ); |
| 3322 | |
| 3323 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3324 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 3325 | ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING ) |
| 3326 | { |
| 3327 | if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 ) |
| 3328 | return( ret ); |
| 3329 | } |
| 3330 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 3331 | |
| 3332 | return( ret ); |
| 3333 | } |
| 3334 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3335 | int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3336 | { |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 3337 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3338 | |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 3339 | if( ssl == NULL || |
| 3340 | ssl->conf == NULL || |
| 3341 | ssl->handshake == NULL || |
Paul Elliott | 27b0d94 | 2022-03-18 21:55:32 +0000 | [diff] [blame] | 3342 | mbedtls_ssl_is_handshake_over( ssl ) == 1 ) |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 3343 | { |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 3344 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 3345 | } |
| 3346 | |
| 3347 | ret = ssl_prepare_handshake_step( ssl ); |
| 3348 | if( ret != 0 ) |
| 3349 | return( ret ); |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 3350 | |
Jerry Yu | e704781 | 2021-09-13 19:26:39 +0800 | [diff] [blame] | 3351 | ret = mbedtls_ssl_handle_pending_alert( ssl ); |
| 3352 | if( ret != 0 ) |
| 3353 | goto cleanup; |
| 3354 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3355 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3356 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 3357 | { |
Ronald Cron | 27c85e7 | 2022-03-08 11:37:55 +0100 | [diff] [blame] | 3358 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "client state: %s", |
| 3359 | mbedtls_ssl_states_str( ssl->state ) ) ); |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 3360 | |
Ronald Cron | 9f0fba3 | 2022-02-10 16:45:15 +0100 | [diff] [blame] | 3361 | switch( ssl->state ) |
| 3362 | { |
| 3363 | case MBEDTLS_SSL_HELLO_REQUEST: |
| 3364 | ssl->state = MBEDTLS_SSL_CLIENT_HELLO; |
| 3365 | break; |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 3366 | |
Ronald Cron | 9f0fba3 | 2022-02-10 16:45:15 +0100 | [diff] [blame] | 3367 | case MBEDTLS_SSL_CLIENT_HELLO: |
| 3368 | ret = mbedtls_ssl_write_client_hello( ssl ); |
| 3369 | break; |
| 3370 | |
| 3371 | default: |
| 3372 | #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] | 3373 | if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 ) |
Ronald Cron | 9f0fba3 | 2022-02-10 16:45:15 +0100 | [diff] [blame] | 3374 | ret = mbedtls_ssl_tls13_handshake_client_step( ssl ); |
| 3375 | else |
| 3376 | ret = mbedtls_ssl_handshake_client_step( ssl ); |
| 3377 | #elif defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 3378 | ret = mbedtls_ssl_handshake_client_step( ssl ); |
| 3379 | #else |
| 3380 | ret = mbedtls_ssl_tls13_handshake_client_step( ssl ); |
| 3381 | #endif |
| 3382 | } |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 3383 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3384 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3385 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3386 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 3387 | { |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3388 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 3389 | if( mbedtls_ssl_conf_is_tls13_only( ssl->conf ) ) |
Jerry Yu | 2756193 | 2021-08-27 17:07:38 +0800 | [diff] [blame] | 3390 | ret = mbedtls_ssl_tls13_handshake_server_step( ssl ); |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3391 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 3392 | |
| 3393 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 3394 | if( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) ) |
| 3395 | ret = mbedtls_ssl_handshake_server_step( ssl ); |
| 3396 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 3397 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3398 | #endif |
| 3399 | |
Jerry Yu | e704781 | 2021-09-13 19:26:39 +0800 | [diff] [blame] | 3400 | if( ret != 0 ) |
| 3401 | { |
Jerry Yu | bbd5a3f | 2021-09-18 20:50:22 +0800 | [diff] [blame] | 3402 | /* handshake_step return error. And it is same |
| 3403 | * with alert_reason. |
| 3404 | */ |
Jerry Yu | 3bf1f97 | 2021-09-22 21:37:18 +0800 | [diff] [blame] | 3405 | if( ssl->send_alert ) |
Jerry Yu | e704781 | 2021-09-13 19:26:39 +0800 | [diff] [blame] | 3406 | { |
Jerry Yu | 3bf1f97 | 2021-09-22 21:37:18 +0800 | [diff] [blame] | 3407 | ret = mbedtls_ssl_handle_pending_alert( ssl ); |
Jerry Yu | e704781 | 2021-09-13 19:26:39 +0800 | [diff] [blame] | 3408 | goto cleanup; |
| 3409 | } |
| 3410 | } |
| 3411 | |
| 3412 | cleanup: |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3413 | return( ret ); |
| 3414 | } |
| 3415 | |
| 3416 | /* |
| 3417 | * Perform the SSL handshake |
| 3418 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3419 | int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl ) |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3420 | { |
| 3421 | int ret = 0; |
| 3422 | |
Hanno Becker | a817ea4 | 2020-10-20 15:20:23 +0100 | [diff] [blame] | 3423 | /* Sanity checks */ |
| 3424 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 3425 | if( ssl == NULL || ssl->conf == NULL ) |
| 3426 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3427 | |
Hanno Becker | a817ea4 | 2020-10-20 15:20:23 +0100 | [diff] [blame] | 3428 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3429 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 3430 | ( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL ) ) |
| 3431 | { |
| 3432 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use " |
| 3433 | "mbedtls_ssl_set_timer_cb() for DTLS" ) ); |
| 3434 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3435 | } |
| 3436 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 3437 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3438 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3439 | |
Hanno Becker | a817ea4 | 2020-10-20 15:20:23 +0100 | [diff] [blame] | 3440 | /* Main handshake loop */ |
Paul Elliott | 27b0d94 | 2022-03-18 21:55:32 +0000 | [diff] [blame] | 3441 | while( mbedtls_ssl_is_handshake_over( ssl ) == 0 ) |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3442 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3443 | ret = mbedtls_ssl_handshake_step( ssl ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3444 | |
| 3445 | if( ret != 0 ) |
| 3446 | break; |
| 3447 | } |
| 3448 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3449 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3450 | |
| 3451 | return( ret ); |
| 3452 | } |
| 3453 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3454 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 3455 | #if defined(MBEDTLS_SSL_SRV_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3456 | /* |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3457 | * Write HelloRequest to request renegotiation on server |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3458 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 3459 | MBEDTLS_CHECK_RETURN_CRITICAL |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3460 | static int ssl_write_hello_request( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3461 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3462 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3463 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3464 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) ); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3465 | |
| 3466 | ssl->out_msglen = 4; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3467 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 3468 | ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST; |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3469 | |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3470 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3471 | { |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3472 | 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] | 3473 | return( ret ); |
| 3474 | } |
| 3475 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3476 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) ); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3477 | |
| 3478 | return( 0 ); |
| 3479 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3480 | #endif /* MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3481 | |
| 3482 | /* |
| 3483 | * Actually renegotiate current connection, triggered by either: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3484 | * - any side: calling mbedtls_ssl_renegotiate(), |
| 3485 | * - client: receiving a HelloRequest during mbedtls_ssl_read(), |
| 3486 | * - 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] | 3487 | * the initial handshake is completed. |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3488 | * 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] | 3489 | * 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] | 3490 | */ |
Hanno Becker | 40cdaa1 | 2020-02-05 10:48:27 +0000 | [diff] [blame] | 3491 | int mbedtls_ssl_start_renegotiation( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3492 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3493 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3494 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3495 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3496 | |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3497 | if( ( ret = ssl_handshake_init( ssl ) ) != 0 ) |
| 3498 | return( ret ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3499 | |
Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 3500 | /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and |
| 3501 | * the ServerHello will have message_seq = 1" */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3502 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3503 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3504 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ) |
Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 3505 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3506 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 3507 | ssl->handshake->out_msg_seq = 1; |
| 3508 | else |
| 3509 | ssl->handshake->in_msg_seq = 1; |
Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 3510 | } |
| 3511 | #endif |
| 3512 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3513 | ssl->state = MBEDTLS_SSL_HELLO_REQUEST; |
| 3514 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3515 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3516 | if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3517 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3518 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3519 | return( ret ); |
| 3520 | } |
| 3521 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3522 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3523 | |
| 3524 | return( 0 ); |
| 3525 | } |
| 3526 | |
| 3527 | /* |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3528 | * Renegotiate current connection on client, |
| 3529 | * or request renegotiation on server |
| 3530 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3531 | int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3532 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3533 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3534 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 3535 | if( ssl == NULL || ssl->conf == NULL ) |
| 3536 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3537 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3538 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3539 | /* On server, just send the request */ |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3540 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3541 | { |
Paul Elliott | 27b0d94 | 2022-03-18 21:55:32 +0000 | [diff] [blame] | 3542 | if( mbedtls_ssl_is_handshake_over( ssl ) == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3543 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3544 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3545 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING; |
Manuel Pégourié-Gonnard | f07f421 | 2014-08-15 19:04:47 +0200 | [diff] [blame] | 3546 | |
| 3547 | /* Did we already try/start sending HelloRequest? */ |
| 3548 | if( ssl->out_left != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3549 | return( mbedtls_ssl_flush_output( ssl ) ); |
Manuel Pégourié-Gonnard | f07f421 | 2014-08-15 19:04:47 +0200 | [diff] [blame] | 3550 | |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3551 | return( ssl_write_hello_request( ssl ) ); |
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 | #endif /* MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3554 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3555 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3556 | /* |
| 3557 | * On client, either start the renegotiation process or, |
| 3558 | * if already in progress, continue the handshake |
| 3559 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3560 | if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3561 | { |
Paul Elliott | 27b0d94 | 2022-03-18 21:55:32 +0000 | [diff] [blame] | 3562 | if( mbedtls_ssl_is_handshake_over( ssl ) == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3563 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3564 | |
Hanno Becker | 40cdaa1 | 2020-02-05 10:48:27 +0000 | [diff] [blame] | 3565 | if( ( ret = mbedtls_ssl_start_renegotiation( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3566 | { |
Hanno Becker | 40cdaa1 | 2020-02-05 10:48:27 +0000 | [diff] [blame] | 3567 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_start_renegotiation", ret ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3568 | return( ret ); |
| 3569 | } |
| 3570 | } |
| 3571 | else |
| 3572 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3573 | if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3574 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3575 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3576 | return( ret ); |
| 3577 | } |
| 3578 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3579 | #endif /* MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3580 | |
Paul Bakker | 37ce0ff | 2013-10-31 14:32:04 +0100 | [diff] [blame] | 3581 | return( ret ); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3582 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3583 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3584 | |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 3585 | void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3586 | { |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 3587 | mbedtls_ssl_handshake_params *handshake = ssl->handshake; |
| 3588 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3589 | if( handshake == NULL ) |
| 3590 | return; |
| 3591 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 3592 | #if defined(MBEDTLS_ECP_C) |
| 3593 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 3594 | if ( ssl->handshake->group_list_heap_allocated ) |
| 3595 | mbedtls_free( (void*) handshake->group_list ); |
| 3596 | handshake->group_list = NULL; |
| 3597 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
| 3598 | #endif /* MBEDTLS_ECP_C */ |
| 3599 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 3600 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 3601 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 3602 | if ( ssl->handshake->sig_algs_heap_allocated ) |
| 3603 | mbedtls_free( (void*) handshake->sig_algs ); |
| 3604 | handshake->sig_algs = NULL; |
| 3605 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
Xiaofei Bai | c234ecf | 2022-02-08 09:59:23 +0000 | [diff] [blame] | 3606 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 3607 | if( ssl->handshake->certificate_request_context ) |
| 3608 | { |
| 3609 | mbedtls_free( (void*) handshake->certificate_request_context ); |
| 3610 | } |
| 3611 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 3612 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 3613 | |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3614 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
| 3615 | if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 ) |
| 3616 | { |
Gilles Peskine | 8f97af7 | 2018-04-26 11:46:10 +0200 | [diff] [blame] | 3617 | ssl->conf->f_async_cancel( ssl ); |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3618 | handshake->async_in_progress = 0; |
| 3619 | } |
| 3620 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
| 3621 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 3622 | #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] | 3623 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 3624 | psa_hash_abort( &handshake->fin_sha256_psa ); |
| 3625 | #else |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 3626 | mbedtls_sha256_free( &handshake->fin_sha256 ); |
| 3627 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3628 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 3629 | #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] | 3630 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 3631 | psa_hash_abort( &handshake->fin_sha384_psa ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3632 | #else |
Andrzej Kurek | a242e83 | 2022-08-11 10:03:14 -0400 | [diff] [blame] | 3633 | mbedtls_sha512_free( &handshake->fin_sha384 ); |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 3634 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3635 | #endif |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 3636 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3637 | #if defined(MBEDTLS_DHM_C) |
| 3638 | mbedtls_dhm_free( &handshake->dhm_ctx ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3639 | #endif |
Neil Armstrong | f3f4641 | 2022-04-12 14:43:39 +0200 | [diff] [blame] | 3640 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3641 | mbedtls_ecdh_free( &handshake->ecdh_ctx ); |
Paul Bakker | 61d113b | 2013-07-04 11:51:43 +0200 | [diff] [blame] | 3642 | #endif |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 3643 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 3644 | mbedtls_ecjpake_free( &handshake->ecjpake_ctx ); |
Manuel Pégourié-Gonnard | 77c0646 | 2015-09-17 13:59:49 +0200 | [diff] [blame] | 3645 | #if defined(MBEDTLS_SSL_CLI_C) |
| 3646 | mbedtls_free( handshake->ecjpake_cache ); |
| 3647 | handshake->ecjpake_cache = NULL; |
| 3648 | handshake->ecjpake_cache_len = 0; |
| 3649 | #endif |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 3650 | #endif |
Paul Bakker | 61d113b | 2013-07-04 11:51:43 +0200 | [diff] [blame] | 3651 | |
Janos Follath | 4ae5c29 | 2016-02-10 11:27:43 +0000 | [diff] [blame] | 3652 | #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ |
| 3653 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 3654 | /* explicit void pointer cast for buggy MS compiler */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3655 | mbedtls_free( (void *) handshake->curves ); |
Manuel Pégourié-Gonnard | d09453c | 2013-09-23 19:11:32 +0200 | [diff] [blame] | 3656 | #endif |
| 3657 | |
Ronald Cron | 73fe8df | 2022-10-05 14:31:43 +0200 | [diff] [blame] | 3658 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED) |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 3659 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 3660 | if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) ) |
| 3661 | { |
| 3662 | /* The maintenance of the external PSK key slot is the |
| 3663 | * user's responsibility. */ |
| 3664 | if( ssl->handshake->psk_opaque_is_internal ) |
| 3665 | { |
| 3666 | psa_destroy_key( ssl->handshake->psk_opaque ); |
| 3667 | ssl->handshake->psk_opaque_is_internal = 0; |
| 3668 | } |
| 3669 | ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT; |
| 3670 | } |
Neil Armstrong | e952a30 | 2022-05-03 10:22:14 +0200 | [diff] [blame] | 3671 | #else |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 3672 | if( handshake->psk != NULL ) |
| 3673 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 3674 | mbedtls_platform_zeroize( handshake->psk, handshake->psk_len ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 3675 | mbedtls_free( handshake->psk ); |
| 3676 | } |
Neil Armstrong | e952a30 | 2022-05-03 10:22:14 +0200 | [diff] [blame] | 3677 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Ronald Cron | 73fe8df | 2022-10-05 14:31:43 +0200 | [diff] [blame] | 3678 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */ |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 3679 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3680 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 3681 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Manuel Pégourié-Gonnard | 8372454 | 2013-09-24 22:30:56 +0200 | [diff] [blame] | 3682 | /* |
| 3683 | * Free only the linked list wrapper, not the keys themselves |
| 3684 | * since the belong to the SNI callback |
| 3685 | */ |
Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 3686 | ssl_key_cert_free( handshake->sni_key_cert ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3687 | #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] | 3688 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3689 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Manuel Pégourié-Gonnard | 6b7301c | 2017-08-15 12:08:45 +0200 | [diff] [blame] | 3690 | mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx ); |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 3691 | if( handshake->ecrs_peer_cert != NULL ) |
| 3692 | { |
| 3693 | mbedtls_x509_crt_free( handshake->ecrs_peer_cert ); |
| 3694 | mbedtls_free( handshake->ecrs_peer_cert ); |
| 3695 | } |
Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 3696 | #endif |
| 3697 | |
Hanno Becker | 7517312 | 2019-02-06 16:18:31 +0000 | [diff] [blame] | 3698 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 3699 | !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 3700 | mbedtls_pk_free( &handshake->peer_pubkey ); |
| 3701 | #endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 3702 | |
XiaokangQian | 9b93c0d | 2022-02-09 06:02:25 +0000 | [diff] [blame] | 3703 | #if defined(MBEDTLS_SSL_CLI_C) && \ |
| 3704 | ( defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3) ) |
| 3705 | mbedtls_free( handshake->cookie ); |
| 3706 | #endif /* MBEDTLS_SSL_CLI_C && |
| 3707 | ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */ |
XiaokangQian | 8499b6c | 2022-01-27 09:00:11 +0000 | [diff] [blame] | 3708 | |
| 3709 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 533ab5f | 2020-02-05 10:49:13 +0000 | [diff] [blame] | 3710 | mbedtls_ssl_flight_free( handshake->flight ); |
| 3711 | mbedtls_ssl_buffering_free( ssl ); |
XiaokangQian | 8499b6c | 2022-01-27 09:00:11 +0000 | [diff] [blame] | 3712 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 3713 | |
Ronald Cron | f12b81d | 2022-03-15 10:42:41 +0100 | [diff] [blame] | 3714 | #if defined(MBEDTLS_ECDH_C) && \ |
| 3715 | ( defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3) ) |
Neil Armstrong | f716a70 | 2022-04-04 11:23:46 +0200 | [diff] [blame] | 3716 | if( handshake->ecdh_psa_privkey_is_external == 0 ) |
Neil Armstrong | 8113d25 | 2022-03-23 10:57:04 +0100 | [diff] [blame] | 3717 | psa_destroy_key( handshake->ecdh_psa_privkey ); |
Hanno Becker | 4a63ed4 | 2019-01-08 11:39:35 +0000 | [diff] [blame] | 3718 | #endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */ |
| 3719 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3720 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | a1a568c | 2021-11-09 10:17:21 +0800 | [diff] [blame] | 3721 | mbedtls_ssl_transform_free( handshake->transform_handshake ); |
| 3722 | mbedtls_ssl_transform_free( handshake->transform_earlydata ); |
Jerry Yu | ba9c727 | 2021-10-30 11:54:10 +0800 | [diff] [blame] | 3723 | mbedtls_free( handshake->transform_earlydata ); |
| 3724 | mbedtls_free( handshake->transform_handshake ); |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3725 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Jerry Yu | ba9c727 | 2021-10-30 11:54:10 +0800 | [diff] [blame] | 3726 | |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 3727 | |
| 3728 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 3729 | /* If the buffers are too big - reallocate. Because of the way Mbed TLS |
| 3730 | * processes datagrams and the fact that a datagram is allowed to have |
| 3731 | * several records in it, it is possible that the I/O buffers are not |
| 3732 | * empty at this stage */ |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 3733 | handle_buffer_resizing( ssl, 1, mbedtls_ssl_get_input_buflen( ssl ), |
| 3734 | mbedtls_ssl_get_output_buflen( ssl ) ); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 3735 | #endif |
Hanno Becker | 3aa186f | 2021-08-10 09:24:19 +0100 | [diff] [blame] | 3736 | |
Jerry Yu | ba9c727 | 2021-10-30 11:54:10 +0800 | [diff] [blame] | 3737 | /* mbedtls_platform_zeroize MUST be last one in this function */ |
| 3738 | mbedtls_platform_zeroize( handshake, |
| 3739 | sizeof( mbedtls_ssl_handshake_params ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3740 | } |
| 3741 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3742 | void mbedtls_ssl_session_free( mbedtls_ssl_session *session ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3743 | { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3744 | if( session == NULL ) |
| 3745 | return; |
| 3746 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3747 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Hanno Becker | 1294a0b | 2019-02-05 12:38:15 +0000 | [diff] [blame] | 3748 | ssl_clear_peer_cert( session ); |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 3749 | #endif |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 3750 | |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 3751 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
Xiaokang Qian | bc663a0 | 2022-10-09 11:14:39 +0000 | [diff] [blame] | 3752 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \ |
| 3753 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Xiaokang Qian | 281fd1b | 2022-09-20 11:35:41 +0000 | [diff] [blame] | 3754 | mbedtls_free( session->hostname ); |
| 3755 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3756 | mbedtls_free( session->ticket ); |
Paul Bakker | a503a63 | 2013-08-14 13:48:06 +0200 | [diff] [blame] | 3757 | #endif |
Manuel Pégourié-Gonnard | 75d4401 | 2013-08-02 14:44:04 +0200 | [diff] [blame] | 3758 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 3759 | mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3760 | } |
| 3761 | |
Manuel Pégourié-Gonnard | 5c0e377 | 2019-07-23 16:13:17 +0200 | [diff] [blame] | 3762 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 3763 | |
| 3764 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 3765 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u |
| 3766 | #else |
| 3767 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u |
| 3768 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 3769 | |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 3770 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 3771 | |
| 3772 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 3773 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u |
| 3774 | #else |
| 3775 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u |
| 3776 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
| 3777 | |
| 3778 | #if defined(MBEDTLS_SSL_ALPN) |
| 3779 | #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u |
| 3780 | #else |
| 3781 | #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u |
| 3782 | #endif /* MBEDTLS_SSL_ALPN */ |
| 3783 | |
| 3784 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0 |
| 3785 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1 |
| 3786 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2 |
| 3787 | #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3 |
| 3788 | |
| 3789 | #define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \ |
| 3790 | ( (uint32_t) ( \ |
| 3791 | ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \ |
| 3792 | ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \ |
| 3793 | ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \ |
| 3794 | ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \ |
| 3795 | 0u ) ) |
| 3796 | |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3797 | static unsigned char ssl_serialized_context_header[] = { |
| 3798 | MBEDTLS_VERSION_MAJOR, |
| 3799 | MBEDTLS_VERSION_MINOR, |
| 3800 | MBEDTLS_VERSION_PATCH, |
Joe Subbiani | 2194dc4 | 2021-07-14 12:31:31 +0100 | [diff] [blame] | 3801 | MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ), |
| 3802 | MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ), |
| 3803 | MBEDTLS_BYTE_2( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ), |
| 3804 | MBEDTLS_BYTE_1( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ), |
| 3805 | MBEDTLS_BYTE_0( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ), |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3806 | }; |
| 3807 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3808 | /* |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 3809 | * Serialize a full SSL context |
Manuel Pégourié-Gonnard | 00400c2 | 2019-07-10 14:58:45 +0200 | [diff] [blame] | 3810 | * |
| 3811 | * The format of the serialized data is: |
| 3812 | * (in the presentation language of TLS, RFC 8446 section 3) |
| 3813 | * |
| 3814 | * // header |
| 3815 | * opaque mbedtls_version[3]; // major, minor, patch |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3816 | * opaque context_format[5]; // version-specific field determining |
Manuel Pégourié-Gonnard | 00400c2 | 2019-07-10 14:58:45 +0200 | [diff] [blame] | 3817 | * // the format of the remaining |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3818 | * // serialized data. |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 3819 | * Note: When updating the format, remember to keep these |
| 3820 | * 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] | 3821 | * |
| 3822 | * // session sub-structure |
| 3823 | * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save() |
| 3824 | * // transform sub-structure |
| 3825 | * uint8 random[64]; // ServerHello.random+ClientHello.random |
| 3826 | * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value |
| 3827 | * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use |
| 3828 | * // fields from ssl_context |
| 3829 | * uint32 badmac_seen; // DTLS: number of records with failing MAC |
| 3830 | * uint64 in_window_top; // DTLS: last validated record seq_num |
| 3831 | * uint64 in_window; // DTLS: bitmask for replay protection |
| 3832 | * uint8 disable_datagram_packing; // DTLS: only one record per datagram |
| 3833 | * uint64 cur_out_ctr; // Record layer: outgoing sequence number |
| 3834 | * uint16 mtu; // DTLS: path mtu (max outgoing fragment size) |
| 3835 | * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol |
| 3836 | * |
| 3837 | * Note that many fields of the ssl_context or sub-structures are not |
| 3838 | * serialized, as they fall in one of the following categories: |
| 3839 | * |
| 3840 | * 1. forced value (eg in_left must be 0) |
| 3841 | * 2. pointer to dynamically-allocated memory (eg session, transform) |
| 3842 | * 3. value can be re-derived from other data (eg session keys from MS) |
| 3843 | * 4. value was temporary (eg content of input buffer) |
| 3844 | * 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] | 3845 | */ |
| 3846 | int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl, |
| 3847 | unsigned char *buf, |
| 3848 | size_t buf_len, |
| 3849 | size_t *olen ) |
| 3850 | { |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3851 | unsigned char *p = buf; |
| 3852 | size_t used = 0; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3853 | size_t session_len; |
| 3854 | int ret = 0; |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3855 | |
Manuel Pégourié-Gonnard | 1aaf669 | 2019-07-10 14:14:05 +0200 | [diff] [blame] | 3856 | /* |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3857 | * Enforce usage restrictions, see "return BAD_INPUT_DATA" in |
| 3858 | * this function's documentation. |
| 3859 | * |
| 3860 | * These are due to assumptions/limitations in the implementation. Some of |
| 3861 | * them are likely to stay (no handshake in progress) some might go away |
| 3862 | * (only DTLS) but are currently used to simplify the implementation. |
Manuel Pégourié-Gonnard | 1aaf669 | 2019-07-10 14:14:05 +0200 | [diff] [blame] | 3863 | */ |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3864 | /* The initial handshake must be over */ |
Paul Elliott | 27b0d94 | 2022-03-18 21:55:32 +0000 | [diff] [blame] | 3865 | if( mbedtls_ssl_is_handshake_over( ssl ) == 0 ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3866 | { |
| 3867 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Initial handshake isn't over" ) ); |
Manuel Pégourié-Gonnard | 1aaf669 | 2019-07-10 14:14:05 +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 | if( ssl->handshake != NULL ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3871 | { |
| 3872 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Handshake isn't completed" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3873 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3874 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3875 | /* Double-check that sub-structures are indeed ready */ |
| 3876 | if( ssl->transform == NULL || ssl->session == NULL ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3877 | { |
| 3878 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Serialised structures aren't ready" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3879 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3880 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3881 | /* There must be no pending incoming or outgoing data */ |
| 3882 | if( mbedtls_ssl_check_pending( ssl ) != 0 ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3883 | { |
| 3884 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending incoming data" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3885 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3886 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3887 | if( ssl->out_left != 0 ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3888 | { |
| 3889 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending outgoing data" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3890 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3891 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3892 | /* Protocol must be DLTS, not TLS */ |
| 3893 | if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3894 | { |
| 3895 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only DTLS is supported" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3896 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3897 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3898 | /* Version must be 1.2 */ |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 3899 | if( ssl->tls_version != MBEDTLS_SSL_VERSION_TLS1_2 ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3900 | { |
| 3901 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only version 1.2 supported" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3902 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3903 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3904 | /* We must be using an AEAD ciphersuite */ |
| 3905 | if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3906 | { |
| 3907 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only AEAD ciphersuites supported" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3908 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3909 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3910 | /* Renegotiation must not be enabled */ |
| 3911 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 3912 | if( ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3913 | { |
| 3914 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Renegotiation must not be enabled" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3915 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3916 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3917 | #endif |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 3918 | |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3919 | /* |
| 3920 | * Version and format identifier |
| 3921 | */ |
| 3922 | used += sizeof( ssl_serialized_context_header ); |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 3923 | |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3924 | if( used <= buf_len ) |
| 3925 | { |
| 3926 | memcpy( p, ssl_serialized_context_header, |
| 3927 | sizeof( ssl_serialized_context_header ) ); |
| 3928 | p += sizeof( ssl_serialized_context_header ); |
| 3929 | } |
| 3930 | |
| 3931 | /* |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3932 | * Session (length + data) |
| 3933 | */ |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 3934 | 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] | 3935 | if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ) |
| 3936 | return( ret ); |
| 3937 | |
| 3938 | used += 4 + session_len; |
| 3939 | if( used <= buf_len ) |
| 3940 | { |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 3941 | MBEDTLS_PUT_UINT32_BE( session_len, p, 0 ); |
| 3942 | p += 4; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3943 | |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 3944 | ret = ssl_session_save( ssl->session, 1, |
| 3945 | p, session_len, &session_len ); |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3946 | if( ret != 0 ) |
| 3947 | return( ret ); |
| 3948 | |
| 3949 | p += session_len; |
| 3950 | } |
| 3951 | |
| 3952 | /* |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 3953 | * Transform |
| 3954 | */ |
| 3955 | used += sizeof( ssl->transform->randbytes ); |
| 3956 | if( used <= buf_len ) |
| 3957 | { |
| 3958 | memcpy( p, ssl->transform->randbytes, |
| 3959 | sizeof( ssl->transform->randbytes ) ); |
| 3960 | p += sizeof( ssl->transform->randbytes ); |
| 3961 | } |
| 3962 | |
| 3963 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 3964 | used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len; |
| 3965 | if( used <= buf_len ) |
| 3966 | { |
| 3967 | *p++ = ssl->transform->in_cid_len; |
| 3968 | memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len ); |
| 3969 | p += ssl->transform->in_cid_len; |
| 3970 | |
| 3971 | *p++ = ssl->transform->out_cid_len; |
| 3972 | memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len ); |
| 3973 | p += ssl->transform->out_cid_len; |
| 3974 | } |
| 3975 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 3976 | |
| 3977 | /* |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3978 | * Saved fields from top-level ssl_context structure |
| 3979 | */ |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3980 | used += 4; |
| 3981 | if( used <= buf_len ) |
| 3982 | { |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 3983 | MBEDTLS_PUT_UINT32_BE( ssl->badmac_seen, p, 0 ); |
| 3984 | p += 4; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3985 | } |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3986 | |
| 3987 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 3988 | used += 16; |
| 3989 | if( used <= buf_len ) |
| 3990 | { |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 3991 | MBEDTLS_PUT_UINT64_BE( ssl->in_window_top, p, 0 ); |
| 3992 | p += 8; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3993 | |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 3994 | MBEDTLS_PUT_UINT64_BE( ssl->in_window, p, 0 ); |
| 3995 | p += 8; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3996 | } |
| 3997 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
| 3998 | |
| 3999 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4000 | used += 1; |
| 4001 | if( used <= buf_len ) |
| 4002 | { |
| 4003 | *p++ = ssl->disable_datagram_packing; |
| 4004 | } |
| 4005 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 4006 | |
Jerry Yu | ae0b2e2 | 2021-10-08 15:21:19 +0800 | [diff] [blame] | 4007 | used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4008 | if( used <= buf_len ) |
| 4009 | { |
Jerry Yu | ae0b2e2 | 2021-10-08 15:21:19 +0800 | [diff] [blame] | 4010 | memcpy( p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN ); |
| 4011 | p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4012 | } |
| 4013 | |
| 4014 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4015 | used += 2; |
| 4016 | if( used <= buf_len ) |
| 4017 | { |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 4018 | MBEDTLS_PUT_UINT16_BE( ssl->mtu, p, 0 ); |
| 4019 | p += 2; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4020 | } |
| 4021 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 4022 | |
| 4023 | #if defined(MBEDTLS_SSL_ALPN) |
| 4024 | { |
| 4025 | const uint8_t alpn_len = ssl->alpn_chosen |
Manuel Pégourié-Gonnard | f041f4e | 2019-07-24 00:58:27 +0200 | [diff] [blame] | 4026 | ? (uint8_t) strlen( ssl->alpn_chosen ) |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4027 | : 0; |
| 4028 | |
| 4029 | used += 1 + alpn_len; |
| 4030 | if( used <= buf_len ) |
| 4031 | { |
| 4032 | *p++ = alpn_len; |
| 4033 | |
| 4034 | if( ssl->alpn_chosen != NULL ) |
| 4035 | { |
| 4036 | memcpy( p, ssl->alpn_chosen, alpn_len ); |
| 4037 | p += alpn_len; |
| 4038 | } |
| 4039 | } |
| 4040 | } |
| 4041 | #endif /* MBEDTLS_SSL_ALPN */ |
| 4042 | |
| 4043 | /* |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4044 | * Done |
| 4045 | */ |
| 4046 | *olen = used; |
| 4047 | |
| 4048 | if( used > buf_len ) |
| 4049 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 4050 | |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4051 | MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used ); |
| 4052 | |
Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 4053 | return( mbedtls_ssl_session_reset_int( ssl, 0 ) ); |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 4054 | } |
| 4055 | |
| 4056 | /* |
Manuel Pégourié-Gonnard | b9dfc9f | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 4057 | * Deserialize context, see mbedtls_ssl_context_save() for format. |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4058 | * |
| 4059 | * This internal version is wrapped by a public function that cleans up in |
| 4060 | * case of error. |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 4061 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 4062 | MBEDTLS_CHECK_RETURN_CRITICAL |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4063 | static int ssl_context_load( mbedtls_ssl_context *ssl, |
| 4064 | const unsigned char *buf, |
| 4065 | size_t len ) |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 4066 | { |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4067 | const unsigned char *p = buf; |
| 4068 | const unsigned char * const end = buf + len; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4069 | size_t session_len; |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 4070 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Andrzej Kurek | 2d59dbc | 2022-10-13 08:34:38 -0400 | [diff] [blame] | 4071 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Andrzej Kurek | 894edde | 2022-09-29 06:31:14 -0400 | [diff] [blame] | 4072 | tls_prf_fn prf_func = NULL; |
Andrzej Kurek | 2d59dbc | 2022-10-13 08:34:38 -0400 | [diff] [blame] | 4073 | #endif |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4074 | |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 4075 | /* |
| 4076 | * The context should have been freshly setup or reset. |
| 4077 | * Give the user an error in case of obvious misuse. |
Manuel Pégourié-Gonnard | 4ca930f | 2019-07-26 16:31:53 +0200 | [diff] [blame] | 4078 | * (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] | 4079 | * renegotiating, or if the user mistakenly loaded a session first.) |
| 4080 | */ |
| 4081 | if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST || |
| 4082 | ssl->session != NULL ) |
| 4083 | { |
| 4084 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4085 | } |
| 4086 | |
| 4087 | /* |
| 4088 | * We can't check that the config matches the initial one, but we can at |
| 4089 | * least check it matches the requirements for serializing. |
| 4090 | */ |
| 4091 | if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM || |
Glenn Strauss | 2dfcea2 | 2022-03-14 17:26:42 -0400 | [diff] [blame] | 4092 | ssl->conf->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2 || |
| 4093 | ssl->conf->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 || |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 4094 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | 9a96fd7 | 2019-07-23 17:11:24 +0200 | [diff] [blame] | 4095 | ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED || |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 4096 | #endif |
Manuel Pégourié-Gonnard | 9a96fd7 | 2019-07-23 17:11:24 +0200 | [diff] [blame] | 4097 | 0 ) |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 4098 | { |
| 4099 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4100 | } |
| 4101 | |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4102 | MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len ); |
| 4103 | |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4104 | /* |
| 4105 | * Check version identifier |
| 4106 | */ |
| 4107 | if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) ) |
| 4108 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4109 | |
| 4110 | if( memcmp( p, ssl_serialized_context_header, |
| 4111 | sizeof( ssl_serialized_context_header ) ) != 0 ) |
| 4112 | { |
| 4113 | return( MBEDTLS_ERR_SSL_VERSION_MISMATCH ); |
| 4114 | } |
| 4115 | p += sizeof( ssl_serialized_context_header ); |
| 4116 | |
| 4117 | /* |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4118 | * Session |
| 4119 | */ |
| 4120 | if( (size_t)( end - p ) < 4 ) |
| 4121 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4122 | |
| 4123 | session_len = ( (size_t) p[0] << 24 ) | |
| 4124 | ( (size_t) p[1] << 16 ) | |
| 4125 | ( (size_t) p[2] << 8 ) | |
| 4126 | ( (size_t) p[3] ); |
| 4127 | p += 4; |
| 4128 | |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 4129 | /* This has been allocated by ssl_handshake_init(), called by |
Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 4130 | * 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] | 4131 | ssl->session = ssl->session_negotiate; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4132 | ssl->session_in = ssl->session; |
| 4133 | ssl->session_out = ssl->session; |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 4134 | ssl->session_negotiate = NULL; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4135 | |
| 4136 | if( (size_t)( end - p ) < session_len ) |
| 4137 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4138 | |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 4139 | ret = ssl_session_load( ssl->session, 1, p, session_len ); |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4140 | if( ret != 0 ) |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 4141 | { |
| 4142 | mbedtls_ssl_session_free( ssl->session ); |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4143 | return( ret ); |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 4144 | } |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4145 | |
| 4146 | p += session_len; |
| 4147 | |
| 4148 | /* |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4149 | * Transform |
| 4150 | */ |
| 4151 | |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 4152 | /* This has been allocated by ssl_handshake_init(), called by |
Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 4153 | * 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] | 4154 | ssl->transform = ssl->transform_negotiate; |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4155 | ssl->transform_in = ssl->transform; |
| 4156 | ssl->transform_out = ssl->transform; |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 4157 | ssl->transform_negotiate = NULL; |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4158 | |
Andrzej Kurek | 2d59dbc | 2022-10-13 08:34:38 -0400 | [diff] [blame] | 4159 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Andrzej Kurek | 894edde | 2022-09-29 06:31:14 -0400 | [diff] [blame] | 4160 | prf_func = ssl_tls12prf_from_cs( ssl->session->ciphersuite ); |
| 4161 | if( prf_func == NULL ) |
| 4162 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4163 | |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4164 | /* Read random bytes and populate structure */ |
| 4165 | if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) ) |
| 4166 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Andrzej Kurek | 2d59dbc | 2022-10-13 08:34:38 -0400 | [diff] [blame] | 4167 | |
Hanno Becker | bd25755 | 2021-03-22 06:59:27 +0000 | [diff] [blame] | 4168 | ret = ssl_tls12_populate_transform( ssl->transform, |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4169 | ssl->session->ciphersuite, |
| 4170 | ssl->session->master, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 4171 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4172 | ssl->session->encrypt_then_mac, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 4173 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Andrzej Kurek | 894edde | 2022-09-29 06:31:14 -0400 | [diff] [blame] | 4174 | prf_func, |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4175 | p, /* currently pointing to randbytes */ |
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 4176 | 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] | 4177 | ssl->conf->endpoint, |
| 4178 | ssl ); |
| 4179 | if( ret != 0 ) |
| 4180 | return( ret ); |
Jerry Yu | 840fbb2 | 2022-02-17 14:59:29 +0800 | [diff] [blame] | 4181 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4182 | p += sizeof( ssl->transform->randbytes ); |
| 4183 | |
| 4184 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 4185 | /* Read connection IDs and store them */ |
| 4186 | if( (size_t)( end - p ) < 1 ) |
| 4187 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4188 | |
| 4189 | ssl->transform->in_cid_len = *p++; |
| 4190 | |
Manuel Pégourié-Gonnard | 5ea13b8 | 2019-07-23 15:02:54 +0200 | [diff] [blame] | 4191 | 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] | 4192 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4193 | |
| 4194 | memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len ); |
| 4195 | p += ssl->transform->in_cid_len; |
| 4196 | |
| 4197 | ssl->transform->out_cid_len = *p++; |
| 4198 | |
| 4199 | if( (size_t)( end - p ) < ssl->transform->out_cid_len ) |
| 4200 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4201 | |
| 4202 | memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len ); |
| 4203 | p += ssl->transform->out_cid_len; |
| 4204 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 4205 | |
| 4206 | /* |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4207 | * Saved fields from top-level ssl_context structure |
| 4208 | */ |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4209 | if( (size_t)( end - p ) < 4 ) |
| 4210 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4211 | |
| 4212 | ssl->badmac_seen = ( (uint32_t) p[0] << 24 ) | |
| 4213 | ( (uint32_t) p[1] << 16 ) | |
| 4214 | ( (uint32_t) p[2] << 8 ) | |
| 4215 | ( (uint32_t) p[3] ); |
| 4216 | p += 4; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4217 | |
| 4218 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 4219 | if( (size_t)( end - p ) < 16 ) |
| 4220 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4221 | |
| 4222 | ssl->in_window_top = ( (uint64_t) p[0] << 56 ) | |
| 4223 | ( (uint64_t) p[1] << 48 ) | |
| 4224 | ( (uint64_t) p[2] << 40 ) | |
| 4225 | ( (uint64_t) p[3] << 32 ) | |
| 4226 | ( (uint64_t) p[4] << 24 ) | |
| 4227 | ( (uint64_t) p[5] << 16 ) | |
| 4228 | ( (uint64_t) p[6] << 8 ) | |
| 4229 | ( (uint64_t) p[7] ); |
| 4230 | p += 8; |
| 4231 | |
| 4232 | ssl->in_window = ( (uint64_t) p[0] << 56 ) | |
| 4233 | ( (uint64_t) p[1] << 48 ) | |
| 4234 | ( (uint64_t) p[2] << 40 ) | |
| 4235 | ( (uint64_t) p[3] << 32 ) | |
| 4236 | ( (uint64_t) p[4] << 24 ) | |
| 4237 | ( (uint64_t) p[5] << 16 ) | |
| 4238 | ( (uint64_t) p[6] << 8 ) | |
| 4239 | ( (uint64_t) p[7] ); |
| 4240 | p += 8; |
| 4241 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
| 4242 | |
| 4243 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4244 | if( (size_t)( end - p ) < 1 ) |
| 4245 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4246 | |
| 4247 | ssl->disable_datagram_packing = *p++; |
| 4248 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 4249 | |
Jerry Yu | d9a94fe | 2021-09-28 18:58:59 +0800 | [diff] [blame] | 4250 | if( (size_t)( end - p ) < sizeof( ssl->cur_out_ctr ) ) |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4251 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jerry Yu | d9a94fe | 2021-09-28 18:58:59 +0800 | [diff] [blame] | 4252 | memcpy( ssl->cur_out_ctr, p, sizeof( ssl->cur_out_ctr ) ); |
| 4253 | p += sizeof( ssl->cur_out_ctr ); |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4254 | |
| 4255 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4256 | if( (size_t)( end - p ) < 2 ) |
| 4257 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4258 | |
| 4259 | ssl->mtu = ( p[0] << 8 ) | p[1]; |
| 4260 | p += 2; |
| 4261 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 4262 | |
| 4263 | #if defined(MBEDTLS_SSL_ALPN) |
| 4264 | { |
| 4265 | uint8_t alpn_len; |
| 4266 | const char **cur; |
| 4267 | |
| 4268 | if( (size_t)( end - p ) < 1 ) |
| 4269 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4270 | |
| 4271 | alpn_len = *p++; |
| 4272 | |
| 4273 | if( alpn_len != 0 && ssl->conf->alpn_list != NULL ) |
| 4274 | { |
| 4275 | /* alpn_chosen should point to an item in the configured list */ |
| 4276 | for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ ) |
| 4277 | { |
| 4278 | if( strlen( *cur ) == alpn_len && |
| 4279 | memcmp( p, cur, alpn_len ) == 0 ) |
| 4280 | { |
| 4281 | ssl->alpn_chosen = *cur; |
| 4282 | break; |
| 4283 | } |
| 4284 | } |
| 4285 | } |
| 4286 | |
| 4287 | /* can only happen on conf mismatch */ |
| 4288 | if( alpn_len != 0 && ssl->alpn_chosen == NULL ) |
| 4289 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4290 | |
| 4291 | p += alpn_len; |
| 4292 | } |
| 4293 | #endif /* MBEDTLS_SSL_ALPN */ |
| 4294 | |
| 4295 | /* |
Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 4296 | * Forced fields from top-level ssl_context structure |
| 4297 | * |
| 4298 | * Most of them already set to the correct value by mbedtls_ssl_init() and |
| 4299 | * mbedtls_ssl_reset(), so we only need to set the remaining ones. |
| 4300 | */ |
| 4301 | ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER; |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 4302 | ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 4303 | |
Hanno Becker | 361b10d | 2019-08-30 10:42:49 +0100 | [diff] [blame] | 4304 | /* Adjust pointers for header fields of outgoing records to |
| 4305 | * the given transform, accounting for explicit IV and CID. */ |
Hanno Becker | 3e6f8ab | 2020-02-05 10:40:57 +0000 | [diff] [blame] | 4306 | mbedtls_ssl_update_out_pointers( ssl, ssl->transform ); |
Hanno Becker | 361b10d | 2019-08-30 10:42:49 +0100 | [diff] [blame] | 4307 | |
Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 4308 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4309 | ssl->in_epoch = 1; |
| 4310 | #endif |
| 4311 | |
| 4312 | /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated, |
| 4313 | * 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] | 4314 | * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform() |
| 4315 | * inappropriately. */ |
Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 4316 | if( ssl->handshake != NULL ) |
| 4317 | { |
| 4318 | mbedtls_ssl_handshake_free( ssl ); |
| 4319 | mbedtls_free( ssl->handshake ); |
| 4320 | ssl->handshake = NULL; |
| 4321 | } |
| 4322 | |
| 4323 | /* |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4324 | * Done - should have consumed entire buffer |
| 4325 | */ |
| 4326 | if( p != end ) |
| 4327 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 4328 | |
| 4329 | return( 0 ); |
| 4330 | } |
| 4331 | |
| 4332 | /* |
Manuel Pégourié-Gonnard | b9dfc9f | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 4333 | * Deserialize context: public wrapper for error cleaning |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4334 | */ |
| 4335 | int mbedtls_ssl_context_load( mbedtls_ssl_context *context, |
| 4336 | const unsigned char *buf, |
| 4337 | size_t len ) |
| 4338 | { |
| 4339 | int ret = ssl_context_load( context, buf, len ); |
| 4340 | |
| 4341 | if( ret != 0 ) |
| 4342 | mbedtls_ssl_free( context ); |
| 4343 | |
| 4344 | return( ret ); |
| 4345 | } |
Manuel Pégourié-Gonnard | 5c0e377 | 2019-07-23 16:13:17 +0200 | [diff] [blame] | 4346 | #endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */ |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4347 | |
| 4348 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4349 | * Free an SSL context |
| 4350 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4351 | void mbedtls_ssl_free( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4352 | { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 4353 | if( ssl == NULL ) |
| 4354 | return; |
| 4355 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4356 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4357 | |
Manuel Pégourié-Gonnard | 7ee6f0e | 2014-02-13 10:54:07 +0100 | [diff] [blame] | 4358 | if( ssl->out_buf != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4359 | { |
sander-visser | b8aa207 | 2020-05-06 22:05:13 +0200 | [diff] [blame] | 4360 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 4361 | size_t out_buf_len = ssl->out_buf_len; |
| 4362 | #else |
| 4363 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; |
| 4364 | #endif |
| 4365 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 4366 | mbedtls_platform_zeroize( ssl->out_buf, out_buf_len ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4367 | mbedtls_free( ssl->out_buf ); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 4368 | ssl->out_buf = NULL; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4369 | } |
| 4370 | |
Manuel Pégourié-Gonnard | 7ee6f0e | 2014-02-13 10:54:07 +0100 | [diff] [blame] | 4371 | if( ssl->in_buf != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4372 | { |
sander-visser | b8aa207 | 2020-05-06 22:05:13 +0200 | [diff] [blame] | 4373 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 4374 | size_t in_buf_len = ssl->in_buf_len; |
| 4375 | #else |
| 4376 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; |
| 4377 | #endif |
| 4378 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 4379 | mbedtls_platform_zeroize( ssl->in_buf, in_buf_len ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4380 | mbedtls_free( ssl->in_buf ); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 4381 | ssl->in_buf = NULL; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4382 | } |
| 4383 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4384 | if( ssl->transform ) |
| 4385 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4386 | mbedtls_ssl_transform_free( ssl->transform ); |
| 4387 | mbedtls_free( ssl->transform ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4388 | } |
| 4389 | |
| 4390 | if( ssl->handshake ) |
| 4391 | { |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 4392 | mbedtls_ssl_handshake_free( ssl ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4393 | mbedtls_ssl_transform_free( ssl->transform_negotiate ); |
| 4394 | mbedtls_ssl_session_free( ssl->session_negotiate ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4395 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4396 | mbedtls_free( ssl->handshake ); |
| 4397 | mbedtls_free( ssl->transform_negotiate ); |
| 4398 | mbedtls_free( ssl->session_negotiate ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4399 | } |
| 4400 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4401 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Hanno Becker | 3aa186f | 2021-08-10 09:24:19 +0100 | [diff] [blame] | 4402 | mbedtls_ssl_transform_free( ssl->transform_application ); |
| 4403 | mbedtls_free( ssl->transform_application ); |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4404 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | 3aa186f | 2021-08-10 09:24:19 +0100 | [diff] [blame] | 4405 | |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 4406 | if( ssl->session ) |
| 4407 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4408 | mbedtls_ssl_session_free( ssl->session ); |
| 4409 | mbedtls_free( ssl->session ); |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 4410 | } |
| 4411 | |
Manuel Pégourié-Gonnard | 55fab2d | 2015-05-11 16:15:19 +0200 | [diff] [blame] | 4412 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 4413 | if( ssl->hostname != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4414 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 4415 | mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4416 | mbedtls_free( ssl->hostname ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4417 | } |
Paul Bakker | 0be444a | 2013-08-27 21:55:01 +0200 | [diff] [blame] | 4418 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4419 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 4420 | #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] | 4421 | mbedtls_free( ssl->cli_id ); |
Manuel Pégourié-Gonnard | 43c0218 | 2014-07-22 17:32:01 +0200 | [diff] [blame] | 4422 | #endif |
| 4423 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4424 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) ); |
Paul Bakker | 2da561c | 2009-02-05 18:00:28 +0000 | [diff] [blame] | 4425 | |
Paul Bakker | 86f04f4 | 2013-02-14 11:20:09 +0100 | [diff] [blame] | 4426 | /* Actually clear after last debug message */ |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 4427 | mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4428 | } |
| 4429 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4430 | /* |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 4431 | * Initialize mbedtls_ssl_config |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4432 | */ |
| 4433 | void mbedtls_ssl_config_init( mbedtls_ssl_config *conf ) |
| 4434 | { |
| 4435 | memset( conf, 0, sizeof( mbedtls_ssl_config ) ); |
| 4436 | } |
| 4437 | |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4438 | /* The selection should be the same as mbedtls_x509_crt_profile_default in |
| 4439 | * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters: |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4440 | * curves with a lower resource usage come first. |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4441 | * See the documentation of mbedtls_ssl_conf_curves() for what we promise |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4442 | * about this list. |
| 4443 | */ |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4444 | static uint16_t ssl_preset_default_groups[] = { |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4445 | #if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4446 | MBEDTLS_SSL_IANA_TLS_GROUP_X25519, |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4447 | #endif |
| 4448 | #if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4449 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1, |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4450 | #endif |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4451 | #if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4452 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4453 | #endif |
| 4454 | #if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4455 | MBEDTLS_SSL_IANA_TLS_GROUP_X448, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4456 | #endif |
| 4457 | #if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4458 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4459 | #endif |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4460 | #if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4461 | MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1, |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4462 | #endif |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4463 | #if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4464 | MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4465 | #endif |
| 4466 | #if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4467 | MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4468 | #endif |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4469 | MBEDTLS_SSL_IANA_TLS_GROUP_NONE |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4470 | }; |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4471 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4472 | static int ssl_preset_suiteb_ciphersuites[] = { |
| 4473 | MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, |
| 4474 | MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, |
| 4475 | 0 |
| 4476 | }; |
| 4477 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 4478 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Hanno Becker | 9c6aa7b | 2021-08-10 13:50:43 +0100 | [diff] [blame] | 4479 | |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4480 | /* NOTICE: |
Jerry Yu | 0b994b8 | 2022-01-25 17:22:12 +0800 | [diff] [blame] | 4481 | * 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] | 4482 | * rules SHOULD be upheld. |
| 4483 | * - No duplicate entries. |
| 4484 | * - 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] | 4485 | * - ssl_tls12_preset* is for TLS 1.2 use only. |
Jerry Yu | 370e146 | 2022-01-25 10:36:53 +0800 | [diff] [blame] | 4486 | * - 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] | 4487 | */ |
Hanno Becker | 9c6aa7b | 2021-08-10 13:50:43 +0100 | [diff] [blame] | 4488 | static uint16_t ssl_preset_default_sig_algs[] = { |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4489 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4490 | #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] | 4491 | defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) |
| 4492 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256, |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4493 | #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] | 4494 | MBEDTLS_ECP_DP_SECP256R1_ENABLED */ |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4495 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4496 | #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] | 4497 | defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) |
| 4498 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384, |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4499 | #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] | 4500 | MBEDTLS_ECP_DP_SECP384R1_ENABLED */ |
| 4501 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4502 | #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] | 4503 | defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) |
| 4504 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512, |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4505 | #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] | 4506 | MBEDTLS_ECP_DP_SECP521R1_ENABLED */ |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4507 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4508 | #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] | 4509 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512, |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4510 | #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] | 4511 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4512 | #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] | 4513 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384, |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4514 | #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] | 4515 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4516 | #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] | 4517 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256, |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4518 | #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] | 4519 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4520 | #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] | 4521 | MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512, |
| 4522 | #endif /* MBEDTLS_RSA_C && MBEDTLS_SHA512_C */ |
| 4523 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4524 | #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] | 4525 | MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384, |
| 4526 | #endif /* MBEDTLS_RSA_C && MBEDTLS_SHA384_C */ |
| 4527 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4528 | #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] | 4529 | MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256, |
| 4530 | #endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */ |
| 4531 | |
Gabor Mezei | 15b95a6 | 2022-05-09 16:37:58 +0200 | [diff] [blame] | 4532 | MBEDTLS_TLS_SIG_NONE |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4533 | }; |
| 4534 | |
| 4535 | /* NOTICE: see above */ |
| 4536 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 4537 | static uint16_t ssl_tls12_preset_default_sig_algs[] = { |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4538 | #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] | 4539 | #if defined(MBEDTLS_ECDSA_C) |
| 4540 | 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] | 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_SHA512, |
| 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_SHA512 ), |
| 4547 | #endif |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4548 | #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] | 4549 | #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] | 4550 | #if defined(MBEDTLS_ECDSA_C) |
| 4551 | 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] | 4552 | #endif |
Jerry Yu | 09a99fc | 2022-07-28 14:22:17 +0800 | [diff] [blame] | 4553 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) |
| 4554 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384, |
| 4555 | #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */ |
Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 4556 | #if defined(MBEDTLS_RSA_C) |
| 4557 | MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ), |
| 4558 | #endif |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4559 | #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] | 4560 | #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] | 4561 | #if defined(MBEDTLS_ECDSA_C) |
| 4562 | 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] | 4563 | #endif |
Jerry Yu | 09a99fc | 2022-07-28 14:22:17 +0800 | [diff] [blame] | 4564 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) |
| 4565 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256, |
| 4566 | #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */ |
Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 4567 | #if defined(MBEDTLS_RSA_C) |
| 4568 | MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ), |
| 4569 | #endif |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4570 | #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] | 4571 | MBEDTLS_TLS_SIG_NONE |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4572 | }; |
| 4573 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 4574 | /* NOTICE: see above */ |
| 4575 | static uint16_t ssl_preset_suiteb_sig_algs[] = { |
| 4576 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4577 | #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] | 4578 | defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) |
| 4579 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256, |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4580 | #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] | 4581 | MBEDTLS_ECP_DP_SECP256R1_ENABLED */ |
| 4582 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4583 | #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] | 4584 | defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) |
| 4585 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384, |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4586 | #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] | 4587 | MBEDTLS_ECP_DP_SECP384R1_ENABLED */ |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4588 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4589 | #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] | 4590 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256, |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4591 | #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] | 4592 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4593 | #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] | 4594 | MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256, |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4595 | #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] | 4596 | |
Gabor Mezei | 15b95a6 | 2022-05-09 16:37:58 +0200 | [diff] [blame] | 4597 | MBEDTLS_TLS_SIG_NONE |
Jerry Yu | 713013f | 2022-01-17 18:16:35 +0800 | [diff] [blame] | 4598 | }; |
Jerry Yu | 6106fdc | 2022-01-12 16:36:14 +0800 | [diff] [blame] | 4599 | |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4600 | /* NOTICE: see above */ |
| 4601 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 4602 | static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = { |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4603 | #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] | 4604 | #if defined(MBEDTLS_ECDSA_C) |
| 4605 | 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] | 4606 | #endif |
Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 4607 | #if defined(MBEDTLS_RSA_C) |
| 4608 | MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ), |
| 4609 | #endif |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4610 | #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] | 4611 | #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] | 4612 | #if defined(MBEDTLS_ECDSA_C) |
| 4613 | 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] | 4614 | #endif |
Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 4615 | #if defined(MBEDTLS_RSA_C) |
| 4616 | MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ), |
| 4617 | #endif |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4618 | #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] | 4619 | MBEDTLS_TLS_SIG_NONE |
Hanno Becker | 9c6aa7b | 2021-08-10 13:50:43 +0100 | [diff] [blame] | 4620 | }; |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4621 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 4622 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 4623 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4624 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4625 | static uint16_t ssl_preset_suiteb_groups[] = { |
Jaeden Amero | d431104 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 4626 | #if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4627 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1, |
Jaeden Amero | d431104 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 4628 | #endif |
| 4629 | #if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4630 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1, |
Jaeden Amero | d431104 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 4631 | #endif |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4632 | MBEDTLS_SSL_IANA_TLS_GROUP_NONE |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4633 | }; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4634 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 4635 | #if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4636 | /* 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] | 4637 | * to make sure there are no duplicated signature algorithm entries. */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 4638 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 4639 | static int ssl_check_no_sig_alg_duplication( uint16_t * sig_algs ) |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4640 | { |
| 4641 | size_t i, j; |
| 4642 | int ret = 0; |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4643 | |
Gabor Mezei | 15b95a6 | 2022-05-09 16:37:58 +0200 | [diff] [blame] | 4644 | for( i = 0; sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ ) |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4645 | { |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 4646 | for( j = 0; j < i; j++ ) |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4647 | { |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 4648 | if( sig_algs[i] != sig_algs[j] ) |
| 4649 | continue; |
| 4650 | mbedtls_printf( " entry(%04x,%" MBEDTLS_PRINTF_SIZET |
| 4651 | ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n", |
| 4652 | sig_algs[i], j, i ); |
| 4653 | ret = -1; |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4654 | } |
| 4655 | } |
| 4656 | return( ret ); |
| 4657 | } |
| 4658 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 4659 | #endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4660 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4661 | /* |
Tillmann Karras | 588ad50 | 2015-09-25 04:27:22 +0200 | [diff] [blame] | 4662 | * Load default in mbedtls_ssl_config |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4663 | */ |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 4664 | int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4665 | int endpoint, int transport, int preset ) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4666 | { |
Manuel Pégourié-Gonnard | 8b431fb | 2015-05-11 12:54:52 +0200 | [diff] [blame] | 4667 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 4668 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 8b431fb | 2015-05-11 12:54:52 +0200 | [diff] [blame] | 4669 | #endif |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4670 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 4671 | #if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 4672 | if( ssl_check_no_sig_alg_duplication( ssl_preset_suiteb_sig_algs ) ) |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4673 | { |
| 4674 | mbedtls_printf( "ssl_preset_suiteb_sig_algs has duplicated entries\n" ); |
| 4675 | return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED ); |
| 4676 | } |
| 4677 | |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 4678 | if( ssl_check_no_sig_alg_duplication( ssl_preset_default_sig_algs ) ) |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4679 | { |
| 4680 | mbedtls_printf( "ssl_preset_default_sig_algs has duplicated entries\n" ); |
| 4681 | return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED ); |
| 4682 | } |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4683 | |
| 4684 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 4685 | 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] | 4686 | { |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4687 | mbedtls_printf( "ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n" ); |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4688 | return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED ); |
| 4689 | } |
| 4690 | |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 4691 | 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] | 4692 | { |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4693 | mbedtls_printf( "ssl_tls12_preset_default_sig_algs has duplicated entries\n" ); |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4694 | return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED ); |
| 4695 | } |
| 4696 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 4697 | #endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4698 | |
Manuel Pégourié-Gonnard | 0de074f | 2015-05-14 12:58:01 +0200 | [diff] [blame] | 4699 | /* Use the functions here so that they are covered in tests, |
| 4700 | * but otherwise access member directly for efficiency */ |
| 4701 | mbedtls_ssl_conf_endpoint( conf, endpoint ); |
| 4702 | mbedtls_ssl_conf_transport( conf, transport ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4703 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4704 | /* |
| 4705 | * Things that are common to all presets |
| 4706 | */ |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 4707 | #if defined(MBEDTLS_SSL_CLI_C) |
| 4708 | if( endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 4709 | { |
| 4710 | conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED; |
| 4711 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 4712 | conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED; |
| 4713 | #endif |
| 4714 | } |
| 4715 | #endif |
| 4716 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4717 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 4718 | conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED; |
| 4719 | #endif |
| 4720 | |
| 4721 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 4722 | conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED; |
| 4723 | #endif |
| 4724 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 4725 | #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] | 4726 | conf->f_cookie_write = ssl_cookie_write_dummy; |
| 4727 | conf->f_cookie_check = ssl_cookie_check_dummy; |
| 4728 | #endif |
| 4729 | |
| 4730 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 4731 | conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED; |
| 4732 | #endif |
| 4733 | |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 4734 | #if defined(MBEDTLS_SSL_SRV_C) |
| 4735 | conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED; |
TRodziewicz | 3946f79 | 2021-06-14 12:11:18 +0200 | [diff] [blame] | 4736 | conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER; |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 4737 | #endif |
| 4738 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4739 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4740 | conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN; |
| 4741 | conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX; |
| 4742 | #endif |
| 4743 | |
| 4744 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 4745 | conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT; |
Andres AG | 2196c7f | 2016-12-15 17:01:16 +0000 | [diff] [blame] | 4746 | memset( conf->renego_period, 0x00, 2 ); |
| 4747 | memset( conf->renego_period + 2, 0xFF, 6 ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4748 | #endif |
| 4749 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4750 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
Hanno Becker | e2defad | 2021-07-24 05:59:17 +0100 | [diff] [blame] | 4751 | if( endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 4752 | { |
| 4753 | const unsigned char dhm_p[] = |
| 4754 | MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN; |
| 4755 | const unsigned char dhm_g[] = |
| 4756 | MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN; |
Hanno Becker | 00d0a68 | 2017-10-04 13:14:29 +0100 | [diff] [blame] | 4757 | |
Hanno Becker | e2defad | 2021-07-24 05:59:17 +0100 | [diff] [blame] | 4758 | if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf, |
| 4759 | dhm_p, sizeof( dhm_p ), |
| 4760 | dhm_g, sizeof( dhm_g ) ) ) != 0 ) |
| 4761 | { |
| 4762 | return( ret ); |
| 4763 | } |
| 4764 | } |
Manuel Pégourié-Gonnard | bd990d6 | 2015-06-11 14:49:42 +0200 | [diff] [blame] | 4765 | #endif |
| 4766 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4767 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | d0766ec | 2022-09-22 10:46:57 +0800 | [diff] [blame] | 4768 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SESSION_TICKETS) |
Jerry Yu | 1ad7ace | 2022-08-09 13:28:39 +0800 | [diff] [blame] | 4769 | mbedtls_ssl_conf_new_session_tickets( |
| 4770 | conf, MBEDTLS_SSL_TLS1_3_DEFAULT_NEW_SESSION_TICKETS ); |
| 4771 | #endif |
Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 4772 | /* |
| 4773 | * Allow all TLS 1.3 key exchange modes by default. |
| 4774 | */ |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 4775 | conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL; |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4776 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 4777 | |
XiaokangQian | 4d3a604 | 2022-04-21 13:46:17 +0000 | [diff] [blame] | 4778 | if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 4779 | { |
Glenn Strauss | 2dfcea2 | 2022-03-14 17:26:42 -0400 | [diff] [blame] | 4780 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
XiaokangQian | 4d3a604 | 2022-04-21 13:46:17 +0000 | [diff] [blame] | 4781 | conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
XiaokangQian | 060d867 | 2022-04-21 09:24:56 +0000 | [diff] [blame] | 4782 | conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
XiaokangQian | 4d3a604 | 2022-04-21 13:46:17 +0000 | [diff] [blame] | 4783 | #else |
XiaokangQian | 060d867 | 2022-04-21 09:24:56 +0000 | [diff] [blame] | 4784 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
XiaokangQian | 060d867 | 2022-04-21 09:24:56 +0000 | [diff] [blame] | 4785 | #endif |
XiaokangQian | 4d3a604 | 2022-04-21 13:46:17 +0000 | [diff] [blame] | 4786 | } |
| 4787 | else |
| 4788 | { |
| 4789 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 4790 | if( endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 4791 | { |
| 4792 | conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 4793 | conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3; |
| 4794 | } |
| 4795 | else |
| 4796 | /* Hybrid TLS 1.2 / 1.3 is not supported on server side yet */ |
| 4797 | { |
| 4798 | conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 4799 | conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 4800 | } |
| 4801 | #elif defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 4802 | conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_3; |
| 4803 | conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3; |
| 4804 | #elif defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 4805 | conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 4806 | conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 4807 | #else |
| 4808 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 4809 | #endif |
| 4810 | } |
Glenn Strauss | 2dfcea2 | 2022-03-14 17:26:42 -0400 | [diff] [blame] | 4811 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4812 | /* |
| 4813 | * Preset-specific defaults |
| 4814 | */ |
| 4815 | switch( preset ) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4816 | { |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4817 | /* |
| 4818 | * NSA Suite B |
| 4819 | */ |
| 4820 | case MBEDTLS_SSL_PRESET_SUITEB: |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4821 | |
Hanno Becker | d60b6c6 | 2021-04-29 12:04:11 +0100 | [diff] [blame] | 4822 | conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4823 | |
| 4824 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 4825 | conf->cert_profile = &mbedtls_x509_crt_profile_suiteb; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4826 | #endif |
| 4827 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 4828 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4829 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 4830 | if( mbedtls_ssl_conf_is_tls12_only( conf ) ) |
| 4831 | conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs; |
| 4832 | else |
| 4833 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 4834 | conf->sig_algs = ssl_preset_suiteb_sig_algs; |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 4835 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4836 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4837 | #if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 4838 | conf->curve_list = NULL; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4839 | #endif |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4840 | conf->group_list = ssl_preset_suiteb_groups; |
Manuel Pégourié-Gonnard | c98204e | 2015-08-11 04:21:01 +0200 | [diff] [blame] | 4841 | break; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4842 | |
| 4843 | /* |
| 4844 | * Default |
| 4845 | */ |
| 4846 | default: |
Ronald Cron | f660655 | 2022-03-15 11:23:25 +0100 | [diff] [blame] | 4847 | |
Hanno Becker | d60b6c6 | 2021-04-29 12:04:11 +0100 | [diff] [blame] | 4848 | conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites(); |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4849 | |
| 4850 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 4851 | conf->cert_profile = &mbedtls_x509_crt_profile_default; |
| 4852 | #endif |
| 4853 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 4854 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4855 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 4856 | if( mbedtls_ssl_conf_is_tls12_only( conf ) ) |
| 4857 | conf->sig_algs = ssl_tls12_preset_default_sig_algs; |
| 4858 | else |
| 4859 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 4860 | conf->sig_algs = ssl_preset_default_sig_algs; |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 4861 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4862 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4863 | #if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 4864 | conf->curve_list = NULL; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4865 | #endif |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4866 | conf->group_list = ssl_preset_default_groups; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4867 | |
| 4868 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C) |
| 4869 | conf->dhm_min_bitlen = 1024; |
| 4870 | #endif |
| 4871 | } |
| 4872 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4873 | return( 0 ); |
| 4874 | } |
| 4875 | |
| 4876 | /* |
| 4877 | * Free mbedtls_ssl_config |
| 4878 | */ |
| 4879 | void mbedtls_ssl_config_free( mbedtls_ssl_config *conf ) |
| 4880 | { |
| 4881 | #if defined(MBEDTLS_DHM_C) |
| 4882 | mbedtls_mpi_free( &conf->dhm_P ); |
| 4883 | mbedtls_mpi_free( &conf->dhm_G ); |
| 4884 | #endif |
| 4885 | |
Ronald Cron | 73fe8df | 2022-10-05 14:31:43 +0200 | [diff] [blame] | 4886 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED) |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 4887 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 4888 | if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) ) |
| 4889 | { |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 4890 | conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT; |
| 4891 | } |
Neil Armstrong | 8ecd668 | 2022-05-05 11:40:35 +0200 | [diff] [blame] | 4892 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4893 | if( conf->psk != NULL ) |
| 4894 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 4895 | mbedtls_platform_zeroize( conf->psk, conf->psk_len ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4896 | mbedtls_free( conf->psk ); |
Azim Khan | 27e8a12 | 2018-03-21 14:24:11 +0000 | [diff] [blame] | 4897 | conf->psk = NULL; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4898 | conf->psk_len = 0; |
junyeonLEE | 316b162 | 2017-12-20 16:29:30 +0900 | [diff] [blame] | 4899 | } |
| 4900 | |
| 4901 | if( conf->psk_identity != NULL ) |
| 4902 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 4903 | mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len ); |
junyeonLEE | 316b162 | 2017-12-20 16:29:30 +0900 | [diff] [blame] | 4904 | mbedtls_free( conf->psk_identity ); |
Azim Khan | 27e8a12 | 2018-03-21 14:24:11 +0000 | [diff] [blame] | 4905 | conf->psk_identity = NULL; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4906 | conf->psk_identity_len = 0; |
| 4907 | } |
Ronald Cron | 73fe8df | 2022-10-05 14:31:43 +0200 | [diff] [blame] | 4908 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */ |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4909 | |
| 4910 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 4911 | ssl_key_cert_free( conf->key_cert ); |
| 4912 | #endif |
| 4913 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 4914 | mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4915 | } |
| 4916 | |
Manuel Pégourié-Gonnard | 5674a97 | 2015-10-19 15:14:03 +0200 | [diff] [blame] | 4917 | #if defined(MBEDTLS_PK_C) && \ |
| 4918 | ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) ) |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 4919 | /* |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4920 | * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 4921 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4922 | 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] | 4923 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4924 | #if defined(MBEDTLS_RSA_C) |
| 4925 | if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) ) |
| 4926 | return( MBEDTLS_SSL_SIG_RSA ); |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 4927 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4928 | #if defined(MBEDTLS_ECDSA_C) |
| 4929 | if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) ) |
| 4930 | return( MBEDTLS_SSL_SIG_ECDSA ); |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 4931 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4932 | return( MBEDTLS_SSL_SIG_ANON ); |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 4933 | } |
| 4934 | |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 4935 | unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type ) |
| 4936 | { |
| 4937 | switch( type ) { |
| 4938 | case MBEDTLS_PK_RSA: |
| 4939 | return( MBEDTLS_SSL_SIG_RSA ); |
| 4940 | case MBEDTLS_PK_ECDSA: |
| 4941 | case MBEDTLS_PK_ECKEY: |
| 4942 | return( MBEDTLS_SSL_SIG_ECDSA ); |
| 4943 | default: |
| 4944 | return( MBEDTLS_SSL_SIG_ANON ); |
| 4945 | } |
| 4946 | } |
| 4947 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4948 | 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] | 4949 | { |
| 4950 | switch( sig ) |
| 4951 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4952 | #if defined(MBEDTLS_RSA_C) |
| 4953 | case MBEDTLS_SSL_SIG_RSA: |
| 4954 | return( MBEDTLS_PK_RSA ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4955 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4956 | #if defined(MBEDTLS_ECDSA_C) |
| 4957 | case MBEDTLS_SSL_SIG_ECDSA: |
| 4958 | return( MBEDTLS_PK_ECDSA ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4959 | #endif |
| 4960 | default: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4961 | return( MBEDTLS_PK_NONE ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4962 | } |
| 4963 | } |
Manuel Pégourié-Gonnard | 5674a97 | 2015-10-19 15:14:03 +0200 | [diff] [blame] | 4964 | #endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */ |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4965 | |
Manuel Pégourié-Gonnard | 1a48383 | 2013-09-20 12:29:15 +0200 | [diff] [blame] | 4966 | /* |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 4967 | * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX |
Manuel Pégourié-Gonnard | 1a48383 | 2013-09-20 12:29:15 +0200 | [diff] [blame] | 4968 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4969 | 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] | 4970 | { |
| 4971 | switch( hash ) |
| 4972 | { |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4973 | #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] | 4974 | case MBEDTLS_SSL_HASH_MD5: |
| 4975 | return( MBEDTLS_MD_MD5 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4976 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4977 | #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] | 4978 | case MBEDTLS_SSL_HASH_SHA1: |
| 4979 | return( MBEDTLS_MD_SHA1 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4980 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4981 | #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] | 4982 | case MBEDTLS_SSL_HASH_SHA224: |
| 4983 | return( MBEDTLS_MD_SHA224 ); |
Mateusz Starzyk | e3c48b4 | 2021-04-19 16:46:28 +0200 | [diff] [blame] | 4984 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4985 | #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] | 4986 | case MBEDTLS_SSL_HASH_SHA256: |
| 4987 | return( MBEDTLS_MD_SHA256 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4988 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4989 | #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] | 4990 | case MBEDTLS_SSL_HASH_SHA384: |
| 4991 | return( MBEDTLS_MD_SHA384 ); |
Mateusz Starzyk | 3352a53 | 2021-04-06 14:28:22 +0200 | [diff] [blame] | 4992 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4993 | #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] | 4994 | case MBEDTLS_SSL_HASH_SHA512: |
| 4995 | return( MBEDTLS_MD_SHA512 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4996 | #endif |
| 4997 | default: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4998 | return( MBEDTLS_MD_NONE ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4999 | } |
| 5000 | } |
| 5001 | |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 5002 | /* |
| 5003 | * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX |
| 5004 | */ |
| 5005 | unsigned char mbedtls_ssl_hash_from_md_alg( int md ) |
| 5006 | { |
| 5007 | switch( md ) |
| 5008 | { |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5009 | #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] | 5010 | case MBEDTLS_MD_MD5: |
| 5011 | return( MBEDTLS_SSL_HASH_MD5 ); |
| 5012 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5013 | #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] | 5014 | case MBEDTLS_MD_SHA1: |
| 5015 | return( MBEDTLS_SSL_HASH_SHA1 ); |
| 5016 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5017 | #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] | 5018 | case MBEDTLS_MD_SHA224: |
| 5019 | return( MBEDTLS_SSL_HASH_SHA224 ); |
Mateusz Starzyk | e3c48b4 | 2021-04-19 16:46:28 +0200 | [diff] [blame] | 5020 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5021 | #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] | 5022 | case MBEDTLS_MD_SHA256: |
| 5023 | return( MBEDTLS_SSL_HASH_SHA256 ); |
| 5024 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5025 | #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] | 5026 | case MBEDTLS_MD_SHA384: |
| 5027 | return( MBEDTLS_SSL_HASH_SHA384 ); |
Mateusz Starzyk | 3352a53 | 2021-04-06 14:28:22 +0200 | [diff] [blame] | 5028 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5029 | #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] | 5030 | case MBEDTLS_MD_SHA512: |
| 5031 | return( MBEDTLS_SSL_HASH_SHA512 ); |
| 5032 | #endif |
| 5033 | default: |
| 5034 | return( MBEDTLS_SSL_HASH_NONE ); |
| 5035 | } |
| 5036 | } |
| 5037 | |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 5038 | /* |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 5039 | * 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] | 5040 | * 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] | 5041 | */ |
Manuel Pégourié-Gonnard | 0d63b84 | 2022-01-18 13:10:56 +0100 | [diff] [blame] | 5042 | 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] | 5043 | { |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5044 | const uint16_t *group_list = mbedtls_ssl_get_groups( ssl ); |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 5045 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5046 | if( group_list == NULL ) |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 5047 | return( -1 ); |
| 5048 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5049 | for( ; *group_list != 0; group_list++ ) |
| 5050 | { |
| 5051 | if( *group_list == tls_id ) |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 5052 | return( 0 ); |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5053 | } |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 5054 | |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 5055 | return( -1 ); |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 5056 | } |
Manuel Pégourié-Gonnard | 0d63b84 | 2022-01-18 13:10:56 +0100 | [diff] [blame] | 5057 | |
| 5058 | #if defined(MBEDTLS_ECP_C) |
| 5059 | /* |
| 5060 | * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id. |
| 5061 | */ |
| 5062 | int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id ) |
| 5063 | { |
Leonid Rozenboim | 19e5973 | 2022-08-08 16:52:38 -0700 | [diff] [blame] | 5064 | const mbedtls_ecp_curve_info *grp_info = |
Tom Cosgrove | bcc13c9 | 2022-08-24 15:08:16 +0100 | [diff] [blame] | 5065 | mbedtls_ecp_curve_info_from_grp_id( grp_id ); |
Leonid Rozenboim | 19e5973 | 2022-08-08 16:52:38 -0700 | [diff] [blame] | 5066 | |
Tom Cosgrove | bcc13c9 | 2022-08-24 15:08:16 +0100 | [diff] [blame] | 5067 | if ( grp_info == NULL ) |
Leonid Rozenboim | 19e5973 | 2022-08-08 16:52:38 -0700 | [diff] [blame] | 5068 | return -1; |
| 5069 | |
| 5070 | uint16_t tls_id = grp_info->tls_id; |
| 5071 | |
Manuel Pégourié-Gonnard | 0d63b84 | 2022-01-18 13:10:56 +0100 | [diff] [blame] | 5072 | return mbedtls_ssl_check_curve_tls_id( ssl, tls_id ); |
| 5073 | } |
Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 5074 | #endif /* MBEDTLS_ECP_C */ |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5075 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5076 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 5077 | int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert, |
| 5078 | const mbedtls_ssl_ciphersuite_t *ciphersuite, |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 5079 | int cert_endpoint, |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 5080 | uint32_t *flags ) |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5081 | { |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 5082 | int ret = 0; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5083 | int usage = 0; |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 5084 | const char *ext_oid; |
| 5085 | size_t ext_len; |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 5086 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5087 | if( cert_endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5088 | { |
| 5089 | /* Server part of the key exchange */ |
| 5090 | switch( ciphersuite->key_exchange ) |
| 5091 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5092 | case MBEDTLS_KEY_EXCHANGE_RSA: |
| 5093 | case MBEDTLS_KEY_EXCHANGE_RSA_PSK: |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 5094 | usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5095 | break; |
| 5096 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5097 | case MBEDTLS_KEY_EXCHANGE_DHE_RSA: |
| 5098 | case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA: |
| 5099 | case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA: |
| 5100 | usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5101 | break; |
| 5102 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5103 | case MBEDTLS_KEY_EXCHANGE_ECDH_RSA: |
| 5104 | case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA: |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 5105 | usage = MBEDTLS_X509_KU_KEY_AGREEMENT; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5106 | break; |
| 5107 | |
| 5108 | /* 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] | 5109 | case MBEDTLS_KEY_EXCHANGE_NONE: |
| 5110 | case MBEDTLS_KEY_EXCHANGE_PSK: |
| 5111 | case MBEDTLS_KEY_EXCHANGE_DHE_PSK: |
| 5112 | case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK: |
Manuel Pégourié-Gonnard | 557535d | 2015-09-15 17:53:32 +0200 | [diff] [blame] | 5113 | case MBEDTLS_KEY_EXCHANGE_ECJPAKE: |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5114 | usage = 0; |
| 5115 | } |
| 5116 | } |
| 5117 | else |
| 5118 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5119 | /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */ |
| 5120 | usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5121 | } |
| 5122 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5123 | if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 ) |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 5124 | { |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 5125 | *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE; |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 5126 | ret = -1; |
| 5127 | } |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5128 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5129 | if( cert_endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 5130 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5131 | ext_oid = MBEDTLS_OID_SERVER_AUTH; |
| 5132 | ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH ); |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 5133 | } |
| 5134 | else |
| 5135 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5136 | ext_oid = MBEDTLS_OID_CLIENT_AUTH; |
| 5137 | ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH ); |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 5138 | } |
| 5139 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5140 | 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] | 5141 | { |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 5142 | *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE; |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 5143 | ret = -1; |
| 5144 | } |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 5145 | |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 5146 | return( ret ); |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5147 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5148 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Manuel Pégourié-Gonnard | 3a306b9 | 2014-04-29 15:11:17 +0200 | [diff] [blame] | 5149 | |
Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 5150 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 5151 | int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl, |
| 5152 | const mbedtls_md_type_t md, |
| 5153 | unsigned char *dst, |
| 5154 | size_t dst_len, |
| 5155 | size_t *olen ) |
| 5156 | { |
Ronald Cron | f6893e1 | 2022-01-07 22:09:01 +0100 | [diff] [blame] | 5157 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 5158 | psa_hash_operation_t *hash_operation_to_clone; |
| 5159 | psa_hash_operation_t hash_operation = psa_hash_operation_init(); |
| 5160 | |
Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 5161 | *olen = 0; |
Ronald Cron | f6893e1 | 2022-01-07 22:09:01 +0100 | [diff] [blame] | 5162 | |
| 5163 | switch( md ) |
| 5164 | { |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5165 | #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] | 5166 | case MBEDTLS_MD_SHA384: |
| 5167 | hash_operation_to_clone = &ssl->handshake->fin_sha384_psa; |
| 5168 | break; |
| 5169 | #endif |
| 5170 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5171 | #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] | 5172 | case MBEDTLS_MD_SHA256: |
| 5173 | hash_operation_to_clone = &ssl->handshake->fin_sha256_psa; |
| 5174 | break; |
| 5175 | #endif |
| 5176 | |
| 5177 | default: |
| 5178 | goto exit; |
| 5179 | } |
| 5180 | |
| 5181 | status = psa_hash_clone( hash_operation_to_clone, &hash_operation ); |
| 5182 | if( status != PSA_SUCCESS ) |
| 5183 | goto exit; |
| 5184 | |
| 5185 | status = psa_hash_finish( &hash_operation, dst, dst_len, olen ); |
| 5186 | if( status != PSA_SUCCESS ) |
| 5187 | goto exit; |
| 5188 | |
| 5189 | exit: |
Andrzej Kurek | eabeb30 | 2022-10-17 07:52:51 -0400 | [diff] [blame] | 5190 | #if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \ |
| 5191 | !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
| 5192 | (void) ssl; |
| 5193 | #endif |
Ronald Cron | b788c04 | 2022-02-15 09:14:15 +0100 | [diff] [blame] | 5194 | return( psa_ssl_status_to_mbedtls( status ) ); |
Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 5195 | } |
| 5196 | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
| 5197 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5198 | #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] | 5199 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | 000f976 | 2021-09-14 11:12:51 +0800 | [diff] [blame] | 5200 | static int ssl_get_handshake_transcript_sha384( mbedtls_ssl_context *ssl, |
| 5201 | unsigned char *dst, |
| 5202 | size_t dst_len, |
| 5203 | size_t *olen ) |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5204 | { |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5205 | int ret; |
| 5206 | mbedtls_sha512_context sha512; |
| 5207 | |
| 5208 | if( dst_len < 48 ) |
| 5209 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 5210 | |
| 5211 | mbedtls_sha512_init( &sha512 ); |
Andrzej Kurek | a242e83 | 2022-08-11 10:03:14 -0400 | [diff] [blame] | 5212 | mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 ); |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5213 | |
| 5214 | if( ( ret = mbedtls_sha512_finish( &sha512, dst ) ) != 0 ) |
| 5215 | { |
| 5216 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha512_finish", ret ); |
| 5217 | goto exit; |
| 5218 | } |
| 5219 | |
| 5220 | *olen = 48; |
| 5221 | |
| 5222 | exit: |
| 5223 | |
| 5224 | mbedtls_sha512_free( &sha512 ); |
| 5225 | return( ret ); |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5226 | } |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5227 | #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] | 5228 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5229 | #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] | 5230 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | 000f976 | 2021-09-14 11:12:51 +0800 | [diff] [blame] | 5231 | static int ssl_get_handshake_transcript_sha256( mbedtls_ssl_context *ssl, |
| 5232 | unsigned char *dst, |
| 5233 | size_t dst_len, |
| 5234 | size_t *olen ) |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5235 | { |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5236 | int ret; |
| 5237 | mbedtls_sha256_context sha256; |
| 5238 | |
| 5239 | if( dst_len < 32 ) |
| 5240 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 5241 | |
| 5242 | mbedtls_sha256_init( &sha256 ); |
| 5243 | mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 ); |
Jerry Yu | c5aef88 | 2021-12-23 20:15:02 +0800 | [diff] [blame] | 5244 | |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5245 | if( ( ret = mbedtls_sha256_finish( &sha256, dst ) ) != 0 ) |
| 5246 | { |
| 5247 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha256_finish", ret ); |
| 5248 | goto exit; |
| 5249 | } |
| 5250 | |
| 5251 | *olen = 32; |
| 5252 | |
| 5253 | exit: |
| 5254 | |
| 5255 | mbedtls_sha256_free( &sha256 ); |
| 5256 | return( ret ); |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5257 | } |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5258 | #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] | 5259 | |
Jerry Yu | 000f976 | 2021-09-14 11:12:51 +0800 | [diff] [blame] | 5260 | int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl, |
| 5261 | const mbedtls_md_type_t md, |
| 5262 | unsigned char *dst, |
| 5263 | size_t dst_len, |
| 5264 | size_t *olen ) |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5265 | { |
Jerry Yu | c1ddeef | 2021-10-08 15:14:45 +0800 | [diff] [blame] | 5266 | switch( md ) |
| 5267 | { |
| 5268 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5269 | #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] | 5270 | case MBEDTLS_MD_SHA384: |
Jerry Yu | c5aef88 | 2021-12-23 20:15:02 +0800 | [diff] [blame] | 5271 | return( ssl_get_handshake_transcript_sha384( ssl, dst, dst_len, olen ) ); |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 5272 | #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] | 5273 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5274 | #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] | 5275 | case MBEDTLS_MD_SHA256: |
Jerry Yu | c5aef88 | 2021-12-23 20:15:02 +0800 | [diff] [blame] | 5276 | return( ssl_get_handshake_transcript_sha256( ssl, dst, dst_len, olen ) ); |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 5277 | #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] | 5278 | |
| 5279 | default: |
Andrzej Kurek | 409248a | 2022-10-24 10:33:21 -0400 | [diff] [blame] | 5280 | #if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \ |
| 5281 | !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
| 5282 | (void) ssl; |
| 5283 | (void) dst; |
| 5284 | (void) dst_len; |
| 5285 | (void) olen; |
| 5286 | #endif |
Jerry Yu | c1ddeef | 2021-10-08 15:14:45 +0800 | [diff] [blame] | 5287 | break; |
| 5288 | } |
Jerry Yu | c5aef88 | 2021-12-23 20:15:02 +0800 | [diff] [blame] | 5289 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5290 | } |
XiaokangQian | 647719a | 2021-12-07 09:16:29 +0000 | [diff] [blame] | 5291 | |
Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 5292 | #endif /* !MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5293 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5294 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 5295 | /* mbedtls_ssl_parse_sig_alg_ext() |
| 5296 | * |
| 5297 | * The `extension_data` field of signature algorithm contains a `SignatureSchemeList` |
| 5298 | * value (TLS 1.3 RFC8446): |
| 5299 | * enum { |
| 5300 | * .... |
| 5301 | * ecdsa_secp256r1_sha256( 0x0403 ), |
| 5302 | * ecdsa_secp384r1_sha384( 0x0503 ), |
| 5303 | * ecdsa_secp521r1_sha512( 0x0603 ), |
| 5304 | * .... |
| 5305 | * } SignatureScheme; |
| 5306 | * |
| 5307 | * struct { |
| 5308 | * SignatureScheme supported_signature_algorithms<2..2^16-2>; |
| 5309 | * } SignatureSchemeList; |
| 5310 | * |
| 5311 | * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm` |
| 5312 | * value (TLS 1.2 RFC5246): |
| 5313 | * enum { |
| 5314 | * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5), |
| 5315 | * sha512(6), (255) |
| 5316 | * } HashAlgorithm; |
| 5317 | * |
| 5318 | * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) } |
| 5319 | * SignatureAlgorithm; |
| 5320 | * |
| 5321 | * struct { |
| 5322 | * HashAlgorithm hash; |
| 5323 | * SignatureAlgorithm signature; |
| 5324 | * } SignatureAndHashAlgorithm; |
| 5325 | * |
| 5326 | * SignatureAndHashAlgorithm |
| 5327 | * supported_signature_algorithms<2..2^16-2>; |
| 5328 | * |
| 5329 | * The TLS 1.3 signature algorithm extension was defined to be a compatible |
| 5330 | * generalization of the TLS 1.2 signature algorithm extension. |
| 5331 | * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by |
| 5332 | * `SignatureScheme` field of TLS 1.3 |
| 5333 | * |
| 5334 | */ |
| 5335 | int mbedtls_ssl_parse_sig_alg_ext( mbedtls_ssl_context *ssl, |
| 5336 | const unsigned char *buf, |
| 5337 | const unsigned char *end ) |
| 5338 | { |
| 5339 | const unsigned char *p = buf; |
| 5340 | size_t supported_sig_algs_len = 0; |
| 5341 | const unsigned char *supported_sig_algs_end; |
| 5342 | uint16_t sig_alg; |
| 5343 | uint32_t common_idx = 0; |
| 5344 | |
| 5345 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 ); |
| 5346 | supported_sig_algs_len = MBEDTLS_GET_UINT16_BE( p, 0 ); |
| 5347 | p += 2; |
| 5348 | |
| 5349 | memset( ssl->handshake->received_sig_algs, 0, |
| 5350 | sizeof(ssl->handshake->received_sig_algs) ); |
| 5351 | |
| 5352 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, supported_sig_algs_len ); |
| 5353 | supported_sig_algs_end = p + supported_sig_algs_len; |
| 5354 | while( p < supported_sig_algs_end ) |
| 5355 | { |
| 5356 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, supported_sig_algs_end, 2 ); |
| 5357 | sig_alg = MBEDTLS_GET_UINT16_BE( p, 0 ); |
| 5358 | p += 2; |
Jerry Yu | f3b46b5 | 2022-06-19 16:52:27 +0800 | [diff] [blame] | 5359 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "received signature algorithm: 0x%x %s", |
| 5360 | sig_alg, |
| 5361 | mbedtls_ssl_sig_alg_to_str( sig_alg ) ) ); |
Jerry Yu | 2fe6c63 | 2022-06-29 10:02:38 +0800 | [diff] [blame] | 5362 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Jerry Yu | 52b7d92 | 2022-07-01 18:03:31 +0800 | [diff] [blame] | 5363 | if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 && |
Jerry Yu | 2fe6c63 | 2022-06-29 10:02:38 +0800 | [diff] [blame] | 5364 | ( ! ( mbedtls_ssl_sig_alg_is_supported( ssl, sig_alg ) && |
| 5365 | mbedtls_ssl_sig_alg_is_offered( ssl, sig_alg ) ) ) ) |
| 5366 | { |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 5367 | continue; |
Jerry Yu | 2fe6c63 | 2022-06-29 10:02:38 +0800 | [diff] [blame] | 5368 | } |
| 5369 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 5370 | |
Jerry Yu | f3b46b5 | 2022-06-19 16:52:27 +0800 | [diff] [blame] | 5371 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "valid signature algorithm: %s", |
| 5372 | mbedtls_ssl_sig_alg_to_str( sig_alg ) ) ); |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 5373 | |
| 5374 | if( common_idx + 1 < MBEDTLS_RECEIVED_SIG_ALGS_SIZE ) |
| 5375 | { |
| 5376 | ssl->handshake->received_sig_algs[common_idx] = sig_alg; |
| 5377 | common_idx += 1; |
| 5378 | } |
| 5379 | } |
| 5380 | /* Check that we consumed all the message. */ |
| 5381 | if( p != end ) |
| 5382 | { |
| 5383 | MBEDTLS_SSL_DEBUG_MSG( 1, |
| 5384 | ( "Signature algorithms extension length misaligned" ) ); |
| 5385 | MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR, |
| 5386 | MBEDTLS_ERR_SSL_DECODE_ERROR ); |
| 5387 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
| 5388 | } |
| 5389 | |
| 5390 | if( common_idx == 0 ) |
| 5391 | { |
| 5392 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "no signature algorithm in common" ) ); |
| 5393 | MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE, |
| 5394 | MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE ); |
| 5395 | return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE ); |
| 5396 | } |
| 5397 | |
Gabor Mezei | 15b95a6 | 2022-05-09 16:37:58 +0200 | [diff] [blame] | 5398 | ssl->handshake->received_sig_algs[common_idx] = MBEDTLS_TLS_SIG_NONE; |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 5399 | return( 0 ); |
| 5400 | } |
| 5401 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5402 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 5403 | |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5404 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 5405 | |
| 5406 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 5407 | |
| 5408 | static psa_status_t setup_psa_key_derivation( psa_key_derivation_operation_t* derivation, |
| 5409 | mbedtls_svc_key_id_t key, |
| 5410 | psa_algorithm_t alg, |
Neil Armstrong | 8ecd668 | 2022-05-05 11:40:35 +0200 | [diff] [blame] | 5411 | const unsigned char* raw_psk, size_t raw_psk_length, |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5412 | const unsigned char* seed, size_t seed_length, |
| 5413 | const unsigned char* label, size_t label_length, |
Przemek Stekiel | 51a1f36 | 2022-04-13 08:57:06 +0200 | [diff] [blame] | 5414 | const unsigned char* other_secret, |
| 5415 | size_t other_secret_length, |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5416 | size_t capacity ) |
| 5417 | { |
| 5418 | psa_status_t status; |
| 5419 | |
| 5420 | status = psa_key_derivation_setup( derivation, alg ); |
| 5421 | if( status != PSA_SUCCESS ) |
| 5422 | return( status ); |
| 5423 | |
| 5424 | if( PSA_ALG_IS_TLS12_PRF( alg ) || PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) ) |
| 5425 | { |
| 5426 | status = psa_key_derivation_input_bytes( derivation, |
| 5427 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 5428 | seed, seed_length ); |
| 5429 | if( status != PSA_SUCCESS ) |
| 5430 | return( status ); |
| 5431 | |
Przemek Stekiel | 51a1f36 | 2022-04-13 08:57:06 +0200 | [diff] [blame] | 5432 | if ( other_secret != NULL ) |
Przemek Stekiel | 1f02703 | 2022-04-05 17:12:11 +0200 | [diff] [blame] | 5433 | { |
| 5434 | status = psa_key_derivation_input_bytes( derivation, |
Przemek Stekiel | 51a1f36 | 2022-04-13 08:57:06 +0200 | [diff] [blame] | 5435 | PSA_KEY_DERIVATION_INPUT_OTHER_SECRET, |
| 5436 | other_secret, other_secret_length ); |
Przemek Stekiel | 1f02703 | 2022-04-05 17:12:11 +0200 | [diff] [blame] | 5437 | if( status != PSA_SUCCESS ) |
| 5438 | return( status ); |
| 5439 | } |
| 5440 | |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5441 | if( mbedtls_svc_key_id_is_null( key ) ) |
| 5442 | { |
| 5443 | status = psa_key_derivation_input_bytes( |
| 5444 | derivation, PSA_KEY_DERIVATION_INPUT_SECRET, |
Neil Armstrong | 8ecd668 | 2022-05-05 11:40:35 +0200 | [diff] [blame] | 5445 | raw_psk, raw_psk_length ); |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5446 | } |
| 5447 | else |
| 5448 | { |
| 5449 | status = psa_key_derivation_input_key( |
| 5450 | derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key ); |
| 5451 | } |
| 5452 | if( status != PSA_SUCCESS ) |
| 5453 | return( status ); |
| 5454 | |
| 5455 | status = psa_key_derivation_input_bytes( derivation, |
| 5456 | PSA_KEY_DERIVATION_INPUT_LABEL, |
| 5457 | label, label_length ); |
| 5458 | if( status != PSA_SUCCESS ) |
| 5459 | return( status ); |
| 5460 | } |
| 5461 | else |
| 5462 | { |
| 5463 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5464 | } |
| 5465 | |
| 5466 | status = psa_key_derivation_set_capacity( derivation, capacity ); |
| 5467 | if( status != PSA_SUCCESS ) |
| 5468 | return( status ); |
| 5469 | |
| 5470 | return( PSA_SUCCESS ); |
| 5471 | } |
| 5472 | |
Andrzej Kurek | 57d1063 | 2022-10-24 10:32:01 -0400 | [diff] [blame] | 5473 | #if defined(PSA_WANT_ALG_SHA_384) || \ |
| 5474 | defined(PSA_WANT_ALG_SHA_256) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 5475 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5476 | static int tls_prf_generic( mbedtls_md_type_t md_type, |
| 5477 | const unsigned char *secret, size_t slen, |
| 5478 | const char *label, |
| 5479 | const unsigned char *random, size_t rlen, |
| 5480 | unsigned char *dstbuf, size_t dlen ) |
| 5481 | { |
| 5482 | psa_status_t status; |
| 5483 | psa_algorithm_t alg; |
| 5484 | mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5485 | psa_key_derivation_operation_t derivation = |
| 5486 | PSA_KEY_DERIVATION_OPERATION_INIT; |
| 5487 | |
| 5488 | if( md_type == MBEDTLS_MD_SHA384 ) |
| 5489 | alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384); |
| 5490 | else |
| 5491 | alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256); |
| 5492 | |
| 5493 | /* Normally a "secret" should be long enough to be impossible to |
| 5494 | * find by brute force, and in particular should not be empty. But |
| 5495 | * this PRF is also used to derive an IV, in particular in EAP-TLS, |
| 5496 | * and for this use case it makes sense to have a 0-length "secret". |
| 5497 | * Since the key API doesn't allow importing a key of length 0, |
| 5498 | * keep master_key=0, which setup_psa_key_derivation() understands |
| 5499 | * to mean a 0-length "secret" input. */ |
| 5500 | if( slen != 0 ) |
| 5501 | { |
| 5502 | psa_key_attributes_t key_attributes = psa_key_attributes_init(); |
| 5503 | psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE ); |
| 5504 | psa_set_key_algorithm( &key_attributes, alg ); |
| 5505 | psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE ); |
| 5506 | |
| 5507 | status = psa_import_key( &key_attributes, secret, slen, &master_key ); |
| 5508 | if( status != PSA_SUCCESS ) |
| 5509 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 5510 | } |
| 5511 | |
| 5512 | status = setup_psa_key_derivation( &derivation, |
| 5513 | master_key, alg, |
Neil Armstrong | 8ecd668 | 2022-05-05 11:40:35 +0200 | [diff] [blame] | 5514 | NULL, 0, |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5515 | random, rlen, |
| 5516 | (unsigned char const *) label, |
| 5517 | (size_t) strlen( label ), |
Przemek Stekiel | 1f02703 | 2022-04-05 17:12:11 +0200 | [diff] [blame] | 5518 | NULL, 0, |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5519 | dlen ); |
| 5520 | if( status != PSA_SUCCESS ) |
| 5521 | { |
| 5522 | psa_key_derivation_abort( &derivation ); |
| 5523 | psa_destroy_key( master_key ); |
| 5524 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 5525 | } |
| 5526 | |
| 5527 | status = psa_key_derivation_output_bytes( &derivation, dstbuf, dlen ); |
| 5528 | if( status != PSA_SUCCESS ) |
| 5529 | { |
| 5530 | psa_key_derivation_abort( &derivation ); |
| 5531 | psa_destroy_key( master_key ); |
| 5532 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 5533 | } |
| 5534 | |
| 5535 | status = psa_key_derivation_abort( &derivation ); |
| 5536 | if( status != PSA_SUCCESS ) |
| 5537 | { |
| 5538 | psa_destroy_key( master_key ); |
| 5539 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 5540 | } |
| 5541 | |
| 5542 | if( ! mbedtls_svc_key_id_is_null( master_key ) ) |
| 5543 | status = psa_destroy_key( master_key ); |
| 5544 | if( status != PSA_SUCCESS ) |
| 5545 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 5546 | |
| 5547 | return( 0 ); |
| 5548 | } |
Andrzej Kurek | 57d1063 | 2022-10-24 10:32:01 -0400 | [diff] [blame] | 5549 | #endif /* PSA_WANT_ALG_SHA_256 || PSA_WANT_ALG_SHA_384 */ |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5550 | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
| 5551 | |
Andrzej Kurek | 57d1063 | 2022-10-24 10:32:01 -0400 | [diff] [blame] | 5552 | #if defined(MBEDTLS_MD_C) && \ |
| 5553 | ( defined(MBEDTLS_SHA256_C) || \ |
| 5554 | defined(MBEDTLS_SHA384_C) ) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 5555 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5556 | static int tls_prf_generic( mbedtls_md_type_t md_type, |
| 5557 | const unsigned char *secret, size_t slen, |
| 5558 | const char *label, |
| 5559 | const unsigned char *random, size_t rlen, |
| 5560 | unsigned char *dstbuf, size_t dlen ) |
| 5561 | { |
| 5562 | size_t nb; |
| 5563 | size_t i, j, k, md_len; |
| 5564 | unsigned char *tmp; |
| 5565 | size_t tmp_len = 0; |
| 5566 | unsigned char h_i[MBEDTLS_MD_MAX_SIZE]; |
| 5567 | const mbedtls_md_info_t *md_info; |
| 5568 | mbedtls_md_context_t md_ctx; |
| 5569 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 5570 | |
| 5571 | mbedtls_md_init( &md_ctx ); |
| 5572 | |
| 5573 | if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL ) |
| 5574 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 5575 | |
| 5576 | md_len = mbedtls_md_get_size( md_info ); |
| 5577 | |
| 5578 | tmp_len = md_len + strlen( label ) + rlen; |
| 5579 | tmp = mbedtls_calloc( 1, tmp_len ); |
| 5580 | if( tmp == NULL ) |
| 5581 | { |
| 5582 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 5583 | goto exit; |
| 5584 | } |
| 5585 | |
| 5586 | nb = strlen( label ); |
| 5587 | memcpy( tmp + md_len, label, nb ); |
| 5588 | memcpy( tmp + md_len + nb, random, rlen ); |
| 5589 | nb += rlen; |
| 5590 | |
| 5591 | /* |
| 5592 | * Compute P_<hash>(secret, label + random)[0..dlen] |
| 5593 | */ |
| 5594 | if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 ) |
| 5595 | goto exit; |
| 5596 | |
| 5597 | ret = mbedtls_md_hmac_starts( &md_ctx, secret, slen ); |
| 5598 | if( ret != 0 ) |
| 5599 | goto exit; |
| 5600 | ret = mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb ); |
| 5601 | if( ret != 0 ) |
| 5602 | goto exit; |
| 5603 | ret = mbedtls_md_hmac_finish( &md_ctx, tmp ); |
| 5604 | if( ret != 0 ) |
| 5605 | goto exit; |
| 5606 | |
| 5607 | for( i = 0; i < dlen; i += md_len ) |
| 5608 | { |
| 5609 | ret = mbedtls_md_hmac_reset ( &md_ctx ); |
| 5610 | if( ret != 0 ) |
| 5611 | goto exit; |
| 5612 | ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb ); |
| 5613 | if( ret != 0 ) |
| 5614 | goto exit; |
| 5615 | ret = mbedtls_md_hmac_finish( &md_ctx, h_i ); |
| 5616 | if( ret != 0 ) |
| 5617 | goto exit; |
| 5618 | |
| 5619 | ret = mbedtls_md_hmac_reset ( &md_ctx ); |
| 5620 | if( ret != 0 ) |
| 5621 | goto exit; |
| 5622 | ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len ); |
| 5623 | if( ret != 0 ) |
| 5624 | goto exit; |
| 5625 | ret = mbedtls_md_hmac_finish( &md_ctx, tmp ); |
| 5626 | if( ret != 0 ) |
| 5627 | goto exit; |
| 5628 | |
| 5629 | k = ( i + md_len > dlen ) ? dlen % md_len : md_len; |
| 5630 | |
| 5631 | for( j = 0; j < k; j++ ) |
| 5632 | dstbuf[i + j] = h_i[j]; |
| 5633 | } |
| 5634 | |
| 5635 | exit: |
| 5636 | mbedtls_md_free( &md_ctx ); |
| 5637 | |
| 5638 | mbedtls_platform_zeroize( tmp, tmp_len ); |
| 5639 | mbedtls_platform_zeroize( h_i, sizeof( h_i ) ); |
| 5640 | |
| 5641 | mbedtls_free( tmp ); |
| 5642 | |
| 5643 | return( ret ); |
| 5644 | } |
Andrzej Kurek | 57d1063 | 2022-10-24 10:32:01 -0400 | [diff] [blame] | 5645 | #endif /* MBEDTLS_MD_C && ( MBEDTLS_SHA256_C || MBEDTLS_SHA384_C ) */ |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5646 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 5647 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5648 | #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] | 5649 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5650 | static int tls_prf_sha256( const unsigned char *secret, size_t slen, |
| 5651 | const char *label, |
| 5652 | const unsigned char *random, size_t rlen, |
| 5653 | unsigned char *dstbuf, size_t dlen ) |
| 5654 | { |
| 5655 | return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen, |
| 5656 | label, random, rlen, dstbuf, dlen ) ); |
| 5657 | } |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 5658 | #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] | 5659 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5660 | #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] | 5661 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5662 | static int tls_prf_sha384( const unsigned char *secret, size_t slen, |
| 5663 | const char *label, |
| 5664 | const unsigned char *random, size_t rlen, |
| 5665 | unsigned char *dstbuf, size_t dlen ) |
| 5666 | { |
| 5667 | return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen, |
| 5668 | label, random, rlen, dstbuf, dlen ) ); |
| 5669 | } |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 5670 | #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] | 5671 | |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5672 | /* |
| 5673 | * Set appropriate PRF function and other SSL / TLS1.2 functions |
| 5674 | * |
| 5675 | * Inputs: |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5676 | * - hash associated with the ciphersuite (only used by TLS 1.2) |
| 5677 | * |
| 5678 | * Outputs: |
| 5679 | * - the tls_prf, calc_verify and calc_finished members of handshake structure |
| 5680 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 5681 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5682 | static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake, |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5683 | mbedtls_md_type_t hash ) |
| 5684 | { |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5685 | #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] | 5686 | if( hash == MBEDTLS_MD_SHA384 ) |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5687 | { |
| 5688 | handshake->tls_prf = tls_prf_sha384; |
| 5689 | handshake->calc_verify = ssl_calc_verify_tls_sha384; |
| 5690 | handshake->calc_finished = ssl_calc_finished_tls_sha384; |
| 5691 | } |
| 5692 | else |
| 5693 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5694 | #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] | 5695 | { |
Ronald Cron | 81591aa | 2022-03-07 09:05:51 +0100 | [diff] [blame] | 5696 | (void) hash; |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5697 | handshake->tls_prf = tls_prf_sha256; |
| 5698 | handshake->calc_verify = ssl_calc_verify_tls_sha256; |
| 5699 | handshake->calc_finished = ssl_calc_finished_tls_sha256; |
| 5700 | } |
Ronald Cron | 81591aa | 2022-03-07 09:05:51 +0100 | [diff] [blame] | 5701 | #else |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5702 | { |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5703 | (void) handshake; |
Ronald Cron | 81591aa | 2022-03-07 09:05:51 +0100 | [diff] [blame] | 5704 | (void) hash; |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5705 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 5706 | } |
Ronald Cron | 81591aa | 2022-03-07 09:05:51 +0100 | [diff] [blame] | 5707 | #endif |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5708 | |
| 5709 | return( 0 ); |
| 5710 | } |
Jerry Yu | d6ab235 | 2022-02-17 14:03:43 +0800 | [diff] [blame] | 5711 | |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5712 | /* |
| 5713 | * Compute master secret if needed |
| 5714 | * |
| 5715 | * Parameters: |
| 5716 | * [in/out] handshake |
| 5717 | * [in] resume, premaster, extended_ms, calc_verify, tls_prf |
| 5718 | * (PSA-PSK) ciphersuite_info, psk_opaque |
| 5719 | * [out] premaster (cleared) |
| 5720 | * [out] master |
| 5721 | * [in] ssl: optionally used for debugging, EMS and PSA-PSK |
| 5722 | * debug: conf->f_dbg, conf->p_dbg |
| 5723 | * EMS: passed to calc_verify (debug + session_negotiate) |
Ronald Cron | a25cf58 | 2022-03-07 11:10:36 +0100 | [diff] [blame] | 5724 | * PSA-PSA: conf |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5725 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 5726 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5727 | static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake, |
| 5728 | unsigned char *master, |
| 5729 | const mbedtls_ssl_context *ssl ) |
| 5730 | { |
| 5731 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 5732 | |
| 5733 | /* cf. RFC 5246, Section 8.1: |
| 5734 | * "The master secret is always exactly 48 bytes in length." */ |
| 5735 | size_t const master_secret_len = 48; |
| 5736 | |
| 5737 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 5738 | unsigned char session_hash[48]; |
| 5739 | #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */ |
| 5740 | |
| 5741 | /* The label for the KDF used for key expansion. |
| 5742 | * This is either "master secret" or "extended master secret" |
| 5743 | * depending on whether the Extended Master Secret extension |
| 5744 | * is used. */ |
| 5745 | char const *lbl = "master secret"; |
| 5746 | |
Przemek Stekiel | ae4ed30 | 2022-04-05 17:15:55 +0200 | [diff] [blame] | 5747 | /* The seed for the KDF used for key expansion. |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5748 | * - If the Extended Master Secret extension is not used, |
| 5749 | * this is ClientHello.Random + ServerHello.Random |
| 5750 | * (see Sect. 8.1 in RFC 5246). |
| 5751 | * - If the Extended Master Secret extension is used, |
| 5752 | * this is the transcript of the handshake so far. |
| 5753 | * (see Sect. 4 in RFC 7627). */ |
Przemek Stekiel | ae4ed30 | 2022-04-05 17:15:55 +0200 | [diff] [blame] | 5754 | unsigned char const *seed = handshake->randbytes; |
| 5755 | size_t seed_len = 64; |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5756 | |
| 5757 | #if !defined(MBEDTLS_DEBUG_C) && \ |
| 5758 | !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \ |
| 5759 | !(defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
| 5760 | defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)) |
| 5761 | ssl = NULL; /* make sure we don't use it except for those cases */ |
| 5762 | (void) ssl; |
| 5763 | #endif |
| 5764 | |
| 5765 | if( handshake->resume != 0 ) |
| 5766 | { |
| 5767 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) ); |
| 5768 | return( 0 ); |
| 5769 | } |
| 5770 | |
| 5771 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 5772 | if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED ) |
| 5773 | { |
| 5774 | lbl = "extended master secret"; |
Przemek Stekiel | ae4ed30 | 2022-04-05 17:15:55 +0200 | [diff] [blame] | 5775 | seed = session_hash; |
| 5776 | handshake->calc_verify( ssl, session_hash, &seed_len ); |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5777 | |
| 5778 | MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret", |
Przemek Stekiel | ae4ed30 | 2022-04-05 17:15:55 +0200 | [diff] [blame] | 5779 | session_hash, seed_len ); |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5780 | } |
Przemek Stekiel | 169bf0b | 2022-04-29 07:53:29 +0200 | [diff] [blame] | 5781 | #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */ |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5782 | |
Przemek Stekiel | 99114f3 | 2022-04-22 11:20:09 +0200 | [diff] [blame] | 5783 | #if defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
Przemek Stekiel | 8a4b7fd | 2022-04-28 09:22:22 +0200 | [diff] [blame] | 5784 | defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Neil Armstrong | e952a30 | 2022-05-03 10:22:14 +0200 | [diff] [blame] | 5785 | if( mbedtls_ssl_ciphersuite_uses_psk( handshake->ciphersuite_info ) == 1 ) |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5786 | { |
| 5787 | /* Perform PSK-to-MS expansion in a single step. */ |
| 5788 | psa_status_t status; |
| 5789 | psa_algorithm_t alg; |
| 5790 | mbedtls_svc_key_id_t psk; |
| 5791 | psa_key_derivation_operation_t derivation = |
| 5792 | PSA_KEY_DERIVATION_OPERATION_INIT; |
| 5793 | mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac; |
| 5794 | |
| 5795 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) ); |
| 5796 | |
| 5797 | psk = mbedtls_ssl_get_opaque_psk( ssl ); |
| 5798 | |
| 5799 | if( hash_alg == MBEDTLS_MD_SHA384 ) |
| 5800 | alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384); |
| 5801 | else |
| 5802 | alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256); |
| 5803 | |
Przemek Stekiel | 51a1f36 | 2022-04-13 08:57:06 +0200 | [diff] [blame] | 5804 | size_t other_secret_len = 0; |
| 5805 | unsigned char* other_secret = NULL; |
Przemek Stekiel | c203340 | 2022-04-05 17:19:41 +0200 | [diff] [blame] | 5806 | |
Przemek Stekiel | 19b80f8 | 2022-04-14 08:29:31 +0200 | [diff] [blame] | 5807 | switch( handshake->ciphersuite_info->key_exchange ) |
Przemek Stekiel | c203340 | 2022-04-05 17:19:41 +0200 | [diff] [blame] | 5808 | { |
Przemek Stekiel | 19b80f8 | 2022-04-14 08:29:31 +0200 | [diff] [blame] | 5809 | /* Provide other secret. |
Przemek Stekiel | 51a1f36 | 2022-04-13 08:57:06 +0200 | [diff] [blame] | 5810 | * Other secret is stored in premaster, where first 2 bytes hold the |
Przemek Stekiel | 19b80f8 | 2022-04-14 08:29:31 +0200 | [diff] [blame] | 5811 | * length of the other key. |
Przemek Stekiel | c203340 | 2022-04-05 17:19:41 +0200 | [diff] [blame] | 5812 | */ |
Przemek Stekiel | 19b80f8 | 2022-04-14 08:29:31 +0200 | [diff] [blame] | 5813 | case MBEDTLS_KEY_EXCHANGE_RSA_PSK: |
Przemek Stekiel | 8abcee9 | 2022-04-28 09:16:28 +0200 | [diff] [blame] | 5814 | /* For RSA-PSK other key length is always 48 bytes. */ |
Przemek Stekiel | 19b80f8 | 2022-04-14 08:29:31 +0200 | [diff] [blame] | 5815 | other_secret_len = 48; |
| 5816 | other_secret = handshake->premaster + 2; |
| 5817 | break; |
| 5818 | case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK: |
Przemek Stekiel | b293aaa | 2022-04-19 12:22:38 +0200 | [diff] [blame] | 5819 | case MBEDTLS_KEY_EXCHANGE_DHE_PSK: |
Przemek Stekiel | 19b80f8 | 2022-04-14 08:29:31 +0200 | [diff] [blame] | 5820 | other_secret_len = MBEDTLS_GET_UINT16_BE(handshake->premaster, 0); |
| 5821 | other_secret = handshake->premaster + 2; |
| 5822 | break; |
| 5823 | default: |
| 5824 | break; |
Przemek Stekiel | c203340 | 2022-04-05 17:19:41 +0200 | [diff] [blame] | 5825 | } |
| 5826 | |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5827 | status = setup_psa_key_derivation( &derivation, psk, alg, |
Neil Armstrong | 8ecd668 | 2022-05-05 11:40:35 +0200 | [diff] [blame] | 5828 | ssl->conf->psk, ssl->conf->psk_len, |
Przemek Stekiel | ae4ed30 | 2022-04-05 17:15:55 +0200 | [diff] [blame] | 5829 | seed, seed_len, |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5830 | (unsigned char const *) lbl, |
| 5831 | (size_t) strlen( lbl ), |
Przemek Stekiel | 51a1f36 | 2022-04-13 08:57:06 +0200 | [diff] [blame] | 5832 | other_secret, other_secret_len, |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5833 | master_secret_len ); |
| 5834 | if( status != PSA_SUCCESS ) |
| 5835 | { |
| 5836 | psa_key_derivation_abort( &derivation ); |
| 5837 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 5838 | } |
| 5839 | |
| 5840 | status = psa_key_derivation_output_bytes( &derivation, |
| 5841 | master, |
| 5842 | master_secret_len ); |
| 5843 | if( status != PSA_SUCCESS ) |
| 5844 | { |
| 5845 | psa_key_derivation_abort( &derivation ); |
| 5846 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 5847 | } |
| 5848 | |
| 5849 | status = psa_key_derivation_abort( &derivation ); |
| 5850 | if( status != PSA_SUCCESS ) |
| 5851 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 5852 | } |
| 5853 | else |
| 5854 | #endif |
| 5855 | { |
| 5856 | ret = handshake->tls_prf( handshake->premaster, handshake->pmslen, |
Przemek Stekiel | ae4ed30 | 2022-04-05 17:15:55 +0200 | [diff] [blame] | 5857 | lbl, seed, seed_len, |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5858 | master, |
| 5859 | master_secret_len ); |
| 5860 | if( ret != 0 ) |
| 5861 | { |
| 5862 | MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret ); |
| 5863 | return( ret ); |
| 5864 | } |
| 5865 | |
| 5866 | MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", |
| 5867 | handshake->premaster, |
| 5868 | handshake->pmslen ); |
| 5869 | |
| 5870 | mbedtls_platform_zeroize( handshake->premaster, |
| 5871 | sizeof(handshake->premaster) ); |
| 5872 | } |
| 5873 | |
| 5874 | return( 0 ); |
| 5875 | } |
| 5876 | |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 5877 | int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl ) |
| 5878 | { |
| 5879 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 5880 | const mbedtls_ssl_ciphersuite_t * const ciphersuite_info = |
| 5881 | ssl->handshake->ciphersuite_info; |
| 5882 | |
| 5883 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) ); |
| 5884 | |
| 5885 | /* Set PRF, calc_verify and calc_finished function pointers */ |
| 5886 | ret = ssl_set_handshake_prfs( ssl->handshake, |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 5887 | ciphersuite_info->mac ); |
| 5888 | if( ret != 0 ) |
| 5889 | { |
| 5890 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret ); |
| 5891 | return( ret ); |
| 5892 | } |
| 5893 | |
| 5894 | /* Compute master secret if needed */ |
| 5895 | ret = ssl_compute_master( ssl->handshake, |
| 5896 | ssl->session_negotiate->master, |
| 5897 | ssl ); |
| 5898 | if( ret != 0 ) |
| 5899 | { |
| 5900 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret ); |
| 5901 | return( ret ); |
| 5902 | } |
| 5903 | |
| 5904 | /* Swap the client and server random values: |
| 5905 | * - MS derivation wanted client+server (RFC 5246 8.1) |
| 5906 | * - key derivation wants server+client (RFC 5246 6.3) */ |
| 5907 | { |
| 5908 | unsigned char tmp[64]; |
| 5909 | memcpy( tmp, ssl->handshake->randbytes, 64 ); |
| 5910 | memcpy( ssl->handshake->randbytes, tmp + 32, 32 ); |
| 5911 | memcpy( ssl->handshake->randbytes + 32, tmp, 32 ); |
| 5912 | mbedtls_platform_zeroize( tmp, sizeof( tmp ) ); |
| 5913 | } |
| 5914 | |
| 5915 | /* Populate transform structure */ |
| 5916 | ret = ssl_tls12_populate_transform( ssl->transform_negotiate, |
| 5917 | ssl->session_negotiate->ciphersuite, |
| 5918 | ssl->session_negotiate->master, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 5919 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 5920 | ssl->session_negotiate->encrypt_then_mac, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 5921 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 5922 | ssl->handshake->tls_prf, |
| 5923 | ssl->handshake->randbytes, |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 5924 | ssl->tls_version, |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 5925 | ssl->conf->endpoint, |
| 5926 | ssl ); |
| 5927 | if( ret != 0 ) |
| 5928 | { |
| 5929 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls12_populate_transform", ret ); |
| 5930 | return( ret ); |
| 5931 | } |
| 5932 | |
| 5933 | /* We no longer need Server/ClientHello.random values */ |
| 5934 | mbedtls_platform_zeroize( ssl->handshake->randbytes, |
| 5935 | sizeof( ssl->handshake->randbytes ) ); |
| 5936 | |
| 5937 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) ); |
| 5938 | |
| 5939 | return( 0 ); |
| 5940 | } |
Jerry Yu | 8392e0d | 2022-02-17 14:10:24 +0800 | [diff] [blame] | 5941 | |
Ronald Cron | 4dcbca9 | 2022-03-07 10:21:40 +0100 | [diff] [blame] | 5942 | int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md ) |
| 5943 | { |
Ronald Cron | 4dcbca9 | 2022-03-07 10:21:40 +0100 | [diff] [blame] | 5944 | switch( md ) |
| 5945 | { |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5946 | #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] | 5947 | case MBEDTLS_SSL_HASH_SHA384: |
| 5948 | ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384; |
| 5949 | break; |
| 5950 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5951 | #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] | 5952 | case MBEDTLS_SSL_HASH_SHA256: |
| 5953 | ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256; |
| 5954 | break; |
| 5955 | #endif |
| 5956 | default: |
| 5957 | return( -1 ); |
| 5958 | } |
Andrzej Kurek | eabeb30 | 2022-10-17 07:52:51 -0400 | [diff] [blame] | 5959 | #if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \ |
| 5960 | !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
| 5961 | (void) ssl; |
| 5962 | #endif |
Ronald Cron | c2f13a0 | 2022-03-07 10:25:24 +0100 | [diff] [blame] | 5963 | return( 0 ); |
Ronald Cron | 4dcbca9 | 2022-03-07 10:21:40 +0100 | [diff] [blame] | 5964 | } |
| 5965 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5966 | #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] | 5967 | void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl, |
| 5968 | unsigned char *hash, |
| 5969 | size_t *hlen ) |
| 5970 | { |
| 5971 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 5972 | size_t hash_size; |
| 5973 | psa_status_t status; |
| 5974 | psa_hash_operation_t sha256_psa = psa_hash_operation_init(); |
| 5975 | |
| 5976 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) ); |
| 5977 | status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa ); |
| 5978 | if( status != PSA_SUCCESS ) |
| 5979 | { |
| 5980 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); |
| 5981 | return; |
| 5982 | } |
| 5983 | |
| 5984 | status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size ); |
| 5985 | if( status != PSA_SUCCESS ) |
| 5986 | { |
| 5987 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); |
| 5988 | return; |
| 5989 | } |
| 5990 | |
| 5991 | *hlen = 32; |
| 5992 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen ); |
| 5993 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) ); |
| 5994 | #else |
| 5995 | mbedtls_sha256_context sha256; |
| 5996 | |
| 5997 | mbedtls_sha256_init( &sha256 ); |
| 5998 | |
| 5999 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) ); |
| 6000 | |
| 6001 | mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 ); |
| 6002 | mbedtls_sha256_finish( &sha256, hash ); |
| 6003 | |
| 6004 | *hlen = 32; |
| 6005 | |
| 6006 | MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen ); |
| 6007 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) ); |
| 6008 | |
| 6009 | mbedtls_sha256_free( &sha256 ); |
| 6010 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 6011 | return; |
| 6012 | } |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 6013 | #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] | 6014 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 6015 | #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] | 6016 | void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl, |
| 6017 | unsigned char *hash, |
| 6018 | size_t *hlen ) |
| 6019 | { |
| 6020 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 6021 | size_t hash_size; |
| 6022 | psa_status_t status; |
| 6023 | psa_hash_operation_t sha384_psa = psa_hash_operation_init(); |
| 6024 | |
| 6025 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) ); |
| 6026 | status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa ); |
| 6027 | if( status != PSA_SUCCESS ) |
| 6028 | { |
| 6029 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); |
| 6030 | return; |
| 6031 | } |
| 6032 | |
| 6033 | status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size ); |
| 6034 | if( status != PSA_SUCCESS ) |
| 6035 | { |
| 6036 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); |
| 6037 | return; |
| 6038 | } |
| 6039 | |
| 6040 | *hlen = 48; |
| 6041 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen ); |
| 6042 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) ); |
| 6043 | #else |
| 6044 | mbedtls_sha512_context sha512; |
| 6045 | |
| 6046 | mbedtls_sha512_init( &sha512 ); |
| 6047 | |
| 6048 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) ); |
| 6049 | |
Andrzej Kurek | a242e83 | 2022-08-11 10:03:14 -0400 | [diff] [blame] | 6050 | mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 ); |
Jerry Yu | c1cb384 | 2022-02-17 14:13:48 +0800 | [diff] [blame] | 6051 | mbedtls_sha512_finish( &sha512, hash ); |
| 6052 | |
| 6053 | *hlen = 48; |
| 6054 | |
| 6055 | MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen ); |
| 6056 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) ); |
| 6057 | |
| 6058 | mbedtls_sha512_free( &sha512 ); |
| 6059 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 6060 | return; |
| 6061 | } |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 6062 | #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] | 6063 | |
Neil Armstrong | 80f6f32 | 2022-05-03 17:56:38 +0200 | [diff] [blame] | 6064 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
| 6065 | defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6066 | int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex ) |
| 6067 | { |
| 6068 | unsigned char *p = ssl->handshake->premaster; |
| 6069 | unsigned char *end = p + sizeof( ssl->handshake->premaster ); |
| 6070 | const unsigned char *psk = NULL; |
| 6071 | size_t psk_len = 0; |
Przemek Stekiel | b293aaa | 2022-04-19 12:22:38 +0200 | [diff] [blame] | 6072 | int psk_ret = mbedtls_ssl_get_psk( ssl, &psk, &psk_len ); |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6073 | |
Przemek Stekiel | b293aaa | 2022-04-19 12:22:38 +0200 | [diff] [blame] | 6074 | if( psk_ret == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED ) |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6075 | { |
| 6076 | /* |
| 6077 | * This should never happen because the existence of a PSK is always |
Przemek Stekiel | b293aaa | 2022-04-19 12:22:38 +0200 | [diff] [blame] | 6078 | * checked before calling this function. |
| 6079 | * |
| 6080 | * The exception is opaque DHE-PSK. For DHE-PSK fill premaster with |
Przemek Stekiel | 8abcee9 | 2022-04-28 09:16:28 +0200 | [diff] [blame] | 6081 | * the shared secret without PSK. |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6082 | */ |
Przemek Stekiel | b293aaa | 2022-04-19 12:22:38 +0200 | [diff] [blame] | 6083 | if ( key_ex != MBEDTLS_KEY_EXCHANGE_DHE_PSK ) |
| 6084 | { |
| 6085 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 6086 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 6087 | } |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6088 | } |
| 6089 | |
| 6090 | /* |
| 6091 | * PMS = struct { |
| 6092 | * opaque other_secret<0..2^16-1>; |
| 6093 | * opaque psk<0..2^16-1>; |
| 6094 | * }; |
| 6095 | * with "other_secret" depending on the particular key exchange |
| 6096 | */ |
| 6097 | #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) |
| 6098 | if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK ) |
| 6099 | { |
| 6100 | if( end - p < 2 ) |
| 6101 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6102 | |
| 6103 | MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 ); |
| 6104 | p += 2; |
| 6105 | |
| 6106 | if( end < p || (size_t)( end - p ) < psk_len ) |
| 6107 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6108 | |
| 6109 | memset( p, 0, psk_len ); |
| 6110 | p += psk_len; |
| 6111 | } |
| 6112 | else |
| 6113 | #endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */ |
| 6114 | #if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) |
| 6115 | if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK ) |
| 6116 | { |
| 6117 | /* |
| 6118 | * other_secret already set by the ClientKeyExchange message, |
| 6119 | * and is 48 bytes long |
| 6120 | */ |
| 6121 | if( end - p < 2 ) |
| 6122 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6123 | |
| 6124 | *p++ = 0; |
| 6125 | *p++ = 48; |
| 6126 | p += 48; |
| 6127 | } |
| 6128 | else |
| 6129 | #endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */ |
| 6130 | #if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) |
| 6131 | if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK ) |
| 6132 | { |
| 6133 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 6134 | size_t len; |
| 6135 | |
| 6136 | /* Write length only when we know the actual value */ |
| 6137 | if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx, |
| 6138 | p + 2, end - ( p + 2 ), &len, |
| 6139 | ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 ) |
| 6140 | { |
| 6141 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret ); |
| 6142 | return( ret ); |
| 6143 | } |
| 6144 | MBEDTLS_PUT_UINT16_BE( len, p, 0 ); |
| 6145 | p += 2 + len; |
| 6146 | |
| 6147 | MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K ); |
| 6148 | } |
| 6149 | else |
| 6150 | #endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */ |
Neil Armstrong | 80f6f32 | 2022-05-03 17:56:38 +0200 | [diff] [blame] | 6151 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6152 | if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ) |
| 6153 | { |
| 6154 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 6155 | size_t zlen; |
| 6156 | |
| 6157 | if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen, |
| 6158 | p + 2, end - ( p + 2 ), |
| 6159 | ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 ) |
| 6160 | { |
| 6161 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret ); |
| 6162 | return( ret ); |
| 6163 | } |
| 6164 | |
| 6165 | MBEDTLS_PUT_UINT16_BE( zlen, p, 0 ); |
| 6166 | p += 2 + zlen; |
| 6167 | |
| 6168 | MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx, |
| 6169 | MBEDTLS_DEBUG_ECDH_Z ); |
| 6170 | } |
| 6171 | else |
Neil Armstrong | 80f6f32 | 2022-05-03 17:56:38 +0200 | [diff] [blame] | 6172 | #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */ |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6173 | { |
| 6174 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 6175 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 6176 | } |
| 6177 | |
| 6178 | /* opaque psk<0..2^16-1>; */ |
| 6179 | if( end - p < 2 ) |
| 6180 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6181 | |
| 6182 | MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 ); |
| 6183 | p += 2; |
| 6184 | |
| 6185 | if( end < p || (size_t)( end - p ) < psk_len ) |
| 6186 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6187 | |
| 6188 | memcpy( p, psk, psk_len ); |
| 6189 | p += psk_len; |
| 6190 | |
| 6191 | ssl->handshake->pmslen = p - ssl->handshake->premaster; |
| 6192 | |
| 6193 | return( 0 ); |
| 6194 | } |
Neil Armstrong | 80f6f32 | 2022-05-03 17:56:38 +0200 | [diff] [blame] | 6195 | #endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ |
Jerry Yu | c2c673d | 2022-02-17 14:20:39 +0800 | [diff] [blame] | 6196 | |
| 6197 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6198 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | c2c673d | 2022-02-17 14:20:39 +0800 | [diff] [blame] | 6199 | static int ssl_write_hello_request( mbedtls_ssl_context *ssl ); |
| 6200 | |
| 6201 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 6202 | int mbedtls_ssl_resend_hello_request( mbedtls_ssl_context *ssl ) |
| 6203 | { |
| 6204 | /* If renegotiation is not enforced, retransmit until we would reach max |
| 6205 | * timeout if we were using the usual handshake doubling scheme */ |
| 6206 | if( ssl->conf->renego_max_records < 0 ) |
| 6207 | { |
| 6208 | uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1; |
| 6209 | unsigned char doublings = 1; |
| 6210 | |
| 6211 | while( ratio != 0 ) |
| 6212 | { |
| 6213 | ++doublings; |
| 6214 | ratio >>= 1; |
| 6215 | } |
| 6216 | |
| 6217 | if( ++ssl->renego_records_seen > doublings ) |
| 6218 | { |
| 6219 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) ); |
| 6220 | return( 0 ); |
| 6221 | } |
| 6222 | } |
| 6223 | |
| 6224 | return( ssl_write_hello_request( ssl ) ); |
| 6225 | } |
| 6226 | #endif |
| 6227 | #endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */ |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6228 | |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6229 | /* |
| 6230 | * Handshake functions |
| 6231 | */ |
| 6232 | #if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
| 6233 | /* No certificate support -> dummy functions */ |
| 6234 | int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl ) |
| 6235 | { |
| 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 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 6249 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 6250 | } |
| 6251 | |
| 6252 | int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl ) |
| 6253 | { |
| 6254 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 6255 | ssl->handshake->ciphersuite_info; |
| 6256 | |
| 6257 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) ); |
| 6258 | |
| 6259 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) |
| 6260 | { |
| 6261 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) ); |
| 6262 | ssl->state++; |
| 6263 | return( 0 ); |
| 6264 | } |
| 6265 | |
| 6266 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 6267 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 6268 | } |
| 6269 | |
| 6270 | #else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 6271 | /* Some certificate support -> implement write and parse */ |
| 6272 | |
| 6273 | int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl ) |
| 6274 | { |
| 6275 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
| 6276 | size_t i, n; |
| 6277 | const mbedtls_x509_crt *crt; |
| 6278 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 6279 | ssl->handshake->ciphersuite_info; |
| 6280 | |
| 6281 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) ); |
| 6282 | |
| 6283 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) |
| 6284 | { |
| 6285 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) ); |
| 6286 | ssl->state++; |
| 6287 | return( 0 ); |
| 6288 | } |
| 6289 | |
| 6290 | #if defined(MBEDTLS_SSL_CLI_C) |
| 6291 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 6292 | { |
| 6293 | if( ssl->handshake->client_auth == 0 ) |
| 6294 | { |
| 6295 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) ); |
| 6296 | ssl->state++; |
| 6297 | return( 0 ); |
| 6298 | } |
| 6299 | } |
| 6300 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 6301 | #if defined(MBEDTLS_SSL_SRV_C) |
| 6302 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 6303 | { |
| 6304 | if( mbedtls_ssl_own_cert( ssl ) == NULL ) |
| 6305 | { |
| 6306 | /* Should never happen because we shouldn't have picked the |
| 6307 | * ciphersuite if we don't have a certificate. */ |
| 6308 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 6309 | } |
| 6310 | } |
| 6311 | #endif |
| 6312 | |
| 6313 | MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) ); |
| 6314 | |
| 6315 | /* |
| 6316 | * 0 . 0 handshake type |
| 6317 | * 1 . 3 handshake length |
| 6318 | * 4 . 6 length of all certs |
| 6319 | * 7 . 9 length of cert. 1 |
| 6320 | * 10 . n-1 peer certificate |
| 6321 | * n . n+2 length of cert. 2 |
| 6322 | * n+3 . ... upper level cert, etc. |
| 6323 | */ |
| 6324 | i = 7; |
| 6325 | crt = mbedtls_ssl_own_cert( ssl ); |
| 6326 | |
| 6327 | while( crt != NULL ) |
| 6328 | { |
| 6329 | n = crt->raw.len; |
| 6330 | if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i ) |
| 6331 | { |
| 6332 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %" MBEDTLS_PRINTF_SIZET |
| 6333 | " > %" MBEDTLS_PRINTF_SIZET, |
| 6334 | i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN ) ); |
| 6335 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 6336 | } |
| 6337 | |
| 6338 | ssl->out_msg[i ] = MBEDTLS_BYTE_2( n ); |
| 6339 | ssl->out_msg[i + 1] = MBEDTLS_BYTE_1( n ); |
| 6340 | ssl->out_msg[i + 2] = MBEDTLS_BYTE_0( n ); |
| 6341 | |
| 6342 | i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n ); |
| 6343 | i += n; crt = crt->next; |
| 6344 | } |
| 6345 | |
| 6346 | ssl->out_msg[4] = MBEDTLS_BYTE_2( i - 7 ); |
| 6347 | ssl->out_msg[5] = MBEDTLS_BYTE_1( i - 7 ); |
| 6348 | ssl->out_msg[6] = MBEDTLS_BYTE_0( i - 7 ); |
| 6349 | |
| 6350 | ssl->out_msglen = i; |
| 6351 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 6352 | ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE; |
| 6353 | |
| 6354 | ssl->state++; |
| 6355 | |
| 6356 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
| 6357 | { |
| 6358 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); |
| 6359 | return( ret ); |
| 6360 | } |
| 6361 | |
| 6362 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) ); |
| 6363 | |
| 6364 | return( ret ); |
| 6365 | } |
| 6366 | |
| 6367 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) |
| 6368 | |
| 6369 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6370 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6371 | static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl, |
| 6372 | unsigned char *crt_buf, |
| 6373 | size_t crt_buf_len ) |
| 6374 | { |
| 6375 | mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert; |
| 6376 | |
| 6377 | if( peer_crt == NULL ) |
| 6378 | return( -1 ); |
| 6379 | |
| 6380 | if( peer_crt->raw.len != crt_buf_len ) |
| 6381 | return( -1 ); |
| 6382 | |
| 6383 | return( memcmp( peer_crt->raw.p, crt_buf, peer_crt->raw.len ) ); |
| 6384 | } |
| 6385 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6386 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6387 | static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl, |
| 6388 | unsigned char *crt_buf, |
| 6389 | size_t crt_buf_len ) |
| 6390 | { |
| 6391 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 6392 | unsigned char const * const peer_cert_digest = |
| 6393 | ssl->session->peer_cert_digest; |
| 6394 | mbedtls_md_type_t const peer_cert_digest_type = |
| 6395 | ssl->session->peer_cert_digest_type; |
| 6396 | mbedtls_md_info_t const * const digest_info = |
| 6397 | mbedtls_md_info_from_type( peer_cert_digest_type ); |
| 6398 | unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN]; |
| 6399 | size_t digest_len; |
| 6400 | |
| 6401 | if( peer_cert_digest == NULL || digest_info == NULL ) |
| 6402 | return( -1 ); |
| 6403 | |
| 6404 | digest_len = mbedtls_md_get_size( digest_info ); |
| 6405 | if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN ) |
| 6406 | return( -1 ); |
| 6407 | |
| 6408 | ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest ); |
| 6409 | if( ret != 0 ) |
| 6410 | return( -1 ); |
| 6411 | |
| 6412 | return( memcmp( tmp_digest, peer_cert_digest, digest_len ) ); |
| 6413 | } |
| 6414 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 6415 | #endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */ |
| 6416 | |
| 6417 | /* |
| 6418 | * Once the certificate message is read, parse it into a cert chain and |
| 6419 | * perform basic checks, but leave actual verification to the caller |
| 6420 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6421 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6422 | static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl, |
| 6423 | mbedtls_x509_crt *chain ) |
| 6424 | { |
| 6425 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 6426 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) |
| 6427 | int crt_cnt=0; |
| 6428 | #endif |
| 6429 | size_t i, n; |
| 6430 | uint8_t alert; |
| 6431 | |
| 6432 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) |
| 6433 | { |
| 6434 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
| 6435 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6436 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
| 6437 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
| 6438 | } |
| 6439 | |
| 6440 | if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ) |
| 6441 | { |
| 6442 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6443 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
| 6444 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
| 6445 | } |
| 6446 | |
| 6447 | if( ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 ) |
| 6448 | { |
| 6449 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
| 6450 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6451 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
| 6452 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
| 6453 | } |
| 6454 | |
| 6455 | i = mbedtls_ssl_hs_hdr_len( ssl ); |
| 6456 | |
| 6457 | /* |
| 6458 | * Same message structure as in mbedtls_ssl_write_certificate() |
| 6459 | */ |
| 6460 | n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2]; |
| 6461 | |
| 6462 | if( ssl->in_msg[i] != 0 || |
| 6463 | ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) ) |
| 6464 | { |
| 6465 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
| 6466 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6467 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
| 6468 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
| 6469 | } |
| 6470 | |
| 6471 | /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */ |
| 6472 | i += 3; |
| 6473 | |
| 6474 | /* Iterate through and parse the CRTs in the provided chain. */ |
| 6475 | while( i < ssl->in_hslen ) |
| 6476 | { |
| 6477 | /* Check that there's room for the next CRT's length fields. */ |
| 6478 | if ( i + 3 > ssl->in_hslen ) { |
| 6479 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
| 6480 | mbedtls_ssl_send_alert_message( ssl, |
| 6481 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6482 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
| 6483 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
| 6484 | } |
| 6485 | /* In theory, the CRT can be up to 2**24 Bytes, but we don't support |
| 6486 | * anything beyond 2**16 ~ 64K. */ |
| 6487 | if( ssl->in_msg[i] != 0 ) |
| 6488 | { |
| 6489 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
| 6490 | mbedtls_ssl_send_alert_message( ssl, |
| 6491 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6492 | MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT ); |
| 6493 | return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE ); |
| 6494 | } |
| 6495 | |
| 6496 | /* Read length of the next CRT in the chain. */ |
| 6497 | n = ( (unsigned int) ssl->in_msg[i + 1] << 8 ) |
| 6498 | | (unsigned int) ssl->in_msg[i + 2]; |
| 6499 | i += 3; |
| 6500 | |
| 6501 | if( n < 128 || i + n > ssl->in_hslen ) |
| 6502 | { |
| 6503 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
| 6504 | mbedtls_ssl_send_alert_message( ssl, |
| 6505 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6506 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
| 6507 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
| 6508 | } |
| 6509 | |
| 6510 | /* Check if we're handling the first CRT in the chain. */ |
| 6511 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) |
| 6512 | if( crt_cnt++ == 0 && |
| 6513 | ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
| 6514 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
| 6515 | { |
| 6516 | /* During client-side renegotiation, check that the server's |
| 6517 | * end-CRTs hasn't changed compared to the initial handshake, |
| 6518 | * mitigating the triple handshake attack. On success, reuse |
| 6519 | * the original end-CRT instead of parsing it again. */ |
| 6520 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) ); |
| 6521 | if( ssl_check_peer_crt_unchanged( ssl, |
| 6522 | &ssl->in_msg[i], |
| 6523 | n ) != 0 ) |
| 6524 | { |
| 6525 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) ); |
| 6526 | mbedtls_ssl_send_alert_message( ssl, |
| 6527 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6528 | MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED ); |
| 6529 | return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE ); |
| 6530 | } |
| 6531 | |
| 6532 | /* Now we can safely free the original chain. */ |
| 6533 | ssl_clear_peer_cert( ssl->session ); |
| 6534 | } |
| 6535 | #endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */ |
| 6536 | |
| 6537 | /* Parse the next certificate in the chain. */ |
| 6538 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 6539 | ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n ); |
| 6540 | #else |
| 6541 | /* If we don't need to store the CRT chain permanently, parse |
| 6542 | * it in-place from the input buffer instead of making a copy. */ |
| 6543 | ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n ); |
| 6544 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 6545 | switch( ret ) |
| 6546 | { |
| 6547 | case 0: /*ok*/ |
| 6548 | case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND: |
| 6549 | /* Ignore certificate with an unknown algorithm: maybe a |
| 6550 | prior certificate was already trusted. */ |
| 6551 | break; |
| 6552 | |
| 6553 | case MBEDTLS_ERR_X509_ALLOC_FAILED: |
| 6554 | alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR; |
| 6555 | goto crt_parse_der_failed; |
| 6556 | |
| 6557 | case MBEDTLS_ERR_X509_UNKNOWN_VERSION: |
| 6558 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 6559 | goto crt_parse_der_failed; |
| 6560 | |
| 6561 | default: |
| 6562 | alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT; |
| 6563 | crt_parse_der_failed: |
| 6564 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert ); |
| 6565 | MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret ); |
| 6566 | return( ret ); |
| 6567 | } |
| 6568 | |
| 6569 | i += n; |
| 6570 | } |
| 6571 | |
| 6572 | MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain ); |
| 6573 | return( 0 ); |
| 6574 | } |
| 6575 | |
| 6576 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6577 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6578 | static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl ) |
| 6579 | { |
| 6580 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 6581 | return( -1 ); |
| 6582 | |
| 6583 | if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) && |
| 6584 | ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 6585 | ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE && |
| 6586 | memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 ) |
| 6587 | { |
Ronald Cron | 1938588 | 2022-06-15 16:26:13 +0200 | [diff] [blame] | 6588 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "peer has no certificate" ) ); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6589 | return( 0 ); |
| 6590 | } |
| 6591 | return( -1 ); |
| 6592 | } |
| 6593 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 6594 | |
| 6595 | /* Check if a certificate message is expected. |
| 6596 | * Return either |
| 6597 | * - SSL_CERTIFICATE_EXPECTED, or |
| 6598 | * - SSL_CERTIFICATE_SKIP |
| 6599 | * indicating whether a Certificate message is expected or not. |
| 6600 | */ |
| 6601 | #define SSL_CERTIFICATE_EXPECTED 0 |
| 6602 | #define SSL_CERTIFICATE_SKIP 1 |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6603 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6604 | static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl, |
| 6605 | int authmode ) |
| 6606 | { |
| 6607 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 6608 | ssl->handshake->ciphersuite_info; |
| 6609 | |
| 6610 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) |
| 6611 | return( SSL_CERTIFICATE_SKIP ); |
| 6612 | |
| 6613 | #if defined(MBEDTLS_SSL_SRV_C) |
| 6614 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 6615 | { |
| 6616 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ) |
| 6617 | return( SSL_CERTIFICATE_SKIP ); |
| 6618 | |
| 6619 | if( authmode == MBEDTLS_SSL_VERIFY_NONE ) |
| 6620 | { |
| 6621 | ssl->session_negotiate->verify_result = |
| 6622 | MBEDTLS_X509_BADCERT_SKIP_VERIFY; |
| 6623 | return( SSL_CERTIFICATE_SKIP ); |
| 6624 | } |
| 6625 | } |
| 6626 | #else |
| 6627 | ((void) authmode); |
| 6628 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 6629 | |
| 6630 | return( SSL_CERTIFICATE_EXPECTED ); |
| 6631 | } |
| 6632 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6633 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6634 | static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl, |
| 6635 | int authmode, |
| 6636 | mbedtls_x509_crt *chain, |
| 6637 | void *rs_ctx ) |
| 6638 | { |
| 6639 | int ret = 0; |
| 6640 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 6641 | ssl->handshake->ciphersuite_info; |
| 6642 | int have_ca_chain = 0; |
| 6643 | |
| 6644 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *); |
| 6645 | void *p_vrfy; |
| 6646 | |
| 6647 | if( authmode == MBEDTLS_SSL_VERIFY_NONE ) |
| 6648 | return( 0 ); |
| 6649 | |
| 6650 | if( ssl->f_vrfy != NULL ) |
| 6651 | { |
| 6652 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) ); |
| 6653 | f_vrfy = ssl->f_vrfy; |
| 6654 | p_vrfy = ssl->p_vrfy; |
| 6655 | } |
| 6656 | else |
| 6657 | { |
| 6658 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) ); |
| 6659 | f_vrfy = ssl->conf->f_vrfy; |
| 6660 | p_vrfy = ssl->conf->p_vrfy; |
| 6661 | } |
| 6662 | |
| 6663 | /* |
| 6664 | * Main check: verify certificate |
| 6665 | */ |
| 6666 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 6667 | if( ssl->conf->f_ca_cb != NULL ) |
| 6668 | { |
| 6669 | ((void) rs_ctx); |
| 6670 | have_ca_chain = 1; |
| 6671 | |
| 6672 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) ); |
| 6673 | ret = mbedtls_x509_crt_verify_with_ca_cb( |
| 6674 | chain, |
| 6675 | ssl->conf->f_ca_cb, |
| 6676 | ssl->conf->p_ca_cb, |
| 6677 | ssl->conf->cert_profile, |
| 6678 | ssl->hostname, |
| 6679 | &ssl->session_negotiate->verify_result, |
| 6680 | f_vrfy, p_vrfy ); |
| 6681 | } |
| 6682 | else |
| 6683 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
| 6684 | { |
| 6685 | mbedtls_x509_crt *ca_chain; |
| 6686 | mbedtls_x509_crl *ca_crl; |
| 6687 | |
| 6688 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 6689 | if( ssl->handshake->sni_ca_chain != NULL ) |
| 6690 | { |
| 6691 | ca_chain = ssl->handshake->sni_ca_chain; |
| 6692 | ca_crl = ssl->handshake->sni_ca_crl; |
| 6693 | } |
| 6694 | else |
| 6695 | #endif |
| 6696 | { |
| 6697 | ca_chain = ssl->conf->ca_chain; |
| 6698 | ca_crl = ssl->conf->ca_crl; |
| 6699 | } |
| 6700 | |
| 6701 | if( ca_chain != NULL ) |
| 6702 | have_ca_chain = 1; |
| 6703 | |
| 6704 | ret = mbedtls_x509_crt_verify_restartable( |
| 6705 | chain, |
| 6706 | ca_chain, ca_crl, |
| 6707 | ssl->conf->cert_profile, |
| 6708 | ssl->hostname, |
| 6709 | &ssl->session_negotiate->verify_result, |
| 6710 | f_vrfy, p_vrfy, rs_ctx ); |
| 6711 | } |
| 6712 | |
| 6713 | if( ret != 0 ) |
| 6714 | { |
| 6715 | MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret ); |
| 6716 | } |
| 6717 | |
| 6718 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
| 6719 | if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS ) |
| 6720 | return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS ); |
| 6721 | #endif |
| 6722 | |
| 6723 | /* |
| 6724 | * Secondary checks: always done, but change 'ret' only if it was 0 |
| 6725 | */ |
| 6726 | |
| 6727 | #if defined(MBEDTLS_ECP_C) |
| 6728 | { |
| 6729 | const mbedtls_pk_context *pk = &chain->pk; |
| 6730 | |
Manuel Pégourié-Gonnard | 66b0d61 | 2022-06-17 10:49:29 +0200 | [diff] [blame] | 6731 | /* If certificate uses an EC key, make sure the curve is OK. |
| 6732 | * This is a public key, so it can't be opaque, so can_do() is a good |
| 6733 | * enough check to ensure pk_ec() is safe to use here. */ |
Leonid Rozenboim | 19e5973 | 2022-08-08 16:52:38 -0700 | [diff] [blame] | 6734 | if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) ) |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6735 | { |
Leonid Rozenboim | 19e5973 | 2022-08-08 16:52:38 -0700 | [diff] [blame] | 6736 | /* and in the unlikely case the above assumption no longer holds |
| 6737 | * we are making sure that pk_ec() here does not return a NULL |
| 6738 | */ |
| 6739 | const mbedtls_ecp_keypair *ec = mbedtls_pk_ec( *pk ); |
| 6740 | if( ec == NULL ) |
| 6741 | { |
Tom Cosgrove | 20c1137 | 2022-08-24 15:06:13 +0100 | [diff] [blame] | 6742 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_pk_ec() returned NULL" ) ); |
Leonid Rozenboim | 19e5973 | 2022-08-08 16:52:38 -0700 | [diff] [blame] | 6743 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 6744 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6745 | |
Leonid Rozenboim | 19e5973 | 2022-08-08 16:52:38 -0700 | [diff] [blame] | 6746 | if( mbedtls_ssl_check_curve( ssl, ec->grp.id ) != 0 ) |
| 6747 | { |
| 6748 | ssl->session_negotiate->verify_result |= |
| 6749 | MBEDTLS_X509_BADCERT_BAD_KEY; |
| 6750 | |
| 6751 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) ); |
| 6752 | if( ret == 0 ) |
| 6753 | ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE; |
| 6754 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6755 | } |
| 6756 | } |
| 6757 | #endif /* MBEDTLS_ECP_C */ |
| 6758 | |
| 6759 | if( mbedtls_ssl_check_cert_usage( chain, |
| 6760 | ciphersuite_info, |
| 6761 | ! ssl->conf->endpoint, |
| 6762 | &ssl->session_negotiate->verify_result ) != 0 ) |
| 6763 | { |
| 6764 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) ); |
| 6765 | if( ret == 0 ) |
| 6766 | ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE; |
| 6767 | } |
| 6768 | |
| 6769 | /* mbedtls_x509_crt_verify_with_profile is supposed to report a |
| 6770 | * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED, |
| 6771 | * with details encoded in the verification flags. All other kinds |
| 6772 | * of error codes, including those from the user provided f_vrfy |
| 6773 | * functions, are treated as fatal and lead to a failure of |
| 6774 | * ssl_parse_certificate even if verification was optional. */ |
| 6775 | if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL && |
| 6776 | ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED || |
| 6777 | ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE ) ) |
| 6778 | { |
| 6779 | ret = 0; |
| 6780 | } |
| 6781 | |
| 6782 | if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED ) |
| 6783 | { |
| 6784 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) ); |
| 6785 | ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED; |
| 6786 | } |
| 6787 | |
| 6788 | if( ret != 0 ) |
| 6789 | { |
| 6790 | uint8_t alert; |
| 6791 | |
| 6792 | /* The certificate may have been rejected for several reasons. |
| 6793 | Pick one and send the corresponding alert. Which alert to send |
| 6794 | may be a subject of debate in some cases. */ |
| 6795 | if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER ) |
| 6796 | alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED; |
| 6797 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH ) |
| 6798 | alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT; |
| 6799 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE ) |
| 6800 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 6801 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE ) |
| 6802 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 6803 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE ) |
| 6804 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 6805 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK ) |
| 6806 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 6807 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY ) |
| 6808 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 6809 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED ) |
| 6810 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED; |
| 6811 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED ) |
| 6812 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED; |
| 6813 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED ) |
| 6814 | alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA; |
| 6815 | else |
| 6816 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN; |
| 6817 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6818 | alert ); |
| 6819 | } |
| 6820 | |
| 6821 | #if defined(MBEDTLS_DEBUG_C) |
| 6822 | if( ssl->session_negotiate->verify_result != 0 ) |
| 6823 | { |
| 6824 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %08x", |
| 6825 | (unsigned int) ssl->session_negotiate->verify_result ) ); |
| 6826 | } |
| 6827 | else |
| 6828 | { |
| 6829 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) ); |
| 6830 | } |
| 6831 | #endif /* MBEDTLS_DEBUG_C */ |
| 6832 | |
| 6833 | return( ret ); |
| 6834 | } |
| 6835 | |
| 6836 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6837 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6838 | static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl, |
| 6839 | unsigned char *start, size_t len ) |
| 6840 | { |
| 6841 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 6842 | /* Remember digest of the peer's end-CRT. */ |
| 6843 | ssl->session_negotiate->peer_cert_digest = |
| 6844 | mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ); |
| 6845 | if( ssl->session_negotiate->peer_cert_digest == NULL ) |
| 6846 | { |
| 6847 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", |
| 6848 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ); |
| 6849 | mbedtls_ssl_send_alert_message( ssl, |
| 6850 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6851 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
| 6852 | |
| 6853 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 6854 | } |
| 6855 | |
| 6856 | ret = mbedtls_md( mbedtls_md_info_from_type( |
| 6857 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ), |
| 6858 | start, len, |
| 6859 | ssl->session_negotiate->peer_cert_digest ); |
| 6860 | |
| 6861 | ssl->session_negotiate->peer_cert_digest_type = |
| 6862 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE; |
| 6863 | ssl->session_negotiate->peer_cert_digest_len = |
| 6864 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN; |
| 6865 | |
| 6866 | return( ret ); |
| 6867 | } |
| 6868 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6869 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6870 | static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl, |
| 6871 | unsigned char *start, size_t len ) |
| 6872 | { |
| 6873 | unsigned char *end = start + len; |
| 6874 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 6875 | |
| 6876 | /* Make a copy of the peer's raw public key. */ |
| 6877 | mbedtls_pk_init( &ssl->handshake->peer_pubkey ); |
| 6878 | ret = mbedtls_pk_parse_subpubkey( &start, end, |
| 6879 | &ssl->handshake->peer_pubkey ); |
| 6880 | if( ret != 0 ) |
| 6881 | { |
| 6882 | /* We should have parsed the public key before. */ |
| 6883 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 6884 | } |
| 6885 | |
| 6886 | return( 0 ); |
| 6887 | } |
| 6888 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 6889 | |
| 6890 | int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl ) |
| 6891 | { |
| 6892 | int ret = 0; |
| 6893 | int crt_expected; |
| 6894 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 6895 | const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET |
| 6896 | ? ssl->handshake->sni_authmode |
| 6897 | : ssl->conf->authmode; |
| 6898 | #else |
| 6899 | const int authmode = ssl->conf->authmode; |
| 6900 | #endif |
| 6901 | void *rs_ctx = NULL; |
| 6902 | mbedtls_x509_crt *chain = NULL; |
| 6903 | |
| 6904 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) ); |
| 6905 | |
| 6906 | crt_expected = ssl_parse_certificate_coordinate( ssl, authmode ); |
| 6907 | if( crt_expected == SSL_CERTIFICATE_SKIP ) |
| 6908 | { |
| 6909 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) ); |
| 6910 | goto exit; |
| 6911 | } |
| 6912 | |
| 6913 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
| 6914 | if( ssl->handshake->ecrs_enabled && |
| 6915 | ssl->handshake->ecrs_state == ssl_ecrs_crt_verify ) |
| 6916 | { |
| 6917 | chain = ssl->handshake->ecrs_peer_cert; |
| 6918 | ssl->handshake->ecrs_peer_cert = NULL; |
| 6919 | goto crt_verify; |
| 6920 | } |
| 6921 | #endif |
| 6922 | |
| 6923 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) |
| 6924 | { |
| 6925 | /* mbedtls_ssl_read_record may have sent an alert already. We |
| 6926 | let it decide whether to alert. */ |
| 6927 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
| 6928 | goto exit; |
| 6929 | } |
| 6930 | |
| 6931 | #if defined(MBEDTLS_SSL_SRV_C) |
| 6932 | if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 ) |
| 6933 | { |
| 6934 | ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING; |
| 6935 | |
| 6936 | if( authmode != MBEDTLS_SSL_VERIFY_OPTIONAL ) |
| 6937 | ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE; |
| 6938 | |
| 6939 | goto exit; |
| 6940 | } |
| 6941 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 6942 | |
| 6943 | /* Clear existing peer CRT structure in case we tried to |
| 6944 | * reuse a session but it failed, and allocate a new one. */ |
| 6945 | ssl_clear_peer_cert( ssl->session_negotiate ); |
| 6946 | |
| 6947 | chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ); |
| 6948 | if( chain == NULL ) |
| 6949 | { |
| 6950 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", |
| 6951 | sizeof( mbedtls_x509_crt ) ) ); |
| 6952 | mbedtls_ssl_send_alert_message( ssl, |
| 6953 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6954 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
| 6955 | |
| 6956 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 6957 | goto exit; |
| 6958 | } |
| 6959 | mbedtls_x509_crt_init( chain ); |
| 6960 | |
| 6961 | ret = ssl_parse_certificate_chain( ssl, chain ); |
| 6962 | if( ret != 0 ) |
| 6963 | goto exit; |
| 6964 | |
| 6965 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
| 6966 | if( ssl->handshake->ecrs_enabled) |
| 6967 | ssl->handshake->ecrs_state = ssl_ecrs_crt_verify; |
| 6968 | |
| 6969 | crt_verify: |
| 6970 | if( ssl->handshake->ecrs_enabled) |
| 6971 | rs_ctx = &ssl->handshake->ecrs_ctx; |
| 6972 | #endif |
| 6973 | |
| 6974 | ret = ssl_parse_certificate_verify( ssl, authmode, |
| 6975 | chain, rs_ctx ); |
| 6976 | if( ret != 0 ) |
| 6977 | goto exit; |
| 6978 | |
| 6979 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 6980 | { |
| 6981 | unsigned char *crt_start, *pk_start; |
| 6982 | size_t crt_len, pk_len; |
| 6983 | |
| 6984 | /* We parse the CRT chain without copying, so |
| 6985 | * these pointers point into the input buffer, |
| 6986 | * and are hence still valid after freeing the |
| 6987 | * CRT chain. */ |
| 6988 | |
| 6989 | crt_start = chain->raw.p; |
| 6990 | crt_len = chain->raw.len; |
| 6991 | |
| 6992 | pk_start = chain->pk_raw.p; |
| 6993 | pk_len = chain->pk_raw.len; |
| 6994 | |
| 6995 | /* Free the CRT structures before computing |
| 6996 | * digest and copying the peer's public key. */ |
| 6997 | mbedtls_x509_crt_free( chain ); |
| 6998 | mbedtls_free( chain ); |
| 6999 | chain = NULL; |
| 7000 | |
| 7001 | ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len ); |
| 7002 | if( ret != 0 ) |
| 7003 | goto exit; |
| 7004 | |
| 7005 | ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len ); |
| 7006 | if( ret != 0 ) |
| 7007 | goto exit; |
| 7008 | } |
| 7009 | #else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 7010 | /* Pass ownership to session structure. */ |
| 7011 | ssl->session_negotiate->peer_cert = chain; |
| 7012 | chain = NULL; |
| 7013 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 7014 | |
| 7015 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) ); |
| 7016 | |
| 7017 | exit: |
| 7018 | |
| 7019 | if( ret == 0 ) |
| 7020 | ssl->state++; |
| 7021 | |
| 7022 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
| 7023 | if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS ) |
| 7024 | { |
| 7025 | ssl->handshake->ecrs_peer_cert = chain; |
| 7026 | chain = NULL; |
| 7027 | } |
| 7028 | #endif |
| 7029 | |
| 7030 | if( chain != NULL ) |
| 7031 | { |
| 7032 | mbedtls_x509_crt_free( chain ); |
| 7033 | mbedtls_free( chain ); |
| 7034 | } |
| 7035 | |
| 7036 | return( ret ); |
| 7037 | } |
| 7038 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 7039 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 7040 | #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] | 7041 | static void ssl_calc_finished_tls_sha256( |
| 7042 | mbedtls_ssl_context *ssl, unsigned char *buf, int from ) |
| 7043 | { |
| 7044 | int len = 12; |
| 7045 | const char *sender; |
| 7046 | unsigned char padbuf[32]; |
| 7047 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7048 | size_t hash_size; |
| 7049 | psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT; |
| 7050 | psa_status_t status; |
| 7051 | #else |
| 7052 | mbedtls_sha256_context sha256; |
| 7053 | #endif |
| 7054 | |
| 7055 | mbedtls_ssl_session *session = ssl->session_negotiate; |
| 7056 | if( !session ) |
| 7057 | session = ssl->session; |
| 7058 | |
| 7059 | sender = ( from == MBEDTLS_SSL_IS_CLIENT ) |
| 7060 | ? "client finished" |
| 7061 | : "server finished"; |
| 7062 | |
| 7063 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7064 | sha256_psa = psa_hash_operation_init(); |
| 7065 | |
| 7066 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) ); |
| 7067 | |
| 7068 | status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa ); |
| 7069 | if( status != PSA_SUCCESS ) |
| 7070 | { |
| 7071 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); |
| 7072 | return; |
| 7073 | } |
| 7074 | |
| 7075 | status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size ); |
| 7076 | if( status != PSA_SUCCESS ) |
| 7077 | { |
| 7078 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); |
| 7079 | return; |
| 7080 | } |
| 7081 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 ); |
| 7082 | #else |
| 7083 | |
| 7084 | mbedtls_sha256_init( &sha256 ); |
| 7085 | |
| 7086 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) ); |
| 7087 | |
| 7088 | mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 ); |
| 7089 | |
| 7090 | /* |
| 7091 | * TLSv1.2: |
| 7092 | * hash = PRF( master, finished_label, |
| 7093 | * Hash( handshake ) )[0.11] |
| 7094 | */ |
| 7095 | |
| 7096 | #if !defined(MBEDTLS_SHA256_ALT) |
| 7097 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *) |
| 7098 | sha256.state, sizeof( sha256.state ) ); |
| 7099 | #endif |
| 7100 | |
| 7101 | mbedtls_sha256_finish( &sha256, padbuf ); |
| 7102 | mbedtls_sha256_free( &sha256 ); |
| 7103 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 7104 | |
| 7105 | ssl->handshake->tls_prf( session->master, 48, sender, |
| 7106 | padbuf, 32, buf, len ); |
| 7107 | |
| 7108 | MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len ); |
| 7109 | |
| 7110 | mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) ); |
| 7111 | |
| 7112 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) ); |
| 7113 | } |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 7114 | #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] | 7115 | |
Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 7116 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 7117 | #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] | 7118 | static void ssl_calc_finished_tls_sha384( |
| 7119 | mbedtls_ssl_context *ssl, unsigned char *buf, int from ) |
| 7120 | { |
| 7121 | int len = 12; |
| 7122 | const char *sender; |
| 7123 | unsigned char padbuf[48]; |
| 7124 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7125 | size_t hash_size; |
| 7126 | psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT; |
| 7127 | psa_status_t status; |
| 7128 | #else |
| 7129 | mbedtls_sha512_context sha512; |
| 7130 | #endif |
| 7131 | |
| 7132 | mbedtls_ssl_session *session = ssl->session_negotiate; |
| 7133 | if( !session ) |
| 7134 | session = ssl->session; |
| 7135 | |
| 7136 | sender = ( from == MBEDTLS_SSL_IS_CLIENT ) |
| 7137 | ? "client finished" |
| 7138 | : "server finished"; |
| 7139 | |
| 7140 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7141 | sha384_psa = psa_hash_operation_init(); |
| 7142 | |
| 7143 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) ); |
| 7144 | |
| 7145 | status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa ); |
| 7146 | if( status != PSA_SUCCESS ) |
| 7147 | { |
| 7148 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); |
| 7149 | return; |
| 7150 | } |
| 7151 | |
| 7152 | status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size ); |
| 7153 | if( status != PSA_SUCCESS ) |
| 7154 | { |
| 7155 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); |
| 7156 | return; |
| 7157 | } |
| 7158 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 ); |
| 7159 | #else |
| 7160 | mbedtls_sha512_init( &sha512 ); |
| 7161 | |
| 7162 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) ); |
| 7163 | |
Andrzej Kurek | a242e83 | 2022-08-11 10:03:14 -0400 | [diff] [blame] | 7164 | mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 ); |
Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 7165 | |
| 7166 | /* |
| 7167 | * TLSv1.2: |
| 7168 | * hash = PRF( master, finished_label, |
| 7169 | * Hash( handshake ) )[0.11] |
| 7170 | */ |
| 7171 | |
| 7172 | #if !defined(MBEDTLS_SHA512_ALT) |
| 7173 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *) |
| 7174 | sha512.state, sizeof( sha512.state ) ); |
| 7175 | #endif |
| 7176 | mbedtls_sha512_finish( &sha512, padbuf ); |
| 7177 | |
| 7178 | mbedtls_sha512_free( &sha512 ); |
| 7179 | #endif |
| 7180 | |
| 7181 | ssl->handshake->tls_prf( session->master, 48, sender, |
| 7182 | padbuf, 48, buf, len ); |
| 7183 | |
| 7184 | MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len ); |
| 7185 | |
| 7186 | mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) ); |
| 7187 | |
| 7188 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) ); |
| 7189 | } |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 7190 | #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] | 7191 | |
Jerry Yu | aef0015 | 2022-02-17 14:27:31 +0800 | [diff] [blame] | 7192 | void mbedtls_ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl ) |
| 7193 | { |
| 7194 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) ); |
| 7195 | |
| 7196 | /* |
| 7197 | * Free our handshake params |
| 7198 | */ |
| 7199 | mbedtls_ssl_handshake_free( ssl ); |
| 7200 | mbedtls_free( ssl->handshake ); |
| 7201 | ssl->handshake = NULL; |
| 7202 | |
| 7203 | /* |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 7204 | * Free the previous transform and switch in the current one |
Jerry Yu | aef0015 | 2022-02-17 14:27:31 +0800 | [diff] [blame] | 7205 | */ |
| 7206 | if( ssl->transform ) |
| 7207 | { |
| 7208 | mbedtls_ssl_transform_free( ssl->transform ); |
| 7209 | mbedtls_free( ssl->transform ); |
| 7210 | } |
| 7211 | ssl->transform = ssl->transform_negotiate; |
| 7212 | ssl->transform_negotiate = NULL; |
| 7213 | |
| 7214 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) ); |
| 7215 | } |
| 7216 | |
Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 7217 | void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl ) |
| 7218 | { |
| 7219 | int resume = ssl->handshake->resume; |
| 7220 | |
| 7221 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) ); |
| 7222 | |
| 7223 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 7224 | if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
| 7225 | { |
| 7226 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE; |
| 7227 | ssl->renego_records_seen = 0; |
| 7228 | } |
| 7229 | #endif |
| 7230 | |
| 7231 | /* |
| 7232 | * Free the previous session and switch in the current one |
| 7233 | */ |
| 7234 | if( ssl->session ) |
| 7235 | { |
| 7236 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 7237 | /* RFC 7366 3.1: keep the EtM state */ |
| 7238 | ssl->session_negotiate->encrypt_then_mac = |
| 7239 | ssl->session->encrypt_then_mac; |
| 7240 | #endif |
| 7241 | |
| 7242 | mbedtls_ssl_session_free( ssl->session ); |
| 7243 | mbedtls_free( ssl->session ); |
| 7244 | } |
| 7245 | ssl->session = ssl->session_negotiate; |
| 7246 | ssl->session_negotiate = NULL; |
| 7247 | |
| 7248 | /* |
| 7249 | * Add cache entry |
| 7250 | */ |
| 7251 | if( ssl->conf->f_set_cache != NULL && |
| 7252 | ssl->session->id_len != 0 && |
| 7253 | resume == 0 ) |
| 7254 | { |
| 7255 | if( ssl->conf->f_set_cache( ssl->conf->p_cache, |
| 7256 | ssl->session->id, |
| 7257 | ssl->session->id_len, |
| 7258 | ssl->session ) != 0 ) |
| 7259 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) ); |
| 7260 | } |
| 7261 | |
| 7262 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 7263 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 7264 | ssl->handshake->flight != NULL ) |
| 7265 | { |
| 7266 | /* Cancel handshake timer */ |
| 7267 | mbedtls_ssl_set_timer( ssl, 0 ); |
| 7268 | |
| 7269 | /* Keep last flight around in case we need to resend it: |
| 7270 | * we need the handshake and transform structures for that */ |
| 7271 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) ); |
| 7272 | } |
| 7273 | else |
| 7274 | #endif |
| 7275 | mbedtls_ssl_handshake_wrapup_free_hs_transform( ssl ); |
| 7276 | |
| 7277 | ssl->state++; |
| 7278 | |
| 7279 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) ); |
| 7280 | } |
| 7281 | |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 7282 | int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl ) |
| 7283 | { |
| 7284 | int ret, hash_len; |
| 7285 | |
| 7286 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) ); |
| 7287 | |
| 7288 | mbedtls_ssl_update_out_pointers( ssl, ssl->transform_negotiate ); |
| 7289 | |
| 7290 | ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint ); |
| 7291 | |
| 7292 | /* |
| 7293 | * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites |
| 7294 | * may define some other value. Currently (early 2016), no defined |
| 7295 | * ciphersuite does this (and this is unlikely to change as activity has |
| 7296 | * moved to TLS 1.3 now) so we can keep the hardcoded 12 here. |
| 7297 | */ |
| 7298 | hash_len = 12; |
| 7299 | |
| 7300 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 7301 | ssl->verify_data_len = hash_len; |
| 7302 | memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len ); |
| 7303 | #endif |
| 7304 | |
| 7305 | ssl->out_msglen = 4 + hash_len; |
| 7306 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 7307 | ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED; |
| 7308 | |
| 7309 | /* |
| 7310 | * In case of session resuming, invert the client and server |
| 7311 | * ChangeCipherSpec messages order. |
| 7312 | */ |
| 7313 | if( ssl->handshake->resume != 0 ) |
| 7314 | { |
| 7315 | #if defined(MBEDTLS_SSL_CLI_C) |
| 7316 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 7317 | ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP; |
| 7318 | #endif |
| 7319 | #if defined(MBEDTLS_SSL_SRV_C) |
| 7320 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 7321 | ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC; |
| 7322 | #endif |
| 7323 | } |
| 7324 | else |
| 7325 | ssl->state++; |
| 7326 | |
| 7327 | /* |
| 7328 | * Switch to our negotiated transform and session parameters for outbound |
| 7329 | * data. |
| 7330 | */ |
| 7331 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) ); |
| 7332 | |
| 7333 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 7334 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 7335 | { |
| 7336 | unsigned char i; |
| 7337 | |
| 7338 | /* Remember current epoch settings for resending */ |
| 7339 | ssl->handshake->alt_transform_out = ssl->transform_out; |
| 7340 | memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, |
| 7341 | sizeof( ssl->handshake->alt_out_ctr ) ); |
| 7342 | |
| 7343 | /* Set sequence_number to zero */ |
| 7344 | memset( &ssl->cur_out_ctr[2], 0, sizeof( ssl->cur_out_ctr ) - 2 ); |
| 7345 | |
| 7346 | |
| 7347 | /* Increment epoch */ |
| 7348 | for( i = 2; i > 0; i-- ) |
| 7349 | if( ++ssl->cur_out_ctr[i - 1] != 0 ) |
| 7350 | break; |
| 7351 | |
| 7352 | /* The loop goes to its end iff the counter is wrapping */ |
| 7353 | if( i == 0 ) |
| 7354 | { |
| 7355 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) ); |
| 7356 | return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING ); |
| 7357 | } |
| 7358 | } |
| 7359 | else |
| 7360 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 7361 | memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) ); |
| 7362 | |
| 7363 | ssl->transform_out = ssl->transform_negotiate; |
| 7364 | ssl->session_out = ssl->session_negotiate; |
| 7365 | |
| 7366 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 7367 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 7368 | mbedtls_ssl_send_flight_completed( ssl ); |
| 7369 | #endif |
| 7370 | |
| 7371 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
| 7372 | { |
| 7373 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); |
| 7374 | return( ret ); |
| 7375 | } |
| 7376 | |
| 7377 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 7378 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 7379 | ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 ) |
| 7380 | { |
| 7381 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret ); |
| 7382 | return( ret ); |
| 7383 | } |
| 7384 | #endif |
| 7385 | |
| 7386 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) ); |
| 7387 | |
| 7388 | return( 0 ); |
| 7389 | } |
| 7390 | |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 7391 | #define SSL_MAX_HASH_LEN 12 |
| 7392 | |
| 7393 | int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl ) |
| 7394 | { |
| 7395 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 7396 | unsigned int hash_len = 12; |
| 7397 | unsigned char buf[SSL_MAX_HASH_LEN]; |
| 7398 | |
| 7399 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) ); |
| 7400 | |
| 7401 | ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 ); |
| 7402 | |
| 7403 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) |
| 7404 | { |
| 7405 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
| 7406 | goto exit; |
| 7407 | } |
| 7408 | |
| 7409 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) |
| 7410 | { |
| 7411 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) ); |
| 7412 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7413 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
| 7414 | ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; |
| 7415 | goto exit; |
| 7416 | } |
| 7417 | |
| 7418 | if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ) |
| 7419 | { |
| 7420 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7421 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
| 7422 | ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; |
| 7423 | goto exit; |
| 7424 | } |
| 7425 | |
| 7426 | if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len ) |
| 7427 | { |
| 7428 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) ); |
| 7429 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7430 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
| 7431 | ret = MBEDTLS_ERR_SSL_DECODE_ERROR; |
| 7432 | goto exit; |
| 7433 | } |
| 7434 | |
| 7435 | if( mbedtls_ct_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), |
| 7436 | buf, hash_len ) != 0 ) |
| 7437 | { |
| 7438 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) ); |
| 7439 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7440 | MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR ); |
| 7441 | ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE; |
| 7442 | goto exit; |
| 7443 | } |
| 7444 | |
| 7445 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 7446 | ssl->verify_data_len = hash_len; |
| 7447 | memcpy( ssl->peer_verify_data, buf, hash_len ); |
| 7448 | #endif |
| 7449 | |
| 7450 | if( ssl->handshake->resume != 0 ) |
| 7451 | { |
| 7452 | #if defined(MBEDTLS_SSL_CLI_C) |
| 7453 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 7454 | ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC; |
| 7455 | #endif |
| 7456 | #if defined(MBEDTLS_SSL_SRV_C) |
| 7457 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 7458 | ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP; |
| 7459 | #endif |
| 7460 | } |
| 7461 | else |
| 7462 | ssl->state++; |
| 7463 | |
| 7464 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 7465 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 7466 | mbedtls_ssl_recv_flight_completed( ssl ); |
| 7467 | #endif |
| 7468 | |
| 7469 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) ); |
| 7470 | |
| 7471 | exit: |
| 7472 | mbedtls_platform_zeroize( buf, hash_len ); |
| 7473 | return( ret ); |
| 7474 | } |
| 7475 | |
Jerry Yu | 392112c | 2022-02-17 14:34:10 +0800 | [diff] [blame] | 7476 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
| 7477 | /* |
| 7478 | * Helper to get TLS 1.2 PRF from ciphersuite |
| 7479 | * (Duplicates bits of logic from ssl_set_handshake_prfs().) |
| 7480 | */ |
| 7481 | static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id ) |
| 7482 | { |
Jerry Yu | 392112c | 2022-02-17 14:34:10 +0800 | [diff] [blame] | 7483 | const mbedtls_ssl_ciphersuite_t * const ciphersuite_info = |
Andrzej Kurek | 6832774 | 2022-10-03 06:18:18 -0400 | [diff] [blame] | 7484 | mbedtls_ssl_ciphersuite_from_id( ciphersuite_id ); |
| 7485 | #if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Leonid Rozenboim | e9d8dcd | 2022-08-08 15:57:48 -0700 | [diff] [blame] | 7486 | if( ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA384 ) |
Jerry Yu | 392112c | 2022-02-17 14:34:10 +0800 | [diff] [blame] | 7487 | return( tls_prf_sha384 ); |
Andrzej Kurek | 894edde | 2022-09-29 06:31:14 -0400 | [diff] [blame] | 7488 | else |
Jerry Yu | 392112c | 2022-02-17 14:34:10 +0800 | [diff] [blame] | 7489 | #endif |
Andrzej Kurek | 894edde | 2022-09-29 06:31:14 -0400 | [diff] [blame] | 7490 | #if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
| 7491 | { |
| 7492 | if( ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA256 ) |
| 7493 | return( tls_prf_sha256 ); |
| 7494 | } |
| 7495 | #endif |
| 7496 | #if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \ |
| 7497 | !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
| 7498 | (void) ciphersuite_info; |
| 7499 | #endif |
Andrzej Kurek | eabeb30 | 2022-10-17 07:52:51 -0400 | [diff] [blame] | 7500 | |
Andrzej Kurek | 894edde | 2022-09-29 06:31:14 -0400 | [diff] [blame] | 7501 | return( NULL ); |
Jerry Yu | 392112c | 2022-02-17 14:34:10 +0800 | [diff] [blame] | 7502 | } |
| 7503 | #endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */ |
Jerry Yu | e93ffcd | 2022-02-17 14:37:06 +0800 | [diff] [blame] | 7504 | |
| 7505 | static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf ) |
| 7506 | { |
| 7507 | ((void) tls_prf); |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 7508 | #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] | 7509 | if( tls_prf == tls_prf_sha384 ) |
| 7510 | { |
| 7511 | return( MBEDTLS_SSL_TLS_PRF_SHA384 ); |
| 7512 | } |
| 7513 | else |
| 7514 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 7515 | #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] | 7516 | if( tls_prf == tls_prf_sha256 ) |
| 7517 | { |
| 7518 | return( MBEDTLS_SSL_TLS_PRF_SHA256 ); |
| 7519 | } |
| 7520 | else |
| 7521 | #endif |
| 7522 | return( MBEDTLS_SSL_TLS_PRF_NONE ); |
| 7523 | } |
| 7524 | |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7525 | /* |
| 7526 | * Populate a transform structure with session keys and all the other |
| 7527 | * necessary information. |
| 7528 | * |
| 7529 | * Parameters: |
| 7530 | * - [in/out]: transform: structure to populate |
| 7531 | * [in] must be just initialised with mbedtls_ssl_transform_init() |
| 7532 | * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf() |
| 7533 | * - [in] ciphersuite |
| 7534 | * - [in] master |
| 7535 | * - [in] encrypt_then_mac |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7536 | * - [in] tls_prf: pointer to PRF to use for key derivation |
| 7537 | * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random |
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 7538 | * - [in] tls_version: TLS version |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7539 | * - [in] endpoint: client or server |
| 7540 | * - [in] ssl: used for: |
| 7541 | * - ssl->conf->{f,p}_export_keys |
| 7542 | * [in] optionally used for: |
| 7543 | * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg |
| 7544 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 7545 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7546 | static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform, |
| 7547 | int ciphersuite, |
| 7548 | const unsigned char master[48], |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 7549 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7550 | int encrypt_then_mac, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 7551 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7552 | ssl_tls_prf_t tls_prf, |
| 7553 | const unsigned char randbytes[64], |
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 7554 | mbedtls_ssl_protocol_version tls_version, |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7555 | unsigned endpoint, |
| 7556 | const mbedtls_ssl_context *ssl ) |
| 7557 | { |
| 7558 | int ret = 0; |
| 7559 | unsigned char keyblk[256]; |
| 7560 | unsigned char *key1; |
| 7561 | unsigned char *key2; |
| 7562 | unsigned char *mac_enc; |
| 7563 | unsigned char *mac_dec; |
| 7564 | size_t mac_key_len = 0; |
| 7565 | size_t iv_copy_len; |
| 7566 | size_t keylen; |
| 7567 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info; |
Neil Armstrong | 7fea33e | 2022-04-01 15:40:25 +0200 | [diff] [blame] | 7568 | mbedtls_ssl_mode_t ssl_mode; |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7569 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7570 | const mbedtls_cipher_info_t *cipher_info; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7571 | const mbedtls_md_info_t *md_info; |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7572 | #endif /* !MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7573 | |
| 7574 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7575 | psa_key_type_t key_type; |
| 7576 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7577 | psa_algorithm_t alg; |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7578 | psa_algorithm_t mac_alg = 0; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7579 | size_t key_bits; |
| 7580 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 7581 | #endif |
| 7582 | |
| 7583 | #if !defined(MBEDTLS_DEBUG_C) && \ |
| 7584 | !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 7585 | if( ssl->f_export_keys == NULL ) |
| 7586 | { |
| 7587 | ssl = NULL; /* make sure we don't use it except for these cases */ |
| 7588 | (void) ssl; |
| 7589 | } |
| 7590 | #endif |
| 7591 | |
| 7592 | /* |
| 7593 | * Some data just needs copying into the structure |
| 7594 | */ |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 7595 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7596 | transform->encrypt_then_mac = encrypt_then_mac; |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 7597 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 7598 | transform->tls_version = tls_version; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7599 | |
| 7600 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
| 7601 | memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) ); |
| 7602 | #endif |
| 7603 | |
| 7604 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 7605 | if( tls_version == MBEDTLS_SSL_VERSION_TLS1_3 ) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7606 | { |
| 7607 | /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform |
| 7608 | * generation separate. This should never happen. */ |
| 7609 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 7610 | } |
| 7611 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 7612 | |
| 7613 | /* |
| 7614 | * Get various info structures |
| 7615 | */ |
| 7616 | ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite ); |
| 7617 | if( ciphersuite_info == NULL ) |
| 7618 | { |
| 7619 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found", |
| 7620 | ciphersuite ) ); |
| 7621 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 7622 | } |
| 7623 | |
Neil Armstrong | ab555e0 | 2022-04-04 11:07:59 +0200 | [diff] [blame] | 7624 | ssl_mode = mbedtls_ssl_get_mode_from_ciphersuite( |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 7625 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Neil Armstrong | 7fea33e | 2022-04-01 15:40:25 +0200 | [diff] [blame] | 7626 | encrypt_then_mac, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 7627 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Neil Armstrong | 7fea33e | 2022-04-01 15:40:25 +0200 | [diff] [blame] | 7628 | ciphersuite_info ); |
| 7629 | |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7630 | if( ssl_mode == MBEDTLS_SSL_MODE_AEAD ) |
| 7631 | transform->taglen = |
| 7632 | ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16; |
| 7633 | |
| 7634 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7635 | if( ( status = mbedtls_ssl_cipher_to_psa( ciphersuite_info->cipher, |
| 7636 | transform->taglen, |
| 7637 | &alg, |
| 7638 | &key_type, |
| 7639 | &key_bits ) ) != PSA_SUCCESS ) |
| 7640 | { |
| 7641 | ret = psa_ssl_status_to_mbedtls( status ); |
| 7642 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_cipher_to_psa", ret ); |
| 7643 | goto end; |
| 7644 | } |
| 7645 | #else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7646 | cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher ); |
| 7647 | if( cipher_info == NULL ) |
| 7648 | { |
| 7649 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %u not found", |
| 7650 | ciphersuite_info->cipher ) ); |
| 7651 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 7652 | } |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7653 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7654 | |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7655 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Manuel Pégourié-Gonnard | abac037 | 2022-07-18 13:41:11 +0200 | [diff] [blame] | 7656 | mac_alg = mbedtls_hash_info_psa_from_md( ciphersuite_info->mac ); |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7657 | if( mac_alg == 0 ) |
| 7658 | { |
Manuel Pégourié-Gonnard | abac037 | 2022-07-18 13:41:11 +0200 | [diff] [blame] | 7659 | 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] | 7660 | (unsigned) ciphersuite_info->mac ) ); |
| 7661 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 7662 | } |
| 7663 | #else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7664 | md_info = mbedtls_md_info_from_type( ciphersuite_info->mac ); |
| 7665 | if( md_info == NULL ) |
| 7666 | { |
| 7667 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %u not found", |
| 7668 | (unsigned) ciphersuite_info->mac ) ); |
| 7669 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 7670 | } |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7671 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7672 | |
| 7673 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 7674 | /* Copy own and peer's CID if the use of the CID |
| 7675 | * extension has been negotiated. */ |
| 7676 | if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED ) |
| 7677 | { |
| 7678 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) ); |
| 7679 | |
| 7680 | transform->in_cid_len = ssl->own_cid_len; |
| 7681 | memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len ); |
| 7682 | MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid, |
| 7683 | transform->in_cid_len ); |
| 7684 | |
| 7685 | transform->out_cid_len = ssl->handshake->peer_cid_len; |
| 7686 | memcpy( transform->out_cid, ssl->handshake->peer_cid, |
| 7687 | ssl->handshake->peer_cid_len ); |
| 7688 | MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid, |
| 7689 | transform->out_cid_len ); |
| 7690 | } |
| 7691 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 7692 | |
| 7693 | /* |
| 7694 | * Compute key block using the PRF |
| 7695 | */ |
| 7696 | ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 ); |
| 7697 | if( ret != 0 ) |
| 7698 | { |
| 7699 | MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret ); |
| 7700 | return( ret ); |
| 7701 | } |
| 7702 | |
| 7703 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s", |
| 7704 | mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) ); |
| 7705 | MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 ); |
| 7706 | MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 ); |
| 7707 | MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 ); |
| 7708 | |
| 7709 | /* |
| 7710 | * Determine the appropriate key, IV and MAC length. |
| 7711 | */ |
| 7712 | |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7713 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7714 | keylen = PSA_BITS_TO_BYTES(key_bits); |
| 7715 | #else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7716 | keylen = mbedtls_cipher_info_get_key_bitlen( cipher_info ) / 8; |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7717 | #endif |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7718 | |
| 7719 | #if defined(MBEDTLS_GCM_C) || \ |
| 7720 | defined(MBEDTLS_CCM_C) || \ |
| 7721 | defined(MBEDTLS_CHACHAPOLY_C) |
Neil Armstrong | 7fea33e | 2022-04-01 15:40:25 +0200 | [diff] [blame] | 7722 | if( ssl_mode == MBEDTLS_SSL_MODE_AEAD ) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7723 | { |
| 7724 | size_t explicit_ivlen; |
| 7725 | |
| 7726 | transform->maclen = 0; |
| 7727 | mac_key_len = 0; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7728 | |
| 7729 | /* All modes haves 96-bit IVs, but the length of the static parts vary |
| 7730 | * with mode and version: |
| 7731 | * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes |
| 7732 | * (to be concatenated with a dynamically chosen IV of 8 Bytes) |
| 7733 | * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's |
| 7734 | * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record |
| 7735 | * sequence number). |
| 7736 | */ |
| 7737 | transform->ivlen = 12; |
David Horstmann | 3b2276a | 2022-10-06 14:49:08 +0100 | [diff] [blame] | 7738 | |
| 7739 | int is_chachapoly = 0; |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7740 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
David Horstmann | 3b2276a | 2022-10-06 14:49:08 +0100 | [diff] [blame] | 7741 | is_chachapoly = ( key_type == PSA_KEY_TYPE_CHACHA20 ); |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7742 | #else |
David Horstmann | 3b2276a | 2022-10-06 14:49:08 +0100 | [diff] [blame] | 7743 | is_chachapoly = ( mbedtls_cipher_info_get_mode( cipher_info ) |
| 7744 | == MBEDTLS_MODE_CHACHAPOLY ); |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7745 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
David Horstmann | 3b2276a | 2022-10-06 14:49:08 +0100 | [diff] [blame] | 7746 | |
| 7747 | if( is_chachapoly ) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7748 | transform->fixed_ivlen = 12; |
| 7749 | else |
| 7750 | transform->fixed_ivlen = 4; |
| 7751 | |
| 7752 | /* Minimum length of encrypted record */ |
| 7753 | explicit_ivlen = transform->ivlen - transform->fixed_ivlen; |
| 7754 | transform->minlen = explicit_ivlen + transform->taglen; |
| 7755 | } |
| 7756 | else |
| 7757 | #endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */ |
| 7758 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
Neil Armstrong | 7fea33e | 2022-04-01 15:40:25 +0200 | [diff] [blame] | 7759 | if( ssl_mode == MBEDTLS_SSL_MODE_STREAM || |
| 7760 | ssl_mode == MBEDTLS_SSL_MODE_CBC || |
| 7761 | ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM ) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7762 | { |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7763 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Neil Armstrong | d1be767 | 2022-04-04 11:21:41 +0200 | [diff] [blame] | 7764 | size_t block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ); |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7765 | #else |
| 7766 | size_t block_size = cipher_info->block_size; |
| 7767 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 7768 | |
| 7769 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7770 | /* Get MAC length */ |
| 7771 | mac_key_len = PSA_HASH_LENGTH(mac_alg); |
| 7772 | #else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7773 | /* Initialize HMAC contexts */ |
| 7774 | if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 || |
| 7775 | ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 ) |
| 7776 | { |
| 7777 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret ); |
| 7778 | goto end; |
| 7779 | } |
| 7780 | |
| 7781 | /* Get MAC length */ |
| 7782 | mac_key_len = mbedtls_md_get_size( md_info ); |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7783 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7784 | transform->maclen = mac_key_len; |
| 7785 | |
| 7786 | /* IV length */ |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7787 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Neil Armstrong | 2230e6c | 2022-04-27 10:36:14 +0200 | [diff] [blame] | 7788 | transform->ivlen = PSA_CIPHER_IV_LENGTH( key_type, alg ); |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7789 | #else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7790 | transform->ivlen = cipher_info->iv_size; |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7791 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7792 | |
| 7793 | /* Minimum length */ |
Neil Armstrong | 7fea33e | 2022-04-01 15:40:25 +0200 | [diff] [blame] | 7794 | if( ssl_mode == MBEDTLS_SSL_MODE_STREAM ) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7795 | transform->minlen = transform->maclen; |
| 7796 | else |
| 7797 | { |
| 7798 | /* |
| 7799 | * GenericBlockCipher: |
| 7800 | * 1. if EtM is in use: one block plus MAC |
| 7801 | * otherwise: * first multiple of blocklen greater than maclen |
| 7802 | * 2. IV |
| 7803 | */ |
| 7804 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Neil Armstrong | 7fea33e | 2022-04-01 15:40:25 +0200 | [diff] [blame] | 7805 | if( ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM ) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7806 | { |
| 7807 | transform->minlen = transform->maclen |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7808 | + block_size; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7809 | } |
| 7810 | else |
| 7811 | #endif |
| 7812 | { |
| 7813 | transform->minlen = transform->maclen |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7814 | + block_size |
| 7815 | - transform->maclen % block_size; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7816 | } |
| 7817 | |
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 7818 | if( tls_version == MBEDTLS_SSL_VERSION_TLS1_2 ) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7819 | { |
| 7820 | transform->minlen += transform->ivlen; |
| 7821 | } |
| 7822 | else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7823 | { |
| 7824 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 7825 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 7826 | goto end; |
| 7827 | } |
| 7828 | } |
| 7829 | } |
| 7830 | else |
| 7831 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
| 7832 | { |
| 7833 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 7834 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 7835 | } |
| 7836 | |
| 7837 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u", |
| 7838 | (unsigned) keylen, |
| 7839 | (unsigned) transform->minlen, |
| 7840 | (unsigned) transform->ivlen, |
| 7841 | (unsigned) transform->maclen ) ); |
| 7842 | |
| 7843 | /* |
| 7844 | * Finally setup the cipher contexts, IVs and MAC secrets. |
| 7845 | */ |
| 7846 | #if defined(MBEDTLS_SSL_CLI_C) |
| 7847 | if( endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 7848 | { |
| 7849 | key1 = keyblk + mac_key_len * 2; |
| 7850 | key2 = keyblk + mac_key_len * 2 + keylen; |
| 7851 | |
| 7852 | mac_enc = keyblk; |
| 7853 | mac_dec = keyblk + mac_key_len; |
| 7854 | |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7855 | iv_copy_len = ( transform->fixed_ivlen ) ? |
| 7856 | transform->fixed_ivlen : transform->ivlen; |
| 7857 | memcpy( transform->iv_enc, key2 + keylen, iv_copy_len ); |
| 7858 | memcpy( transform->iv_dec, key2 + keylen + iv_copy_len, |
| 7859 | iv_copy_len ); |
| 7860 | } |
| 7861 | else |
| 7862 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 7863 | #if defined(MBEDTLS_SSL_SRV_C) |
| 7864 | if( endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 7865 | { |
| 7866 | key1 = keyblk + mac_key_len * 2 + keylen; |
| 7867 | key2 = keyblk + mac_key_len * 2; |
| 7868 | |
| 7869 | mac_enc = keyblk + mac_key_len; |
| 7870 | mac_dec = keyblk; |
| 7871 | |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7872 | iv_copy_len = ( transform->fixed_ivlen ) ? |
| 7873 | transform->fixed_ivlen : transform->ivlen; |
| 7874 | memcpy( transform->iv_dec, key1 + keylen, iv_copy_len ); |
| 7875 | memcpy( transform->iv_enc, key1 + keylen + iv_copy_len, |
| 7876 | iv_copy_len ); |
| 7877 | } |
| 7878 | else |
| 7879 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 7880 | { |
| 7881 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 7882 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 7883 | goto end; |
| 7884 | } |
| 7885 | |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7886 | if( ssl != NULL && ssl->f_export_keys != NULL ) |
| 7887 | { |
| 7888 | ssl->f_export_keys( ssl->p_export_keys, |
| 7889 | MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET, |
| 7890 | master, 48, |
| 7891 | randbytes + 32, |
| 7892 | randbytes, |
| 7893 | tls_prf_get_type( tls_prf ) ); |
| 7894 | } |
| 7895 | |
| 7896 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7897 | transform->psa_alg = alg; |
| 7898 | |
| 7899 | if ( alg != MBEDTLS_SSL_NULL_CIPHER ) |
| 7900 | { |
| 7901 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 7902 | psa_set_key_algorithm( &attributes, alg ); |
| 7903 | psa_set_key_type( &attributes, key_type ); |
| 7904 | |
| 7905 | if( ( status = psa_import_key( &attributes, |
| 7906 | key1, |
| 7907 | PSA_BITS_TO_BYTES( key_bits ), |
| 7908 | &transform->psa_key_enc ) ) != PSA_SUCCESS ) |
| 7909 | { |
| 7910 | MBEDTLS_SSL_DEBUG_RET( 3, "psa_import_key", (int)status ); |
| 7911 | ret = psa_ssl_status_to_mbedtls( status ); |
| 7912 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret ); |
| 7913 | goto end; |
| 7914 | } |
| 7915 | |
| 7916 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 7917 | |
| 7918 | if( ( status = psa_import_key( &attributes, |
| 7919 | key2, |
| 7920 | PSA_BITS_TO_BYTES( key_bits ), |
| 7921 | &transform->psa_key_dec ) ) != PSA_SUCCESS ) |
| 7922 | { |
| 7923 | ret = psa_ssl_status_to_mbedtls( status ); |
| 7924 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret ); |
| 7925 | goto end; |
| 7926 | } |
| 7927 | } |
| 7928 | #else |
| 7929 | if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc, |
| 7930 | cipher_info ) ) != 0 ) |
| 7931 | { |
| 7932 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret ); |
| 7933 | goto end; |
| 7934 | } |
| 7935 | |
| 7936 | if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec, |
| 7937 | cipher_info ) ) != 0 ) |
| 7938 | { |
| 7939 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret ); |
| 7940 | goto end; |
| 7941 | } |
| 7942 | |
| 7943 | if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1, |
| 7944 | (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ), |
| 7945 | MBEDTLS_ENCRYPT ) ) != 0 ) |
| 7946 | { |
| 7947 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret ); |
| 7948 | goto end; |
| 7949 | } |
| 7950 | |
| 7951 | if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2, |
| 7952 | (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ), |
| 7953 | MBEDTLS_DECRYPT ) ) != 0 ) |
| 7954 | { |
| 7955 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret ); |
| 7956 | goto end; |
| 7957 | } |
| 7958 | |
| 7959 | #if defined(MBEDTLS_CIPHER_MODE_CBC) |
| 7960 | if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CBC ) |
| 7961 | { |
| 7962 | if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc, |
| 7963 | MBEDTLS_PADDING_NONE ) ) != 0 ) |
| 7964 | { |
| 7965 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret ); |
| 7966 | goto end; |
| 7967 | } |
| 7968 | |
| 7969 | if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec, |
| 7970 | MBEDTLS_PADDING_NONE ) ) != 0 ) |
| 7971 | { |
| 7972 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret ); |
| 7973 | goto end; |
| 7974 | } |
| 7975 | } |
| 7976 | #endif /* MBEDTLS_CIPHER_MODE_CBC */ |
| 7977 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 7978 | |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 7979 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
| 7980 | /* For HMAC-based ciphersuites, initialize the HMAC transforms. |
| 7981 | For AEAD-based ciphersuites, there is nothing to do here. */ |
| 7982 | if( mac_key_len != 0 ) |
| 7983 | { |
| 7984 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7985 | transform->psa_mac_alg = PSA_ALG_HMAC( mac_alg ); |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 7986 | |
| 7987 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE ); |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7988 | psa_set_key_algorithm( &attributes, PSA_ALG_HMAC( mac_alg ) ); |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 7989 | psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC ); |
| 7990 | |
| 7991 | if( ( status = psa_import_key( &attributes, |
| 7992 | mac_enc, mac_key_len, |
| 7993 | &transform->psa_mac_enc ) ) != PSA_SUCCESS ) |
| 7994 | { |
| 7995 | ret = psa_ssl_status_to_mbedtls( status ); |
| 7996 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret ); |
| 7997 | goto end; |
| 7998 | } |
| 7999 | |
Ronald Cron | fb39f15 | 2022-03-25 14:36:28 +0100 | [diff] [blame] | 8000 | if( ( transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER ) || |
Andrzej Kurek | 8c95ac4 | 2022-08-17 16:17:00 -0400 | [diff] [blame] | 8001 | ( ( transform->psa_alg == PSA_ALG_CBC_NO_PADDING ) |
| 8002 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
| 8003 | && ( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED ) |
| 8004 | #endif |
| 8005 | ) ) |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 8006 | /* mbedtls_ct_hmac() requires the key to be exportable */ |
| 8007 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT | |
| 8008 | PSA_KEY_USAGE_VERIFY_HASH ); |
| 8009 | else |
| 8010 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH ); |
| 8011 | |
| 8012 | if( ( status = psa_import_key( &attributes, |
| 8013 | mac_dec, mac_key_len, |
| 8014 | &transform->psa_mac_dec ) ) != PSA_SUCCESS ) |
| 8015 | { |
| 8016 | ret = psa_ssl_status_to_mbedtls( status ); |
| 8017 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret ); |
| 8018 | goto end; |
| 8019 | } |
| 8020 | #else |
| 8021 | ret = mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len ); |
| 8022 | if( ret != 0 ) |
| 8023 | goto end; |
| 8024 | ret = mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len ); |
| 8025 | if( ret != 0 ) |
| 8026 | goto end; |
| 8027 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 8028 | } |
| 8029 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
| 8030 | |
| 8031 | ((void) mac_dec); |
| 8032 | ((void) mac_enc); |
| 8033 | |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8034 | end: |
| 8035 | mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) ); |
| 8036 | return( ret ); |
| 8037 | } |
| 8038 | |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 8039 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 8040 | int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl, |
| 8041 | unsigned char *hash, size_t *hashlen, |
| 8042 | unsigned char *data, size_t data_len, |
| 8043 | mbedtls_md_type_t md_alg ) |
| 8044 | { |
| 8045 | psa_status_t status; |
| 8046 | psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT; |
Manuel Pégourié-Gonnard | abac037 | 2022-07-18 13:41:11 +0200 | [diff] [blame] | 8047 | 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] | 8048 | |
| 8049 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) ); |
| 8050 | |
| 8051 | if( ( status = psa_hash_setup( &hash_operation, |
| 8052 | hash_alg ) ) != PSA_SUCCESS ) |
| 8053 | { |
| 8054 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status ); |
| 8055 | goto exit; |
| 8056 | } |
| 8057 | |
| 8058 | if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes, |
| 8059 | 64 ) ) != PSA_SUCCESS ) |
| 8060 | { |
| 8061 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status ); |
| 8062 | goto exit; |
| 8063 | } |
| 8064 | |
| 8065 | if( ( status = psa_hash_update( &hash_operation, |
| 8066 | data, data_len ) ) != PSA_SUCCESS ) |
| 8067 | { |
| 8068 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status ); |
| 8069 | goto exit; |
| 8070 | } |
| 8071 | |
| 8072 | if( ( status = psa_hash_finish( &hash_operation, hash, PSA_HASH_MAX_SIZE, |
| 8073 | hashlen ) ) != PSA_SUCCESS ) |
| 8074 | { |
| 8075 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status ); |
| 8076 | goto exit; |
| 8077 | } |
| 8078 | |
| 8079 | exit: |
| 8080 | if( status != PSA_SUCCESS ) |
| 8081 | { |
| 8082 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 8083 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
| 8084 | switch( status ) |
| 8085 | { |
| 8086 | case PSA_ERROR_NOT_SUPPORTED: |
| 8087 | return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE ); |
| 8088 | case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */ |
| 8089 | case PSA_ERROR_BUFFER_TOO_SMALL: |
| 8090 | return( MBEDTLS_ERR_MD_BAD_INPUT_DATA ); |
| 8091 | case PSA_ERROR_INSUFFICIENT_MEMORY: |
| 8092 | return( MBEDTLS_ERR_MD_ALLOC_FAILED ); |
| 8093 | default: |
| 8094 | return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED ); |
| 8095 | } |
| 8096 | } |
| 8097 | return( 0 ); |
| 8098 | } |
| 8099 | |
| 8100 | #else |
| 8101 | |
| 8102 | int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl, |
| 8103 | unsigned char *hash, size_t *hashlen, |
| 8104 | unsigned char *data, size_t data_len, |
| 8105 | mbedtls_md_type_t md_alg ) |
| 8106 | { |
| 8107 | int ret = 0; |
| 8108 | mbedtls_md_context_t ctx; |
| 8109 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg ); |
| 8110 | *hashlen = mbedtls_md_get_size( md_info ); |
| 8111 | |
| 8112 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) ); |
| 8113 | |
| 8114 | mbedtls_md_init( &ctx ); |
| 8115 | |
| 8116 | /* |
| 8117 | * digitally-signed struct { |
| 8118 | * opaque client_random[32]; |
| 8119 | * opaque server_random[32]; |
| 8120 | * ServerDHParams params; |
| 8121 | * }; |
| 8122 | */ |
| 8123 | if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 ) |
| 8124 | { |
| 8125 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret ); |
| 8126 | goto exit; |
| 8127 | } |
| 8128 | if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 ) |
| 8129 | { |
| 8130 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret ); |
| 8131 | goto exit; |
| 8132 | } |
| 8133 | if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 ) |
| 8134 | { |
| 8135 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret ); |
| 8136 | goto exit; |
| 8137 | } |
| 8138 | if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 ) |
| 8139 | { |
| 8140 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret ); |
| 8141 | goto exit; |
| 8142 | } |
| 8143 | if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 ) |
| 8144 | { |
| 8145 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret ); |
| 8146 | goto exit; |
| 8147 | } |
| 8148 | |
| 8149 | exit: |
| 8150 | mbedtls_md_free( &ctx ); |
| 8151 | |
| 8152 | if( ret != 0 ) |
| 8153 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 8154 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
| 8155 | |
| 8156 | return( ret ); |
| 8157 | } |
| 8158 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 8159 | |
Jerry Yu | d9d91da | 2022-02-17 14:57:06 +0800 | [diff] [blame] | 8160 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
| 8161 | |
Gabor Mezei | a3d016c | 2022-05-10 12:44:09 +0200 | [diff] [blame] | 8162 | /* Find the preferred hash for a given signature algorithm. */ |
| 8163 | unsigned int mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg( |
| 8164 | mbedtls_ssl_context *ssl, |
| 8165 | unsigned int sig_alg ) |
Jerry Yu | d9d91da | 2022-02-17 14:57:06 +0800 | [diff] [blame] | 8166 | { |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 8167 | unsigned int i; |
Gabor Mezei | a3d016c | 2022-05-10 12:44:09 +0200 | [diff] [blame] | 8168 | uint16_t *received_sig_algs = ssl->handshake->received_sig_algs; |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 8169 | |
| 8170 | if( sig_alg == MBEDTLS_SSL_SIG_ANON ) |
Gabor Mezei | a3d016c | 2022-05-10 12:44:09 +0200 | [diff] [blame] | 8171 | return( MBEDTLS_SSL_HASH_NONE ); |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 8172 | |
Gabor Mezei | a3d016c | 2022-05-10 12:44:09 +0200 | [diff] [blame] | 8173 | for( i = 0; received_sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ ) |
Jerry Yu | d9d91da | 2022-02-17 14:57:06 +0800 | [diff] [blame] | 8174 | { |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 8175 | unsigned int hash_alg_received = |
| 8176 | MBEDTLS_SSL_TLS12_HASH_ALG_FROM_SIG_AND_HASH_ALG( |
| 8177 | received_sig_algs[i] ); |
| 8178 | unsigned int sig_alg_received = |
| 8179 | MBEDTLS_SSL_TLS12_SIG_ALG_FROM_SIG_AND_HASH_ALG( |
| 8180 | received_sig_algs[i] ); |
| 8181 | |
| 8182 | if( sig_alg == sig_alg_received ) |
| 8183 | { |
| 8184 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 8185 | if( ssl->handshake->key_cert && ssl->handshake->key_cert->key ) |
| 8186 | { |
Neil Armstrong | 96eceb8 | 2022-06-30 18:05:05 +0200 | [diff] [blame] | 8187 | psa_algorithm_t psa_hash_alg = |
Manuel Pégourié-Gonnard | abac037 | 2022-07-18 13:41:11 +0200 | [diff] [blame] | 8188 | mbedtls_hash_info_psa_from_md( hash_alg_received ); |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 8189 | |
Neil Armstrong | 96eceb8 | 2022-06-30 18:05:05 +0200 | [diff] [blame] | 8190 | if( sig_alg_received == MBEDTLS_SSL_SIG_ECDSA && |
| 8191 | ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key, |
| 8192 | PSA_ALG_ECDSA( psa_hash_alg ), |
| 8193 | PSA_KEY_USAGE_SIGN_HASH ) ) |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 8194 | continue; |
| 8195 | |
Neil Armstrong | 96eceb8 | 2022-06-30 18:05:05 +0200 | [diff] [blame] | 8196 | if( sig_alg_received == MBEDTLS_SSL_SIG_RSA && |
Neil Armstrong | 971f30d | 2022-07-01 16:23:50 +0200 | [diff] [blame] | 8197 | ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key, |
| 8198 | PSA_ALG_RSA_PKCS1V15_SIGN( |
| 8199 | psa_hash_alg ), |
| 8200 | PSA_KEY_USAGE_SIGN_HASH ) ) |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 8201 | continue; |
| 8202 | } |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 8203 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Neil Armstrong | 96eceb8 | 2022-06-30 18:05:05 +0200 | [diff] [blame] | 8204 | |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 8205 | return( hash_alg_received ); |
| 8206 | } |
Jerry Yu | d9d91da | 2022-02-17 14:57:06 +0800 | [diff] [blame] | 8207 | } |
Jerry Yu | d9d91da | 2022-02-17 14:57:06 +0800 | [diff] [blame] | 8208 | |
Gabor Mezei | a3d016c | 2022-05-10 12:44:09 +0200 | [diff] [blame] | 8209 | return( MBEDTLS_SSL_HASH_NONE ); |
Jerry Yu | d9d91da | 2022-02-17 14:57:06 +0800 | [diff] [blame] | 8210 | } |
| 8211 | |
| 8212 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 8213 | |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8214 | /* Serialization of TLS 1.2 sessions: |
| 8215 | * |
| 8216 | * struct { |
| 8217 | * uint64 start_time; |
| 8218 | * uint8 ciphersuite[2]; // defined by the standard |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8219 | * uint8 session_id_len; // at most 32 |
| 8220 | * opaque session_id[32]; |
| 8221 | * opaque master[48]; // fixed length in the standard |
| 8222 | * uint32 verify_result; |
| 8223 | * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert |
| 8224 | * opaque ticket<0..2^24-1>; // length 0 means no ticket |
| 8225 | * uint32 ticket_lifetime; |
| 8226 | * uint8 mfl_code; // up to 255 according to standard |
| 8227 | * uint8 encrypt_then_mac; // 0 or 1 |
| 8228 | * } serialized_session_tls12; |
| 8229 | * |
| 8230 | */ |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 8231 | static size_t ssl_tls12_session_save( const mbedtls_ssl_session *session, |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8232 | unsigned char *buf, |
| 8233 | size_t buf_len ) |
| 8234 | { |
| 8235 | unsigned char *p = buf; |
| 8236 | size_t used = 0; |
| 8237 | |
| 8238 | #if defined(MBEDTLS_HAVE_TIME) |
| 8239 | uint64_t start; |
| 8240 | #endif |
| 8241 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 8242 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 8243 | size_t cert_len; |
| 8244 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 8245 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 8246 | |
| 8247 | /* |
| 8248 | * Time |
| 8249 | */ |
| 8250 | #if defined(MBEDTLS_HAVE_TIME) |
| 8251 | used += 8; |
| 8252 | |
| 8253 | if( used <= buf_len ) |
| 8254 | { |
| 8255 | start = (uint64_t) session->start; |
| 8256 | |
| 8257 | MBEDTLS_PUT_UINT64_BE( start, p, 0 ); |
| 8258 | p += 8; |
| 8259 | } |
| 8260 | #endif /* MBEDTLS_HAVE_TIME */ |
| 8261 | |
| 8262 | /* |
| 8263 | * Basic mandatory fields |
| 8264 | */ |
| 8265 | used += 2 /* ciphersuite */ |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8266 | + 1 /* id_len */ |
| 8267 | + sizeof( session->id ) |
| 8268 | + sizeof( session->master ) |
| 8269 | + 4; /* verify_result */ |
| 8270 | |
| 8271 | if( used <= buf_len ) |
| 8272 | { |
| 8273 | MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 0 ); |
| 8274 | p += 2; |
| 8275 | |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8276 | *p++ = MBEDTLS_BYTE_0( session->id_len ); |
| 8277 | memcpy( p, session->id, 32 ); |
| 8278 | p += 32; |
| 8279 | |
| 8280 | memcpy( p, session->master, 48 ); |
| 8281 | p += 48; |
| 8282 | |
| 8283 | MBEDTLS_PUT_UINT32_BE( session->verify_result, p, 0 ); |
| 8284 | p += 4; |
| 8285 | } |
| 8286 | |
| 8287 | /* |
| 8288 | * Peer's end-entity certificate |
| 8289 | */ |
| 8290 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 8291 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 8292 | if( session->peer_cert == NULL ) |
| 8293 | cert_len = 0; |
| 8294 | else |
| 8295 | cert_len = session->peer_cert->raw.len; |
| 8296 | |
| 8297 | used += 3 + cert_len; |
| 8298 | |
| 8299 | if( used <= buf_len ) |
| 8300 | { |
| 8301 | *p++ = MBEDTLS_BYTE_2( cert_len ); |
| 8302 | *p++ = MBEDTLS_BYTE_1( cert_len ); |
| 8303 | *p++ = MBEDTLS_BYTE_0( cert_len ); |
| 8304 | |
| 8305 | if( session->peer_cert != NULL ) |
| 8306 | { |
| 8307 | memcpy( p, session->peer_cert->raw.p, cert_len ); |
| 8308 | p += cert_len; |
| 8309 | } |
| 8310 | } |
| 8311 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 8312 | if( session->peer_cert_digest != NULL ) |
| 8313 | { |
| 8314 | used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len; |
| 8315 | if( used <= buf_len ) |
| 8316 | { |
| 8317 | *p++ = (unsigned char) session->peer_cert_digest_type; |
| 8318 | *p++ = (unsigned char) session->peer_cert_digest_len; |
| 8319 | memcpy( p, session->peer_cert_digest, |
| 8320 | session->peer_cert_digest_len ); |
| 8321 | p += session->peer_cert_digest_len; |
| 8322 | } |
| 8323 | } |
| 8324 | else |
| 8325 | { |
| 8326 | used += 2; |
| 8327 | if( used <= buf_len ) |
| 8328 | { |
| 8329 | *p++ = (unsigned char) MBEDTLS_MD_NONE; |
| 8330 | *p++ = 0; |
| 8331 | } |
| 8332 | } |
| 8333 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 8334 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 8335 | |
| 8336 | /* |
| 8337 | * Session ticket if any, plus associated data |
| 8338 | */ |
| 8339 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
| 8340 | used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */ |
| 8341 | |
| 8342 | if( used <= buf_len ) |
| 8343 | { |
| 8344 | *p++ = MBEDTLS_BYTE_2( session->ticket_len ); |
| 8345 | *p++ = MBEDTLS_BYTE_1( session->ticket_len ); |
| 8346 | *p++ = MBEDTLS_BYTE_0( session->ticket_len ); |
| 8347 | |
| 8348 | if( session->ticket != NULL ) |
| 8349 | { |
| 8350 | memcpy( p, session->ticket, session->ticket_len ); |
| 8351 | p += session->ticket_len; |
| 8352 | } |
| 8353 | |
| 8354 | MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 ); |
| 8355 | p += 4; |
| 8356 | } |
| 8357 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
| 8358 | |
| 8359 | /* |
| 8360 | * Misc extension-related info |
| 8361 | */ |
| 8362 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 8363 | used += 1; |
| 8364 | |
| 8365 | if( used <= buf_len ) |
| 8366 | *p++ = session->mfl_code; |
| 8367 | #endif |
| 8368 | |
| 8369 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 8370 | used += 1; |
| 8371 | |
| 8372 | if( used <= buf_len ) |
| 8373 | *p++ = MBEDTLS_BYTE_0( session->encrypt_then_mac ); |
| 8374 | #endif |
| 8375 | |
| 8376 | return( used ); |
| 8377 | } |
| 8378 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 8379 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 8380 | static int ssl_tls12_session_load( mbedtls_ssl_session *session, |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8381 | const unsigned char *buf, |
| 8382 | size_t len ) |
| 8383 | { |
| 8384 | #if defined(MBEDTLS_HAVE_TIME) |
| 8385 | uint64_t start; |
| 8386 | #endif |
| 8387 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 8388 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 8389 | size_t cert_len; |
| 8390 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 8391 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 8392 | |
| 8393 | const unsigned char *p = buf; |
| 8394 | const unsigned char * const end = buf + len; |
| 8395 | |
| 8396 | /* |
| 8397 | * Time |
| 8398 | */ |
| 8399 | #if defined(MBEDTLS_HAVE_TIME) |
| 8400 | if( 8 > (size_t)( end - p ) ) |
| 8401 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8402 | |
| 8403 | start = ( (uint64_t) p[0] << 56 ) | |
| 8404 | ( (uint64_t) p[1] << 48 ) | |
| 8405 | ( (uint64_t) p[2] << 40 ) | |
| 8406 | ( (uint64_t) p[3] << 32 ) | |
| 8407 | ( (uint64_t) p[4] << 24 ) | |
| 8408 | ( (uint64_t) p[5] << 16 ) | |
| 8409 | ( (uint64_t) p[6] << 8 ) | |
| 8410 | ( (uint64_t) p[7] ); |
| 8411 | p += 8; |
| 8412 | |
| 8413 | session->start = (time_t) start; |
| 8414 | #endif /* MBEDTLS_HAVE_TIME */ |
| 8415 | |
| 8416 | /* |
| 8417 | * Basic mandatory fields |
| 8418 | */ |
Thomas Daubney | 20f89a9 | 2022-06-20 15:12:19 +0100 | [diff] [blame] | 8419 | if( 2 + 1 + 32 + 48 + 4 > (size_t)( end - p ) ) |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8420 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8421 | |
| 8422 | session->ciphersuite = ( p[0] << 8 ) | p[1]; |
| 8423 | p += 2; |
| 8424 | |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8425 | session->id_len = *p++; |
| 8426 | memcpy( session->id, p, 32 ); |
| 8427 | p += 32; |
| 8428 | |
| 8429 | memcpy( session->master, p, 48 ); |
| 8430 | p += 48; |
| 8431 | |
| 8432 | session->verify_result = ( (uint32_t) p[0] << 24 ) | |
| 8433 | ( (uint32_t) p[1] << 16 ) | |
| 8434 | ( (uint32_t) p[2] << 8 ) | |
| 8435 | ( (uint32_t) p[3] ); |
| 8436 | p += 4; |
| 8437 | |
| 8438 | /* Immediately clear invalid pointer values that have been read, in case |
| 8439 | * we exit early before we replaced them with valid ones. */ |
| 8440 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 8441 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 8442 | session->peer_cert = NULL; |
| 8443 | #else |
| 8444 | session->peer_cert_digest = NULL; |
| 8445 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 8446 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 8447 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
| 8448 | session->ticket = NULL; |
| 8449 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
| 8450 | |
| 8451 | /* |
| 8452 | * Peer certificate |
| 8453 | */ |
| 8454 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 8455 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 8456 | /* Deserialize CRT from the end of the ticket. */ |
| 8457 | if( 3 > (size_t)( end - p ) ) |
| 8458 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8459 | |
| 8460 | cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2]; |
| 8461 | p += 3; |
| 8462 | |
| 8463 | if( cert_len != 0 ) |
| 8464 | { |
| 8465 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 8466 | |
| 8467 | if( cert_len > (size_t)( end - p ) ) |
| 8468 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8469 | |
| 8470 | session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ); |
| 8471 | |
| 8472 | if( session->peer_cert == NULL ) |
| 8473 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 8474 | |
| 8475 | mbedtls_x509_crt_init( session->peer_cert ); |
| 8476 | |
| 8477 | if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert, |
| 8478 | p, cert_len ) ) != 0 ) |
| 8479 | { |
| 8480 | mbedtls_x509_crt_free( session->peer_cert ); |
| 8481 | mbedtls_free( session->peer_cert ); |
| 8482 | session->peer_cert = NULL; |
| 8483 | return( ret ); |
| 8484 | } |
| 8485 | |
| 8486 | p += cert_len; |
| 8487 | } |
| 8488 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 8489 | /* Deserialize CRT digest from the end of the ticket. */ |
| 8490 | if( 2 > (size_t)( end - p ) ) |
| 8491 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8492 | |
| 8493 | session->peer_cert_digest_type = (mbedtls_md_type_t) *p++; |
| 8494 | session->peer_cert_digest_len = (size_t) *p++; |
| 8495 | |
| 8496 | if( session->peer_cert_digest_len != 0 ) |
| 8497 | { |
| 8498 | const mbedtls_md_info_t *md_info = |
| 8499 | mbedtls_md_info_from_type( session->peer_cert_digest_type ); |
| 8500 | if( md_info == NULL ) |
| 8501 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8502 | if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) ) |
| 8503 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8504 | |
| 8505 | if( session->peer_cert_digest_len > (size_t)( end - p ) ) |
| 8506 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8507 | |
| 8508 | session->peer_cert_digest = |
| 8509 | mbedtls_calloc( 1, session->peer_cert_digest_len ); |
| 8510 | if( session->peer_cert_digest == NULL ) |
| 8511 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 8512 | |
| 8513 | memcpy( session->peer_cert_digest, p, |
| 8514 | session->peer_cert_digest_len ); |
| 8515 | p += session->peer_cert_digest_len; |
| 8516 | } |
| 8517 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 8518 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 8519 | |
| 8520 | /* |
| 8521 | * Session ticket and associated data |
| 8522 | */ |
| 8523 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
| 8524 | if( 3 > (size_t)( end - p ) ) |
| 8525 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8526 | |
| 8527 | session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2]; |
| 8528 | p += 3; |
| 8529 | |
| 8530 | if( session->ticket_len != 0 ) |
| 8531 | { |
| 8532 | if( session->ticket_len > (size_t)( end - p ) ) |
| 8533 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8534 | |
| 8535 | session->ticket = mbedtls_calloc( 1, session->ticket_len ); |
| 8536 | if( session->ticket == NULL ) |
| 8537 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 8538 | |
| 8539 | memcpy( session->ticket, p, session->ticket_len ); |
| 8540 | p += session->ticket_len; |
| 8541 | } |
| 8542 | |
| 8543 | if( 4 > (size_t)( end - p ) ) |
| 8544 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8545 | |
| 8546 | session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) | |
| 8547 | ( (uint32_t) p[1] << 16 ) | |
| 8548 | ( (uint32_t) p[2] << 8 ) | |
| 8549 | ( (uint32_t) p[3] ); |
| 8550 | p += 4; |
| 8551 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
| 8552 | |
| 8553 | /* |
| 8554 | * Misc extension-related info |
| 8555 | */ |
| 8556 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 8557 | if( 1 > (size_t)( end - p ) ) |
| 8558 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8559 | |
| 8560 | session->mfl_code = *p++; |
| 8561 | #endif |
| 8562 | |
| 8563 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 8564 | if( 1 > (size_t)( end - p ) ) |
| 8565 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8566 | |
| 8567 | session->encrypt_then_mac = *p++; |
| 8568 | #endif |
| 8569 | |
| 8570 | /* Done, should have consumed entire buffer */ |
| 8571 | if( p != end ) |
| 8572 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8573 | |
| 8574 | return( 0 ); |
| 8575 | } |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 8576 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 8577 | |
XiaokangQian | 75d40ef | 2022-04-20 11:05:24 +0000 | [diff] [blame] | 8578 | int mbedtls_ssl_validate_ciphersuite( |
| 8579 | const mbedtls_ssl_context *ssl, |
| 8580 | const mbedtls_ssl_ciphersuite_t *suite_info, |
| 8581 | mbedtls_ssl_protocol_version min_tls_version, |
| 8582 | mbedtls_ssl_protocol_version max_tls_version ) |
| 8583 | { |
| 8584 | (void) ssl; |
| 8585 | |
| 8586 | if( suite_info == NULL ) |
| 8587 | return( -1 ); |
| 8588 | |
| 8589 | if( ( suite_info->min_tls_version > max_tls_version ) || |
| 8590 | ( suite_info->max_tls_version < min_tls_version ) ) |
| 8591 | { |
| 8592 | return( -1 ); |
| 8593 | } |
| 8594 | |
XiaokangQian | 060d867 | 2022-04-21 09:24:56 +0000 | [diff] [blame] | 8595 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_CLI_C) |
XiaokangQian | 75d40ef | 2022-04-20 11:05:24 +0000 | [diff] [blame] | 8596 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
| 8597 | if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE && |
| 8598 | mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 ) |
| 8599 | { |
| 8600 | return( -1 ); |
| 8601 | } |
| 8602 | #endif |
| 8603 | |
| 8604 | /* Don't suggest PSK-based ciphersuite if no PSK is available. */ |
| 8605 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
| 8606 | if( mbedtls_ssl_ciphersuite_uses_psk( suite_info ) && |
| 8607 | mbedtls_ssl_conf_has_static_psk( ssl->conf ) == 0 ) |
| 8608 | { |
| 8609 | return( -1 ); |
| 8610 | } |
| 8611 | #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ |
| 8612 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 8613 | |
| 8614 | return( 0 ); |
| 8615 | } |
| 8616 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 8617 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 8618 | /* |
| 8619 | * Function for writing a signature algorithm extension. |
| 8620 | * |
| 8621 | * The `extension_data` field of signature algorithm contains a `SignatureSchemeList` |
| 8622 | * value (TLS 1.3 RFC8446): |
| 8623 | * enum { |
| 8624 | * .... |
| 8625 | * ecdsa_secp256r1_sha256( 0x0403 ), |
| 8626 | * ecdsa_secp384r1_sha384( 0x0503 ), |
| 8627 | * ecdsa_secp521r1_sha512( 0x0603 ), |
| 8628 | * .... |
| 8629 | * } SignatureScheme; |
| 8630 | * |
| 8631 | * struct { |
| 8632 | * SignatureScheme supported_signature_algorithms<2..2^16-2>; |
| 8633 | * } SignatureSchemeList; |
| 8634 | * |
| 8635 | * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm` |
| 8636 | * value (TLS 1.2 RFC5246): |
| 8637 | * enum { |
| 8638 | * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5), |
| 8639 | * sha512(6), (255) |
| 8640 | * } HashAlgorithm; |
| 8641 | * |
| 8642 | * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) } |
| 8643 | * SignatureAlgorithm; |
| 8644 | * |
| 8645 | * struct { |
| 8646 | * HashAlgorithm hash; |
| 8647 | * SignatureAlgorithm signature; |
| 8648 | * } SignatureAndHashAlgorithm; |
| 8649 | * |
| 8650 | * SignatureAndHashAlgorithm |
| 8651 | * supported_signature_algorithms<2..2^16-2>; |
| 8652 | * |
| 8653 | * The TLS 1.3 signature algorithm extension was defined to be a compatible |
| 8654 | * generalization of the TLS 1.2 signature algorithm extension. |
| 8655 | * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by |
| 8656 | * `SignatureScheme` field of TLS 1.3 |
| 8657 | * |
| 8658 | */ |
| 8659 | int mbedtls_ssl_write_sig_alg_ext( mbedtls_ssl_context *ssl, unsigned char *buf, |
| 8660 | const unsigned char *end, size_t *out_len ) |
| 8661 | { |
| 8662 | unsigned char *p = buf; |
| 8663 | unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */ |
| 8664 | size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */ |
| 8665 | |
| 8666 | *out_len = 0; |
| 8667 | |
| 8668 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding signature_algorithms extension" ) ); |
| 8669 | |
| 8670 | /* Check if we have space for header and length field: |
| 8671 | * - extension_type (2 bytes) |
| 8672 | * - extension_data_length (2 bytes) |
| 8673 | * - supported_signature_algorithms_length (2 bytes) |
| 8674 | */ |
| 8675 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 ); |
| 8676 | p += 6; |
| 8677 | |
| 8678 | /* |
| 8679 | * Write supported_signature_algorithms |
| 8680 | */ |
| 8681 | supported_sig_alg = p; |
| 8682 | const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs( ssl ); |
| 8683 | if( sig_alg == NULL ) |
| 8684 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); |
| 8685 | |
| 8686 | for( ; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++ ) |
| 8687 | { |
Jerry Yu | 53f5c15 | 2022-06-22 20:24:38 +0800 | [diff] [blame] | 8688 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "got signature scheme [%x] %s", |
| 8689 | *sig_alg, |
| 8690 | mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) ); |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 8691 | if( ! mbedtls_ssl_sig_alg_is_supported( ssl, *sig_alg ) ) |
| 8692 | continue; |
| 8693 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 ); |
| 8694 | MBEDTLS_PUT_UINT16_BE( *sig_alg, p, 0 ); |
| 8695 | p += 2; |
Jerry Yu | 80dd5db | 2022-06-22 19:30:32 +0800 | [diff] [blame] | 8696 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "sent signature scheme [%x] %s", |
Jerry Yu | f3b46b5 | 2022-06-19 16:52:27 +0800 | [diff] [blame] | 8697 | *sig_alg, |
| 8698 | mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) ); |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 8699 | } |
| 8700 | |
| 8701 | /* Length of supported_signature_algorithms */ |
| 8702 | supported_sig_alg_len = p - supported_sig_alg; |
| 8703 | if( supported_sig_alg_len == 0 ) |
| 8704 | { |
| 8705 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "No signature algorithms defined." ) ); |
| 8706 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 8707 | } |
| 8708 | |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 8709 | MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SIG_ALG, buf, 0 ); |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 8710 | MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len + 2, buf, 2 ); |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 8711 | MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len, buf, 4 ); |
| 8712 | |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 8713 | *out_len = p - buf; |
| 8714 | |
| 8715 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 8716 | ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SIG_ALG; |
| 8717 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 8718 | return( 0 ); |
| 8719 | } |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 8720 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 8721 | |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 8722 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 8723 | /* |
| 8724 | * mbedtls_ssl_parse_server_name_ext |
| 8725 | * |
| 8726 | * Structure of server_name extension: |
| 8727 | * |
| 8728 | * enum { |
| 8729 | * host_name(0), (255) |
| 8730 | * } NameType; |
| 8731 | * opaque HostName<1..2^16-1>; |
| 8732 | * |
| 8733 | * struct { |
| 8734 | * NameType name_type; |
| 8735 | * select (name_type) { |
| 8736 | * case host_name: HostName; |
| 8737 | * } name; |
| 8738 | * } ServerName; |
| 8739 | * struct { |
| 8740 | * ServerName server_name_list<1..2^16-1> |
| 8741 | * } ServerNameList; |
| 8742 | */ |
Ronald Cron | ce7d76e | 2022-07-08 18:56:49 +0200 | [diff] [blame] | 8743 | MBEDTLS_CHECK_RETURN_CRITICAL |
XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 8744 | int mbedtls_ssl_parse_server_name_ext( mbedtls_ssl_context *ssl, |
| 8745 | const unsigned char *buf, |
| 8746 | const unsigned char *end ) |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 8747 | { |
| 8748 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 8749 | const unsigned char *p = buf; |
XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 8750 | size_t server_name_list_len, hostname_len; |
| 8751 | const unsigned char *server_name_list_end; |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 8752 | |
XiaokangQian | f2a9420 | 2022-05-20 06:44:24 +0000 | [diff] [blame] | 8753 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "parse ServerName extension" ) ); |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 8754 | |
| 8755 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 ); |
XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 8756 | server_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 ); |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 8757 | p += 2; |
| 8758 | |
XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 8759 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, server_name_list_len ); |
| 8760 | server_name_list_end = p + server_name_list_len; |
XiaokangQian | 75fe8c7 | 2022-06-15 09:42:45 +0000 | [diff] [blame] | 8761 | while( p < server_name_list_end ) |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 8762 | { |
XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 8763 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end, 3 ); |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 8764 | hostname_len = MBEDTLS_GET_UINT16_BE( p, 1 ); |
XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 8765 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end, |
| 8766 | hostname_len + 3 ); |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 8767 | |
| 8768 | if( p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME ) |
| 8769 | { |
XiaokangQian | 75fe8c7 | 2022-06-15 09:42:45 +0000 | [diff] [blame] | 8770 | /* sni_name is intended to be used only during the parsing of the |
| 8771 | * ClientHello message (it is reset to NULL before the end of |
| 8772 | * the message parsing). Thus it is ok to just point to the |
| 8773 | * reception buffer and not make a copy of it. |
| 8774 | */ |
XiaokangQian | f2a9420 | 2022-05-20 06:44:24 +0000 | [diff] [blame] | 8775 | ssl->handshake->sni_name = p + 3; |
| 8776 | ssl->handshake->sni_name_len = hostname_len; |
| 8777 | if( ssl->conf->f_sni == NULL ) |
| 8778 | return( 0 ); |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 8779 | ret = ssl->conf->f_sni( ssl->conf->p_sni, |
XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 8780 | ssl, p + 3, hostname_len ); |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 8781 | if( ret != 0 ) |
| 8782 | { |
XiaokangQian | f2a9420 | 2022-05-20 06:44:24 +0000 | [diff] [blame] | 8783 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_sni_wrapper", ret ); |
XiaokangQian | 129aeb9 | 2022-06-02 09:29:18 +0000 | [diff] [blame] | 8784 | MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME, |
| 8785 | MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME ); |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 8786 | return( MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME ); |
| 8787 | } |
| 8788 | return( 0 ); |
| 8789 | } |
| 8790 | |
| 8791 | p += hostname_len + 3; |
| 8792 | } |
| 8793 | |
| 8794 | return( 0 ); |
| 8795 | } |
| 8796 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
| 8797 | |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8798 | #if defined(MBEDTLS_SSL_ALPN) |
Ronald Cron | ce7d76e | 2022-07-08 18:56:49 +0200 | [diff] [blame] | 8799 | MBEDTLS_CHECK_RETURN_CRITICAL |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8800 | int mbedtls_ssl_parse_alpn_ext( mbedtls_ssl_context *ssl, |
| 8801 | const unsigned char *buf, |
| 8802 | const unsigned char *end ) |
| 8803 | { |
| 8804 | const unsigned char *p = buf; |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 8805 | size_t protocol_name_list_len; |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 8806 | const unsigned char *protocol_name_list; |
| 8807 | const unsigned char *protocol_name_list_end; |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 8808 | size_t protocol_name_len; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8809 | |
| 8810 | /* If ALPN not configured, just ignore the extension */ |
| 8811 | if( ssl->conf->alpn_list == NULL ) |
| 8812 | return( 0 ); |
| 8813 | |
| 8814 | /* |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 8815 | * RFC7301, section 3.1 |
| 8816 | * opaque ProtocolName<1..2^8-1>; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8817 | * |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 8818 | * struct { |
| 8819 | * ProtocolName protocol_name_list<2..2^16-1> |
| 8820 | * } ProtocolNameList; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8821 | */ |
| 8822 | |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 8823 | /* |
XiaokangQian | 0b776e2 | 2022-06-24 09:04:59 +0000 | [diff] [blame] | 8824 | * protocol_name_list_len 2 bytes |
| 8825 | * protocol_name_len 1 bytes |
| 8826 | * protocol_name >=1 byte |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 8827 | */ |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8828 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 ); |
| 8829 | |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 8830 | protocol_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 ); |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8831 | p += 2; |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 8832 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, protocol_name_list_len ); |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 8833 | protocol_name_list = p; |
| 8834 | protocol_name_list_end = p + protocol_name_list_len; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8835 | |
| 8836 | /* Validate peer's list (lengths) */ |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 8837 | while( p < protocol_name_list_end ) |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8838 | { |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 8839 | protocol_name_len = *p++; |
| 8840 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, protocol_name_list_end, |
| 8841 | protocol_name_len ); |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 8842 | if( protocol_name_len == 0 ) |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 8843 | { |
| 8844 | MBEDTLS_SSL_PEND_FATAL_ALERT( |
| 8845 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER, |
| 8846 | MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER ); |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8847 | return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER ); |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 8848 | } |
| 8849 | |
| 8850 | p += protocol_name_len; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8851 | } |
| 8852 | |
| 8853 | /* Use our order of preference */ |
| 8854 | for( const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++ ) |
| 8855 | { |
| 8856 | size_t const alpn_len = strlen( *alpn ); |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 8857 | p = protocol_name_list; |
| 8858 | while( p < protocol_name_list_end ) |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8859 | { |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 8860 | protocol_name_len = *p++; |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 8861 | if( protocol_name_len == alpn_len && |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 8862 | memcmp( p, *alpn, alpn_len ) == 0 ) |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8863 | { |
| 8864 | ssl->alpn_chosen = *alpn; |
| 8865 | return( 0 ); |
| 8866 | } |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 8867 | |
| 8868 | p += protocol_name_len; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8869 | } |
| 8870 | } |
| 8871 | |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 8872 | /* If we get here, no match was found */ |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8873 | MBEDTLS_SSL_PEND_FATAL_ALERT( |
| 8874 | MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL, |
| 8875 | MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL ); |
| 8876 | return( MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL ); |
| 8877 | } |
| 8878 | |
| 8879 | int mbedtls_ssl_write_alpn_ext( mbedtls_ssl_context *ssl, |
| 8880 | unsigned char *buf, |
| 8881 | unsigned char *end, |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 8882 | size_t *out_len ) |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8883 | { |
| 8884 | unsigned char *p = buf; |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 8885 | size_t protocol_name_len; |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 8886 | *out_len = 0; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8887 | |
| 8888 | if( ssl->alpn_chosen == NULL ) |
| 8889 | { |
| 8890 | return( 0 ); |
| 8891 | } |
| 8892 | |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 8893 | protocol_name_len = strlen( ssl->alpn_chosen ); |
| 8894 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 7 + protocol_name_len ); |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8895 | |
| 8896 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "server side, adding alpn extension" ) ); |
| 8897 | /* |
| 8898 | * 0 . 1 ext identifier |
| 8899 | * 2 . 3 ext length |
| 8900 | * 4 . 5 protocol list length |
| 8901 | * 6 . 6 protocol name length |
| 8902 | * 7 . 7+n protocol name |
| 8903 | */ |
| 8904 | MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ALPN, p, 0 ); |
| 8905 | |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 8906 | *out_len = 7 + protocol_name_len; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8907 | |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 8908 | MBEDTLS_PUT_UINT16_BE( protocol_name_len + 3, p, 2 ); |
| 8909 | MBEDTLS_PUT_UINT16_BE( protocol_name_len + 1, p, 4 ); |
XiaokangQian | 0b776e2 | 2022-06-24 09:04:59 +0000 | [diff] [blame] | 8910 | /* Note: the length of the chosen protocol has been checked to be less |
| 8911 | * than 255 bytes in `mbedtls_ssl_conf_alpn_protocols`. |
| 8912 | */ |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 8913 | p[6] = MBEDTLS_BYTE_0( protocol_name_len ); |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8914 | |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 8915 | memcpy( p + 7, ssl->alpn_chosen, protocol_name_len ); |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8916 | return ( 0 ); |
| 8917 | } |
| 8918 | #endif /* MBEDTLS_SSL_ALPN */ |
| 8919 | |
Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 8920 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \ |
Xiaokang Qian | 0340929 | 2022-10-12 02:49:52 +0000 | [diff] [blame] | 8921 | defined(MBEDTLS_SSL_SESSION_TICKETS) && \ |
Xiaokang Qian | ed0620c | 2022-10-12 06:58:13 +0000 | [diff] [blame] | 8922 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \ |
Xiaokang Qian | ed3afcd | 2022-10-12 08:31:11 +0000 | [diff] [blame] | 8923 | defined(MBEDTLS_SSL_CLI_C) |
Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 8924 | int mbedtls_ssl_session_set_hostname( mbedtls_ssl_session *session, |
| 8925 | const char *hostname ) |
| 8926 | { |
| 8927 | /* Initialize to suppress unnecessary compiler warning */ |
| 8928 | size_t hostname_len = 0; |
| 8929 | |
| 8930 | /* Check if new hostname is valid before |
| 8931 | * making any change to current one */ |
| 8932 | if( hostname != NULL ) |
| 8933 | { |
| 8934 | hostname_len = strlen( hostname ); |
| 8935 | |
| 8936 | if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN ) |
| 8937 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8938 | } |
| 8939 | |
| 8940 | /* Now it's clear that we will overwrite the old hostname, |
| 8941 | * so we can free it safely */ |
| 8942 | if( session->hostname != NULL ) |
| 8943 | { |
| 8944 | mbedtls_platform_zeroize( session->hostname, |
| 8945 | strlen( session->hostname ) ); |
| 8946 | mbedtls_free( session->hostname ); |
| 8947 | } |
| 8948 | |
| 8949 | /* Passing NULL as hostname shall clear the old one */ |
| 8950 | if( hostname == NULL ) |
| 8951 | { |
| 8952 | session->hostname = NULL; |
| 8953 | } |
| 8954 | else |
| 8955 | { |
| 8956 | session->hostname = mbedtls_calloc( 1, hostname_len + 1 ); |
| 8957 | if( session->hostname == NULL ) |
| 8958 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 8959 | |
| 8960 | memcpy( session->hostname, hostname, hostname_len ); |
| 8961 | } |
| 8962 | |
| 8963 | return( 0 ); |
| 8964 | } |
Xiaokang Qian | 0340929 | 2022-10-12 02:49:52 +0000 | [diff] [blame] | 8965 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 && |
| 8966 | MBEDTLS_SSL_SESSION_TICKETS && |
Xiaokang Qian | ed0620c | 2022-10-12 06:58:13 +0000 | [diff] [blame] | 8967 | MBEDTLS_SSL_SERVER_NAME_INDICATION && |
Xiaokang Qian | ed3afcd | 2022-10-12 08:31:11 +0000 | [diff] [blame] | 8968 | MBEDTLS_SSL_CLI_C */ |
Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 8969 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8970 | #endif /* MBEDTLS_SSL_TLS_C */ |