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 | |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 524 | uint32_t mbedtls_ssl_get_extension_id( unsigned int extension_type ) |
| 525 | { |
| 526 | switch( extension_type ) |
| 527 | { |
| 528 | case MBEDTLS_TLS_EXT_SERVERNAME: |
| 529 | return( MBEDTLS_SSL_EXT_ID_SERVERNAME ); |
| 530 | |
| 531 | case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH: |
| 532 | return( MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH ); |
| 533 | |
| 534 | case MBEDTLS_TLS_EXT_STATUS_REQUEST: |
| 535 | return( MBEDTLS_SSL_EXT_ID_STATUS_REQUEST ); |
| 536 | |
| 537 | case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS: |
| 538 | return( MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS ); |
| 539 | |
| 540 | case MBEDTLS_TLS_EXT_SIG_ALG: |
| 541 | return( MBEDTLS_SSL_EXT_ID_SIG_ALG ); |
| 542 | |
| 543 | case MBEDTLS_TLS_EXT_USE_SRTP: |
| 544 | return( MBEDTLS_SSL_EXT_ID_USE_SRTP ); |
| 545 | |
| 546 | case MBEDTLS_TLS_EXT_HEARTBEAT: |
| 547 | return( MBEDTLS_SSL_EXT_ID_HEARTBEAT ); |
| 548 | |
| 549 | case MBEDTLS_TLS_EXT_ALPN: |
| 550 | return( MBEDTLS_SSL_EXT_ID_ALPN ); |
| 551 | |
| 552 | case MBEDTLS_TLS_EXT_SCT: |
| 553 | return( MBEDTLS_SSL_EXT_ID_SCT ); |
| 554 | |
| 555 | case MBEDTLS_TLS_EXT_CLI_CERT_TYPE: |
| 556 | return( MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE ); |
| 557 | |
| 558 | case MBEDTLS_TLS_EXT_SERV_CERT_TYPE: |
| 559 | return( MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE ); |
| 560 | |
| 561 | case MBEDTLS_TLS_EXT_PADDING: |
| 562 | return( MBEDTLS_SSL_EXT_ID_PADDING ); |
| 563 | |
| 564 | case MBEDTLS_TLS_EXT_PRE_SHARED_KEY: |
| 565 | return( MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY ); |
| 566 | |
| 567 | case MBEDTLS_TLS_EXT_EARLY_DATA: |
| 568 | return( MBEDTLS_SSL_EXT_ID_EARLY_DATA ); |
| 569 | |
| 570 | case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS: |
| 571 | return( MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS ); |
| 572 | |
| 573 | case MBEDTLS_TLS_EXT_COOKIE: |
| 574 | return( MBEDTLS_SSL_EXT_ID_COOKIE ); |
| 575 | |
| 576 | case MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES: |
| 577 | return( MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES ); |
| 578 | |
| 579 | case MBEDTLS_TLS_EXT_CERT_AUTH: |
| 580 | return( MBEDTLS_SSL_EXT_ID_CERT_AUTH ); |
| 581 | |
| 582 | case MBEDTLS_TLS_EXT_OID_FILTERS: |
| 583 | return( MBEDTLS_SSL_EXT_ID_OID_FILTERS ); |
| 584 | |
| 585 | case MBEDTLS_TLS_EXT_POST_HANDSHAKE_AUTH: |
| 586 | return( MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH ); |
| 587 | |
| 588 | case MBEDTLS_TLS_EXT_SIG_ALG_CERT: |
| 589 | return( MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT ); |
| 590 | |
| 591 | case MBEDTLS_TLS_EXT_KEY_SHARE: |
| 592 | return( MBEDTLS_SSL_EXT_ID_KEY_SHARE ); |
| 593 | |
| 594 | case MBEDTLS_TLS_EXT_TRUNCATED_HMAC: |
| 595 | return( MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC ); |
| 596 | |
| 597 | case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS: |
| 598 | return( MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS ); |
| 599 | |
| 600 | case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC: |
| 601 | return( MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC ); |
| 602 | |
| 603 | case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET: |
| 604 | return( MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET ); |
| 605 | |
| 606 | case MBEDTLS_TLS_EXT_SESSION_TICKET: |
| 607 | return( MBEDTLS_SSL_EXT_ID_SESSION_TICKET ); |
| 608 | |
| 609 | } |
| 610 | |
| 611 | return( MBEDTLS_SSL_EXT_ID_UNRECOGNIZED ); |
| 612 | } |
| 613 | |
| 614 | uint32_t mbedtls_ssl_get_extension_mask( unsigned int extension_type ) |
| 615 | { |
| 616 | return( 1 << mbedtls_ssl_get_extension_id( extension_type ) ); |
| 617 | } |
| 618 | |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 619 | void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl, |
| 620 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info ) |
| 621 | { |
| 622 | ((void) ciphersuite_info); |
| 623 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 624 | #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] | 625 | if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 ) |
| 626 | ssl->handshake->update_checksum = ssl_update_checksum_sha384; |
| 627 | else |
| 628 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 629 | #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] | 630 | if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 ) |
| 631 | ssl->handshake->update_checksum = ssl_update_checksum_sha256; |
| 632 | else |
| 633 | #endif |
| 634 | { |
| 635 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 636 | return; |
| 637 | } |
| 638 | } |
| 639 | |
XiaokangQian | adab9a6 | 2022-07-18 07:41:26 +0000 | [diff] [blame] | 640 | void mbedtls_ssl_add_hs_hdr_to_checksum( mbedtls_ssl_context *ssl, |
| 641 | unsigned hs_type, |
| 642 | size_t total_hs_len ) |
Ronald Cron | 8f6d39a | 2022-03-10 18:56:50 +0100 | [diff] [blame] | 643 | { |
| 644 | unsigned char hs_hdr[4]; |
| 645 | |
| 646 | /* Build HS header for checksum update. */ |
| 647 | hs_hdr[0] = MBEDTLS_BYTE_0( hs_type ); |
| 648 | hs_hdr[1] = MBEDTLS_BYTE_2( total_hs_len ); |
| 649 | hs_hdr[2] = MBEDTLS_BYTE_1( total_hs_len ); |
| 650 | hs_hdr[3] = MBEDTLS_BYTE_0( total_hs_len ); |
| 651 | |
| 652 | ssl->handshake->update_checksum( ssl, hs_hdr, sizeof( hs_hdr ) ); |
| 653 | } |
| 654 | |
| 655 | void mbedtls_ssl_add_hs_msg_to_checksum( mbedtls_ssl_context *ssl, |
| 656 | unsigned hs_type, |
| 657 | unsigned char const *msg, |
| 658 | size_t msg_len ) |
| 659 | { |
| 660 | mbedtls_ssl_add_hs_hdr_to_checksum( ssl, hs_type, msg_len ); |
| 661 | ssl->handshake->update_checksum( ssl, msg, msg_len ); |
| 662 | } |
| 663 | |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 664 | void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl ) |
| 665 | { |
| 666 | ((void) ssl); |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 667 | #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] | 668 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 669 | psa_hash_abort( &ssl->handshake->fin_sha256_psa ); |
| 670 | psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 ); |
| 671 | #else |
| 672 | mbedtls_sha256_starts( &ssl->handshake->fin_sha256, 0 ); |
| 673 | #endif |
| 674 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 675 | #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] | 676 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 677 | psa_hash_abort( &ssl->handshake->fin_sha384_psa ); |
| 678 | psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 ); |
| 679 | #else |
Andrzej Kurek | a242e83 | 2022-08-11 10:03:14 -0400 | [diff] [blame] | 680 | mbedtls_sha512_starts( &ssl->handshake->fin_sha384, 1 ); |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 681 | #endif |
| 682 | #endif |
| 683 | } |
| 684 | |
| 685 | static void ssl_update_checksum_start( mbedtls_ssl_context *ssl, |
| 686 | const unsigned char *buf, size_t len ) |
| 687 | { |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 688 | #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] | 689 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 690 | psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len ); |
| 691 | #else |
| 692 | mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len ); |
| 693 | #endif |
| 694 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 695 | #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] | 696 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 697 | psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len ); |
| 698 | #else |
Andrzej Kurek | a242e83 | 2022-08-11 10:03:14 -0400 | [diff] [blame] | 699 | mbedtls_sha512_update( &ssl->handshake->fin_sha384, buf, len ); |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 700 | #endif |
| 701 | #endif |
Andrzej Kurek | eabeb30 | 2022-10-17 07:52:51 -0400 | [diff] [blame] | 702 | #if !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \ |
| 703 | !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
| 704 | (void) ssl; |
| 705 | (void) buf; |
| 706 | (void) len; |
| 707 | #endif |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 708 | } |
| 709 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 710 | #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] | 711 | static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl, |
| 712 | const unsigned char *buf, size_t len ) |
| 713 | { |
| 714 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 715 | psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len ); |
| 716 | #else |
| 717 | mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len ); |
| 718 | #endif |
| 719 | } |
| 720 | #endif |
| 721 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 722 | #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] | 723 | static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl, |
| 724 | const unsigned char *buf, size_t len ) |
| 725 | { |
| 726 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 727 | psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len ); |
| 728 | #else |
Andrzej Kurek | a242e83 | 2022-08-11 10:03:14 -0400 | [diff] [blame] | 729 | mbedtls_sha512_update( &ssl->handshake->fin_sha384, buf, len ); |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 730 | #endif |
| 731 | } |
| 732 | #endif |
| 733 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 734 | static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake ) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 735 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 736 | memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 737 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 738 | #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] | 739 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 740 | handshake->fin_sha256_psa = psa_hash_operation_init(); |
| 741 | psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 ); |
| 742 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 743 | mbedtls_sha256_init( &handshake->fin_sha256 ); |
TRodziewicz | 26371e4 | 2021-06-08 16:45:41 +0200 | [diff] [blame] | 744 | mbedtls_sha256_starts( &handshake->fin_sha256, 0 ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 745 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 746 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 747 | #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] | 748 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 749 | handshake->fin_sha384_psa = psa_hash_operation_init(); |
| 750 | psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 751 | #else |
Andrzej Kurek | a242e83 | 2022-08-11 10:03:14 -0400 | [diff] [blame] | 752 | mbedtls_sha512_init( &handshake->fin_sha384 ); |
| 753 | mbedtls_sha512_starts( &handshake->fin_sha384, 1 ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 754 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 755 | #endif |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 756 | |
| 757 | handshake->update_checksum = ssl_update_checksum_start; |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 758 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 759 | #if defined(MBEDTLS_DHM_C) |
| 760 | mbedtls_dhm_init( &handshake->dhm_ctx ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 761 | #endif |
Neil Armstrong | f3f4641 | 2022-04-12 14:43:39 +0200 | [diff] [blame] | 762 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 763 | mbedtls_ecdh_init( &handshake->ecdh_ctx ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 764 | #endif |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 765 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 766 | mbedtls_ecjpake_init( &handshake->ecjpake_ctx ); |
Manuel Pégourié-Gonnard | 77c0646 | 2015-09-17 13:59:49 +0200 | [diff] [blame] | 767 | #if defined(MBEDTLS_SSL_CLI_C) |
| 768 | handshake->ecjpake_cache = NULL; |
| 769 | handshake->ecjpake_cache_len = 0; |
| 770 | #endif |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 771 | #endif |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 772 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 773 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Manuel Pégourié-Gonnard | 6b7301c | 2017-08-15 12:08:45 +0200 | [diff] [blame] | 774 | mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx ); |
Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 775 | #endif |
| 776 | |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 777 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 778 | handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET; |
| 779 | #endif |
Hanno Becker | 7517312 | 2019-02-06 16:18:31 +0000 | [diff] [blame] | 780 | |
| 781 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 782 | !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 783 | mbedtls_pk_init( &handshake->peer_pubkey ); |
| 784 | #endif |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 785 | } |
| 786 | |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 787 | void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform ) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 788 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 789 | memset( transform, 0, sizeof(mbedtls_ssl_transform) ); |
Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 790 | |
Przemyslaw Stekiel | 8f80fb9 | 2022-01-11 08:28:13 +0100 | [diff] [blame] | 791 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 792 | transform->psa_key_enc = MBEDTLS_SVC_KEY_ID_INIT; |
| 793 | transform->psa_key_dec = MBEDTLS_SVC_KEY_ID_INIT; |
Przemyslaw Stekiel | 6be9cf5 | 2022-01-19 16:00:22 +0100 | [diff] [blame] | 794 | #else |
| 795 | mbedtls_cipher_init( &transform->cipher_ctx_enc ); |
| 796 | mbedtls_cipher_init( &transform->cipher_ctx_dec ); |
Przemyslaw Stekiel | 8f80fb9 | 2022-01-11 08:28:13 +0100 | [diff] [blame] | 797 | #endif |
| 798 | |
Hanno Becker | fd86ca8 | 2020-11-30 08:54:23 +0000 | [diff] [blame] | 799 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
Neil Armstrong | 39b8e7d | 2022-02-23 09:24:45 +0100 | [diff] [blame] | 800 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 801 | transform->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT; |
| 802 | transform->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT; |
Neil Armstrong | cf8841a | 2022-02-24 11:17:45 +0100 | [diff] [blame] | 803 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 804 | mbedtls_md_init( &transform->md_ctx_enc ); |
| 805 | mbedtls_md_init( &transform->md_ctx_dec ); |
Hanno Becker | d56ed24 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 806 | #endif |
Neil Armstrong | cf8841a | 2022-02-24 11:17:45 +0100 | [diff] [blame] | 807 | #endif |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 808 | } |
| 809 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 810 | void mbedtls_ssl_session_init( mbedtls_ssl_session *session ) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 811 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 812 | memset( session, 0, sizeof(mbedtls_ssl_session) ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 813 | } |
| 814 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 815 | MBEDTLS_CHECK_RETURN_CRITICAL |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 816 | static int ssl_handshake_init( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 817 | { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 818 | /* Clear old handshake information if present */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 819 | if( ssl->transform_negotiate ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 820 | mbedtls_ssl_transform_free( ssl->transform_negotiate ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 821 | if( ssl->session_negotiate ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 822 | mbedtls_ssl_session_free( ssl->session_negotiate ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 823 | if( ssl->handshake ) |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 824 | mbedtls_ssl_handshake_free( ssl ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 825 | |
| 826 | /* |
| 827 | * Either the pointers are now NULL or cleared properly and can be freed. |
| 828 | * Now allocate missing structures. |
| 829 | */ |
| 830 | if( ssl->transform_negotiate == NULL ) |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 831 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 832 | ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 833 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 834 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 835 | if( ssl->session_negotiate == NULL ) |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 836 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 837 | ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 838 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 839 | |
Paul Bakker | 82788fb | 2014-10-20 13:59:19 +0200 | [diff] [blame] | 840 | if( ssl->handshake == NULL ) |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 841 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 842 | ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 843 | } |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 844 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 845 | /* If the buffers are too small - reallocate */ |
Andrzej Kurek | 8ea6872 | 2020-04-03 06:40:47 -0400 | [diff] [blame] | 846 | |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 847 | handle_buffer_resizing( ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN, |
| 848 | MBEDTLS_SSL_OUT_BUFFER_LEN ); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 849 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 850 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 851 | /* All pointers should exist and can be directly freed without issue */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 852 | if( ssl->handshake == NULL || |
| 853 | ssl->transform_negotiate == NULL || |
| 854 | ssl->session_negotiate == NULL ) |
| 855 | { |
Manuel Pégourié-Gonnard | b2a18a2 | 2015-05-27 16:29:56 +0200 | [diff] [blame] | 856 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 857 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 858 | mbedtls_free( ssl->handshake ); |
| 859 | mbedtls_free( ssl->transform_negotiate ); |
| 860 | mbedtls_free( ssl->session_negotiate ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 861 | |
| 862 | ssl->handshake = NULL; |
| 863 | ssl->transform_negotiate = NULL; |
| 864 | ssl->session_negotiate = NULL; |
| 865 | |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 866 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 867 | } |
| 868 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 869 | /* Initialize structures */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 870 | mbedtls_ssl_session_init( ssl->session_negotiate ); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 871 | mbedtls_ssl_transform_init( ssl->transform_negotiate ); |
Paul Bakker | 968afaa | 2014-07-09 11:09:24 +0200 | [diff] [blame] | 872 | ssl_handshake_params_init( ssl->handshake ); |
| 873 | |
Jerry Yu | d0766ec | 2022-09-22 10:46:57 +0800 | [diff] [blame] | 874 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \ |
| 875 | defined(MBEDTLS_SSL_SRV_C) && \ |
| 876 | defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 877 | ssl->handshake->new_session_tickets_count = |
| 878 | ssl->conf->new_session_tickets_count ; |
| 879 | #endif |
| 880 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 881 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 882 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 883 | { |
| 884 | ssl->handshake->alt_transform_out = ssl->transform_out; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 885 | |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 886 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 887 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING; |
| 888 | else |
| 889 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING; |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 890 | |
Hanno Becker | 0f57a65 | 2020-02-05 10:37:26 +0000 | [diff] [blame] | 891 | mbedtls_ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 892 | } |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 893 | #endif |
| 894 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 895 | /* |
| 896 | * curve_list is translated to IANA TLS group identifiers here because |
| 897 | * mbedtls_ssl_conf_curves returns void and so can't return |
| 898 | * any error codes. |
| 899 | */ |
| 900 | #if defined(MBEDTLS_ECP_C) |
| 901 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 902 | /* Heap allocate and translate curve_list from internal to IANA group ids */ |
| 903 | if ( ssl->conf->curve_list != NULL ) |
| 904 | { |
| 905 | size_t length; |
| 906 | const mbedtls_ecp_group_id *curve_list = ssl->conf->curve_list; |
| 907 | |
| 908 | for( length = 0; ( curve_list[length] != MBEDTLS_ECP_DP_NONE ) && |
| 909 | ( length < MBEDTLS_ECP_DP_MAX ); length++ ) {} |
| 910 | |
| 911 | /* Leave room for zero termination */ |
| 912 | uint16_t *group_list = mbedtls_calloc( length + 1, sizeof(uint16_t) ); |
| 913 | if ( group_list == NULL ) |
| 914 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 915 | |
| 916 | for( size_t i = 0; i < length; i++ ) |
| 917 | { |
| 918 | const mbedtls_ecp_curve_info *info = |
| 919 | mbedtls_ecp_curve_info_from_grp_id( curve_list[i] ); |
| 920 | if ( info == NULL ) |
| 921 | { |
| 922 | mbedtls_free( group_list ); |
| 923 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); |
| 924 | } |
| 925 | group_list[i] = info->tls_id; |
| 926 | } |
| 927 | |
| 928 | group_list[length] = 0; |
| 929 | |
| 930 | ssl->handshake->group_list = group_list; |
| 931 | ssl->handshake->group_list_heap_allocated = 1; |
| 932 | } |
| 933 | else |
| 934 | { |
| 935 | ssl->handshake->group_list = ssl->conf->group_list; |
| 936 | ssl->handshake->group_list_heap_allocated = 0; |
| 937 | } |
| 938 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
| 939 | #endif /* MBEDTLS_ECP_C */ |
| 940 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 941 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 942 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Jerry Yu | a69269a | 2022-01-17 21:06:01 +0800 | [diff] [blame] | 943 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Jerry Yu | 713013f | 2022-01-17 18:16:35 +0800 | [diff] [blame] | 944 | /* Heap allocate and translate sig_hashes from internal hash identifiers to |
| 945 | signature algorithms IANA identifiers. */ |
| 946 | if ( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) && |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 947 | ssl->conf->sig_hashes != NULL ) |
| 948 | { |
| 949 | const int *md; |
| 950 | const int *sig_hashes = ssl->conf->sig_hashes; |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 951 | size_t sig_algs_len = 0; |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 952 | uint16_t *p; |
| 953 | |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 954 | #if defined(static_assert) |
| 955 | static_assert( MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN |
| 956 | <= ( SIZE_MAX - ( 2 * sizeof(uint16_t) ) ), |
| 957 | "MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN too big" ); |
Jerry Yu | a68dca2 | 2022-01-20 16:28:27 +0800 | [diff] [blame] | 958 | #endif |
| 959 | |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 960 | for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ ) |
| 961 | { |
| 962 | if( mbedtls_ssl_hash_from_md_alg( *md ) == MBEDTLS_SSL_HASH_NONE ) |
| 963 | continue; |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 964 | #if defined(MBEDTLS_ECDSA_C) |
| 965 | sig_algs_len += sizeof( uint16_t ); |
| 966 | #endif |
Jerry Yu | a68dca2 | 2022-01-20 16:28:27 +0800 | [diff] [blame] | 967 | |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 968 | #if defined(MBEDTLS_RSA_C) |
| 969 | sig_algs_len += sizeof( uint16_t ); |
| 970 | #endif |
| 971 | if( sig_algs_len > MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN ) |
| 972 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 973 | } |
| 974 | |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 975 | if( sig_algs_len < MBEDTLS_SSL_MIN_SIG_ALG_LIST_LEN ) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 976 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); |
| 977 | |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 978 | ssl->handshake->sig_algs = mbedtls_calloc( 1, sig_algs_len + |
| 979 | sizeof( uint16_t )); |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 980 | if( ssl->handshake->sig_algs == NULL ) |
| 981 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 982 | |
| 983 | p = (uint16_t *)ssl->handshake->sig_algs; |
| 984 | for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ ) |
| 985 | { |
| 986 | unsigned char hash = mbedtls_ssl_hash_from_md_alg( *md ); |
| 987 | if( hash == MBEDTLS_SSL_HASH_NONE ) |
| 988 | continue; |
Jerry Yu | 6106fdc | 2022-01-12 16:36:14 +0800 | [diff] [blame] | 989 | #if defined(MBEDTLS_ECDSA_C) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 990 | *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_ECDSA); |
| 991 | p++; |
Jerry Yu | 6106fdc | 2022-01-12 16:36:14 +0800 | [diff] [blame] | 992 | #endif |
| 993 | #if defined(MBEDTLS_RSA_C) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 994 | *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_RSA); |
| 995 | p++; |
Jerry Yu | 6106fdc | 2022-01-12 16:36:14 +0800 | [diff] [blame] | 996 | #endif |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 997 | } |
Gabor Mezei | 15b95a6 | 2022-05-09 16:37:58 +0200 | [diff] [blame] | 998 | *p = MBEDTLS_TLS_SIG_NONE; |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 999 | ssl->handshake->sig_algs_heap_allocated = 1; |
| 1000 | } |
| 1001 | else |
Jerry Yu | a69269a | 2022-01-17 21:06:01 +0800 | [diff] [blame] | 1002 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1003 | { |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1004 | ssl->handshake->sig_algs_heap_allocated = 0; |
| 1005 | } |
Jerry Yu | cc53910 | 2022-06-27 16:27:35 +0800 | [diff] [blame] | 1006 | #endif /* !MBEDTLS_DEPRECATED_REMOVED */ |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 1007 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1008 | return( 0 ); |
| 1009 | } |
| 1010 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 1011 | #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] | 1012 | /* Dummy cookie callbacks for defaults */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 1013 | MBEDTLS_CHECK_RETURN_CRITICAL |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 1014 | static int ssl_cookie_write_dummy( void *ctx, |
| 1015 | unsigned char **p, unsigned char *end, |
| 1016 | const unsigned char *cli_id, size_t cli_id_len ) |
| 1017 | { |
| 1018 | ((void) ctx); |
| 1019 | ((void) p); |
| 1020 | ((void) end); |
| 1021 | ((void) cli_id); |
| 1022 | ((void) cli_id_len); |
| 1023 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1024 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 1025 | } |
| 1026 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 1027 | MBEDTLS_CHECK_RETURN_CRITICAL |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 1028 | static int ssl_cookie_check_dummy( void *ctx, |
| 1029 | const unsigned char *cookie, size_t cookie_len, |
| 1030 | const unsigned char *cli_id, size_t cli_id_len ) |
| 1031 | { |
| 1032 | ((void) ctx); |
| 1033 | ((void) cookie); |
| 1034 | ((void) cookie_len); |
| 1035 | ((void) cli_id); |
| 1036 | ((void) cli_id_len); |
| 1037 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1038 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 1039 | } |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 1040 | #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 1041 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1042 | /* |
| 1043 | * Initialize an SSL context |
| 1044 | */ |
Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 1045 | void mbedtls_ssl_init( mbedtls_ssl_context *ssl ) |
| 1046 | { |
| 1047 | memset( ssl, 0, sizeof( mbedtls_ssl_context ) ); |
| 1048 | } |
| 1049 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 1050 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1051 | static int ssl_conf_version_check( const mbedtls_ssl_context *ssl ) |
| 1052 | { |
Ronald Cron | 086ee0b | 2022-03-15 15:18:51 +0100 | [diff] [blame] | 1053 | const mbedtls_ssl_config *conf = ssl->conf; |
| 1054 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 1055 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Ronald Cron | 086ee0b | 2022-03-15 15:18:51 +0100 | [diff] [blame] | 1056 | if( mbedtls_ssl_conf_is_tls13_only( conf ) ) |
| 1057 | { |
XiaokangQian | ed582dd | 2022-04-13 08:21:05 +0000 | [diff] [blame] | 1058 | if( conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 1059 | { |
| 1060 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS 1.3 is not yet supported." ) ); |
| 1061 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 1062 | } |
| 1063 | |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1064 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls13 only." ) ); |
| 1065 | return( 0 ); |
| 1066 | } |
| 1067 | #endif |
| 1068 | |
| 1069 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Ronald Cron | 086ee0b | 2022-03-15 15:18:51 +0100 | [diff] [blame] | 1070 | if( mbedtls_ssl_conf_is_tls12_only( conf ) ) |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1071 | { |
| 1072 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls12 only." ) ); |
| 1073 | return( 0 ); |
| 1074 | } |
| 1075 | #endif |
| 1076 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 1077 | #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] | 1078 | if( mbedtls_ssl_conf_is_hybrid_tls12_tls13( conf ) ) |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1079 | { |
XiaokangQian | ed582dd | 2022-04-13 08:21:05 +0000 | [diff] [blame] | 1080 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 1081 | { |
| 1082 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS not yet supported in Hybrid TLS 1.3 + TLS 1.2" ) ); |
| 1083 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 1084 | } |
| 1085 | |
XiaokangQian | 8f9dfe4 | 2022-04-15 02:52:39 +0000 | [diff] [blame] | 1086 | if( conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 1087 | { |
| 1088 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS 1.3 server is not supported yet." ) ); |
| 1089 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 1090 | } |
| 1091 | |
| 1092 | |
Ronald Cron | e1d3f06 | 2022-02-10 14:50:54 +0100 | [diff] [blame] | 1093 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is TLS 1.3 or TLS 1.2." ) ); |
| 1094 | return( 0 ); |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1095 | } |
| 1096 | #endif |
| 1097 | |
| 1098 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "The SSL configuration is invalid." ) ); |
| 1099 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); |
| 1100 | } |
| 1101 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 1102 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1103 | static int ssl_conf_check(const mbedtls_ssl_context *ssl) |
| 1104 | { |
| 1105 | int ret; |
| 1106 | ret = ssl_conf_version_check( ssl ); |
| 1107 | if( ret != 0 ) |
| 1108 | return( ret ); |
| 1109 | |
| 1110 | /* Space for further checks */ |
| 1111 | |
| 1112 | return( 0 ); |
| 1113 | } |
| 1114 | |
Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 1115 | /* |
| 1116 | * Setup an SSL context |
| 1117 | */ |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 1118 | |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 1119 | int mbedtls_ssl_setup( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 1897af9 | 2015-05-10 23:27:38 +0200 | [diff] [blame] | 1120 | const mbedtls_ssl_config *conf ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1121 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1122 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1123 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; |
| 1124 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1125 | |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 1126 | ssl->conf = conf; |
Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 1127 | |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1128 | if( ( ret = ssl_conf_check( ssl ) ) != 0 ) |
| 1129 | return( ret ); |
| 1130 | |
Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 1131 | /* |
Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 1132 | * Prepare base structures |
Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 1133 | */ |
k-stachowiak | c9a5f02 | 2018-07-24 13:53:31 +0200 | [diff] [blame] | 1134 | |
| 1135 | /* Set to NULL in case of an error condition */ |
| 1136 | ssl->out_buf = NULL; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1137 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1138 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 1139 | ssl->in_buf_len = in_buf_len; |
| 1140 | #endif |
| 1141 | ssl->in_buf = mbedtls_calloc( 1, in_buf_len ); |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 1142 | if( ssl->in_buf == NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1143 | { |
Paul Elliott | d48d5c6 | 2021-01-07 14:47:05 +0000 | [diff] [blame] | 1144 | 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] | 1145 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1146 | goto error; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 1147 | } |
| 1148 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1149 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 1150 | ssl->out_buf_len = out_buf_len; |
| 1151 | #endif |
| 1152 | ssl->out_buf = mbedtls_calloc( 1, out_buf_len ); |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 1153 | if( ssl->out_buf == NULL ) |
| 1154 | { |
Paul Elliott | d48d5c6 | 2021-01-07 14:47:05 +0000 | [diff] [blame] | 1155 | 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] | 1156 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1157 | goto error; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1158 | } |
| 1159 | |
Hanno Becker | 3e6f8ab | 2020-02-05 10:40:57 +0000 | [diff] [blame] | 1160 | mbedtls_ssl_reset_in_out_pointers( ssl ); |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 1161 | |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 1162 | #if defined(MBEDTLS_SSL_DTLS_SRTP) |
Ron Eldor | 3adb992 | 2017-12-21 10:15:08 +0200 | [diff] [blame] | 1163 | memset( &ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info) ); |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 1164 | #endif |
| 1165 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1166 | if( ( ret = ssl_handshake_init( ssl ) ) != 0 ) |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1167 | goto error; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1168 | |
| 1169 | return( 0 ); |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1170 | |
| 1171 | error: |
| 1172 | mbedtls_free( ssl->in_buf ); |
| 1173 | mbedtls_free( ssl->out_buf ); |
| 1174 | |
| 1175 | ssl->conf = NULL; |
| 1176 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1177 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 1178 | ssl->in_buf_len = 0; |
| 1179 | ssl->out_buf_len = 0; |
| 1180 | #endif |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1181 | ssl->in_buf = NULL; |
| 1182 | ssl->out_buf = NULL; |
| 1183 | |
| 1184 | ssl->in_hdr = NULL; |
| 1185 | ssl->in_ctr = NULL; |
| 1186 | ssl->in_len = NULL; |
| 1187 | ssl->in_iv = NULL; |
| 1188 | ssl->in_msg = NULL; |
| 1189 | |
| 1190 | ssl->out_hdr = NULL; |
| 1191 | ssl->out_ctr = NULL; |
| 1192 | ssl->out_len = NULL; |
| 1193 | ssl->out_iv = NULL; |
| 1194 | ssl->out_msg = NULL; |
| 1195 | |
k-stachowiak | 9f7798e | 2018-07-31 16:52:32 +0200 | [diff] [blame] | 1196 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1197 | } |
| 1198 | |
| 1199 | /* |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 1200 | * Reset an initialized and used SSL context for re-use while retaining |
| 1201 | * all application-set variables, function pointers and data. |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 1202 | * |
| 1203 | * If partial is non-zero, keep data in the input buffer and client ID. |
| 1204 | * (Use when a DTLS client reconnects from the same port.) |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 1205 | */ |
XiaokangQian | 78b1fa7 | 2022-01-19 06:56:30 +0000 | [diff] [blame] | 1206 | void mbedtls_ssl_session_reset_msg_layer( mbedtls_ssl_context *ssl, |
| 1207 | int partial ) |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 1208 | { |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1209 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 1210 | size_t in_buf_len = ssl->in_buf_len; |
| 1211 | size_t out_buf_len = ssl->out_buf_len; |
| 1212 | #else |
| 1213 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; |
| 1214 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; |
| 1215 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1216 | |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1217 | #if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || !defined(MBEDTLS_SSL_SRV_C) |
| 1218 | partial = 0; |
Hanno Becker | 7e77213 | 2018-08-10 12:38:21 +0100 | [diff] [blame] | 1219 | #endif |
| 1220 | |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 1221 | /* Cancel any possibly running timer */ |
Hanno Becker | 0f57a65 | 2020-02-05 10:37:26 +0000 | [diff] [blame] | 1222 | mbedtls_ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 1223 | |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1224 | mbedtls_ssl_reset_in_out_pointers( ssl ); |
| 1225 | |
| 1226 | /* Reset incoming message parsing */ |
| 1227 | ssl->in_offt = NULL; |
| 1228 | ssl->nb_zero = 0; |
| 1229 | ssl->in_msgtype = 0; |
| 1230 | ssl->in_msglen = 0; |
| 1231 | ssl->in_hslen = 0; |
| 1232 | ssl->keep_current_message = 0; |
| 1233 | ssl->transform_in = NULL; |
| 1234 | |
| 1235 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 1236 | ssl->next_record_offset = 0; |
| 1237 | ssl->in_epoch = 0; |
| 1238 | #endif |
| 1239 | |
| 1240 | /* Keep current datagram if partial == 1 */ |
| 1241 | if( partial == 0 ) |
| 1242 | { |
| 1243 | ssl->in_left = 0; |
| 1244 | memset( ssl->in_buf, 0, in_buf_len ); |
| 1245 | } |
| 1246 | |
Ronald Cron | ad8c17b | 2022-06-10 17:18:09 +0200 | [diff] [blame] | 1247 | ssl->send_alert = 0; |
| 1248 | |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1249 | /* Reset outgoing message writing */ |
| 1250 | ssl->out_msgtype = 0; |
| 1251 | ssl->out_msglen = 0; |
| 1252 | ssl->out_left = 0; |
| 1253 | memset( ssl->out_buf, 0, out_buf_len ); |
| 1254 | memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) ); |
| 1255 | ssl->transform_out = NULL; |
| 1256 | |
| 1257 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 1258 | mbedtls_ssl_dtls_replay_reset( ssl ); |
| 1259 | #endif |
| 1260 | |
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1261 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1262 | if( ssl->transform ) |
| 1263 | { |
| 1264 | mbedtls_ssl_transform_free( ssl->transform ); |
| 1265 | mbedtls_free( ssl->transform ); |
| 1266 | ssl->transform = NULL; |
| 1267 | } |
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1268 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 1269 | |
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1270 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 1271 | mbedtls_ssl_transform_free( ssl->transform_application ); |
| 1272 | mbedtls_free( ssl->transform_application ); |
| 1273 | ssl->transform_application = NULL; |
| 1274 | |
| 1275 | if( ssl->handshake != NULL ) |
| 1276 | { |
| 1277 | mbedtls_ssl_transform_free( ssl->handshake->transform_earlydata ); |
| 1278 | mbedtls_free( ssl->handshake->transform_earlydata ); |
| 1279 | ssl->handshake->transform_earlydata = NULL; |
| 1280 | |
| 1281 | mbedtls_ssl_transform_free( ssl->handshake->transform_handshake ); |
| 1282 | mbedtls_free( ssl->handshake->transform_handshake ); |
| 1283 | ssl->handshake->transform_handshake = NULL; |
| 1284 | } |
| 1285 | |
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1286 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1287 | } |
| 1288 | |
| 1289 | int mbedtls_ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial ) |
| 1290 | { |
| 1291 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 1292 | |
| 1293 | ssl->state = MBEDTLS_SSL_HELLO_REQUEST; |
| 1294 | |
XiaokangQian | 78b1fa7 | 2022-01-19 06:56:30 +0000 | [diff] [blame] | 1295 | mbedtls_ssl_session_reset_msg_layer( ssl, partial ); |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1296 | |
| 1297 | /* Reset renegotiation state */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1298 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 1299 | ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE; |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 1300 | ssl->renego_records_seen = 0; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1301 | |
| 1302 | ssl->verify_data_len = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1303 | memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN ); |
| 1304 | 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] | 1305 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1306 | ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1307 | |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1308 | ssl->session_in = NULL; |
Hanno Becker | 7864090 | 2018-08-13 16:35:15 +0100 | [diff] [blame] | 1309 | ssl->session_out = NULL; |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 1310 | if( ssl->session ) |
| 1311 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1312 | mbedtls_ssl_session_free( ssl->session ); |
| 1313 | mbedtls_free( ssl->session ); |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 1314 | ssl->session = NULL; |
| 1315 | } |
| 1316 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1317 | #if defined(MBEDTLS_SSL_ALPN) |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 1318 | ssl->alpn_chosen = NULL; |
| 1319 | #endif |
| 1320 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 1321 | #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C) |
David Horstmann | 3b2276a | 2022-10-06 14:49:08 +0100 | [diff] [blame] | 1322 | int free_cli_id = 1; |
Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 1323 | #if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) |
David Horstmann | 3b2276a | 2022-10-06 14:49:08 +0100 | [diff] [blame] | 1324 | free_cli_id = ( partial == 0 ); |
Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 1325 | #endif |
David Horstmann | 3b2276a | 2022-10-06 14:49:08 +0100 | [diff] [blame] | 1326 | if( free_cli_id ) |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 1327 | { |
| 1328 | mbedtls_free( ssl->cli_id ); |
| 1329 | ssl->cli_id = NULL; |
| 1330 | ssl->cli_id_len = 0; |
| 1331 | } |
Manuel Pégourié-Gonnard | 43c0218 | 2014-07-22 17:32:01 +0200 | [diff] [blame] | 1332 | #endif |
| 1333 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1334 | if( ( ret = ssl_handshake_init( ssl ) ) != 0 ) |
| 1335 | return( ret ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1336 | |
| 1337 | return( 0 ); |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 1338 | } |
| 1339 | |
Manuel Pégourié-Gonnard | 779e429 | 2013-08-03 13:50:48 +0200 | [diff] [blame] | 1340 | /* |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 1341 | * Reset an initialized and used SSL context for re-use while retaining |
| 1342 | * all application-set variables, function pointers and data. |
| 1343 | */ |
| 1344 | int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl ) |
| 1345 | { |
Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 1346 | return( mbedtls_ssl_session_reset_int( ssl, 0 ) ); |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 1347 | } |
| 1348 | |
| 1349 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1350 | * SSL set accessors |
| 1351 | */ |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1352 | void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1353 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1354 | conf->endpoint = endpoint; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1355 | } |
| 1356 | |
Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 1357 | 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] | 1358 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1359 | conf->transport = transport; |
Manuel Pégourié-Gonnard | 0b1ff29 | 2014-02-06 13:04:16 +0100 | [diff] [blame] | 1360 | } |
| 1361 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1362 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1363 | 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] | 1364 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1365 | conf->anti_replay = mode; |
Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 1366 | } |
| 1367 | #endif |
| 1368 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1369 | 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] | 1370 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1371 | conf->badmac_limit = limit; |
Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 1372 | } |
Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 1373 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1374 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 1375 | |
Hanno Becker | 1841b0a | 2018-08-24 11:13:57 +0100 | [diff] [blame] | 1376 | void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl, |
| 1377 | unsigned allow_packing ) |
Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 1378 | { |
| 1379 | ssl->disable_datagram_packing = !allow_packing; |
| 1380 | } |
| 1381 | |
| 1382 | void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf, |
| 1383 | uint32_t min, uint32_t max ) |
Manuel Pégourié-Gonnard | 905dd24 | 2014-10-01 12:03:55 +0200 | [diff] [blame] | 1384 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1385 | conf->hs_timeout_min = min; |
| 1386 | conf->hs_timeout_max = max; |
Manuel Pégourié-Gonnard | 905dd24 | 2014-10-01 12:03:55 +0200 | [diff] [blame] | 1387 | } |
| 1388 | #endif |
| 1389 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1390 | void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1391 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1392 | conf->authmode = authmode; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1393 | } |
| 1394 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1395 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1396 | void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 1397 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 1398 | void *p_vrfy ) |
| 1399 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1400 | conf->f_vrfy = f_vrfy; |
| 1401 | conf->p_vrfy = p_vrfy; |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 1402 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1403 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 1404 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1405 | void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf, |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 1406 | int (*f_rng)(void *, unsigned char *, size_t), |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1407 | void *p_rng ) |
| 1408 | { |
Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 1409 | conf->f_rng = f_rng; |
| 1410 | conf->p_rng = p_rng; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1411 | } |
| 1412 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1413 | void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | fd47423 | 2015-06-23 16:34:24 +0200 | [diff] [blame] | 1414 | void (*f_dbg)(void *, int, const char *, int, const char *), |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1415 | void *p_dbg ) |
| 1416 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1417 | conf->f_dbg = f_dbg; |
| 1418 | conf->p_dbg = p_dbg; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1419 | } |
| 1420 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1421 | void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 1422 | void *p_bio, |
Simon Butcher | e846b51 | 2016-03-01 17:31:49 +0000 | [diff] [blame] | 1423 | mbedtls_ssl_send_t *f_send, |
| 1424 | mbedtls_ssl_recv_t *f_recv, |
| 1425 | mbedtls_ssl_recv_timeout_t *f_recv_timeout ) |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 1426 | { |
| 1427 | ssl->p_bio = p_bio; |
| 1428 | ssl->f_send = f_send; |
| 1429 | ssl->f_recv = f_recv; |
| 1430 | ssl->f_recv_timeout = f_recv_timeout; |
Manuel Pégourié-Gonnard | 97fd52c | 2015-05-06 15:38:52 +0100 | [diff] [blame] | 1431 | } |
| 1432 | |
Manuel Pégourié-Gonnard | 6e7aaca | 2018-08-20 10:37:23 +0200 | [diff] [blame] | 1433 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 1434 | void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu ) |
| 1435 | { |
| 1436 | ssl->mtu = mtu; |
| 1437 | } |
| 1438 | #endif |
| 1439 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1440 | 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] | 1441 | { |
| 1442 | conf->read_timeout = timeout; |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 1443 | } |
| 1444 | |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 1445 | void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl, |
| 1446 | void *p_timer, |
Simon Butcher | e846b51 | 2016-03-01 17:31:49 +0000 | [diff] [blame] | 1447 | mbedtls_ssl_set_timer_t *f_set_timer, |
| 1448 | mbedtls_ssl_get_timer_t *f_get_timer ) |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 1449 | { |
| 1450 | ssl->p_timer = p_timer; |
| 1451 | ssl->f_set_timer = f_set_timer; |
| 1452 | ssl->f_get_timer = f_get_timer; |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 1453 | |
| 1454 | /* Make sure we start with no timer running */ |
Hanno Becker | 0f57a65 | 2020-02-05 10:37:26 +0000 | [diff] [blame] | 1455 | mbedtls_ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 1456 | } |
| 1457 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1458 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1459 | void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf, |
Hanno Becker | a637ff6 | 2021-04-15 08:42:48 +0100 | [diff] [blame] | 1460 | void *p_cache, |
| 1461 | mbedtls_ssl_cache_get_t *f_get_cache, |
| 1462 | mbedtls_ssl_cache_set_t *f_set_cache ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1463 | { |
Manuel Pégourié-Gonnard | 5cb3308 | 2015-05-06 18:06:26 +0100 | [diff] [blame] | 1464 | conf->p_cache = p_cache; |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1465 | conf->f_get_cache = f_get_cache; |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1466 | conf->f_set_cache = f_set_cache; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1467 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1468 | #endif /* MBEDTLS_SSL_SRV_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1469 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1470 | #if defined(MBEDTLS_SSL_CLI_C) |
| 1471 | 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] | 1472 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1473 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1474 | |
| 1475 | if( ssl == NULL || |
| 1476 | session == NULL || |
| 1477 | ssl->session_negotiate == NULL || |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1478 | ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT ) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1479 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1480 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1481 | } |
| 1482 | |
Hanno Becker | e810bbc | 2021-05-14 16:01:05 +0100 | [diff] [blame] | 1483 | if( ssl->handshake->resume == 1 ) |
| 1484 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 1485 | |
Jerry Yu | 2109206 | 2022-10-10 21:21:31 +0800 | [diff] [blame] | 1486 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 1487 | if( session->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 ) |
Jerry Yu | 40afab6 | 2022-10-08 10:42:13 +0800 | [diff] [blame] | 1488 | { |
Jerry Yu | 2109206 | 2022-10-10 21:21:31 +0800 | [diff] [blame] | 1489 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 1490 | mbedtls_ssl_ciphersuite_from_id( session->ciphersuite ); |
| 1491 | |
| 1492 | if( mbedtls_ssl_validate_ciphersuite( |
| 1493 | ssl, ciphersuite_info, MBEDTLS_SSL_VERSION_TLS1_3, |
| 1494 | MBEDTLS_SSL_VERSION_TLS1_3 ) != 0 ) |
| 1495 | { |
| 1496 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "%d is not a valid TLS 1.3 ciphersuite.", |
| 1497 | session->ciphersuite ) ); |
| 1498 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1499 | } |
Jerry Yu | 40afab6 | 2022-10-08 10:42:13 +0800 | [diff] [blame] | 1500 | } |
Jerry Yu | 2109206 | 2022-10-10 21:21:31 +0800 | [diff] [blame] | 1501 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Jerry Yu | 40afab6 | 2022-10-08 10:42:13 +0800 | [diff] [blame] | 1502 | |
Hanno Becker | 52055ae | 2019-02-06 14:30:46 +0000 | [diff] [blame] | 1503 | if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate, |
| 1504 | session ) ) != 0 ) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1505 | return( ret ); |
| 1506 | |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 1507 | ssl->handshake->resume = 1; |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1508 | |
| 1509 | return( 0 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1510 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1511 | #endif /* MBEDTLS_SSL_CLI_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1512 | |
Mateusz Starzyk | 06b07fb | 2021-02-18 13:55:21 +0100 | [diff] [blame] | 1513 | void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf, |
| 1514 | const int *ciphersuites ) |
| 1515 | { |
Hanno Becker | d60b6c6 | 2021-04-29 12:04:11 +0100 | [diff] [blame] | 1516 | conf->ciphersuite_list = ciphersuites; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1517 | } |
| 1518 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 1519 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | cadebe5 | 2021-08-24 10:36:45 +0800 | [diff] [blame] | 1520 | void mbedtls_ssl_conf_tls13_key_exchange_modes( mbedtls_ssl_config *conf, |
Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 1521 | const int kex_modes ) |
| 1522 | { |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 1523 | 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] | 1524 | } |
Xiaokang Qian | 72de95d | 2022-10-25 02:54:33 +0000 | [diff] [blame] | 1525 | |
| 1526 | #if defined(MBEDTLS_SSL_EARLY_DATA) |
| 1527 | void mbedtls_ssl_tls13_conf_early_data( mbedtls_ssl_config *conf, |
Xiaokang Qian | 72dbfef | 2022-10-26 06:33:57 +0000 | [diff] [blame] | 1528 | int early_data_enabled ) |
Xiaokang Qian | 72de95d | 2022-10-25 02:54:33 +0000 | [diff] [blame] | 1529 | { |
| 1530 | conf->early_data_enabled = early_data_enabled; |
| 1531 | } |
| 1532 | #endif /* MBEDTLS_SSL_EARLY_DATA */ |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 1533 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 1534 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1535 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 6e3ee3a | 2015-06-17 10:58:20 +0200 | [diff] [blame] | 1536 | void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf, |
Nicholas Wilson | 2088e2e | 2015-09-08 16:53:18 +0100 | [diff] [blame] | 1537 | const mbedtls_x509_crt_profile *profile ) |
Manuel Pégourié-Gonnard | 6e3ee3a | 2015-06-17 10:58:20 +0200 | [diff] [blame] | 1538 | { |
| 1539 | conf->cert_profile = profile; |
| 1540 | } |
| 1541 | |
Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 1542 | static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert ) |
| 1543 | { |
| 1544 | mbedtls_ssl_key_cert *cur = key_cert, *next; |
| 1545 | |
| 1546 | while( cur != NULL ) |
| 1547 | { |
| 1548 | next = cur->next; |
| 1549 | mbedtls_free( cur ); |
| 1550 | cur = next; |
| 1551 | } |
| 1552 | } |
| 1553 | |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1554 | /* Append a new keycert entry to a (possibly empty) list */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 1555 | MBEDTLS_CHECK_RETURN_CRITICAL |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1556 | static int ssl_append_key_cert( mbedtls_ssl_key_cert **head, |
| 1557 | mbedtls_x509_crt *cert, |
| 1558 | mbedtls_pk_context *key ) |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1559 | { |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1560 | mbedtls_ssl_key_cert *new_cert; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1561 | |
Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 1562 | if( cert == NULL ) |
| 1563 | { |
| 1564 | /* Free list if cert is null */ |
| 1565 | ssl_key_cert_free( *head ); |
| 1566 | *head = NULL; |
| 1567 | return( 0 ); |
| 1568 | } |
| 1569 | |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1570 | new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) ); |
| 1571 | if( new_cert == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 1572 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1573 | |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1574 | new_cert->cert = cert; |
| 1575 | new_cert->key = key; |
| 1576 | new_cert->next = NULL; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1577 | |
Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 1578 | /* 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] | 1579 | if( *head == NULL ) |
Paul Bakker | 0333b97 | 2013-11-04 17:08:28 +0100 | [diff] [blame] | 1580 | { |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1581 | *head = new_cert; |
Paul Bakker | 0333b97 | 2013-11-04 17:08:28 +0100 | [diff] [blame] | 1582 | } |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1583 | else |
| 1584 | { |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1585 | mbedtls_ssl_key_cert *cur = *head; |
| 1586 | while( cur->next != NULL ) |
| 1587 | cur = cur->next; |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1588 | cur->next = new_cert; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1589 | } |
| 1590 | |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1591 | return( 0 ); |
| 1592 | } |
| 1593 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1594 | int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1595 | mbedtls_x509_crt *own_cert, |
| 1596 | mbedtls_pk_context *pk_key ) |
| 1597 | { |
Manuel Pégourié-Gonnard | 17a40cd | 2015-05-10 23:17:17 +0200 | [diff] [blame] | 1598 | 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] | 1599 | } |
| 1600 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1601 | void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 1602 | mbedtls_x509_crt *ca_chain, |
| 1603 | mbedtls_x509_crl *ca_crl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1604 | { |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 1605 | conf->ca_chain = ca_chain; |
| 1606 | conf->ca_crl = ca_crl; |
Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 1607 | |
| 1608 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 1609 | /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb() |
| 1610 | * cannot be used together. */ |
| 1611 | conf->f_ca_cb = NULL; |
| 1612 | conf->p_ca_cb = NULL; |
| 1613 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1614 | } |
Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 1615 | |
| 1616 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 1617 | void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf, |
Hanno Becker | afd0b0a | 2019-03-27 16:55:01 +0000 | [diff] [blame] | 1618 | mbedtls_x509_crt_ca_cb_t f_ca_cb, |
Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 1619 | void *p_ca_cb ) |
| 1620 | { |
| 1621 | conf->f_ca_cb = f_ca_cb; |
| 1622 | conf->p_ca_cb = p_ca_cb; |
| 1623 | |
| 1624 | /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb() |
| 1625 | * cannot be used together. */ |
| 1626 | conf->ca_chain = NULL; |
| 1627 | conf->ca_crl = NULL; |
| 1628 | } |
| 1629 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1630 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | eb2c658 | 2012-09-27 19:15:01 +0000 | [diff] [blame] | 1631 | |
Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 1632 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Glenn Strauss | 6989407 | 2022-01-24 12:58:00 -0500 | [diff] [blame] | 1633 | const unsigned char *mbedtls_ssl_get_hs_sni( mbedtls_ssl_context *ssl, |
| 1634 | size_t *name_len ) |
| 1635 | { |
| 1636 | *name_len = ssl->handshake->sni_name_len; |
| 1637 | return( ssl->handshake->sni_name ); |
| 1638 | } |
| 1639 | |
Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 1640 | int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl, |
| 1641 | mbedtls_x509_crt *own_cert, |
| 1642 | mbedtls_pk_context *pk_key ) |
| 1643 | { |
| 1644 | return( ssl_append_key_cert( &ssl->handshake->sni_key_cert, |
| 1645 | own_cert, pk_key ) ); |
| 1646 | } |
Manuel Pégourié-Gonnard | 22bfa4b | 2015-05-11 08:46:37 +0200 | [diff] [blame] | 1647 | |
| 1648 | void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl, |
| 1649 | mbedtls_x509_crt *ca_chain, |
| 1650 | mbedtls_x509_crl *ca_crl ) |
| 1651 | { |
| 1652 | ssl->handshake->sni_ca_chain = ca_chain; |
| 1653 | ssl->handshake->sni_ca_crl = ca_crl; |
| 1654 | } |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 1655 | |
Glenn Strauss | 999ef70 | 2022-03-11 01:37:23 -0500 | [diff] [blame] | 1656 | #if defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED) |
| 1657 | void mbedtls_ssl_set_hs_dn_hints( mbedtls_ssl_context *ssl, |
| 1658 | const mbedtls_x509_crt *crt) |
| 1659 | { |
| 1660 | ssl->handshake->dn_hints = crt; |
| 1661 | } |
| 1662 | #endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */ |
| 1663 | |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 1664 | void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl, |
| 1665 | int authmode ) |
| 1666 | { |
| 1667 | ssl->handshake->sni_authmode = authmode; |
| 1668 | } |
Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 1669 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
| 1670 | |
Hanno Becker | 8927c83 | 2019-04-03 12:52:50 +0100 | [diff] [blame] | 1671 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 1672 | void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl, |
| 1673 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), |
| 1674 | void *p_vrfy ) |
| 1675 | { |
| 1676 | ssl->f_vrfy = f_vrfy; |
| 1677 | ssl->p_vrfy = p_vrfy; |
| 1678 | } |
| 1679 | #endif |
| 1680 | |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 1681 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 1682 | /* |
| 1683 | * Set EC J-PAKE password for current handshake |
| 1684 | */ |
| 1685 | int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl, |
| 1686 | const unsigned char *pw, |
| 1687 | size_t pw_len ) |
| 1688 | { |
| 1689 | mbedtls_ecjpake_role role; |
| 1690 | |
Janos Follath | 8eb6413 | 2016-06-03 15:40:57 +0100 | [diff] [blame] | 1691 | if( ssl->handshake == NULL || ssl->conf == NULL ) |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 1692 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1693 | |
| 1694 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 1695 | role = MBEDTLS_ECJPAKE_SERVER; |
| 1696 | else |
| 1697 | role = MBEDTLS_ECJPAKE_CLIENT; |
| 1698 | |
| 1699 | return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx, |
| 1700 | role, |
| 1701 | MBEDTLS_MD_SHA256, |
| 1702 | MBEDTLS_ECP_DP_SECP256R1, |
| 1703 | pw, pw_len ) ); |
| 1704 | } |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 1705 | #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 1706 | |
Ronald Cron | 73fe8df | 2022-10-05 14:31:43 +0200 | [diff] [blame] | 1707 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED) |
Ronald Cron | d29e13e | 2022-10-19 10:33:48 +0200 | [diff] [blame] | 1708 | int mbedtls_ssl_conf_has_static_psk( mbedtls_ssl_config const *conf ) |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 1709 | { |
Ronald Cron | d29e13e | 2022-10-19 10:33:48 +0200 | [diff] [blame] | 1710 | if( conf->psk_identity == NULL || |
| 1711 | conf->psk_identity_len == 0 ) |
| 1712 | { |
| 1713 | return( 0 ); |
| 1714 | } |
| 1715 | |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 1716 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Ronald Cron | d29e13e | 2022-10-19 10:33:48 +0200 | [diff] [blame] | 1717 | if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) ) |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 1718 | return( 1 ); |
| 1719 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Ronald Cron | d29e13e | 2022-10-19 10:33:48 +0200 | [diff] [blame] | 1720 | |
| 1721 | if( conf->psk != NULL && conf->psk_len != 0 ) |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 1722 | return( 1 ); |
| 1723 | |
| 1724 | return( 0 ); |
| 1725 | } |
| 1726 | |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1727 | static void ssl_conf_remove_psk( mbedtls_ssl_config *conf ) |
| 1728 | { |
| 1729 | /* Remove reference to existing PSK, if any. */ |
| 1730 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1731 | if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) ) |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1732 | { |
| 1733 | /* The maintenance of the PSK key slot is the |
| 1734 | * user's responsibility. */ |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1735 | conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT; |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1736 | } |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1737 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 1738 | if( conf->psk != NULL ) |
| 1739 | { |
| 1740 | mbedtls_platform_zeroize( conf->psk, conf->psk_len ); |
| 1741 | |
| 1742 | mbedtls_free( conf->psk ); |
| 1743 | conf->psk = NULL; |
| 1744 | conf->psk_len = 0; |
| 1745 | } |
| 1746 | |
| 1747 | /* Remove reference to PSK identity, if any. */ |
| 1748 | if( conf->psk_identity != NULL ) |
| 1749 | { |
| 1750 | mbedtls_free( conf->psk_identity ); |
| 1751 | conf->psk_identity = NULL; |
| 1752 | conf->psk_identity_len = 0; |
| 1753 | } |
| 1754 | } |
| 1755 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1756 | /* This function assumes that PSK identity in the SSL config is unset. |
| 1757 | * It checks that the provided identity is well-formed and attempts |
| 1758 | * to make a copy of it in the SSL config. |
| 1759 | * On failure, the PSK identity in the config remains unset. */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 1760 | MBEDTLS_CHECK_RETURN_CRITICAL |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1761 | static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf, |
| 1762 | unsigned char const *psk_identity, |
| 1763 | size_t psk_identity_len ) |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 1764 | { |
Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 1765 | /* Identity len will be encoded on two bytes */ |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1766 | if( psk_identity == NULL || |
Ronald Cron | 2a87e9b | 2022-10-19 10:55:26 +0200 | [diff] [blame] | 1767 | psk_identity_len == 0 || |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1768 | ( psk_identity_len >> 16 ) != 0 || |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 1769 | psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN ) |
Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 1770 | { |
| 1771 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1772 | } |
| 1773 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1774 | conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ); |
| 1775 | if( conf->psk_identity == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 1776 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 1777 | |
Manuel Pégourié-Gonnard | 120fdbd | 2015-05-07 17:07:50 +0100 | [diff] [blame] | 1778 | conf->psk_identity_len = psk_identity_len; |
Manuel Pégourié-Gonnard | 120fdbd | 2015-05-07 17:07:50 +0100 | [diff] [blame] | 1779 | memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len ); |
Paul Bakker | 5ad403f | 2013-09-18 21:21:30 +0200 | [diff] [blame] | 1780 | |
| 1781 | return( 0 ); |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 1782 | } |
| 1783 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1784 | int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf, |
| 1785 | const unsigned char *psk, size_t psk_len, |
| 1786 | const unsigned char *psk_identity, size_t psk_identity_len ) |
| 1787 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1788 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 1789 | |
| 1790 | /* We currently only support one PSK, raw or opaque. */ |
Ronald Cron | d29e13e | 2022-10-19 10:33:48 +0200 | [diff] [blame] | 1791 | if( mbedtls_ssl_conf_has_static_psk( conf ) ) |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 1792 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1793 | |
| 1794 | /* Check and set raw PSK */ |
Piotr Nowicki | 9926eaf | 2019-11-20 14:54:36 +0100 | [diff] [blame] | 1795 | if( psk == NULL ) |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1796 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Piotr Nowicki | 9926eaf | 2019-11-20 14:54:36 +0100 | [diff] [blame] | 1797 | if( psk_len == 0 ) |
| 1798 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1799 | if( psk_len > MBEDTLS_PSK_MAX_LEN ) |
| 1800 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1801 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1802 | if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ) |
| 1803 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 1804 | conf->psk_len = psk_len; |
| 1805 | memcpy( conf->psk, psk, conf->psk_len ); |
| 1806 | |
| 1807 | /* Check and set PSK Identity */ |
| 1808 | ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len ); |
| 1809 | if( ret != 0 ) |
| 1810 | ssl_conf_remove_psk( conf ); |
| 1811 | |
| 1812 | return( ret ); |
| 1813 | } |
| 1814 | |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1815 | static void ssl_remove_psk( mbedtls_ssl_context *ssl ) |
| 1816 | { |
| 1817 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1818 | if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) ) |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1819 | { |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 1820 | /* The maintenance of the external PSK key slot is the |
| 1821 | * user's responsibility. */ |
| 1822 | if( ssl->handshake->psk_opaque_is_internal ) |
| 1823 | { |
| 1824 | psa_destroy_key( ssl->handshake->psk_opaque ); |
| 1825 | ssl->handshake->psk_opaque_is_internal = 0; |
| 1826 | } |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1827 | ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT; |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1828 | } |
Neil Armstrong | e952a30 | 2022-05-03 10:22:14 +0200 | [diff] [blame] | 1829 | #else |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1830 | if( ssl->handshake->psk != NULL ) |
| 1831 | { |
| 1832 | mbedtls_platform_zeroize( ssl->handshake->psk, |
| 1833 | ssl->handshake->psk_len ); |
| 1834 | mbedtls_free( ssl->handshake->psk ); |
| 1835 | ssl->handshake->psk_len = 0; |
| 1836 | } |
Neil Armstrong | e952a30 | 2022-05-03 10:22:14 +0200 | [diff] [blame] | 1837 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1838 | } |
| 1839 | |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1840 | int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl, |
| 1841 | const unsigned char *psk, size_t psk_len ) |
| 1842 | { |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 1843 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1844 | psa_key_attributes_t key_attributes = psa_key_attributes_init(); |
Jerry Yu | 5d01c05 | 2022-08-17 10:18:10 +0800 | [diff] [blame] | 1845 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Jerry Yu | 24b8c81 | 2022-08-20 19:06:56 +0800 | [diff] [blame] | 1846 | psa_algorithm_t alg = PSA_ALG_NONE; |
Jerry Yu | 5d01c05 | 2022-08-17 10:18:10 +0800 | [diff] [blame] | 1847 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 1848 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 1849 | |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1850 | if( psk == NULL || ssl->handshake == NULL ) |
| 1851 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1852 | |
| 1853 | if( psk_len > MBEDTLS_PSK_MAX_LEN ) |
| 1854 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1855 | |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1856 | ssl_remove_psk( ssl ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1857 | |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 1858 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Jerry Yu | ccc68a4 | 2022-07-26 16:39:20 +0800 | [diff] [blame] | 1859 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 1860 | if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 ) |
| 1861 | { |
Jerry Yu | 6cf6b47 | 2022-08-16 14:50:28 +0800 | [diff] [blame] | 1862 | if( ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384 ) |
Jerry Yu | ccc68a4 | 2022-07-26 16:39:20 +0800 | [diff] [blame] | 1863 | alg = PSA_ALG_TLS12_PSK_TO_MS( PSA_ALG_SHA_384 ); |
| 1864 | else |
| 1865 | alg = PSA_ALG_TLS12_PSK_TO_MS( PSA_ALG_SHA_256 ); |
| 1866 | psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE ); |
| 1867 | } |
| 1868 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 1869 | |
Jerry Yu | 568ec25 | 2022-07-22 21:27:34 +0800 | [diff] [blame] | 1870 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | ccc68a4 | 2022-07-26 16:39:20 +0800 | [diff] [blame] | 1871 | if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 ) |
| 1872 | { |
| 1873 | alg = PSA_ALG_HKDF_EXTRACT( PSA_ALG_ANY_HASH ); |
| 1874 | psa_set_key_usage_flags( &key_attributes, |
| 1875 | PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT ); |
| 1876 | } |
| 1877 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 1878 | |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 1879 | psa_set_key_algorithm( &key_attributes, alg ); |
| 1880 | psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE ); |
| 1881 | |
| 1882 | status = psa_import_key( &key_attributes, psk, psk_len, &key ); |
| 1883 | if( status != PSA_SUCCESS ) |
| 1884 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 1885 | |
| 1886 | /* Allow calling psa_destroy_key() on psk remove */ |
| 1887 | ssl->handshake->psk_opaque_is_internal = 1; |
| 1888 | return mbedtls_ssl_set_hs_psk_opaque( ssl, key ); |
| 1889 | #else |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 1890 | if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 1891 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1892 | |
| 1893 | ssl->handshake->psk_len = psk_len; |
| 1894 | memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len ); |
| 1895 | |
| 1896 | return( 0 ); |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 1897 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1898 | } |
| 1899 | |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1900 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1901 | int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf, |
Andrzej Kurek | 03e0146 | 2022-01-03 12:53:24 +0100 | [diff] [blame] | 1902 | mbedtls_svc_key_id_t psk, |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1903 | const unsigned char *psk_identity, |
| 1904 | size_t psk_identity_len ) |
| 1905 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1906 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 1907 | |
| 1908 | /* We currently only support one PSK, raw or opaque. */ |
Ronald Cron | d29e13e | 2022-10-19 10:33:48 +0200 | [diff] [blame] | 1909 | if( mbedtls_ssl_conf_has_static_psk( conf ) ) |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 1910 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1911 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1912 | /* Check and set opaque PSK */ |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1913 | if( mbedtls_svc_key_id_is_null( psk ) ) |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1914 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1915 | conf->psk_opaque = psk; |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1916 | |
| 1917 | /* Check and set PSK Identity */ |
| 1918 | ret = ssl_conf_set_psk_identity( conf, psk_identity, |
| 1919 | psk_identity_len ); |
| 1920 | if( ret != 0 ) |
| 1921 | ssl_conf_remove_psk( conf ); |
| 1922 | |
| 1923 | return( ret ); |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1924 | } |
| 1925 | |
Przemyslaw Stekiel | 6928a51 | 2022-02-03 13:50:35 +0100 | [diff] [blame] | 1926 | int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl, |
| 1927 | mbedtls_svc_key_id_t psk ) |
| 1928 | { |
| 1929 | if( ( mbedtls_svc_key_id_is_null( psk ) ) || |
| 1930 | ( ssl->handshake == NULL ) ) |
| 1931 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1932 | |
| 1933 | ssl_remove_psk( ssl ); |
| 1934 | ssl->handshake->psk_opaque = psk; |
| 1935 | return( 0 ); |
| 1936 | } |
| 1937 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 1938 | |
Jerry Yu | 8897c07 | 2022-08-12 13:56:53 +0800 | [diff] [blame] | 1939 | #if defined(MBEDTLS_SSL_SRV_C) |
Przemyslaw Stekiel | 6928a51 | 2022-02-03 13:50:35 +0100 | [diff] [blame] | 1940 | void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf, |
| 1941 | int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *, |
| 1942 | size_t), |
| 1943 | void *p_psk ) |
| 1944 | { |
| 1945 | conf->f_psk = f_psk; |
| 1946 | conf->p_psk = p_psk; |
| 1947 | } |
Jerry Yu | 8897c07 | 2022-08-12 13:56:53 +0800 | [diff] [blame] | 1948 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 1949 | |
Ronald Cron | 73fe8df | 2022-10-05 14:31:43 +0200 | [diff] [blame] | 1950 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */ |
Przemyslaw Stekiel | 6928a51 | 2022-02-03 13:50:35 +0100 | [diff] [blame] | 1951 | |
| 1952 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 301711e | 2022-04-26 16:57:05 +0200 | [diff] [blame] | 1953 | static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode( |
| 1954 | psa_algorithm_t alg ) |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 1955 | { |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 1956 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1957 | if( alg == PSA_ALG_CBC_NO_PADDING ) |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 1958 | return( MBEDTLS_SSL_MODE_CBC ); |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 1959 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1960 | if( PSA_ALG_IS_AEAD( alg ) ) |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 1961 | return( MBEDTLS_SSL_MODE_AEAD ); |
Gilles Peskine | 301711e | 2022-04-26 16:57:05 +0200 | [diff] [blame] | 1962 | return( MBEDTLS_SSL_MODE_STREAM ); |
| 1963 | } |
| 1964 | |
| 1965 | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
| 1966 | |
| 1967 | static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode( |
| 1968 | mbedtls_cipher_mode_t mode ) |
| 1969 | { |
| 1970 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
| 1971 | if( mode == MBEDTLS_MODE_CBC ) |
| 1972 | return( MBEDTLS_SSL_MODE_CBC ); |
| 1973 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
| 1974 | |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 1975 | #if defined(MBEDTLS_GCM_C) || \ |
| 1976 | defined(MBEDTLS_CCM_C) || \ |
| 1977 | defined(MBEDTLS_CHACHAPOLY_C) |
| 1978 | if( mode == MBEDTLS_MODE_GCM || |
| 1979 | mode == MBEDTLS_MODE_CCM || |
| 1980 | mode == MBEDTLS_MODE_CHACHAPOLY ) |
| 1981 | return( MBEDTLS_SSL_MODE_AEAD ); |
| 1982 | #endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */ |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 1983 | |
| 1984 | return( MBEDTLS_SSL_MODE_STREAM ); |
| 1985 | } |
Gilles Peskine | 301711e | 2022-04-26 16:57:05 +0200 | [diff] [blame] | 1986 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 1987 | |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 1988 | static mbedtls_ssl_mode_t mbedtls_ssl_get_actual_mode( |
| 1989 | mbedtls_ssl_mode_t base_mode, |
| 1990 | int encrypt_then_mac ) |
| 1991 | { |
| 1992 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
| 1993 | if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED && |
| 1994 | base_mode == MBEDTLS_SSL_MODE_CBC ) |
| 1995 | { |
| 1996 | return( MBEDTLS_SSL_MODE_CBC_ETM ); |
| 1997 | } |
| 1998 | #else |
| 1999 | (void) encrypt_then_mac; |
| 2000 | #endif |
| 2001 | return( base_mode ); |
| 2002 | } |
| 2003 | |
Neil Armstrong | ab555e0 | 2022-04-04 11:07:59 +0200 | [diff] [blame] | 2004 | mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_transform( |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2005 | const mbedtls_ssl_transform *transform ) |
| 2006 | { |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2007 | mbedtls_ssl_mode_t base_mode = mbedtls_ssl_get_base_mode( |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2008 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2009 | transform->psa_alg |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2010 | #else |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2011 | mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) |
| 2012 | #endif |
| 2013 | ); |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2014 | |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2015 | int encrypt_then_mac = 0; |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 2016 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2017 | encrypt_then_mac = transform->encrypt_then_mac; |
| 2018 | #endif |
| 2019 | return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) ); |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2020 | } |
| 2021 | |
Neil Armstrong | ab555e0 | 2022-04-04 11:07:59 +0200 | [diff] [blame] | 2022 | mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_ciphersuite( |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 2023 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2024 | int encrypt_then_mac, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 2025 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2026 | const mbedtls_ssl_ciphersuite_t *suite ) |
| 2027 | { |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2028 | mbedtls_ssl_mode_t base_mode = MBEDTLS_SSL_MODE_STREAM; |
| 2029 | |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2030 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 2031 | psa_status_t status; |
| 2032 | psa_algorithm_t alg; |
| 2033 | psa_key_type_t type; |
| 2034 | size_t size; |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2035 | status = mbedtls_ssl_cipher_to_psa( suite->cipher, 0, &alg, &type, &size ); |
| 2036 | if( status == PSA_SUCCESS ) |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2037 | base_mode = mbedtls_ssl_get_base_mode( alg ); |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2038 | #else |
| 2039 | const mbedtls_cipher_info_t *cipher = |
| 2040 | mbedtls_cipher_info_from_type( suite->cipher ); |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2041 | if( cipher != NULL ) |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2042 | { |
| 2043 | base_mode = |
| 2044 | mbedtls_ssl_get_base_mode( |
| 2045 | mbedtls_cipher_info_get_mode( cipher ) ); |
| 2046 | } |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2047 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 2048 | |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2049 | #if !defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
| 2050 | int encrypt_then_mac = 0; |
| 2051 | #endif |
| 2052 | return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) ); |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2053 | } |
| 2054 | |
Neil Armstrong | 8395d7a | 2022-05-18 11:44:56 +0200 | [diff] [blame] | 2055 | #if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 2056 | |
| 2057 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2058 | /* Serialization of TLS 1.3 sessions: |
| 2059 | * |
| 2060 | * struct { |
Xiaokang Qian | 126bf8e | 2022-10-13 02:22:40 +0000 | [diff] [blame] | 2061 | * opaque hostname<0..2^16-1>; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2062 | * uint64 ticket_received; |
| 2063 | * uint32 ticket_lifetime; |
Jerry Yu | 3419107 | 2022-08-18 10:32:09 +0800 | [diff] [blame] | 2064 | * opaque ticket<1..2^16-1>; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2065 | * } ClientOnlyData; |
| 2066 | * |
| 2067 | * struct { |
| 2068 | * uint8 endpoint; |
| 2069 | * uint8 ciphersuite[2]; |
| 2070 | * uint32 ticket_age_add; |
| 2071 | * uint8 ticket_flags; |
| 2072 | * opaque resumption_key<0..255>; |
| 2073 | * select ( endpoint ) { |
| 2074 | * case client: ClientOnlyData; |
| 2075 | * case server: uint64 start_time; |
| 2076 | * }; |
| 2077 | * } serialized_session_tls13; |
| 2078 | * |
| 2079 | */ |
| 2080 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 2081 | MBEDTLS_CHECK_RETURN_CRITICAL |
| 2082 | static int ssl_tls13_session_save( const mbedtls_ssl_session *session, |
| 2083 | unsigned char *buf, |
| 2084 | size_t buf_len, |
| 2085 | size_t *olen ) |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2086 | { |
| 2087 | unsigned char *p = buf; |
Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 2088 | #if defined(MBEDTLS_SSL_CLI_C) && \ |
| 2089 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 2090 | size_t hostname_len = ( session->hostname == NULL ) ? |
Xiaokang Qian | ed0620c | 2022-10-12 06:58:13 +0000 | [diff] [blame] | 2091 | 0 : strlen( session->hostname ) + 1; |
Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 2092 | #endif |
Jerry Yu | bc7c1a4 | 2022-07-21 22:57:37 +0800 | [diff] [blame] | 2093 | size_t needed = 1 /* endpoint */ |
| 2094 | + 2 /* ciphersuite */ |
| 2095 | + 4 /* ticket_age_add */ |
Jerry Yu | 3419107 | 2022-08-18 10:32:09 +0800 | [diff] [blame] | 2096 | + 1 /* ticket_flags */ |
| 2097 | + 1; /* resumption_key length */ |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 2098 | *olen = 0; |
Jerry Yu | 3419107 | 2022-08-18 10:32:09 +0800 | [diff] [blame] | 2099 | |
| 2100 | if( session->resumption_key_len > MBEDTLS_SSL_TLS1_3_TICKET_RESUMPTION_KEY_LEN ) |
| 2101 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2102 | needed += session->resumption_key_len; /* resumption_key */ |
| 2103 | |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2104 | #if defined(MBEDTLS_HAVE_TIME) |
| 2105 | needed += 8; /* start_time or ticket_received */ |
| 2106 | #endif |
| 2107 | |
| 2108 | #if defined(MBEDTLS_SSL_CLI_C) |
| 2109 | if( session->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 2110 | { |
Xiaokang Qian | 126bf8e | 2022-10-13 02:22:40 +0000 | [diff] [blame] | 2111 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Xiaokang Qian | bc663a0 | 2022-10-09 11:14:39 +0000 | [diff] [blame] | 2112 | needed += 2 /* hostname_len */ |
Xiaokang Qian | 2f9efd3 | 2022-10-10 11:24:08 +0000 | [diff] [blame] | 2113 | + hostname_len; /* hostname */ |
Xiaokang Qian | 281fd1b | 2022-09-20 11:35:41 +0000 | [diff] [blame] | 2114 | #endif |
| 2115 | |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2116 | needed += 4 /* ticket_lifetime */ |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 2117 | + 2; /* ticket_len */ |
Jerry Yu | 3419107 | 2022-08-18 10:32:09 +0800 | [diff] [blame] | 2118 | |
| 2119 | /* Check size_t overflow */ |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 2120 | if( session->ticket_len > SIZE_MAX - needed ) |
| 2121 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jerry Yu | 3419107 | 2022-08-18 10:32:09 +0800 | [diff] [blame] | 2122 | |
Jerry Yu | e28d974 | 2022-08-18 15:44:03 +0800 | [diff] [blame] | 2123 | needed += session->ticket_len; /* ticket */ |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2124 | } |
| 2125 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 2126 | |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 2127 | *olen = needed; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2128 | if( needed > buf_len ) |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 2129 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2130 | |
| 2131 | p[0] = session->endpoint; |
| 2132 | MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 1 ); |
| 2133 | MBEDTLS_PUT_UINT32_BE( session->ticket_age_add, p, 3 ); |
| 2134 | p[7] = session->ticket_flags; |
| 2135 | |
| 2136 | /* save resumption_key */ |
Jerry Yu | bc7c1a4 | 2022-07-21 22:57:37 +0800 | [diff] [blame] | 2137 | p[8] = session->resumption_key_len; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2138 | p += 9; |
Jerry Yu | bc7c1a4 | 2022-07-21 22:57:37 +0800 | [diff] [blame] | 2139 | memcpy( p, session->resumption_key, session->resumption_key_len ); |
| 2140 | p += session->resumption_key_len; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2141 | |
| 2142 | #if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C) |
| 2143 | if( session->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 2144 | { |
| 2145 | MBEDTLS_PUT_UINT64_BE( (uint64_t) session->start, p, 0 ); |
| 2146 | p += 8; |
| 2147 | } |
| 2148 | #endif /* MBEDTLS_HAVE_TIME */ |
| 2149 | |
| 2150 | #if defined(MBEDTLS_SSL_CLI_C) |
| 2151 | if( session->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 2152 | { |
Xiaokang Qian | ed0620c | 2022-10-12 06:58:13 +0000 | [diff] [blame] | 2153 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 2154 | MBEDTLS_PUT_UINT16_BE( hostname_len, p, 0 ); |
| 2155 | p += 2; |
Xiaokang Qian | 126bf8e | 2022-10-13 02:22:40 +0000 | [diff] [blame] | 2156 | if( hostname_len > 0 ) |
Xiaokang Qian | ed0620c | 2022-10-12 06:58:13 +0000 | [diff] [blame] | 2157 | { |
| 2158 | /* save host name */ |
| 2159 | memcpy( p, session->hostname, hostname_len ); |
| 2160 | p += hostname_len; |
| 2161 | } |
| 2162 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
| 2163 | |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2164 | #if defined(MBEDTLS_HAVE_TIME) |
| 2165 | MBEDTLS_PUT_UINT64_BE( (uint64_t) session->ticket_received, p, 0 ); |
| 2166 | p += 8; |
| 2167 | #endif |
| 2168 | MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 ); |
| 2169 | p += 4; |
| 2170 | |
| 2171 | MBEDTLS_PUT_UINT16_BE( session->ticket_len, p, 0 ); |
| 2172 | p += 2; |
Jerry Yu | 3419107 | 2022-08-18 10:32:09 +0800 | [diff] [blame] | 2173 | |
| 2174 | if( session->ticket != NULL && session->ticket_len > 0 ) |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2175 | { |
| 2176 | memcpy( p, session->ticket, session->ticket_len ); |
| 2177 | p += session->ticket_len; |
| 2178 | } |
| 2179 | } |
| 2180 | #endif /* MBEDTLS_SSL_CLI_C */ |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 2181 | return( 0 ); |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2182 | } |
| 2183 | |
| 2184 | MBEDTLS_CHECK_RETURN_CRITICAL |
| 2185 | static int ssl_tls13_session_load( mbedtls_ssl_session *session, |
| 2186 | const unsigned char *buf, |
| 2187 | size_t len ) |
| 2188 | { |
| 2189 | const unsigned char *p = buf; |
| 2190 | const unsigned char *end = buf + len; |
| 2191 | |
| 2192 | if( end - p < 9 ) |
| 2193 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2194 | session->endpoint = p[0]; |
| 2195 | session->ciphersuite = MBEDTLS_GET_UINT16_BE( p, 1 ); |
| 2196 | session->ticket_age_add = MBEDTLS_GET_UINT32_BE( p, 3 ); |
| 2197 | session->ticket_flags = p[7]; |
| 2198 | |
| 2199 | /* load resumption_key */ |
Jerry Yu | bc7c1a4 | 2022-07-21 22:57:37 +0800 | [diff] [blame] | 2200 | session->resumption_key_len = p[8]; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2201 | p += 9; |
| 2202 | |
Jerry Yu | bc7c1a4 | 2022-07-21 22:57:37 +0800 | [diff] [blame] | 2203 | if( end - p < session->resumption_key_len ) |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2204 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2205 | |
Jerry Yu | bc7c1a4 | 2022-07-21 22:57:37 +0800 | [diff] [blame] | 2206 | if( sizeof( session->resumption_key ) < session->resumption_key_len ) |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2207 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jerry Yu | bc7c1a4 | 2022-07-21 22:57:37 +0800 | [diff] [blame] | 2208 | memcpy( session->resumption_key, p, session->resumption_key_len ); |
| 2209 | p += session->resumption_key_len; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2210 | |
| 2211 | #if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C) |
| 2212 | if( session->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 2213 | { |
| 2214 | if( end - p < 8 ) |
| 2215 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2216 | session->start = MBEDTLS_GET_UINT64_BE( p, 0 ); |
| 2217 | p += 8; |
| 2218 | } |
| 2219 | #endif /* MBEDTLS_HAVE_TIME */ |
| 2220 | |
| 2221 | #if defined(MBEDTLS_SSL_CLI_C) |
| 2222 | if( session->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 2223 | { |
Xiaokang Qian | ed0620c | 2022-10-12 06:58:13 +0000 | [diff] [blame] | 2224 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \ |
| 2225 | defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 2226 | size_t hostname_len; |
| 2227 | /* load host name */ |
| 2228 | if( end - p < 2 ) |
| 2229 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2230 | hostname_len = MBEDTLS_GET_UINT16_BE( p, 0 ); |
| 2231 | p += 2; |
| 2232 | |
| 2233 | if( end - p < ( long int )hostname_len ) |
| 2234 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2235 | if( hostname_len > 0 ) |
| 2236 | { |
| 2237 | session->hostname = mbedtls_calloc( 1, hostname_len ); |
| 2238 | if( session->hostname == NULL ) |
| 2239 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 2240 | memcpy( session->hostname, p, hostname_len ); |
| 2241 | p += hostname_len; |
| 2242 | } |
| 2243 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION && |
| 2244 | MBEDTLS_SSL_SESSION_TICKETS */ |
| 2245 | |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2246 | #if defined(MBEDTLS_HAVE_TIME) |
| 2247 | if( end - p < 8 ) |
| 2248 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2249 | session->ticket_received = MBEDTLS_GET_UINT64_BE( p, 0 ); |
| 2250 | p += 8; |
| 2251 | #endif |
| 2252 | if( end - p < 4 ) |
| 2253 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2254 | session->ticket_lifetime = MBEDTLS_GET_UINT32_BE( p, 0 ); |
| 2255 | p += 4; |
| 2256 | |
| 2257 | if( end - p < 2 ) |
| 2258 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2259 | session->ticket_len = MBEDTLS_GET_UINT16_BE( p, 0 ); |
| 2260 | p += 2; |
| 2261 | |
| 2262 | if( end - p < ( long int )session->ticket_len ) |
| 2263 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2264 | if( session->ticket_len > 0 ) |
| 2265 | { |
| 2266 | session->ticket = mbedtls_calloc( 1, session->ticket_len ); |
| 2267 | if( session->ticket == NULL ) |
| 2268 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 2269 | memcpy( session->ticket, p, session->ticket_len ); |
| 2270 | p += session->ticket_len; |
| 2271 | } |
| 2272 | } |
| 2273 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 2274 | |
| 2275 | return( 0 ); |
| 2276 | |
| 2277 | } |
| 2278 | #else /* MBEDTLS_SSL_SESSION_TICKETS */ |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 2279 | MBEDTLS_CHECK_RETURN_CRITICAL |
| 2280 | static int ssl_tls13_session_save( const mbedtls_ssl_session *session, |
| 2281 | unsigned char *buf, |
| 2282 | size_t buf_len, |
| 2283 | size_t *olen ) |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 2284 | { |
| 2285 | ((void) session); |
| 2286 | ((void) buf); |
| 2287 | ((void) buf_len); |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 2288 | *olen = 0; |
| 2289 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 2290 | } |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2291 | |
| 2292 | static int ssl_tls13_session_load( const mbedtls_ssl_session *session, |
| 2293 | unsigned char *buf, |
| 2294 | size_t buf_len ) |
| 2295 | { |
| 2296 | ((void) session); |
| 2297 | ((void) buf); |
| 2298 | ((void) buf_len); |
| 2299 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 2300 | } |
| 2301 | #endif /* !MBEDTLS_SSL_SESSION_TICKETS */ |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 2302 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 2303 | |
Przemyslaw Stekiel | f57b456 | 2022-01-25 00:04:18 +0100 | [diff] [blame] | 2304 | 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] | 2305 | size_t taglen, |
| 2306 | psa_algorithm_t *alg, |
| 2307 | psa_key_type_t *key_type, |
| 2308 | size_t *key_size ) |
| 2309 | { |
| 2310 | switch ( mbedtls_cipher_type ) |
| 2311 | { |
| 2312 | case MBEDTLS_CIPHER_AES_128_CBC: |
| 2313 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 2314 | *key_type = PSA_KEY_TYPE_AES; |
| 2315 | *key_size = 128; |
| 2316 | break; |
| 2317 | case MBEDTLS_CIPHER_AES_128_CCM: |
| 2318 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 2319 | *key_type = PSA_KEY_TYPE_AES; |
| 2320 | *key_size = 128; |
| 2321 | break; |
| 2322 | case MBEDTLS_CIPHER_AES_128_GCM: |
| 2323 | *alg = PSA_ALG_GCM; |
| 2324 | *key_type = PSA_KEY_TYPE_AES; |
| 2325 | *key_size = 128; |
| 2326 | break; |
| 2327 | case MBEDTLS_CIPHER_AES_192_CCM: |
| 2328 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 2329 | *key_type = PSA_KEY_TYPE_AES; |
| 2330 | *key_size = 192; |
| 2331 | break; |
| 2332 | case MBEDTLS_CIPHER_AES_192_GCM: |
| 2333 | *alg = PSA_ALG_GCM; |
| 2334 | *key_type = PSA_KEY_TYPE_AES; |
| 2335 | *key_size = 192; |
| 2336 | break; |
| 2337 | case MBEDTLS_CIPHER_AES_256_CBC: |
| 2338 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 2339 | *key_type = PSA_KEY_TYPE_AES; |
| 2340 | *key_size = 256; |
| 2341 | break; |
| 2342 | case MBEDTLS_CIPHER_AES_256_CCM: |
| 2343 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 2344 | *key_type = PSA_KEY_TYPE_AES; |
| 2345 | *key_size = 256; |
| 2346 | break; |
| 2347 | case MBEDTLS_CIPHER_AES_256_GCM: |
| 2348 | *alg = PSA_ALG_GCM; |
| 2349 | *key_type = PSA_KEY_TYPE_AES; |
| 2350 | *key_size = 256; |
| 2351 | break; |
| 2352 | case MBEDTLS_CIPHER_ARIA_128_CBC: |
| 2353 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 2354 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2355 | *key_size = 128; |
| 2356 | break; |
| 2357 | case MBEDTLS_CIPHER_ARIA_128_CCM: |
| 2358 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 2359 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2360 | *key_size = 128; |
| 2361 | break; |
| 2362 | case MBEDTLS_CIPHER_ARIA_128_GCM: |
| 2363 | *alg = PSA_ALG_GCM; |
| 2364 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2365 | *key_size = 128; |
| 2366 | break; |
| 2367 | case MBEDTLS_CIPHER_ARIA_192_CCM: |
| 2368 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 2369 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2370 | *key_size = 192; |
| 2371 | break; |
| 2372 | case MBEDTLS_CIPHER_ARIA_192_GCM: |
| 2373 | *alg = PSA_ALG_GCM; |
| 2374 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2375 | *key_size = 192; |
| 2376 | break; |
| 2377 | case MBEDTLS_CIPHER_ARIA_256_CBC: |
| 2378 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 2379 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2380 | *key_size = 256; |
| 2381 | break; |
| 2382 | case MBEDTLS_CIPHER_ARIA_256_CCM: |
| 2383 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 2384 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2385 | *key_size = 256; |
| 2386 | break; |
| 2387 | case MBEDTLS_CIPHER_ARIA_256_GCM: |
| 2388 | *alg = PSA_ALG_GCM; |
| 2389 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2390 | *key_size = 256; |
| 2391 | break; |
| 2392 | case MBEDTLS_CIPHER_CAMELLIA_128_CBC: |
| 2393 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 2394 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2395 | *key_size = 128; |
| 2396 | break; |
| 2397 | case MBEDTLS_CIPHER_CAMELLIA_128_CCM: |
| 2398 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 2399 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2400 | *key_size = 128; |
| 2401 | break; |
| 2402 | case MBEDTLS_CIPHER_CAMELLIA_128_GCM: |
| 2403 | *alg = PSA_ALG_GCM; |
| 2404 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2405 | *key_size = 128; |
| 2406 | break; |
| 2407 | case MBEDTLS_CIPHER_CAMELLIA_192_CCM: |
| 2408 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 2409 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2410 | *key_size = 192; |
| 2411 | break; |
| 2412 | case MBEDTLS_CIPHER_CAMELLIA_192_GCM: |
| 2413 | *alg = PSA_ALG_GCM; |
| 2414 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2415 | *key_size = 192; |
| 2416 | break; |
| 2417 | case MBEDTLS_CIPHER_CAMELLIA_256_CBC: |
| 2418 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 2419 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2420 | *key_size = 256; |
| 2421 | break; |
| 2422 | case MBEDTLS_CIPHER_CAMELLIA_256_CCM: |
| 2423 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 2424 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2425 | *key_size = 256; |
| 2426 | break; |
| 2427 | case MBEDTLS_CIPHER_CAMELLIA_256_GCM: |
| 2428 | *alg = PSA_ALG_GCM; |
| 2429 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2430 | *key_size = 256; |
| 2431 | break; |
| 2432 | case MBEDTLS_CIPHER_CHACHA20_POLY1305: |
| 2433 | *alg = PSA_ALG_CHACHA20_POLY1305; |
| 2434 | *key_type = PSA_KEY_TYPE_CHACHA20; |
| 2435 | *key_size = 256; |
| 2436 | break; |
| 2437 | case MBEDTLS_CIPHER_NULL: |
| 2438 | *alg = MBEDTLS_SSL_NULL_CIPHER; |
| 2439 | *key_type = 0; |
| 2440 | *key_size = 0; |
| 2441 | break; |
| 2442 | default: |
| 2443 | return PSA_ERROR_NOT_SUPPORTED; |
| 2444 | } |
| 2445 | |
| 2446 | return PSA_SUCCESS; |
| 2447 | } |
Neil Armstrong | 8395d7a | 2022-05-18 11:44:56 +0200 | [diff] [blame] | 2448 | #endif /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2449 | |
Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 2450 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 2451 | int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf, |
| 2452 | const unsigned char *dhm_P, size_t P_len, |
| 2453 | const unsigned char *dhm_G, size_t G_len ) |
| 2454 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2455 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 2456 | |
Glenn Strauss | cee1129 | 2021-12-20 01:43:17 -0500 | [diff] [blame] | 2457 | mbedtls_mpi_free( &conf->dhm_P ); |
| 2458 | mbedtls_mpi_free( &conf->dhm_G ); |
| 2459 | |
Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 2460 | if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 || |
| 2461 | ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 ) |
| 2462 | { |
| 2463 | mbedtls_mpi_free( &conf->dhm_P ); |
| 2464 | mbedtls_mpi_free( &conf->dhm_G ); |
| 2465 | return( ret ); |
| 2466 | } |
| 2467 | |
| 2468 | return( 0 ); |
| 2469 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2470 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2471 | 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] | 2472 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2473 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 2474 | |
Glenn Strauss | cee1129 | 2021-12-20 01:43:17 -0500 | [diff] [blame] | 2475 | mbedtls_mpi_free( &conf->dhm_P ); |
| 2476 | mbedtls_mpi_free( &conf->dhm_G ); |
| 2477 | |
Gilles Peskine | e570248 | 2021-06-11 21:59:08 +0200 | [diff] [blame] | 2478 | if( ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_P, |
| 2479 | &conf->dhm_P ) ) != 0 || |
| 2480 | ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_G, |
| 2481 | &conf->dhm_G ) ) != 0 ) |
Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 2482 | { |
| 2483 | mbedtls_mpi_free( &conf->dhm_P ); |
| 2484 | mbedtls_mpi_free( &conf->dhm_G ); |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 2485 | return( ret ); |
Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 2486 | } |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 2487 | |
| 2488 | return( 0 ); |
| 2489 | } |
Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 2490 | #endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */ |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 2491 | |
Manuel Pégourié-Gonnard | bd990d6 | 2015-06-11 14:49:42 +0200 | [diff] [blame] | 2492 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C) |
| 2493 | /* |
| 2494 | * Set the minimum length for Diffie-Hellman parameters |
| 2495 | */ |
| 2496 | void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf, |
| 2497 | unsigned int bitlen ) |
| 2498 | { |
| 2499 | conf->dhm_min_bitlen = bitlen; |
| 2500 | } |
| 2501 | #endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */ |
| 2502 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 2503 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Jerry Yu | 7ddc38c | 2022-01-19 11:08:05 +0800 | [diff] [blame] | 2504 | #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] | 2505 | /* |
| 2506 | * Set allowed/preferred hashes for handshake signatures |
| 2507 | */ |
| 2508 | void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf, |
| 2509 | const int *hashes ) |
| 2510 | { |
| 2511 | conf->sig_hashes = hashes; |
| 2512 | } |
Jerry Yu | 7ddc38c | 2022-01-19 11:08:05 +0800 | [diff] [blame] | 2513 | #endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */ |
Hanno Becker | 1cd6e00 | 2021-08-10 13:27:10 +0100 | [diff] [blame] | 2514 | |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 2515 | /* Configure allowed signature algorithms for handshake */ |
Hanno Becker | 1cd6e00 | 2021-08-10 13:27:10 +0100 | [diff] [blame] | 2516 | void mbedtls_ssl_conf_sig_algs( mbedtls_ssl_config *conf, |
| 2517 | const uint16_t* sig_algs ) |
| 2518 | { |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 2519 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 2520 | conf->sig_hashes = NULL; |
| 2521 | #endif /* !MBEDTLS_DEPRECATED_REMOVED */ |
| 2522 | conf->sig_algs = sig_algs; |
Hanno Becker | 1cd6e00 | 2021-08-10 13:27:10 +0100 | [diff] [blame] | 2523 | } |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 2524 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Manuel Pégourié-Gonnard | 36a8b57 | 2015-06-17 12:43:26 +0200 | [diff] [blame] | 2525 | |
Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 2526 | #if defined(MBEDTLS_ECP_C) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 2527 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 2528 | /* |
| 2529 | * Set the allowed elliptic curves |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 2530 | * |
| 2531 | * mbedtls_ssl_setup() takes the provided list |
| 2532 | * and translates it to a list of IANA TLS group identifiers, |
| 2533 | * stored in ssl->handshake->group_list. |
| 2534 | * |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 2535 | */ |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2536 | void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2537 | const mbedtls_ecp_group_id *curve_list ) |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 2538 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2539 | conf->curve_list = curve_list; |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 2540 | conf->group_list = NULL; |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 2541 | } |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 2542 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2543 | #endif /* MBEDTLS_ECP_C */ |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 2544 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 2545 | /* |
| 2546 | * Set the allowed groups |
| 2547 | */ |
| 2548 | void mbedtls_ssl_conf_groups( mbedtls_ssl_config *conf, |
| 2549 | const uint16_t *group_list ) |
| 2550 | { |
| 2551 | #if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 2552 | conf->curve_list = NULL; |
| 2553 | #endif |
| 2554 | conf->group_list = group_list; |
| 2555 | } |
| 2556 | |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 2557 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2558 | int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2559 | { |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2560 | /* Initialize to suppress unnecessary compiler warning */ |
| 2561 | size_t hostname_len = 0; |
| 2562 | |
| 2563 | /* Check if new hostname is valid before |
| 2564 | * making any change to current one */ |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2565 | if( hostname != NULL ) |
| 2566 | { |
| 2567 | hostname_len = strlen( hostname ); |
| 2568 | |
| 2569 | if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN ) |
| 2570 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2571 | } |
| 2572 | |
| 2573 | /* Now it's clear that we will overwrite the old hostname, |
| 2574 | * so we can free it safely */ |
| 2575 | |
| 2576 | if( ssl->hostname != NULL ) |
| 2577 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 2578 | mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) ); |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2579 | mbedtls_free( ssl->hostname ); |
| 2580 | } |
| 2581 | |
| 2582 | /* Passing NULL as hostname shall clear the old one */ |
Manuel Pégourié-Gonnard | ba26c24 | 2015-05-06 10:47:06 +0100 | [diff] [blame] | 2583 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2584 | if( hostname == NULL ) |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2585 | { |
| 2586 | ssl->hostname = NULL; |
| 2587 | } |
| 2588 | else |
| 2589 | { |
| 2590 | ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 ); |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2591 | if( ssl->hostname == NULL ) |
| 2592 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Paul Bakker | 75c1a6f | 2013-08-19 14:25:29 +0200 | [diff] [blame] | 2593 | |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2594 | memcpy( ssl->hostname, hostname, hostname_len ); |
Paul Bakker | 75c1a6f | 2013-08-19 14:25:29 +0200 | [diff] [blame] | 2595 | |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2596 | ssl->hostname[hostname_len] = '\0'; |
| 2597 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2598 | |
| 2599 | return( 0 ); |
| 2600 | } |
Hanno Becker | 1a9a51c | 2017-04-07 13:02:16 +0100 | [diff] [blame] | 2601 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2602 | |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 2603 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2604 | void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2605 | int (*f_sni)(void *, mbedtls_ssl_context *, |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 2606 | const unsigned char *, size_t), |
| 2607 | void *p_sni ) |
| 2608 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2609 | conf->f_sni = f_sni; |
| 2610 | conf->p_sni = p_sni; |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 2611 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2612 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 2613 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2614 | #if defined(MBEDTLS_SSL_ALPN) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2615 | 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] | 2616 | { |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 2617 | size_t cur_len, tot_len; |
| 2618 | const char **p; |
| 2619 | |
| 2620 | /* |
Brian J Murray | 1903fb3 | 2016-11-06 04:45:15 -0800 | [diff] [blame] | 2621 | * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings |
| 2622 | * MUST NOT be truncated." |
| 2623 | * We check lengths now rather than later. |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 2624 | */ |
| 2625 | tot_len = 0; |
| 2626 | for( p = protos; *p != NULL; p++ ) |
| 2627 | { |
| 2628 | cur_len = strlen( *p ); |
| 2629 | tot_len += cur_len; |
| 2630 | |
Ronald Cron | 8216dd3 | 2020-04-23 16:41:44 +0200 | [diff] [blame] | 2631 | if( ( cur_len == 0 ) || |
| 2632 | ( cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN ) || |
| 2633 | ( tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN ) ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2634 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 2635 | } |
| 2636 | |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2637 | conf->alpn_list = protos; |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 2638 | |
| 2639 | return( 0 ); |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 2640 | } |
| 2641 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2642 | 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] | 2643 | { |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 2644 | return( ssl->alpn_chosen ); |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 2645 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2646 | #endif /* MBEDTLS_SSL_ALPN */ |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 2647 | |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2648 | #if defined(MBEDTLS_SSL_DTLS_SRTP) |
Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 2649 | void mbedtls_ssl_conf_srtp_mki_value_supported( mbedtls_ssl_config *conf, |
| 2650 | int support_mki_value ) |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 2651 | { |
| 2652 | conf->dtls_srtp_mki_support = support_mki_value; |
| 2653 | } |
| 2654 | |
Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 2655 | int mbedtls_ssl_dtls_srtp_set_mki_value( mbedtls_ssl_context *ssl, |
| 2656 | unsigned char *mki_value, |
Johan Pascal | f6417ec | 2020-09-22 15:15:19 +0200 | [diff] [blame] | 2657 | uint16_t mki_len ) |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 2658 | { |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 2659 | if( mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH ) |
Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 2660 | { |
Johan Pascal | d576fdb | 2020-09-22 10:39:53 +0200 | [diff] [blame] | 2661 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 2662 | } |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 2663 | |
| 2664 | 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] | 2665 | { |
Johan Pascal | d576fdb | 2020-09-22 10:39:53 +0200 | [diff] [blame] | 2666 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 2667 | } |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 2668 | |
| 2669 | memcpy( ssl->dtls_srtp_info.mki_value, mki_value, mki_len ); |
| 2670 | ssl->dtls_srtp_info.mki_len = mki_len; |
Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 2671 | return( 0 ); |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 2672 | } |
| 2673 | |
Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 2674 | int mbedtls_ssl_conf_dtls_srtp_protection_profiles( mbedtls_ssl_config *conf, |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 2675 | const mbedtls_ssl_srtp_profile *profiles ) |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2676 | { |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 2677 | const mbedtls_ssl_srtp_profile *p; |
| 2678 | size_t list_size = 0; |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2679 | |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 2680 | /* check the profiles list: all entry must be valid, |
| 2681 | * 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] | 2682 | for( p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET && |
| 2683 | list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH; |
| 2684 | p++ ) |
Johan Pascal | d576fdb | 2020-09-22 10:39:53 +0200 | [diff] [blame] | 2685 | { |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 2686 | if( mbedtls_ssl_check_srtp_profile_value( *p ) != MBEDTLS_TLS_SRTP_UNSET ) |
Johan Pascal | d576fdb | 2020-09-22 10:39:53 +0200 | [diff] [blame] | 2687 | { |
Johan Pascal | 76fdf1d | 2020-10-22 23:31:00 +0200 | [diff] [blame] | 2688 | list_size++; |
| 2689 | } |
| 2690 | else |
| 2691 | { |
| 2692 | /* unsupported value, stop parsing and set the size to an error value */ |
| 2693 | list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1; |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2694 | } |
| 2695 | } |
| 2696 | |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 2697 | if( list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH ) |
Johan Pascal | d387aa0 | 2020-09-23 18:47:56 +0200 | [diff] [blame] | 2698 | { |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 2699 | conf->dtls_srtp_profile_list = NULL; |
| 2700 | conf->dtls_srtp_profile_list_len = 0; |
| 2701 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2702 | } |
| 2703 | |
Johan Pascal | 9bc97ca | 2020-09-21 23:44:45 +0200 | [diff] [blame] | 2704 | conf->dtls_srtp_profile_list = profiles; |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 2705 | conf->dtls_srtp_profile_list_len = list_size; |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2706 | |
| 2707 | return( 0 ); |
| 2708 | } |
| 2709 | |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 2710 | void mbedtls_ssl_get_dtls_srtp_negotiation_result( const mbedtls_ssl_context *ssl, |
| 2711 | mbedtls_dtls_srtp_info *dtls_srtp_info ) |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2712 | { |
Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 2713 | dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile; |
| 2714 | /* do not copy the mki value if there is no chosen profile */ |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 2715 | if( dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET ) |
Johan Pascal | 0dbcd1d | 2020-10-28 11:03:07 +0100 | [diff] [blame] | 2716 | { |
Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 2717 | dtls_srtp_info->mki_len = 0; |
Johan Pascal | 0dbcd1d | 2020-10-28 11:03:07 +0100 | [diff] [blame] | 2718 | } |
Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 2719 | else |
| 2720 | { |
| 2721 | dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len; |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 2722 | memcpy( dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value, |
| 2723 | ssl->dtls_srtp_info.mki_len ); |
Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 2724 | } |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2725 | } |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2726 | #endif /* MBEDTLS_SSL_DTLS_SRTP */ |
| 2727 | |
Aditya Patwardhan | 3096f33 | 2022-07-26 14:31:46 +0530 | [diff] [blame] | 2728 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 2729 | 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] | 2730 | { |
Glenn Strauss | 2dfcea2 | 2022-03-14 17:26:42 -0400 | [diff] [blame] | 2731 | conf->max_tls_version = (major << 8) | minor; |
Paul Bakker | 490ecc8 | 2011-10-06 13:04:09 +0000 | [diff] [blame] | 2732 | } |
| 2733 | |
Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 2734 | 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] | 2735 | { |
Glenn Strauss | 2dfcea2 | 2022-03-14 17:26:42 -0400 | [diff] [blame] | 2736 | conf->min_tls_version = (major << 8) | minor; |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 2737 | } |
Aditya Patwardhan | 3096f33 | 2022-07-26 14:31:46 +0530 | [diff] [blame] | 2738 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 2739 | |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 2740 | #if defined(MBEDTLS_SSL_SRV_C) |
| 2741 | void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf, |
| 2742 | char cert_req_ca_list ) |
| 2743 | { |
| 2744 | conf->cert_req_ca_list = cert_req_ca_list; |
| 2745 | } |
| 2746 | #endif |
| 2747 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2748 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2749 | 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] | 2750 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2751 | conf->encrypt_then_mac = etm; |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 2752 | } |
| 2753 | #endif |
| 2754 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2755 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2756 | 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] | 2757 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2758 | conf->extended_ms = ems; |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 2759 | } |
| 2760 | #endif |
| 2761 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2762 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2763 | 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] | 2764 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2765 | if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID || |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 2766 | 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] | 2767 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2768 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 2769 | } |
| 2770 | |
Manuel Pégourié-Gonnard | 6bf89d6 | 2015-05-05 17:01:57 +0100 | [diff] [blame] | 2771 | conf->mfl_code = mfl_code; |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 2772 | |
| 2773 | return( 0 ); |
| 2774 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2775 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 2776 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2777 | 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] | 2778 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2779 | conf->allow_legacy_renegotiation = allow_legacy; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2780 | } |
| 2781 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2782 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2783 | 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] | 2784 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2785 | conf->disable_renegotiation = renegotiation; |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 2786 | } |
| 2787 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2788 | 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] | 2789 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2790 | conf->renego_max_records = max_records; |
Manuel Pégourié-Gonnard | a9964db | 2014-07-03 19:29:16 +0200 | [diff] [blame] | 2791 | } |
| 2792 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2793 | void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 837f0fe | 2014-11-05 13:58:53 +0100 | [diff] [blame] | 2794 | const unsigned char period[8] ) |
| 2795 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2796 | memcpy( conf->renego_period, period, 8 ); |
Manuel Pégourié-Gonnard | 837f0fe | 2014-11-05 13:58:53 +0100 | [diff] [blame] | 2797 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2798 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2799 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2800 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 2801 | #if defined(MBEDTLS_SSL_CLI_C) |
| 2802 | 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] | 2803 | { |
Manuel Pégourié-Gonnard | 2b49445 | 2015-05-06 10:05:11 +0100 | [diff] [blame] | 2804 | conf->session_tickets = use_tickets; |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 2805 | } |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 2806 | #endif |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 2807 | |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 2808 | #if defined(MBEDTLS_SSL_SRV_C) |
Jerry Yu | 1ad7ace | 2022-08-09 13:28:39 +0800 | [diff] [blame] | 2809 | |
Jerry Yu | d0766ec | 2022-09-22 10:46:57 +0800 | [diff] [blame] | 2810 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS) |
Jerry Yu | 1ad7ace | 2022-08-09 13:28:39 +0800 | [diff] [blame] | 2811 | void mbedtls_ssl_conf_new_session_tickets( mbedtls_ssl_config *conf, |
| 2812 | uint16_t num_tickets ) |
| 2813 | { |
Jerry Yu | d0766ec | 2022-09-22 10:46:57 +0800 | [diff] [blame] | 2814 | conf->new_session_tickets_count = num_tickets; |
Jerry Yu | 1ad7ace | 2022-08-09 13:28:39 +0800 | [diff] [blame] | 2815 | } |
| 2816 | #endif |
| 2817 | |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 2818 | void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf, |
| 2819 | mbedtls_ssl_ticket_write_t *f_ticket_write, |
| 2820 | mbedtls_ssl_ticket_parse_t *f_ticket_parse, |
| 2821 | void *p_ticket ) |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 2822 | { |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 2823 | conf->f_ticket_write = f_ticket_write; |
| 2824 | conf->f_ticket_parse = f_ticket_parse; |
| 2825 | conf->p_ticket = p_ticket; |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 2826 | } |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 2827 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2828 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 2829 | |
Hanno Becker | 7e6c178 | 2021-06-08 09:24:55 +0100 | [diff] [blame] | 2830 | void mbedtls_ssl_set_export_keys_cb( mbedtls_ssl_context *ssl, |
| 2831 | mbedtls_ssl_export_keys_t *f_export_keys, |
| 2832 | void *p_export_keys ) |
Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 2833 | { |
Hanno Becker | 7e6c178 | 2021-06-08 09:24:55 +0100 | [diff] [blame] | 2834 | ssl->f_export_keys = f_export_keys; |
| 2835 | ssl->p_export_keys = p_export_keys; |
Ron Eldor | f5cc10d | 2019-05-07 18:33:40 +0300 | [diff] [blame] | 2836 | } |
Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 2837 | |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 2838 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 2839 | void mbedtls_ssl_conf_async_private_cb( |
| 2840 | mbedtls_ssl_config *conf, |
| 2841 | mbedtls_ssl_async_sign_t *f_async_sign, |
| 2842 | mbedtls_ssl_async_decrypt_t *f_async_decrypt, |
| 2843 | mbedtls_ssl_async_resume_t *f_async_resume, |
| 2844 | mbedtls_ssl_async_cancel_t *f_async_cancel, |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 2845 | void *async_config_data ) |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 2846 | { |
| 2847 | conf->f_async_sign_start = f_async_sign; |
| 2848 | conf->f_async_decrypt_start = f_async_decrypt; |
| 2849 | conf->f_async_resume = f_async_resume; |
| 2850 | conf->f_async_cancel = f_async_cancel; |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 2851 | conf->p_async_config_data = async_config_data; |
| 2852 | } |
| 2853 | |
Gilles Peskine | 8f97af7 | 2018-04-26 11:46:10 +0200 | [diff] [blame] | 2854 | void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf ) |
| 2855 | { |
| 2856 | return( conf->p_async_config_data ); |
| 2857 | } |
| 2858 | |
Gilles Peskine | 1febfef | 2018-04-30 11:54:39 +0200 | [diff] [blame] | 2859 | void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl ) |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 2860 | { |
| 2861 | if( ssl->handshake == NULL ) |
| 2862 | return( NULL ); |
| 2863 | else |
| 2864 | return( ssl->handshake->user_async_ctx ); |
| 2865 | } |
| 2866 | |
Gilles Peskine | 1febfef | 2018-04-30 11:54:39 +0200 | [diff] [blame] | 2867 | void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl, |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 2868 | void *ctx ) |
| 2869 | { |
| 2870 | if( ssl->handshake != NULL ) |
| 2871 | ssl->handshake->user_async_ctx = ctx; |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 2872 | } |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 2873 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 2874 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2875 | /* |
| 2876 | * SSL get accessors |
| 2877 | */ |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 2878 | uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2879 | { |
Manuel Pégourié-Gonnard | e89163c | 2015-01-23 14:30:57 +0000 | [diff] [blame] | 2880 | if( ssl->session != NULL ) |
| 2881 | return( ssl->session->verify_result ); |
| 2882 | |
| 2883 | if( ssl->session_negotiate != NULL ) |
| 2884 | return( ssl->session_negotiate->verify_result ); |
| 2885 | |
Manuel Pégourié-Gonnard | 6ab9b00 | 2015-05-14 11:25:04 +0200 | [diff] [blame] | 2886 | return( 0xFFFFFFFF ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2887 | } |
| 2888 | |
Glenn Strauss | 8f52690 | 2022-01-13 00:04:49 -0500 | [diff] [blame] | 2889 | int mbedtls_ssl_get_ciphersuite_id_from_ssl( const mbedtls_ssl_context *ssl ) |
| 2890 | { |
| 2891 | if( ssl == NULL || ssl->session == NULL ) |
| 2892 | return( 0 ); |
| 2893 | |
| 2894 | return( ssl->session->ciphersuite ); |
| 2895 | } |
| 2896 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2897 | const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl ) |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 2898 | { |
Paul Bakker | 926c8e4 | 2013-03-06 10:23:34 +0100 | [diff] [blame] | 2899 | if( ssl == NULL || ssl->session == NULL ) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 2900 | return( NULL ); |
Paul Bakker | 926c8e4 | 2013-03-06 10:23:34 +0100 | [diff] [blame] | 2901 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2902 | return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite ); |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 2903 | } |
| 2904 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2905 | const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl ) |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 2906 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2907 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2908 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 2909 | { |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 2910 | switch( ssl->tls_version ) |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 2911 | { |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 2912 | case MBEDTLS_SSL_VERSION_TLS1_2: |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 2913 | return( "DTLSv1.2" ); |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 2914 | default: |
| 2915 | return( "unknown (DTLS)" ); |
| 2916 | } |
| 2917 | } |
| 2918 | #endif |
| 2919 | |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 2920 | switch( ssl->tls_version ) |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 2921 | { |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 2922 | case MBEDTLS_SSL_VERSION_TLS1_2: |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2923 | return( "TLSv1.2" ); |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 2924 | case MBEDTLS_SSL_VERSION_TLS1_3: |
Gilles Peskine | c63a1e0 | 2022-01-13 01:10:24 +0100 | [diff] [blame] | 2925 | return( "TLSv1.3" ); |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 2926 | default: |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 2927 | return( "unknown" ); |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 2928 | } |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 2929 | } |
| 2930 | |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 2931 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 2932 | size_t mbedtls_ssl_get_input_max_frag_len( const mbedtls_ssl_context *ssl ) |
| 2933 | { |
David Horstmann | 95d516f | 2021-05-04 18:36:56 +0100 | [diff] [blame] | 2934 | size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN; |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 2935 | size_t read_mfl; |
| 2936 | |
| 2937 | /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */ |
| 2938 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
| 2939 | ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE ) |
| 2940 | { |
| 2941 | return ssl_mfl_code_to_length( ssl->conf->mfl_code ); |
| 2942 | } |
| 2943 | |
| 2944 | /* Check if a smaller max length was negotiated */ |
| 2945 | if( ssl->session_out != NULL ) |
| 2946 | { |
| 2947 | read_mfl = ssl_mfl_code_to_length( ssl->session_out->mfl_code ); |
| 2948 | if( read_mfl < max_len ) |
| 2949 | { |
| 2950 | max_len = read_mfl; |
| 2951 | } |
| 2952 | } |
| 2953 | |
| 2954 | // During a handshake, use the value being negotiated |
| 2955 | if( ssl->session_negotiate != NULL ) |
| 2956 | { |
| 2957 | read_mfl = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ); |
| 2958 | if( read_mfl < max_len ) |
| 2959 | { |
| 2960 | max_len = read_mfl; |
| 2961 | } |
| 2962 | } |
| 2963 | |
| 2964 | return( max_len ); |
| 2965 | } |
| 2966 | |
| 2967 | 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] | 2968 | { |
| 2969 | size_t max_len; |
| 2970 | |
| 2971 | /* |
| 2972 | * Assume mfl_code is correct since it was checked when set |
| 2973 | */ |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 2974 | 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] | 2975 | |
Manuel Pégourié-Gonnard | 2cb17e2 | 2017-09-19 13:00:47 +0200 | [diff] [blame] | 2976 | /* Check if a smaller max length was negotiated */ |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 2977 | if( ssl->session_out != NULL && |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 2978 | 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] | 2979 | { |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 2980 | 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] | 2981 | } |
| 2982 | |
Manuel Pégourié-Gonnard | 2cb17e2 | 2017-09-19 13:00:47 +0200 | [diff] [blame] | 2983 | /* During a handshake, use the value being negotiated */ |
| 2984 | if( ssl->session_negotiate != NULL && |
| 2985 | ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len ) |
| 2986 | { |
| 2987 | max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ); |
| 2988 | } |
| 2989 | |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 2990 | return( max_len ); |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 2991 | } |
| 2992 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
| 2993 | |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 2994 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 8949071 | 2020-02-05 10:50:12 +0000 | [diff] [blame] | 2995 | 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] | 2996 | { |
Andrzej Kurek | ef43ce6 | 2018-10-09 08:24:12 -0400 | [diff] [blame] | 2997 | /* Return unlimited mtu for client hello messages to avoid fragmentation. */ |
| 2998 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
| 2999 | ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO || |
| 3000 | ssl->state == MBEDTLS_SSL_SERVER_HELLO ) ) |
| 3001 | return ( 0 ); |
| 3002 | |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 3003 | if( ssl->handshake == NULL || ssl->handshake->mtu == 0 ) |
| 3004 | return( ssl->mtu ); |
| 3005 | |
| 3006 | if( ssl->mtu == 0 ) |
| 3007 | return( ssl->handshake->mtu ); |
| 3008 | |
| 3009 | return( ssl->mtu < ssl->handshake->mtu ? |
| 3010 | ssl->mtu : ssl->handshake->mtu ); |
| 3011 | } |
| 3012 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 3013 | |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3014 | int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl ) |
| 3015 | { |
| 3016 | size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN; |
| 3017 | |
Manuel Pégourié-Gonnard | 000281e | 2018-08-21 11:20:58 +0200 | [diff] [blame] | 3018 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \ |
| 3019 | !defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3020 | (void) ssl; |
| 3021 | #endif |
| 3022 | |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3023 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 3024 | 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] | 3025 | |
| 3026 | if( max_len > mfl ) |
| 3027 | max_len = mfl; |
| 3028 | #endif |
| 3029 | |
| 3030 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 8949071 | 2020-02-05 10:50:12 +0000 | [diff] [blame] | 3031 | if( mbedtls_ssl_get_current_mtu( ssl ) != 0 ) |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3032 | { |
Hanno Becker | 8949071 | 2020-02-05 10:50:12 +0000 | [diff] [blame] | 3033 | const size_t mtu = mbedtls_ssl_get_current_mtu( ssl ); |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3034 | const int ret = mbedtls_ssl_get_record_expansion( ssl ); |
| 3035 | const size_t overhead = (size_t) ret; |
| 3036 | |
| 3037 | if( ret < 0 ) |
| 3038 | return( ret ); |
| 3039 | |
| 3040 | if( mtu <= overhead ) |
| 3041 | { |
| 3042 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) ); |
| 3043 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 3044 | } |
| 3045 | |
| 3046 | if( max_len > mtu - overhead ) |
| 3047 | max_len = mtu - overhead; |
| 3048 | } |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 3049 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3050 | |
Hanno Becker | 0defedb | 2018-08-10 12:35:02 +0100 | [diff] [blame] | 3051 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \ |
| 3052 | !defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3053 | ((void) ssl); |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3054 | #endif |
| 3055 | |
| 3056 | return( (int) max_len ); |
| 3057 | } |
| 3058 | |
Hanno Becker | 2d8e99b | 2021-04-21 06:19:50 +0100 | [diff] [blame] | 3059 | int mbedtls_ssl_get_max_in_record_payload( const mbedtls_ssl_context *ssl ) |
| 3060 | { |
| 3061 | size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN; |
| 3062 | |
| 3063 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 3064 | (void) ssl; |
| 3065 | #endif |
| 3066 | |
| 3067 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 3068 | const size_t mfl = mbedtls_ssl_get_input_max_frag_len( ssl ); |
| 3069 | |
| 3070 | if( max_len > mfl ) |
| 3071 | max_len = mfl; |
| 3072 | #endif |
| 3073 | |
| 3074 | return( (int) max_len ); |
| 3075 | } |
| 3076 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3077 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 3078 | 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] | 3079 | { |
| 3080 | if( ssl == NULL || ssl->session == NULL ) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 3081 | return( NULL ); |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 3082 | |
Hanno Becker | e682457 | 2019-02-07 13:18:46 +0000 | [diff] [blame] | 3083 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 3084 | return( ssl->session->peer_cert ); |
Hanno Becker | e682457 | 2019-02-07 13:18:46 +0000 | [diff] [blame] | 3085 | #else |
| 3086 | return( NULL ); |
| 3087 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 3088 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3089 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 3090 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3091 | #if defined(MBEDTLS_SSL_CLI_C) |
Hanno Becker | f852b1c | 2019-02-05 11:42:30 +0000 | [diff] [blame] | 3092 | int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, |
| 3093 | mbedtls_ssl_session *dst ) |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 3094 | { |
Hanno Becker | e810bbc | 2021-05-14 16:01:05 +0100 | [diff] [blame] | 3095 | int ret; |
| 3096 | |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 3097 | if( ssl == NULL || |
| 3098 | dst == NULL || |
| 3099 | ssl->session == NULL || |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3100 | ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT ) |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 3101 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3102 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 3103 | } |
| 3104 | |
Hanno Becker | e810bbc | 2021-05-14 16:01:05 +0100 | [diff] [blame] | 3105 | /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer |
| 3106 | * idempotent: Each session can only be exported once. |
| 3107 | * |
| 3108 | * (This is in preparation for TLS 1.3 support where we will |
| 3109 | * need the ability to export multiple sessions (aka tickets), |
| 3110 | * which will be achieved by calling mbedtls_ssl_get_session() |
| 3111 | * multiple times until it fails.) |
| 3112 | * |
| 3113 | * Check whether we have already exported the current session, |
| 3114 | * and fail if so. |
| 3115 | */ |
| 3116 | if( ssl->session->exported == 1 ) |
| 3117 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 3118 | |
| 3119 | ret = mbedtls_ssl_session_copy( dst, ssl->session ); |
| 3120 | if( ret != 0 ) |
| 3121 | return( ret ); |
| 3122 | |
| 3123 | /* Remember that we've exported the session. */ |
| 3124 | ssl->session->exported = 1; |
| 3125 | return( 0 ); |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 3126 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3127 | #endif /* MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 3128 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3129 | /* |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 3130 | * Define ticket header determining Mbed TLS version |
| 3131 | * and structure of the ticket. |
| 3132 | */ |
| 3133 | |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3134 | /* |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 3135 | * Define bitflag determining compile-time settings influencing |
| 3136 | * structure of serialized SSL sessions. |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3137 | */ |
| 3138 | |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 3139 | #if defined(MBEDTLS_HAVE_TIME) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3140 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME 1 |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 3141 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3142 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3143 | #endif /* MBEDTLS_HAVE_TIME */ |
| 3144 | |
| 3145 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3146 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3147 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3148 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3149 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 3150 | |
| 3151 | #if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3152 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3153 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3154 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3155 | #endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */ |
| 3156 | |
| 3157 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3158 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3159 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3160 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3161 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
| 3162 | |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3163 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3164 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3165 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3166 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3167 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ |
| 3168 | |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3169 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 3170 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1 |
| 3171 | #else |
| 3172 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0 |
| 3173 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
| 3174 | |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3175 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0 |
| 3176 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1 |
| 3177 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2 |
| 3178 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3 |
Hanno Becker | 37bdbe6 | 2021-08-01 05:38:58 +0100 | [diff] [blame] | 3179 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4 |
| 3180 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5 |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3181 | |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 3182 | #define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \ |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3183 | ( (uint16_t) ( \ |
| 3184 | ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \ |
| 3185 | ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \ |
| 3186 | ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \ |
| 3187 | ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \ |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3188 | ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \ |
Hanno Becker | be34e8e | 2019-06-04 09:43:16 +0100 | [diff] [blame] | 3189 | ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) ) ) |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3190 | |
Hanno Becker | f878707 | 2019-05-16 12:41:07 +0100 | [diff] [blame] | 3191 | static unsigned char ssl_serialized_session_header[] = { |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3192 | MBEDTLS_VERSION_MAJOR, |
| 3193 | MBEDTLS_VERSION_MINOR, |
| 3194 | MBEDTLS_VERSION_PATCH, |
Joe Subbiani | 2194dc4 | 2021-07-14 12:31:31 +0100 | [diff] [blame] | 3195 | MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ), |
| 3196 | MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ), |
Hanno Becker | f878707 | 2019-05-16 12:41:07 +0100 | [diff] [blame] | 3197 | }; |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 3198 | |
| 3199 | /* |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 3200 | * Serialize a session in the following format: |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 3201 | * (in the presentation language of TLS, RFC 8446 section 3) |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 3202 | * |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3203 | * struct { |
Hanno Becker | dc28b6c | 2019-05-29 11:08:00 +0100 | [diff] [blame] | 3204 | * |
Hanno Becker | dce5097 | 2021-08-01 05:39:23 +0100 | [diff] [blame] | 3205 | * opaque mbedtls_version[3]; // library version: major, minor, patch |
| 3206 | * opaque session_format[2]; // library-version specific 16-bit field |
| 3207 | * // determining the format of the remaining |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3208 | * // serialized data. |
Hanno Becker | dc28b6c | 2019-05-29 11:08:00 +0100 | [diff] [blame] | 3209 | * |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3210 | * Note: When updating the format, remember to keep |
| 3211 | * these version+format bytes. |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 3212 | * |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3213 | * // In this version, `session_format` determines |
| 3214 | * // the setting of those compile-time |
| 3215 | * // configuration options which influence |
| 3216 | * // the structure of mbedtls_ssl_session. |
| 3217 | * |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 3218 | * uint8_t minor_ver; // Protocol minor version. Possible values: |
| 3219 | * // - TLS 1.2 (3) |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3220 | * |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 3221 | * select (serialized_session.tls_version) { |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3222 | * |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 3223 | * case MBEDTLS_SSL_VERSION_TLS1_2: |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3224 | * serialized_session_tls12 data; |
| 3225 | * |
| 3226 | * }; |
| 3227 | * |
| 3228 | * } serialized_session; |
| 3229 | * |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 3230 | */ |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3231 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 3232 | MBEDTLS_CHECK_RETURN_CRITICAL |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3233 | static int ssl_session_save( const mbedtls_ssl_session *session, |
| 3234 | unsigned char omit_header, |
| 3235 | unsigned char *buf, |
| 3236 | size_t buf_len, |
| 3237 | size_t *olen ) |
| 3238 | { |
| 3239 | unsigned char *p = buf; |
| 3240 | size_t used = 0; |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 3241 | size_t remaining_len; |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 3242 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 3243 | size_t out_len; |
| 3244 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 3245 | #endif |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 3246 | if( session == NULL ) |
| 3247 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 3248 | |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3249 | if( !omit_header ) |
| 3250 | { |
| 3251 | /* |
| 3252 | * Add Mbed TLS version identifier |
| 3253 | */ |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3254 | used += sizeof( ssl_serialized_session_header ); |
| 3255 | |
| 3256 | if( used <= buf_len ) |
| 3257 | { |
| 3258 | memcpy( p, ssl_serialized_session_header, |
| 3259 | sizeof( ssl_serialized_session_header ) ); |
| 3260 | p += sizeof( ssl_serialized_session_header ); |
| 3261 | } |
| 3262 | } |
| 3263 | |
| 3264 | /* |
| 3265 | * TLS version identifier |
| 3266 | */ |
| 3267 | used += 1; |
| 3268 | if( used <= buf_len ) |
| 3269 | { |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 3270 | *p++ = MBEDTLS_BYTE_0( session->tls_version ); |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3271 | } |
| 3272 | |
| 3273 | /* Forward to version-specific serialization routine. */ |
Jerry Yu | fca4d57 | 2022-07-21 10:37:48 +0800 | [diff] [blame] | 3274 | remaining_len = (buf_len >= used) ? buf_len - used : 0; |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 3275 | switch( session->tls_version ) |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3276 | { |
| 3277 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 3278 | case MBEDTLS_SSL_VERSION_TLS1_2: |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 3279 | used += ssl_tls12_session_save( session, p, remaining_len ); |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3280 | break; |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3281 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 3282 | |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 3283 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 3284 | case MBEDTLS_SSL_VERSION_TLS1_3: |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 3285 | ret = ssl_tls13_session_save( session, p, remaining_len, &out_len ); |
| 3286 | if( ret != 0 && ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ) |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 3287 | return( ret ); |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 3288 | used += out_len; |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 3289 | break; |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 3290 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 3291 | |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3292 | default: |
| 3293 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 3294 | } |
| 3295 | |
| 3296 | *olen = used; |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 3297 | if( used > buf_len ) |
| 3298 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 3299 | |
| 3300 | return( 0 ); |
| 3301 | } |
| 3302 | |
| 3303 | /* |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 3304 | * Public wrapper for ssl_session_save() |
| 3305 | */ |
| 3306 | int mbedtls_ssl_session_save( const mbedtls_ssl_session *session, |
| 3307 | unsigned char *buf, |
| 3308 | size_t buf_len, |
| 3309 | size_t *olen ) |
| 3310 | { |
| 3311 | return( ssl_session_save( session, 0, buf, buf_len, olen ) ); |
| 3312 | } |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 3313 | |
Jerry Yu | f1b23ca | 2022-02-18 11:48:47 +0800 | [diff] [blame] | 3314 | /* |
| 3315 | * Deserialize session, see mbedtls_ssl_session_save() for format. |
| 3316 | * |
| 3317 | * This internal version is wrapped by a public function that cleans up in |
| 3318 | * case of error, and has an extra option omit_header. |
| 3319 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 3320 | MBEDTLS_CHECK_RETURN_CRITICAL |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3321 | static int ssl_session_load( mbedtls_ssl_session *session, |
| 3322 | unsigned char omit_header, |
| 3323 | const unsigned char *buf, |
| 3324 | size_t len ) |
| 3325 | { |
| 3326 | const unsigned char *p = buf; |
| 3327 | const unsigned char * const end = buf + len; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 3328 | size_t remaining_len; |
| 3329 | |
| 3330 | |
| 3331 | if( session == NULL ) |
| 3332 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3333 | |
| 3334 | if( !omit_header ) |
| 3335 | { |
| 3336 | /* |
| 3337 | * Check Mbed TLS version identifier |
| 3338 | */ |
| 3339 | |
| 3340 | if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) ) |
| 3341 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3342 | |
| 3343 | if( memcmp( p, ssl_serialized_session_header, |
| 3344 | sizeof( ssl_serialized_session_header ) ) != 0 ) |
| 3345 | { |
| 3346 | return( MBEDTLS_ERR_SSL_VERSION_MISMATCH ); |
| 3347 | } |
| 3348 | p += sizeof( ssl_serialized_session_header ); |
| 3349 | } |
| 3350 | |
| 3351 | /* |
| 3352 | * TLS version identifier |
| 3353 | */ |
| 3354 | if( 1 > (size_t)( end - p ) ) |
| 3355 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 3356 | session->tls_version = 0x0300 | *p++; |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3357 | |
| 3358 | /* Dispatch according to TLS version. */ |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 3359 | remaining_len = ( end - p ); |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 3360 | switch( session->tls_version ) |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3361 | { |
| 3362 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 3363 | case MBEDTLS_SSL_VERSION_TLS1_2: |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 3364 | return( ssl_tls12_session_load( session, p, remaining_len ) ); |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3365 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 3366 | |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 3367 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 3368 | case MBEDTLS_SSL_VERSION_TLS1_3: |
| 3369 | return( ssl_tls13_session_load( session, p, remaining_len ) ); |
| 3370 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 3371 | |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3372 | default: |
| 3373 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3374 | } |
| 3375 | } |
| 3376 | |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 3377 | /* |
Manuel Pégourié-Gonnard | b9dfc9f | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 3378 | * Deserialize session: public wrapper for error cleaning |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 3379 | */ |
| 3380 | int mbedtls_ssl_session_load( mbedtls_ssl_session *session, |
| 3381 | const unsigned char *buf, |
| 3382 | size_t len ) |
| 3383 | { |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 3384 | int ret = ssl_session_load( session, 0, buf, len ); |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 3385 | |
| 3386 | if( ret != 0 ) |
| 3387 | mbedtls_ssl_session_free( session ); |
| 3388 | |
| 3389 | return( ret ); |
| 3390 | } |
| 3391 | |
| 3392 | /* |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3393 | * Perform a single step of the SSL handshake |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3394 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 3395 | MBEDTLS_CHECK_RETURN_CRITICAL |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 3396 | static int ssl_prepare_handshake_step( mbedtls_ssl_context *ssl ) |
| 3397 | { |
| 3398 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 3399 | |
Ronald Cron | 66dbf91 | 2022-02-02 15:33:46 +0100 | [diff] [blame] | 3400 | /* |
| 3401 | * 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] | 3402 | * that were written into the output buffer by the previous handshake step, |
| 3403 | * if the write to the network callback returned with the |
Ronald Cron | 66dbf91 | 2022-02-02 15:33:46 +0100 | [diff] [blame] | 3404 | * #MBEDTLS_ERR_SSL_WANT_WRITE error code. |
| 3405 | * We proceed to the next handshake step only when all data from the |
| 3406 | * previous one have been sent to the peer, thus we make sure that this is |
| 3407 | * the case here by calling `mbedtls_ssl_flush_output()`. The function may |
| 3408 | * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case |
| 3409 | * we have to wait before to go ahead. |
| 3410 | * In the case of TLS 1.3, handshake step handlers do not send data to the |
| 3411 | * peer. Data are only sent here and through |
| 3412 | * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an |
Andrzej Kurek | 5c65c57 | 2022-04-13 14:28:52 -0400 | [diff] [blame] | 3413 | * alert occurred. |
Ronald Cron | 66dbf91 | 2022-02-02 15:33:46 +0100 | [diff] [blame] | 3414 | */ |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 3415 | if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) |
| 3416 | return( ret ); |
| 3417 | |
| 3418 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3419 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 3420 | ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING ) |
| 3421 | { |
| 3422 | if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 ) |
| 3423 | return( ret ); |
| 3424 | } |
| 3425 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 3426 | |
| 3427 | return( ret ); |
| 3428 | } |
| 3429 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3430 | int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3431 | { |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 3432 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3433 | |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 3434 | if( ssl == NULL || |
| 3435 | ssl->conf == NULL || |
| 3436 | ssl->handshake == NULL || |
Paul Elliott | 27b0d94 | 2022-03-18 21:55:32 +0000 | [diff] [blame] | 3437 | mbedtls_ssl_is_handshake_over( ssl ) == 1 ) |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 3438 | { |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 3439 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 3440 | } |
| 3441 | |
| 3442 | ret = ssl_prepare_handshake_step( ssl ); |
| 3443 | if( ret != 0 ) |
| 3444 | return( ret ); |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 3445 | |
Jerry Yu | e704781 | 2021-09-13 19:26:39 +0800 | [diff] [blame] | 3446 | ret = mbedtls_ssl_handle_pending_alert( ssl ); |
| 3447 | if( ret != 0 ) |
| 3448 | goto cleanup; |
| 3449 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3450 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3451 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 3452 | { |
Ronald Cron | 27c85e7 | 2022-03-08 11:37:55 +0100 | [diff] [blame] | 3453 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "client state: %s", |
| 3454 | mbedtls_ssl_states_str( ssl->state ) ) ); |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 3455 | |
Ronald Cron | 9f0fba3 | 2022-02-10 16:45:15 +0100 | [diff] [blame] | 3456 | switch( ssl->state ) |
| 3457 | { |
| 3458 | case MBEDTLS_SSL_HELLO_REQUEST: |
| 3459 | ssl->state = MBEDTLS_SSL_CLIENT_HELLO; |
| 3460 | break; |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 3461 | |
Ronald Cron | 9f0fba3 | 2022-02-10 16:45:15 +0100 | [diff] [blame] | 3462 | case MBEDTLS_SSL_CLIENT_HELLO: |
| 3463 | ret = mbedtls_ssl_write_client_hello( ssl ); |
| 3464 | break; |
| 3465 | |
| 3466 | default: |
| 3467 | #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] | 3468 | if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 ) |
Ronald Cron | 9f0fba3 | 2022-02-10 16:45:15 +0100 | [diff] [blame] | 3469 | ret = mbedtls_ssl_tls13_handshake_client_step( ssl ); |
| 3470 | else |
| 3471 | ret = mbedtls_ssl_handshake_client_step( ssl ); |
| 3472 | #elif defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 3473 | ret = mbedtls_ssl_handshake_client_step( ssl ); |
| 3474 | #else |
| 3475 | ret = mbedtls_ssl_tls13_handshake_client_step( ssl ); |
| 3476 | #endif |
| 3477 | } |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 3478 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3479 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3480 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3481 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 3482 | { |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3483 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 3484 | if( mbedtls_ssl_conf_is_tls13_only( ssl->conf ) ) |
Jerry Yu | 2756193 | 2021-08-27 17:07:38 +0800 | [diff] [blame] | 3485 | ret = mbedtls_ssl_tls13_handshake_server_step( ssl ); |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3486 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 3487 | |
| 3488 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 3489 | if( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) ) |
| 3490 | ret = mbedtls_ssl_handshake_server_step( ssl ); |
| 3491 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 3492 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3493 | #endif |
| 3494 | |
Jerry Yu | e704781 | 2021-09-13 19:26:39 +0800 | [diff] [blame] | 3495 | if( ret != 0 ) |
| 3496 | { |
Jerry Yu | bbd5a3f | 2021-09-18 20:50:22 +0800 | [diff] [blame] | 3497 | /* handshake_step return error. And it is same |
| 3498 | * with alert_reason. |
| 3499 | */ |
Jerry Yu | 3bf1f97 | 2021-09-22 21:37:18 +0800 | [diff] [blame] | 3500 | if( ssl->send_alert ) |
Jerry Yu | e704781 | 2021-09-13 19:26:39 +0800 | [diff] [blame] | 3501 | { |
Jerry Yu | 3bf1f97 | 2021-09-22 21:37:18 +0800 | [diff] [blame] | 3502 | ret = mbedtls_ssl_handle_pending_alert( ssl ); |
Jerry Yu | e704781 | 2021-09-13 19:26:39 +0800 | [diff] [blame] | 3503 | goto cleanup; |
| 3504 | } |
| 3505 | } |
| 3506 | |
| 3507 | cleanup: |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3508 | return( ret ); |
| 3509 | } |
| 3510 | |
| 3511 | /* |
| 3512 | * Perform the SSL handshake |
| 3513 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3514 | int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl ) |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3515 | { |
| 3516 | int ret = 0; |
| 3517 | |
Hanno Becker | a817ea4 | 2020-10-20 15:20:23 +0100 | [diff] [blame] | 3518 | /* Sanity checks */ |
| 3519 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 3520 | if( ssl == NULL || ssl->conf == NULL ) |
| 3521 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3522 | |
Hanno Becker | a817ea4 | 2020-10-20 15:20:23 +0100 | [diff] [blame] | 3523 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3524 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 3525 | ( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL ) ) |
| 3526 | { |
| 3527 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use " |
| 3528 | "mbedtls_ssl_set_timer_cb() for DTLS" ) ); |
| 3529 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3530 | } |
| 3531 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 3532 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3533 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3534 | |
Hanno Becker | a817ea4 | 2020-10-20 15:20:23 +0100 | [diff] [blame] | 3535 | /* Main handshake loop */ |
Paul Elliott | 27b0d94 | 2022-03-18 21:55:32 +0000 | [diff] [blame] | 3536 | while( mbedtls_ssl_is_handshake_over( ssl ) == 0 ) |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3537 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3538 | ret = mbedtls_ssl_handshake_step( ssl ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3539 | |
| 3540 | if( ret != 0 ) |
| 3541 | break; |
| 3542 | } |
| 3543 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3544 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3545 | |
| 3546 | return( ret ); |
| 3547 | } |
| 3548 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3549 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 3550 | #if defined(MBEDTLS_SSL_SRV_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3551 | /* |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3552 | * Write HelloRequest to request renegotiation on server |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3553 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 3554 | MBEDTLS_CHECK_RETURN_CRITICAL |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3555 | static int ssl_write_hello_request( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3556 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3557 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3558 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3559 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) ); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3560 | |
| 3561 | ssl->out_msglen = 4; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3562 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 3563 | ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST; |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3564 | |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3565 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3566 | { |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3567 | 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] | 3568 | return( ret ); |
| 3569 | } |
| 3570 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3571 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) ); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3572 | |
| 3573 | return( 0 ); |
| 3574 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3575 | #endif /* MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3576 | |
| 3577 | /* |
| 3578 | * Actually renegotiate current connection, triggered by either: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3579 | * - any side: calling mbedtls_ssl_renegotiate(), |
| 3580 | * - client: receiving a HelloRequest during mbedtls_ssl_read(), |
| 3581 | * - 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] | 3582 | * the initial handshake is completed. |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3583 | * 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] | 3584 | * 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] | 3585 | */ |
Hanno Becker | 40cdaa1 | 2020-02-05 10:48:27 +0000 | [diff] [blame] | 3586 | int mbedtls_ssl_start_renegotiation( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3587 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3588 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3589 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3590 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3591 | |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3592 | if( ( ret = ssl_handshake_init( ssl ) ) != 0 ) |
| 3593 | return( ret ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3594 | |
Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 3595 | /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and |
| 3596 | * the ServerHello will have message_seq = 1" */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3597 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3598 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3599 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ) |
Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 3600 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3601 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 3602 | ssl->handshake->out_msg_seq = 1; |
| 3603 | else |
| 3604 | ssl->handshake->in_msg_seq = 1; |
Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 3605 | } |
| 3606 | #endif |
| 3607 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3608 | ssl->state = MBEDTLS_SSL_HELLO_REQUEST; |
| 3609 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3610 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3611 | if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3612 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3613 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3614 | return( ret ); |
| 3615 | } |
| 3616 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3617 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3618 | |
| 3619 | return( 0 ); |
| 3620 | } |
| 3621 | |
| 3622 | /* |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3623 | * Renegotiate current connection on client, |
| 3624 | * or request renegotiation on server |
| 3625 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3626 | int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3627 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3628 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3629 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 3630 | if( ssl == NULL || ssl->conf == NULL ) |
| 3631 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3632 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3633 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3634 | /* On server, just send the request */ |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3635 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3636 | { |
Paul Elliott | 27b0d94 | 2022-03-18 21:55:32 +0000 | [diff] [blame] | 3637 | if( mbedtls_ssl_is_handshake_over( ssl ) == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3638 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3639 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3640 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING; |
Manuel Pégourié-Gonnard | f07f421 | 2014-08-15 19:04:47 +0200 | [diff] [blame] | 3641 | |
| 3642 | /* Did we already try/start sending HelloRequest? */ |
| 3643 | if( ssl->out_left != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3644 | return( mbedtls_ssl_flush_output( ssl ) ); |
Manuel Pégourié-Gonnard | f07f421 | 2014-08-15 19:04:47 +0200 | [diff] [blame] | 3645 | |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3646 | return( ssl_write_hello_request( ssl ) ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3647 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3648 | #endif /* MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3649 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3650 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3651 | /* |
| 3652 | * On client, either start the renegotiation process or, |
| 3653 | * if already in progress, continue the handshake |
| 3654 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3655 | if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3656 | { |
Paul Elliott | 27b0d94 | 2022-03-18 21:55:32 +0000 | [diff] [blame] | 3657 | if( mbedtls_ssl_is_handshake_over( ssl ) == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3658 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3659 | |
Hanno Becker | 40cdaa1 | 2020-02-05 10:48:27 +0000 | [diff] [blame] | 3660 | if( ( ret = mbedtls_ssl_start_renegotiation( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3661 | { |
Hanno Becker | 40cdaa1 | 2020-02-05 10:48:27 +0000 | [diff] [blame] | 3662 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_start_renegotiation", ret ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3663 | return( ret ); |
| 3664 | } |
| 3665 | } |
| 3666 | else |
| 3667 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3668 | if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3669 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3670 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3671 | return( ret ); |
| 3672 | } |
| 3673 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3674 | #endif /* MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3675 | |
Paul Bakker | 37ce0ff | 2013-10-31 14:32:04 +0100 | [diff] [blame] | 3676 | return( ret ); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3677 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3678 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3679 | |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 3680 | void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3681 | { |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 3682 | mbedtls_ssl_handshake_params *handshake = ssl->handshake; |
| 3683 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3684 | if( handshake == NULL ) |
| 3685 | return; |
| 3686 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 3687 | #if defined(MBEDTLS_ECP_C) |
| 3688 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 3689 | if ( ssl->handshake->group_list_heap_allocated ) |
| 3690 | mbedtls_free( (void*) handshake->group_list ); |
| 3691 | handshake->group_list = NULL; |
| 3692 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
| 3693 | #endif /* MBEDTLS_ECP_C */ |
| 3694 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 3695 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 3696 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 3697 | if ( ssl->handshake->sig_algs_heap_allocated ) |
| 3698 | mbedtls_free( (void*) handshake->sig_algs ); |
| 3699 | handshake->sig_algs = NULL; |
| 3700 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
Xiaofei Bai | c234ecf | 2022-02-08 09:59:23 +0000 | [diff] [blame] | 3701 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 3702 | if( ssl->handshake->certificate_request_context ) |
| 3703 | { |
| 3704 | mbedtls_free( (void*) handshake->certificate_request_context ); |
| 3705 | } |
| 3706 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 3707 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 3708 | |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3709 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
| 3710 | if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 ) |
| 3711 | { |
Gilles Peskine | 8f97af7 | 2018-04-26 11:46:10 +0200 | [diff] [blame] | 3712 | ssl->conf->f_async_cancel( ssl ); |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3713 | handshake->async_in_progress = 0; |
| 3714 | } |
| 3715 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
| 3716 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 3717 | #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] | 3718 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 3719 | psa_hash_abort( &handshake->fin_sha256_psa ); |
| 3720 | #else |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 3721 | mbedtls_sha256_free( &handshake->fin_sha256 ); |
| 3722 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3723 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 3724 | #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] | 3725 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 3726 | psa_hash_abort( &handshake->fin_sha384_psa ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3727 | #else |
Andrzej Kurek | a242e83 | 2022-08-11 10:03:14 -0400 | [diff] [blame] | 3728 | mbedtls_sha512_free( &handshake->fin_sha384 ); |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 3729 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3730 | #endif |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 3731 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3732 | #if defined(MBEDTLS_DHM_C) |
| 3733 | mbedtls_dhm_free( &handshake->dhm_ctx ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3734 | #endif |
Neil Armstrong | f3f4641 | 2022-04-12 14:43:39 +0200 | [diff] [blame] | 3735 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3736 | mbedtls_ecdh_free( &handshake->ecdh_ctx ); |
Paul Bakker | 61d113b | 2013-07-04 11:51:43 +0200 | [diff] [blame] | 3737 | #endif |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 3738 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 3739 | mbedtls_ecjpake_free( &handshake->ecjpake_ctx ); |
Manuel Pégourié-Gonnard | 77c0646 | 2015-09-17 13:59:49 +0200 | [diff] [blame] | 3740 | #if defined(MBEDTLS_SSL_CLI_C) |
| 3741 | mbedtls_free( handshake->ecjpake_cache ); |
| 3742 | handshake->ecjpake_cache = NULL; |
| 3743 | handshake->ecjpake_cache_len = 0; |
| 3744 | #endif |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 3745 | #endif |
Paul Bakker | 61d113b | 2013-07-04 11:51:43 +0200 | [diff] [blame] | 3746 | |
Janos Follath | 4ae5c29 | 2016-02-10 11:27:43 +0000 | [diff] [blame] | 3747 | #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ |
| 3748 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 3749 | /* explicit void pointer cast for buggy MS compiler */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3750 | mbedtls_free( (void *) handshake->curves ); |
Manuel Pégourié-Gonnard | d09453c | 2013-09-23 19:11:32 +0200 | [diff] [blame] | 3751 | #endif |
| 3752 | |
Ronald Cron | 73fe8df | 2022-10-05 14:31:43 +0200 | [diff] [blame] | 3753 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED) |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 3754 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 3755 | if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) ) |
| 3756 | { |
| 3757 | /* The maintenance of the external PSK key slot is the |
| 3758 | * user's responsibility. */ |
| 3759 | if( ssl->handshake->psk_opaque_is_internal ) |
| 3760 | { |
| 3761 | psa_destroy_key( ssl->handshake->psk_opaque ); |
| 3762 | ssl->handshake->psk_opaque_is_internal = 0; |
| 3763 | } |
| 3764 | ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT; |
| 3765 | } |
Neil Armstrong | e952a30 | 2022-05-03 10:22:14 +0200 | [diff] [blame] | 3766 | #else |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 3767 | if( handshake->psk != NULL ) |
| 3768 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 3769 | mbedtls_platform_zeroize( handshake->psk, handshake->psk_len ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 3770 | mbedtls_free( handshake->psk ); |
| 3771 | } |
Neil Armstrong | e952a30 | 2022-05-03 10:22:14 +0200 | [diff] [blame] | 3772 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Ronald Cron | 73fe8df | 2022-10-05 14:31:43 +0200 | [diff] [blame] | 3773 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */ |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 3774 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3775 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 3776 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Manuel Pégourié-Gonnard | 8372454 | 2013-09-24 22:30:56 +0200 | [diff] [blame] | 3777 | /* |
| 3778 | * Free only the linked list wrapper, not the keys themselves |
| 3779 | * since the belong to the SNI callback |
| 3780 | */ |
Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 3781 | ssl_key_cert_free( handshake->sni_key_cert ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3782 | #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] | 3783 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3784 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Manuel Pégourié-Gonnard | 6b7301c | 2017-08-15 12:08:45 +0200 | [diff] [blame] | 3785 | mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx ); |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 3786 | if( handshake->ecrs_peer_cert != NULL ) |
| 3787 | { |
| 3788 | mbedtls_x509_crt_free( handshake->ecrs_peer_cert ); |
| 3789 | mbedtls_free( handshake->ecrs_peer_cert ); |
| 3790 | } |
Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 3791 | #endif |
| 3792 | |
Hanno Becker | 7517312 | 2019-02-06 16:18:31 +0000 | [diff] [blame] | 3793 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 3794 | !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 3795 | mbedtls_pk_free( &handshake->peer_pubkey ); |
| 3796 | #endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 3797 | |
XiaokangQian | 9b93c0d | 2022-02-09 06:02:25 +0000 | [diff] [blame] | 3798 | #if defined(MBEDTLS_SSL_CLI_C) && \ |
| 3799 | ( defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3) ) |
| 3800 | mbedtls_free( handshake->cookie ); |
| 3801 | #endif /* MBEDTLS_SSL_CLI_C && |
| 3802 | ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */ |
XiaokangQian | 8499b6c | 2022-01-27 09:00:11 +0000 | [diff] [blame] | 3803 | |
| 3804 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 533ab5f | 2020-02-05 10:49:13 +0000 | [diff] [blame] | 3805 | mbedtls_ssl_flight_free( handshake->flight ); |
| 3806 | mbedtls_ssl_buffering_free( ssl ); |
XiaokangQian | 8499b6c | 2022-01-27 09:00:11 +0000 | [diff] [blame] | 3807 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 3808 | |
Ronald Cron | f12b81d | 2022-03-15 10:42:41 +0100 | [diff] [blame] | 3809 | #if defined(MBEDTLS_ECDH_C) && \ |
| 3810 | ( defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3) ) |
Neil Armstrong | f716a70 | 2022-04-04 11:23:46 +0200 | [diff] [blame] | 3811 | if( handshake->ecdh_psa_privkey_is_external == 0 ) |
Neil Armstrong | 8113d25 | 2022-03-23 10:57:04 +0100 | [diff] [blame] | 3812 | psa_destroy_key( handshake->ecdh_psa_privkey ); |
Hanno Becker | 4a63ed4 | 2019-01-08 11:39:35 +0000 | [diff] [blame] | 3813 | #endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */ |
| 3814 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3815 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | a1a568c | 2021-11-09 10:17:21 +0800 | [diff] [blame] | 3816 | mbedtls_ssl_transform_free( handshake->transform_handshake ); |
| 3817 | mbedtls_ssl_transform_free( handshake->transform_earlydata ); |
Jerry Yu | ba9c727 | 2021-10-30 11:54:10 +0800 | [diff] [blame] | 3818 | mbedtls_free( handshake->transform_earlydata ); |
| 3819 | mbedtls_free( handshake->transform_handshake ); |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3820 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Jerry Yu | ba9c727 | 2021-10-30 11:54:10 +0800 | [diff] [blame] | 3821 | |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 3822 | |
| 3823 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 3824 | /* If the buffers are too big - reallocate. Because of the way Mbed TLS |
| 3825 | * processes datagrams and the fact that a datagram is allowed to have |
| 3826 | * several records in it, it is possible that the I/O buffers are not |
| 3827 | * empty at this stage */ |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 3828 | handle_buffer_resizing( ssl, 1, mbedtls_ssl_get_input_buflen( ssl ), |
| 3829 | mbedtls_ssl_get_output_buflen( ssl ) ); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 3830 | #endif |
Hanno Becker | 3aa186f | 2021-08-10 09:24:19 +0100 | [diff] [blame] | 3831 | |
Jerry Yu | ba9c727 | 2021-10-30 11:54:10 +0800 | [diff] [blame] | 3832 | /* mbedtls_platform_zeroize MUST be last one in this function */ |
| 3833 | mbedtls_platform_zeroize( handshake, |
| 3834 | sizeof( mbedtls_ssl_handshake_params ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3835 | } |
| 3836 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3837 | void mbedtls_ssl_session_free( mbedtls_ssl_session *session ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3838 | { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3839 | if( session == NULL ) |
| 3840 | return; |
| 3841 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3842 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Hanno Becker | 1294a0b | 2019-02-05 12:38:15 +0000 | [diff] [blame] | 3843 | ssl_clear_peer_cert( session ); |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 3844 | #endif |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 3845 | |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 3846 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
Xiaokang Qian | bc663a0 | 2022-10-09 11:14:39 +0000 | [diff] [blame] | 3847 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \ |
| 3848 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Xiaokang Qian | 281fd1b | 2022-09-20 11:35:41 +0000 | [diff] [blame] | 3849 | mbedtls_free( session->hostname ); |
| 3850 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3851 | mbedtls_free( session->ticket ); |
Paul Bakker | a503a63 | 2013-08-14 13:48:06 +0200 | [diff] [blame] | 3852 | #endif |
Manuel Pégourié-Gonnard | 75d4401 | 2013-08-02 14:44:04 +0200 | [diff] [blame] | 3853 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 3854 | mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3855 | } |
| 3856 | |
Manuel Pégourié-Gonnard | 5c0e377 | 2019-07-23 16:13:17 +0200 | [diff] [blame] | 3857 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 3858 | |
| 3859 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 3860 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u |
| 3861 | #else |
| 3862 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u |
| 3863 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 3864 | |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 3865 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 3866 | |
| 3867 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 3868 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u |
| 3869 | #else |
| 3870 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u |
| 3871 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
| 3872 | |
| 3873 | #if defined(MBEDTLS_SSL_ALPN) |
| 3874 | #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u |
| 3875 | #else |
| 3876 | #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u |
| 3877 | #endif /* MBEDTLS_SSL_ALPN */ |
| 3878 | |
| 3879 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0 |
| 3880 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1 |
| 3881 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2 |
| 3882 | #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3 |
| 3883 | |
| 3884 | #define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \ |
| 3885 | ( (uint32_t) ( \ |
| 3886 | ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \ |
| 3887 | ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \ |
| 3888 | ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \ |
| 3889 | ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \ |
| 3890 | 0u ) ) |
| 3891 | |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3892 | static unsigned char ssl_serialized_context_header[] = { |
| 3893 | MBEDTLS_VERSION_MAJOR, |
| 3894 | MBEDTLS_VERSION_MINOR, |
| 3895 | MBEDTLS_VERSION_PATCH, |
Joe Subbiani | 2194dc4 | 2021-07-14 12:31:31 +0100 | [diff] [blame] | 3896 | MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ), |
| 3897 | MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ), |
| 3898 | MBEDTLS_BYTE_2( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ), |
| 3899 | MBEDTLS_BYTE_1( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ), |
| 3900 | MBEDTLS_BYTE_0( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ), |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3901 | }; |
| 3902 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3903 | /* |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 3904 | * Serialize a full SSL context |
Manuel Pégourié-Gonnard | 00400c2 | 2019-07-10 14:58:45 +0200 | [diff] [blame] | 3905 | * |
| 3906 | * The format of the serialized data is: |
| 3907 | * (in the presentation language of TLS, RFC 8446 section 3) |
| 3908 | * |
| 3909 | * // header |
| 3910 | * opaque mbedtls_version[3]; // major, minor, patch |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3911 | * opaque context_format[5]; // version-specific field determining |
Manuel Pégourié-Gonnard | 00400c2 | 2019-07-10 14:58:45 +0200 | [diff] [blame] | 3912 | * // the format of the remaining |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3913 | * // serialized data. |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 3914 | * Note: When updating the format, remember to keep these |
| 3915 | * 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] | 3916 | * |
| 3917 | * // session sub-structure |
| 3918 | * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save() |
| 3919 | * // transform sub-structure |
| 3920 | * uint8 random[64]; // ServerHello.random+ClientHello.random |
| 3921 | * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value |
| 3922 | * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use |
| 3923 | * // fields from ssl_context |
| 3924 | * uint32 badmac_seen; // DTLS: number of records with failing MAC |
| 3925 | * uint64 in_window_top; // DTLS: last validated record seq_num |
| 3926 | * uint64 in_window; // DTLS: bitmask for replay protection |
| 3927 | * uint8 disable_datagram_packing; // DTLS: only one record per datagram |
| 3928 | * uint64 cur_out_ctr; // Record layer: outgoing sequence number |
| 3929 | * uint16 mtu; // DTLS: path mtu (max outgoing fragment size) |
| 3930 | * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol |
| 3931 | * |
| 3932 | * Note that many fields of the ssl_context or sub-structures are not |
| 3933 | * serialized, as they fall in one of the following categories: |
| 3934 | * |
| 3935 | * 1. forced value (eg in_left must be 0) |
| 3936 | * 2. pointer to dynamically-allocated memory (eg session, transform) |
| 3937 | * 3. value can be re-derived from other data (eg session keys from MS) |
| 3938 | * 4. value was temporary (eg content of input buffer) |
| 3939 | * 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] | 3940 | */ |
| 3941 | int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl, |
| 3942 | unsigned char *buf, |
| 3943 | size_t buf_len, |
| 3944 | size_t *olen ) |
| 3945 | { |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3946 | unsigned char *p = buf; |
| 3947 | size_t used = 0; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3948 | size_t session_len; |
| 3949 | int ret = 0; |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3950 | |
Manuel Pégourié-Gonnard | 1aaf669 | 2019-07-10 14:14:05 +0200 | [diff] [blame] | 3951 | /* |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3952 | * Enforce usage restrictions, see "return BAD_INPUT_DATA" in |
| 3953 | * this function's documentation. |
| 3954 | * |
| 3955 | * These are due to assumptions/limitations in the implementation. Some of |
| 3956 | * them are likely to stay (no handshake in progress) some might go away |
| 3957 | * (only DTLS) but are currently used to simplify the implementation. |
Manuel Pégourié-Gonnard | 1aaf669 | 2019-07-10 14:14:05 +0200 | [diff] [blame] | 3958 | */ |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3959 | /* The initial handshake must be over */ |
Paul Elliott | 27b0d94 | 2022-03-18 21:55:32 +0000 | [diff] [blame] | 3960 | if( mbedtls_ssl_is_handshake_over( ssl ) == 0 ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3961 | { |
| 3962 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Initial handshake isn't over" ) ); |
Manuel Pégourié-Gonnard | 1aaf669 | 2019-07-10 14:14:05 +0200 | [diff] [blame] | 3963 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3964 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3965 | if( ssl->handshake != NULL ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3966 | { |
| 3967 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Handshake isn't completed" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3968 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3969 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3970 | /* Double-check that sub-structures are indeed ready */ |
| 3971 | if( ssl->transform == NULL || ssl->session == NULL ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3972 | { |
| 3973 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Serialised structures aren't ready" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3974 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3975 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3976 | /* There must be no pending incoming or outgoing data */ |
| 3977 | if( mbedtls_ssl_check_pending( ssl ) != 0 ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3978 | { |
| 3979 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending incoming data" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3980 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3981 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3982 | if( ssl->out_left != 0 ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3983 | { |
| 3984 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending outgoing data" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3985 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3986 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3987 | /* Protocol must be DLTS, not TLS */ |
| 3988 | if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3989 | { |
| 3990 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only DTLS is supported" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3991 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3992 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3993 | /* Version must be 1.2 */ |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 3994 | if( ssl->tls_version != MBEDTLS_SSL_VERSION_TLS1_2 ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3995 | { |
| 3996 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only version 1.2 supported" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3997 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3998 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3999 | /* We must be using an AEAD ciphersuite */ |
| 4000 | if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 4001 | { |
| 4002 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only AEAD ciphersuites supported" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 4003 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 4004 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 4005 | /* Renegotiation must not be enabled */ |
| 4006 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 4007 | if( ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 4008 | { |
| 4009 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Renegotiation must not be enabled" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 4010 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 4011 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 4012 | #endif |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 4013 | |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4014 | /* |
| 4015 | * Version and format identifier |
| 4016 | */ |
| 4017 | used += sizeof( ssl_serialized_context_header ); |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 4018 | |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4019 | if( used <= buf_len ) |
| 4020 | { |
| 4021 | memcpy( p, ssl_serialized_context_header, |
| 4022 | sizeof( ssl_serialized_context_header ) ); |
| 4023 | p += sizeof( ssl_serialized_context_header ); |
| 4024 | } |
| 4025 | |
| 4026 | /* |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4027 | * Session (length + data) |
| 4028 | */ |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 4029 | 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] | 4030 | if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ) |
| 4031 | return( ret ); |
| 4032 | |
| 4033 | used += 4 + session_len; |
| 4034 | if( used <= buf_len ) |
| 4035 | { |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 4036 | MBEDTLS_PUT_UINT32_BE( session_len, p, 0 ); |
| 4037 | p += 4; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4038 | |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 4039 | ret = ssl_session_save( ssl->session, 1, |
| 4040 | p, session_len, &session_len ); |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4041 | if( ret != 0 ) |
| 4042 | return( ret ); |
| 4043 | |
| 4044 | p += session_len; |
| 4045 | } |
| 4046 | |
| 4047 | /* |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4048 | * Transform |
| 4049 | */ |
| 4050 | used += sizeof( ssl->transform->randbytes ); |
| 4051 | if( used <= buf_len ) |
| 4052 | { |
| 4053 | memcpy( p, ssl->transform->randbytes, |
| 4054 | sizeof( ssl->transform->randbytes ) ); |
| 4055 | p += sizeof( ssl->transform->randbytes ); |
| 4056 | } |
| 4057 | |
| 4058 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 4059 | used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len; |
| 4060 | if( used <= buf_len ) |
| 4061 | { |
| 4062 | *p++ = ssl->transform->in_cid_len; |
| 4063 | memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len ); |
| 4064 | p += ssl->transform->in_cid_len; |
| 4065 | |
| 4066 | *p++ = ssl->transform->out_cid_len; |
| 4067 | memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len ); |
| 4068 | p += ssl->transform->out_cid_len; |
| 4069 | } |
| 4070 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 4071 | |
| 4072 | /* |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4073 | * Saved fields from top-level ssl_context structure |
| 4074 | */ |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4075 | used += 4; |
| 4076 | if( used <= buf_len ) |
| 4077 | { |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 4078 | MBEDTLS_PUT_UINT32_BE( ssl->badmac_seen, p, 0 ); |
| 4079 | p += 4; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4080 | } |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4081 | |
| 4082 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 4083 | used += 16; |
| 4084 | if( used <= buf_len ) |
| 4085 | { |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 4086 | MBEDTLS_PUT_UINT64_BE( ssl->in_window_top, p, 0 ); |
| 4087 | p += 8; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4088 | |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 4089 | MBEDTLS_PUT_UINT64_BE( ssl->in_window, p, 0 ); |
| 4090 | p += 8; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4091 | } |
| 4092 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
| 4093 | |
| 4094 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4095 | used += 1; |
| 4096 | if( used <= buf_len ) |
| 4097 | { |
| 4098 | *p++ = ssl->disable_datagram_packing; |
| 4099 | } |
| 4100 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 4101 | |
Jerry Yu | ae0b2e2 | 2021-10-08 15:21:19 +0800 | [diff] [blame] | 4102 | used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4103 | if( used <= buf_len ) |
| 4104 | { |
Jerry Yu | ae0b2e2 | 2021-10-08 15:21:19 +0800 | [diff] [blame] | 4105 | memcpy( p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN ); |
| 4106 | p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4107 | } |
| 4108 | |
| 4109 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4110 | used += 2; |
| 4111 | if( used <= buf_len ) |
| 4112 | { |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 4113 | MBEDTLS_PUT_UINT16_BE( ssl->mtu, p, 0 ); |
| 4114 | p += 2; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4115 | } |
| 4116 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 4117 | |
| 4118 | #if defined(MBEDTLS_SSL_ALPN) |
| 4119 | { |
| 4120 | const uint8_t alpn_len = ssl->alpn_chosen |
Manuel Pégourié-Gonnard | f041f4e | 2019-07-24 00:58:27 +0200 | [diff] [blame] | 4121 | ? (uint8_t) strlen( ssl->alpn_chosen ) |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4122 | : 0; |
| 4123 | |
| 4124 | used += 1 + alpn_len; |
| 4125 | if( used <= buf_len ) |
| 4126 | { |
| 4127 | *p++ = alpn_len; |
| 4128 | |
| 4129 | if( ssl->alpn_chosen != NULL ) |
| 4130 | { |
| 4131 | memcpy( p, ssl->alpn_chosen, alpn_len ); |
| 4132 | p += alpn_len; |
| 4133 | } |
| 4134 | } |
| 4135 | } |
| 4136 | #endif /* MBEDTLS_SSL_ALPN */ |
| 4137 | |
| 4138 | /* |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4139 | * Done |
| 4140 | */ |
| 4141 | *olen = used; |
| 4142 | |
| 4143 | if( used > buf_len ) |
| 4144 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 4145 | |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4146 | MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used ); |
| 4147 | |
Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 4148 | return( mbedtls_ssl_session_reset_int( ssl, 0 ) ); |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 4149 | } |
| 4150 | |
| 4151 | /* |
Manuel Pégourié-Gonnard | b9dfc9f | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 4152 | * Deserialize context, see mbedtls_ssl_context_save() for format. |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4153 | * |
| 4154 | * This internal version is wrapped by a public function that cleans up in |
| 4155 | * case of error. |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 4156 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 4157 | MBEDTLS_CHECK_RETURN_CRITICAL |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4158 | static int ssl_context_load( mbedtls_ssl_context *ssl, |
| 4159 | const unsigned char *buf, |
| 4160 | size_t len ) |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 4161 | { |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4162 | const unsigned char *p = buf; |
| 4163 | const unsigned char * const end = buf + len; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4164 | size_t session_len; |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 4165 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Andrzej Kurek | 2d59dbc | 2022-10-13 08:34:38 -0400 | [diff] [blame] | 4166 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Andrzej Kurek | 894edde | 2022-09-29 06:31:14 -0400 | [diff] [blame] | 4167 | tls_prf_fn prf_func = NULL; |
Andrzej Kurek | 2d59dbc | 2022-10-13 08:34:38 -0400 | [diff] [blame] | 4168 | #endif |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4169 | |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 4170 | /* |
| 4171 | * The context should have been freshly setup or reset. |
| 4172 | * Give the user an error in case of obvious misuse. |
Manuel Pégourié-Gonnard | 4ca930f | 2019-07-26 16:31:53 +0200 | [diff] [blame] | 4173 | * (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] | 4174 | * renegotiating, or if the user mistakenly loaded a session first.) |
| 4175 | */ |
| 4176 | if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST || |
| 4177 | ssl->session != NULL ) |
| 4178 | { |
| 4179 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4180 | } |
| 4181 | |
| 4182 | /* |
| 4183 | * We can't check that the config matches the initial one, but we can at |
| 4184 | * least check it matches the requirements for serializing. |
| 4185 | */ |
| 4186 | if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM || |
Glenn Strauss | 2dfcea2 | 2022-03-14 17:26:42 -0400 | [diff] [blame] | 4187 | ssl->conf->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2 || |
| 4188 | ssl->conf->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 || |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 4189 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | 9a96fd7 | 2019-07-23 17:11:24 +0200 | [diff] [blame] | 4190 | ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED || |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 4191 | #endif |
Manuel Pégourié-Gonnard | 9a96fd7 | 2019-07-23 17:11:24 +0200 | [diff] [blame] | 4192 | 0 ) |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 4193 | { |
| 4194 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4195 | } |
| 4196 | |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4197 | MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len ); |
| 4198 | |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4199 | /* |
| 4200 | * Check version identifier |
| 4201 | */ |
| 4202 | if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) ) |
| 4203 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4204 | |
| 4205 | if( memcmp( p, ssl_serialized_context_header, |
| 4206 | sizeof( ssl_serialized_context_header ) ) != 0 ) |
| 4207 | { |
| 4208 | return( MBEDTLS_ERR_SSL_VERSION_MISMATCH ); |
| 4209 | } |
| 4210 | p += sizeof( ssl_serialized_context_header ); |
| 4211 | |
| 4212 | /* |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4213 | * Session |
| 4214 | */ |
| 4215 | if( (size_t)( end - p ) < 4 ) |
| 4216 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4217 | |
| 4218 | session_len = ( (size_t) p[0] << 24 ) | |
| 4219 | ( (size_t) p[1] << 16 ) | |
| 4220 | ( (size_t) p[2] << 8 ) | |
| 4221 | ( (size_t) p[3] ); |
| 4222 | p += 4; |
| 4223 | |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 4224 | /* This has been allocated by ssl_handshake_init(), called by |
Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 4225 | * 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] | 4226 | ssl->session = ssl->session_negotiate; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4227 | ssl->session_in = ssl->session; |
| 4228 | ssl->session_out = ssl->session; |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 4229 | ssl->session_negotiate = NULL; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4230 | |
| 4231 | if( (size_t)( end - p ) < session_len ) |
| 4232 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4233 | |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 4234 | ret = ssl_session_load( ssl->session, 1, p, session_len ); |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4235 | if( ret != 0 ) |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 4236 | { |
| 4237 | mbedtls_ssl_session_free( ssl->session ); |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4238 | return( ret ); |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 4239 | } |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4240 | |
| 4241 | p += session_len; |
| 4242 | |
| 4243 | /* |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4244 | * Transform |
| 4245 | */ |
| 4246 | |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 4247 | /* This has been allocated by ssl_handshake_init(), called by |
Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 4248 | * 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] | 4249 | ssl->transform = ssl->transform_negotiate; |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4250 | ssl->transform_in = ssl->transform; |
| 4251 | ssl->transform_out = ssl->transform; |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 4252 | ssl->transform_negotiate = NULL; |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4253 | |
Andrzej Kurek | 2d59dbc | 2022-10-13 08:34:38 -0400 | [diff] [blame] | 4254 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Andrzej Kurek | 894edde | 2022-09-29 06:31:14 -0400 | [diff] [blame] | 4255 | prf_func = ssl_tls12prf_from_cs( ssl->session->ciphersuite ); |
| 4256 | if( prf_func == NULL ) |
| 4257 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4258 | |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4259 | /* Read random bytes and populate structure */ |
| 4260 | if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) ) |
| 4261 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Andrzej Kurek | 2d59dbc | 2022-10-13 08:34:38 -0400 | [diff] [blame] | 4262 | |
Hanno Becker | bd25755 | 2021-03-22 06:59:27 +0000 | [diff] [blame] | 4263 | ret = ssl_tls12_populate_transform( ssl->transform, |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4264 | ssl->session->ciphersuite, |
| 4265 | ssl->session->master, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 4266 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4267 | ssl->session->encrypt_then_mac, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 4268 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Andrzej Kurek | 894edde | 2022-09-29 06:31:14 -0400 | [diff] [blame] | 4269 | prf_func, |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4270 | p, /* currently pointing to randbytes */ |
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 4271 | 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] | 4272 | ssl->conf->endpoint, |
| 4273 | ssl ); |
| 4274 | if( ret != 0 ) |
| 4275 | return( ret ); |
Jerry Yu | 840fbb2 | 2022-02-17 14:59:29 +0800 | [diff] [blame] | 4276 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4277 | p += sizeof( ssl->transform->randbytes ); |
| 4278 | |
| 4279 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 4280 | /* Read connection IDs and store them */ |
| 4281 | if( (size_t)( end - p ) < 1 ) |
| 4282 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4283 | |
| 4284 | ssl->transform->in_cid_len = *p++; |
| 4285 | |
Manuel Pégourié-Gonnard | 5ea13b8 | 2019-07-23 15:02:54 +0200 | [diff] [blame] | 4286 | 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] | 4287 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4288 | |
| 4289 | memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len ); |
| 4290 | p += ssl->transform->in_cid_len; |
| 4291 | |
| 4292 | ssl->transform->out_cid_len = *p++; |
| 4293 | |
| 4294 | if( (size_t)( end - p ) < ssl->transform->out_cid_len ) |
| 4295 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4296 | |
| 4297 | memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len ); |
| 4298 | p += ssl->transform->out_cid_len; |
| 4299 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 4300 | |
| 4301 | /* |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4302 | * Saved fields from top-level ssl_context structure |
| 4303 | */ |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4304 | if( (size_t)( end - p ) < 4 ) |
| 4305 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4306 | |
| 4307 | ssl->badmac_seen = ( (uint32_t) p[0] << 24 ) | |
| 4308 | ( (uint32_t) p[1] << 16 ) | |
| 4309 | ( (uint32_t) p[2] << 8 ) | |
| 4310 | ( (uint32_t) p[3] ); |
| 4311 | p += 4; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4312 | |
| 4313 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 4314 | if( (size_t)( end - p ) < 16 ) |
| 4315 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4316 | |
| 4317 | ssl->in_window_top = ( (uint64_t) p[0] << 56 ) | |
| 4318 | ( (uint64_t) p[1] << 48 ) | |
| 4319 | ( (uint64_t) p[2] << 40 ) | |
| 4320 | ( (uint64_t) p[3] << 32 ) | |
| 4321 | ( (uint64_t) p[4] << 24 ) | |
| 4322 | ( (uint64_t) p[5] << 16 ) | |
| 4323 | ( (uint64_t) p[6] << 8 ) | |
| 4324 | ( (uint64_t) p[7] ); |
| 4325 | p += 8; |
| 4326 | |
| 4327 | ssl->in_window = ( (uint64_t) p[0] << 56 ) | |
| 4328 | ( (uint64_t) p[1] << 48 ) | |
| 4329 | ( (uint64_t) p[2] << 40 ) | |
| 4330 | ( (uint64_t) p[3] << 32 ) | |
| 4331 | ( (uint64_t) p[4] << 24 ) | |
| 4332 | ( (uint64_t) p[5] << 16 ) | |
| 4333 | ( (uint64_t) p[6] << 8 ) | |
| 4334 | ( (uint64_t) p[7] ); |
| 4335 | p += 8; |
| 4336 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
| 4337 | |
| 4338 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4339 | if( (size_t)( end - p ) < 1 ) |
| 4340 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4341 | |
| 4342 | ssl->disable_datagram_packing = *p++; |
| 4343 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 4344 | |
Jerry Yu | d9a94fe | 2021-09-28 18:58:59 +0800 | [diff] [blame] | 4345 | if( (size_t)( end - p ) < sizeof( ssl->cur_out_ctr ) ) |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4346 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jerry Yu | d9a94fe | 2021-09-28 18:58:59 +0800 | [diff] [blame] | 4347 | memcpy( ssl->cur_out_ctr, p, sizeof( ssl->cur_out_ctr ) ); |
| 4348 | p += sizeof( ssl->cur_out_ctr ); |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4349 | |
| 4350 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4351 | if( (size_t)( end - p ) < 2 ) |
| 4352 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4353 | |
| 4354 | ssl->mtu = ( p[0] << 8 ) | p[1]; |
| 4355 | p += 2; |
| 4356 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 4357 | |
| 4358 | #if defined(MBEDTLS_SSL_ALPN) |
| 4359 | { |
| 4360 | uint8_t alpn_len; |
| 4361 | const char **cur; |
| 4362 | |
| 4363 | if( (size_t)( end - p ) < 1 ) |
| 4364 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4365 | |
| 4366 | alpn_len = *p++; |
| 4367 | |
| 4368 | if( alpn_len != 0 && ssl->conf->alpn_list != NULL ) |
| 4369 | { |
| 4370 | /* alpn_chosen should point to an item in the configured list */ |
| 4371 | for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ ) |
| 4372 | { |
| 4373 | if( strlen( *cur ) == alpn_len && |
| 4374 | memcmp( p, cur, alpn_len ) == 0 ) |
| 4375 | { |
| 4376 | ssl->alpn_chosen = *cur; |
| 4377 | break; |
| 4378 | } |
| 4379 | } |
| 4380 | } |
| 4381 | |
| 4382 | /* can only happen on conf mismatch */ |
| 4383 | if( alpn_len != 0 && ssl->alpn_chosen == NULL ) |
| 4384 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4385 | |
| 4386 | p += alpn_len; |
| 4387 | } |
| 4388 | #endif /* MBEDTLS_SSL_ALPN */ |
| 4389 | |
| 4390 | /* |
Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 4391 | * Forced fields from top-level ssl_context structure |
| 4392 | * |
| 4393 | * Most of them already set to the correct value by mbedtls_ssl_init() and |
| 4394 | * mbedtls_ssl_reset(), so we only need to set the remaining ones. |
| 4395 | */ |
| 4396 | ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER; |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 4397 | ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 4398 | |
Hanno Becker | 361b10d | 2019-08-30 10:42:49 +0100 | [diff] [blame] | 4399 | /* Adjust pointers for header fields of outgoing records to |
| 4400 | * the given transform, accounting for explicit IV and CID. */ |
Hanno Becker | 3e6f8ab | 2020-02-05 10:40:57 +0000 | [diff] [blame] | 4401 | mbedtls_ssl_update_out_pointers( ssl, ssl->transform ); |
Hanno Becker | 361b10d | 2019-08-30 10:42:49 +0100 | [diff] [blame] | 4402 | |
Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 4403 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4404 | ssl->in_epoch = 1; |
| 4405 | #endif |
| 4406 | |
| 4407 | /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated, |
| 4408 | * 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] | 4409 | * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform() |
| 4410 | * inappropriately. */ |
Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 4411 | if( ssl->handshake != NULL ) |
| 4412 | { |
| 4413 | mbedtls_ssl_handshake_free( ssl ); |
| 4414 | mbedtls_free( ssl->handshake ); |
| 4415 | ssl->handshake = NULL; |
| 4416 | } |
| 4417 | |
| 4418 | /* |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4419 | * Done - should have consumed entire buffer |
| 4420 | */ |
| 4421 | if( p != end ) |
| 4422 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 4423 | |
| 4424 | return( 0 ); |
| 4425 | } |
| 4426 | |
| 4427 | /* |
Manuel Pégourié-Gonnard | b9dfc9f | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 4428 | * Deserialize context: public wrapper for error cleaning |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4429 | */ |
| 4430 | int mbedtls_ssl_context_load( mbedtls_ssl_context *context, |
| 4431 | const unsigned char *buf, |
| 4432 | size_t len ) |
| 4433 | { |
| 4434 | int ret = ssl_context_load( context, buf, len ); |
| 4435 | |
| 4436 | if( ret != 0 ) |
| 4437 | mbedtls_ssl_free( context ); |
| 4438 | |
| 4439 | return( ret ); |
| 4440 | } |
Manuel Pégourié-Gonnard | 5c0e377 | 2019-07-23 16:13:17 +0200 | [diff] [blame] | 4441 | #endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */ |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4442 | |
| 4443 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4444 | * Free an SSL context |
| 4445 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4446 | void mbedtls_ssl_free( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4447 | { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 4448 | if( ssl == NULL ) |
| 4449 | return; |
| 4450 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4451 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4452 | |
Manuel Pégourié-Gonnard | 7ee6f0e | 2014-02-13 10:54:07 +0100 | [diff] [blame] | 4453 | if( ssl->out_buf != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4454 | { |
sander-visser | b8aa207 | 2020-05-06 22:05:13 +0200 | [diff] [blame] | 4455 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 4456 | size_t out_buf_len = ssl->out_buf_len; |
| 4457 | #else |
| 4458 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; |
| 4459 | #endif |
| 4460 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 4461 | mbedtls_platform_zeroize( ssl->out_buf, out_buf_len ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4462 | mbedtls_free( ssl->out_buf ); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 4463 | ssl->out_buf = NULL; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4464 | } |
| 4465 | |
Manuel Pégourié-Gonnard | 7ee6f0e | 2014-02-13 10:54:07 +0100 | [diff] [blame] | 4466 | if( ssl->in_buf != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4467 | { |
sander-visser | b8aa207 | 2020-05-06 22:05:13 +0200 | [diff] [blame] | 4468 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 4469 | size_t in_buf_len = ssl->in_buf_len; |
| 4470 | #else |
| 4471 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; |
| 4472 | #endif |
| 4473 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 4474 | mbedtls_platform_zeroize( ssl->in_buf, in_buf_len ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4475 | mbedtls_free( ssl->in_buf ); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 4476 | ssl->in_buf = NULL; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4477 | } |
| 4478 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4479 | if( ssl->transform ) |
| 4480 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4481 | mbedtls_ssl_transform_free( ssl->transform ); |
| 4482 | mbedtls_free( ssl->transform ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4483 | } |
| 4484 | |
| 4485 | if( ssl->handshake ) |
| 4486 | { |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 4487 | mbedtls_ssl_handshake_free( ssl ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4488 | mbedtls_ssl_transform_free( ssl->transform_negotiate ); |
| 4489 | mbedtls_ssl_session_free( ssl->session_negotiate ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4490 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4491 | mbedtls_free( ssl->handshake ); |
| 4492 | mbedtls_free( ssl->transform_negotiate ); |
| 4493 | mbedtls_free( ssl->session_negotiate ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4494 | } |
| 4495 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4496 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Hanno Becker | 3aa186f | 2021-08-10 09:24:19 +0100 | [diff] [blame] | 4497 | mbedtls_ssl_transform_free( ssl->transform_application ); |
| 4498 | mbedtls_free( ssl->transform_application ); |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4499 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | 3aa186f | 2021-08-10 09:24:19 +0100 | [diff] [blame] | 4500 | |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 4501 | if( ssl->session ) |
| 4502 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4503 | mbedtls_ssl_session_free( ssl->session ); |
| 4504 | mbedtls_free( ssl->session ); |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 4505 | } |
| 4506 | |
Manuel Pégourié-Gonnard | 55fab2d | 2015-05-11 16:15:19 +0200 | [diff] [blame] | 4507 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 4508 | if( ssl->hostname != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4509 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 4510 | mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4511 | mbedtls_free( ssl->hostname ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4512 | } |
Paul Bakker | 0be444a | 2013-08-27 21:55:01 +0200 | [diff] [blame] | 4513 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4514 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 4515 | #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] | 4516 | mbedtls_free( ssl->cli_id ); |
Manuel Pégourié-Gonnard | 43c0218 | 2014-07-22 17:32:01 +0200 | [diff] [blame] | 4517 | #endif |
| 4518 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4519 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) ); |
Paul Bakker | 2da561c | 2009-02-05 18:00:28 +0000 | [diff] [blame] | 4520 | |
Paul Bakker | 86f04f4 | 2013-02-14 11:20:09 +0100 | [diff] [blame] | 4521 | /* Actually clear after last debug message */ |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 4522 | mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4523 | } |
| 4524 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4525 | /* |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 4526 | * Initialize mbedtls_ssl_config |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4527 | */ |
| 4528 | void mbedtls_ssl_config_init( mbedtls_ssl_config *conf ) |
| 4529 | { |
| 4530 | memset( conf, 0, sizeof( mbedtls_ssl_config ) ); |
| 4531 | } |
| 4532 | |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4533 | /* The selection should be the same as mbedtls_x509_crt_profile_default in |
| 4534 | * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters: |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4535 | * curves with a lower resource usage come first. |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4536 | * See the documentation of mbedtls_ssl_conf_curves() for what we promise |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4537 | * about this list. |
| 4538 | */ |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4539 | static uint16_t ssl_preset_default_groups[] = { |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4540 | #if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4541 | MBEDTLS_SSL_IANA_TLS_GROUP_X25519, |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4542 | #endif |
| 4543 | #if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4544 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1, |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4545 | #endif |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4546 | #if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4547 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4548 | #endif |
| 4549 | #if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4550 | MBEDTLS_SSL_IANA_TLS_GROUP_X448, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4551 | #endif |
| 4552 | #if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4553 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4554 | #endif |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4555 | #if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4556 | MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1, |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4557 | #endif |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4558 | #if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4559 | MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4560 | #endif |
| 4561 | #if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4562 | MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4563 | #endif |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4564 | MBEDTLS_SSL_IANA_TLS_GROUP_NONE |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4565 | }; |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4566 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4567 | static int ssl_preset_suiteb_ciphersuites[] = { |
| 4568 | MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, |
| 4569 | MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, |
| 4570 | 0 |
| 4571 | }; |
| 4572 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 4573 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Hanno Becker | 9c6aa7b | 2021-08-10 13:50:43 +0100 | [diff] [blame] | 4574 | |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4575 | /* NOTICE: |
Jerry Yu | 0b994b8 | 2022-01-25 17:22:12 +0800 | [diff] [blame] | 4576 | * 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] | 4577 | * rules SHOULD be upheld. |
| 4578 | * - No duplicate entries. |
| 4579 | * - 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] | 4580 | * - ssl_tls12_preset* is for TLS 1.2 use only. |
Jerry Yu | 370e146 | 2022-01-25 10:36:53 +0800 | [diff] [blame] | 4581 | * - 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] | 4582 | */ |
Hanno Becker | 9c6aa7b | 2021-08-10 13:50:43 +0100 | [diff] [blame] | 4583 | static uint16_t ssl_preset_default_sig_algs[] = { |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4584 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4585 | #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] | 4586 | defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) |
| 4587 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256, |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4588 | #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] | 4589 | MBEDTLS_ECP_DP_SECP256R1_ENABLED */ |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4590 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4591 | #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] | 4592 | defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) |
| 4593 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384, |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4594 | #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] | 4595 | MBEDTLS_ECP_DP_SECP384R1_ENABLED */ |
| 4596 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4597 | #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] | 4598 | defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) |
| 4599 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512, |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4600 | #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] | 4601 | MBEDTLS_ECP_DP_SECP521R1_ENABLED */ |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4602 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4603 | #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] | 4604 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512, |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4605 | #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] | 4606 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4607 | #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] | 4608 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384, |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4609 | #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] | 4610 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4611 | #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] | 4612 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256, |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4613 | #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] | 4614 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4615 | #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] | 4616 | MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512, |
| 4617 | #endif /* MBEDTLS_RSA_C && MBEDTLS_SHA512_C */ |
| 4618 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4619 | #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] | 4620 | MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384, |
| 4621 | #endif /* MBEDTLS_RSA_C && MBEDTLS_SHA384_C */ |
| 4622 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4623 | #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] | 4624 | MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256, |
| 4625 | #endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */ |
| 4626 | |
Gabor Mezei | 15b95a6 | 2022-05-09 16:37:58 +0200 | [diff] [blame] | 4627 | MBEDTLS_TLS_SIG_NONE |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4628 | }; |
| 4629 | |
| 4630 | /* NOTICE: see above */ |
| 4631 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 4632 | static uint16_t ssl_tls12_preset_default_sig_algs[] = { |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4633 | #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] | 4634 | #if defined(MBEDTLS_ECDSA_C) |
| 4635 | 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] | 4636 | #endif |
Jerry Yu | 09a99fc | 2022-07-28 14:22:17 +0800 | [diff] [blame] | 4637 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) |
| 4638 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512, |
| 4639 | #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */ |
Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 4640 | #if defined(MBEDTLS_RSA_C) |
| 4641 | MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA512 ), |
| 4642 | #endif |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4643 | #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] | 4644 | #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] | 4645 | #if defined(MBEDTLS_ECDSA_C) |
| 4646 | 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] | 4647 | #endif |
Jerry Yu | 09a99fc | 2022-07-28 14:22:17 +0800 | [diff] [blame] | 4648 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) |
| 4649 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384, |
| 4650 | #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */ |
Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 4651 | #if defined(MBEDTLS_RSA_C) |
| 4652 | MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ), |
| 4653 | #endif |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4654 | #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] | 4655 | #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] | 4656 | #if defined(MBEDTLS_ECDSA_C) |
| 4657 | 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] | 4658 | #endif |
Jerry Yu | 09a99fc | 2022-07-28 14:22:17 +0800 | [diff] [blame] | 4659 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) |
| 4660 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256, |
| 4661 | #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */ |
Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 4662 | #if defined(MBEDTLS_RSA_C) |
| 4663 | MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ), |
| 4664 | #endif |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4665 | #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] | 4666 | MBEDTLS_TLS_SIG_NONE |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4667 | }; |
| 4668 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 4669 | /* NOTICE: see above */ |
| 4670 | static uint16_t ssl_preset_suiteb_sig_algs[] = { |
| 4671 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4672 | #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] | 4673 | defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) |
| 4674 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256, |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4675 | #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] | 4676 | MBEDTLS_ECP_DP_SECP256R1_ENABLED */ |
| 4677 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4678 | #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] | 4679 | defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) |
| 4680 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384, |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4681 | #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] | 4682 | MBEDTLS_ECP_DP_SECP384R1_ENABLED */ |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4683 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4684 | #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] | 4685 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256, |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4686 | #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] | 4687 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4688 | #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] | 4689 | MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256, |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4690 | #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] | 4691 | |
Gabor Mezei | 15b95a6 | 2022-05-09 16:37:58 +0200 | [diff] [blame] | 4692 | MBEDTLS_TLS_SIG_NONE |
Jerry Yu | 713013f | 2022-01-17 18:16:35 +0800 | [diff] [blame] | 4693 | }; |
Jerry Yu | 6106fdc | 2022-01-12 16:36:14 +0800 | [diff] [blame] | 4694 | |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4695 | /* NOTICE: see above */ |
| 4696 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 4697 | static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = { |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4698 | #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] | 4699 | #if defined(MBEDTLS_ECDSA_C) |
| 4700 | 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] | 4701 | #endif |
Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 4702 | #if defined(MBEDTLS_RSA_C) |
| 4703 | MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ), |
| 4704 | #endif |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4705 | #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] | 4706 | #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] | 4707 | #if defined(MBEDTLS_ECDSA_C) |
| 4708 | 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] | 4709 | #endif |
Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 4710 | #if defined(MBEDTLS_RSA_C) |
| 4711 | MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ), |
| 4712 | #endif |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4713 | #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] | 4714 | MBEDTLS_TLS_SIG_NONE |
Hanno Becker | 9c6aa7b | 2021-08-10 13:50:43 +0100 | [diff] [blame] | 4715 | }; |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4716 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 4717 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 4718 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4719 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4720 | static uint16_t ssl_preset_suiteb_groups[] = { |
Jaeden Amero | d431104 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 4721 | #if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4722 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1, |
Jaeden Amero | d431104 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 4723 | #endif |
| 4724 | #if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4725 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1, |
Jaeden Amero | d431104 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 4726 | #endif |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4727 | MBEDTLS_SSL_IANA_TLS_GROUP_NONE |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4728 | }; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4729 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 4730 | #if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4731 | /* 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] | 4732 | * to make sure there are no duplicated signature algorithm entries. */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 4733 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 4734 | static int ssl_check_no_sig_alg_duplication( uint16_t * sig_algs ) |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4735 | { |
| 4736 | size_t i, j; |
| 4737 | int ret = 0; |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4738 | |
Gabor Mezei | 15b95a6 | 2022-05-09 16:37:58 +0200 | [diff] [blame] | 4739 | for( i = 0; sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ ) |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4740 | { |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 4741 | for( j = 0; j < i; j++ ) |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4742 | { |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 4743 | if( sig_algs[i] != sig_algs[j] ) |
| 4744 | continue; |
| 4745 | mbedtls_printf( " entry(%04x,%" MBEDTLS_PRINTF_SIZET |
| 4746 | ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n", |
| 4747 | sig_algs[i], j, i ); |
| 4748 | ret = -1; |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4749 | } |
| 4750 | } |
| 4751 | return( ret ); |
| 4752 | } |
| 4753 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 4754 | #endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4755 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4756 | /* |
Tillmann Karras | 588ad50 | 2015-09-25 04:27:22 +0200 | [diff] [blame] | 4757 | * Load default in mbedtls_ssl_config |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4758 | */ |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 4759 | int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4760 | int endpoint, int transport, int preset ) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4761 | { |
Manuel Pégourié-Gonnard | 8b431fb | 2015-05-11 12:54:52 +0200 | [diff] [blame] | 4762 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 4763 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 8b431fb | 2015-05-11 12:54:52 +0200 | [diff] [blame] | 4764 | #endif |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4765 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 4766 | #if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 4767 | if( ssl_check_no_sig_alg_duplication( ssl_preset_suiteb_sig_algs ) ) |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4768 | { |
| 4769 | mbedtls_printf( "ssl_preset_suiteb_sig_algs has duplicated entries\n" ); |
| 4770 | return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED ); |
| 4771 | } |
| 4772 | |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 4773 | if( ssl_check_no_sig_alg_duplication( ssl_preset_default_sig_algs ) ) |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4774 | { |
| 4775 | mbedtls_printf( "ssl_preset_default_sig_algs has duplicated entries\n" ); |
| 4776 | return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED ); |
| 4777 | } |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4778 | |
| 4779 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 4780 | 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] | 4781 | { |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4782 | mbedtls_printf( "ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n" ); |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4783 | return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED ); |
| 4784 | } |
| 4785 | |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 4786 | 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] | 4787 | { |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4788 | mbedtls_printf( "ssl_tls12_preset_default_sig_algs has duplicated entries\n" ); |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4789 | return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED ); |
| 4790 | } |
| 4791 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 4792 | #endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4793 | |
Manuel Pégourié-Gonnard | 0de074f | 2015-05-14 12:58:01 +0200 | [diff] [blame] | 4794 | /* Use the functions here so that they are covered in tests, |
| 4795 | * but otherwise access member directly for efficiency */ |
| 4796 | mbedtls_ssl_conf_endpoint( conf, endpoint ); |
| 4797 | mbedtls_ssl_conf_transport( conf, transport ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4798 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4799 | /* |
| 4800 | * Things that are common to all presets |
| 4801 | */ |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 4802 | #if defined(MBEDTLS_SSL_CLI_C) |
| 4803 | if( endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 4804 | { |
| 4805 | conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED; |
| 4806 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 4807 | conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED; |
| 4808 | #endif |
| 4809 | } |
| 4810 | #endif |
| 4811 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4812 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 4813 | conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED; |
| 4814 | #endif |
| 4815 | |
| 4816 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 4817 | conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED; |
| 4818 | #endif |
| 4819 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 4820 | #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] | 4821 | conf->f_cookie_write = ssl_cookie_write_dummy; |
| 4822 | conf->f_cookie_check = ssl_cookie_check_dummy; |
| 4823 | #endif |
| 4824 | |
| 4825 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 4826 | conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED; |
| 4827 | #endif |
| 4828 | |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 4829 | #if defined(MBEDTLS_SSL_SRV_C) |
| 4830 | conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED; |
TRodziewicz | 3946f79 | 2021-06-14 12:11:18 +0200 | [diff] [blame] | 4831 | conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER; |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 4832 | #endif |
| 4833 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4834 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4835 | conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN; |
| 4836 | conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX; |
| 4837 | #endif |
| 4838 | |
| 4839 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 4840 | conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT; |
Andres AG | 2196c7f | 2016-12-15 17:01:16 +0000 | [diff] [blame] | 4841 | memset( conf->renego_period, 0x00, 2 ); |
| 4842 | memset( conf->renego_period + 2, 0xFF, 6 ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4843 | #endif |
| 4844 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4845 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
Hanno Becker | e2defad | 2021-07-24 05:59:17 +0100 | [diff] [blame] | 4846 | if( endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 4847 | { |
| 4848 | const unsigned char dhm_p[] = |
| 4849 | MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN; |
| 4850 | const unsigned char dhm_g[] = |
| 4851 | MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN; |
Hanno Becker | 00d0a68 | 2017-10-04 13:14:29 +0100 | [diff] [blame] | 4852 | |
Hanno Becker | e2defad | 2021-07-24 05:59:17 +0100 | [diff] [blame] | 4853 | if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf, |
| 4854 | dhm_p, sizeof( dhm_p ), |
| 4855 | dhm_g, sizeof( dhm_g ) ) ) != 0 ) |
| 4856 | { |
| 4857 | return( ret ); |
| 4858 | } |
| 4859 | } |
Manuel Pégourié-Gonnard | bd990d6 | 2015-06-11 14:49:42 +0200 | [diff] [blame] | 4860 | #endif |
| 4861 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4862 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | d0766ec | 2022-09-22 10:46:57 +0800 | [diff] [blame] | 4863 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SESSION_TICKETS) |
Jerry Yu | 1ad7ace | 2022-08-09 13:28:39 +0800 | [diff] [blame] | 4864 | mbedtls_ssl_conf_new_session_tickets( |
| 4865 | conf, MBEDTLS_SSL_TLS1_3_DEFAULT_NEW_SESSION_TICKETS ); |
| 4866 | #endif |
Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 4867 | /* |
| 4868 | * Allow all TLS 1.3 key exchange modes by default. |
| 4869 | */ |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 4870 | conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL; |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4871 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 4872 | |
XiaokangQian | 4d3a604 | 2022-04-21 13:46:17 +0000 | [diff] [blame] | 4873 | if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 4874 | { |
Glenn Strauss | 2dfcea2 | 2022-03-14 17:26:42 -0400 | [diff] [blame] | 4875 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
XiaokangQian | 4d3a604 | 2022-04-21 13:46:17 +0000 | [diff] [blame] | 4876 | conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
XiaokangQian | 060d867 | 2022-04-21 09:24:56 +0000 | [diff] [blame] | 4877 | conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
XiaokangQian | 4d3a604 | 2022-04-21 13:46:17 +0000 | [diff] [blame] | 4878 | #else |
XiaokangQian | 060d867 | 2022-04-21 09:24:56 +0000 | [diff] [blame] | 4879 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
XiaokangQian | 060d867 | 2022-04-21 09:24:56 +0000 | [diff] [blame] | 4880 | #endif |
XiaokangQian | 4d3a604 | 2022-04-21 13:46:17 +0000 | [diff] [blame] | 4881 | } |
| 4882 | else |
| 4883 | { |
| 4884 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 4885 | if( endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 4886 | { |
| 4887 | conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 4888 | conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3; |
| 4889 | } |
| 4890 | else |
| 4891 | /* Hybrid TLS 1.2 / 1.3 is not supported on server side yet */ |
| 4892 | { |
| 4893 | conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 4894 | conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 4895 | } |
| 4896 | #elif defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 4897 | conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_3; |
| 4898 | conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3; |
| 4899 | #elif defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 4900 | conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 4901 | conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 4902 | #else |
| 4903 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 4904 | #endif |
| 4905 | } |
Glenn Strauss | 2dfcea2 | 2022-03-14 17:26:42 -0400 | [diff] [blame] | 4906 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4907 | /* |
| 4908 | * Preset-specific defaults |
| 4909 | */ |
| 4910 | switch( preset ) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4911 | { |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4912 | /* |
| 4913 | * NSA Suite B |
| 4914 | */ |
| 4915 | case MBEDTLS_SSL_PRESET_SUITEB: |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4916 | |
Hanno Becker | d60b6c6 | 2021-04-29 12:04:11 +0100 | [diff] [blame] | 4917 | conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4918 | |
| 4919 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 4920 | conf->cert_profile = &mbedtls_x509_crt_profile_suiteb; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4921 | #endif |
| 4922 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 4923 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4924 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 4925 | if( mbedtls_ssl_conf_is_tls12_only( conf ) ) |
| 4926 | conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs; |
| 4927 | else |
| 4928 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 4929 | conf->sig_algs = ssl_preset_suiteb_sig_algs; |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 4930 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4931 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4932 | #if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 4933 | conf->curve_list = NULL; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4934 | #endif |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4935 | conf->group_list = ssl_preset_suiteb_groups; |
Manuel Pégourié-Gonnard | c98204e | 2015-08-11 04:21:01 +0200 | [diff] [blame] | 4936 | break; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4937 | |
| 4938 | /* |
| 4939 | * Default |
| 4940 | */ |
| 4941 | default: |
Ronald Cron | f660655 | 2022-03-15 11:23:25 +0100 | [diff] [blame] | 4942 | |
Hanno Becker | d60b6c6 | 2021-04-29 12:04:11 +0100 | [diff] [blame] | 4943 | conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites(); |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4944 | |
| 4945 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 4946 | conf->cert_profile = &mbedtls_x509_crt_profile_default; |
| 4947 | #endif |
| 4948 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 4949 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4950 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 4951 | if( mbedtls_ssl_conf_is_tls12_only( conf ) ) |
| 4952 | conf->sig_algs = ssl_tls12_preset_default_sig_algs; |
| 4953 | else |
| 4954 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 4955 | conf->sig_algs = ssl_preset_default_sig_algs; |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 4956 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4957 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4958 | #if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 4959 | conf->curve_list = NULL; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4960 | #endif |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4961 | conf->group_list = ssl_preset_default_groups; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4962 | |
| 4963 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C) |
| 4964 | conf->dhm_min_bitlen = 1024; |
| 4965 | #endif |
| 4966 | } |
| 4967 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4968 | return( 0 ); |
| 4969 | } |
| 4970 | |
| 4971 | /* |
| 4972 | * Free mbedtls_ssl_config |
| 4973 | */ |
| 4974 | void mbedtls_ssl_config_free( mbedtls_ssl_config *conf ) |
| 4975 | { |
| 4976 | #if defined(MBEDTLS_DHM_C) |
| 4977 | mbedtls_mpi_free( &conf->dhm_P ); |
| 4978 | mbedtls_mpi_free( &conf->dhm_G ); |
| 4979 | #endif |
| 4980 | |
Ronald Cron | 73fe8df | 2022-10-05 14:31:43 +0200 | [diff] [blame] | 4981 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED) |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 4982 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 4983 | if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) ) |
| 4984 | { |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 4985 | conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT; |
| 4986 | } |
Neil Armstrong | 8ecd668 | 2022-05-05 11:40:35 +0200 | [diff] [blame] | 4987 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4988 | if( conf->psk != NULL ) |
| 4989 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 4990 | mbedtls_platform_zeroize( conf->psk, conf->psk_len ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4991 | mbedtls_free( conf->psk ); |
Azim Khan | 27e8a12 | 2018-03-21 14:24:11 +0000 | [diff] [blame] | 4992 | conf->psk = NULL; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4993 | conf->psk_len = 0; |
junyeonLEE | 316b162 | 2017-12-20 16:29:30 +0900 | [diff] [blame] | 4994 | } |
| 4995 | |
| 4996 | if( conf->psk_identity != NULL ) |
| 4997 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 4998 | mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len ); |
junyeonLEE | 316b162 | 2017-12-20 16:29:30 +0900 | [diff] [blame] | 4999 | mbedtls_free( conf->psk_identity ); |
Azim Khan | 27e8a12 | 2018-03-21 14:24:11 +0000 | [diff] [blame] | 5000 | conf->psk_identity = NULL; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5001 | conf->psk_identity_len = 0; |
| 5002 | } |
Ronald Cron | 73fe8df | 2022-10-05 14:31:43 +0200 | [diff] [blame] | 5003 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */ |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5004 | |
| 5005 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 5006 | ssl_key_cert_free( conf->key_cert ); |
| 5007 | #endif |
| 5008 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 5009 | mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5010 | } |
| 5011 | |
Manuel Pégourié-Gonnard | 5674a97 | 2015-10-19 15:14:03 +0200 | [diff] [blame] | 5012 | #if defined(MBEDTLS_PK_C) && \ |
| 5013 | ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) ) |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 5014 | /* |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5015 | * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 5016 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5017 | 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] | 5018 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5019 | #if defined(MBEDTLS_RSA_C) |
| 5020 | if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) ) |
| 5021 | return( MBEDTLS_SSL_SIG_RSA ); |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 5022 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5023 | #if defined(MBEDTLS_ECDSA_C) |
| 5024 | if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) ) |
| 5025 | return( MBEDTLS_SSL_SIG_ECDSA ); |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 5026 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5027 | return( MBEDTLS_SSL_SIG_ANON ); |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 5028 | } |
| 5029 | |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 5030 | unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type ) |
| 5031 | { |
| 5032 | switch( type ) { |
| 5033 | case MBEDTLS_PK_RSA: |
| 5034 | return( MBEDTLS_SSL_SIG_RSA ); |
| 5035 | case MBEDTLS_PK_ECDSA: |
| 5036 | case MBEDTLS_PK_ECKEY: |
| 5037 | return( MBEDTLS_SSL_SIG_ECDSA ); |
| 5038 | default: |
| 5039 | return( MBEDTLS_SSL_SIG_ANON ); |
| 5040 | } |
| 5041 | } |
| 5042 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5043 | 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] | 5044 | { |
| 5045 | switch( sig ) |
| 5046 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5047 | #if defined(MBEDTLS_RSA_C) |
| 5048 | case MBEDTLS_SSL_SIG_RSA: |
| 5049 | return( MBEDTLS_PK_RSA ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 5050 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5051 | #if defined(MBEDTLS_ECDSA_C) |
| 5052 | case MBEDTLS_SSL_SIG_ECDSA: |
| 5053 | return( MBEDTLS_PK_ECDSA ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 5054 | #endif |
| 5055 | default: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5056 | return( MBEDTLS_PK_NONE ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 5057 | } |
| 5058 | } |
Manuel Pégourié-Gonnard | 5674a97 | 2015-10-19 15:14:03 +0200 | [diff] [blame] | 5059 | #endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */ |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 5060 | |
Manuel Pégourié-Gonnard | 1a48383 | 2013-09-20 12:29:15 +0200 | [diff] [blame] | 5061 | /* |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 5062 | * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX |
Manuel Pégourié-Gonnard | 1a48383 | 2013-09-20 12:29:15 +0200 | [diff] [blame] | 5063 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5064 | 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] | 5065 | { |
| 5066 | switch( hash ) |
| 5067 | { |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5068 | #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] | 5069 | case MBEDTLS_SSL_HASH_MD5: |
| 5070 | return( MBEDTLS_MD_MD5 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 5071 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5072 | #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] | 5073 | case MBEDTLS_SSL_HASH_SHA1: |
| 5074 | return( MBEDTLS_MD_SHA1 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 5075 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5076 | #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] | 5077 | case MBEDTLS_SSL_HASH_SHA224: |
| 5078 | return( MBEDTLS_MD_SHA224 ); |
Mateusz Starzyk | e3c48b4 | 2021-04-19 16:46:28 +0200 | [diff] [blame] | 5079 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5080 | #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] | 5081 | case MBEDTLS_SSL_HASH_SHA256: |
| 5082 | return( MBEDTLS_MD_SHA256 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 5083 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5084 | #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] | 5085 | case MBEDTLS_SSL_HASH_SHA384: |
| 5086 | return( MBEDTLS_MD_SHA384 ); |
Mateusz Starzyk | 3352a53 | 2021-04-06 14:28:22 +0200 | [diff] [blame] | 5087 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5088 | #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] | 5089 | case MBEDTLS_SSL_HASH_SHA512: |
| 5090 | return( MBEDTLS_MD_SHA512 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 5091 | #endif |
| 5092 | default: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5093 | return( MBEDTLS_MD_NONE ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 5094 | } |
| 5095 | } |
| 5096 | |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 5097 | /* |
| 5098 | * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX |
| 5099 | */ |
| 5100 | unsigned char mbedtls_ssl_hash_from_md_alg( int md ) |
| 5101 | { |
| 5102 | switch( md ) |
| 5103 | { |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5104 | #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] | 5105 | case MBEDTLS_MD_MD5: |
| 5106 | return( MBEDTLS_SSL_HASH_MD5 ); |
| 5107 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5108 | #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] | 5109 | case MBEDTLS_MD_SHA1: |
| 5110 | return( MBEDTLS_SSL_HASH_SHA1 ); |
| 5111 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5112 | #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] | 5113 | case MBEDTLS_MD_SHA224: |
| 5114 | return( MBEDTLS_SSL_HASH_SHA224 ); |
Mateusz Starzyk | e3c48b4 | 2021-04-19 16:46:28 +0200 | [diff] [blame] | 5115 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5116 | #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] | 5117 | case MBEDTLS_MD_SHA256: |
| 5118 | return( MBEDTLS_SSL_HASH_SHA256 ); |
| 5119 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5120 | #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] | 5121 | case MBEDTLS_MD_SHA384: |
| 5122 | return( MBEDTLS_SSL_HASH_SHA384 ); |
Mateusz Starzyk | 3352a53 | 2021-04-06 14:28:22 +0200 | [diff] [blame] | 5123 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5124 | #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] | 5125 | case MBEDTLS_MD_SHA512: |
| 5126 | return( MBEDTLS_SSL_HASH_SHA512 ); |
| 5127 | #endif |
| 5128 | default: |
| 5129 | return( MBEDTLS_SSL_HASH_NONE ); |
| 5130 | } |
| 5131 | } |
| 5132 | |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 5133 | /* |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 5134 | * 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] | 5135 | * 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] | 5136 | */ |
Manuel Pégourié-Gonnard | 0d63b84 | 2022-01-18 13:10:56 +0100 | [diff] [blame] | 5137 | 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] | 5138 | { |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5139 | const uint16_t *group_list = mbedtls_ssl_get_groups( ssl ); |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 5140 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5141 | if( group_list == NULL ) |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 5142 | return( -1 ); |
| 5143 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5144 | for( ; *group_list != 0; group_list++ ) |
| 5145 | { |
| 5146 | if( *group_list == tls_id ) |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 5147 | return( 0 ); |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5148 | } |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 5149 | |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 5150 | return( -1 ); |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 5151 | } |
Manuel Pégourié-Gonnard | 0d63b84 | 2022-01-18 13:10:56 +0100 | [diff] [blame] | 5152 | |
| 5153 | #if defined(MBEDTLS_ECP_C) |
| 5154 | /* |
| 5155 | * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id. |
| 5156 | */ |
| 5157 | int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id ) |
| 5158 | { |
Leonid Rozenboim | 19e5973 | 2022-08-08 16:52:38 -0700 | [diff] [blame] | 5159 | const mbedtls_ecp_curve_info *grp_info = |
Tom Cosgrove | bcc13c9 | 2022-08-24 15:08:16 +0100 | [diff] [blame] | 5160 | mbedtls_ecp_curve_info_from_grp_id( grp_id ); |
Leonid Rozenboim | 19e5973 | 2022-08-08 16:52:38 -0700 | [diff] [blame] | 5161 | |
Tom Cosgrove | bcc13c9 | 2022-08-24 15:08:16 +0100 | [diff] [blame] | 5162 | if ( grp_info == NULL ) |
Leonid Rozenboim | 19e5973 | 2022-08-08 16:52:38 -0700 | [diff] [blame] | 5163 | return -1; |
| 5164 | |
| 5165 | uint16_t tls_id = grp_info->tls_id; |
| 5166 | |
Manuel Pégourié-Gonnard | 0d63b84 | 2022-01-18 13:10:56 +0100 | [diff] [blame] | 5167 | return mbedtls_ssl_check_curve_tls_id( ssl, tls_id ); |
| 5168 | } |
Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 5169 | #endif /* MBEDTLS_ECP_C */ |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5170 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5171 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 5172 | int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert, |
| 5173 | const mbedtls_ssl_ciphersuite_t *ciphersuite, |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 5174 | int cert_endpoint, |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 5175 | uint32_t *flags ) |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5176 | { |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 5177 | int ret = 0; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5178 | int usage = 0; |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 5179 | const char *ext_oid; |
| 5180 | size_t ext_len; |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 5181 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5182 | if( cert_endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5183 | { |
| 5184 | /* Server part of the key exchange */ |
| 5185 | switch( ciphersuite->key_exchange ) |
| 5186 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5187 | case MBEDTLS_KEY_EXCHANGE_RSA: |
| 5188 | case MBEDTLS_KEY_EXCHANGE_RSA_PSK: |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 5189 | usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5190 | break; |
| 5191 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5192 | case MBEDTLS_KEY_EXCHANGE_DHE_RSA: |
| 5193 | case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA: |
| 5194 | case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA: |
| 5195 | usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5196 | break; |
| 5197 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5198 | case MBEDTLS_KEY_EXCHANGE_ECDH_RSA: |
| 5199 | case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA: |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 5200 | usage = MBEDTLS_X509_KU_KEY_AGREEMENT; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5201 | break; |
| 5202 | |
| 5203 | /* 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] | 5204 | case MBEDTLS_KEY_EXCHANGE_NONE: |
| 5205 | case MBEDTLS_KEY_EXCHANGE_PSK: |
| 5206 | case MBEDTLS_KEY_EXCHANGE_DHE_PSK: |
| 5207 | case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK: |
Manuel Pégourié-Gonnard | 557535d | 2015-09-15 17:53:32 +0200 | [diff] [blame] | 5208 | case MBEDTLS_KEY_EXCHANGE_ECJPAKE: |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5209 | usage = 0; |
| 5210 | } |
| 5211 | } |
| 5212 | else |
| 5213 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5214 | /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */ |
| 5215 | usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5216 | } |
| 5217 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5218 | if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 ) |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 5219 | { |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 5220 | *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE; |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 5221 | ret = -1; |
| 5222 | } |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5223 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5224 | if( cert_endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 5225 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5226 | ext_oid = MBEDTLS_OID_SERVER_AUTH; |
| 5227 | ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH ); |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 5228 | } |
| 5229 | else |
| 5230 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5231 | ext_oid = MBEDTLS_OID_CLIENT_AUTH; |
| 5232 | ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH ); |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 5233 | } |
| 5234 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5235 | 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] | 5236 | { |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 5237 | *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE; |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 5238 | ret = -1; |
| 5239 | } |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 5240 | |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 5241 | return( ret ); |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5242 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5243 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Manuel Pégourié-Gonnard | 3a306b9 | 2014-04-29 15:11:17 +0200 | [diff] [blame] | 5244 | |
Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 5245 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 5246 | int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl, |
| 5247 | const mbedtls_md_type_t md, |
| 5248 | unsigned char *dst, |
| 5249 | size_t dst_len, |
| 5250 | size_t *olen ) |
| 5251 | { |
Ronald Cron | f6893e1 | 2022-01-07 22:09:01 +0100 | [diff] [blame] | 5252 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 5253 | psa_hash_operation_t *hash_operation_to_clone; |
| 5254 | psa_hash_operation_t hash_operation = psa_hash_operation_init(); |
| 5255 | |
Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 5256 | *olen = 0; |
Ronald Cron | f6893e1 | 2022-01-07 22:09:01 +0100 | [diff] [blame] | 5257 | |
| 5258 | switch( md ) |
| 5259 | { |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5260 | #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] | 5261 | case MBEDTLS_MD_SHA384: |
| 5262 | hash_operation_to_clone = &ssl->handshake->fin_sha384_psa; |
| 5263 | break; |
| 5264 | #endif |
| 5265 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5266 | #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] | 5267 | case MBEDTLS_MD_SHA256: |
| 5268 | hash_operation_to_clone = &ssl->handshake->fin_sha256_psa; |
| 5269 | break; |
| 5270 | #endif |
| 5271 | |
| 5272 | default: |
| 5273 | goto exit; |
| 5274 | } |
| 5275 | |
| 5276 | status = psa_hash_clone( hash_operation_to_clone, &hash_operation ); |
| 5277 | if( status != PSA_SUCCESS ) |
| 5278 | goto exit; |
| 5279 | |
| 5280 | status = psa_hash_finish( &hash_operation, dst, dst_len, olen ); |
| 5281 | if( status != PSA_SUCCESS ) |
| 5282 | goto exit; |
| 5283 | |
| 5284 | exit: |
Andrzej Kurek | eabeb30 | 2022-10-17 07:52:51 -0400 | [diff] [blame] | 5285 | #if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \ |
| 5286 | !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
| 5287 | (void) ssl; |
| 5288 | #endif |
Ronald Cron | b788c04 | 2022-02-15 09:14:15 +0100 | [diff] [blame] | 5289 | return( psa_ssl_status_to_mbedtls( status ) ); |
Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 5290 | } |
| 5291 | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
| 5292 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5293 | #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] | 5294 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | 000f976 | 2021-09-14 11:12:51 +0800 | [diff] [blame] | 5295 | static int ssl_get_handshake_transcript_sha384( mbedtls_ssl_context *ssl, |
| 5296 | unsigned char *dst, |
| 5297 | size_t dst_len, |
| 5298 | size_t *olen ) |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5299 | { |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5300 | int ret; |
| 5301 | mbedtls_sha512_context sha512; |
| 5302 | |
| 5303 | if( dst_len < 48 ) |
| 5304 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 5305 | |
| 5306 | mbedtls_sha512_init( &sha512 ); |
Andrzej Kurek | a242e83 | 2022-08-11 10:03:14 -0400 | [diff] [blame] | 5307 | mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 ); |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5308 | |
| 5309 | if( ( ret = mbedtls_sha512_finish( &sha512, dst ) ) != 0 ) |
| 5310 | { |
| 5311 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha512_finish", ret ); |
| 5312 | goto exit; |
| 5313 | } |
| 5314 | |
| 5315 | *olen = 48; |
| 5316 | |
| 5317 | exit: |
| 5318 | |
| 5319 | mbedtls_sha512_free( &sha512 ); |
| 5320 | return( ret ); |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5321 | } |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5322 | #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] | 5323 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5324 | #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] | 5325 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | 000f976 | 2021-09-14 11:12:51 +0800 | [diff] [blame] | 5326 | static int ssl_get_handshake_transcript_sha256( mbedtls_ssl_context *ssl, |
| 5327 | unsigned char *dst, |
| 5328 | size_t dst_len, |
| 5329 | size_t *olen ) |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5330 | { |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5331 | int ret; |
| 5332 | mbedtls_sha256_context sha256; |
| 5333 | |
| 5334 | if( dst_len < 32 ) |
| 5335 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 5336 | |
| 5337 | mbedtls_sha256_init( &sha256 ); |
| 5338 | mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 ); |
Jerry Yu | c5aef88 | 2021-12-23 20:15:02 +0800 | [diff] [blame] | 5339 | |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5340 | if( ( ret = mbedtls_sha256_finish( &sha256, dst ) ) != 0 ) |
| 5341 | { |
| 5342 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha256_finish", ret ); |
| 5343 | goto exit; |
| 5344 | } |
| 5345 | |
| 5346 | *olen = 32; |
| 5347 | |
| 5348 | exit: |
| 5349 | |
| 5350 | mbedtls_sha256_free( &sha256 ); |
| 5351 | return( ret ); |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5352 | } |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5353 | #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] | 5354 | |
Jerry Yu | 000f976 | 2021-09-14 11:12:51 +0800 | [diff] [blame] | 5355 | int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl, |
| 5356 | const mbedtls_md_type_t md, |
| 5357 | unsigned char *dst, |
| 5358 | size_t dst_len, |
| 5359 | size_t *olen ) |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5360 | { |
Jerry Yu | c1ddeef | 2021-10-08 15:14:45 +0800 | [diff] [blame] | 5361 | switch( md ) |
| 5362 | { |
| 5363 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5364 | #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] | 5365 | case MBEDTLS_MD_SHA384: |
Jerry Yu | c5aef88 | 2021-12-23 20:15:02 +0800 | [diff] [blame] | 5366 | return( ssl_get_handshake_transcript_sha384( ssl, dst, dst_len, olen ) ); |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 5367 | #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] | 5368 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5369 | #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] | 5370 | case MBEDTLS_MD_SHA256: |
Jerry Yu | c5aef88 | 2021-12-23 20:15:02 +0800 | [diff] [blame] | 5371 | return( ssl_get_handshake_transcript_sha256( ssl, dst, dst_len, olen ) ); |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 5372 | #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] | 5373 | |
| 5374 | default: |
Andrzej Kurek | 409248a | 2022-10-24 10:33:21 -0400 | [diff] [blame] | 5375 | #if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \ |
| 5376 | !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
| 5377 | (void) ssl; |
| 5378 | (void) dst; |
| 5379 | (void) dst_len; |
| 5380 | (void) olen; |
| 5381 | #endif |
Jerry Yu | c1ddeef | 2021-10-08 15:14:45 +0800 | [diff] [blame] | 5382 | break; |
| 5383 | } |
Jerry Yu | c5aef88 | 2021-12-23 20:15:02 +0800 | [diff] [blame] | 5384 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5385 | } |
XiaokangQian | 647719a | 2021-12-07 09:16:29 +0000 | [diff] [blame] | 5386 | |
Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 5387 | #endif /* !MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5388 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5389 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 5390 | /* mbedtls_ssl_parse_sig_alg_ext() |
| 5391 | * |
| 5392 | * The `extension_data` field of signature algorithm contains a `SignatureSchemeList` |
| 5393 | * value (TLS 1.3 RFC8446): |
| 5394 | * enum { |
| 5395 | * .... |
| 5396 | * ecdsa_secp256r1_sha256( 0x0403 ), |
| 5397 | * ecdsa_secp384r1_sha384( 0x0503 ), |
| 5398 | * ecdsa_secp521r1_sha512( 0x0603 ), |
| 5399 | * .... |
| 5400 | * } SignatureScheme; |
| 5401 | * |
| 5402 | * struct { |
| 5403 | * SignatureScheme supported_signature_algorithms<2..2^16-2>; |
| 5404 | * } SignatureSchemeList; |
| 5405 | * |
| 5406 | * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm` |
| 5407 | * value (TLS 1.2 RFC5246): |
| 5408 | * enum { |
| 5409 | * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5), |
| 5410 | * sha512(6), (255) |
| 5411 | * } HashAlgorithm; |
| 5412 | * |
| 5413 | * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) } |
| 5414 | * SignatureAlgorithm; |
| 5415 | * |
| 5416 | * struct { |
| 5417 | * HashAlgorithm hash; |
| 5418 | * SignatureAlgorithm signature; |
| 5419 | * } SignatureAndHashAlgorithm; |
| 5420 | * |
| 5421 | * SignatureAndHashAlgorithm |
| 5422 | * supported_signature_algorithms<2..2^16-2>; |
| 5423 | * |
| 5424 | * The TLS 1.3 signature algorithm extension was defined to be a compatible |
| 5425 | * generalization of the TLS 1.2 signature algorithm extension. |
| 5426 | * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by |
| 5427 | * `SignatureScheme` field of TLS 1.3 |
| 5428 | * |
| 5429 | */ |
| 5430 | int mbedtls_ssl_parse_sig_alg_ext( mbedtls_ssl_context *ssl, |
| 5431 | const unsigned char *buf, |
| 5432 | const unsigned char *end ) |
| 5433 | { |
| 5434 | const unsigned char *p = buf; |
| 5435 | size_t supported_sig_algs_len = 0; |
| 5436 | const unsigned char *supported_sig_algs_end; |
| 5437 | uint16_t sig_alg; |
| 5438 | uint32_t common_idx = 0; |
| 5439 | |
| 5440 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 ); |
| 5441 | supported_sig_algs_len = MBEDTLS_GET_UINT16_BE( p, 0 ); |
| 5442 | p += 2; |
| 5443 | |
| 5444 | memset( ssl->handshake->received_sig_algs, 0, |
| 5445 | sizeof(ssl->handshake->received_sig_algs) ); |
| 5446 | |
| 5447 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, supported_sig_algs_len ); |
| 5448 | supported_sig_algs_end = p + supported_sig_algs_len; |
| 5449 | while( p < supported_sig_algs_end ) |
| 5450 | { |
| 5451 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, supported_sig_algs_end, 2 ); |
| 5452 | sig_alg = MBEDTLS_GET_UINT16_BE( p, 0 ); |
| 5453 | p += 2; |
Jerry Yu | f3b46b5 | 2022-06-19 16:52:27 +0800 | [diff] [blame] | 5454 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "received signature algorithm: 0x%x %s", |
| 5455 | sig_alg, |
| 5456 | mbedtls_ssl_sig_alg_to_str( sig_alg ) ) ); |
Jerry Yu | 2fe6c63 | 2022-06-29 10:02:38 +0800 | [diff] [blame] | 5457 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Jerry Yu | 52b7d92 | 2022-07-01 18:03:31 +0800 | [diff] [blame] | 5458 | if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 && |
Jerry Yu | 2fe6c63 | 2022-06-29 10:02:38 +0800 | [diff] [blame] | 5459 | ( ! ( mbedtls_ssl_sig_alg_is_supported( ssl, sig_alg ) && |
| 5460 | mbedtls_ssl_sig_alg_is_offered( ssl, sig_alg ) ) ) ) |
| 5461 | { |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 5462 | continue; |
Jerry Yu | 2fe6c63 | 2022-06-29 10:02:38 +0800 | [diff] [blame] | 5463 | } |
| 5464 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 5465 | |
Jerry Yu | f3b46b5 | 2022-06-19 16:52:27 +0800 | [diff] [blame] | 5466 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "valid signature algorithm: %s", |
| 5467 | mbedtls_ssl_sig_alg_to_str( sig_alg ) ) ); |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 5468 | |
| 5469 | if( common_idx + 1 < MBEDTLS_RECEIVED_SIG_ALGS_SIZE ) |
| 5470 | { |
| 5471 | ssl->handshake->received_sig_algs[common_idx] = sig_alg; |
| 5472 | common_idx += 1; |
| 5473 | } |
| 5474 | } |
| 5475 | /* Check that we consumed all the message. */ |
| 5476 | if( p != end ) |
| 5477 | { |
| 5478 | MBEDTLS_SSL_DEBUG_MSG( 1, |
| 5479 | ( "Signature algorithms extension length misaligned" ) ); |
| 5480 | MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR, |
| 5481 | MBEDTLS_ERR_SSL_DECODE_ERROR ); |
| 5482 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
| 5483 | } |
| 5484 | |
| 5485 | if( common_idx == 0 ) |
| 5486 | { |
| 5487 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "no signature algorithm in common" ) ); |
| 5488 | MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE, |
| 5489 | MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE ); |
| 5490 | return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE ); |
| 5491 | } |
| 5492 | |
Gabor Mezei | 15b95a6 | 2022-05-09 16:37:58 +0200 | [diff] [blame] | 5493 | ssl->handshake->received_sig_algs[common_idx] = MBEDTLS_TLS_SIG_NONE; |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 5494 | return( 0 ); |
| 5495 | } |
| 5496 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5497 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 5498 | |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5499 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 5500 | |
| 5501 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 5502 | |
| 5503 | static psa_status_t setup_psa_key_derivation( psa_key_derivation_operation_t* derivation, |
| 5504 | mbedtls_svc_key_id_t key, |
| 5505 | psa_algorithm_t alg, |
Neil Armstrong | 8ecd668 | 2022-05-05 11:40:35 +0200 | [diff] [blame] | 5506 | const unsigned char* raw_psk, size_t raw_psk_length, |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5507 | const unsigned char* seed, size_t seed_length, |
| 5508 | const unsigned char* label, size_t label_length, |
Przemek Stekiel | 51a1f36 | 2022-04-13 08:57:06 +0200 | [diff] [blame] | 5509 | const unsigned char* other_secret, |
| 5510 | size_t other_secret_length, |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5511 | size_t capacity ) |
| 5512 | { |
| 5513 | psa_status_t status; |
| 5514 | |
| 5515 | status = psa_key_derivation_setup( derivation, alg ); |
| 5516 | if( status != PSA_SUCCESS ) |
| 5517 | return( status ); |
| 5518 | |
| 5519 | if( PSA_ALG_IS_TLS12_PRF( alg ) || PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) ) |
| 5520 | { |
| 5521 | status = psa_key_derivation_input_bytes( derivation, |
| 5522 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 5523 | seed, seed_length ); |
| 5524 | if( status != PSA_SUCCESS ) |
| 5525 | return( status ); |
| 5526 | |
Przemek Stekiel | 51a1f36 | 2022-04-13 08:57:06 +0200 | [diff] [blame] | 5527 | if ( other_secret != NULL ) |
Przemek Stekiel | 1f02703 | 2022-04-05 17:12:11 +0200 | [diff] [blame] | 5528 | { |
| 5529 | status = psa_key_derivation_input_bytes( derivation, |
Przemek Stekiel | 51a1f36 | 2022-04-13 08:57:06 +0200 | [diff] [blame] | 5530 | PSA_KEY_DERIVATION_INPUT_OTHER_SECRET, |
| 5531 | other_secret, other_secret_length ); |
Przemek Stekiel | 1f02703 | 2022-04-05 17:12:11 +0200 | [diff] [blame] | 5532 | if( status != PSA_SUCCESS ) |
| 5533 | return( status ); |
| 5534 | } |
| 5535 | |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5536 | if( mbedtls_svc_key_id_is_null( key ) ) |
| 5537 | { |
| 5538 | status = psa_key_derivation_input_bytes( |
| 5539 | derivation, PSA_KEY_DERIVATION_INPUT_SECRET, |
Neil Armstrong | 8ecd668 | 2022-05-05 11:40:35 +0200 | [diff] [blame] | 5540 | raw_psk, raw_psk_length ); |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5541 | } |
| 5542 | else |
| 5543 | { |
| 5544 | status = psa_key_derivation_input_key( |
| 5545 | derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key ); |
| 5546 | } |
| 5547 | if( status != PSA_SUCCESS ) |
| 5548 | return( status ); |
| 5549 | |
| 5550 | status = psa_key_derivation_input_bytes( derivation, |
| 5551 | PSA_KEY_DERIVATION_INPUT_LABEL, |
| 5552 | label, label_length ); |
| 5553 | if( status != PSA_SUCCESS ) |
| 5554 | return( status ); |
| 5555 | } |
| 5556 | else |
| 5557 | { |
| 5558 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5559 | } |
| 5560 | |
| 5561 | status = psa_key_derivation_set_capacity( derivation, capacity ); |
| 5562 | if( status != PSA_SUCCESS ) |
| 5563 | return( status ); |
| 5564 | |
| 5565 | return( PSA_SUCCESS ); |
| 5566 | } |
| 5567 | |
Andrzej Kurek | 57d1063 | 2022-10-24 10:32:01 -0400 | [diff] [blame] | 5568 | #if defined(PSA_WANT_ALG_SHA_384) || \ |
| 5569 | defined(PSA_WANT_ALG_SHA_256) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 5570 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5571 | static int tls_prf_generic( mbedtls_md_type_t md_type, |
| 5572 | const unsigned char *secret, size_t slen, |
| 5573 | const char *label, |
| 5574 | const unsigned char *random, size_t rlen, |
| 5575 | unsigned char *dstbuf, size_t dlen ) |
| 5576 | { |
| 5577 | psa_status_t status; |
| 5578 | psa_algorithm_t alg; |
| 5579 | mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5580 | psa_key_derivation_operation_t derivation = |
| 5581 | PSA_KEY_DERIVATION_OPERATION_INIT; |
| 5582 | |
| 5583 | if( md_type == MBEDTLS_MD_SHA384 ) |
| 5584 | alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384); |
| 5585 | else |
| 5586 | alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256); |
| 5587 | |
| 5588 | /* Normally a "secret" should be long enough to be impossible to |
| 5589 | * find by brute force, and in particular should not be empty. But |
| 5590 | * this PRF is also used to derive an IV, in particular in EAP-TLS, |
| 5591 | * and for this use case it makes sense to have a 0-length "secret". |
| 5592 | * Since the key API doesn't allow importing a key of length 0, |
| 5593 | * keep master_key=0, which setup_psa_key_derivation() understands |
| 5594 | * to mean a 0-length "secret" input. */ |
| 5595 | if( slen != 0 ) |
| 5596 | { |
| 5597 | psa_key_attributes_t key_attributes = psa_key_attributes_init(); |
| 5598 | psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE ); |
| 5599 | psa_set_key_algorithm( &key_attributes, alg ); |
| 5600 | psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE ); |
| 5601 | |
| 5602 | status = psa_import_key( &key_attributes, secret, slen, &master_key ); |
| 5603 | if( status != PSA_SUCCESS ) |
| 5604 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 5605 | } |
| 5606 | |
| 5607 | status = setup_psa_key_derivation( &derivation, |
| 5608 | master_key, alg, |
Neil Armstrong | 8ecd668 | 2022-05-05 11:40:35 +0200 | [diff] [blame] | 5609 | NULL, 0, |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5610 | random, rlen, |
| 5611 | (unsigned char const *) label, |
| 5612 | (size_t) strlen( label ), |
Przemek Stekiel | 1f02703 | 2022-04-05 17:12:11 +0200 | [diff] [blame] | 5613 | NULL, 0, |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5614 | dlen ); |
| 5615 | if( status != PSA_SUCCESS ) |
| 5616 | { |
| 5617 | psa_key_derivation_abort( &derivation ); |
| 5618 | psa_destroy_key( master_key ); |
| 5619 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 5620 | } |
| 5621 | |
| 5622 | status = psa_key_derivation_output_bytes( &derivation, dstbuf, dlen ); |
| 5623 | if( status != PSA_SUCCESS ) |
| 5624 | { |
| 5625 | psa_key_derivation_abort( &derivation ); |
| 5626 | psa_destroy_key( master_key ); |
| 5627 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 5628 | } |
| 5629 | |
| 5630 | status = psa_key_derivation_abort( &derivation ); |
| 5631 | if( status != PSA_SUCCESS ) |
| 5632 | { |
| 5633 | psa_destroy_key( master_key ); |
| 5634 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 5635 | } |
| 5636 | |
| 5637 | if( ! mbedtls_svc_key_id_is_null( master_key ) ) |
| 5638 | status = psa_destroy_key( master_key ); |
| 5639 | if( status != PSA_SUCCESS ) |
| 5640 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 5641 | |
| 5642 | return( 0 ); |
| 5643 | } |
Andrzej Kurek | 57d1063 | 2022-10-24 10:32:01 -0400 | [diff] [blame] | 5644 | #endif /* PSA_WANT_ALG_SHA_256 || PSA_WANT_ALG_SHA_384 */ |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5645 | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
| 5646 | |
Andrzej Kurek | 57d1063 | 2022-10-24 10:32:01 -0400 | [diff] [blame] | 5647 | #if defined(MBEDTLS_MD_C) && \ |
| 5648 | ( defined(MBEDTLS_SHA256_C) || \ |
| 5649 | defined(MBEDTLS_SHA384_C) ) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 5650 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5651 | static int tls_prf_generic( mbedtls_md_type_t md_type, |
| 5652 | const unsigned char *secret, size_t slen, |
| 5653 | const char *label, |
| 5654 | const unsigned char *random, size_t rlen, |
| 5655 | unsigned char *dstbuf, size_t dlen ) |
| 5656 | { |
| 5657 | size_t nb; |
| 5658 | size_t i, j, k, md_len; |
| 5659 | unsigned char *tmp; |
| 5660 | size_t tmp_len = 0; |
| 5661 | unsigned char h_i[MBEDTLS_MD_MAX_SIZE]; |
| 5662 | const mbedtls_md_info_t *md_info; |
| 5663 | mbedtls_md_context_t md_ctx; |
| 5664 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 5665 | |
| 5666 | mbedtls_md_init( &md_ctx ); |
| 5667 | |
| 5668 | if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL ) |
| 5669 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 5670 | |
| 5671 | md_len = mbedtls_md_get_size( md_info ); |
| 5672 | |
| 5673 | tmp_len = md_len + strlen( label ) + rlen; |
| 5674 | tmp = mbedtls_calloc( 1, tmp_len ); |
| 5675 | if( tmp == NULL ) |
| 5676 | { |
| 5677 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 5678 | goto exit; |
| 5679 | } |
| 5680 | |
| 5681 | nb = strlen( label ); |
| 5682 | memcpy( tmp + md_len, label, nb ); |
| 5683 | memcpy( tmp + md_len + nb, random, rlen ); |
| 5684 | nb += rlen; |
| 5685 | |
| 5686 | /* |
| 5687 | * Compute P_<hash>(secret, label + random)[0..dlen] |
| 5688 | */ |
| 5689 | if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 ) |
| 5690 | goto exit; |
| 5691 | |
| 5692 | ret = mbedtls_md_hmac_starts( &md_ctx, secret, slen ); |
| 5693 | if( ret != 0 ) |
| 5694 | goto exit; |
| 5695 | ret = mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb ); |
| 5696 | if( ret != 0 ) |
| 5697 | goto exit; |
| 5698 | ret = mbedtls_md_hmac_finish( &md_ctx, tmp ); |
| 5699 | if( ret != 0 ) |
| 5700 | goto exit; |
| 5701 | |
| 5702 | for( i = 0; i < dlen; i += md_len ) |
| 5703 | { |
| 5704 | ret = mbedtls_md_hmac_reset ( &md_ctx ); |
| 5705 | if( ret != 0 ) |
| 5706 | goto exit; |
| 5707 | ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb ); |
| 5708 | if( ret != 0 ) |
| 5709 | goto exit; |
| 5710 | ret = mbedtls_md_hmac_finish( &md_ctx, h_i ); |
| 5711 | if( ret != 0 ) |
| 5712 | goto exit; |
| 5713 | |
| 5714 | ret = mbedtls_md_hmac_reset ( &md_ctx ); |
| 5715 | if( ret != 0 ) |
| 5716 | goto exit; |
| 5717 | ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len ); |
| 5718 | if( ret != 0 ) |
| 5719 | goto exit; |
| 5720 | ret = mbedtls_md_hmac_finish( &md_ctx, tmp ); |
| 5721 | if( ret != 0 ) |
| 5722 | goto exit; |
| 5723 | |
| 5724 | k = ( i + md_len > dlen ) ? dlen % md_len : md_len; |
| 5725 | |
| 5726 | for( j = 0; j < k; j++ ) |
| 5727 | dstbuf[i + j] = h_i[j]; |
| 5728 | } |
| 5729 | |
| 5730 | exit: |
| 5731 | mbedtls_md_free( &md_ctx ); |
| 5732 | |
| 5733 | mbedtls_platform_zeroize( tmp, tmp_len ); |
| 5734 | mbedtls_platform_zeroize( h_i, sizeof( h_i ) ); |
| 5735 | |
| 5736 | mbedtls_free( tmp ); |
| 5737 | |
| 5738 | return( ret ); |
| 5739 | } |
Andrzej Kurek | 57d1063 | 2022-10-24 10:32:01 -0400 | [diff] [blame] | 5740 | #endif /* MBEDTLS_MD_C && ( MBEDTLS_SHA256_C || MBEDTLS_SHA384_C ) */ |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5741 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 5742 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5743 | #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] | 5744 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5745 | static int tls_prf_sha256( const unsigned char *secret, size_t slen, |
| 5746 | const char *label, |
| 5747 | const unsigned char *random, size_t rlen, |
| 5748 | unsigned char *dstbuf, size_t dlen ) |
| 5749 | { |
| 5750 | return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen, |
| 5751 | label, random, rlen, dstbuf, dlen ) ); |
| 5752 | } |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 5753 | #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] | 5754 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5755 | #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] | 5756 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5757 | static int tls_prf_sha384( const unsigned char *secret, size_t slen, |
| 5758 | const char *label, |
| 5759 | const unsigned char *random, size_t rlen, |
| 5760 | unsigned char *dstbuf, size_t dlen ) |
| 5761 | { |
| 5762 | return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen, |
| 5763 | label, random, rlen, dstbuf, dlen ) ); |
| 5764 | } |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 5765 | #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] | 5766 | |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5767 | /* |
| 5768 | * Set appropriate PRF function and other SSL / TLS1.2 functions |
| 5769 | * |
| 5770 | * Inputs: |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5771 | * - hash associated with the ciphersuite (only used by TLS 1.2) |
| 5772 | * |
| 5773 | * Outputs: |
| 5774 | * - the tls_prf, calc_verify and calc_finished members of handshake structure |
| 5775 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 5776 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5777 | static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake, |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5778 | mbedtls_md_type_t hash ) |
| 5779 | { |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5780 | #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] | 5781 | if( hash == MBEDTLS_MD_SHA384 ) |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5782 | { |
| 5783 | handshake->tls_prf = tls_prf_sha384; |
| 5784 | handshake->calc_verify = ssl_calc_verify_tls_sha384; |
| 5785 | handshake->calc_finished = ssl_calc_finished_tls_sha384; |
| 5786 | } |
| 5787 | else |
| 5788 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5789 | #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] | 5790 | { |
Ronald Cron | 81591aa | 2022-03-07 09:05:51 +0100 | [diff] [blame] | 5791 | (void) hash; |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5792 | handshake->tls_prf = tls_prf_sha256; |
| 5793 | handshake->calc_verify = ssl_calc_verify_tls_sha256; |
| 5794 | handshake->calc_finished = ssl_calc_finished_tls_sha256; |
| 5795 | } |
Ronald Cron | 81591aa | 2022-03-07 09:05:51 +0100 | [diff] [blame] | 5796 | #else |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5797 | { |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5798 | (void) handshake; |
Ronald Cron | 81591aa | 2022-03-07 09:05:51 +0100 | [diff] [blame] | 5799 | (void) hash; |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5800 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 5801 | } |
Ronald Cron | 81591aa | 2022-03-07 09:05:51 +0100 | [diff] [blame] | 5802 | #endif |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5803 | |
| 5804 | return( 0 ); |
| 5805 | } |
Jerry Yu | d6ab235 | 2022-02-17 14:03:43 +0800 | [diff] [blame] | 5806 | |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5807 | /* |
| 5808 | * Compute master secret if needed |
| 5809 | * |
| 5810 | * Parameters: |
| 5811 | * [in/out] handshake |
| 5812 | * [in] resume, premaster, extended_ms, calc_verify, tls_prf |
| 5813 | * (PSA-PSK) ciphersuite_info, psk_opaque |
| 5814 | * [out] premaster (cleared) |
| 5815 | * [out] master |
| 5816 | * [in] ssl: optionally used for debugging, EMS and PSA-PSK |
| 5817 | * debug: conf->f_dbg, conf->p_dbg |
| 5818 | * EMS: passed to calc_verify (debug + session_negotiate) |
Ronald Cron | a25cf58 | 2022-03-07 11:10:36 +0100 | [diff] [blame] | 5819 | * PSA-PSA: conf |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5820 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 5821 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5822 | static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake, |
| 5823 | unsigned char *master, |
| 5824 | const mbedtls_ssl_context *ssl ) |
| 5825 | { |
| 5826 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 5827 | |
| 5828 | /* cf. RFC 5246, Section 8.1: |
| 5829 | * "The master secret is always exactly 48 bytes in length." */ |
| 5830 | size_t const master_secret_len = 48; |
| 5831 | |
| 5832 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 5833 | unsigned char session_hash[48]; |
| 5834 | #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */ |
| 5835 | |
| 5836 | /* The label for the KDF used for key expansion. |
| 5837 | * This is either "master secret" or "extended master secret" |
| 5838 | * depending on whether the Extended Master Secret extension |
| 5839 | * is used. */ |
| 5840 | char const *lbl = "master secret"; |
| 5841 | |
Przemek Stekiel | ae4ed30 | 2022-04-05 17:15:55 +0200 | [diff] [blame] | 5842 | /* The seed for the KDF used for key expansion. |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5843 | * - If the Extended Master Secret extension is not used, |
| 5844 | * this is ClientHello.Random + ServerHello.Random |
| 5845 | * (see Sect. 8.1 in RFC 5246). |
| 5846 | * - If the Extended Master Secret extension is used, |
| 5847 | * this is the transcript of the handshake so far. |
| 5848 | * (see Sect. 4 in RFC 7627). */ |
Przemek Stekiel | ae4ed30 | 2022-04-05 17:15:55 +0200 | [diff] [blame] | 5849 | unsigned char const *seed = handshake->randbytes; |
| 5850 | size_t seed_len = 64; |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5851 | |
| 5852 | #if !defined(MBEDTLS_DEBUG_C) && \ |
| 5853 | !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \ |
| 5854 | !(defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
| 5855 | defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)) |
| 5856 | ssl = NULL; /* make sure we don't use it except for those cases */ |
| 5857 | (void) ssl; |
| 5858 | #endif |
| 5859 | |
| 5860 | if( handshake->resume != 0 ) |
| 5861 | { |
| 5862 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) ); |
| 5863 | return( 0 ); |
| 5864 | } |
| 5865 | |
| 5866 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 5867 | if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED ) |
| 5868 | { |
| 5869 | lbl = "extended master secret"; |
Przemek Stekiel | ae4ed30 | 2022-04-05 17:15:55 +0200 | [diff] [blame] | 5870 | seed = session_hash; |
| 5871 | handshake->calc_verify( ssl, session_hash, &seed_len ); |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5872 | |
| 5873 | MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret", |
Przemek Stekiel | ae4ed30 | 2022-04-05 17:15:55 +0200 | [diff] [blame] | 5874 | session_hash, seed_len ); |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5875 | } |
Przemek Stekiel | 169bf0b | 2022-04-29 07:53:29 +0200 | [diff] [blame] | 5876 | #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */ |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5877 | |
Przemek Stekiel | 99114f3 | 2022-04-22 11:20:09 +0200 | [diff] [blame] | 5878 | #if defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
Przemek Stekiel | 8a4b7fd | 2022-04-28 09:22:22 +0200 | [diff] [blame] | 5879 | defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Neil Armstrong | e952a30 | 2022-05-03 10:22:14 +0200 | [diff] [blame] | 5880 | if( mbedtls_ssl_ciphersuite_uses_psk( handshake->ciphersuite_info ) == 1 ) |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5881 | { |
| 5882 | /* Perform PSK-to-MS expansion in a single step. */ |
| 5883 | psa_status_t status; |
| 5884 | psa_algorithm_t alg; |
| 5885 | mbedtls_svc_key_id_t psk; |
| 5886 | psa_key_derivation_operation_t derivation = |
| 5887 | PSA_KEY_DERIVATION_OPERATION_INIT; |
| 5888 | mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac; |
| 5889 | |
| 5890 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) ); |
| 5891 | |
| 5892 | psk = mbedtls_ssl_get_opaque_psk( ssl ); |
| 5893 | |
| 5894 | if( hash_alg == MBEDTLS_MD_SHA384 ) |
| 5895 | alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384); |
| 5896 | else |
| 5897 | alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256); |
| 5898 | |
Przemek Stekiel | 51a1f36 | 2022-04-13 08:57:06 +0200 | [diff] [blame] | 5899 | size_t other_secret_len = 0; |
| 5900 | unsigned char* other_secret = NULL; |
Przemek Stekiel | c203340 | 2022-04-05 17:19:41 +0200 | [diff] [blame] | 5901 | |
Przemek Stekiel | 19b80f8 | 2022-04-14 08:29:31 +0200 | [diff] [blame] | 5902 | switch( handshake->ciphersuite_info->key_exchange ) |
Przemek Stekiel | c203340 | 2022-04-05 17:19:41 +0200 | [diff] [blame] | 5903 | { |
Przemek Stekiel | 19b80f8 | 2022-04-14 08:29:31 +0200 | [diff] [blame] | 5904 | /* Provide other secret. |
Przemek Stekiel | 51a1f36 | 2022-04-13 08:57:06 +0200 | [diff] [blame] | 5905 | * Other secret is stored in premaster, where first 2 bytes hold the |
Przemek Stekiel | 19b80f8 | 2022-04-14 08:29:31 +0200 | [diff] [blame] | 5906 | * length of the other key. |
Przemek Stekiel | c203340 | 2022-04-05 17:19:41 +0200 | [diff] [blame] | 5907 | */ |
Przemek Stekiel | 19b80f8 | 2022-04-14 08:29:31 +0200 | [diff] [blame] | 5908 | case MBEDTLS_KEY_EXCHANGE_RSA_PSK: |
Przemek Stekiel | 8abcee9 | 2022-04-28 09:16:28 +0200 | [diff] [blame] | 5909 | /* For RSA-PSK other key length is always 48 bytes. */ |
Przemek Stekiel | 19b80f8 | 2022-04-14 08:29:31 +0200 | [diff] [blame] | 5910 | other_secret_len = 48; |
| 5911 | other_secret = handshake->premaster + 2; |
| 5912 | break; |
| 5913 | case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK: |
Przemek Stekiel | b293aaa | 2022-04-19 12:22:38 +0200 | [diff] [blame] | 5914 | case MBEDTLS_KEY_EXCHANGE_DHE_PSK: |
Przemek Stekiel | 19b80f8 | 2022-04-14 08:29:31 +0200 | [diff] [blame] | 5915 | other_secret_len = MBEDTLS_GET_UINT16_BE(handshake->premaster, 0); |
| 5916 | other_secret = handshake->premaster + 2; |
| 5917 | break; |
| 5918 | default: |
| 5919 | break; |
Przemek Stekiel | c203340 | 2022-04-05 17:19:41 +0200 | [diff] [blame] | 5920 | } |
| 5921 | |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5922 | status = setup_psa_key_derivation( &derivation, psk, alg, |
Neil Armstrong | 8ecd668 | 2022-05-05 11:40:35 +0200 | [diff] [blame] | 5923 | ssl->conf->psk, ssl->conf->psk_len, |
Przemek Stekiel | ae4ed30 | 2022-04-05 17:15:55 +0200 | [diff] [blame] | 5924 | seed, seed_len, |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5925 | (unsigned char const *) lbl, |
| 5926 | (size_t) strlen( lbl ), |
Przemek Stekiel | 51a1f36 | 2022-04-13 08:57:06 +0200 | [diff] [blame] | 5927 | other_secret, other_secret_len, |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5928 | master_secret_len ); |
| 5929 | if( status != PSA_SUCCESS ) |
| 5930 | { |
| 5931 | psa_key_derivation_abort( &derivation ); |
| 5932 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 5933 | } |
| 5934 | |
| 5935 | status = psa_key_derivation_output_bytes( &derivation, |
| 5936 | master, |
| 5937 | master_secret_len ); |
| 5938 | if( status != PSA_SUCCESS ) |
| 5939 | { |
| 5940 | psa_key_derivation_abort( &derivation ); |
| 5941 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 5942 | } |
| 5943 | |
| 5944 | status = psa_key_derivation_abort( &derivation ); |
| 5945 | if( status != PSA_SUCCESS ) |
| 5946 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 5947 | } |
| 5948 | else |
| 5949 | #endif |
| 5950 | { |
| 5951 | ret = handshake->tls_prf( handshake->premaster, handshake->pmslen, |
Przemek Stekiel | ae4ed30 | 2022-04-05 17:15:55 +0200 | [diff] [blame] | 5952 | lbl, seed, seed_len, |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5953 | master, |
| 5954 | master_secret_len ); |
| 5955 | if( ret != 0 ) |
| 5956 | { |
| 5957 | MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret ); |
| 5958 | return( ret ); |
| 5959 | } |
| 5960 | |
| 5961 | MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", |
| 5962 | handshake->premaster, |
| 5963 | handshake->pmslen ); |
| 5964 | |
| 5965 | mbedtls_platform_zeroize( handshake->premaster, |
| 5966 | sizeof(handshake->premaster) ); |
| 5967 | } |
| 5968 | |
| 5969 | return( 0 ); |
| 5970 | } |
| 5971 | |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 5972 | int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl ) |
| 5973 | { |
| 5974 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 5975 | const mbedtls_ssl_ciphersuite_t * const ciphersuite_info = |
| 5976 | ssl->handshake->ciphersuite_info; |
| 5977 | |
| 5978 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) ); |
| 5979 | |
| 5980 | /* Set PRF, calc_verify and calc_finished function pointers */ |
| 5981 | ret = ssl_set_handshake_prfs( ssl->handshake, |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 5982 | ciphersuite_info->mac ); |
| 5983 | if( ret != 0 ) |
| 5984 | { |
| 5985 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret ); |
| 5986 | return( ret ); |
| 5987 | } |
| 5988 | |
| 5989 | /* Compute master secret if needed */ |
| 5990 | ret = ssl_compute_master( ssl->handshake, |
| 5991 | ssl->session_negotiate->master, |
| 5992 | ssl ); |
| 5993 | if( ret != 0 ) |
| 5994 | { |
| 5995 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret ); |
| 5996 | return( ret ); |
| 5997 | } |
| 5998 | |
| 5999 | /* Swap the client and server random values: |
| 6000 | * - MS derivation wanted client+server (RFC 5246 8.1) |
| 6001 | * - key derivation wants server+client (RFC 5246 6.3) */ |
| 6002 | { |
| 6003 | unsigned char tmp[64]; |
| 6004 | memcpy( tmp, ssl->handshake->randbytes, 64 ); |
| 6005 | memcpy( ssl->handshake->randbytes, tmp + 32, 32 ); |
| 6006 | memcpy( ssl->handshake->randbytes + 32, tmp, 32 ); |
| 6007 | mbedtls_platform_zeroize( tmp, sizeof( tmp ) ); |
| 6008 | } |
| 6009 | |
| 6010 | /* Populate transform structure */ |
| 6011 | ret = ssl_tls12_populate_transform( ssl->transform_negotiate, |
| 6012 | ssl->session_negotiate->ciphersuite, |
| 6013 | ssl->session_negotiate->master, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 6014 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 6015 | ssl->session_negotiate->encrypt_then_mac, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 6016 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 6017 | ssl->handshake->tls_prf, |
| 6018 | ssl->handshake->randbytes, |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 6019 | ssl->tls_version, |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 6020 | ssl->conf->endpoint, |
| 6021 | ssl ); |
| 6022 | if( ret != 0 ) |
| 6023 | { |
| 6024 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls12_populate_transform", ret ); |
| 6025 | return( ret ); |
| 6026 | } |
| 6027 | |
| 6028 | /* We no longer need Server/ClientHello.random values */ |
| 6029 | mbedtls_platform_zeroize( ssl->handshake->randbytes, |
| 6030 | sizeof( ssl->handshake->randbytes ) ); |
| 6031 | |
| 6032 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) ); |
| 6033 | |
| 6034 | return( 0 ); |
| 6035 | } |
Jerry Yu | 8392e0d | 2022-02-17 14:10:24 +0800 | [diff] [blame] | 6036 | |
Ronald Cron | 4dcbca9 | 2022-03-07 10:21:40 +0100 | [diff] [blame] | 6037 | int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md ) |
| 6038 | { |
Ronald Cron | 4dcbca9 | 2022-03-07 10:21:40 +0100 | [diff] [blame] | 6039 | switch( md ) |
| 6040 | { |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 6041 | #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] | 6042 | case MBEDTLS_SSL_HASH_SHA384: |
| 6043 | ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384; |
| 6044 | break; |
| 6045 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 6046 | #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] | 6047 | case MBEDTLS_SSL_HASH_SHA256: |
| 6048 | ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256; |
| 6049 | break; |
| 6050 | #endif |
| 6051 | default: |
| 6052 | return( -1 ); |
| 6053 | } |
Andrzej Kurek | eabeb30 | 2022-10-17 07:52:51 -0400 | [diff] [blame] | 6054 | #if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \ |
| 6055 | !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
| 6056 | (void) ssl; |
| 6057 | #endif |
Ronald Cron | c2f13a0 | 2022-03-07 10:25:24 +0100 | [diff] [blame] | 6058 | return( 0 ); |
Ronald Cron | 4dcbca9 | 2022-03-07 10:21:40 +0100 | [diff] [blame] | 6059 | } |
| 6060 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 6061 | #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] | 6062 | void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl, |
| 6063 | unsigned char *hash, |
| 6064 | size_t *hlen ) |
| 6065 | { |
| 6066 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 6067 | size_t hash_size; |
| 6068 | psa_status_t status; |
| 6069 | psa_hash_operation_t sha256_psa = psa_hash_operation_init(); |
| 6070 | |
| 6071 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) ); |
| 6072 | status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa ); |
| 6073 | if( status != PSA_SUCCESS ) |
| 6074 | { |
| 6075 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); |
| 6076 | return; |
| 6077 | } |
| 6078 | |
| 6079 | status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size ); |
| 6080 | if( status != PSA_SUCCESS ) |
| 6081 | { |
| 6082 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); |
| 6083 | return; |
| 6084 | } |
| 6085 | |
| 6086 | *hlen = 32; |
| 6087 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen ); |
| 6088 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) ); |
| 6089 | #else |
| 6090 | mbedtls_sha256_context sha256; |
| 6091 | |
| 6092 | mbedtls_sha256_init( &sha256 ); |
| 6093 | |
| 6094 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) ); |
| 6095 | |
| 6096 | mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 ); |
| 6097 | mbedtls_sha256_finish( &sha256, hash ); |
| 6098 | |
| 6099 | *hlen = 32; |
| 6100 | |
| 6101 | MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen ); |
| 6102 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) ); |
| 6103 | |
| 6104 | mbedtls_sha256_free( &sha256 ); |
| 6105 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 6106 | return; |
| 6107 | } |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 6108 | #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] | 6109 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 6110 | #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] | 6111 | void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl, |
| 6112 | unsigned char *hash, |
| 6113 | size_t *hlen ) |
| 6114 | { |
| 6115 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 6116 | size_t hash_size; |
| 6117 | psa_status_t status; |
| 6118 | psa_hash_operation_t sha384_psa = psa_hash_operation_init(); |
| 6119 | |
| 6120 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) ); |
| 6121 | status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa ); |
| 6122 | if( status != PSA_SUCCESS ) |
| 6123 | { |
| 6124 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); |
| 6125 | return; |
| 6126 | } |
| 6127 | |
| 6128 | status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size ); |
| 6129 | if( status != PSA_SUCCESS ) |
| 6130 | { |
| 6131 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); |
| 6132 | return; |
| 6133 | } |
| 6134 | |
| 6135 | *hlen = 48; |
| 6136 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen ); |
| 6137 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) ); |
| 6138 | #else |
| 6139 | mbedtls_sha512_context sha512; |
| 6140 | |
| 6141 | mbedtls_sha512_init( &sha512 ); |
| 6142 | |
| 6143 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) ); |
| 6144 | |
Andrzej Kurek | a242e83 | 2022-08-11 10:03:14 -0400 | [diff] [blame] | 6145 | mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 ); |
Jerry Yu | c1cb384 | 2022-02-17 14:13:48 +0800 | [diff] [blame] | 6146 | mbedtls_sha512_finish( &sha512, hash ); |
| 6147 | |
| 6148 | *hlen = 48; |
| 6149 | |
| 6150 | MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen ); |
| 6151 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) ); |
| 6152 | |
| 6153 | mbedtls_sha512_free( &sha512 ); |
| 6154 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 6155 | return; |
| 6156 | } |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 6157 | #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] | 6158 | |
Neil Armstrong | 80f6f32 | 2022-05-03 17:56:38 +0200 | [diff] [blame] | 6159 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
| 6160 | defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6161 | int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex ) |
| 6162 | { |
| 6163 | unsigned char *p = ssl->handshake->premaster; |
| 6164 | unsigned char *end = p + sizeof( ssl->handshake->premaster ); |
| 6165 | const unsigned char *psk = NULL; |
| 6166 | size_t psk_len = 0; |
Przemek Stekiel | b293aaa | 2022-04-19 12:22:38 +0200 | [diff] [blame] | 6167 | int psk_ret = mbedtls_ssl_get_psk( ssl, &psk, &psk_len ); |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6168 | |
Przemek Stekiel | b293aaa | 2022-04-19 12:22:38 +0200 | [diff] [blame] | 6169 | if( psk_ret == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED ) |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6170 | { |
| 6171 | /* |
| 6172 | * This should never happen because the existence of a PSK is always |
Przemek Stekiel | b293aaa | 2022-04-19 12:22:38 +0200 | [diff] [blame] | 6173 | * checked before calling this function. |
| 6174 | * |
| 6175 | * The exception is opaque DHE-PSK. For DHE-PSK fill premaster with |
Przemek Stekiel | 8abcee9 | 2022-04-28 09:16:28 +0200 | [diff] [blame] | 6176 | * the shared secret without PSK. |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6177 | */ |
Przemek Stekiel | b293aaa | 2022-04-19 12:22:38 +0200 | [diff] [blame] | 6178 | if ( key_ex != MBEDTLS_KEY_EXCHANGE_DHE_PSK ) |
| 6179 | { |
| 6180 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 6181 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 6182 | } |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6183 | } |
| 6184 | |
| 6185 | /* |
| 6186 | * PMS = struct { |
| 6187 | * opaque other_secret<0..2^16-1>; |
| 6188 | * opaque psk<0..2^16-1>; |
| 6189 | * }; |
| 6190 | * with "other_secret" depending on the particular key exchange |
| 6191 | */ |
| 6192 | #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) |
| 6193 | if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK ) |
| 6194 | { |
| 6195 | if( end - p < 2 ) |
| 6196 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6197 | |
| 6198 | MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 ); |
| 6199 | p += 2; |
| 6200 | |
| 6201 | if( end < p || (size_t)( end - p ) < psk_len ) |
| 6202 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6203 | |
| 6204 | memset( p, 0, psk_len ); |
| 6205 | p += psk_len; |
| 6206 | } |
| 6207 | else |
| 6208 | #endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */ |
| 6209 | #if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) |
| 6210 | if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK ) |
| 6211 | { |
| 6212 | /* |
| 6213 | * other_secret already set by the ClientKeyExchange message, |
| 6214 | * and is 48 bytes long |
| 6215 | */ |
| 6216 | if( end - p < 2 ) |
| 6217 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6218 | |
| 6219 | *p++ = 0; |
| 6220 | *p++ = 48; |
| 6221 | p += 48; |
| 6222 | } |
| 6223 | else |
| 6224 | #endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */ |
| 6225 | #if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) |
| 6226 | if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK ) |
| 6227 | { |
| 6228 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 6229 | size_t len; |
| 6230 | |
| 6231 | /* Write length only when we know the actual value */ |
| 6232 | if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx, |
| 6233 | p + 2, end - ( p + 2 ), &len, |
| 6234 | ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 ) |
| 6235 | { |
| 6236 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret ); |
| 6237 | return( ret ); |
| 6238 | } |
| 6239 | MBEDTLS_PUT_UINT16_BE( len, p, 0 ); |
| 6240 | p += 2 + len; |
| 6241 | |
| 6242 | MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K ); |
| 6243 | } |
| 6244 | else |
| 6245 | #endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */ |
Neil Armstrong | 80f6f32 | 2022-05-03 17:56:38 +0200 | [diff] [blame] | 6246 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6247 | if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ) |
| 6248 | { |
| 6249 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 6250 | size_t zlen; |
| 6251 | |
| 6252 | if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen, |
| 6253 | p + 2, end - ( p + 2 ), |
| 6254 | ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 ) |
| 6255 | { |
| 6256 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret ); |
| 6257 | return( ret ); |
| 6258 | } |
| 6259 | |
| 6260 | MBEDTLS_PUT_UINT16_BE( zlen, p, 0 ); |
| 6261 | p += 2 + zlen; |
| 6262 | |
| 6263 | MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx, |
| 6264 | MBEDTLS_DEBUG_ECDH_Z ); |
| 6265 | } |
| 6266 | else |
Neil Armstrong | 80f6f32 | 2022-05-03 17:56:38 +0200 | [diff] [blame] | 6267 | #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */ |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6268 | { |
| 6269 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 6270 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 6271 | } |
| 6272 | |
| 6273 | /* opaque psk<0..2^16-1>; */ |
| 6274 | if( end - p < 2 ) |
| 6275 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6276 | |
| 6277 | MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 ); |
| 6278 | p += 2; |
| 6279 | |
| 6280 | if( end < p || (size_t)( end - p ) < psk_len ) |
| 6281 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6282 | |
| 6283 | memcpy( p, psk, psk_len ); |
| 6284 | p += psk_len; |
| 6285 | |
| 6286 | ssl->handshake->pmslen = p - ssl->handshake->premaster; |
| 6287 | |
| 6288 | return( 0 ); |
| 6289 | } |
Neil Armstrong | 80f6f32 | 2022-05-03 17:56:38 +0200 | [diff] [blame] | 6290 | #endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ |
Jerry Yu | c2c673d | 2022-02-17 14:20:39 +0800 | [diff] [blame] | 6291 | |
| 6292 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6293 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | c2c673d | 2022-02-17 14:20:39 +0800 | [diff] [blame] | 6294 | static int ssl_write_hello_request( mbedtls_ssl_context *ssl ); |
| 6295 | |
| 6296 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 6297 | int mbedtls_ssl_resend_hello_request( mbedtls_ssl_context *ssl ) |
| 6298 | { |
| 6299 | /* If renegotiation is not enforced, retransmit until we would reach max |
| 6300 | * timeout if we were using the usual handshake doubling scheme */ |
| 6301 | if( ssl->conf->renego_max_records < 0 ) |
| 6302 | { |
| 6303 | uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1; |
| 6304 | unsigned char doublings = 1; |
| 6305 | |
| 6306 | while( ratio != 0 ) |
| 6307 | { |
| 6308 | ++doublings; |
| 6309 | ratio >>= 1; |
| 6310 | } |
| 6311 | |
| 6312 | if( ++ssl->renego_records_seen > doublings ) |
| 6313 | { |
| 6314 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) ); |
| 6315 | return( 0 ); |
| 6316 | } |
| 6317 | } |
| 6318 | |
| 6319 | return( ssl_write_hello_request( ssl ) ); |
| 6320 | } |
| 6321 | #endif |
| 6322 | #endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */ |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6323 | |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6324 | /* |
| 6325 | * Handshake functions |
| 6326 | */ |
| 6327 | #if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
| 6328 | /* No certificate support -> dummy functions */ |
| 6329 | int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl ) |
| 6330 | { |
| 6331 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 6332 | ssl->handshake->ciphersuite_info; |
| 6333 | |
| 6334 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) ); |
| 6335 | |
| 6336 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) |
| 6337 | { |
| 6338 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) ); |
| 6339 | ssl->state++; |
| 6340 | return( 0 ); |
| 6341 | } |
| 6342 | |
| 6343 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 6344 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 6345 | } |
| 6346 | |
| 6347 | int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl ) |
| 6348 | { |
| 6349 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 6350 | ssl->handshake->ciphersuite_info; |
| 6351 | |
| 6352 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) ); |
| 6353 | |
| 6354 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) |
| 6355 | { |
| 6356 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) ); |
| 6357 | ssl->state++; |
| 6358 | return( 0 ); |
| 6359 | } |
| 6360 | |
| 6361 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 6362 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 6363 | } |
| 6364 | |
| 6365 | #else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 6366 | /* Some certificate support -> implement write and parse */ |
| 6367 | |
| 6368 | int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl ) |
| 6369 | { |
| 6370 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
| 6371 | size_t i, n; |
| 6372 | const mbedtls_x509_crt *crt; |
| 6373 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 6374 | ssl->handshake->ciphersuite_info; |
| 6375 | |
| 6376 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) ); |
| 6377 | |
| 6378 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) |
| 6379 | { |
| 6380 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) ); |
| 6381 | ssl->state++; |
| 6382 | return( 0 ); |
| 6383 | } |
| 6384 | |
| 6385 | #if defined(MBEDTLS_SSL_CLI_C) |
| 6386 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 6387 | { |
| 6388 | if( ssl->handshake->client_auth == 0 ) |
| 6389 | { |
| 6390 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) ); |
| 6391 | ssl->state++; |
| 6392 | return( 0 ); |
| 6393 | } |
| 6394 | } |
| 6395 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 6396 | #if defined(MBEDTLS_SSL_SRV_C) |
| 6397 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 6398 | { |
| 6399 | if( mbedtls_ssl_own_cert( ssl ) == NULL ) |
| 6400 | { |
| 6401 | /* Should never happen because we shouldn't have picked the |
| 6402 | * ciphersuite if we don't have a certificate. */ |
| 6403 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 6404 | } |
| 6405 | } |
| 6406 | #endif |
| 6407 | |
| 6408 | MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) ); |
| 6409 | |
| 6410 | /* |
| 6411 | * 0 . 0 handshake type |
| 6412 | * 1 . 3 handshake length |
| 6413 | * 4 . 6 length of all certs |
| 6414 | * 7 . 9 length of cert. 1 |
| 6415 | * 10 . n-1 peer certificate |
| 6416 | * n . n+2 length of cert. 2 |
| 6417 | * n+3 . ... upper level cert, etc. |
| 6418 | */ |
| 6419 | i = 7; |
| 6420 | crt = mbedtls_ssl_own_cert( ssl ); |
| 6421 | |
| 6422 | while( crt != NULL ) |
| 6423 | { |
| 6424 | n = crt->raw.len; |
| 6425 | if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i ) |
| 6426 | { |
| 6427 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %" MBEDTLS_PRINTF_SIZET |
| 6428 | " > %" MBEDTLS_PRINTF_SIZET, |
| 6429 | i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN ) ); |
| 6430 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 6431 | } |
| 6432 | |
| 6433 | ssl->out_msg[i ] = MBEDTLS_BYTE_2( n ); |
| 6434 | ssl->out_msg[i + 1] = MBEDTLS_BYTE_1( n ); |
| 6435 | ssl->out_msg[i + 2] = MBEDTLS_BYTE_0( n ); |
| 6436 | |
| 6437 | i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n ); |
| 6438 | i += n; crt = crt->next; |
| 6439 | } |
| 6440 | |
| 6441 | ssl->out_msg[4] = MBEDTLS_BYTE_2( i - 7 ); |
| 6442 | ssl->out_msg[5] = MBEDTLS_BYTE_1( i - 7 ); |
| 6443 | ssl->out_msg[6] = MBEDTLS_BYTE_0( i - 7 ); |
| 6444 | |
| 6445 | ssl->out_msglen = i; |
| 6446 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 6447 | ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE; |
| 6448 | |
| 6449 | ssl->state++; |
| 6450 | |
| 6451 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
| 6452 | { |
| 6453 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); |
| 6454 | return( ret ); |
| 6455 | } |
| 6456 | |
| 6457 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) ); |
| 6458 | |
| 6459 | return( ret ); |
| 6460 | } |
| 6461 | |
| 6462 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) |
| 6463 | |
| 6464 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6465 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6466 | static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl, |
| 6467 | unsigned char *crt_buf, |
| 6468 | size_t crt_buf_len ) |
| 6469 | { |
| 6470 | mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert; |
| 6471 | |
| 6472 | if( peer_crt == NULL ) |
| 6473 | return( -1 ); |
| 6474 | |
| 6475 | if( peer_crt->raw.len != crt_buf_len ) |
| 6476 | return( -1 ); |
| 6477 | |
| 6478 | return( memcmp( peer_crt->raw.p, crt_buf, peer_crt->raw.len ) ); |
| 6479 | } |
| 6480 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6481 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6482 | static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl, |
| 6483 | unsigned char *crt_buf, |
| 6484 | size_t crt_buf_len ) |
| 6485 | { |
| 6486 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 6487 | unsigned char const * const peer_cert_digest = |
| 6488 | ssl->session->peer_cert_digest; |
| 6489 | mbedtls_md_type_t const peer_cert_digest_type = |
| 6490 | ssl->session->peer_cert_digest_type; |
| 6491 | mbedtls_md_info_t const * const digest_info = |
| 6492 | mbedtls_md_info_from_type( peer_cert_digest_type ); |
| 6493 | unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN]; |
| 6494 | size_t digest_len; |
| 6495 | |
| 6496 | if( peer_cert_digest == NULL || digest_info == NULL ) |
| 6497 | return( -1 ); |
| 6498 | |
| 6499 | digest_len = mbedtls_md_get_size( digest_info ); |
| 6500 | if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN ) |
| 6501 | return( -1 ); |
| 6502 | |
| 6503 | ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest ); |
| 6504 | if( ret != 0 ) |
| 6505 | return( -1 ); |
| 6506 | |
| 6507 | return( memcmp( tmp_digest, peer_cert_digest, digest_len ) ); |
| 6508 | } |
| 6509 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 6510 | #endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */ |
| 6511 | |
| 6512 | /* |
| 6513 | * Once the certificate message is read, parse it into a cert chain and |
| 6514 | * perform basic checks, but leave actual verification to the caller |
| 6515 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6516 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6517 | static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl, |
| 6518 | mbedtls_x509_crt *chain ) |
| 6519 | { |
| 6520 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 6521 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) |
| 6522 | int crt_cnt=0; |
| 6523 | #endif |
| 6524 | size_t i, n; |
| 6525 | uint8_t alert; |
| 6526 | |
| 6527 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) |
| 6528 | { |
| 6529 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
| 6530 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6531 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
| 6532 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
| 6533 | } |
| 6534 | |
| 6535 | if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ) |
| 6536 | { |
| 6537 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6538 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
| 6539 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
| 6540 | } |
| 6541 | |
| 6542 | if( ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 ) |
| 6543 | { |
| 6544 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
| 6545 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6546 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
| 6547 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
| 6548 | } |
| 6549 | |
| 6550 | i = mbedtls_ssl_hs_hdr_len( ssl ); |
| 6551 | |
| 6552 | /* |
| 6553 | * Same message structure as in mbedtls_ssl_write_certificate() |
| 6554 | */ |
| 6555 | n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2]; |
| 6556 | |
| 6557 | if( ssl->in_msg[i] != 0 || |
| 6558 | ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) ) |
| 6559 | { |
| 6560 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
| 6561 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6562 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
| 6563 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
| 6564 | } |
| 6565 | |
| 6566 | /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */ |
| 6567 | i += 3; |
| 6568 | |
| 6569 | /* Iterate through and parse the CRTs in the provided chain. */ |
| 6570 | while( i < ssl->in_hslen ) |
| 6571 | { |
| 6572 | /* Check that there's room for the next CRT's length fields. */ |
| 6573 | if ( i + 3 > ssl->in_hslen ) { |
| 6574 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
| 6575 | mbedtls_ssl_send_alert_message( ssl, |
| 6576 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6577 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
| 6578 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
| 6579 | } |
| 6580 | /* In theory, the CRT can be up to 2**24 Bytes, but we don't support |
| 6581 | * anything beyond 2**16 ~ 64K. */ |
| 6582 | if( ssl->in_msg[i] != 0 ) |
| 6583 | { |
| 6584 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
| 6585 | mbedtls_ssl_send_alert_message( ssl, |
| 6586 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6587 | MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT ); |
| 6588 | return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE ); |
| 6589 | } |
| 6590 | |
| 6591 | /* Read length of the next CRT in the chain. */ |
| 6592 | n = ( (unsigned int) ssl->in_msg[i + 1] << 8 ) |
| 6593 | | (unsigned int) ssl->in_msg[i + 2]; |
| 6594 | i += 3; |
| 6595 | |
| 6596 | if( n < 128 || i + n > ssl->in_hslen ) |
| 6597 | { |
| 6598 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
| 6599 | mbedtls_ssl_send_alert_message( ssl, |
| 6600 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6601 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
| 6602 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
| 6603 | } |
| 6604 | |
| 6605 | /* Check if we're handling the first CRT in the chain. */ |
| 6606 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) |
| 6607 | if( crt_cnt++ == 0 && |
| 6608 | ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
| 6609 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
| 6610 | { |
| 6611 | /* During client-side renegotiation, check that the server's |
| 6612 | * end-CRTs hasn't changed compared to the initial handshake, |
| 6613 | * mitigating the triple handshake attack. On success, reuse |
| 6614 | * the original end-CRT instead of parsing it again. */ |
| 6615 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) ); |
| 6616 | if( ssl_check_peer_crt_unchanged( ssl, |
| 6617 | &ssl->in_msg[i], |
| 6618 | n ) != 0 ) |
| 6619 | { |
| 6620 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) ); |
| 6621 | mbedtls_ssl_send_alert_message( ssl, |
| 6622 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6623 | MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED ); |
| 6624 | return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE ); |
| 6625 | } |
| 6626 | |
| 6627 | /* Now we can safely free the original chain. */ |
| 6628 | ssl_clear_peer_cert( ssl->session ); |
| 6629 | } |
| 6630 | #endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */ |
| 6631 | |
| 6632 | /* Parse the next certificate in the chain. */ |
| 6633 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 6634 | ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n ); |
| 6635 | #else |
| 6636 | /* If we don't need to store the CRT chain permanently, parse |
| 6637 | * it in-place from the input buffer instead of making a copy. */ |
| 6638 | ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n ); |
| 6639 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 6640 | switch( ret ) |
| 6641 | { |
| 6642 | case 0: /*ok*/ |
| 6643 | case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND: |
| 6644 | /* Ignore certificate with an unknown algorithm: maybe a |
| 6645 | prior certificate was already trusted. */ |
| 6646 | break; |
| 6647 | |
| 6648 | case MBEDTLS_ERR_X509_ALLOC_FAILED: |
| 6649 | alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR; |
| 6650 | goto crt_parse_der_failed; |
| 6651 | |
| 6652 | case MBEDTLS_ERR_X509_UNKNOWN_VERSION: |
| 6653 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 6654 | goto crt_parse_der_failed; |
| 6655 | |
| 6656 | default: |
| 6657 | alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT; |
| 6658 | crt_parse_der_failed: |
| 6659 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert ); |
| 6660 | MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret ); |
| 6661 | return( ret ); |
| 6662 | } |
| 6663 | |
| 6664 | i += n; |
| 6665 | } |
| 6666 | |
| 6667 | MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain ); |
| 6668 | return( 0 ); |
| 6669 | } |
| 6670 | |
| 6671 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6672 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6673 | static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl ) |
| 6674 | { |
| 6675 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 6676 | return( -1 ); |
| 6677 | |
| 6678 | if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) && |
| 6679 | ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 6680 | ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE && |
| 6681 | memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 ) |
| 6682 | { |
Ronald Cron | 1938588 | 2022-06-15 16:26:13 +0200 | [diff] [blame] | 6683 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "peer has no certificate" ) ); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6684 | return( 0 ); |
| 6685 | } |
| 6686 | return( -1 ); |
| 6687 | } |
| 6688 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 6689 | |
| 6690 | /* Check if a certificate message is expected. |
| 6691 | * Return either |
| 6692 | * - SSL_CERTIFICATE_EXPECTED, or |
| 6693 | * - SSL_CERTIFICATE_SKIP |
| 6694 | * indicating whether a Certificate message is expected or not. |
| 6695 | */ |
| 6696 | #define SSL_CERTIFICATE_EXPECTED 0 |
| 6697 | #define SSL_CERTIFICATE_SKIP 1 |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6698 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6699 | static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl, |
| 6700 | int authmode ) |
| 6701 | { |
| 6702 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 6703 | ssl->handshake->ciphersuite_info; |
| 6704 | |
| 6705 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) |
| 6706 | return( SSL_CERTIFICATE_SKIP ); |
| 6707 | |
| 6708 | #if defined(MBEDTLS_SSL_SRV_C) |
| 6709 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 6710 | { |
| 6711 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ) |
| 6712 | return( SSL_CERTIFICATE_SKIP ); |
| 6713 | |
| 6714 | if( authmode == MBEDTLS_SSL_VERIFY_NONE ) |
| 6715 | { |
| 6716 | ssl->session_negotiate->verify_result = |
| 6717 | MBEDTLS_X509_BADCERT_SKIP_VERIFY; |
| 6718 | return( SSL_CERTIFICATE_SKIP ); |
| 6719 | } |
| 6720 | } |
| 6721 | #else |
| 6722 | ((void) authmode); |
| 6723 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 6724 | |
| 6725 | return( SSL_CERTIFICATE_EXPECTED ); |
| 6726 | } |
| 6727 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6728 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6729 | static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl, |
| 6730 | int authmode, |
| 6731 | mbedtls_x509_crt *chain, |
| 6732 | void *rs_ctx ) |
| 6733 | { |
| 6734 | int ret = 0; |
| 6735 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 6736 | ssl->handshake->ciphersuite_info; |
| 6737 | int have_ca_chain = 0; |
| 6738 | |
| 6739 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *); |
| 6740 | void *p_vrfy; |
| 6741 | |
| 6742 | if( authmode == MBEDTLS_SSL_VERIFY_NONE ) |
| 6743 | return( 0 ); |
| 6744 | |
| 6745 | if( ssl->f_vrfy != NULL ) |
| 6746 | { |
| 6747 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) ); |
| 6748 | f_vrfy = ssl->f_vrfy; |
| 6749 | p_vrfy = ssl->p_vrfy; |
| 6750 | } |
| 6751 | else |
| 6752 | { |
| 6753 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) ); |
| 6754 | f_vrfy = ssl->conf->f_vrfy; |
| 6755 | p_vrfy = ssl->conf->p_vrfy; |
| 6756 | } |
| 6757 | |
| 6758 | /* |
| 6759 | * Main check: verify certificate |
| 6760 | */ |
| 6761 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 6762 | if( ssl->conf->f_ca_cb != NULL ) |
| 6763 | { |
| 6764 | ((void) rs_ctx); |
| 6765 | have_ca_chain = 1; |
| 6766 | |
| 6767 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) ); |
| 6768 | ret = mbedtls_x509_crt_verify_with_ca_cb( |
| 6769 | chain, |
| 6770 | ssl->conf->f_ca_cb, |
| 6771 | ssl->conf->p_ca_cb, |
| 6772 | ssl->conf->cert_profile, |
| 6773 | ssl->hostname, |
| 6774 | &ssl->session_negotiate->verify_result, |
| 6775 | f_vrfy, p_vrfy ); |
| 6776 | } |
| 6777 | else |
| 6778 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
| 6779 | { |
| 6780 | mbedtls_x509_crt *ca_chain; |
| 6781 | mbedtls_x509_crl *ca_crl; |
| 6782 | |
| 6783 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 6784 | if( ssl->handshake->sni_ca_chain != NULL ) |
| 6785 | { |
| 6786 | ca_chain = ssl->handshake->sni_ca_chain; |
| 6787 | ca_crl = ssl->handshake->sni_ca_crl; |
| 6788 | } |
| 6789 | else |
| 6790 | #endif |
| 6791 | { |
| 6792 | ca_chain = ssl->conf->ca_chain; |
| 6793 | ca_crl = ssl->conf->ca_crl; |
| 6794 | } |
| 6795 | |
| 6796 | if( ca_chain != NULL ) |
| 6797 | have_ca_chain = 1; |
| 6798 | |
| 6799 | ret = mbedtls_x509_crt_verify_restartable( |
| 6800 | chain, |
| 6801 | ca_chain, ca_crl, |
| 6802 | ssl->conf->cert_profile, |
| 6803 | ssl->hostname, |
| 6804 | &ssl->session_negotiate->verify_result, |
| 6805 | f_vrfy, p_vrfy, rs_ctx ); |
| 6806 | } |
| 6807 | |
| 6808 | if( ret != 0 ) |
| 6809 | { |
| 6810 | MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret ); |
| 6811 | } |
| 6812 | |
| 6813 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
| 6814 | if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS ) |
| 6815 | return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS ); |
| 6816 | #endif |
| 6817 | |
| 6818 | /* |
| 6819 | * Secondary checks: always done, but change 'ret' only if it was 0 |
| 6820 | */ |
| 6821 | |
| 6822 | #if defined(MBEDTLS_ECP_C) |
| 6823 | { |
| 6824 | const mbedtls_pk_context *pk = &chain->pk; |
| 6825 | |
Manuel Pégourié-Gonnard | 66b0d61 | 2022-06-17 10:49:29 +0200 | [diff] [blame] | 6826 | /* If certificate uses an EC key, make sure the curve is OK. |
| 6827 | * This is a public key, so it can't be opaque, so can_do() is a good |
| 6828 | * enough check to ensure pk_ec() is safe to use here. */ |
Leonid Rozenboim | 19e5973 | 2022-08-08 16:52:38 -0700 | [diff] [blame] | 6829 | if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) ) |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6830 | { |
Leonid Rozenboim | 19e5973 | 2022-08-08 16:52:38 -0700 | [diff] [blame] | 6831 | /* and in the unlikely case the above assumption no longer holds |
| 6832 | * we are making sure that pk_ec() here does not return a NULL |
| 6833 | */ |
| 6834 | const mbedtls_ecp_keypair *ec = mbedtls_pk_ec( *pk ); |
| 6835 | if( ec == NULL ) |
| 6836 | { |
Tom Cosgrove | 20c1137 | 2022-08-24 15:06:13 +0100 | [diff] [blame] | 6837 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_pk_ec() returned NULL" ) ); |
Leonid Rozenboim | 19e5973 | 2022-08-08 16:52:38 -0700 | [diff] [blame] | 6838 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 6839 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6840 | |
Leonid Rozenboim | 19e5973 | 2022-08-08 16:52:38 -0700 | [diff] [blame] | 6841 | if( mbedtls_ssl_check_curve( ssl, ec->grp.id ) != 0 ) |
| 6842 | { |
| 6843 | ssl->session_negotiate->verify_result |= |
| 6844 | MBEDTLS_X509_BADCERT_BAD_KEY; |
| 6845 | |
| 6846 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) ); |
| 6847 | if( ret == 0 ) |
| 6848 | ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE; |
| 6849 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6850 | } |
| 6851 | } |
| 6852 | #endif /* MBEDTLS_ECP_C */ |
| 6853 | |
| 6854 | if( mbedtls_ssl_check_cert_usage( chain, |
| 6855 | ciphersuite_info, |
| 6856 | ! ssl->conf->endpoint, |
| 6857 | &ssl->session_negotiate->verify_result ) != 0 ) |
| 6858 | { |
| 6859 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) ); |
| 6860 | if( ret == 0 ) |
| 6861 | ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE; |
| 6862 | } |
| 6863 | |
| 6864 | /* mbedtls_x509_crt_verify_with_profile is supposed to report a |
| 6865 | * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED, |
| 6866 | * with details encoded in the verification flags. All other kinds |
| 6867 | * of error codes, including those from the user provided f_vrfy |
| 6868 | * functions, are treated as fatal and lead to a failure of |
| 6869 | * ssl_parse_certificate even if verification was optional. */ |
| 6870 | if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL && |
| 6871 | ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED || |
| 6872 | ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE ) ) |
| 6873 | { |
| 6874 | ret = 0; |
| 6875 | } |
| 6876 | |
| 6877 | if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED ) |
| 6878 | { |
| 6879 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) ); |
| 6880 | ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED; |
| 6881 | } |
| 6882 | |
| 6883 | if( ret != 0 ) |
| 6884 | { |
| 6885 | uint8_t alert; |
| 6886 | |
| 6887 | /* The certificate may have been rejected for several reasons. |
| 6888 | Pick one and send the corresponding alert. Which alert to send |
| 6889 | may be a subject of debate in some cases. */ |
| 6890 | if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER ) |
| 6891 | alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED; |
| 6892 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH ) |
| 6893 | alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT; |
| 6894 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE ) |
| 6895 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 6896 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE ) |
| 6897 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 6898 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE ) |
| 6899 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 6900 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK ) |
| 6901 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 6902 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY ) |
| 6903 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 6904 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED ) |
| 6905 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED; |
| 6906 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED ) |
| 6907 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED; |
| 6908 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED ) |
| 6909 | alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA; |
| 6910 | else |
| 6911 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN; |
| 6912 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6913 | alert ); |
| 6914 | } |
| 6915 | |
| 6916 | #if defined(MBEDTLS_DEBUG_C) |
| 6917 | if( ssl->session_negotiate->verify_result != 0 ) |
| 6918 | { |
| 6919 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %08x", |
| 6920 | (unsigned int) ssl->session_negotiate->verify_result ) ); |
| 6921 | } |
| 6922 | else |
| 6923 | { |
| 6924 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) ); |
| 6925 | } |
| 6926 | #endif /* MBEDTLS_DEBUG_C */ |
| 6927 | |
| 6928 | return( ret ); |
| 6929 | } |
| 6930 | |
| 6931 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6932 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6933 | static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl, |
| 6934 | unsigned char *start, size_t len ) |
| 6935 | { |
| 6936 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 6937 | /* Remember digest of the peer's end-CRT. */ |
| 6938 | ssl->session_negotiate->peer_cert_digest = |
| 6939 | mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ); |
| 6940 | if( ssl->session_negotiate->peer_cert_digest == NULL ) |
| 6941 | { |
| 6942 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", |
| 6943 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ); |
| 6944 | mbedtls_ssl_send_alert_message( ssl, |
| 6945 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6946 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
| 6947 | |
| 6948 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 6949 | } |
| 6950 | |
| 6951 | ret = mbedtls_md( mbedtls_md_info_from_type( |
| 6952 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ), |
| 6953 | start, len, |
| 6954 | ssl->session_negotiate->peer_cert_digest ); |
| 6955 | |
| 6956 | ssl->session_negotiate->peer_cert_digest_type = |
| 6957 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE; |
| 6958 | ssl->session_negotiate->peer_cert_digest_len = |
| 6959 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN; |
| 6960 | |
| 6961 | return( ret ); |
| 6962 | } |
| 6963 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6964 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6965 | static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl, |
| 6966 | unsigned char *start, size_t len ) |
| 6967 | { |
| 6968 | unsigned char *end = start + len; |
| 6969 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 6970 | |
| 6971 | /* Make a copy of the peer's raw public key. */ |
| 6972 | mbedtls_pk_init( &ssl->handshake->peer_pubkey ); |
| 6973 | ret = mbedtls_pk_parse_subpubkey( &start, end, |
| 6974 | &ssl->handshake->peer_pubkey ); |
| 6975 | if( ret != 0 ) |
| 6976 | { |
| 6977 | /* We should have parsed the public key before. */ |
| 6978 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 6979 | } |
| 6980 | |
| 6981 | return( 0 ); |
| 6982 | } |
| 6983 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 6984 | |
| 6985 | int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl ) |
| 6986 | { |
| 6987 | int ret = 0; |
| 6988 | int crt_expected; |
| 6989 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 6990 | const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET |
| 6991 | ? ssl->handshake->sni_authmode |
| 6992 | : ssl->conf->authmode; |
| 6993 | #else |
| 6994 | const int authmode = ssl->conf->authmode; |
| 6995 | #endif |
| 6996 | void *rs_ctx = NULL; |
| 6997 | mbedtls_x509_crt *chain = NULL; |
| 6998 | |
| 6999 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) ); |
| 7000 | |
| 7001 | crt_expected = ssl_parse_certificate_coordinate( ssl, authmode ); |
| 7002 | if( crt_expected == SSL_CERTIFICATE_SKIP ) |
| 7003 | { |
| 7004 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) ); |
| 7005 | goto exit; |
| 7006 | } |
| 7007 | |
| 7008 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
| 7009 | if( ssl->handshake->ecrs_enabled && |
| 7010 | ssl->handshake->ecrs_state == ssl_ecrs_crt_verify ) |
| 7011 | { |
| 7012 | chain = ssl->handshake->ecrs_peer_cert; |
| 7013 | ssl->handshake->ecrs_peer_cert = NULL; |
| 7014 | goto crt_verify; |
| 7015 | } |
| 7016 | #endif |
| 7017 | |
| 7018 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) |
| 7019 | { |
| 7020 | /* mbedtls_ssl_read_record may have sent an alert already. We |
| 7021 | let it decide whether to alert. */ |
| 7022 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
| 7023 | goto exit; |
| 7024 | } |
| 7025 | |
| 7026 | #if defined(MBEDTLS_SSL_SRV_C) |
| 7027 | if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 ) |
| 7028 | { |
| 7029 | ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING; |
| 7030 | |
| 7031 | if( authmode != MBEDTLS_SSL_VERIFY_OPTIONAL ) |
| 7032 | ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE; |
| 7033 | |
| 7034 | goto exit; |
| 7035 | } |
| 7036 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 7037 | |
| 7038 | /* Clear existing peer CRT structure in case we tried to |
| 7039 | * reuse a session but it failed, and allocate a new one. */ |
| 7040 | ssl_clear_peer_cert( ssl->session_negotiate ); |
| 7041 | |
| 7042 | chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ); |
| 7043 | if( chain == NULL ) |
| 7044 | { |
| 7045 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", |
| 7046 | sizeof( mbedtls_x509_crt ) ) ); |
| 7047 | mbedtls_ssl_send_alert_message( ssl, |
| 7048 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7049 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
| 7050 | |
| 7051 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 7052 | goto exit; |
| 7053 | } |
| 7054 | mbedtls_x509_crt_init( chain ); |
| 7055 | |
| 7056 | ret = ssl_parse_certificate_chain( ssl, chain ); |
| 7057 | if( ret != 0 ) |
| 7058 | goto exit; |
| 7059 | |
| 7060 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
| 7061 | if( ssl->handshake->ecrs_enabled) |
| 7062 | ssl->handshake->ecrs_state = ssl_ecrs_crt_verify; |
| 7063 | |
| 7064 | crt_verify: |
| 7065 | if( ssl->handshake->ecrs_enabled) |
| 7066 | rs_ctx = &ssl->handshake->ecrs_ctx; |
| 7067 | #endif |
| 7068 | |
| 7069 | ret = ssl_parse_certificate_verify( ssl, authmode, |
| 7070 | chain, rs_ctx ); |
| 7071 | if( ret != 0 ) |
| 7072 | goto exit; |
| 7073 | |
| 7074 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 7075 | { |
| 7076 | unsigned char *crt_start, *pk_start; |
| 7077 | size_t crt_len, pk_len; |
| 7078 | |
| 7079 | /* We parse the CRT chain without copying, so |
| 7080 | * these pointers point into the input buffer, |
| 7081 | * and are hence still valid after freeing the |
| 7082 | * CRT chain. */ |
| 7083 | |
| 7084 | crt_start = chain->raw.p; |
| 7085 | crt_len = chain->raw.len; |
| 7086 | |
| 7087 | pk_start = chain->pk_raw.p; |
| 7088 | pk_len = chain->pk_raw.len; |
| 7089 | |
| 7090 | /* Free the CRT structures before computing |
| 7091 | * digest and copying the peer's public key. */ |
| 7092 | mbedtls_x509_crt_free( chain ); |
| 7093 | mbedtls_free( chain ); |
| 7094 | chain = NULL; |
| 7095 | |
| 7096 | ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len ); |
| 7097 | if( ret != 0 ) |
| 7098 | goto exit; |
| 7099 | |
| 7100 | ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len ); |
| 7101 | if( ret != 0 ) |
| 7102 | goto exit; |
| 7103 | } |
| 7104 | #else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 7105 | /* Pass ownership to session structure. */ |
| 7106 | ssl->session_negotiate->peer_cert = chain; |
| 7107 | chain = NULL; |
| 7108 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 7109 | |
| 7110 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) ); |
| 7111 | |
| 7112 | exit: |
| 7113 | |
| 7114 | if( ret == 0 ) |
| 7115 | ssl->state++; |
| 7116 | |
| 7117 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
| 7118 | if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS ) |
| 7119 | { |
| 7120 | ssl->handshake->ecrs_peer_cert = chain; |
| 7121 | chain = NULL; |
| 7122 | } |
| 7123 | #endif |
| 7124 | |
| 7125 | if( chain != NULL ) |
| 7126 | { |
| 7127 | mbedtls_x509_crt_free( chain ); |
| 7128 | mbedtls_free( chain ); |
| 7129 | } |
| 7130 | |
| 7131 | return( ret ); |
| 7132 | } |
| 7133 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 7134 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 7135 | #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] | 7136 | static void ssl_calc_finished_tls_sha256( |
| 7137 | mbedtls_ssl_context *ssl, unsigned char *buf, int from ) |
| 7138 | { |
| 7139 | int len = 12; |
| 7140 | const char *sender; |
| 7141 | unsigned char padbuf[32]; |
| 7142 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7143 | size_t hash_size; |
| 7144 | psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT; |
| 7145 | psa_status_t status; |
| 7146 | #else |
| 7147 | mbedtls_sha256_context sha256; |
| 7148 | #endif |
| 7149 | |
| 7150 | mbedtls_ssl_session *session = ssl->session_negotiate; |
| 7151 | if( !session ) |
| 7152 | session = ssl->session; |
| 7153 | |
| 7154 | sender = ( from == MBEDTLS_SSL_IS_CLIENT ) |
| 7155 | ? "client finished" |
| 7156 | : "server finished"; |
| 7157 | |
| 7158 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7159 | sha256_psa = psa_hash_operation_init(); |
| 7160 | |
| 7161 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) ); |
| 7162 | |
| 7163 | status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa ); |
| 7164 | if( status != PSA_SUCCESS ) |
| 7165 | { |
| 7166 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); |
| 7167 | return; |
| 7168 | } |
| 7169 | |
| 7170 | status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size ); |
| 7171 | if( status != PSA_SUCCESS ) |
| 7172 | { |
| 7173 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); |
| 7174 | return; |
| 7175 | } |
| 7176 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 ); |
| 7177 | #else |
| 7178 | |
| 7179 | mbedtls_sha256_init( &sha256 ); |
| 7180 | |
| 7181 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) ); |
| 7182 | |
| 7183 | mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 ); |
| 7184 | |
| 7185 | /* |
| 7186 | * TLSv1.2: |
| 7187 | * hash = PRF( master, finished_label, |
| 7188 | * Hash( handshake ) )[0.11] |
| 7189 | */ |
| 7190 | |
| 7191 | #if !defined(MBEDTLS_SHA256_ALT) |
| 7192 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *) |
| 7193 | sha256.state, sizeof( sha256.state ) ); |
| 7194 | #endif |
| 7195 | |
| 7196 | mbedtls_sha256_finish( &sha256, padbuf ); |
| 7197 | mbedtls_sha256_free( &sha256 ); |
| 7198 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 7199 | |
| 7200 | ssl->handshake->tls_prf( session->master, 48, sender, |
| 7201 | padbuf, 32, buf, len ); |
| 7202 | |
| 7203 | MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len ); |
| 7204 | |
| 7205 | mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) ); |
| 7206 | |
| 7207 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) ); |
| 7208 | } |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 7209 | #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] | 7210 | |
Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 7211 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 7212 | #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] | 7213 | static void ssl_calc_finished_tls_sha384( |
| 7214 | mbedtls_ssl_context *ssl, unsigned char *buf, int from ) |
| 7215 | { |
| 7216 | int len = 12; |
| 7217 | const char *sender; |
| 7218 | unsigned char padbuf[48]; |
| 7219 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7220 | size_t hash_size; |
| 7221 | psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT; |
| 7222 | psa_status_t status; |
| 7223 | #else |
| 7224 | mbedtls_sha512_context sha512; |
| 7225 | #endif |
| 7226 | |
| 7227 | mbedtls_ssl_session *session = ssl->session_negotiate; |
| 7228 | if( !session ) |
| 7229 | session = ssl->session; |
| 7230 | |
| 7231 | sender = ( from == MBEDTLS_SSL_IS_CLIENT ) |
| 7232 | ? "client finished" |
| 7233 | : "server finished"; |
| 7234 | |
| 7235 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7236 | sha384_psa = psa_hash_operation_init(); |
| 7237 | |
| 7238 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) ); |
| 7239 | |
| 7240 | status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa ); |
| 7241 | if( status != PSA_SUCCESS ) |
| 7242 | { |
| 7243 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); |
| 7244 | return; |
| 7245 | } |
| 7246 | |
| 7247 | status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size ); |
| 7248 | if( status != PSA_SUCCESS ) |
| 7249 | { |
| 7250 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); |
| 7251 | return; |
| 7252 | } |
| 7253 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 ); |
| 7254 | #else |
| 7255 | mbedtls_sha512_init( &sha512 ); |
| 7256 | |
| 7257 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) ); |
| 7258 | |
Andrzej Kurek | a242e83 | 2022-08-11 10:03:14 -0400 | [diff] [blame] | 7259 | mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 ); |
Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 7260 | |
| 7261 | /* |
| 7262 | * TLSv1.2: |
| 7263 | * hash = PRF( master, finished_label, |
| 7264 | * Hash( handshake ) )[0.11] |
| 7265 | */ |
| 7266 | |
| 7267 | #if !defined(MBEDTLS_SHA512_ALT) |
| 7268 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *) |
| 7269 | sha512.state, sizeof( sha512.state ) ); |
| 7270 | #endif |
| 7271 | mbedtls_sha512_finish( &sha512, padbuf ); |
| 7272 | |
| 7273 | mbedtls_sha512_free( &sha512 ); |
| 7274 | #endif |
| 7275 | |
| 7276 | ssl->handshake->tls_prf( session->master, 48, sender, |
| 7277 | padbuf, 48, buf, len ); |
| 7278 | |
| 7279 | MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len ); |
| 7280 | |
| 7281 | mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) ); |
| 7282 | |
| 7283 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) ); |
| 7284 | } |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 7285 | #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] | 7286 | |
Jerry Yu | aef0015 | 2022-02-17 14:27:31 +0800 | [diff] [blame] | 7287 | void mbedtls_ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl ) |
| 7288 | { |
| 7289 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) ); |
| 7290 | |
| 7291 | /* |
| 7292 | * Free our handshake params |
| 7293 | */ |
| 7294 | mbedtls_ssl_handshake_free( ssl ); |
| 7295 | mbedtls_free( ssl->handshake ); |
| 7296 | ssl->handshake = NULL; |
| 7297 | |
| 7298 | /* |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 7299 | * Free the previous transform and switch in the current one |
Jerry Yu | aef0015 | 2022-02-17 14:27:31 +0800 | [diff] [blame] | 7300 | */ |
| 7301 | if( ssl->transform ) |
| 7302 | { |
| 7303 | mbedtls_ssl_transform_free( ssl->transform ); |
| 7304 | mbedtls_free( ssl->transform ); |
| 7305 | } |
| 7306 | ssl->transform = ssl->transform_negotiate; |
| 7307 | ssl->transform_negotiate = NULL; |
| 7308 | |
| 7309 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) ); |
| 7310 | } |
| 7311 | |
Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 7312 | void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl ) |
| 7313 | { |
| 7314 | int resume = ssl->handshake->resume; |
| 7315 | |
| 7316 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) ); |
| 7317 | |
| 7318 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 7319 | if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
| 7320 | { |
| 7321 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE; |
| 7322 | ssl->renego_records_seen = 0; |
| 7323 | } |
| 7324 | #endif |
| 7325 | |
| 7326 | /* |
| 7327 | * Free the previous session and switch in the current one |
| 7328 | */ |
| 7329 | if( ssl->session ) |
| 7330 | { |
| 7331 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 7332 | /* RFC 7366 3.1: keep the EtM state */ |
| 7333 | ssl->session_negotiate->encrypt_then_mac = |
| 7334 | ssl->session->encrypt_then_mac; |
| 7335 | #endif |
| 7336 | |
| 7337 | mbedtls_ssl_session_free( ssl->session ); |
| 7338 | mbedtls_free( ssl->session ); |
| 7339 | } |
| 7340 | ssl->session = ssl->session_negotiate; |
| 7341 | ssl->session_negotiate = NULL; |
| 7342 | |
| 7343 | /* |
| 7344 | * Add cache entry |
| 7345 | */ |
| 7346 | if( ssl->conf->f_set_cache != NULL && |
| 7347 | ssl->session->id_len != 0 && |
| 7348 | resume == 0 ) |
| 7349 | { |
| 7350 | if( ssl->conf->f_set_cache( ssl->conf->p_cache, |
| 7351 | ssl->session->id, |
| 7352 | ssl->session->id_len, |
| 7353 | ssl->session ) != 0 ) |
| 7354 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) ); |
| 7355 | } |
| 7356 | |
| 7357 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 7358 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 7359 | ssl->handshake->flight != NULL ) |
| 7360 | { |
| 7361 | /* Cancel handshake timer */ |
| 7362 | mbedtls_ssl_set_timer( ssl, 0 ); |
| 7363 | |
| 7364 | /* Keep last flight around in case we need to resend it: |
| 7365 | * we need the handshake and transform structures for that */ |
| 7366 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) ); |
| 7367 | } |
| 7368 | else |
| 7369 | #endif |
| 7370 | mbedtls_ssl_handshake_wrapup_free_hs_transform( ssl ); |
| 7371 | |
| 7372 | ssl->state++; |
| 7373 | |
| 7374 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) ); |
| 7375 | } |
| 7376 | |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 7377 | int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl ) |
| 7378 | { |
| 7379 | int ret, hash_len; |
| 7380 | |
| 7381 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) ); |
| 7382 | |
| 7383 | mbedtls_ssl_update_out_pointers( ssl, ssl->transform_negotiate ); |
| 7384 | |
| 7385 | ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint ); |
| 7386 | |
| 7387 | /* |
| 7388 | * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites |
| 7389 | * may define some other value. Currently (early 2016), no defined |
| 7390 | * ciphersuite does this (and this is unlikely to change as activity has |
| 7391 | * moved to TLS 1.3 now) so we can keep the hardcoded 12 here. |
| 7392 | */ |
| 7393 | hash_len = 12; |
| 7394 | |
| 7395 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 7396 | ssl->verify_data_len = hash_len; |
| 7397 | memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len ); |
| 7398 | #endif |
| 7399 | |
| 7400 | ssl->out_msglen = 4 + hash_len; |
| 7401 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 7402 | ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED; |
| 7403 | |
| 7404 | /* |
| 7405 | * In case of session resuming, invert the client and server |
| 7406 | * ChangeCipherSpec messages order. |
| 7407 | */ |
| 7408 | if( ssl->handshake->resume != 0 ) |
| 7409 | { |
| 7410 | #if defined(MBEDTLS_SSL_CLI_C) |
| 7411 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 7412 | ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP; |
| 7413 | #endif |
| 7414 | #if defined(MBEDTLS_SSL_SRV_C) |
| 7415 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 7416 | ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC; |
| 7417 | #endif |
| 7418 | } |
| 7419 | else |
| 7420 | ssl->state++; |
| 7421 | |
| 7422 | /* |
| 7423 | * Switch to our negotiated transform and session parameters for outbound |
| 7424 | * data. |
| 7425 | */ |
| 7426 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) ); |
| 7427 | |
| 7428 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 7429 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 7430 | { |
| 7431 | unsigned char i; |
| 7432 | |
| 7433 | /* Remember current epoch settings for resending */ |
| 7434 | ssl->handshake->alt_transform_out = ssl->transform_out; |
| 7435 | memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, |
| 7436 | sizeof( ssl->handshake->alt_out_ctr ) ); |
| 7437 | |
| 7438 | /* Set sequence_number to zero */ |
| 7439 | memset( &ssl->cur_out_ctr[2], 0, sizeof( ssl->cur_out_ctr ) - 2 ); |
| 7440 | |
| 7441 | |
| 7442 | /* Increment epoch */ |
| 7443 | for( i = 2; i > 0; i-- ) |
| 7444 | if( ++ssl->cur_out_ctr[i - 1] != 0 ) |
| 7445 | break; |
| 7446 | |
| 7447 | /* The loop goes to its end iff the counter is wrapping */ |
| 7448 | if( i == 0 ) |
| 7449 | { |
| 7450 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) ); |
| 7451 | return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING ); |
| 7452 | } |
| 7453 | } |
| 7454 | else |
| 7455 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 7456 | memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) ); |
| 7457 | |
| 7458 | ssl->transform_out = ssl->transform_negotiate; |
| 7459 | ssl->session_out = ssl->session_negotiate; |
| 7460 | |
| 7461 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 7462 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 7463 | mbedtls_ssl_send_flight_completed( ssl ); |
| 7464 | #endif |
| 7465 | |
| 7466 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
| 7467 | { |
| 7468 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); |
| 7469 | return( ret ); |
| 7470 | } |
| 7471 | |
| 7472 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 7473 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 7474 | ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 ) |
| 7475 | { |
| 7476 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret ); |
| 7477 | return( ret ); |
| 7478 | } |
| 7479 | #endif |
| 7480 | |
| 7481 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) ); |
| 7482 | |
| 7483 | return( 0 ); |
| 7484 | } |
| 7485 | |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 7486 | #define SSL_MAX_HASH_LEN 12 |
| 7487 | |
| 7488 | int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl ) |
| 7489 | { |
| 7490 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 7491 | unsigned int hash_len = 12; |
| 7492 | unsigned char buf[SSL_MAX_HASH_LEN]; |
| 7493 | |
| 7494 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) ); |
| 7495 | |
| 7496 | ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 ); |
| 7497 | |
| 7498 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) |
| 7499 | { |
| 7500 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
| 7501 | goto exit; |
| 7502 | } |
| 7503 | |
| 7504 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) |
| 7505 | { |
| 7506 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) ); |
| 7507 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7508 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
| 7509 | ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; |
| 7510 | goto exit; |
| 7511 | } |
| 7512 | |
| 7513 | if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ) |
| 7514 | { |
| 7515 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7516 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
| 7517 | ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; |
| 7518 | goto exit; |
| 7519 | } |
| 7520 | |
| 7521 | if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len ) |
| 7522 | { |
| 7523 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) ); |
| 7524 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7525 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
| 7526 | ret = MBEDTLS_ERR_SSL_DECODE_ERROR; |
| 7527 | goto exit; |
| 7528 | } |
| 7529 | |
| 7530 | if( mbedtls_ct_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), |
| 7531 | buf, hash_len ) != 0 ) |
| 7532 | { |
| 7533 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) ); |
| 7534 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7535 | MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR ); |
| 7536 | ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE; |
| 7537 | goto exit; |
| 7538 | } |
| 7539 | |
| 7540 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 7541 | ssl->verify_data_len = hash_len; |
| 7542 | memcpy( ssl->peer_verify_data, buf, hash_len ); |
| 7543 | #endif |
| 7544 | |
| 7545 | if( ssl->handshake->resume != 0 ) |
| 7546 | { |
| 7547 | #if defined(MBEDTLS_SSL_CLI_C) |
| 7548 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 7549 | ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC; |
| 7550 | #endif |
| 7551 | #if defined(MBEDTLS_SSL_SRV_C) |
| 7552 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 7553 | ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP; |
| 7554 | #endif |
| 7555 | } |
| 7556 | else |
| 7557 | ssl->state++; |
| 7558 | |
| 7559 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 7560 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 7561 | mbedtls_ssl_recv_flight_completed( ssl ); |
| 7562 | #endif |
| 7563 | |
| 7564 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) ); |
| 7565 | |
| 7566 | exit: |
| 7567 | mbedtls_platform_zeroize( buf, hash_len ); |
| 7568 | return( ret ); |
| 7569 | } |
| 7570 | |
Jerry Yu | 392112c | 2022-02-17 14:34:10 +0800 | [diff] [blame] | 7571 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
| 7572 | /* |
| 7573 | * Helper to get TLS 1.2 PRF from ciphersuite |
| 7574 | * (Duplicates bits of logic from ssl_set_handshake_prfs().) |
| 7575 | */ |
| 7576 | static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id ) |
| 7577 | { |
Jerry Yu | 392112c | 2022-02-17 14:34:10 +0800 | [diff] [blame] | 7578 | const mbedtls_ssl_ciphersuite_t * const ciphersuite_info = |
Andrzej Kurek | 6832774 | 2022-10-03 06:18:18 -0400 | [diff] [blame] | 7579 | mbedtls_ssl_ciphersuite_from_id( ciphersuite_id ); |
| 7580 | #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] | 7581 | if( ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA384 ) |
Jerry Yu | 392112c | 2022-02-17 14:34:10 +0800 | [diff] [blame] | 7582 | return( tls_prf_sha384 ); |
Andrzej Kurek | 894edde | 2022-09-29 06:31:14 -0400 | [diff] [blame] | 7583 | else |
Jerry Yu | 392112c | 2022-02-17 14:34:10 +0800 | [diff] [blame] | 7584 | #endif |
Andrzej Kurek | 894edde | 2022-09-29 06:31:14 -0400 | [diff] [blame] | 7585 | #if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
| 7586 | { |
| 7587 | if( ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA256 ) |
| 7588 | return( tls_prf_sha256 ); |
| 7589 | } |
| 7590 | #endif |
| 7591 | #if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \ |
| 7592 | !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
| 7593 | (void) ciphersuite_info; |
| 7594 | #endif |
Andrzej Kurek | eabeb30 | 2022-10-17 07:52:51 -0400 | [diff] [blame] | 7595 | |
Andrzej Kurek | 894edde | 2022-09-29 06:31:14 -0400 | [diff] [blame] | 7596 | return( NULL ); |
Jerry Yu | 392112c | 2022-02-17 14:34:10 +0800 | [diff] [blame] | 7597 | } |
| 7598 | #endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */ |
Jerry Yu | e93ffcd | 2022-02-17 14:37:06 +0800 | [diff] [blame] | 7599 | |
| 7600 | static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf ) |
| 7601 | { |
| 7602 | ((void) tls_prf); |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 7603 | #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] | 7604 | if( tls_prf == tls_prf_sha384 ) |
| 7605 | { |
| 7606 | return( MBEDTLS_SSL_TLS_PRF_SHA384 ); |
| 7607 | } |
| 7608 | else |
| 7609 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 7610 | #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] | 7611 | if( tls_prf == tls_prf_sha256 ) |
| 7612 | { |
| 7613 | return( MBEDTLS_SSL_TLS_PRF_SHA256 ); |
| 7614 | } |
| 7615 | else |
| 7616 | #endif |
| 7617 | return( MBEDTLS_SSL_TLS_PRF_NONE ); |
| 7618 | } |
| 7619 | |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7620 | /* |
| 7621 | * Populate a transform structure with session keys and all the other |
| 7622 | * necessary information. |
| 7623 | * |
| 7624 | * Parameters: |
| 7625 | * - [in/out]: transform: structure to populate |
| 7626 | * [in] must be just initialised with mbedtls_ssl_transform_init() |
| 7627 | * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf() |
| 7628 | * - [in] ciphersuite |
| 7629 | * - [in] master |
| 7630 | * - [in] encrypt_then_mac |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7631 | * - [in] tls_prf: pointer to PRF to use for key derivation |
| 7632 | * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random |
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 7633 | * - [in] tls_version: TLS version |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7634 | * - [in] endpoint: client or server |
| 7635 | * - [in] ssl: used for: |
| 7636 | * - ssl->conf->{f,p}_export_keys |
| 7637 | * [in] optionally used for: |
| 7638 | * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg |
| 7639 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 7640 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7641 | static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform, |
| 7642 | int ciphersuite, |
| 7643 | const unsigned char master[48], |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 7644 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7645 | int encrypt_then_mac, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 7646 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7647 | ssl_tls_prf_t tls_prf, |
| 7648 | const unsigned char randbytes[64], |
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 7649 | mbedtls_ssl_protocol_version tls_version, |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7650 | unsigned endpoint, |
| 7651 | const mbedtls_ssl_context *ssl ) |
| 7652 | { |
| 7653 | int ret = 0; |
| 7654 | unsigned char keyblk[256]; |
| 7655 | unsigned char *key1; |
| 7656 | unsigned char *key2; |
| 7657 | unsigned char *mac_enc; |
| 7658 | unsigned char *mac_dec; |
| 7659 | size_t mac_key_len = 0; |
| 7660 | size_t iv_copy_len; |
| 7661 | size_t keylen; |
| 7662 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info; |
Neil Armstrong | 7fea33e | 2022-04-01 15:40:25 +0200 | [diff] [blame] | 7663 | mbedtls_ssl_mode_t ssl_mode; |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7664 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7665 | const mbedtls_cipher_info_t *cipher_info; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7666 | const mbedtls_md_info_t *md_info; |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7667 | #endif /* !MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7668 | |
| 7669 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7670 | psa_key_type_t key_type; |
| 7671 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7672 | psa_algorithm_t alg; |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7673 | psa_algorithm_t mac_alg = 0; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7674 | size_t key_bits; |
| 7675 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 7676 | #endif |
| 7677 | |
| 7678 | #if !defined(MBEDTLS_DEBUG_C) && \ |
| 7679 | !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 7680 | if( ssl->f_export_keys == NULL ) |
| 7681 | { |
| 7682 | ssl = NULL; /* make sure we don't use it except for these cases */ |
| 7683 | (void) ssl; |
| 7684 | } |
| 7685 | #endif |
| 7686 | |
| 7687 | /* |
| 7688 | * Some data just needs copying into the structure |
| 7689 | */ |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 7690 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7691 | transform->encrypt_then_mac = encrypt_then_mac; |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 7692 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 7693 | transform->tls_version = tls_version; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7694 | |
| 7695 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
| 7696 | memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) ); |
| 7697 | #endif |
| 7698 | |
| 7699 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 7700 | if( tls_version == MBEDTLS_SSL_VERSION_TLS1_3 ) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7701 | { |
| 7702 | /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform |
| 7703 | * generation separate. This should never happen. */ |
| 7704 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 7705 | } |
| 7706 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 7707 | |
| 7708 | /* |
| 7709 | * Get various info structures |
| 7710 | */ |
| 7711 | ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite ); |
| 7712 | if( ciphersuite_info == NULL ) |
| 7713 | { |
| 7714 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found", |
| 7715 | ciphersuite ) ); |
| 7716 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 7717 | } |
| 7718 | |
Neil Armstrong | ab555e0 | 2022-04-04 11:07:59 +0200 | [diff] [blame] | 7719 | ssl_mode = mbedtls_ssl_get_mode_from_ciphersuite( |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 7720 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Neil Armstrong | 7fea33e | 2022-04-01 15:40:25 +0200 | [diff] [blame] | 7721 | encrypt_then_mac, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 7722 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Neil Armstrong | 7fea33e | 2022-04-01 15:40:25 +0200 | [diff] [blame] | 7723 | ciphersuite_info ); |
| 7724 | |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7725 | if( ssl_mode == MBEDTLS_SSL_MODE_AEAD ) |
| 7726 | transform->taglen = |
| 7727 | ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16; |
| 7728 | |
| 7729 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7730 | if( ( status = mbedtls_ssl_cipher_to_psa( ciphersuite_info->cipher, |
| 7731 | transform->taglen, |
| 7732 | &alg, |
| 7733 | &key_type, |
| 7734 | &key_bits ) ) != PSA_SUCCESS ) |
| 7735 | { |
| 7736 | ret = psa_ssl_status_to_mbedtls( status ); |
| 7737 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_cipher_to_psa", ret ); |
| 7738 | goto end; |
| 7739 | } |
| 7740 | #else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7741 | cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher ); |
| 7742 | if( cipher_info == NULL ) |
| 7743 | { |
| 7744 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %u not found", |
| 7745 | ciphersuite_info->cipher ) ); |
| 7746 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 7747 | } |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7748 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7749 | |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7750 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Manuel Pégourié-Gonnard | abac037 | 2022-07-18 13:41:11 +0200 | [diff] [blame] | 7751 | mac_alg = mbedtls_hash_info_psa_from_md( ciphersuite_info->mac ); |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7752 | if( mac_alg == 0 ) |
| 7753 | { |
Manuel Pégourié-Gonnard | abac037 | 2022-07-18 13:41:11 +0200 | [diff] [blame] | 7754 | 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] | 7755 | (unsigned) ciphersuite_info->mac ) ); |
| 7756 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 7757 | } |
| 7758 | #else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7759 | md_info = mbedtls_md_info_from_type( ciphersuite_info->mac ); |
| 7760 | if( md_info == NULL ) |
| 7761 | { |
| 7762 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %u not found", |
| 7763 | (unsigned) ciphersuite_info->mac ) ); |
| 7764 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 7765 | } |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7766 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7767 | |
| 7768 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 7769 | /* Copy own and peer's CID if the use of the CID |
| 7770 | * extension has been negotiated. */ |
| 7771 | if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED ) |
| 7772 | { |
| 7773 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) ); |
| 7774 | |
| 7775 | transform->in_cid_len = ssl->own_cid_len; |
| 7776 | memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len ); |
| 7777 | MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid, |
| 7778 | transform->in_cid_len ); |
| 7779 | |
| 7780 | transform->out_cid_len = ssl->handshake->peer_cid_len; |
| 7781 | memcpy( transform->out_cid, ssl->handshake->peer_cid, |
| 7782 | ssl->handshake->peer_cid_len ); |
| 7783 | MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid, |
| 7784 | transform->out_cid_len ); |
| 7785 | } |
| 7786 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 7787 | |
| 7788 | /* |
| 7789 | * Compute key block using the PRF |
| 7790 | */ |
| 7791 | ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 ); |
| 7792 | if( ret != 0 ) |
| 7793 | { |
| 7794 | MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret ); |
| 7795 | return( ret ); |
| 7796 | } |
| 7797 | |
| 7798 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s", |
| 7799 | mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) ); |
| 7800 | MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 ); |
| 7801 | MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 ); |
| 7802 | MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 ); |
| 7803 | |
| 7804 | /* |
| 7805 | * Determine the appropriate key, IV and MAC length. |
| 7806 | */ |
| 7807 | |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7808 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7809 | keylen = PSA_BITS_TO_BYTES(key_bits); |
| 7810 | #else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7811 | keylen = mbedtls_cipher_info_get_key_bitlen( cipher_info ) / 8; |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7812 | #endif |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7813 | |
| 7814 | #if defined(MBEDTLS_GCM_C) || \ |
| 7815 | defined(MBEDTLS_CCM_C) || \ |
| 7816 | defined(MBEDTLS_CHACHAPOLY_C) |
Neil Armstrong | 7fea33e | 2022-04-01 15:40:25 +0200 | [diff] [blame] | 7817 | if( ssl_mode == MBEDTLS_SSL_MODE_AEAD ) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7818 | { |
| 7819 | size_t explicit_ivlen; |
| 7820 | |
| 7821 | transform->maclen = 0; |
| 7822 | mac_key_len = 0; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7823 | |
| 7824 | /* All modes haves 96-bit IVs, but the length of the static parts vary |
| 7825 | * with mode and version: |
| 7826 | * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes |
| 7827 | * (to be concatenated with a dynamically chosen IV of 8 Bytes) |
| 7828 | * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's |
| 7829 | * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record |
| 7830 | * sequence number). |
| 7831 | */ |
| 7832 | transform->ivlen = 12; |
David Horstmann | 3b2276a | 2022-10-06 14:49:08 +0100 | [diff] [blame] | 7833 | |
| 7834 | int is_chachapoly = 0; |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7835 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
David Horstmann | 3b2276a | 2022-10-06 14:49:08 +0100 | [diff] [blame] | 7836 | is_chachapoly = ( key_type == PSA_KEY_TYPE_CHACHA20 ); |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7837 | #else |
David Horstmann | 3b2276a | 2022-10-06 14:49:08 +0100 | [diff] [blame] | 7838 | is_chachapoly = ( mbedtls_cipher_info_get_mode( cipher_info ) |
| 7839 | == MBEDTLS_MODE_CHACHAPOLY ); |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7840 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
David Horstmann | 3b2276a | 2022-10-06 14:49:08 +0100 | [diff] [blame] | 7841 | |
| 7842 | if( is_chachapoly ) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7843 | transform->fixed_ivlen = 12; |
| 7844 | else |
| 7845 | transform->fixed_ivlen = 4; |
| 7846 | |
| 7847 | /* Minimum length of encrypted record */ |
| 7848 | explicit_ivlen = transform->ivlen - transform->fixed_ivlen; |
| 7849 | transform->minlen = explicit_ivlen + transform->taglen; |
| 7850 | } |
| 7851 | else |
| 7852 | #endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */ |
| 7853 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
Neil Armstrong | 7fea33e | 2022-04-01 15:40:25 +0200 | [diff] [blame] | 7854 | if( ssl_mode == MBEDTLS_SSL_MODE_STREAM || |
| 7855 | ssl_mode == MBEDTLS_SSL_MODE_CBC || |
| 7856 | ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM ) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7857 | { |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7858 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Neil Armstrong | d1be767 | 2022-04-04 11:21:41 +0200 | [diff] [blame] | 7859 | size_t block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ); |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7860 | #else |
| 7861 | size_t block_size = cipher_info->block_size; |
| 7862 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 7863 | |
| 7864 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7865 | /* Get MAC length */ |
| 7866 | mac_key_len = PSA_HASH_LENGTH(mac_alg); |
| 7867 | #else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7868 | /* Initialize HMAC contexts */ |
| 7869 | if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 || |
| 7870 | ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 ) |
| 7871 | { |
| 7872 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret ); |
| 7873 | goto end; |
| 7874 | } |
| 7875 | |
| 7876 | /* Get MAC length */ |
| 7877 | mac_key_len = mbedtls_md_get_size( md_info ); |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7878 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7879 | transform->maclen = mac_key_len; |
| 7880 | |
| 7881 | /* IV length */ |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7882 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Neil Armstrong | 2230e6c | 2022-04-27 10:36:14 +0200 | [diff] [blame] | 7883 | transform->ivlen = PSA_CIPHER_IV_LENGTH( key_type, alg ); |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7884 | #else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7885 | transform->ivlen = cipher_info->iv_size; |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7886 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7887 | |
| 7888 | /* Minimum length */ |
Neil Armstrong | 7fea33e | 2022-04-01 15:40:25 +0200 | [diff] [blame] | 7889 | if( ssl_mode == MBEDTLS_SSL_MODE_STREAM ) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7890 | transform->minlen = transform->maclen; |
| 7891 | else |
| 7892 | { |
| 7893 | /* |
| 7894 | * GenericBlockCipher: |
| 7895 | * 1. if EtM is in use: one block plus MAC |
| 7896 | * otherwise: * first multiple of blocklen greater than maclen |
| 7897 | * 2. IV |
| 7898 | */ |
| 7899 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Neil Armstrong | 7fea33e | 2022-04-01 15:40:25 +0200 | [diff] [blame] | 7900 | if( ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM ) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7901 | { |
| 7902 | transform->minlen = transform->maclen |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7903 | + block_size; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7904 | } |
| 7905 | else |
| 7906 | #endif |
| 7907 | { |
| 7908 | transform->minlen = transform->maclen |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7909 | + block_size |
| 7910 | - transform->maclen % block_size; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7911 | } |
| 7912 | |
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 7913 | if( tls_version == MBEDTLS_SSL_VERSION_TLS1_2 ) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7914 | { |
| 7915 | transform->minlen += transform->ivlen; |
| 7916 | } |
| 7917 | else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7918 | { |
| 7919 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 7920 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 7921 | goto end; |
| 7922 | } |
| 7923 | } |
| 7924 | } |
| 7925 | else |
| 7926 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
| 7927 | { |
| 7928 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 7929 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 7930 | } |
| 7931 | |
| 7932 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u", |
| 7933 | (unsigned) keylen, |
| 7934 | (unsigned) transform->minlen, |
| 7935 | (unsigned) transform->ivlen, |
| 7936 | (unsigned) transform->maclen ) ); |
| 7937 | |
| 7938 | /* |
| 7939 | * Finally setup the cipher contexts, IVs and MAC secrets. |
| 7940 | */ |
| 7941 | #if defined(MBEDTLS_SSL_CLI_C) |
| 7942 | if( endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 7943 | { |
| 7944 | key1 = keyblk + mac_key_len * 2; |
| 7945 | key2 = keyblk + mac_key_len * 2 + keylen; |
| 7946 | |
| 7947 | mac_enc = keyblk; |
| 7948 | mac_dec = keyblk + mac_key_len; |
| 7949 | |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7950 | iv_copy_len = ( transform->fixed_ivlen ) ? |
| 7951 | transform->fixed_ivlen : transform->ivlen; |
| 7952 | memcpy( transform->iv_enc, key2 + keylen, iv_copy_len ); |
| 7953 | memcpy( transform->iv_dec, key2 + keylen + iv_copy_len, |
| 7954 | iv_copy_len ); |
| 7955 | } |
| 7956 | else |
| 7957 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 7958 | #if defined(MBEDTLS_SSL_SRV_C) |
| 7959 | if( endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 7960 | { |
| 7961 | key1 = keyblk + mac_key_len * 2 + keylen; |
| 7962 | key2 = keyblk + mac_key_len * 2; |
| 7963 | |
| 7964 | mac_enc = keyblk + mac_key_len; |
| 7965 | mac_dec = keyblk; |
| 7966 | |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7967 | iv_copy_len = ( transform->fixed_ivlen ) ? |
| 7968 | transform->fixed_ivlen : transform->ivlen; |
| 7969 | memcpy( transform->iv_dec, key1 + keylen, iv_copy_len ); |
| 7970 | memcpy( transform->iv_enc, key1 + keylen + iv_copy_len, |
| 7971 | iv_copy_len ); |
| 7972 | } |
| 7973 | else |
| 7974 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 7975 | { |
| 7976 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 7977 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 7978 | goto end; |
| 7979 | } |
| 7980 | |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7981 | if( ssl != NULL && ssl->f_export_keys != NULL ) |
| 7982 | { |
| 7983 | ssl->f_export_keys( ssl->p_export_keys, |
| 7984 | MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET, |
| 7985 | master, 48, |
| 7986 | randbytes + 32, |
| 7987 | randbytes, |
| 7988 | tls_prf_get_type( tls_prf ) ); |
| 7989 | } |
| 7990 | |
| 7991 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7992 | transform->psa_alg = alg; |
| 7993 | |
| 7994 | if ( alg != MBEDTLS_SSL_NULL_CIPHER ) |
| 7995 | { |
| 7996 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 7997 | psa_set_key_algorithm( &attributes, alg ); |
| 7998 | psa_set_key_type( &attributes, key_type ); |
| 7999 | |
| 8000 | if( ( status = psa_import_key( &attributes, |
| 8001 | key1, |
| 8002 | PSA_BITS_TO_BYTES( key_bits ), |
| 8003 | &transform->psa_key_enc ) ) != PSA_SUCCESS ) |
| 8004 | { |
| 8005 | MBEDTLS_SSL_DEBUG_RET( 3, "psa_import_key", (int)status ); |
| 8006 | ret = psa_ssl_status_to_mbedtls( status ); |
| 8007 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret ); |
| 8008 | goto end; |
| 8009 | } |
| 8010 | |
| 8011 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 8012 | |
| 8013 | if( ( status = psa_import_key( &attributes, |
| 8014 | key2, |
| 8015 | PSA_BITS_TO_BYTES( key_bits ), |
| 8016 | &transform->psa_key_dec ) ) != PSA_SUCCESS ) |
| 8017 | { |
| 8018 | ret = psa_ssl_status_to_mbedtls( status ); |
| 8019 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret ); |
| 8020 | goto end; |
| 8021 | } |
| 8022 | } |
| 8023 | #else |
| 8024 | if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc, |
| 8025 | cipher_info ) ) != 0 ) |
| 8026 | { |
| 8027 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret ); |
| 8028 | goto end; |
| 8029 | } |
| 8030 | |
| 8031 | if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec, |
| 8032 | cipher_info ) ) != 0 ) |
| 8033 | { |
| 8034 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret ); |
| 8035 | goto end; |
| 8036 | } |
| 8037 | |
| 8038 | if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1, |
| 8039 | (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ), |
| 8040 | MBEDTLS_ENCRYPT ) ) != 0 ) |
| 8041 | { |
| 8042 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret ); |
| 8043 | goto end; |
| 8044 | } |
| 8045 | |
| 8046 | if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2, |
| 8047 | (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ), |
| 8048 | MBEDTLS_DECRYPT ) ) != 0 ) |
| 8049 | { |
| 8050 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret ); |
| 8051 | goto end; |
| 8052 | } |
| 8053 | |
| 8054 | #if defined(MBEDTLS_CIPHER_MODE_CBC) |
| 8055 | if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CBC ) |
| 8056 | { |
| 8057 | if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc, |
| 8058 | MBEDTLS_PADDING_NONE ) ) != 0 ) |
| 8059 | { |
| 8060 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret ); |
| 8061 | goto end; |
| 8062 | } |
| 8063 | |
| 8064 | if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec, |
| 8065 | MBEDTLS_PADDING_NONE ) ) != 0 ) |
| 8066 | { |
| 8067 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret ); |
| 8068 | goto end; |
| 8069 | } |
| 8070 | } |
| 8071 | #endif /* MBEDTLS_CIPHER_MODE_CBC */ |
| 8072 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 8073 | |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 8074 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
| 8075 | /* For HMAC-based ciphersuites, initialize the HMAC transforms. |
| 8076 | For AEAD-based ciphersuites, there is nothing to do here. */ |
| 8077 | if( mac_key_len != 0 ) |
| 8078 | { |
| 8079 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 8080 | transform->psa_mac_alg = PSA_ALG_HMAC( mac_alg ); |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 8081 | |
| 8082 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE ); |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 8083 | psa_set_key_algorithm( &attributes, PSA_ALG_HMAC( mac_alg ) ); |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 8084 | psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC ); |
| 8085 | |
| 8086 | if( ( status = psa_import_key( &attributes, |
| 8087 | mac_enc, mac_key_len, |
| 8088 | &transform->psa_mac_enc ) ) != PSA_SUCCESS ) |
| 8089 | { |
| 8090 | ret = psa_ssl_status_to_mbedtls( status ); |
| 8091 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret ); |
| 8092 | goto end; |
| 8093 | } |
| 8094 | |
Ronald Cron | fb39f15 | 2022-03-25 14:36:28 +0100 | [diff] [blame] | 8095 | if( ( transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER ) || |
Andrzej Kurek | 8c95ac4 | 2022-08-17 16:17:00 -0400 | [diff] [blame] | 8096 | ( ( transform->psa_alg == PSA_ALG_CBC_NO_PADDING ) |
| 8097 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
| 8098 | && ( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED ) |
| 8099 | #endif |
| 8100 | ) ) |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 8101 | /* mbedtls_ct_hmac() requires the key to be exportable */ |
| 8102 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT | |
| 8103 | PSA_KEY_USAGE_VERIFY_HASH ); |
| 8104 | else |
| 8105 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH ); |
| 8106 | |
| 8107 | if( ( status = psa_import_key( &attributes, |
| 8108 | mac_dec, mac_key_len, |
| 8109 | &transform->psa_mac_dec ) ) != PSA_SUCCESS ) |
| 8110 | { |
| 8111 | ret = psa_ssl_status_to_mbedtls( status ); |
| 8112 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret ); |
| 8113 | goto end; |
| 8114 | } |
| 8115 | #else |
| 8116 | ret = mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len ); |
| 8117 | if( ret != 0 ) |
| 8118 | goto end; |
| 8119 | ret = mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len ); |
| 8120 | if( ret != 0 ) |
| 8121 | goto end; |
| 8122 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 8123 | } |
| 8124 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
| 8125 | |
| 8126 | ((void) mac_dec); |
| 8127 | ((void) mac_enc); |
| 8128 | |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8129 | end: |
| 8130 | mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) ); |
| 8131 | return( ret ); |
| 8132 | } |
| 8133 | |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 8134 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 8135 | int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl, |
| 8136 | unsigned char *hash, size_t *hashlen, |
| 8137 | unsigned char *data, size_t data_len, |
| 8138 | mbedtls_md_type_t md_alg ) |
| 8139 | { |
| 8140 | psa_status_t status; |
| 8141 | psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT; |
Manuel Pégourié-Gonnard | abac037 | 2022-07-18 13:41:11 +0200 | [diff] [blame] | 8142 | 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] | 8143 | |
| 8144 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) ); |
| 8145 | |
| 8146 | if( ( status = psa_hash_setup( &hash_operation, |
| 8147 | hash_alg ) ) != PSA_SUCCESS ) |
| 8148 | { |
| 8149 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status ); |
| 8150 | goto exit; |
| 8151 | } |
| 8152 | |
| 8153 | if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes, |
| 8154 | 64 ) ) != PSA_SUCCESS ) |
| 8155 | { |
| 8156 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status ); |
| 8157 | goto exit; |
| 8158 | } |
| 8159 | |
| 8160 | if( ( status = psa_hash_update( &hash_operation, |
| 8161 | data, data_len ) ) != PSA_SUCCESS ) |
| 8162 | { |
| 8163 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status ); |
| 8164 | goto exit; |
| 8165 | } |
| 8166 | |
| 8167 | if( ( status = psa_hash_finish( &hash_operation, hash, PSA_HASH_MAX_SIZE, |
| 8168 | hashlen ) ) != PSA_SUCCESS ) |
| 8169 | { |
| 8170 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status ); |
| 8171 | goto exit; |
| 8172 | } |
| 8173 | |
| 8174 | exit: |
| 8175 | if( status != PSA_SUCCESS ) |
| 8176 | { |
| 8177 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 8178 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
| 8179 | switch( status ) |
| 8180 | { |
| 8181 | case PSA_ERROR_NOT_SUPPORTED: |
| 8182 | return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE ); |
| 8183 | case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */ |
| 8184 | case PSA_ERROR_BUFFER_TOO_SMALL: |
| 8185 | return( MBEDTLS_ERR_MD_BAD_INPUT_DATA ); |
| 8186 | case PSA_ERROR_INSUFFICIENT_MEMORY: |
| 8187 | return( MBEDTLS_ERR_MD_ALLOC_FAILED ); |
| 8188 | default: |
| 8189 | return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED ); |
| 8190 | } |
| 8191 | } |
| 8192 | return( 0 ); |
| 8193 | } |
| 8194 | |
| 8195 | #else |
| 8196 | |
| 8197 | int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl, |
| 8198 | unsigned char *hash, size_t *hashlen, |
| 8199 | unsigned char *data, size_t data_len, |
| 8200 | mbedtls_md_type_t md_alg ) |
| 8201 | { |
| 8202 | int ret = 0; |
| 8203 | mbedtls_md_context_t ctx; |
| 8204 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg ); |
| 8205 | *hashlen = mbedtls_md_get_size( md_info ); |
| 8206 | |
| 8207 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) ); |
| 8208 | |
| 8209 | mbedtls_md_init( &ctx ); |
| 8210 | |
| 8211 | /* |
| 8212 | * digitally-signed struct { |
| 8213 | * opaque client_random[32]; |
| 8214 | * opaque server_random[32]; |
| 8215 | * ServerDHParams params; |
| 8216 | * }; |
| 8217 | */ |
| 8218 | if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 ) |
| 8219 | { |
| 8220 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret ); |
| 8221 | goto exit; |
| 8222 | } |
| 8223 | if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 ) |
| 8224 | { |
| 8225 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret ); |
| 8226 | goto exit; |
| 8227 | } |
| 8228 | if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 ) |
| 8229 | { |
| 8230 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret ); |
| 8231 | goto exit; |
| 8232 | } |
| 8233 | if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 ) |
| 8234 | { |
| 8235 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret ); |
| 8236 | goto exit; |
| 8237 | } |
| 8238 | if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 ) |
| 8239 | { |
| 8240 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret ); |
| 8241 | goto exit; |
| 8242 | } |
| 8243 | |
| 8244 | exit: |
| 8245 | mbedtls_md_free( &ctx ); |
| 8246 | |
| 8247 | if( ret != 0 ) |
| 8248 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 8249 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
| 8250 | |
| 8251 | return( ret ); |
| 8252 | } |
| 8253 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 8254 | |
Jerry Yu | d9d91da | 2022-02-17 14:57:06 +0800 | [diff] [blame] | 8255 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
| 8256 | |
Gabor Mezei | a3d016c | 2022-05-10 12:44:09 +0200 | [diff] [blame] | 8257 | /* Find the preferred hash for a given signature algorithm. */ |
| 8258 | unsigned int mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg( |
| 8259 | mbedtls_ssl_context *ssl, |
| 8260 | unsigned int sig_alg ) |
Jerry Yu | d9d91da | 2022-02-17 14:57:06 +0800 | [diff] [blame] | 8261 | { |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 8262 | unsigned int i; |
Gabor Mezei | a3d016c | 2022-05-10 12:44:09 +0200 | [diff] [blame] | 8263 | uint16_t *received_sig_algs = ssl->handshake->received_sig_algs; |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 8264 | |
| 8265 | if( sig_alg == MBEDTLS_SSL_SIG_ANON ) |
Gabor Mezei | a3d016c | 2022-05-10 12:44:09 +0200 | [diff] [blame] | 8266 | return( MBEDTLS_SSL_HASH_NONE ); |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 8267 | |
Gabor Mezei | a3d016c | 2022-05-10 12:44:09 +0200 | [diff] [blame] | 8268 | for( i = 0; received_sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ ) |
Jerry Yu | d9d91da | 2022-02-17 14:57:06 +0800 | [diff] [blame] | 8269 | { |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 8270 | unsigned int hash_alg_received = |
| 8271 | MBEDTLS_SSL_TLS12_HASH_ALG_FROM_SIG_AND_HASH_ALG( |
| 8272 | received_sig_algs[i] ); |
| 8273 | unsigned int sig_alg_received = |
| 8274 | MBEDTLS_SSL_TLS12_SIG_ALG_FROM_SIG_AND_HASH_ALG( |
| 8275 | received_sig_algs[i] ); |
| 8276 | |
| 8277 | if( sig_alg == sig_alg_received ) |
| 8278 | { |
| 8279 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 8280 | if( ssl->handshake->key_cert && ssl->handshake->key_cert->key ) |
| 8281 | { |
Neil Armstrong | 96eceb8 | 2022-06-30 18:05:05 +0200 | [diff] [blame] | 8282 | psa_algorithm_t psa_hash_alg = |
Manuel Pégourié-Gonnard | abac037 | 2022-07-18 13:41:11 +0200 | [diff] [blame] | 8283 | mbedtls_hash_info_psa_from_md( hash_alg_received ); |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 8284 | |
Neil Armstrong | 96eceb8 | 2022-06-30 18:05:05 +0200 | [diff] [blame] | 8285 | if( sig_alg_received == MBEDTLS_SSL_SIG_ECDSA && |
| 8286 | ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key, |
| 8287 | PSA_ALG_ECDSA( psa_hash_alg ), |
| 8288 | PSA_KEY_USAGE_SIGN_HASH ) ) |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 8289 | continue; |
| 8290 | |
Neil Armstrong | 96eceb8 | 2022-06-30 18:05:05 +0200 | [diff] [blame] | 8291 | if( sig_alg_received == MBEDTLS_SSL_SIG_RSA && |
Neil Armstrong | 971f30d | 2022-07-01 16:23:50 +0200 | [diff] [blame] | 8292 | ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key, |
| 8293 | PSA_ALG_RSA_PKCS1V15_SIGN( |
| 8294 | psa_hash_alg ), |
| 8295 | PSA_KEY_USAGE_SIGN_HASH ) ) |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 8296 | continue; |
| 8297 | } |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 8298 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Neil Armstrong | 96eceb8 | 2022-06-30 18:05:05 +0200 | [diff] [blame] | 8299 | |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 8300 | return( hash_alg_received ); |
| 8301 | } |
Jerry Yu | d9d91da | 2022-02-17 14:57:06 +0800 | [diff] [blame] | 8302 | } |
Jerry Yu | d9d91da | 2022-02-17 14:57:06 +0800 | [diff] [blame] | 8303 | |
Gabor Mezei | a3d016c | 2022-05-10 12:44:09 +0200 | [diff] [blame] | 8304 | return( MBEDTLS_SSL_HASH_NONE ); |
Jerry Yu | d9d91da | 2022-02-17 14:57:06 +0800 | [diff] [blame] | 8305 | } |
| 8306 | |
| 8307 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 8308 | |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8309 | /* Serialization of TLS 1.2 sessions: |
| 8310 | * |
| 8311 | * struct { |
| 8312 | * uint64 start_time; |
| 8313 | * uint8 ciphersuite[2]; // defined by the standard |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8314 | * uint8 session_id_len; // at most 32 |
| 8315 | * opaque session_id[32]; |
| 8316 | * opaque master[48]; // fixed length in the standard |
| 8317 | * uint32 verify_result; |
| 8318 | * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert |
| 8319 | * opaque ticket<0..2^24-1>; // length 0 means no ticket |
| 8320 | * uint32 ticket_lifetime; |
| 8321 | * uint8 mfl_code; // up to 255 according to standard |
| 8322 | * uint8 encrypt_then_mac; // 0 or 1 |
| 8323 | * } serialized_session_tls12; |
| 8324 | * |
| 8325 | */ |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 8326 | static size_t ssl_tls12_session_save( const mbedtls_ssl_session *session, |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8327 | unsigned char *buf, |
| 8328 | size_t buf_len ) |
| 8329 | { |
| 8330 | unsigned char *p = buf; |
| 8331 | size_t used = 0; |
| 8332 | |
| 8333 | #if defined(MBEDTLS_HAVE_TIME) |
| 8334 | uint64_t start; |
| 8335 | #endif |
| 8336 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 8337 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 8338 | size_t cert_len; |
| 8339 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 8340 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 8341 | |
| 8342 | /* |
| 8343 | * Time |
| 8344 | */ |
| 8345 | #if defined(MBEDTLS_HAVE_TIME) |
| 8346 | used += 8; |
| 8347 | |
| 8348 | if( used <= buf_len ) |
| 8349 | { |
| 8350 | start = (uint64_t) session->start; |
| 8351 | |
| 8352 | MBEDTLS_PUT_UINT64_BE( start, p, 0 ); |
| 8353 | p += 8; |
| 8354 | } |
| 8355 | #endif /* MBEDTLS_HAVE_TIME */ |
| 8356 | |
| 8357 | /* |
| 8358 | * Basic mandatory fields |
| 8359 | */ |
| 8360 | used += 2 /* ciphersuite */ |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8361 | + 1 /* id_len */ |
| 8362 | + sizeof( session->id ) |
| 8363 | + sizeof( session->master ) |
| 8364 | + 4; /* verify_result */ |
| 8365 | |
| 8366 | if( used <= buf_len ) |
| 8367 | { |
| 8368 | MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 0 ); |
| 8369 | p += 2; |
| 8370 | |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8371 | *p++ = MBEDTLS_BYTE_0( session->id_len ); |
| 8372 | memcpy( p, session->id, 32 ); |
| 8373 | p += 32; |
| 8374 | |
| 8375 | memcpy( p, session->master, 48 ); |
| 8376 | p += 48; |
| 8377 | |
| 8378 | MBEDTLS_PUT_UINT32_BE( session->verify_result, p, 0 ); |
| 8379 | p += 4; |
| 8380 | } |
| 8381 | |
| 8382 | /* |
| 8383 | * Peer's end-entity certificate |
| 8384 | */ |
| 8385 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 8386 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 8387 | if( session->peer_cert == NULL ) |
| 8388 | cert_len = 0; |
| 8389 | else |
| 8390 | cert_len = session->peer_cert->raw.len; |
| 8391 | |
| 8392 | used += 3 + cert_len; |
| 8393 | |
| 8394 | if( used <= buf_len ) |
| 8395 | { |
| 8396 | *p++ = MBEDTLS_BYTE_2( cert_len ); |
| 8397 | *p++ = MBEDTLS_BYTE_1( cert_len ); |
| 8398 | *p++ = MBEDTLS_BYTE_0( cert_len ); |
| 8399 | |
| 8400 | if( session->peer_cert != NULL ) |
| 8401 | { |
| 8402 | memcpy( p, session->peer_cert->raw.p, cert_len ); |
| 8403 | p += cert_len; |
| 8404 | } |
| 8405 | } |
| 8406 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 8407 | if( session->peer_cert_digest != NULL ) |
| 8408 | { |
| 8409 | used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len; |
| 8410 | if( used <= buf_len ) |
| 8411 | { |
| 8412 | *p++ = (unsigned char) session->peer_cert_digest_type; |
| 8413 | *p++ = (unsigned char) session->peer_cert_digest_len; |
| 8414 | memcpy( p, session->peer_cert_digest, |
| 8415 | session->peer_cert_digest_len ); |
| 8416 | p += session->peer_cert_digest_len; |
| 8417 | } |
| 8418 | } |
| 8419 | else |
| 8420 | { |
| 8421 | used += 2; |
| 8422 | if( used <= buf_len ) |
| 8423 | { |
| 8424 | *p++ = (unsigned char) MBEDTLS_MD_NONE; |
| 8425 | *p++ = 0; |
| 8426 | } |
| 8427 | } |
| 8428 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 8429 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 8430 | |
| 8431 | /* |
| 8432 | * Session ticket if any, plus associated data |
| 8433 | */ |
| 8434 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
| 8435 | used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */ |
| 8436 | |
| 8437 | if( used <= buf_len ) |
| 8438 | { |
| 8439 | *p++ = MBEDTLS_BYTE_2( session->ticket_len ); |
| 8440 | *p++ = MBEDTLS_BYTE_1( session->ticket_len ); |
| 8441 | *p++ = MBEDTLS_BYTE_0( session->ticket_len ); |
| 8442 | |
| 8443 | if( session->ticket != NULL ) |
| 8444 | { |
| 8445 | memcpy( p, session->ticket, session->ticket_len ); |
| 8446 | p += session->ticket_len; |
| 8447 | } |
| 8448 | |
| 8449 | MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 ); |
| 8450 | p += 4; |
| 8451 | } |
| 8452 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
| 8453 | |
| 8454 | /* |
| 8455 | * Misc extension-related info |
| 8456 | */ |
| 8457 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 8458 | used += 1; |
| 8459 | |
| 8460 | if( used <= buf_len ) |
| 8461 | *p++ = session->mfl_code; |
| 8462 | #endif |
| 8463 | |
| 8464 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 8465 | used += 1; |
| 8466 | |
| 8467 | if( used <= buf_len ) |
| 8468 | *p++ = MBEDTLS_BYTE_0( session->encrypt_then_mac ); |
| 8469 | #endif |
| 8470 | |
| 8471 | return( used ); |
| 8472 | } |
| 8473 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 8474 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 8475 | static int ssl_tls12_session_load( mbedtls_ssl_session *session, |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8476 | const unsigned char *buf, |
| 8477 | size_t len ) |
| 8478 | { |
| 8479 | #if defined(MBEDTLS_HAVE_TIME) |
| 8480 | uint64_t start; |
| 8481 | #endif |
| 8482 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 8483 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 8484 | size_t cert_len; |
| 8485 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 8486 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 8487 | |
| 8488 | const unsigned char *p = buf; |
| 8489 | const unsigned char * const end = buf + len; |
| 8490 | |
| 8491 | /* |
| 8492 | * Time |
| 8493 | */ |
| 8494 | #if defined(MBEDTLS_HAVE_TIME) |
| 8495 | if( 8 > (size_t)( end - p ) ) |
| 8496 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8497 | |
| 8498 | start = ( (uint64_t) p[0] << 56 ) | |
| 8499 | ( (uint64_t) p[1] << 48 ) | |
| 8500 | ( (uint64_t) p[2] << 40 ) | |
| 8501 | ( (uint64_t) p[3] << 32 ) | |
| 8502 | ( (uint64_t) p[4] << 24 ) | |
| 8503 | ( (uint64_t) p[5] << 16 ) | |
| 8504 | ( (uint64_t) p[6] << 8 ) | |
| 8505 | ( (uint64_t) p[7] ); |
| 8506 | p += 8; |
| 8507 | |
| 8508 | session->start = (time_t) start; |
| 8509 | #endif /* MBEDTLS_HAVE_TIME */ |
| 8510 | |
| 8511 | /* |
| 8512 | * Basic mandatory fields |
| 8513 | */ |
Thomas Daubney | 20f89a9 | 2022-06-20 15:12:19 +0100 | [diff] [blame] | 8514 | if( 2 + 1 + 32 + 48 + 4 > (size_t)( end - p ) ) |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8515 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8516 | |
| 8517 | session->ciphersuite = ( p[0] << 8 ) | p[1]; |
| 8518 | p += 2; |
| 8519 | |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8520 | session->id_len = *p++; |
| 8521 | memcpy( session->id, p, 32 ); |
| 8522 | p += 32; |
| 8523 | |
| 8524 | memcpy( session->master, p, 48 ); |
| 8525 | p += 48; |
| 8526 | |
| 8527 | session->verify_result = ( (uint32_t) p[0] << 24 ) | |
| 8528 | ( (uint32_t) p[1] << 16 ) | |
| 8529 | ( (uint32_t) p[2] << 8 ) | |
| 8530 | ( (uint32_t) p[3] ); |
| 8531 | p += 4; |
| 8532 | |
| 8533 | /* Immediately clear invalid pointer values that have been read, in case |
| 8534 | * we exit early before we replaced them with valid ones. */ |
| 8535 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 8536 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 8537 | session->peer_cert = NULL; |
| 8538 | #else |
| 8539 | session->peer_cert_digest = NULL; |
| 8540 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 8541 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 8542 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
| 8543 | session->ticket = NULL; |
| 8544 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
| 8545 | |
| 8546 | /* |
| 8547 | * Peer certificate |
| 8548 | */ |
| 8549 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 8550 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 8551 | /* Deserialize CRT from the end of the ticket. */ |
| 8552 | if( 3 > (size_t)( end - p ) ) |
| 8553 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8554 | |
| 8555 | cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2]; |
| 8556 | p += 3; |
| 8557 | |
| 8558 | if( cert_len != 0 ) |
| 8559 | { |
| 8560 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 8561 | |
| 8562 | if( cert_len > (size_t)( end - p ) ) |
| 8563 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8564 | |
| 8565 | session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ); |
| 8566 | |
| 8567 | if( session->peer_cert == NULL ) |
| 8568 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 8569 | |
| 8570 | mbedtls_x509_crt_init( session->peer_cert ); |
| 8571 | |
| 8572 | if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert, |
| 8573 | p, cert_len ) ) != 0 ) |
| 8574 | { |
| 8575 | mbedtls_x509_crt_free( session->peer_cert ); |
| 8576 | mbedtls_free( session->peer_cert ); |
| 8577 | session->peer_cert = NULL; |
| 8578 | return( ret ); |
| 8579 | } |
| 8580 | |
| 8581 | p += cert_len; |
| 8582 | } |
| 8583 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 8584 | /* Deserialize CRT digest from the end of the ticket. */ |
| 8585 | if( 2 > (size_t)( end - p ) ) |
| 8586 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8587 | |
| 8588 | session->peer_cert_digest_type = (mbedtls_md_type_t) *p++; |
| 8589 | session->peer_cert_digest_len = (size_t) *p++; |
| 8590 | |
| 8591 | if( session->peer_cert_digest_len != 0 ) |
| 8592 | { |
| 8593 | const mbedtls_md_info_t *md_info = |
| 8594 | mbedtls_md_info_from_type( session->peer_cert_digest_type ); |
| 8595 | if( md_info == NULL ) |
| 8596 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8597 | if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) ) |
| 8598 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8599 | |
| 8600 | if( session->peer_cert_digest_len > (size_t)( end - p ) ) |
| 8601 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8602 | |
| 8603 | session->peer_cert_digest = |
| 8604 | mbedtls_calloc( 1, session->peer_cert_digest_len ); |
| 8605 | if( session->peer_cert_digest == NULL ) |
| 8606 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 8607 | |
| 8608 | memcpy( session->peer_cert_digest, p, |
| 8609 | session->peer_cert_digest_len ); |
| 8610 | p += session->peer_cert_digest_len; |
| 8611 | } |
| 8612 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 8613 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 8614 | |
| 8615 | /* |
| 8616 | * Session ticket and associated data |
| 8617 | */ |
| 8618 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
| 8619 | if( 3 > (size_t)( end - p ) ) |
| 8620 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8621 | |
| 8622 | session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2]; |
| 8623 | p += 3; |
| 8624 | |
| 8625 | if( session->ticket_len != 0 ) |
| 8626 | { |
| 8627 | if( session->ticket_len > (size_t)( end - p ) ) |
| 8628 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8629 | |
| 8630 | session->ticket = mbedtls_calloc( 1, session->ticket_len ); |
| 8631 | if( session->ticket == NULL ) |
| 8632 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 8633 | |
| 8634 | memcpy( session->ticket, p, session->ticket_len ); |
| 8635 | p += session->ticket_len; |
| 8636 | } |
| 8637 | |
| 8638 | if( 4 > (size_t)( end - p ) ) |
| 8639 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8640 | |
| 8641 | session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) | |
| 8642 | ( (uint32_t) p[1] << 16 ) | |
| 8643 | ( (uint32_t) p[2] << 8 ) | |
| 8644 | ( (uint32_t) p[3] ); |
| 8645 | p += 4; |
| 8646 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
| 8647 | |
| 8648 | /* |
| 8649 | * Misc extension-related info |
| 8650 | */ |
| 8651 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 8652 | if( 1 > (size_t)( end - p ) ) |
| 8653 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8654 | |
| 8655 | session->mfl_code = *p++; |
| 8656 | #endif |
| 8657 | |
| 8658 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 8659 | if( 1 > (size_t)( end - p ) ) |
| 8660 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8661 | |
| 8662 | session->encrypt_then_mac = *p++; |
| 8663 | #endif |
| 8664 | |
| 8665 | /* Done, should have consumed entire buffer */ |
| 8666 | if( p != end ) |
| 8667 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8668 | |
| 8669 | return( 0 ); |
| 8670 | } |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 8671 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 8672 | |
XiaokangQian | 75d40ef | 2022-04-20 11:05:24 +0000 | [diff] [blame] | 8673 | int mbedtls_ssl_validate_ciphersuite( |
| 8674 | const mbedtls_ssl_context *ssl, |
| 8675 | const mbedtls_ssl_ciphersuite_t *suite_info, |
| 8676 | mbedtls_ssl_protocol_version min_tls_version, |
| 8677 | mbedtls_ssl_protocol_version max_tls_version ) |
| 8678 | { |
| 8679 | (void) ssl; |
| 8680 | |
| 8681 | if( suite_info == NULL ) |
| 8682 | return( -1 ); |
| 8683 | |
| 8684 | if( ( suite_info->min_tls_version > max_tls_version ) || |
| 8685 | ( suite_info->max_tls_version < min_tls_version ) ) |
| 8686 | { |
| 8687 | return( -1 ); |
| 8688 | } |
| 8689 | |
XiaokangQian | 060d867 | 2022-04-21 09:24:56 +0000 | [diff] [blame] | 8690 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_CLI_C) |
XiaokangQian | 75d40ef | 2022-04-20 11:05:24 +0000 | [diff] [blame] | 8691 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
| 8692 | if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE && |
| 8693 | mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 ) |
| 8694 | { |
| 8695 | return( -1 ); |
| 8696 | } |
| 8697 | #endif |
| 8698 | |
| 8699 | /* Don't suggest PSK-based ciphersuite if no PSK is available. */ |
| 8700 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
| 8701 | if( mbedtls_ssl_ciphersuite_uses_psk( suite_info ) && |
| 8702 | mbedtls_ssl_conf_has_static_psk( ssl->conf ) == 0 ) |
| 8703 | { |
| 8704 | return( -1 ); |
| 8705 | } |
| 8706 | #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ |
| 8707 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 8708 | |
| 8709 | return( 0 ); |
| 8710 | } |
| 8711 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 8712 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 8713 | /* |
| 8714 | * Function for writing a signature algorithm extension. |
| 8715 | * |
| 8716 | * The `extension_data` field of signature algorithm contains a `SignatureSchemeList` |
| 8717 | * value (TLS 1.3 RFC8446): |
| 8718 | * enum { |
| 8719 | * .... |
| 8720 | * ecdsa_secp256r1_sha256( 0x0403 ), |
| 8721 | * ecdsa_secp384r1_sha384( 0x0503 ), |
| 8722 | * ecdsa_secp521r1_sha512( 0x0603 ), |
| 8723 | * .... |
| 8724 | * } SignatureScheme; |
| 8725 | * |
| 8726 | * struct { |
| 8727 | * SignatureScheme supported_signature_algorithms<2..2^16-2>; |
| 8728 | * } SignatureSchemeList; |
| 8729 | * |
| 8730 | * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm` |
| 8731 | * value (TLS 1.2 RFC5246): |
| 8732 | * enum { |
| 8733 | * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5), |
| 8734 | * sha512(6), (255) |
| 8735 | * } HashAlgorithm; |
| 8736 | * |
| 8737 | * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) } |
| 8738 | * SignatureAlgorithm; |
| 8739 | * |
| 8740 | * struct { |
| 8741 | * HashAlgorithm hash; |
| 8742 | * SignatureAlgorithm signature; |
| 8743 | * } SignatureAndHashAlgorithm; |
| 8744 | * |
| 8745 | * SignatureAndHashAlgorithm |
| 8746 | * supported_signature_algorithms<2..2^16-2>; |
| 8747 | * |
| 8748 | * The TLS 1.3 signature algorithm extension was defined to be a compatible |
| 8749 | * generalization of the TLS 1.2 signature algorithm extension. |
| 8750 | * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by |
| 8751 | * `SignatureScheme` field of TLS 1.3 |
| 8752 | * |
| 8753 | */ |
| 8754 | int mbedtls_ssl_write_sig_alg_ext( mbedtls_ssl_context *ssl, unsigned char *buf, |
| 8755 | const unsigned char *end, size_t *out_len ) |
| 8756 | { |
| 8757 | unsigned char *p = buf; |
| 8758 | unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */ |
| 8759 | size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */ |
| 8760 | |
| 8761 | *out_len = 0; |
| 8762 | |
| 8763 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding signature_algorithms extension" ) ); |
| 8764 | |
| 8765 | /* Check if we have space for header and length field: |
| 8766 | * - extension_type (2 bytes) |
| 8767 | * - extension_data_length (2 bytes) |
| 8768 | * - supported_signature_algorithms_length (2 bytes) |
| 8769 | */ |
| 8770 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 ); |
| 8771 | p += 6; |
| 8772 | |
| 8773 | /* |
| 8774 | * Write supported_signature_algorithms |
| 8775 | */ |
| 8776 | supported_sig_alg = p; |
| 8777 | const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs( ssl ); |
| 8778 | if( sig_alg == NULL ) |
| 8779 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); |
| 8780 | |
| 8781 | for( ; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++ ) |
| 8782 | { |
Jerry Yu | 53f5c15 | 2022-06-22 20:24:38 +0800 | [diff] [blame] | 8783 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "got signature scheme [%x] %s", |
| 8784 | *sig_alg, |
| 8785 | mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) ); |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 8786 | if( ! mbedtls_ssl_sig_alg_is_supported( ssl, *sig_alg ) ) |
| 8787 | continue; |
| 8788 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 ); |
| 8789 | MBEDTLS_PUT_UINT16_BE( *sig_alg, p, 0 ); |
| 8790 | p += 2; |
Jerry Yu | 80dd5db | 2022-06-22 19:30:32 +0800 | [diff] [blame] | 8791 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "sent signature scheme [%x] %s", |
Jerry Yu | f3b46b5 | 2022-06-19 16:52:27 +0800 | [diff] [blame] | 8792 | *sig_alg, |
| 8793 | mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) ); |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 8794 | } |
| 8795 | |
| 8796 | /* Length of supported_signature_algorithms */ |
| 8797 | supported_sig_alg_len = p - supported_sig_alg; |
| 8798 | if( supported_sig_alg_len == 0 ) |
| 8799 | { |
| 8800 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "No signature algorithms defined." ) ); |
| 8801 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 8802 | } |
| 8803 | |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 8804 | MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SIG_ALG, buf, 0 ); |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 8805 | MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len + 2, buf, 2 ); |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 8806 | MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len, buf, 4 ); |
| 8807 | |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 8808 | *out_len = p - buf; |
| 8809 | |
| 8810 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | c4bf5d6 | 2022-10-29 09:08:47 +0800 | [diff] [blame] | 8811 | mbedtls_ssl_tls13_set_hs_sent_ext_mask( ssl, MBEDTLS_TLS_EXT_SIG_ALG ); |
Jerry Yu | 0c354a2 | 2022-08-29 15:25:36 +0800 | [diff] [blame] | 8812 | MBEDTLS_SSL_DEBUG_MSG( |
| 8813 | 4, ( "sent %s extension", |
| 8814 | mbedtls_tls13_get_extension_name( |
| 8815 | MBEDTLS_TLS_EXT_SIG_ALG ) ) ); |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 8816 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Jerry Yu | 0c354a2 | 2022-08-29 15:25:36 +0800 | [diff] [blame] | 8817 | |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 8818 | return( 0 ); |
| 8819 | } |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 8820 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 8821 | |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 8822 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 8823 | /* |
| 8824 | * mbedtls_ssl_parse_server_name_ext |
| 8825 | * |
| 8826 | * Structure of server_name extension: |
| 8827 | * |
| 8828 | * enum { |
| 8829 | * host_name(0), (255) |
| 8830 | * } NameType; |
| 8831 | * opaque HostName<1..2^16-1>; |
| 8832 | * |
| 8833 | * struct { |
| 8834 | * NameType name_type; |
| 8835 | * select (name_type) { |
| 8836 | * case host_name: HostName; |
| 8837 | * } name; |
| 8838 | * } ServerName; |
| 8839 | * struct { |
| 8840 | * ServerName server_name_list<1..2^16-1> |
| 8841 | * } ServerNameList; |
| 8842 | */ |
Ronald Cron | ce7d76e | 2022-07-08 18:56:49 +0200 | [diff] [blame] | 8843 | MBEDTLS_CHECK_RETURN_CRITICAL |
XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 8844 | int mbedtls_ssl_parse_server_name_ext( mbedtls_ssl_context *ssl, |
| 8845 | const unsigned char *buf, |
| 8846 | const unsigned char *end ) |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 8847 | { |
| 8848 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 8849 | const unsigned char *p = buf; |
XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 8850 | size_t server_name_list_len, hostname_len; |
| 8851 | const unsigned char *server_name_list_end; |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 8852 | |
XiaokangQian | f2a9420 | 2022-05-20 06:44:24 +0000 | [diff] [blame] | 8853 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "parse ServerName extension" ) ); |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 8854 | |
| 8855 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 ); |
XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 8856 | server_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 ); |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 8857 | p += 2; |
| 8858 | |
XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 8859 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, server_name_list_len ); |
| 8860 | server_name_list_end = p + server_name_list_len; |
XiaokangQian | 75fe8c7 | 2022-06-15 09:42:45 +0000 | [diff] [blame] | 8861 | while( p < server_name_list_end ) |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 8862 | { |
XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 8863 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end, 3 ); |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 8864 | hostname_len = MBEDTLS_GET_UINT16_BE( p, 1 ); |
XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 8865 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end, |
| 8866 | hostname_len + 3 ); |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 8867 | |
| 8868 | if( p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME ) |
| 8869 | { |
XiaokangQian | 75fe8c7 | 2022-06-15 09:42:45 +0000 | [diff] [blame] | 8870 | /* sni_name is intended to be used only during the parsing of the |
| 8871 | * ClientHello message (it is reset to NULL before the end of |
| 8872 | * the message parsing). Thus it is ok to just point to the |
| 8873 | * reception buffer and not make a copy of it. |
| 8874 | */ |
XiaokangQian | f2a9420 | 2022-05-20 06:44:24 +0000 | [diff] [blame] | 8875 | ssl->handshake->sni_name = p + 3; |
| 8876 | ssl->handshake->sni_name_len = hostname_len; |
| 8877 | if( ssl->conf->f_sni == NULL ) |
| 8878 | return( 0 ); |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 8879 | ret = ssl->conf->f_sni( ssl->conf->p_sni, |
XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 8880 | ssl, p + 3, hostname_len ); |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 8881 | if( ret != 0 ) |
| 8882 | { |
XiaokangQian | f2a9420 | 2022-05-20 06:44:24 +0000 | [diff] [blame] | 8883 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_sni_wrapper", ret ); |
XiaokangQian | 129aeb9 | 2022-06-02 09:29:18 +0000 | [diff] [blame] | 8884 | MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME, |
| 8885 | MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME ); |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 8886 | return( MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME ); |
| 8887 | } |
| 8888 | return( 0 ); |
| 8889 | } |
| 8890 | |
| 8891 | p += hostname_len + 3; |
| 8892 | } |
| 8893 | |
| 8894 | return( 0 ); |
| 8895 | } |
| 8896 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
| 8897 | |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8898 | #if defined(MBEDTLS_SSL_ALPN) |
Ronald Cron | ce7d76e | 2022-07-08 18:56:49 +0200 | [diff] [blame] | 8899 | MBEDTLS_CHECK_RETURN_CRITICAL |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8900 | int mbedtls_ssl_parse_alpn_ext( mbedtls_ssl_context *ssl, |
| 8901 | const unsigned char *buf, |
| 8902 | const unsigned char *end ) |
| 8903 | { |
| 8904 | const unsigned char *p = buf; |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 8905 | size_t protocol_name_list_len; |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 8906 | const unsigned char *protocol_name_list; |
| 8907 | const unsigned char *protocol_name_list_end; |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 8908 | size_t protocol_name_len; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8909 | |
| 8910 | /* If ALPN not configured, just ignore the extension */ |
| 8911 | if( ssl->conf->alpn_list == NULL ) |
| 8912 | return( 0 ); |
| 8913 | |
| 8914 | /* |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 8915 | * RFC7301, section 3.1 |
| 8916 | * opaque ProtocolName<1..2^8-1>; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8917 | * |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 8918 | * struct { |
| 8919 | * ProtocolName protocol_name_list<2..2^16-1> |
| 8920 | * } ProtocolNameList; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8921 | */ |
| 8922 | |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 8923 | /* |
XiaokangQian | 0b776e2 | 2022-06-24 09:04:59 +0000 | [diff] [blame] | 8924 | * protocol_name_list_len 2 bytes |
| 8925 | * protocol_name_len 1 bytes |
| 8926 | * protocol_name >=1 byte |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 8927 | */ |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8928 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 ); |
| 8929 | |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 8930 | protocol_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 ); |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8931 | p += 2; |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 8932 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, protocol_name_list_len ); |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 8933 | protocol_name_list = p; |
| 8934 | protocol_name_list_end = p + protocol_name_list_len; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8935 | |
| 8936 | /* Validate peer's list (lengths) */ |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 8937 | while( p < protocol_name_list_end ) |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8938 | { |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 8939 | protocol_name_len = *p++; |
| 8940 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, protocol_name_list_end, |
| 8941 | protocol_name_len ); |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 8942 | if( protocol_name_len == 0 ) |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 8943 | { |
| 8944 | MBEDTLS_SSL_PEND_FATAL_ALERT( |
| 8945 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER, |
| 8946 | MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER ); |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8947 | return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER ); |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 8948 | } |
| 8949 | |
| 8950 | p += protocol_name_len; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8951 | } |
| 8952 | |
| 8953 | /* Use our order of preference */ |
| 8954 | for( const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++ ) |
| 8955 | { |
| 8956 | size_t const alpn_len = strlen( *alpn ); |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 8957 | p = protocol_name_list; |
| 8958 | while( p < protocol_name_list_end ) |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8959 | { |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 8960 | protocol_name_len = *p++; |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 8961 | if( protocol_name_len == alpn_len && |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 8962 | memcmp( p, *alpn, alpn_len ) == 0 ) |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8963 | { |
| 8964 | ssl->alpn_chosen = *alpn; |
| 8965 | return( 0 ); |
| 8966 | } |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 8967 | |
| 8968 | p += protocol_name_len; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8969 | } |
| 8970 | } |
| 8971 | |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 8972 | /* If we get here, no match was found */ |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8973 | MBEDTLS_SSL_PEND_FATAL_ALERT( |
| 8974 | MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL, |
| 8975 | MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL ); |
| 8976 | return( MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL ); |
| 8977 | } |
| 8978 | |
| 8979 | int mbedtls_ssl_write_alpn_ext( mbedtls_ssl_context *ssl, |
| 8980 | unsigned char *buf, |
| 8981 | unsigned char *end, |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 8982 | size_t *out_len ) |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8983 | { |
| 8984 | unsigned char *p = buf; |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 8985 | size_t protocol_name_len; |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 8986 | *out_len = 0; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8987 | |
| 8988 | if( ssl->alpn_chosen == NULL ) |
| 8989 | { |
| 8990 | return( 0 ); |
| 8991 | } |
| 8992 | |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 8993 | protocol_name_len = strlen( ssl->alpn_chosen ); |
| 8994 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 7 + protocol_name_len ); |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8995 | |
| 8996 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "server side, adding alpn extension" ) ); |
| 8997 | /* |
| 8998 | * 0 . 1 ext identifier |
| 8999 | * 2 . 3 ext length |
| 9000 | * 4 . 5 protocol list length |
| 9001 | * 6 . 6 protocol name length |
| 9002 | * 7 . 7+n protocol name |
| 9003 | */ |
| 9004 | MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ALPN, p, 0 ); |
| 9005 | |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9006 | *out_len = 7 + protocol_name_len; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9007 | |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9008 | MBEDTLS_PUT_UINT16_BE( protocol_name_len + 3, p, 2 ); |
| 9009 | MBEDTLS_PUT_UINT16_BE( protocol_name_len + 1, p, 4 ); |
XiaokangQian | 0b776e2 | 2022-06-24 09:04:59 +0000 | [diff] [blame] | 9010 | /* Note: the length of the chosen protocol has been checked to be less |
| 9011 | * than 255 bytes in `mbedtls_ssl_conf_alpn_protocols`. |
| 9012 | */ |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9013 | p[6] = MBEDTLS_BYTE_0( protocol_name_len ); |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9014 | |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9015 | memcpy( p + 7, ssl->alpn_chosen, protocol_name_len ); |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9016 | return ( 0 ); |
| 9017 | } |
| 9018 | #endif /* MBEDTLS_SSL_ALPN */ |
| 9019 | |
Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 9020 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \ |
Xiaokang Qian | 0340929 | 2022-10-12 02:49:52 +0000 | [diff] [blame] | 9021 | defined(MBEDTLS_SSL_SESSION_TICKETS) && \ |
Xiaokang Qian | ed0620c | 2022-10-12 06:58:13 +0000 | [diff] [blame] | 9022 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \ |
Xiaokang Qian | ed3afcd | 2022-10-12 08:31:11 +0000 | [diff] [blame] | 9023 | defined(MBEDTLS_SSL_CLI_C) |
Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 9024 | int mbedtls_ssl_session_set_hostname( mbedtls_ssl_session *session, |
| 9025 | const char *hostname ) |
| 9026 | { |
| 9027 | /* Initialize to suppress unnecessary compiler warning */ |
| 9028 | size_t hostname_len = 0; |
| 9029 | |
| 9030 | /* Check if new hostname is valid before |
| 9031 | * making any change to current one */ |
| 9032 | if( hostname != NULL ) |
| 9033 | { |
| 9034 | hostname_len = strlen( hostname ); |
| 9035 | |
| 9036 | if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN ) |
| 9037 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 9038 | } |
| 9039 | |
| 9040 | /* Now it's clear that we will overwrite the old hostname, |
| 9041 | * so we can free it safely */ |
| 9042 | if( session->hostname != NULL ) |
| 9043 | { |
| 9044 | mbedtls_platform_zeroize( session->hostname, |
| 9045 | strlen( session->hostname ) ); |
| 9046 | mbedtls_free( session->hostname ); |
| 9047 | } |
| 9048 | |
| 9049 | /* Passing NULL as hostname shall clear the old one */ |
| 9050 | if( hostname == NULL ) |
| 9051 | { |
| 9052 | session->hostname = NULL; |
| 9053 | } |
| 9054 | else |
| 9055 | { |
| 9056 | session->hostname = mbedtls_calloc( 1, hostname_len + 1 ); |
| 9057 | if( session->hostname == NULL ) |
| 9058 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 9059 | |
| 9060 | memcpy( session->hostname, hostname, hostname_len ); |
| 9061 | } |
| 9062 | |
| 9063 | return( 0 ); |
| 9064 | } |
Xiaokang Qian | 0340929 | 2022-10-12 02:49:52 +0000 | [diff] [blame] | 9065 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 && |
| 9066 | MBEDTLS_SSL_SESSION_TICKETS && |
Xiaokang Qian | ed0620c | 2022-10-12 06:58:13 +0000 | [diff] [blame] | 9067 | MBEDTLS_SSL_SERVER_NAME_INDICATION && |
Xiaokang Qian | ed3afcd | 2022-10-12 08:31:11 +0000 | [diff] [blame] | 9068 | MBEDTLS_SSL_CLI_C */ |
Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 9069 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9070 | #endif /* MBEDTLS_SSL_TLS_C */ |