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 || |
Jerry Yu | 6848a61 | 2022-10-27 13:03:26 +0800 | [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 | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 619 | #if defined(MBEDTLS_DEBUG_C) |
| 620 | static const char *extension_name_table[] = { |
Jerry Yu | ea52ed9 | 2022-11-08 21:01:17 +0800 | [diff] [blame] | 621 | [MBEDTLS_SSL_EXT_ID_UNRECOGNIZED] = "unrecognized", |
Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 622 | [MBEDTLS_SSL_EXT_ID_SERVERNAME] = "server_name", |
| 623 | [MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH] = "max_fragment_length", |
| 624 | [MBEDTLS_SSL_EXT_ID_STATUS_REQUEST] = "status_request", |
| 625 | [MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS] = "supported_groups", |
| 626 | [MBEDTLS_SSL_EXT_ID_SIG_ALG] = "signature_algorithms", |
| 627 | [MBEDTLS_SSL_EXT_ID_USE_SRTP] = "use_srtp", |
| 628 | [MBEDTLS_SSL_EXT_ID_HEARTBEAT] = "heartbeat", |
| 629 | [MBEDTLS_SSL_EXT_ID_ALPN] = "application_layer_protocol_negotiation", |
| 630 | [MBEDTLS_SSL_EXT_ID_SCT] = "signed_certificate_timestamp", |
| 631 | [MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE] = "client_certificate_type", |
| 632 | [MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE] = "server_certificate_type", |
| 633 | [MBEDTLS_SSL_EXT_ID_PADDING] = "padding", |
| 634 | [MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY] = "pre_shared_key", |
| 635 | [MBEDTLS_SSL_EXT_ID_EARLY_DATA] = "early_data", |
| 636 | [MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS] = "supported_versions", |
| 637 | [MBEDTLS_SSL_EXT_ID_COOKIE] = "cookie", |
| 638 | [MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES] = "psk_key_exchange_modes", |
| 639 | [MBEDTLS_SSL_EXT_ID_CERT_AUTH] = "certificate_authorities", |
| 640 | [MBEDTLS_SSL_EXT_ID_OID_FILTERS] = "oid_filters", |
| 641 | [MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH] = "post_handshake_auth", |
| 642 | [MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT] = "signature_algorithms_cert", |
| 643 | [MBEDTLS_SSL_EXT_ID_KEY_SHARE] = "key_share", |
| 644 | [MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC] = "truncated_hmac", |
| 645 | [MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS] = "supported_point_formats", |
| 646 | [MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC] = "encrypt_then_mac", |
| 647 | [MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET] = "extended_master_secret", |
| 648 | [MBEDTLS_SSL_EXT_ID_SESSION_TICKET] = "session_ticket" |
| 649 | }; |
| 650 | |
Jerry Yu | ea52ed9 | 2022-11-08 21:01:17 +0800 | [diff] [blame] | 651 | static unsigned int extension_type_table[]={ |
Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 652 | [MBEDTLS_SSL_EXT_ID_UNRECOGNIZED] = 0xff, |
| 653 | [MBEDTLS_SSL_EXT_ID_SERVERNAME] = MBEDTLS_TLS_EXT_SERVERNAME, |
| 654 | [MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH] = MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH, |
| 655 | [MBEDTLS_SSL_EXT_ID_STATUS_REQUEST] = MBEDTLS_TLS_EXT_STATUS_REQUEST, |
| 656 | [MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS] = MBEDTLS_TLS_EXT_SUPPORTED_GROUPS, |
| 657 | [MBEDTLS_SSL_EXT_ID_SIG_ALG] = MBEDTLS_TLS_EXT_SIG_ALG, |
| 658 | [MBEDTLS_SSL_EXT_ID_USE_SRTP] = MBEDTLS_TLS_EXT_USE_SRTP, |
| 659 | [MBEDTLS_SSL_EXT_ID_HEARTBEAT] = MBEDTLS_TLS_EXT_HEARTBEAT, |
| 660 | [MBEDTLS_SSL_EXT_ID_ALPN] = MBEDTLS_TLS_EXT_ALPN, |
| 661 | [MBEDTLS_SSL_EXT_ID_SCT] = MBEDTLS_TLS_EXT_SCT, |
| 662 | [MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE] = MBEDTLS_TLS_EXT_CLI_CERT_TYPE, |
| 663 | [MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE] = MBEDTLS_TLS_EXT_SERV_CERT_TYPE, |
| 664 | [MBEDTLS_SSL_EXT_ID_PADDING] = MBEDTLS_TLS_EXT_PADDING, |
| 665 | [MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY] = MBEDTLS_TLS_EXT_PRE_SHARED_KEY, |
| 666 | [MBEDTLS_SSL_EXT_ID_EARLY_DATA] = MBEDTLS_TLS_EXT_EARLY_DATA, |
| 667 | [MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS] = MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, |
| 668 | [MBEDTLS_SSL_EXT_ID_COOKIE] = MBEDTLS_TLS_EXT_COOKIE, |
| 669 | [MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES] = MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES, |
| 670 | [MBEDTLS_SSL_EXT_ID_CERT_AUTH] = MBEDTLS_TLS_EXT_CERT_AUTH, |
| 671 | [MBEDTLS_SSL_EXT_ID_OID_FILTERS] = MBEDTLS_TLS_EXT_OID_FILTERS, |
| 672 | [MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH] = MBEDTLS_TLS_EXT_POST_HANDSHAKE_AUTH, |
| 673 | [MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT] = MBEDTLS_TLS_EXT_SIG_ALG_CERT, |
| 674 | [MBEDTLS_SSL_EXT_ID_KEY_SHARE] = MBEDTLS_TLS_EXT_KEY_SHARE, |
| 675 | [MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC] = MBEDTLS_TLS_EXT_TRUNCATED_HMAC, |
| 676 | [MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS] = MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS, |
| 677 | [MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC] = MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC, |
| 678 | [MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET] = MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET, |
| 679 | [MBEDTLS_SSL_EXT_ID_SESSION_TICKET] = MBEDTLS_TLS_EXT_SESSION_TICKET |
| 680 | }; |
| 681 | |
| 682 | const char *mbedtls_ssl_get_extension_name( unsigned int extension_type ) |
| 683 | { |
| 684 | return( extension_name_table[ |
| 685 | mbedtls_ssl_get_extension_id( extension_type ) ] ); |
| 686 | } |
| 687 | |
| 688 | static const char *ssl_tls13_get_hs_msg_name( int hs_msg_type ) |
| 689 | { |
| 690 | switch( hs_msg_type ) |
| 691 | { |
| 692 | case MBEDTLS_SSL_HS_CLIENT_HELLO: |
| 693 | return( "ClientHello" ); |
| 694 | case MBEDTLS_SSL_HS_SERVER_HELLO: |
| 695 | return( "ServerHello" ); |
| 696 | case MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST: |
| 697 | return( "HelloRetryRequest" ); |
| 698 | case MBEDTLS_SSL_HS_NEW_SESSION_TICKET: |
| 699 | return( "NewSessionTicket" ); |
| 700 | case MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS: |
| 701 | return( "EncryptedExtensions" ); |
| 702 | case MBEDTLS_SSL_HS_CERTIFICATE: |
| 703 | return( "Certificate" ); |
| 704 | case MBEDTLS_SSL_HS_CERTIFICATE_REQUEST: |
| 705 | return( "CertificateRequest" ); |
| 706 | } |
Jerry Yu | 79aa721 | 2022-11-08 21:30:21 +0800 | [diff] [blame] | 707 | return( "Unknown" ); |
Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 708 | } |
| 709 | |
Jerry Yu | 79aa721 | 2022-11-08 21:30:21 +0800 | [diff] [blame] | 710 | void mbedtls_ssl_print_extension( const mbedtls_ssl_context *ssl, |
| 711 | int level, const char *file, int line, |
| 712 | int hs_msg_type, unsigned int extension_type, |
| 713 | const char *extra_msg0, const char *extra_msg1 ) |
Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 714 | { |
| 715 | const char *extra_msg; |
| 716 | if( extra_msg0 && extra_msg1 ) |
| 717 | { |
| 718 | mbedtls_debug_print_msg( |
| 719 | ssl, level, file, line, |
| 720 | "%s: %s(%u) extension %s %s.", |
| 721 | ssl_tls13_get_hs_msg_name( hs_msg_type ), |
| 722 | mbedtls_ssl_get_extension_name( extension_type ), |
| 723 | extension_type, |
| 724 | extra_msg0, extra_msg1 ); |
| 725 | return; |
| 726 | } |
| 727 | |
| 728 | extra_msg = extra_msg0 ? extra_msg0 : extra_msg1; |
| 729 | if( extra_msg ) |
| 730 | { |
| 731 | mbedtls_debug_print_msg( |
| 732 | ssl, level, file, line, |
| 733 | "%s: %s(%u) extension %s.", ssl_tls13_get_hs_msg_name( hs_msg_type ), |
| 734 | mbedtls_ssl_get_extension_name( extension_type ), extension_type, |
| 735 | extra_msg ); |
| 736 | return; |
| 737 | } |
| 738 | |
| 739 | mbedtls_debug_print_msg( |
| 740 | ssl, level, file, line, |
| 741 | "%s: %s(%u) extension.", ssl_tls13_get_hs_msg_name( hs_msg_type ), |
| 742 | mbedtls_ssl_get_extension_name( extension_type ), extension_type ); |
| 743 | } |
| 744 | |
| 745 | void mbedtls_ssl_print_extensions( const mbedtls_ssl_context *ssl, |
| 746 | int level, const char *file, int line, |
| 747 | int hs_msg_type, uint32_t extensions_mask, |
| 748 | const char *extra ) |
| 749 | { |
| 750 | |
| 751 | for( unsigned i = 0; |
| 752 | i < sizeof( extension_name_table ) / sizeof( extension_name_table[0] ); |
| 753 | i++ ) |
| 754 | { |
Jerry Yu | 79aa721 | 2022-11-08 21:30:21 +0800 | [diff] [blame] | 755 | mbedtls_ssl_print_extension( |
Jerry Yu | ea52ed9 | 2022-11-08 21:01:17 +0800 | [diff] [blame] | 756 | ssl, level, file, line, hs_msg_type, extension_type_table[i], |
Jerry Yu | 97be6a9 | 2022-11-09 22:43:31 +0800 | [diff] [blame] | 757 | extensions_mask & ( 1 << i ) ? "exists" : "does not exist", extra ); |
Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 758 | } |
| 759 | } |
| 760 | |
| 761 | #endif /* MBEDTLS_DEBUG_C */ |
| 762 | |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 763 | void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl, |
| 764 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info ) |
| 765 | { |
| 766 | ((void) ciphersuite_info); |
| 767 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 768 | #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] | 769 | if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 ) |
| 770 | ssl->handshake->update_checksum = ssl_update_checksum_sha384; |
| 771 | else |
| 772 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 773 | #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] | 774 | if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 ) |
| 775 | ssl->handshake->update_checksum = ssl_update_checksum_sha256; |
| 776 | else |
| 777 | #endif |
| 778 | { |
| 779 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 780 | return; |
| 781 | } |
| 782 | } |
| 783 | |
XiaokangQian | adab9a6 | 2022-07-18 07:41:26 +0000 | [diff] [blame] | 784 | void mbedtls_ssl_add_hs_hdr_to_checksum( mbedtls_ssl_context *ssl, |
| 785 | unsigned hs_type, |
| 786 | size_t total_hs_len ) |
Ronald Cron | 8f6d39a | 2022-03-10 18:56:50 +0100 | [diff] [blame] | 787 | { |
| 788 | unsigned char hs_hdr[4]; |
| 789 | |
| 790 | /* Build HS header for checksum update. */ |
| 791 | hs_hdr[0] = MBEDTLS_BYTE_0( hs_type ); |
| 792 | hs_hdr[1] = MBEDTLS_BYTE_2( total_hs_len ); |
| 793 | hs_hdr[2] = MBEDTLS_BYTE_1( total_hs_len ); |
| 794 | hs_hdr[3] = MBEDTLS_BYTE_0( total_hs_len ); |
| 795 | |
| 796 | ssl->handshake->update_checksum( ssl, hs_hdr, sizeof( hs_hdr ) ); |
| 797 | } |
| 798 | |
| 799 | void mbedtls_ssl_add_hs_msg_to_checksum( mbedtls_ssl_context *ssl, |
| 800 | unsigned hs_type, |
| 801 | unsigned char const *msg, |
| 802 | size_t msg_len ) |
| 803 | { |
| 804 | mbedtls_ssl_add_hs_hdr_to_checksum( ssl, hs_type, msg_len ); |
| 805 | ssl->handshake->update_checksum( ssl, msg, msg_len ); |
| 806 | } |
| 807 | |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 808 | void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl ) |
| 809 | { |
| 810 | ((void) ssl); |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 811 | #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] | 812 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 813 | psa_hash_abort( &ssl->handshake->fin_sha256_psa ); |
| 814 | psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 ); |
| 815 | #else |
| 816 | mbedtls_sha256_starts( &ssl->handshake->fin_sha256, 0 ); |
| 817 | #endif |
| 818 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 819 | #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] | 820 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 821 | psa_hash_abort( &ssl->handshake->fin_sha384_psa ); |
| 822 | psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 ); |
| 823 | #else |
Andrzej Kurek | a242e83 | 2022-08-11 10:03:14 -0400 | [diff] [blame] | 824 | mbedtls_sha512_starts( &ssl->handshake->fin_sha384, 1 ); |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 825 | #endif |
| 826 | #endif |
| 827 | } |
| 828 | |
| 829 | static void ssl_update_checksum_start( mbedtls_ssl_context *ssl, |
| 830 | const unsigned char *buf, size_t len ) |
| 831 | { |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 832 | #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] | 833 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 834 | psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len ); |
| 835 | #else |
| 836 | mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len ); |
| 837 | #endif |
| 838 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 839 | #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] | 840 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 841 | psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len ); |
| 842 | #else |
Andrzej Kurek | a242e83 | 2022-08-11 10:03:14 -0400 | [diff] [blame] | 843 | mbedtls_sha512_update( &ssl->handshake->fin_sha384, buf, len ); |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 844 | #endif |
| 845 | #endif |
Andrzej Kurek | eabeb30 | 2022-10-17 07:52:51 -0400 | [diff] [blame] | 846 | #if !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \ |
| 847 | !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
| 848 | (void) ssl; |
| 849 | (void) buf; |
| 850 | (void) len; |
| 851 | #endif |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 852 | } |
| 853 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 854 | #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] | 855 | static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl, |
| 856 | const unsigned char *buf, size_t len ) |
| 857 | { |
| 858 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 859 | psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len ); |
| 860 | #else |
| 861 | mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len ); |
| 862 | #endif |
| 863 | } |
| 864 | #endif |
| 865 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 866 | #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] | 867 | static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl, |
| 868 | const unsigned char *buf, size_t len ) |
| 869 | { |
| 870 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 871 | psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len ); |
| 872 | #else |
Andrzej Kurek | a242e83 | 2022-08-11 10:03:14 -0400 | [diff] [blame] | 873 | mbedtls_sha512_update( &ssl->handshake->fin_sha384, buf, len ); |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 874 | #endif |
| 875 | } |
| 876 | #endif |
| 877 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 878 | static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake ) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 879 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 880 | memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 881 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 882 | #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] | 883 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 884 | handshake->fin_sha256_psa = psa_hash_operation_init(); |
| 885 | psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 ); |
| 886 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 887 | mbedtls_sha256_init( &handshake->fin_sha256 ); |
TRodziewicz | 26371e4 | 2021-06-08 16:45:41 +0200 | [diff] [blame] | 888 | mbedtls_sha256_starts( &handshake->fin_sha256, 0 ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 889 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 890 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 891 | #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] | 892 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 893 | handshake->fin_sha384_psa = psa_hash_operation_init(); |
| 894 | psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 895 | #else |
Andrzej Kurek | a242e83 | 2022-08-11 10:03:14 -0400 | [diff] [blame] | 896 | mbedtls_sha512_init( &handshake->fin_sha384 ); |
| 897 | mbedtls_sha512_starts( &handshake->fin_sha384, 1 ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 898 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 899 | #endif |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 900 | |
| 901 | handshake->update_checksum = ssl_update_checksum_start; |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 902 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 903 | #if defined(MBEDTLS_DHM_C) |
| 904 | mbedtls_dhm_init( &handshake->dhm_ctx ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 905 | #endif |
Neil Armstrong | f3f4641 | 2022-04-12 14:43:39 +0200 | [diff] [blame] | 906 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 907 | mbedtls_ecdh_init( &handshake->ecdh_ctx ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 908 | #endif |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 909 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 910 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 911 | handshake->psa_pake_ctx = psa_pake_operation_init(); |
| 912 | handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT; |
| 913 | #else |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 914 | mbedtls_ecjpake_init( &handshake->ecjpake_ctx ); |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 915 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | 77c0646 | 2015-09-17 13:59:49 +0200 | [diff] [blame] | 916 | #if defined(MBEDTLS_SSL_CLI_C) |
| 917 | handshake->ecjpake_cache = NULL; |
| 918 | handshake->ecjpake_cache_len = 0; |
| 919 | #endif |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 920 | #endif |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 921 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 922 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Manuel Pégourié-Gonnard | 6b7301c | 2017-08-15 12:08:45 +0200 | [diff] [blame] | 923 | mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx ); |
Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 924 | #endif |
| 925 | |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 926 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 927 | handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET; |
| 928 | #endif |
Hanno Becker | 7517312 | 2019-02-06 16:18:31 +0000 | [diff] [blame] | 929 | |
| 930 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 931 | !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 932 | mbedtls_pk_init( &handshake->peer_pubkey ); |
| 933 | #endif |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 934 | } |
| 935 | |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 936 | void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform ) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 937 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 938 | memset( transform, 0, sizeof(mbedtls_ssl_transform) ); |
Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 939 | |
Przemyslaw Stekiel | 8f80fb9 | 2022-01-11 08:28:13 +0100 | [diff] [blame] | 940 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 941 | transform->psa_key_enc = MBEDTLS_SVC_KEY_ID_INIT; |
| 942 | transform->psa_key_dec = MBEDTLS_SVC_KEY_ID_INIT; |
Przemyslaw Stekiel | 6be9cf5 | 2022-01-19 16:00:22 +0100 | [diff] [blame] | 943 | #else |
| 944 | mbedtls_cipher_init( &transform->cipher_ctx_enc ); |
| 945 | mbedtls_cipher_init( &transform->cipher_ctx_dec ); |
Przemyslaw Stekiel | 8f80fb9 | 2022-01-11 08:28:13 +0100 | [diff] [blame] | 946 | #endif |
| 947 | |
Hanno Becker | fd86ca8 | 2020-11-30 08:54:23 +0000 | [diff] [blame] | 948 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
Neil Armstrong | 39b8e7d | 2022-02-23 09:24:45 +0100 | [diff] [blame] | 949 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 950 | transform->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT; |
| 951 | transform->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT; |
Neil Armstrong | cf8841a | 2022-02-24 11:17:45 +0100 | [diff] [blame] | 952 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 953 | mbedtls_md_init( &transform->md_ctx_enc ); |
| 954 | mbedtls_md_init( &transform->md_ctx_dec ); |
Hanno Becker | d56ed24 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 955 | #endif |
Neil Armstrong | cf8841a | 2022-02-24 11:17:45 +0100 | [diff] [blame] | 956 | #endif |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 957 | } |
| 958 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 959 | void mbedtls_ssl_session_init( mbedtls_ssl_session *session ) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 960 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 961 | memset( session, 0, sizeof(mbedtls_ssl_session) ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 962 | } |
| 963 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 964 | MBEDTLS_CHECK_RETURN_CRITICAL |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 965 | static int ssl_handshake_init( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 966 | { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 967 | /* Clear old handshake information if present */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 968 | if( ssl->transform_negotiate ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 969 | mbedtls_ssl_transform_free( ssl->transform_negotiate ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 970 | if( ssl->session_negotiate ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 971 | mbedtls_ssl_session_free( ssl->session_negotiate ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 972 | if( ssl->handshake ) |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 973 | mbedtls_ssl_handshake_free( ssl ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 974 | |
| 975 | /* |
| 976 | * Either the pointers are now NULL or cleared properly and can be freed. |
| 977 | * Now allocate missing structures. |
| 978 | */ |
| 979 | if( ssl->transform_negotiate == NULL ) |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 980 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 981 | ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 982 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 983 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 984 | if( ssl->session_negotiate == NULL ) |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 985 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 986 | ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 987 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 988 | |
Paul Bakker | 82788fb | 2014-10-20 13:59:19 +0200 | [diff] [blame] | 989 | if( ssl->handshake == NULL ) |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 990 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 991 | ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 992 | } |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 993 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 994 | /* If the buffers are too small - reallocate */ |
Andrzej Kurek | 8ea6872 | 2020-04-03 06:40:47 -0400 | [diff] [blame] | 995 | |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 996 | handle_buffer_resizing( ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN, |
| 997 | MBEDTLS_SSL_OUT_BUFFER_LEN ); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 998 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 999 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 1000 | /* All pointers should exist and can be directly freed without issue */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1001 | if( ssl->handshake == NULL || |
| 1002 | ssl->transform_negotiate == NULL || |
| 1003 | ssl->session_negotiate == NULL ) |
| 1004 | { |
Manuel Pégourié-Gonnard | b2a18a2 | 2015-05-27 16:29:56 +0200 | [diff] [blame] | 1005 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 1006 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1007 | mbedtls_free( ssl->handshake ); |
| 1008 | mbedtls_free( ssl->transform_negotiate ); |
| 1009 | mbedtls_free( ssl->session_negotiate ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 1010 | |
| 1011 | ssl->handshake = NULL; |
| 1012 | ssl->transform_negotiate = NULL; |
| 1013 | ssl->session_negotiate = NULL; |
| 1014 | |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 1015 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1016 | } |
| 1017 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 1018 | /* Initialize structures */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1019 | mbedtls_ssl_session_init( ssl->session_negotiate ); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1020 | mbedtls_ssl_transform_init( ssl->transform_negotiate ); |
Paul Bakker | 968afaa | 2014-07-09 11:09:24 +0200 | [diff] [blame] | 1021 | ssl_handshake_params_init( ssl->handshake ); |
| 1022 | |
Jerry Yu | d0766ec | 2022-09-22 10:46:57 +0800 | [diff] [blame] | 1023 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \ |
| 1024 | defined(MBEDTLS_SSL_SRV_C) && \ |
| 1025 | defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 1026 | ssl->handshake->new_session_tickets_count = |
| 1027 | ssl->conf->new_session_tickets_count ; |
| 1028 | #endif |
| 1029 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1030 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 1031 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 1032 | { |
| 1033 | ssl->handshake->alt_transform_out = ssl->transform_out; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 1034 | |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 1035 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 1036 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING; |
| 1037 | else |
| 1038 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING; |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 1039 | |
Hanno Becker | 0f57a65 | 2020-02-05 10:37:26 +0000 | [diff] [blame] | 1040 | mbedtls_ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 1041 | } |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 1042 | #endif |
| 1043 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 1044 | /* |
| 1045 | * curve_list is translated to IANA TLS group identifiers here because |
| 1046 | * mbedtls_ssl_conf_curves returns void and so can't return |
| 1047 | * any error codes. |
| 1048 | */ |
| 1049 | #if defined(MBEDTLS_ECP_C) |
| 1050 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 1051 | /* Heap allocate and translate curve_list from internal to IANA group ids */ |
| 1052 | if ( ssl->conf->curve_list != NULL ) |
| 1053 | { |
| 1054 | size_t length; |
| 1055 | const mbedtls_ecp_group_id *curve_list = ssl->conf->curve_list; |
| 1056 | |
| 1057 | for( length = 0; ( curve_list[length] != MBEDTLS_ECP_DP_NONE ) && |
| 1058 | ( length < MBEDTLS_ECP_DP_MAX ); length++ ) {} |
| 1059 | |
| 1060 | /* Leave room for zero termination */ |
| 1061 | uint16_t *group_list = mbedtls_calloc( length + 1, sizeof(uint16_t) ); |
| 1062 | if ( group_list == NULL ) |
| 1063 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 1064 | |
| 1065 | for( size_t i = 0; i < length; i++ ) |
| 1066 | { |
| 1067 | const mbedtls_ecp_curve_info *info = |
| 1068 | mbedtls_ecp_curve_info_from_grp_id( curve_list[i] ); |
| 1069 | if ( info == NULL ) |
| 1070 | { |
| 1071 | mbedtls_free( group_list ); |
| 1072 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); |
| 1073 | } |
| 1074 | group_list[i] = info->tls_id; |
| 1075 | } |
| 1076 | |
| 1077 | group_list[length] = 0; |
| 1078 | |
| 1079 | ssl->handshake->group_list = group_list; |
| 1080 | ssl->handshake->group_list_heap_allocated = 1; |
| 1081 | } |
| 1082 | else |
| 1083 | { |
| 1084 | ssl->handshake->group_list = ssl->conf->group_list; |
| 1085 | ssl->handshake->group_list_heap_allocated = 0; |
| 1086 | } |
| 1087 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
| 1088 | #endif /* MBEDTLS_ECP_C */ |
| 1089 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 1090 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1091 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Jerry Yu | a69269a | 2022-01-17 21:06:01 +0800 | [diff] [blame] | 1092 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Jerry Yu | 713013f | 2022-01-17 18:16:35 +0800 | [diff] [blame] | 1093 | /* Heap allocate and translate sig_hashes from internal hash identifiers to |
| 1094 | signature algorithms IANA identifiers. */ |
| 1095 | if ( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) && |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1096 | ssl->conf->sig_hashes != NULL ) |
| 1097 | { |
| 1098 | const int *md; |
| 1099 | const int *sig_hashes = ssl->conf->sig_hashes; |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 1100 | size_t sig_algs_len = 0; |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1101 | uint16_t *p; |
| 1102 | |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 1103 | #if defined(static_assert) |
| 1104 | static_assert( MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN |
| 1105 | <= ( SIZE_MAX - ( 2 * sizeof(uint16_t) ) ), |
| 1106 | "MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN too big" ); |
Jerry Yu | a68dca2 | 2022-01-20 16:28:27 +0800 | [diff] [blame] | 1107 | #endif |
| 1108 | |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1109 | for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ ) |
| 1110 | { |
| 1111 | if( mbedtls_ssl_hash_from_md_alg( *md ) == MBEDTLS_SSL_HASH_NONE ) |
| 1112 | continue; |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 1113 | #if defined(MBEDTLS_ECDSA_C) |
| 1114 | sig_algs_len += sizeof( uint16_t ); |
| 1115 | #endif |
Jerry Yu | a68dca2 | 2022-01-20 16:28:27 +0800 | [diff] [blame] | 1116 | |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 1117 | #if defined(MBEDTLS_RSA_C) |
| 1118 | sig_algs_len += sizeof( uint16_t ); |
| 1119 | #endif |
| 1120 | if( sig_algs_len > MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN ) |
| 1121 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1122 | } |
| 1123 | |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 1124 | if( sig_algs_len < MBEDTLS_SSL_MIN_SIG_ALG_LIST_LEN ) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1125 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); |
| 1126 | |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 1127 | ssl->handshake->sig_algs = mbedtls_calloc( 1, sig_algs_len + |
| 1128 | sizeof( uint16_t )); |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1129 | if( ssl->handshake->sig_algs == NULL ) |
| 1130 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 1131 | |
| 1132 | p = (uint16_t *)ssl->handshake->sig_algs; |
| 1133 | for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ ) |
| 1134 | { |
| 1135 | unsigned char hash = mbedtls_ssl_hash_from_md_alg( *md ); |
| 1136 | if( hash == MBEDTLS_SSL_HASH_NONE ) |
| 1137 | continue; |
Jerry Yu | 6106fdc | 2022-01-12 16:36:14 +0800 | [diff] [blame] | 1138 | #if defined(MBEDTLS_ECDSA_C) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1139 | *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_ECDSA); |
| 1140 | p++; |
Jerry Yu | 6106fdc | 2022-01-12 16:36:14 +0800 | [diff] [blame] | 1141 | #endif |
| 1142 | #if defined(MBEDTLS_RSA_C) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1143 | *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_RSA); |
| 1144 | p++; |
Jerry Yu | 6106fdc | 2022-01-12 16:36:14 +0800 | [diff] [blame] | 1145 | #endif |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1146 | } |
Gabor Mezei | 15b95a6 | 2022-05-09 16:37:58 +0200 | [diff] [blame] | 1147 | *p = MBEDTLS_TLS_SIG_NONE; |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1148 | ssl->handshake->sig_algs_heap_allocated = 1; |
| 1149 | } |
| 1150 | else |
Jerry Yu | a69269a | 2022-01-17 21:06:01 +0800 | [diff] [blame] | 1151 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1152 | { |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1153 | ssl->handshake->sig_algs_heap_allocated = 0; |
| 1154 | } |
Jerry Yu | cc53910 | 2022-06-27 16:27:35 +0800 | [diff] [blame] | 1155 | #endif /* !MBEDTLS_DEPRECATED_REMOVED */ |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 1156 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1157 | return( 0 ); |
| 1158 | } |
| 1159 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 1160 | #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] | 1161 | /* Dummy cookie callbacks for defaults */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 1162 | MBEDTLS_CHECK_RETURN_CRITICAL |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 1163 | static int ssl_cookie_write_dummy( void *ctx, |
| 1164 | unsigned char **p, unsigned char *end, |
| 1165 | const unsigned char *cli_id, size_t cli_id_len ) |
| 1166 | { |
| 1167 | ((void) ctx); |
| 1168 | ((void) p); |
| 1169 | ((void) end); |
| 1170 | ((void) cli_id); |
| 1171 | ((void) cli_id_len); |
| 1172 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1173 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 1174 | } |
| 1175 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 1176 | MBEDTLS_CHECK_RETURN_CRITICAL |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 1177 | static int ssl_cookie_check_dummy( void *ctx, |
| 1178 | const unsigned char *cookie, size_t cookie_len, |
| 1179 | const unsigned char *cli_id, size_t cli_id_len ) |
| 1180 | { |
| 1181 | ((void) ctx); |
| 1182 | ((void) cookie); |
| 1183 | ((void) cookie_len); |
| 1184 | ((void) cli_id); |
| 1185 | ((void) cli_id_len); |
| 1186 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1187 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 1188 | } |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 1189 | #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 1190 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1191 | /* |
| 1192 | * Initialize an SSL context |
| 1193 | */ |
Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 1194 | void mbedtls_ssl_init( mbedtls_ssl_context *ssl ) |
| 1195 | { |
| 1196 | memset( ssl, 0, sizeof( mbedtls_ssl_context ) ); |
| 1197 | } |
| 1198 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 1199 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1200 | static int ssl_conf_version_check( const mbedtls_ssl_context *ssl ) |
| 1201 | { |
Ronald Cron | 086ee0b | 2022-03-15 15:18:51 +0100 | [diff] [blame] | 1202 | const mbedtls_ssl_config *conf = ssl->conf; |
| 1203 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 1204 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Ronald Cron | 086ee0b | 2022-03-15 15:18:51 +0100 | [diff] [blame] | 1205 | if( mbedtls_ssl_conf_is_tls13_only( conf ) ) |
| 1206 | { |
XiaokangQian | ed582dd | 2022-04-13 08:21:05 +0000 | [diff] [blame] | 1207 | if( conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 1208 | { |
| 1209 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS 1.3 is not yet supported." ) ); |
| 1210 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 1211 | } |
| 1212 | |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1213 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls13 only." ) ); |
| 1214 | return( 0 ); |
| 1215 | } |
| 1216 | #endif |
| 1217 | |
| 1218 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Ronald Cron | 086ee0b | 2022-03-15 15:18:51 +0100 | [diff] [blame] | 1219 | if( mbedtls_ssl_conf_is_tls12_only( conf ) ) |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1220 | { |
| 1221 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls12 only." ) ); |
| 1222 | return( 0 ); |
| 1223 | } |
| 1224 | #endif |
| 1225 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 1226 | #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] | 1227 | if( mbedtls_ssl_conf_is_hybrid_tls12_tls13( conf ) ) |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1228 | { |
XiaokangQian | ed582dd | 2022-04-13 08:21:05 +0000 | [diff] [blame] | 1229 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 1230 | { |
| 1231 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS not yet supported in Hybrid TLS 1.3 + TLS 1.2" ) ); |
| 1232 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 1233 | } |
| 1234 | |
XiaokangQian | 8f9dfe4 | 2022-04-15 02:52:39 +0000 | [diff] [blame] | 1235 | if( conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 1236 | { |
| 1237 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS 1.3 server is not supported yet." ) ); |
| 1238 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 1239 | } |
| 1240 | |
| 1241 | |
Ronald Cron | e1d3f06 | 2022-02-10 14:50:54 +0100 | [diff] [blame] | 1242 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is TLS 1.3 or TLS 1.2." ) ); |
| 1243 | return( 0 ); |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1244 | } |
| 1245 | #endif |
| 1246 | |
| 1247 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "The SSL configuration is invalid." ) ); |
| 1248 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); |
| 1249 | } |
| 1250 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 1251 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1252 | static int ssl_conf_check(const mbedtls_ssl_context *ssl) |
| 1253 | { |
| 1254 | int ret; |
| 1255 | ret = ssl_conf_version_check( ssl ); |
| 1256 | if( ret != 0 ) |
| 1257 | return( ret ); |
| 1258 | |
Jerry Yu | def7ae4 | 2022-10-30 14:13:19 +0800 | [diff] [blame] | 1259 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 1260 | /* RFC 8446 section 4.4.3 |
| 1261 | * |
| 1262 | * If the verification fails, the receiver MUST terminate the handshake with |
| 1263 | * a "decrypt_error" alert. |
| 1264 | * |
| 1265 | * If the client is configured as TLS 1.3 only with optional verify, return |
| 1266 | * bad config. |
| 1267 | * |
| 1268 | */ |
| 1269 | if( mbedtls_ssl_conf_tls13_ephemeral_enabled( |
| 1270 | (mbedtls_ssl_context *)ssl ) && |
| 1271 | ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
| 1272 | ssl->conf->max_tls_version == MBEDTLS_SSL_VERSION_TLS1_3 && |
| 1273 | ssl->conf->min_tls_version == MBEDTLS_SSL_VERSION_TLS1_3 && |
| 1274 | ssl->conf->authmode == MBEDTLS_SSL_VERIFY_OPTIONAL ) |
| 1275 | { |
| 1276 | MBEDTLS_SSL_DEBUG_MSG( |
Dave Rodgman | e8734d8 | 2022-10-31 14:30:24 +0000 | [diff] [blame] | 1277 | 1, ( "Optional verify auth mode " |
Jerry Yu | def7ae4 | 2022-10-30 14:13:19 +0800 | [diff] [blame] | 1278 | "is not available for TLS 1.3 client" ) ); |
| 1279 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); |
| 1280 | } |
| 1281 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 1282 | |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1283 | /* Space for further checks */ |
| 1284 | |
| 1285 | return( 0 ); |
| 1286 | } |
| 1287 | |
Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 1288 | /* |
| 1289 | * Setup an SSL context |
| 1290 | */ |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 1291 | |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 1292 | int mbedtls_ssl_setup( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 1897af9 | 2015-05-10 23:27:38 +0200 | [diff] [blame] | 1293 | const mbedtls_ssl_config *conf ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1294 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1295 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1296 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; |
| 1297 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1298 | |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 1299 | ssl->conf = conf; |
Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 1300 | |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1301 | if( ( ret = ssl_conf_check( ssl ) ) != 0 ) |
| 1302 | return( ret ); |
| 1303 | |
Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 1304 | /* |
Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 1305 | * Prepare base structures |
Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 1306 | */ |
k-stachowiak | c9a5f02 | 2018-07-24 13:53:31 +0200 | [diff] [blame] | 1307 | |
| 1308 | /* Set to NULL in case of an error condition */ |
| 1309 | ssl->out_buf = NULL; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1310 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1311 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 1312 | ssl->in_buf_len = in_buf_len; |
| 1313 | #endif |
| 1314 | ssl->in_buf = mbedtls_calloc( 1, in_buf_len ); |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 1315 | if( ssl->in_buf == NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1316 | { |
Paul Elliott | d48d5c6 | 2021-01-07 14:47:05 +0000 | [diff] [blame] | 1317 | 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] | 1318 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1319 | goto error; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 1320 | } |
| 1321 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1322 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 1323 | ssl->out_buf_len = out_buf_len; |
| 1324 | #endif |
| 1325 | ssl->out_buf = mbedtls_calloc( 1, out_buf_len ); |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 1326 | if( ssl->out_buf == NULL ) |
| 1327 | { |
Paul Elliott | d48d5c6 | 2021-01-07 14:47:05 +0000 | [diff] [blame] | 1328 | 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] | 1329 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1330 | goto error; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1331 | } |
| 1332 | |
Hanno Becker | 3e6f8ab | 2020-02-05 10:40:57 +0000 | [diff] [blame] | 1333 | mbedtls_ssl_reset_in_out_pointers( ssl ); |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 1334 | |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 1335 | #if defined(MBEDTLS_SSL_DTLS_SRTP) |
Ron Eldor | 3adb992 | 2017-12-21 10:15:08 +0200 | [diff] [blame] | 1336 | memset( &ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info) ); |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 1337 | #endif |
| 1338 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1339 | if( ( ret = ssl_handshake_init( ssl ) ) != 0 ) |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1340 | goto error; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1341 | |
| 1342 | return( 0 ); |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1343 | |
| 1344 | error: |
| 1345 | mbedtls_free( ssl->in_buf ); |
| 1346 | mbedtls_free( ssl->out_buf ); |
| 1347 | |
| 1348 | ssl->conf = NULL; |
| 1349 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1350 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 1351 | ssl->in_buf_len = 0; |
| 1352 | ssl->out_buf_len = 0; |
| 1353 | #endif |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1354 | ssl->in_buf = NULL; |
| 1355 | ssl->out_buf = NULL; |
| 1356 | |
| 1357 | ssl->in_hdr = NULL; |
| 1358 | ssl->in_ctr = NULL; |
| 1359 | ssl->in_len = NULL; |
| 1360 | ssl->in_iv = NULL; |
| 1361 | ssl->in_msg = NULL; |
| 1362 | |
| 1363 | ssl->out_hdr = NULL; |
| 1364 | ssl->out_ctr = NULL; |
| 1365 | ssl->out_len = NULL; |
| 1366 | ssl->out_iv = NULL; |
| 1367 | ssl->out_msg = NULL; |
| 1368 | |
k-stachowiak | 9f7798e | 2018-07-31 16:52:32 +0200 | [diff] [blame] | 1369 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1370 | } |
| 1371 | |
| 1372 | /* |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 1373 | * Reset an initialized and used SSL context for re-use while retaining |
| 1374 | * all application-set variables, function pointers and data. |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 1375 | * |
| 1376 | * If partial is non-zero, keep data in the input buffer and client ID. |
| 1377 | * (Use when a DTLS client reconnects from the same port.) |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 1378 | */ |
XiaokangQian | 78b1fa7 | 2022-01-19 06:56:30 +0000 | [diff] [blame] | 1379 | void mbedtls_ssl_session_reset_msg_layer( mbedtls_ssl_context *ssl, |
| 1380 | int partial ) |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 1381 | { |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1382 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 1383 | size_t in_buf_len = ssl->in_buf_len; |
| 1384 | size_t out_buf_len = ssl->out_buf_len; |
| 1385 | #else |
| 1386 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; |
| 1387 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; |
| 1388 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1389 | |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1390 | #if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || !defined(MBEDTLS_SSL_SRV_C) |
| 1391 | partial = 0; |
Hanno Becker | 7e77213 | 2018-08-10 12:38:21 +0100 | [diff] [blame] | 1392 | #endif |
| 1393 | |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 1394 | /* Cancel any possibly running timer */ |
Hanno Becker | 0f57a65 | 2020-02-05 10:37:26 +0000 | [diff] [blame] | 1395 | mbedtls_ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 1396 | |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1397 | mbedtls_ssl_reset_in_out_pointers( ssl ); |
| 1398 | |
| 1399 | /* Reset incoming message parsing */ |
| 1400 | ssl->in_offt = NULL; |
| 1401 | ssl->nb_zero = 0; |
| 1402 | ssl->in_msgtype = 0; |
| 1403 | ssl->in_msglen = 0; |
| 1404 | ssl->in_hslen = 0; |
| 1405 | ssl->keep_current_message = 0; |
| 1406 | ssl->transform_in = NULL; |
| 1407 | |
| 1408 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 1409 | ssl->next_record_offset = 0; |
| 1410 | ssl->in_epoch = 0; |
| 1411 | #endif |
| 1412 | |
| 1413 | /* Keep current datagram if partial == 1 */ |
| 1414 | if( partial == 0 ) |
| 1415 | { |
| 1416 | ssl->in_left = 0; |
| 1417 | memset( ssl->in_buf, 0, in_buf_len ); |
| 1418 | } |
| 1419 | |
Ronald Cron | ad8c17b | 2022-06-10 17:18:09 +0200 | [diff] [blame] | 1420 | ssl->send_alert = 0; |
| 1421 | |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1422 | /* Reset outgoing message writing */ |
| 1423 | ssl->out_msgtype = 0; |
| 1424 | ssl->out_msglen = 0; |
| 1425 | ssl->out_left = 0; |
| 1426 | memset( ssl->out_buf, 0, out_buf_len ); |
| 1427 | memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) ); |
| 1428 | ssl->transform_out = NULL; |
| 1429 | |
| 1430 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 1431 | mbedtls_ssl_dtls_replay_reset( ssl ); |
| 1432 | #endif |
| 1433 | |
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1434 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1435 | if( ssl->transform ) |
| 1436 | { |
| 1437 | mbedtls_ssl_transform_free( ssl->transform ); |
| 1438 | mbedtls_free( ssl->transform ); |
| 1439 | ssl->transform = NULL; |
| 1440 | } |
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1441 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 1442 | |
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1443 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 1444 | mbedtls_ssl_transform_free( ssl->transform_application ); |
| 1445 | mbedtls_free( ssl->transform_application ); |
| 1446 | ssl->transform_application = NULL; |
| 1447 | |
| 1448 | if( ssl->handshake != NULL ) |
| 1449 | { |
| 1450 | mbedtls_ssl_transform_free( ssl->handshake->transform_earlydata ); |
| 1451 | mbedtls_free( ssl->handshake->transform_earlydata ); |
| 1452 | ssl->handshake->transform_earlydata = NULL; |
| 1453 | |
| 1454 | mbedtls_ssl_transform_free( ssl->handshake->transform_handshake ); |
| 1455 | mbedtls_free( ssl->handshake->transform_handshake ); |
| 1456 | ssl->handshake->transform_handshake = NULL; |
| 1457 | } |
| 1458 | |
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1459 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1460 | } |
| 1461 | |
| 1462 | int mbedtls_ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial ) |
| 1463 | { |
| 1464 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 1465 | |
| 1466 | ssl->state = MBEDTLS_SSL_HELLO_REQUEST; |
| 1467 | |
XiaokangQian | 78b1fa7 | 2022-01-19 06:56:30 +0000 | [diff] [blame] | 1468 | mbedtls_ssl_session_reset_msg_layer( ssl, partial ); |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1469 | |
| 1470 | /* Reset renegotiation state */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1471 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 1472 | ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE; |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 1473 | ssl->renego_records_seen = 0; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1474 | |
| 1475 | ssl->verify_data_len = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1476 | memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN ); |
| 1477 | 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] | 1478 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1479 | ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1480 | |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1481 | ssl->session_in = NULL; |
Hanno Becker | 7864090 | 2018-08-13 16:35:15 +0100 | [diff] [blame] | 1482 | ssl->session_out = NULL; |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 1483 | if( ssl->session ) |
| 1484 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1485 | mbedtls_ssl_session_free( ssl->session ); |
| 1486 | mbedtls_free( ssl->session ); |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 1487 | ssl->session = NULL; |
| 1488 | } |
| 1489 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1490 | #if defined(MBEDTLS_SSL_ALPN) |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 1491 | ssl->alpn_chosen = NULL; |
| 1492 | #endif |
| 1493 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 1494 | #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C) |
David Horstmann | 3b2276a | 2022-10-06 14:49:08 +0100 | [diff] [blame] | 1495 | int free_cli_id = 1; |
Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 1496 | #if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) |
David Horstmann | 3b2276a | 2022-10-06 14:49:08 +0100 | [diff] [blame] | 1497 | free_cli_id = ( partial == 0 ); |
Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 1498 | #endif |
David Horstmann | 3b2276a | 2022-10-06 14:49:08 +0100 | [diff] [blame] | 1499 | if( free_cli_id ) |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 1500 | { |
| 1501 | mbedtls_free( ssl->cli_id ); |
| 1502 | ssl->cli_id = NULL; |
| 1503 | ssl->cli_id_len = 0; |
| 1504 | } |
Manuel Pégourié-Gonnard | 43c0218 | 2014-07-22 17:32:01 +0200 | [diff] [blame] | 1505 | #endif |
| 1506 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1507 | if( ( ret = ssl_handshake_init( ssl ) ) != 0 ) |
| 1508 | return( ret ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1509 | |
| 1510 | return( 0 ); |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 1511 | } |
| 1512 | |
Manuel Pégourié-Gonnard | 779e429 | 2013-08-03 13:50:48 +0200 | [diff] [blame] | 1513 | /* |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 1514 | * Reset an initialized and used SSL context for re-use while retaining |
| 1515 | * all application-set variables, function pointers and data. |
| 1516 | */ |
| 1517 | int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl ) |
| 1518 | { |
Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 1519 | return( mbedtls_ssl_session_reset_int( ssl, 0 ) ); |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 1520 | } |
| 1521 | |
| 1522 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1523 | * SSL set accessors |
| 1524 | */ |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1525 | void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1526 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1527 | conf->endpoint = endpoint; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1528 | } |
| 1529 | |
Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 1530 | 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] | 1531 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1532 | conf->transport = transport; |
Manuel Pégourié-Gonnard | 0b1ff29 | 2014-02-06 13:04:16 +0100 | [diff] [blame] | 1533 | } |
| 1534 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1535 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1536 | 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] | 1537 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1538 | conf->anti_replay = mode; |
Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 1539 | } |
| 1540 | #endif |
| 1541 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1542 | 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] | 1543 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1544 | conf->badmac_limit = limit; |
Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 1545 | } |
Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 1546 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1547 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 1548 | |
Hanno Becker | 1841b0a | 2018-08-24 11:13:57 +0100 | [diff] [blame] | 1549 | void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl, |
| 1550 | unsigned allow_packing ) |
Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 1551 | { |
| 1552 | ssl->disable_datagram_packing = !allow_packing; |
| 1553 | } |
| 1554 | |
| 1555 | void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf, |
| 1556 | uint32_t min, uint32_t max ) |
Manuel Pégourié-Gonnard | 905dd24 | 2014-10-01 12:03:55 +0200 | [diff] [blame] | 1557 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1558 | conf->hs_timeout_min = min; |
| 1559 | conf->hs_timeout_max = max; |
Manuel Pégourié-Gonnard | 905dd24 | 2014-10-01 12:03:55 +0200 | [diff] [blame] | 1560 | } |
| 1561 | #endif |
| 1562 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1563 | void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1564 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1565 | conf->authmode = authmode; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1566 | } |
| 1567 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1568 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1569 | void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 1570 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 1571 | void *p_vrfy ) |
| 1572 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1573 | conf->f_vrfy = f_vrfy; |
| 1574 | conf->p_vrfy = p_vrfy; |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 1575 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1576 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 1577 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1578 | void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf, |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 1579 | int (*f_rng)(void *, unsigned char *, size_t), |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1580 | void *p_rng ) |
| 1581 | { |
Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 1582 | conf->f_rng = f_rng; |
| 1583 | conf->p_rng = p_rng; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1584 | } |
| 1585 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1586 | void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | fd47423 | 2015-06-23 16:34:24 +0200 | [diff] [blame] | 1587 | void (*f_dbg)(void *, int, const char *, int, const char *), |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1588 | void *p_dbg ) |
| 1589 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1590 | conf->f_dbg = f_dbg; |
| 1591 | conf->p_dbg = p_dbg; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1592 | } |
| 1593 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1594 | void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 1595 | void *p_bio, |
Simon Butcher | e846b51 | 2016-03-01 17:31:49 +0000 | [diff] [blame] | 1596 | mbedtls_ssl_send_t *f_send, |
| 1597 | mbedtls_ssl_recv_t *f_recv, |
| 1598 | mbedtls_ssl_recv_timeout_t *f_recv_timeout ) |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 1599 | { |
| 1600 | ssl->p_bio = p_bio; |
| 1601 | ssl->f_send = f_send; |
| 1602 | ssl->f_recv = f_recv; |
| 1603 | ssl->f_recv_timeout = f_recv_timeout; |
Manuel Pégourié-Gonnard | 97fd52c | 2015-05-06 15:38:52 +0100 | [diff] [blame] | 1604 | } |
| 1605 | |
Manuel Pégourié-Gonnard | 6e7aaca | 2018-08-20 10:37:23 +0200 | [diff] [blame] | 1606 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 1607 | void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu ) |
| 1608 | { |
| 1609 | ssl->mtu = mtu; |
| 1610 | } |
| 1611 | #endif |
| 1612 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1613 | 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] | 1614 | { |
| 1615 | conf->read_timeout = timeout; |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 1616 | } |
| 1617 | |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 1618 | void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl, |
| 1619 | void *p_timer, |
Simon Butcher | e846b51 | 2016-03-01 17:31:49 +0000 | [diff] [blame] | 1620 | mbedtls_ssl_set_timer_t *f_set_timer, |
| 1621 | mbedtls_ssl_get_timer_t *f_get_timer ) |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 1622 | { |
| 1623 | ssl->p_timer = p_timer; |
| 1624 | ssl->f_set_timer = f_set_timer; |
| 1625 | ssl->f_get_timer = f_get_timer; |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 1626 | |
| 1627 | /* Make sure we start with no timer running */ |
Hanno Becker | 0f57a65 | 2020-02-05 10:37:26 +0000 | [diff] [blame] | 1628 | mbedtls_ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 1629 | } |
| 1630 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1631 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1632 | void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf, |
Hanno Becker | a637ff6 | 2021-04-15 08:42:48 +0100 | [diff] [blame] | 1633 | void *p_cache, |
| 1634 | mbedtls_ssl_cache_get_t *f_get_cache, |
| 1635 | mbedtls_ssl_cache_set_t *f_set_cache ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1636 | { |
Manuel Pégourié-Gonnard | 5cb3308 | 2015-05-06 18:06:26 +0100 | [diff] [blame] | 1637 | conf->p_cache = p_cache; |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1638 | conf->f_get_cache = f_get_cache; |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1639 | conf->f_set_cache = f_set_cache; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1640 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1641 | #endif /* MBEDTLS_SSL_SRV_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1642 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1643 | #if defined(MBEDTLS_SSL_CLI_C) |
| 1644 | 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] | 1645 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1646 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1647 | |
| 1648 | if( ssl == NULL || |
| 1649 | session == NULL || |
| 1650 | ssl->session_negotiate == NULL || |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1651 | ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT ) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1652 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1653 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1654 | } |
| 1655 | |
Hanno Becker | e810bbc | 2021-05-14 16:01:05 +0100 | [diff] [blame] | 1656 | if( ssl->handshake->resume == 1 ) |
| 1657 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 1658 | |
Jerry Yu | 2109206 | 2022-10-10 21:21:31 +0800 | [diff] [blame] | 1659 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 1660 | if( session->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 ) |
Jerry Yu | 40afab6 | 2022-10-08 10:42:13 +0800 | [diff] [blame] | 1661 | { |
Jerry Yu | 2109206 | 2022-10-10 21:21:31 +0800 | [diff] [blame] | 1662 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 1663 | mbedtls_ssl_ciphersuite_from_id( session->ciphersuite ); |
| 1664 | |
| 1665 | if( mbedtls_ssl_validate_ciphersuite( |
| 1666 | ssl, ciphersuite_info, MBEDTLS_SSL_VERSION_TLS1_3, |
| 1667 | MBEDTLS_SSL_VERSION_TLS1_3 ) != 0 ) |
| 1668 | { |
| 1669 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "%d is not a valid TLS 1.3 ciphersuite.", |
| 1670 | session->ciphersuite ) ); |
| 1671 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1672 | } |
Jerry Yu | 40afab6 | 2022-10-08 10:42:13 +0800 | [diff] [blame] | 1673 | } |
Jerry Yu | 2109206 | 2022-10-10 21:21:31 +0800 | [diff] [blame] | 1674 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Jerry Yu | 40afab6 | 2022-10-08 10:42:13 +0800 | [diff] [blame] | 1675 | |
Hanno Becker | 52055ae | 2019-02-06 14:30:46 +0000 | [diff] [blame] | 1676 | if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate, |
| 1677 | session ) ) != 0 ) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1678 | return( ret ); |
| 1679 | |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 1680 | ssl->handshake->resume = 1; |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1681 | |
| 1682 | return( 0 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1683 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1684 | #endif /* MBEDTLS_SSL_CLI_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1685 | |
Mateusz Starzyk | 06b07fb | 2021-02-18 13:55:21 +0100 | [diff] [blame] | 1686 | void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf, |
| 1687 | const int *ciphersuites ) |
| 1688 | { |
Hanno Becker | d60b6c6 | 2021-04-29 12:04:11 +0100 | [diff] [blame] | 1689 | conf->ciphersuite_list = ciphersuites; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1690 | } |
| 1691 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 1692 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | cadebe5 | 2021-08-24 10:36:45 +0800 | [diff] [blame] | 1693 | void mbedtls_ssl_conf_tls13_key_exchange_modes( mbedtls_ssl_config *conf, |
Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 1694 | const int kex_modes ) |
| 1695 | { |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 1696 | 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] | 1697 | } |
Xiaokang Qian | 72de95d | 2022-10-25 02:54:33 +0000 | [diff] [blame] | 1698 | |
| 1699 | #if defined(MBEDTLS_SSL_EARLY_DATA) |
| 1700 | void mbedtls_ssl_tls13_conf_early_data( mbedtls_ssl_config *conf, |
Xiaokang Qian | 72dbfef | 2022-10-26 06:33:57 +0000 | [diff] [blame] | 1701 | int early_data_enabled ) |
Xiaokang Qian | 72de95d | 2022-10-25 02:54:33 +0000 | [diff] [blame] | 1702 | { |
| 1703 | conf->early_data_enabled = early_data_enabled; |
| 1704 | } |
| 1705 | #endif /* MBEDTLS_SSL_EARLY_DATA */ |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 1706 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 1707 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1708 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 6e3ee3a | 2015-06-17 10:58:20 +0200 | [diff] [blame] | 1709 | void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf, |
Nicholas Wilson | 2088e2e | 2015-09-08 16:53:18 +0100 | [diff] [blame] | 1710 | const mbedtls_x509_crt_profile *profile ) |
Manuel Pégourié-Gonnard | 6e3ee3a | 2015-06-17 10:58:20 +0200 | [diff] [blame] | 1711 | { |
| 1712 | conf->cert_profile = profile; |
| 1713 | } |
| 1714 | |
Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 1715 | static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert ) |
| 1716 | { |
| 1717 | mbedtls_ssl_key_cert *cur = key_cert, *next; |
| 1718 | |
| 1719 | while( cur != NULL ) |
| 1720 | { |
| 1721 | next = cur->next; |
| 1722 | mbedtls_free( cur ); |
| 1723 | cur = next; |
| 1724 | } |
| 1725 | } |
| 1726 | |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1727 | /* Append a new keycert entry to a (possibly empty) list */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 1728 | MBEDTLS_CHECK_RETURN_CRITICAL |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1729 | static int ssl_append_key_cert( mbedtls_ssl_key_cert **head, |
| 1730 | mbedtls_x509_crt *cert, |
| 1731 | mbedtls_pk_context *key ) |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1732 | { |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1733 | mbedtls_ssl_key_cert *new_cert; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1734 | |
Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 1735 | if( cert == NULL ) |
| 1736 | { |
| 1737 | /* Free list if cert is null */ |
| 1738 | ssl_key_cert_free( *head ); |
| 1739 | *head = NULL; |
| 1740 | return( 0 ); |
| 1741 | } |
| 1742 | |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1743 | new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) ); |
| 1744 | if( new_cert == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 1745 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1746 | |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1747 | new_cert->cert = cert; |
| 1748 | new_cert->key = key; |
| 1749 | new_cert->next = NULL; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1750 | |
Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 1751 | /* 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] | 1752 | if( *head == NULL ) |
Paul Bakker | 0333b97 | 2013-11-04 17:08:28 +0100 | [diff] [blame] | 1753 | { |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1754 | *head = new_cert; |
Paul Bakker | 0333b97 | 2013-11-04 17:08:28 +0100 | [diff] [blame] | 1755 | } |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1756 | else |
| 1757 | { |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1758 | mbedtls_ssl_key_cert *cur = *head; |
| 1759 | while( cur->next != NULL ) |
| 1760 | cur = cur->next; |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1761 | cur->next = new_cert; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1762 | } |
| 1763 | |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1764 | return( 0 ); |
| 1765 | } |
| 1766 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1767 | int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1768 | mbedtls_x509_crt *own_cert, |
| 1769 | mbedtls_pk_context *pk_key ) |
| 1770 | { |
Manuel Pégourié-Gonnard | 17a40cd | 2015-05-10 23:17:17 +0200 | [diff] [blame] | 1771 | 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] | 1772 | } |
| 1773 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1774 | void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 1775 | mbedtls_x509_crt *ca_chain, |
| 1776 | mbedtls_x509_crl *ca_crl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1777 | { |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 1778 | conf->ca_chain = ca_chain; |
| 1779 | conf->ca_crl = ca_crl; |
Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 1780 | |
| 1781 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 1782 | /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb() |
| 1783 | * cannot be used together. */ |
| 1784 | conf->f_ca_cb = NULL; |
| 1785 | conf->p_ca_cb = NULL; |
| 1786 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1787 | } |
Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 1788 | |
| 1789 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 1790 | void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf, |
Hanno Becker | afd0b0a | 2019-03-27 16:55:01 +0000 | [diff] [blame] | 1791 | mbedtls_x509_crt_ca_cb_t f_ca_cb, |
Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 1792 | void *p_ca_cb ) |
| 1793 | { |
| 1794 | conf->f_ca_cb = f_ca_cb; |
| 1795 | conf->p_ca_cb = p_ca_cb; |
| 1796 | |
| 1797 | /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb() |
| 1798 | * cannot be used together. */ |
| 1799 | conf->ca_chain = NULL; |
| 1800 | conf->ca_crl = NULL; |
| 1801 | } |
| 1802 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1803 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | eb2c658 | 2012-09-27 19:15:01 +0000 | [diff] [blame] | 1804 | |
Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 1805 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Glenn Strauss | 6989407 | 2022-01-24 12:58:00 -0500 | [diff] [blame] | 1806 | const unsigned char *mbedtls_ssl_get_hs_sni( mbedtls_ssl_context *ssl, |
| 1807 | size_t *name_len ) |
| 1808 | { |
| 1809 | *name_len = ssl->handshake->sni_name_len; |
| 1810 | return( ssl->handshake->sni_name ); |
| 1811 | } |
| 1812 | |
Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 1813 | int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl, |
| 1814 | mbedtls_x509_crt *own_cert, |
| 1815 | mbedtls_pk_context *pk_key ) |
| 1816 | { |
| 1817 | return( ssl_append_key_cert( &ssl->handshake->sni_key_cert, |
| 1818 | own_cert, pk_key ) ); |
| 1819 | } |
Manuel Pégourié-Gonnard | 22bfa4b | 2015-05-11 08:46:37 +0200 | [diff] [blame] | 1820 | |
| 1821 | void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl, |
| 1822 | mbedtls_x509_crt *ca_chain, |
| 1823 | mbedtls_x509_crl *ca_crl ) |
| 1824 | { |
| 1825 | ssl->handshake->sni_ca_chain = ca_chain; |
| 1826 | ssl->handshake->sni_ca_crl = ca_crl; |
| 1827 | } |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 1828 | |
Glenn Strauss | 999ef70 | 2022-03-11 01:37:23 -0500 | [diff] [blame] | 1829 | #if defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED) |
| 1830 | void mbedtls_ssl_set_hs_dn_hints( mbedtls_ssl_context *ssl, |
| 1831 | const mbedtls_x509_crt *crt) |
| 1832 | { |
| 1833 | ssl->handshake->dn_hints = crt; |
| 1834 | } |
| 1835 | #endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */ |
| 1836 | |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 1837 | void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl, |
| 1838 | int authmode ) |
| 1839 | { |
| 1840 | ssl->handshake->sni_authmode = authmode; |
| 1841 | } |
Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 1842 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
| 1843 | |
Hanno Becker | 8927c83 | 2019-04-03 12:52:50 +0100 | [diff] [blame] | 1844 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 1845 | void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl, |
| 1846 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), |
| 1847 | void *p_vrfy ) |
| 1848 | { |
| 1849 | ssl->f_vrfy = f_vrfy; |
| 1850 | ssl->p_vrfy = p_vrfy; |
| 1851 | } |
| 1852 | #endif |
| 1853 | |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 1854 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 1855 | /* |
| 1856 | * Set EC J-PAKE password for current handshake |
| 1857 | */ |
Valerio Setti | 02c25b5 | 2022-11-15 14:08:42 +0100 | [diff] [blame] | 1858 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 1859 | int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl, |
| 1860 | const unsigned char *pw, |
| 1861 | size_t pw_len ) |
| 1862 | { |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 1863 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 1864 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1865 | psa_pake_role_t psa_role; |
| 1866 | psa_status_t status; |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 1867 | |
Janos Follath | 8eb6413 | 2016-06-03 15:40:57 +0100 | [diff] [blame] | 1868 | if( ssl->handshake == NULL || ssl->conf == NULL ) |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 1869 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1870 | |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 1871 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 1872 | psa_role = PSA_PAKE_ROLE_SERVER; |
| 1873 | else |
| 1874 | psa_role = PSA_PAKE_ROLE_CLIENT; |
| 1875 | |
Valerio Setti | aca21b7 | 2022-11-17 18:17:01 +0100 | [diff] [blame] | 1876 | /* Empty password is not valid */ |
| 1877 | if( ( pw == NULL) || ( pw_len == 0 ) ) |
| 1878 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 1879 | |
Valerio Setti | aca21b7 | 2022-11-17 18:17:01 +0100 | [diff] [blame] | 1880 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 1881 | psa_set_key_algorithm( &attributes, PSA_ALG_JPAKE ); |
| 1882 | psa_set_key_type( &attributes, PSA_KEY_TYPE_PASSWORD ); |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 1883 | |
Valerio Setti | aca21b7 | 2022-11-17 18:17:01 +0100 | [diff] [blame] | 1884 | status = psa_import_key( &attributes, pw, pw_len, |
| 1885 | &ssl->handshake->psa_pake_password ); |
| 1886 | if( status != PSA_SUCCESS ) |
| 1887 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 1888 | |
| 1889 | psa_pake_cs_set_algorithm( &cipher_suite, PSA_ALG_JPAKE ); |
| 1890 | psa_pake_cs_set_primitive( &cipher_suite, |
| 1891 | PSA_PAKE_PRIMITIVE( PSA_PAKE_PRIMITIVE_TYPE_ECC, |
| 1892 | PSA_ECC_FAMILY_SECP_R1, |
| 1893 | 256) ); |
| 1894 | psa_pake_cs_set_hash( &cipher_suite, PSA_ALG_SHA_256 ); |
| 1895 | |
| 1896 | status = psa_pake_setup( &ssl->handshake->psa_pake_ctx, &cipher_suite ); |
| 1897 | if( status != PSA_SUCCESS ) |
| 1898 | { |
| 1899 | psa_destroy_key( ssl->handshake->psa_pake_password ); |
| 1900 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 1901 | } |
| 1902 | |
| 1903 | status = psa_pake_set_role( &ssl->handshake->psa_pake_ctx, psa_role ); |
| 1904 | if( status != PSA_SUCCESS ) |
| 1905 | { |
| 1906 | psa_destroy_key( ssl->handshake->psa_pake_password ); |
| 1907 | psa_pake_abort( &ssl->handshake->psa_pake_ctx ); |
| 1908 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 1909 | } |
| 1910 | |
Valerio Setti | 70d1fa5 | 2022-12-07 16:20:27 +0100 | [diff] [blame] | 1911 | status = psa_pake_set_password_key( &ssl->handshake->psa_pake_ctx, |
Valerio Setti | aca21b7 | 2022-11-17 18:17:01 +0100 | [diff] [blame] | 1912 | ssl->handshake->psa_pake_password ); |
| 1913 | if( status != PSA_SUCCESS ) |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 1914 | { |
Valerio Setti | aca21b7 | 2022-11-17 18:17:01 +0100 | [diff] [blame] | 1915 | psa_destroy_key( ssl->handshake->psa_pake_password ); |
| 1916 | psa_pake_abort( &ssl->handshake->psa_pake_ctx ); |
| 1917 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 1918 | } |
| 1919 | |
| 1920 | ssl->handshake->psa_pake_ctx_is_ok = 1; |
| 1921 | |
| 1922 | return( 0 ); |
Valerio Setti | 02c25b5 | 2022-11-15 14:08:42 +0100 | [diff] [blame] | 1923 | } |
Valerio Setti | a9a97dc | 2022-11-28 18:26:16 +0100 | [diff] [blame] | 1924 | |
| 1925 | int mbedtls_ssl_set_hs_ecjpake_password_opaque( mbedtls_ssl_context *ssl, |
| 1926 | mbedtls_svc_key_id_t pwd ) |
| 1927 | { |
| 1928 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 1929 | psa_pake_role_t psa_role; |
| 1930 | psa_status_t status; |
| 1931 | |
| 1932 | if( ssl->handshake == NULL || ssl->conf == NULL ) |
| 1933 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1934 | |
Valerio Setti | a9a97dc | 2022-11-28 18:26:16 +0100 | [diff] [blame] | 1935 | if( mbedtls_svc_key_id_is_null( pwd ) ) |
| 1936 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Valerio Setti | a9a97dc | 2022-11-28 18:26:16 +0100 | [diff] [blame] | 1937 | |
| 1938 | psa_pake_cs_set_algorithm( &cipher_suite, PSA_ALG_JPAKE ); |
| 1939 | psa_pake_cs_set_primitive( &cipher_suite, |
| 1940 | PSA_PAKE_PRIMITIVE( PSA_PAKE_PRIMITIVE_TYPE_ECC, |
| 1941 | PSA_ECC_FAMILY_SECP_R1, |
| 1942 | 256) ); |
| 1943 | psa_pake_cs_set_hash( &cipher_suite, PSA_ALG_SHA_256 ); |
| 1944 | |
| 1945 | status = psa_pake_setup( &ssl->handshake->psa_pake_ctx, &cipher_suite ); |
| 1946 | if( status != PSA_SUCCESS ) |
Valerio Setti | ae7fe7e | 2022-12-07 17:36:59 +0100 | [diff] [blame] | 1947 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
Valerio Setti | a9a97dc | 2022-11-28 18:26:16 +0100 | [diff] [blame] | 1948 | |
Valerio Setti | c689ed8 | 2022-12-07 14:40:38 +0100 | [diff] [blame] | 1949 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 1950 | psa_role = PSA_PAKE_ROLE_SERVER; |
| 1951 | else |
| 1952 | psa_role = PSA_PAKE_ROLE_CLIENT; |
| 1953 | |
Valerio Setti | a9a97dc | 2022-11-28 18:26:16 +0100 | [diff] [blame] | 1954 | status = psa_pake_set_role( &ssl->handshake->psa_pake_ctx, psa_role ); |
| 1955 | if( status != PSA_SUCCESS ) |
Valerio Setti | 757f359 | 2022-12-02 11:07:11 +0100 | [diff] [blame] | 1956 | goto error; |
Valerio Setti | a9a97dc | 2022-11-28 18:26:16 +0100 | [diff] [blame] | 1957 | |
Valerio Setti | eb3f788 | 2022-12-08 18:42:58 +0100 | [diff] [blame^] | 1958 | status = psa_pake_set_password_key( &ssl->handshake->psa_pake_ctx, pwd ); |
Valerio Setti | a9a97dc | 2022-11-28 18:26:16 +0100 | [diff] [blame] | 1959 | if( status != PSA_SUCCESS ) |
Valerio Setti | 757f359 | 2022-12-02 11:07:11 +0100 | [diff] [blame] | 1960 | goto error; |
Valerio Setti | a9a97dc | 2022-11-28 18:26:16 +0100 | [diff] [blame] | 1961 | |
| 1962 | ssl->handshake->psa_pake_ctx_is_ok = 1; |
| 1963 | |
| 1964 | return( 0 ); |
Valerio Setti | 757f359 | 2022-12-02 11:07:11 +0100 | [diff] [blame] | 1965 | |
| 1966 | error: |
| 1967 | psa_pake_abort( &ssl->handshake->psa_pake_ctx ); |
| 1968 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
Valerio Setti | a9a97dc | 2022-11-28 18:26:16 +0100 | [diff] [blame] | 1969 | } |
Valerio Setti | 02c25b5 | 2022-11-15 14:08:42 +0100 | [diff] [blame] | 1970 | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 1971 | int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl, |
| 1972 | const unsigned char *pw, |
| 1973 | size_t pw_len ) |
| 1974 | { |
| 1975 | mbedtls_ecjpake_role role; |
| 1976 | |
| 1977 | if( ssl->handshake == NULL || ssl->conf == NULL ) |
| 1978 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1979 | |
Valerio Setti | c689ed8 | 2022-12-07 14:40:38 +0100 | [diff] [blame] | 1980 | /* Empty password is not valid */ |
| 1981 | if( ( pw == NULL) || ( pw_len == 0 ) ) |
| 1982 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1983 | |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 1984 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 1985 | role = MBEDTLS_ECJPAKE_SERVER; |
| 1986 | else |
| 1987 | role = MBEDTLS_ECJPAKE_CLIENT; |
| 1988 | |
| 1989 | return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx, |
| 1990 | role, |
| 1991 | MBEDTLS_MD_SHA256, |
| 1992 | MBEDTLS_ECP_DP_SECP256R1, |
| 1993 | pw, pw_len ) ); |
| 1994 | } |
Valerio Setti | 02c25b5 | 2022-11-15 14:08:42 +0100 | [diff] [blame] | 1995 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 1996 | #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 1997 | |
Ronald Cron | 73fe8df | 2022-10-05 14:31:43 +0200 | [diff] [blame] | 1998 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED) |
Ronald Cron | d29e13e | 2022-10-19 10:33:48 +0200 | [diff] [blame] | 1999 | int mbedtls_ssl_conf_has_static_psk( mbedtls_ssl_config const *conf ) |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 2000 | { |
Ronald Cron | d29e13e | 2022-10-19 10:33:48 +0200 | [diff] [blame] | 2001 | if( conf->psk_identity == NULL || |
| 2002 | conf->psk_identity_len == 0 ) |
| 2003 | { |
| 2004 | return( 0 ); |
| 2005 | } |
| 2006 | |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 2007 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Ronald Cron | d29e13e | 2022-10-19 10:33:48 +0200 | [diff] [blame] | 2008 | if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) ) |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 2009 | return( 1 ); |
| 2010 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Ronald Cron | d29e13e | 2022-10-19 10:33:48 +0200 | [diff] [blame] | 2011 | |
| 2012 | if( conf->psk != NULL && conf->psk_len != 0 ) |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 2013 | return( 1 ); |
| 2014 | |
| 2015 | return( 0 ); |
| 2016 | } |
| 2017 | |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2018 | static void ssl_conf_remove_psk( mbedtls_ssl_config *conf ) |
| 2019 | { |
| 2020 | /* Remove reference to existing PSK, if any. */ |
| 2021 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2022 | if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) ) |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2023 | { |
| 2024 | /* The maintenance of the PSK key slot is the |
| 2025 | * user's responsibility. */ |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2026 | conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT; |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2027 | } |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2028 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 2029 | if( conf->psk != NULL ) |
| 2030 | { |
| 2031 | mbedtls_platform_zeroize( conf->psk, conf->psk_len ); |
| 2032 | |
| 2033 | mbedtls_free( conf->psk ); |
| 2034 | conf->psk = NULL; |
| 2035 | conf->psk_len = 0; |
| 2036 | } |
| 2037 | |
| 2038 | /* Remove reference to PSK identity, if any. */ |
| 2039 | if( conf->psk_identity != NULL ) |
| 2040 | { |
| 2041 | mbedtls_free( conf->psk_identity ); |
| 2042 | conf->psk_identity = NULL; |
| 2043 | conf->psk_identity_len = 0; |
| 2044 | } |
| 2045 | } |
| 2046 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 2047 | /* This function assumes that PSK identity in the SSL config is unset. |
| 2048 | * It checks that the provided identity is well-formed and attempts |
| 2049 | * to make a copy of it in the SSL config. |
| 2050 | * On failure, the PSK identity in the config remains unset. */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 2051 | MBEDTLS_CHECK_RETURN_CRITICAL |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 2052 | static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf, |
| 2053 | unsigned char const *psk_identity, |
| 2054 | size_t psk_identity_len ) |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 2055 | { |
Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 2056 | /* Identity len will be encoded on two bytes */ |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 2057 | if( psk_identity == NULL || |
Ronald Cron | 2a87e9b | 2022-10-19 10:55:26 +0200 | [diff] [blame] | 2058 | psk_identity_len == 0 || |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 2059 | ( psk_identity_len >> 16 ) != 0 || |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 2060 | psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN ) |
Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 2061 | { |
| 2062 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2063 | } |
| 2064 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 2065 | conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ); |
| 2066 | if( conf->psk_identity == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 2067 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 2068 | |
Manuel Pégourié-Gonnard | 120fdbd | 2015-05-07 17:07:50 +0100 | [diff] [blame] | 2069 | conf->psk_identity_len = psk_identity_len; |
Manuel Pégourié-Gonnard | 120fdbd | 2015-05-07 17:07:50 +0100 | [diff] [blame] | 2070 | memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len ); |
Paul Bakker | 5ad403f | 2013-09-18 21:21:30 +0200 | [diff] [blame] | 2071 | |
| 2072 | return( 0 ); |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 2073 | } |
| 2074 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 2075 | int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf, |
| 2076 | const unsigned char *psk, size_t psk_len, |
| 2077 | const unsigned char *psk_identity, size_t psk_identity_len ) |
| 2078 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2079 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 2080 | |
| 2081 | /* We currently only support one PSK, raw or opaque. */ |
Ronald Cron | d29e13e | 2022-10-19 10:33:48 +0200 | [diff] [blame] | 2082 | if( mbedtls_ssl_conf_has_static_psk( conf ) ) |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 2083 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 2084 | |
| 2085 | /* Check and set raw PSK */ |
Piotr Nowicki | 9926eaf | 2019-11-20 14:54:36 +0100 | [diff] [blame] | 2086 | if( psk == NULL ) |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 2087 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Piotr Nowicki | 9926eaf | 2019-11-20 14:54:36 +0100 | [diff] [blame] | 2088 | if( psk_len == 0 ) |
| 2089 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2090 | if( psk_len > MBEDTLS_PSK_MAX_LEN ) |
| 2091 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2092 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 2093 | if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ) |
| 2094 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 2095 | conf->psk_len = psk_len; |
| 2096 | memcpy( conf->psk, psk, conf->psk_len ); |
| 2097 | |
| 2098 | /* Check and set PSK Identity */ |
| 2099 | ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len ); |
| 2100 | if( ret != 0 ) |
| 2101 | ssl_conf_remove_psk( conf ); |
| 2102 | |
| 2103 | return( ret ); |
| 2104 | } |
| 2105 | |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2106 | static void ssl_remove_psk( mbedtls_ssl_context *ssl ) |
| 2107 | { |
| 2108 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2109 | if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) ) |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2110 | { |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 2111 | /* The maintenance of the external PSK key slot is the |
| 2112 | * user's responsibility. */ |
| 2113 | if( ssl->handshake->psk_opaque_is_internal ) |
| 2114 | { |
| 2115 | psa_destroy_key( ssl->handshake->psk_opaque ); |
| 2116 | ssl->handshake->psk_opaque_is_internal = 0; |
| 2117 | } |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2118 | ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT; |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2119 | } |
Neil Armstrong | e952a30 | 2022-05-03 10:22:14 +0200 | [diff] [blame] | 2120 | #else |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2121 | if( ssl->handshake->psk != NULL ) |
| 2122 | { |
| 2123 | mbedtls_platform_zeroize( ssl->handshake->psk, |
| 2124 | ssl->handshake->psk_len ); |
| 2125 | mbedtls_free( ssl->handshake->psk ); |
| 2126 | ssl->handshake->psk_len = 0; |
| 2127 | } |
Neil Armstrong | e952a30 | 2022-05-03 10:22:14 +0200 | [diff] [blame] | 2128 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2129 | } |
| 2130 | |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 2131 | int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl, |
| 2132 | const unsigned char *psk, size_t psk_len ) |
| 2133 | { |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 2134 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 2135 | psa_key_attributes_t key_attributes = psa_key_attributes_init(); |
Jerry Yu | 5d01c05 | 2022-08-17 10:18:10 +0800 | [diff] [blame] | 2136 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Jerry Yu | 24b8c81 | 2022-08-20 19:06:56 +0800 | [diff] [blame] | 2137 | psa_algorithm_t alg = PSA_ALG_NONE; |
Jerry Yu | 5d01c05 | 2022-08-17 10:18:10 +0800 | [diff] [blame] | 2138 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 2139 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 2140 | |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 2141 | if( psk == NULL || ssl->handshake == NULL ) |
| 2142 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2143 | |
| 2144 | if( psk_len > MBEDTLS_PSK_MAX_LEN ) |
| 2145 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2146 | |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2147 | ssl_remove_psk( ssl ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 2148 | |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 2149 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Jerry Yu | ccc68a4 | 2022-07-26 16:39:20 +0800 | [diff] [blame] | 2150 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 2151 | if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 ) |
| 2152 | { |
Jerry Yu | 6cf6b47 | 2022-08-16 14:50:28 +0800 | [diff] [blame] | 2153 | if( ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384 ) |
Jerry Yu | ccc68a4 | 2022-07-26 16:39:20 +0800 | [diff] [blame] | 2154 | alg = PSA_ALG_TLS12_PSK_TO_MS( PSA_ALG_SHA_384 ); |
| 2155 | else |
| 2156 | alg = PSA_ALG_TLS12_PSK_TO_MS( PSA_ALG_SHA_256 ); |
| 2157 | psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE ); |
| 2158 | } |
| 2159 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 2160 | |
Jerry Yu | 568ec25 | 2022-07-22 21:27:34 +0800 | [diff] [blame] | 2161 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | ccc68a4 | 2022-07-26 16:39:20 +0800 | [diff] [blame] | 2162 | if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 ) |
| 2163 | { |
| 2164 | alg = PSA_ALG_HKDF_EXTRACT( PSA_ALG_ANY_HASH ); |
| 2165 | psa_set_key_usage_flags( &key_attributes, |
| 2166 | PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT ); |
| 2167 | } |
| 2168 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 2169 | |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 2170 | psa_set_key_algorithm( &key_attributes, alg ); |
| 2171 | psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE ); |
| 2172 | |
| 2173 | status = psa_import_key( &key_attributes, psk, psk_len, &key ); |
| 2174 | if( status != PSA_SUCCESS ) |
| 2175 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 2176 | |
| 2177 | /* Allow calling psa_destroy_key() on psk remove */ |
| 2178 | ssl->handshake->psk_opaque_is_internal = 1; |
| 2179 | return mbedtls_ssl_set_hs_psk_opaque( ssl, key ); |
| 2180 | #else |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 2181 | if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 2182 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 2183 | |
| 2184 | ssl->handshake->psk_len = psk_len; |
| 2185 | memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len ); |
| 2186 | |
| 2187 | return( 0 ); |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 2188 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 2189 | } |
| 2190 | |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2191 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 2192 | int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf, |
Andrzej Kurek | 03e0146 | 2022-01-03 12:53:24 +0100 | [diff] [blame] | 2193 | mbedtls_svc_key_id_t psk, |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2194 | const unsigned char *psk_identity, |
| 2195 | size_t psk_identity_len ) |
| 2196 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2197 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 2198 | |
| 2199 | /* We currently only support one PSK, raw or opaque. */ |
Ronald Cron | d29e13e | 2022-10-19 10:33:48 +0200 | [diff] [blame] | 2200 | if( mbedtls_ssl_conf_has_static_psk( conf ) ) |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 2201 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2202 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 2203 | /* Check and set opaque PSK */ |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2204 | if( mbedtls_svc_key_id_is_null( psk ) ) |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 2205 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2206 | conf->psk_opaque = psk; |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 2207 | |
| 2208 | /* Check and set PSK Identity */ |
| 2209 | ret = ssl_conf_set_psk_identity( conf, psk_identity, |
| 2210 | psk_identity_len ); |
| 2211 | if( ret != 0 ) |
| 2212 | ssl_conf_remove_psk( conf ); |
| 2213 | |
| 2214 | return( ret ); |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2215 | } |
| 2216 | |
Przemyslaw Stekiel | 6928a51 | 2022-02-03 13:50:35 +0100 | [diff] [blame] | 2217 | int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl, |
| 2218 | mbedtls_svc_key_id_t psk ) |
| 2219 | { |
| 2220 | if( ( mbedtls_svc_key_id_is_null( psk ) ) || |
| 2221 | ( ssl->handshake == NULL ) ) |
| 2222 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2223 | |
| 2224 | ssl_remove_psk( ssl ); |
| 2225 | ssl->handshake->psk_opaque = psk; |
| 2226 | return( 0 ); |
| 2227 | } |
| 2228 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 2229 | |
Jerry Yu | 8897c07 | 2022-08-12 13:56:53 +0800 | [diff] [blame] | 2230 | #if defined(MBEDTLS_SSL_SRV_C) |
Przemyslaw Stekiel | 6928a51 | 2022-02-03 13:50:35 +0100 | [diff] [blame] | 2231 | void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf, |
| 2232 | int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *, |
| 2233 | size_t), |
| 2234 | void *p_psk ) |
| 2235 | { |
| 2236 | conf->f_psk = f_psk; |
| 2237 | conf->p_psk = p_psk; |
| 2238 | } |
Jerry Yu | 8897c07 | 2022-08-12 13:56:53 +0800 | [diff] [blame] | 2239 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 2240 | |
Ronald Cron | 73fe8df | 2022-10-05 14:31:43 +0200 | [diff] [blame] | 2241 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */ |
Przemyslaw Stekiel | 6928a51 | 2022-02-03 13:50:35 +0100 | [diff] [blame] | 2242 | |
| 2243 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 301711e | 2022-04-26 16:57:05 +0200 | [diff] [blame] | 2244 | static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode( |
| 2245 | psa_algorithm_t alg ) |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 2246 | { |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 2247 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2248 | if( alg == PSA_ALG_CBC_NO_PADDING ) |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 2249 | return( MBEDTLS_SSL_MODE_CBC ); |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 2250 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2251 | if( PSA_ALG_IS_AEAD( alg ) ) |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 2252 | return( MBEDTLS_SSL_MODE_AEAD ); |
Gilles Peskine | 301711e | 2022-04-26 16:57:05 +0200 | [diff] [blame] | 2253 | return( MBEDTLS_SSL_MODE_STREAM ); |
| 2254 | } |
| 2255 | |
| 2256 | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
| 2257 | |
| 2258 | static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode( |
| 2259 | mbedtls_cipher_mode_t mode ) |
| 2260 | { |
| 2261 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
| 2262 | if( mode == MBEDTLS_MODE_CBC ) |
| 2263 | return( MBEDTLS_SSL_MODE_CBC ); |
| 2264 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
| 2265 | |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 2266 | #if defined(MBEDTLS_GCM_C) || \ |
| 2267 | defined(MBEDTLS_CCM_C) || \ |
| 2268 | defined(MBEDTLS_CHACHAPOLY_C) |
| 2269 | if( mode == MBEDTLS_MODE_GCM || |
| 2270 | mode == MBEDTLS_MODE_CCM || |
| 2271 | mode == MBEDTLS_MODE_CHACHAPOLY ) |
| 2272 | return( MBEDTLS_SSL_MODE_AEAD ); |
| 2273 | #endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */ |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 2274 | |
| 2275 | return( MBEDTLS_SSL_MODE_STREAM ); |
| 2276 | } |
Gilles Peskine | 301711e | 2022-04-26 16:57:05 +0200 | [diff] [blame] | 2277 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 2278 | |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2279 | static mbedtls_ssl_mode_t mbedtls_ssl_get_actual_mode( |
| 2280 | mbedtls_ssl_mode_t base_mode, |
| 2281 | int encrypt_then_mac ) |
| 2282 | { |
| 2283 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
| 2284 | if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED && |
| 2285 | base_mode == MBEDTLS_SSL_MODE_CBC ) |
| 2286 | { |
| 2287 | return( MBEDTLS_SSL_MODE_CBC_ETM ); |
| 2288 | } |
| 2289 | #else |
| 2290 | (void) encrypt_then_mac; |
| 2291 | #endif |
| 2292 | return( base_mode ); |
| 2293 | } |
| 2294 | |
Neil Armstrong | ab555e0 | 2022-04-04 11:07:59 +0200 | [diff] [blame] | 2295 | mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_transform( |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2296 | const mbedtls_ssl_transform *transform ) |
| 2297 | { |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2298 | mbedtls_ssl_mode_t base_mode = mbedtls_ssl_get_base_mode( |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2299 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2300 | transform->psa_alg |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2301 | #else |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2302 | mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) |
| 2303 | #endif |
| 2304 | ); |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2305 | |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2306 | int encrypt_then_mac = 0; |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 2307 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2308 | encrypt_then_mac = transform->encrypt_then_mac; |
| 2309 | #endif |
| 2310 | return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) ); |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2311 | } |
| 2312 | |
Neil Armstrong | ab555e0 | 2022-04-04 11:07:59 +0200 | [diff] [blame] | 2313 | mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_ciphersuite( |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 2314 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2315 | int encrypt_then_mac, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 2316 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2317 | const mbedtls_ssl_ciphersuite_t *suite ) |
| 2318 | { |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2319 | mbedtls_ssl_mode_t base_mode = MBEDTLS_SSL_MODE_STREAM; |
| 2320 | |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2321 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 2322 | psa_status_t status; |
| 2323 | psa_algorithm_t alg; |
| 2324 | psa_key_type_t type; |
| 2325 | size_t size; |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2326 | status = mbedtls_ssl_cipher_to_psa( suite->cipher, 0, &alg, &type, &size ); |
| 2327 | if( status == PSA_SUCCESS ) |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2328 | base_mode = mbedtls_ssl_get_base_mode( alg ); |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2329 | #else |
| 2330 | const mbedtls_cipher_info_t *cipher = |
| 2331 | mbedtls_cipher_info_from_type( suite->cipher ); |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2332 | if( cipher != NULL ) |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2333 | { |
| 2334 | base_mode = |
| 2335 | mbedtls_ssl_get_base_mode( |
| 2336 | mbedtls_cipher_info_get_mode( cipher ) ); |
| 2337 | } |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2338 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 2339 | |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2340 | #if !defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
| 2341 | int encrypt_then_mac = 0; |
| 2342 | #endif |
| 2343 | return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) ); |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2344 | } |
| 2345 | |
Neil Armstrong | 8395d7a | 2022-05-18 11:44:56 +0200 | [diff] [blame] | 2346 | #if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 2347 | |
| 2348 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2349 | /* Serialization of TLS 1.3 sessions: |
| 2350 | * |
| 2351 | * struct { |
Xiaokang Qian | 126bf8e | 2022-10-13 02:22:40 +0000 | [diff] [blame] | 2352 | * opaque hostname<0..2^16-1>; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2353 | * uint64 ticket_received; |
| 2354 | * uint32 ticket_lifetime; |
Jerry Yu | 3419107 | 2022-08-18 10:32:09 +0800 | [diff] [blame] | 2355 | * opaque ticket<1..2^16-1>; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2356 | * } ClientOnlyData; |
| 2357 | * |
| 2358 | * struct { |
| 2359 | * uint8 endpoint; |
| 2360 | * uint8 ciphersuite[2]; |
| 2361 | * uint32 ticket_age_add; |
| 2362 | * uint8 ticket_flags; |
| 2363 | * opaque resumption_key<0..255>; |
| 2364 | * select ( endpoint ) { |
| 2365 | * case client: ClientOnlyData; |
| 2366 | * case server: uint64 start_time; |
| 2367 | * }; |
| 2368 | * } serialized_session_tls13; |
| 2369 | * |
| 2370 | */ |
| 2371 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 2372 | MBEDTLS_CHECK_RETURN_CRITICAL |
| 2373 | static int ssl_tls13_session_save( const mbedtls_ssl_session *session, |
| 2374 | unsigned char *buf, |
| 2375 | size_t buf_len, |
| 2376 | size_t *olen ) |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2377 | { |
| 2378 | unsigned char *p = buf; |
Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 2379 | #if defined(MBEDTLS_SSL_CLI_C) && \ |
| 2380 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 2381 | size_t hostname_len = ( session->hostname == NULL ) ? |
Xiaokang Qian | ed0620c | 2022-10-12 06:58:13 +0000 | [diff] [blame] | 2382 | 0 : strlen( session->hostname ) + 1; |
Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 2383 | #endif |
Jerry Yu | bc7c1a4 | 2022-07-21 22:57:37 +0800 | [diff] [blame] | 2384 | size_t needed = 1 /* endpoint */ |
| 2385 | + 2 /* ciphersuite */ |
| 2386 | + 4 /* ticket_age_add */ |
Jerry Yu | 3419107 | 2022-08-18 10:32:09 +0800 | [diff] [blame] | 2387 | + 1 /* ticket_flags */ |
| 2388 | + 1; /* resumption_key length */ |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 2389 | *olen = 0; |
Jerry Yu | 3419107 | 2022-08-18 10:32:09 +0800 | [diff] [blame] | 2390 | |
| 2391 | if( session->resumption_key_len > MBEDTLS_SSL_TLS1_3_TICKET_RESUMPTION_KEY_LEN ) |
| 2392 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2393 | needed += session->resumption_key_len; /* resumption_key */ |
| 2394 | |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2395 | #if defined(MBEDTLS_HAVE_TIME) |
| 2396 | needed += 8; /* start_time or ticket_received */ |
| 2397 | #endif |
| 2398 | |
| 2399 | #if defined(MBEDTLS_SSL_CLI_C) |
| 2400 | if( session->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 2401 | { |
Xiaokang Qian | 126bf8e | 2022-10-13 02:22:40 +0000 | [diff] [blame] | 2402 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Xiaokang Qian | bc663a0 | 2022-10-09 11:14:39 +0000 | [diff] [blame] | 2403 | needed += 2 /* hostname_len */ |
Xiaokang Qian | 2f9efd3 | 2022-10-10 11:24:08 +0000 | [diff] [blame] | 2404 | + hostname_len; /* hostname */ |
Xiaokang Qian | 281fd1b | 2022-09-20 11:35:41 +0000 | [diff] [blame] | 2405 | #endif |
| 2406 | |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2407 | needed += 4 /* ticket_lifetime */ |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 2408 | + 2; /* ticket_len */ |
Jerry Yu | 3419107 | 2022-08-18 10:32:09 +0800 | [diff] [blame] | 2409 | |
| 2410 | /* Check size_t overflow */ |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 2411 | if( session->ticket_len > SIZE_MAX - needed ) |
| 2412 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jerry Yu | 3419107 | 2022-08-18 10:32:09 +0800 | [diff] [blame] | 2413 | |
Jerry Yu | e28d974 | 2022-08-18 15:44:03 +0800 | [diff] [blame] | 2414 | needed += session->ticket_len; /* ticket */ |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2415 | } |
| 2416 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 2417 | |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 2418 | *olen = needed; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2419 | if( needed > buf_len ) |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 2420 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2421 | |
| 2422 | p[0] = session->endpoint; |
| 2423 | MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 1 ); |
| 2424 | MBEDTLS_PUT_UINT32_BE( session->ticket_age_add, p, 3 ); |
| 2425 | p[7] = session->ticket_flags; |
| 2426 | |
| 2427 | /* save resumption_key */ |
Jerry Yu | bc7c1a4 | 2022-07-21 22:57:37 +0800 | [diff] [blame] | 2428 | p[8] = session->resumption_key_len; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2429 | p += 9; |
Jerry Yu | bc7c1a4 | 2022-07-21 22:57:37 +0800 | [diff] [blame] | 2430 | memcpy( p, session->resumption_key, session->resumption_key_len ); |
| 2431 | p += session->resumption_key_len; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2432 | |
| 2433 | #if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C) |
| 2434 | if( session->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 2435 | { |
| 2436 | MBEDTLS_PUT_UINT64_BE( (uint64_t) session->start, p, 0 ); |
| 2437 | p += 8; |
| 2438 | } |
| 2439 | #endif /* MBEDTLS_HAVE_TIME */ |
| 2440 | |
| 2441 | #if defined(MBEDTLS_SSL_CLI_C) |
| 2442 | if( session->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 2443 | { |
Xiaokang Qian | ed0620c | 2022-10-12 06:58:13 +0000 | [diff] [blame] | 2444 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 2445 | MBEDTLS_PUT_UINT16_BE( hostname_len, p, 0 ); |
| 2446 | p += 2; |
Xiaokang Qian | 126bf8e | 2022-10-13 02:22:40 +0000 | [diff] [blame] | 2447 | if( hostname_len > 0 ) |
Xiaokang Qian | ed0620c | 2022-10-12 06:58:13 +0000 | [diff] [blame] | 2448 | { |
| 2449 | /* save host name */ |
| 2450 | memcpy( p, session->hostname, hostname_len ); |
| 2451 | p += hostname_len; |
| 2452 | } |
| 2453 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
| 2454 | |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2455 | #if defined(MBEDTLS_HAVE_TIME) |
| 2456 | MBEDTLS_PUT_UINT64_BE( (uint64_t) session->ticket_received, p, 0 ); |
| 2457 | p += 8; |
| 2458 | #endif |
| 2459 | MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 ); |
| 2460 | p += 4; |
| 2461 | |
| 2462 | MBEDTLS_PUT_UINT16_BE( session->ticket_len, p, 0 ); |
| 2463 | p += 2; |
Jerry Yu | 3419107 | 2022-08-18 10:32:09 +0800 | [diff] [blame] | 2464 | |
| 2465 | if( session->ticket != NULL && session->ticket_len > 0 ) |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2466 | { |
| 2467 | memcpy( p, session->ticket, session->ticket_len ); |
| 2468 | p += session->ticket_len; |
| 2469 | } |
| 2470 | } |
| 2471 | #endif /* MBEDTLS_SSL_CLI_C */ |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 2472 | return( 0 ); |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2473 | } |
| 2474 | |
| 2475 | MBEDTLS_CHECK_RETURN_CRITICAL |
| 2476 | static int ssl_tls13_session_load( mbedtls_ssl_session *session, |
| 2477 | const unsigned char *buf, |
| 2478 | size_t len ) |
| 2479 | { |
| 2480 | const unsigned char *p = buf; |
| 2481 | const unsigned char *end = buf + len; |
| 2482 | |
| 2483 | if( end - p < 9 ) |
| 2484 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2485 | session->endpoint = p[0]; |
| 2486 | session->ciphersuite = MBEDTLS_GET_UINT16_BE( p, 1 ); |
| 2487 | session->ticket_age_add = MBEDTLS_GET_UINT32_BE( p, 3 ); |
| 2488 | session->ticket_flags = p[7]; |
| 2489 | |
| 2490 | /* load resumption_key */ |
Jerry Yu | bc7c1a4 | 2022-07-21 22:57:37 +0800 | [diff] [blame] | 2491 | session->resumption_key_len = p[8]; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2492 | p += 9; |
| 2493 | |
Jerry Yu | bc7c1a4 | 2022-07-21 22:57:37 +0800 | [diff] [blame] | 2494 | if( end - p < session->resumption_key_len ) |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2495 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2496 | |
Jerry Yu | bc7c1a4 | 2022-07-21 22:57:37 +0800 | [diff] [blame] | 2497 | if( sizeof( session->resumption_key ) < session->resumption_key_len ) |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2498 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jerry Yu | bc7c1a4 | 2022-07-21 22:57:37 +0800 | [diff] [blame] | 2499 | memcpy( session->resumption_key, p, session->resumption_key_len ); |
| 2500 | p += session->resumption_key_len; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2501 | |
| 2502 | #if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C) |
| 2503 | if( session->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 2504 | { |
| 2505 | if( end - p < 8 ) |
| 2506 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2507 | session->start = MBEDTLS_GET_UINT64_BE( p, 0 ); |
| 2508 | p += 8; |
| 2509 | } |
| 2510 | #endif /* MBEDTLS_HAVE_TIME */ |
| 2511 | |
| 2512 | #if defined(MBEDTLS_SSL_CLI_C) |
| 2513 | if( session->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 2514 | { |
Xiaokang Qian | ed0620c | 2022-10-12 06:58:13 +0000 | [diff] [blame] | 2515 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \ |
| 2516 | defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 2517 | size_t hostname_len; |
| 2518 | /* load host name */ |
| 2519 | if( end - p < 2 ) |
| 2520 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2521 | hostname_len = MBEDTLS_GET_UINT16_BE( p, 0 ); |
| 2522 | p += 2; |
| 2523 | |
| 2524 | if( end - p < ( long int )hostname_len ) |
| 2525 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2526 | if( hostname_len > 0 ) |
| 2527 | { |
| 2528 | session->hostname = mbedtls_calloc( 1, hostname_len ); |
| 2529 | if( session->hostname == NULL ) |
| 2530 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 2531 | memcpy( session->hostname, p, hostname_len ); |
| 2532 | p += hostname_len; |
| 2533 | } |
| 2534 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION && |
| 2535 | MBEDTLS_SSL_SESSION_TICKETS */ |
| 2536 | |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2537 | #if defined(MBEDTLS_HAVE_TIME) |
| 2538 | if( end - p < 8 ) |
| 2539 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2540 | session->ticket_received = MBEDTLS_GET_UINT64_BE( p, 0 ); |
| 2541 | p += 8; |
| 2542 | #endif |
| 2543 | if( end - p < 4 ) |
| 2544 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2545 | session->ticket_lifetime = MBEDTLS_GET_UINT32_BE( p, 0 ); |
| 2546 | p += 4; |
| 2547 | |
| 2548 | if( end - p < 2 ) |
| 2549 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2550 | session->ticket_len = MBEDTLS_GET_UINT16_BE( p, 0 ); |
| 2551 | p += 2; |
| 2552 | |
| 2553 | if( end - p < ( long int )session->ticket_len ) |
| 2554 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2555 | if( session->ticket_len > 0 ) |
| 2556 | { |
| 2557 | session->ticket = mbedtls_calloc( 1, session->ticket_len ); |
| 2558 | if( session->ticket == NULL ) |
| 2559 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 2560 | memcpy( session->ticket, p, session->ticket_len ); |
| 2561 | p += session->ticket_len; |
| 2562 | } |
| 2563 | } |
| 2564 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 2565 | |
| 2566 | return( 0 ); |
| 2567 | |
| 2568 | } |
| 2569 | #else /* MBEDTLS_SSL_SESSION_TICKETS */ |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 2570 | MBEDTLS_CHECK_RETURN_CRITICAL |
| 2571 | static int ssl_tls13_session_save( const mbedtls_ssl_session *session, |
| 2572 | unsigned char *buf, |
| 2573 | size_t buf_len, |
| 2574 | size_t *olen ) |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 2575 | { |
| 2576 | ((void) session); |
| 2577 | ((void) buf); |
| 2578 | ((void) buf_len); |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 2579 | *olen = 0; |
| 2580 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 2581 | } |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2582 | |
| 2583 | static int ssl_tls13_session_load( const mbedtls_ssl_session *session, |
| 2584 | unsigned char *buf, |
| 2585 | size_t buf_len ) |
| 2586 | { |
| 2587 | ((void) session); |
| 2588 | ((void) buf); |
| 2589 | ((void) buf_len); |
| 2590 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 2591 | } |
| 2592 | #endif /* !MBEDTLS_SSL_SESSION_TICKETS */ |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 2593 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 2594 | |
Przemyslaw Stekiel | f57b456 | 2022-01-25 00:04:18 +0100 | [diff] [blame] | 2595 | 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] | 2596 | size_t taglen, |
| 2597 | psa_algorithm_t *alg, |
| 2598 | psa_key_type_t *key_type, |
| 2599 | size_t *key_size ) |
| 2600 | { |
| 2601 | switch ( mbedtls_cipher_type ) |
| 2602 | { |
| 2603 | case MBEDTLS_CIPHER_AES_128_CBC: |
| 2604 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 2605 | *key_type = PSA_KEY_TYPE_AES; |
| 2606 | *key_size = 128; |
| 2607 | break; |
| 2608 | case MBEDTLS_CIPHER_AES_128_CCM: |
| 2609 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 2610 | *key_type = PSA_KEY_TYPE_AES; |
| 2611 | *key_size = 128; |
| 2612 | break; |
| 2613 | case MBEDTLS_CIPHER_AES_128_GCM: |
| 2614 | *alg = PSA_ALG_GCM; |
| 2615 | *key_type = PSA_KEY_TYPE_AES; |
| 2616 | *key_size = 128; |
| 2617 | break; |
| 2618 | case MBEDTLS_CIPHER_AES_192_CCM: |
| 2619 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 2620 | *key_type = PSA_KEY_TYPE_AES; |
| 2621 | *key_size = 192; |
| 2622 | break; |
| 2623 | case MBEDTLS_CIPHER_AES_192_GCM: |
| 2624 | *alg = PSA_ALG_GCM; |
| 2625 | *key_type = PSA_KEY_TYPE_AES; |
| 2626 | *key_size = 192; |
| 2627 | break; |
| 2628 | case MBEDTLS_CIPHER_AES_256_CBC: |
| 2629 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 2630 | *key_type = PSA_KEY_TYPE_AES; |
| 2631 | *key_size = 256; |
| 2632 | break; |
| 2633 | case MBEDTLS_CIPHER_AES_256_CCM: |
| 2634 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 2635 | *key_type = PSA_KEY_TYPE_AES; |
| 2636 | *key_size = 256; |
| 2637 | break; |
| 2638 | case MBEDTLS_CIPHER_AES_256_GCM: |
| 2639 | *alg = PSA_ALG_GCM; |
| 2640 | *key_type = PSA_KEY_TYPE_AES; |
| 2641 | *key_size = 256; |
| 2642 | break; |
| 2643 | case MBEDTLS_CIPHER_ARIA_128_CBC: |
| 2644 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 2645 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2646 | *key_size = 128; |
| 2647 | break; |
| 2648 | case MBEDTLS_CIPHER_ARIA_128_CCM: |
| 2649 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 2650 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2651 | *key_size = 128; |
| 2652 | break; |
| 2653 | case MBEDTLS_CIPHER_ARIA_128_GCM: |
| 2654 | *alg = PSA_ALG_GCM; |
| 2655 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2656 | *key_size = 128; |
| 2657 | break; |
| 2658 | case MBEDTLS_CIPHER_ARIA_192_CCM: |
| 2659 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 2660 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2661 | *key_size = 192; |
| 2662 | break; |
| 2663 | case MBEDTLS_CIPHER_ARIA_192_GCM: |
| 2664 | *alg = PSA_ALG_GCM; |
| 2665 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2666 | *key_size = 192; |
| 2667 | break; |
| 2668 | case MBEDTLS_CIPHER_ARIA_256_CBC: |
| 2669 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 2670 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2671 | *key_size = 256; |
| 2672 | break; |
| 2673 | case MBEDTLS_CIPHER_ARIA_256_CCM: |
| 2674 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 2675 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2676 | *key_size = 256; |
| 2677 | break; |
| 2678 | case MBEDTLS_CIPHER_ARIA_256_GCM: |
| 2679 | *alg = PSA_ALG_GCM; |
| 2680 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2681 | *key_size = 256; |
| 2682 | break; |
| 2683 | case MBEDTLS_CIPHER_CAMELLIA_128_CBC: |
| 2684 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 2685 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2686 | *key_size = 128; |
| 2687 | break; |
| 2688 | case MBEDTLS_CIPHER_CAMELLIA_128_CCM: |
| 2689 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 2690 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2691 | *key_size = 128; |
| 2692 | break; |
| 2693 | case MBEDTLS_CIPHER_CAMELLIA_128_GCM: |
| 2694 | *alg = PSA_ALG_GCM; |
| 2695 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2696 | *key_size = 128; |
| 2697 | break; |
| 2698 | case MBEDTLS_CIPHER_CAMELLIA_192_CCM: |
| 2699 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 2700 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2701 | *key_size = 192; |
| 2702 | break; |
| 2703 | case MBEDTLS_CIPHER_CAMELLIA_192_GCM: |
| 2704 | *alg = PSA_ALG_GCM; |
| 2705 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2706 | *key_size = 192; |
| 2707 | break; |
| 2708 | case MBEDTLS_CIPHER_CAMELLIA_256_CBC: |
| 2709 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 2710 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2711 | *key_size = 256; |
| 2712 | break; |
| 2713 | case MBEDTLS_CIPHER_CAMELLIA_256_CCM: |
| 2714 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 2715 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2716 | *key_size = 256; |
| 2717 | break; |
| 2718 | case MBEDTLS_CIPHER_CAMELLIA_256_GCM: |
| 2719 | *alg = PSA_ALG_GCM; |
| 2720 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2721 | *key_size = 256; |
| 2722 | break; |
| 2723 | case MBEDTLS_CIPHER_CHACHA20_POLY1305: |
| 2724 | *alg = PSA_ALG_CHACHA20_POLY1305; |
| 2725 | *key_type = PSA_KEY_TYPE_CHACHA20; |
| 2726 | *key_size = 256; |
| 2727 | break; |
| 2728 | case MBEDTLS_CIPHER_NULL: |
| 2729 | *alg = MBEDTLS_SSL_NULL_CIPHER; |
| 2730 | *key_type = 0; |
| 2731 | *key_size = 0; |
| 2732 | break; |
| 2733 | default: |
| 2734 | return PSA_ERROR_NOT_SUPPORTED; |
| 2735 | } |
| 2736 | |
| 2737 | return PSA_SUCCESS; |
| 2738 | } |
Neil Armstrong | 8395d7a | 2022-05-18 11:44:56 +0200 | [diff] [blame] | 2739 | #endif /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2740 | |
Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 2741 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 2742 | int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf, |
| 2743 | const unsigned char *dhm_P, size_t P_len, |
| 2744 | const unsigned char *dhm_G, size_t G_len ) |
| 2745 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2746 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 2747 | |
Glenn Strauss | cee1129 | 2021-12-20 01:43:17 -0500 | [diff] [blame] | 2748 | mbedtls_mpi_free( &conf->dhm_P ); |
| 2749 | mbedtls_mpi_free( &conf->dhm_G ); |
| 2750 | |
Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 2751 | if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 || |
| 2752 | ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 ) |
| 2753 | { |
| 2754 | mbedtls_mpi_free( &conf->dhm_P ); |
| 2755 | mbedtls_mpi_free( &conf->dhm_G ); |
| 2756 | return( ret ); |
| 2757 | } |
| 2758 | |
| 2759 | return( 0 ); |
| 2760 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2761 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2762 | 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] | 2763 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2764 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 2765 | |
Glenn Strauss | cee1129 | 2021-12-20 01:43:17 -0500 | [diff] [blame] | 2766 | mbedtls_mpi_free( &conf->dhm_P ); |
| 2767 | mbedtls_mpi_free( &conf->dhm_G ); |
| 2768 | |
Gilles Peskine | e570248 | 2021-06-11 21:59:08 +0200 | [diff] [blame] | 2769 | if( ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_P, |
| 2770 | &conf->dhm_P ) ) != 0 || |
| 2771 | ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_G, |
| 2772 | &conf->dhm_G ) ) != 0 ) |
Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 2773 | { |
| 2774 | mbedtls_mpi_free( &conf->dhm_P ); |
| 2775 | mbedtls_mpi_free( &conf->dhm_G ); |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 2776 | return( ret ); |
Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 2777 | } |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 2778 | |
| 2779 | return( 0 ); |
| 2780 | } |
Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 2781 | #endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */ |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 2782 | |
Manuel Pégourié-Gonnard | bd990d6 | 2015-06-11 14:49:42 +0200 | [diff] [blame] | 2783 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C) |
| 2784 | /* |
| 2785 | * Set the minimum length for Diffie-Hellman parameters |
| 2786 | */ |
| 2787 | void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf, |
| 2788 | unsigned int bitlen ) |
| 2789 | { |
| 2790 | conf->dhm_min_bitlen = bitlen; |
| 2791 | } |
| 2792 | #endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */ |
| 2793 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 2794 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Jerry Yu | 7ddc38c | 2022-01-19 11:08:05 +0800 | [diff] [blame] | 2795 | #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] | 2796 | /* |
| 2797 | * Set allowed/preferred hashes for handshake signatures |
| 2798 | */ |
| 2799 | void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf, |
| 2800 | const int *hashes ) |
| 2801 | { |
| 2802 | conf->sig_hashes = hashes; |
| 2803 | } |
Jerry Yu | 7ddc38c | 2022-01-19 11:08:05 +0800 | [diff] [blame] | 2804 | #endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */ |
Hanno Becker | 1cd6e00 | 2021-08-10 13:27:10 +0100 | [diff] [blame] | 2805 | |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 2806 | /* Configure allowed signature algorithms for handshake */ |
Hanno Becker | 1cd6e00 | 2021-08-10 13:27:10 +0100 | [diff] [blame] | 2807 | void mbedtls_ssl_conf_sig_algs( mbedtls_ssl_config *conf, |
| 2808 | const uint16_t* sig_algs ) |
| 2809 | { |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 2810 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 2811 | conf->sig_hashes = NULL; |
| 2812 | #endif /* !MBEDTLS_DEPRECATED_REMOVED */ |
| 2813 | conf->sig_algs = sig_algs; |
Hanno Becker | 1cd6e00 | 2021-08-10 13:27:10 +0100 | [diff] [blame] | 2814 | } |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 2815 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Manuel Pégourié-Gonnard | 36a8b57 | 2015-06-17 12:43:26 +0200 | [diff] [blame] | 2816 | |
Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 2817 | #if defined(MBEDTLS_ECP_C) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 2818 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 2819 | /* |
| 2820 | * Set the allowed elliptic curves |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 2821 | * |
| 2822 | * mbedtls_ssl_setup() takes the provided list |
| 2823 | * and translates it to a list of IANA TLS group identifiers, |
| 2824 | * stored in ssl->handshake->group_list. |
| 2825 | * |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 2826 | */ |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2827 | void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2828 | const mbedtls_ecp_group_id *curve_list ) |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 2829 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2830 | conf->curve_list = curve_list; |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 2831 | conf->group_list = NULL; |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 2832 | } |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 2833 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2834 | #endif /* MBEDTLS_ECP_C */ |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 2835 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 2836 | /* |
| 2837 | * Set the allowed groups |
| 2838 | */ |
| 2839 | void mbedtls_ssl_conf_groups( mbedtls_ssl_config *conf, |
| 2840 | const uint16_t *group_list ) |
| 2841 | { |
| 2842 | #if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 2843 | conf->curve_list = NULL; |
| 2844 | #endif |
| 2845 | conf->group_list = group_list; |
| 2846 | } |
| 2847 | |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 2848 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2849 | int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2850 | { |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2851 | /* Initialize to suppress unnecessary compiler warning */ |
| 2852 | size_t hostname_len = 0; |
| 2853 | |
| 2854 | /* Check if new hostname is valid before |
| 2855 | * making any change to current one */ |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2856 | if( hostname != NULL ) |
| 2857 | { |
| 2858 | hostname_len = strlen( hostname ); |
| 2859 | |
| 2860 | if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN ) |
| 2861 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2862 | } |
| 2863 | |
| 2864 | /* Now it's clear that we will overwrite the old hostname, |
| 2865 | * so we can free it safely */ |
| 2866 | |
| 2867 | if( ssl->hostname != NULL ) |
| 2868 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 2869 | mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) ); |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2870 | mbedtls_free( ssl->hostname ); |
| 2871 | } |
| 2872 | |
| 2873 | /* Passing NULL as hostname shall clear the old one */ |
Manuel Pégourié-Gonnard | ba26c24 | 2015-05-06 10:47:06 +0100 | [diff] [blame] | 2874 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2875 | if( hostname == NULL ) |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2876 | { |
| 2877 | ssl->hostname = NULL; |
| 2878 | } |
| 2879 | else |
| 2880 | { |
| 2881 | ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 ); |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2882 | if( ssl->hostname == NULL ) |
| 2883 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Paul Bakker | 75c1a6f | 2013-08-19 14:25:29 +0200 | [diff] [blame] | 2884 | |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2885 | memcpy( ssl->hostname, hostname, hostname_len ); |
Paul Bakker | 75c1a6f | 2013-08-19 14:25:29 +0200 | [diff] [blame] | 2886 | |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2887 | ssl->hostname[hostname_len] = '\0'; |
| 2888 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2889 | |
| 2890 | return( 0 ); |
| 2891 | } |
Hanno Becker | 1a9a51c | 2017-04-07 13:02:16 +0100 | [diff] [blame] | 2892 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2893 | |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 2894 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2895 | void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2896 | int (*f_sni)(void *, mbedtls_ssl_context *, |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 2897 | const unsigned char *, size_t), |
| 2898 | void *p_sni ) |
| 2899 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2900 | conf->f_sni = f_sni; |
| 2901 | conf->p_sni = p_sni; |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 2902 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2903 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 2904 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2905 | #if defined(MBEDTLS_SSL_ALPN) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2906 | 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] | 2907 | { |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 2908 | size_t cur_len, tot_len; |
| 2909 | const char **p; |
| 2910 | |
| 2911 | /* |
Brian J Murray | 1903fb3 | 2016-11-06 04:45:15 -0800 | [diff] [blame] | 2912 | * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings |
| 2913 | * MUST NOT be truncated." |
| 2914 | * We check lengths now rather than later. |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 2915 | */ |
| 2916 | tot_len = 0; |
| 2917 | for( p = protos; *p != NULL; p++ ) |
| 2918 | { |
| 2919 | cur_len = strlen( *p ); |
| 2920 | tot_len += cur_len; |
| 2921 | |
Ronald Cron | 8216dd3 | 2020-04-23 16:41:44 +0200 | [diff] [blame] | 2922 | if( ( cur_len == 0 ) || |
| 2923 | ( cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN ) || |
| 2924 | ( tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN ) ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2925 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 2926 | } |
| 2927 | |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2928 | conf->alpn_list = protos; |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 2929 | |
| 2930 | return( 0 ); |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 2931 | } |
| 2932 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2933 | 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] | 2934 | { |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 2935 | return( ssl->alpn_chosen ); |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 2936 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2937 | #endif /* MBEDTLS_SSL_ALPN */ |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 2938 | |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2939 | #if defined(MBEDTLS_SSL_DTLS_SRTP) |
Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 2940 | void mbedtls_ssl_conf_srtp_mki_value_supported( mbedtls_ssl_config *conf, |
| 2941 | int support_mki_value ) |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 2942 | { |
| 2943 | conf->dtls_srtp_mki_support = support_mki_value; |
| 2944 | } |
| 2945 | |
Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 2946 | int mbedtls_ssl_dtls_srtp_set_mki_value( mbedtls_ssl_context *ssl, |
| 2947 | unsigned char *mki_value, |
Johan Pascal | f6417ec | 2020-09-22 15:15:19 +0200 | [diff] [blame] | 2948 | uint16_t mki_len ) |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 2949 | { |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 2950 | if( mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH ) |
Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 2951 | { |
Johan Pascal | d576fdb | 2020-09-22 10:39:53 +0200 | [diff] [blame] | 2952 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 2953 | } |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 2954 | |
| 2955 | 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] | 2956 | { |
Johan Pascal | d576fdb | 2020-09-22 10:39:53 +0200 | [diff] [blame] | 2957 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 2958 | } |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 2959 | |
| 2960 | memcpy( ssl->dtls_srtp_info.mki_value, mki_value, mki_len ); |
| 2961 | ssl->dtls_srtp_info.mki_len = mki_len; |
Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 2962 | return( 0 ); |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 2963 | } |
| 2964 | |
Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 2965 | int mbedtls_ssl_conf_dtls_srtp_protection_profiles( mbedtls_ssl_config *conf, |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 2966 | const mbedtls_ssl_srtp_profile *profiles ) |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2967 | { |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 2968 | const mbedtls_ssl_srtp_profile *p; |
| 2969 | size_t list_size = 0; |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2970 | |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 2971 | /* check the profiles list: all entry must be valid, |
| 2972 | * 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] | 2973 | for( p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET && |
| 2974 | list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH; |
| 2975 | p++ ) |
Johan Pascal | d576fdb | 2020-09-22 10:39:53 +0200 | [diff] [blame] | 2976 | { |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 2977 | if( mbedtls_ssl_check_srtp_profile_value( *p ) != MBEDTLS_TLS_SRTP_UNSET ) |
Johan Pascal | d576fdb | 2020-09-22 10:39:53 +0200 | [diff] [blame] | 2978 | { |
Johan Pascal | 76fdf1d | 2020-10-22 23:31:00 +0200 | [diff] [blame] | 2979 | list_size++; |
| 2980 | } |
| 2981 | else |
| 2982 | { |
| 2983 | /* unsupported value, stop parsing and set the size to an error value */ |
| 2984 | list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1; |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2985 | } |
| 2986 | } |
| 2987 | |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 2988 | if( list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH ) |
Johan Pascal | d387aa0 | 2020-09-23 18:47:56 +0200 | [diff] [blame] | 2989 | { |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 2990 | conf->dtls_srtp_profile_list = NULL; |
| 2991 | conf->dtls_srtp_profile_list_len = 0; |
| 2992 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2993 | } |
| 2994 | |
Johan Pascal | 9bc97ca | 2020-09-21 23:44:45 +0200 | [diff] [blame] | 2995 | conf->dtls_srtp_profile_list = profiles; |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 2996 | conf->dtls_srtp_profile_list_len = list_size; |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2997 | |
| 2998 | return( 0 ); |
| 2999 | } |
| 3000 | |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 3001 | void mbedtls_ssl_get_dtls_srtp_negotiation_result( const mbedtls_ssl_context *ssl, |
| 3002 | mbedtls_dtls_srtp_info *dtls_srtp_info ) |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 3003 | { |
Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 3004 | dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile; |
| 3005 | /* do not copy the mki value if there is no chosen profile */ |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 3006 | if( dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET ) |
Johan Pascal | 0dbcd1d | 2020-10-28 11:03:07 +0100 | [diff] [blame] | 3007 | { |
Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 3008 | dtls_srtp_info->mki_len = 0; |
Johan Pascal | 0dbcd1d | 2020-10-28 11:03:07 +0100 | [diff] [blame] | 3009 | } |
Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 3010 | else |
| 3011 | { |
| 3012 | dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len; |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 3013 | memcpy( dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value, |
| 3014 | ssl->dtls_srtp_info.mki_len ); |
Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 3015 | } |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 3016 | } |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 3017 | #endif /* MBEDTLS_SSL_DTLS_SRTP */ |
| 3018 | |
Aditya Patwardhan | 3096f33 | 2022-07-26 14:31:46 +0530 | [diff] [blame] | 3019 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 3020 | 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] | 3021 | { |
Glenn Strauss | 2dfcea2 | 2022-03-14 17:26:42 -0400 | [diff] [blame] | 3022 | conf->max_tls_version = (major << 8) | minor; |
Paul Bakker | 490ecc8 | 2011-10-06 13:04:09 +0000 | [diff] [blame] | 3023 | } |
| 3024 | |
Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 3025 | 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] | 3026 | { |
Glenn Strauss | 2dfcea2 | 2022-03-14 17:26:42 -0400 | [diff] [blame] | 3027 | conf->min_tls_version = (major << 8) | minor; |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 3028 | } |
Aditya Patwardhan | 3096f33 | 2022-07-26 14:31:46 +0530 | [diff] [blame] | 3029 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 3030 | |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 3031 | #if defined(MBEDTLS_SSL_SRV_C) |
| 3032 | void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf, |
| 3033 | char cert_req_ca_list ) |
| 3034 | { |
| 3035 | conf->cert_req_ca_list = cert_req_ca_list; |
| 3036 | } |
| 3037 | #endif |
| 3038 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3039 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 3040 | 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] | 3041 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 3042 | conf->encrypt_then_mac = etm; |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 3043 | } |
| 3044 | #endif |
| 3045 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3046 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 3047 | 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] | 3048 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 3049 | conf->extended_ms = ems; |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 3050 | } |
| 3051 | #endif |
| 3052 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3053 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 3054 | 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] | 3055 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3056 | if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID || |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3057 | 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] | 3058 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3059 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 3060 | } |
| 3061 | |
Manuel Pégourié-Gonnard | 6bf89d6 | 2015-05-05 17:01:57 +0100 | [diff] [blame] | 3062 | conf->mfl_code = mfl_code; |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 3063 | |
| 3064 | return( 0 ); |
| 3065 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3066 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 3067 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 3068 | 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] | 3069 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 3070 | conf->allow_legacy_renegotiation = allow_legacy; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3071 | } |
| 3072 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3073 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 3074 | 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] | 3075 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 3076 | conf->disable_renegotiation = renegotiation; |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 3077 | } |
| 3078 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 3079 | 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] | 3080 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 3081 | conf->renego_max_records = max_records; |
Manuel Pégourié-Gonnard | a9964db | 2014-07-03 19:29:16 +0200 | [diff] [blame] | 3082 | } |
| 3083 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 3084 | void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 837f0fe | 2014-11-05 13:58:53 +0100 | [diff] [blame] | 3085 | const unsigned char period[8] ) |
| 3086 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 3087 | memcpy( conf->renego_period, period, 8 ); |
Manuel Pégourié-Gonnard | 837f0fe | 2014-11-05 13:58:53 +0100 | [diff] [blame] | 3088 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3089 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3090 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3091 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 3092 | #if defined(MBEDTLS_SSL_CLI_C) |
| 3093 | 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] | 3094 | { |
Manuel Pégourié-Gonnard | 2b49445 | 2015-05-06 10:05:11 +0100 | [diff] [blame] | 3095 | conf->session_tickets = use_tickets; |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 3096 | } |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 3097 | #endif |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 3098 | |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 3099 | #if defined(MBEDTLS_SSL_SRV_C) |
Jerry Yu | 1ad7ace | 2022-08-09 13:28:39 +0800 | [diff] [blame] | 3100 | |
Jerry Yu | d0766ec | 2022-09-22 10:46:57 +0800 | [diff] [blame] | 3101 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS) |
Jerry Yu | 1ad7ace | 2022-08-09 13:28:39 +0800 | [diff] [blame] | 3102 | void mbedtls_ssl_conf_new_session_tickets( mbedtls_ssl_config *conf, |
| 3103 | uint16_t num_tickets ) |
| 3104 | { |
Jerry Yu | d0766ec | 2022-09-22 10:46:57 +0800 | [diff] [blame] | 3105 | conf->new_session_tickets_count = num_tickets; |
Jerry Yu | 1ad7ace | 2022-08-09 13:28:39 +0800 | [diff] [blame] | 3106 | } |
| 3107 | #endif |
| 3108 | |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 3109 | void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf, |
| 3110 | mbedtls_ssl_ticket_write_t *f_ticket_write, |
| 3111 | mbedtls_ssl_ticket_parse_t *f_ticket_parse, |
| 3112 | void *p_ticket ) |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 3113 | { |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 3114 | conf->f_ticket_write = f_ticket_write; |
| 3115 | conf->f_ticket_parse = f_ticket_parse; |
| 3116 | conf->p_ticket = p_ticket; |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 3117 | } |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 3118 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3119 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 3120 | |
Hanno Becker | 7e6c178 | 2021-06-08 09:24:55 +0100 | [diff] [blame] | 3121 | void mbedtls_ssl_set_export_keys_cb( mbedtls_ssl_context *ssl, |
| 3122 | mbedtls_ssl_export_keys_t *f_export_keys, |
| 3123 | void *p_export_keys ) |
Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 3124 | { |
Hanno Becker | 7e6c178 | 2021-06-08 09:24:55 +0100 | [diff] [blame] | 3125 | ssl->f_export_keys = f_export_keys; |
| 3126 | ssl->p_export_keys = p_export_keys; |
Ron Eldor | f5cc10d | 2019-05-07 18:33:40 +0300 | [diff] [blame] | 3127 | } |
Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 3128 | |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 3129 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 3130 | void mbedtls_ssl_conf_async_private_cb( |
| 3131 | mbedtls_ssl_config *conf, |
| 3132 | mbedtls_ssl_async_sign_t *f_async_sign, |
| 3133 | mbedtls_ssl_async_decrypt_t *f_async_decrypt, |
| 3134 | mbedtls_ssl_async_resume_t *f_async_resume, |
| 3135 | mbedtls_ssl_async_cancel_t *f_async_cancel, |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3136 | void *async_config_data ) |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 3137 | { |
| 3138 | conf->f_async_sign_start = f_async_sign; |
| 3139 | conf->f_async_decrypt_start = f_async_decrypt; |
| 3140 | conf->f_async_resume = f_async_resume; |
| 3141 | conf->f_async_cancel = f_async_cancel; |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3142 | conf->p_async_config_data = async_config_data; |
| 3143 | } |
| 3144 | |
Gilles Peskine | 8f97af7 | 2018-04-26 11:46:10 +0200 | [diff] [blame] | 3145 | void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf ) |
| 3146 | { |
| 3147 | return( conf->p_async_config_data ); |
| 3148 | } |
| 3149 | |
Gilles Peskine | 1febfef | 2018-04-30 11:54:39 +0200 | [diff] [blame] | 3150 | void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl ) |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3151 | { |
| 3152 | if( ssl->handshake == NULL ) |
| 3153 | return( NULL ); |
| 3154 | else |
| 3155 | return( ssl->handshake->user_async_ctx ); |
| 3156 | } |
| 3157 | |
Gilles Peskine | 1febfef | 2018-04-30 11:54:39 +0200 | [diff] [blame] | 3158 | void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl, |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3159 | void *ctx ) |
| 3160 | { |
| 3161 | if( ssl->handshake != NULL ) |
| 3162 | ssl->handshake->user_async_ctx = ctx; |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 3163 | } |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 3164 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 3165 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3166 | /* |
| 3167 | * SSL get accessors |
| 3168 | */ |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 3169 | uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3170 | { |
Manuel Pégourié-Gonnard | e89163c | 2015-01-23 14:30:57 +0000 | [diff] [blame] | 3171 | if( ssl->session != NULL ) |
| 3172 | return( ssl->session->verify_result ); |
| 3173 | |
| 3174 | if( ssl->session_negotiate != NULL ) |
| 3175 | return( ssl->session_negotiate->verify_result ); |
| 3176 | |
Manuel Pégourié-Gonnard | 6ab9b00 | 2015-05-14 11:25:04 +0200 | [diff] [blame] | 3177 | return( 0xFFFFFFFF ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3178 | } |
| 3179 | |
Glenn Strauss | 8f52690 | 2022-01-13 00:04:49 -0500 | [diff] [blame] | 3180 | int mbedtls_ssl_get_ciphersuite_id_from_ssl( const mbedtls_ssl_context *ssl ) |
| 3181 | { |
| 3182 | if( ssl == NULL || ssl->session == NULL ) |
| 3183 | return( 0 ); |
| 3184 | |
| 3185 | return( ssl->session->ciphersuite ); |
| 3186 | } |
| 3187 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3188 | const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl ) |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 3189 | { |
Paul Bakker | 926c8e4 | 2013-03-06 10:23:34 +0100 | [diff] [blame] | 3190 | if( ssl == NULL || ssl->session == NULL ) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 3191 | return( NULL ); |
Paul Bakker | 926c8e4 | 2013-03-06 10:23:34 +0100 | [diff] [blame] | 3192 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3193 | return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite ); |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 3194 | } |
| 3195 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3196 | const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl ) |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 3197 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3198 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3199 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 3200 | { |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 3201 | switch( ssl->tls_version ) |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 3202 | { |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 3203 | case MBEDTLS_SSL_VERSION_TLS1_2: |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 3204 | return( "DTLSv1.2" ); |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 3205 | default: |
| 3206 | return( "unknown (DTLS)" ); |
| 3207 | } |
| 3208 | } |
| 3209 | #endif |
| 3210 | |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 3211 | switch( ssl->tls_version ) |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 3212 | { |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 3213 | case MBEDTLS_SSL_VERSION_TLS1_2: |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3214 | return( "TLSv1.2" ); |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 3215 | case MBEDTLS_SSL_VERSION_TLS1_3: |
Gilles Peskine | c63a1e0 | 2022-01-13 01:10:24 +0100 | [diff] [blame] | 3216 | return( "TLSv1.3" ); |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 3217 | default: |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 3218 | return( "unknown" ); |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 3219 | } |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 3220 | } |
| 3221 | |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 3222 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 3223 | size_t mbedtls_ssl_get_input_max_frag_len( const mbedtls_ssl_context *ssl ) |
| 3224 | { |
David Horstmann | 95d516f | 2021-05-04 18:36:56 +0100 | [diff] [blame] | 3225 | size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN; |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 3226 | size_t read_mfl; |
| 3227 | |
| 3228 | /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */ |
| 3229 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
| 3230 | ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE ) |
| 3231 | { |
| 3232 | return ssl_mfl_code_to_length( ssl->conf->mfl_code ); |
| 3233 | } |
| 3234 | |
| 3235 | /* Check if a smaller max length was negotiated */ |
| 3236 | if( ssl->session_out != NULL ) |
| 3237 | { |
| 3238 | read_mfl = ssl_mfl_code_to_length( ssl->session_out->mfl_code ); |
| 3239 | if( read_mfl < max_len ) |
| 3240 | { |
| 3241 | max_len = read_mfl; |
| 3242 | } |
| 3243 | } |
| 3244 | |
| 3245 | // During a handshake, use the value being negotiated |
| 3246 | if( ssl->session_negotiate != NULL ) |
| 3247 | { |
| 3248 | read_mfl = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ); |
| 3249 | if( read_mfl < max_len ) |
| 3250 | { |
| 3251 | max_len = read_mfl; |
| 3252 | } |
| 3253 | } |
| 3254 | |
| 3255 | return( max_len ); |
| 3256 | } |
| 3257 | |
| 3258 | 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] | 3259 | { |
| 3260 | size_t max_len; |
| 3261 | |
| 3262 | /* |
| 3263 | * Assume mfl_code is correct since it was checked when set |
| 3264 | */ |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3265 | 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] | 3266 | |
Manuel Pégourié-Gonnard | 2cb17e2 | 2017-09-19 13:00:47 +0200 | [diff] [blame] | 3267 | /* Check if a smaller max length was negotiated */ |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 3268 | if( ssl->session_out != NULL && |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3269 | 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] | 3270 | { |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3271 | 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] | 3272 | } |
| 3273 | |
Manuel Pégourié-Gonnard | 2cb17e2 | 2017-09-19 13:00:47 +0200 | [diff] [blame] | 3274 | /* During a handshake, use the value being negotiated */ |
| 3275 | if( ssl->session_negotiate != NULL && |
| 3276 | ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len ) |
| 3277 | { |
| 3278 | max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ); |
| 3279 | } |
| 3280 | |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3281 | return( max_len ); |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 3282 | } |
| 3283 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
| 3284 | |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 3285 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 8949071 | 2020-02-05 10:50:12 +0000 | [diff] [blame] | 3286 | 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] | 3287 | { |
Andrzej Kurek | ef43ce6 | 2018-10-09 08:24:12 -0400 | [diff] [blame] | 3288 | /* Return unlimited mtu for client hello messages to avoid fragmentation. */ |
| 3289 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
| 3290 | ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO || |
| 3291 | ssl->state == MBEDTLS_SSL_SERVER_HELLO ) ) |
| 3292 | return ( 0 ); |
| 3293 | |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 3294 | if( ssl->handshake == NULL || ssl->handshake->mtu == 0 ) |
| 3295 | return( ssl->mtu ); |
| 3296 | |
| 3297 | if( ssl->mtu == 0 ) |
| 3298 | return( ssl->handshake->mtu ); |
| 3299 | |
| 3300 | return( ssl->mtu < ssl->handshake->mtu ? |
| 3301 | ssl->mtu : ssl->handshake->mtu ); |
| 3302 | } |
| 3303 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 3304 | |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3305 | int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl ) |
| 3306 | { |
| 3307 | size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN; |
| 3308 | |
Manuel Pégourié-Gonnard | 000281e | 2018-08-21 11:20:58 +0200 | [diff] [blame] | 3309 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \ |
| 3310 | !defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3311 | (void) ssl; |
| 3312 | #endif |
| 3313 | |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3314 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 3315 | 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] | 3316 | |
| 3317 | if( max_len > mfl ) |
| 3318 | max_len = mfl; |
| 3319 | #endif |
| 3320 | |
| 3321 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 8949071 | 2020-02-05 10:50:12 +0000 | [diff] [blame] | 3322 | if( mbedtls_ssl_get_current_mtu( ssl ) != 0 ) |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3323 | { |
Hanno Becker | 8949071 | 2020-02-05 10:50:12 +0000 | [diff] [blame] | 3324 | const size_t mtu = mbedtls_ssl_get_current_mtu( ssl ); |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3325 | const int ret = mbedtls_ssl_get_record_expansion( ssl ); |
| 3326 | const size_t overhead = (size_t) ret; |
| 3327 | |
| 3328 | if( ret < 0 ) |
| 3329 | return( ret ); |
| 3330 | |
| 3331 | if( mtu <= overhead ) |
| 3332 | { |
| 3333 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) ); |
| 3334 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 3335 | } |
| 3336 | |
| 3337 | if( max_len > mtu - overhead ) |
| 3338 | max_len = mtu - overhead; |
| 3339 | } |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 3340 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3341 | |
Hanno Becker | 0defedb | 2018-08-10 12:35:02 +0100 | [diff] [blame] | 3342 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \ |
| 3343 | !defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3344 | ((void) ssl); |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3345 | #endif |
| 3346 | |
| 3347 | return( (int) max_len ); |
| 3348 | } |
| 3349 | |
Hanno Becker | 2d8e99b | 2021-04-21 06:19:50 +0100 | [diff] [blame] | 3350 | int mbedtls_ssl_get_max_in_record_payload( const mbedtls_ssl_context *ssl ) |
| 3351 | { |
| 3352 | size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN; |
| 3353 | |
| 3354 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 3355 | (void) ssl; |
| 3356 | #endif |
| 3357 | |
| 3358 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 3359 | const size_t mfl = mbedtls_ssl_get_input_max_frag_len( ssl ); |
| 3360 | |
| 3361 | if( max_len > mfl ) |
| 3362 | max_len = mfl; |
| 3363 | #endif |
| 3364 | |
| 3365 | return( (int) max_len ); |
| 3366 | } |
| 3367 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3368 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 3369 | 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] | 3370 | { |
| 3371 | if( ssl == NULL || ssl->session == NULL ) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 3372 | return( NULL ); |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 3373 | |
Hanno Becker | e682457 | 2019-02-07 13:18:46 +0000 | [diff] [blame] | 3374 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 3375 | return( ssl->session->peer_cert ); |
Hanno Becker | e682457 | 2019-02-07 13:18:46 +0000 | [diff] [blame] | 3376 | #else |
| 3377 | return( NULL ); |
| 3378 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 3379 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3380 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 3381 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3382 | #if defined(MBEDTLS_SSL_CLI_C) |
Hanno Becker | f852b1c | 2019-02-05 11:42:30 +0000 | [diff] [blame] | 3383 | int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, |
| 3384 | mbedtls_ssl_session *dst ) |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 3385 | { |
Hanno Becker | e810bbc | 2021-05-14 16:01:05 +0100 | [diff] [blame] | 3386 | int ret; |
| 3387 | |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 3388 | if( ssl == NULL || |
| 3389 | dst == NULL || |
| 3390 | ssl->session == NULL || |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3391 | ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT ) |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 3392 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3393 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 3394 | } |
| 3395 | |
Hanno Becker | e810bbc | 2021-05-14 16:01:05 +0100 | [diff] [blame] | 3396 | /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer |
| 3397 | * idempotent: Each session can only be exported once. |
| 3398 | * |
| 3399 | * (This is in preparation for TLS 1.3 support where we will |
| 3400 | * need the ability to export multiple sessions (aka tickets), |
| 3401 | * which will be achieved by calling mbedtls_ssl_get_session() |
| 3402 | * multiple times until it fails.) |
| 3403 | * |
| 3404 | * Check whether we have already exported the current session, |
| 3405 | * and fail if so. |
| 3406 | */ |
| 3407 | if( ssl->session->exported == 1 ) |
| 3408 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 3409 | |
| 3410 | ret = mbedtls_ssl_session_copy( dst, ssl->session ); |
| 3411 | if( ret != 0 ) |
| 3412 | return( ret ); |
| 3413 | |
| 3414 | /* Remember that we've exported the session. */ |
| 3415 | ssl->session->exported = 1; |
| 3416 | return( 0 ); |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 3417 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3418 | #endif /* MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 3419 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3420 | /* |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 3421 | * Define ticket header determining Mbed TLS version |
| 3422 | * and structure of the ticket. |
| 3423 | */ |
| 3424 | |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3425 | /* |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 3426 | * Define bitflag determining compile-time settings influencing |
| 3427 | * structure of serialized SSL sessions. |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3428 | */ |
| 3429 | |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 3430 | #if defined(MBEDTLS_HAVE_TIME) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3431 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME 1 |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 3432 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3433 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3434 | #endif /* MBEDTLS_HAVE_TIME */ |
| 3435 | |
| 3436 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3437 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3438 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3439 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3440 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 3441 | |
| 3442 | #if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3443 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3444 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3445 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3446 | #endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */ |
| 3447 | |
| 3448 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3449 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3450 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3451 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3452 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
| 3453 | |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3454 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3455 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3456 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3457 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3458 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ |
| 3459 | |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3460 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 3461 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1 |
| 3462 | #else |
| 3463 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0 |
| 3464 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
| 3465 | |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3466 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0 |
| 3467 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1 |
| 3468 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2 |
| 3469 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3 |
Hanno Becker | 37bdbe6 | 2021-08-01 05:38:58 +0100 | [diff] [blame] | 3470 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4 |
| 3471 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5 |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3472 | |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 3473 | #define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \ |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3474 | ( (uint16_t) ( \ |
| 3475 | ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \ |
| 3476 | ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \ |
| 3477 | ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \ |
| 3478 | ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \ |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3479 | ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \ |
Hanno Becker | be34e8e | 2019-06-04 09:43:16 +0100 | [diff] [blame] | 3480 | ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) ) ) |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3481 | |
Hanno Becker | f878707 | 2019-05-16 12:41:07 +0100 | [diff] [blame] | 3482 | static unsigned char ssl_serialized_session_header[] = { |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3483 | MBEDTLS_VERSION_MAJOR, |
| 3484 | MBEDTLS_VERSION_MINOR, |
| 3485 | MBEDTLS_VERSION_PATCH, |
Joe Subbiani | 2194dc4 | 2021-07-14 12:31:31 +0100 | [diff] [blame] | 3486 | MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ), |
| 3487 | MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ), |
Hanno Becker | f878707 | 2019-05-16 12:41:07 +0100 | [diff] [blame] | 3488 | }; |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 3489 | |
| 3490 | /* |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 3491 | * Serialize a session in the following format: |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 3492 | * (in the presentation language of TLS, RFC 8446 section 3) |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 3493 | * |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3494 | * struct { |
Hanno Becker | dc28b6c | 2019-05-29 11:08:00 +0100 | [diff] [blame] | 3495 | * |
Hanno Becker | dce5097 | 2021-08-01 05:39:23 +0100 | [diff] [blame] | 3496 | * opaque mbedtls_version[3]; // library version: major, minor, patch |
| 3497 | * opaque session_format[2]; // library-version specific 16-bit field |
| 3498 | * // determining the format of the remaining |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3499 | * // serialized data. |
Hanno Becker | dc28b6c | 2019-05-29 11:08:00 +0100 | [diff] [blame] | 3500 | * |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3501 | * Note: When updating the format, remember to keep |
| 3502 | * these version+format bytes. |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 3503 | * |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3504 | * // In this version, `session_format` determines |
| 3505 | * // the setting of those compile-time |
| 3506 | * // configuration options which influence |
| 3507 | * // the structure of mbedtls_ssl_session. |
| 3508 | * |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 3509 | * uint8_t minor_ver; // Protocol minor version. Possible values: |
| 3510 | * // - TLS 1.2 (3) |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3511 | * |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 3512 | * select (serialized_session.tls_version) { |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3513 | * |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 3514 | * case MBEDTLS_SSL_VERSION_TLS1_2: |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3515 | * serialized_session_tls12 data; |
| 3516 | * |
| 3517 | * }; |
| 3518 | * |
| 3519 | * } serialized_session; |
| 3520 | * |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 3521 | */ |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3522 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 3523 | MBEDTLS_CHECK_RETURN_CRITICAL |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3524 | static int ssl_session_save( const mbedtls_ssl_session *session, |
| 3525 | unsigned char omit_header, |
| 3526 | unsigned char *buf, |
| 3527 | size_t buf_len, |
| 3528 | size_t *olen ) |
| 3529 | { |
| 3530 | unsigned char *p = buf; |
| 3531 | size_t used = 0; |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 3532 | size_t remaining_len; |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 3533 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 3534 | size_t out_len; |
| 3535 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 3536 | #endif |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 3537 | if( session == NULL ) |
| 3538 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 3539 | |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3540 | if( !omit_header ) |
| 3541 | { |
| 3542 | /* |
| 3543 | * Add Mbed TLS version identifier |
| 3544 | */ |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3545 | used += sizeof( ssl_serialized_session_header ); |
| 3546 | |
| 3547 | if( used <= buf_len ) |
| 3548 | { |
| 3549 | memcpy( p, ssl_serialized_session_header, |
| 3550 | sizeof( ssl_serialized_session_header ) ); |
| 3551 | p += sizeof( ssl_serialized_session_header ); |
| 3552 | } |
| 3553 | } |
| 3554 | |
| 3555 | /* |
| 3556 | * TLS version identifier |
| 3557 | */ |
| 3558 | used += 1; |
| 3559 | if( used <= buf_len ) |
| 3560 | { |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 3561 | *p++ = MBEDTLS_BYTE_0( session->tls_version ); |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3562 | } |
| 3563 | |
| 3564 | /* Forward to version-specific serialization routine. */ |
Jerry Yu | fca4d57 | 2022-07-21 10:37:48 +0800 | [diff] [blame] | 3565 | remaining_len = (buf_len >= used) ? buf_len - used : 0; |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 3566 | switch( session->tls_version ) |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3567 | { |
| 3568 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 3569 | case MBEDTLS_SSL_VERSION_TLS1_2: |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 3570 | used += ssl_tls12_session_save( session, p, remaining_len ); |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3571 | break; |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3572 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 3573 | |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 3574 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 3575 | case MBEDTLS_SSL_VERSION_TLS1_3: |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 3576 | ret = ssl_tls13_session_save( session, p, remaining_len, &out_len ); |
| 3577 | if( ret != 0 && ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ) |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 3578 | return( ret ); |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 3579 | used += out_len; |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 3580 | break; |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 3581 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 3582 | |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3583 | default: |
| 3584 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 3585 | } |
| 3586 | |
| 3587 | *olen = used; |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 3588 | if( used > buf_len ) |
| 3589 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 3590 | |
| 3591 | return( 0 ); |
| 3592 | } |
| 3593 | |
| 3594 | /* |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 3595 | * Public wrapper for ssl_session_save() |
| 3596 | */ |
| 3597 | int mbedtls_ssl_session_save( const mbedtls_ssl_session *session, |
| 3598 | unsigned char *buf, |
| 3599 | size_t buf_len, |
| 3600 | size_t *olen ) |
| 3601 | { |
| 3602 | return( ssl_session_save( session, 0, buf, buf_len, olen ) ); |
| 3603 | } |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 3604 | |
Jerry Yu | f1b23ca | 2022-02-18 11:48:47 +0800 | [diff] [blame] | 3605 | /* |
| 3606 | * Deserialize session, see mbedtls_ssl_session_save() for format. |
| 3607 | * |
| 3608 | * This internal version is wrapped by a public function that cleans up in |
| 3609 | * case of error, and has an extra option omit_header. |
| 3610 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 3611 | MBEDTLS_CHECK_RETURN_CRITICAL |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3612 | static int ssl_session_load( mbedtls_ssl_session *session, |
| 3613 | unsigned char omit_header, |
| 3614 | const unsigned char *buf, |
| 3615 | size_t len ) |
| 3616 | { |
| 3617 | const unsigned char *p = buf; |
| 3618 | const unsigned char * const end = buf + len; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 3619 | size_t remaining_len; |
| 3620 | |
| 3621 | |
| 3622 | if( session == NULL ) |
| 3623 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3624 | |
| 3625 | if( !omit_header ) |
| 3626 | { |
| 3627 | /* |
| 3628 | * Check Mbed TLS version identifier |
| 3629 | */ |
| 3630 | |
| 3631 | if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) ) |
| 3632 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3633 | |
| 3634 | if( memcmp( p, ssl_serialized_session_header, |
| 3635 | sizeof( ssl_serialized_session_header ) ) != 0 ) |
| 3636 | { |
| 3637 | return( MBEDTLS_ERR_SSL_VERSION_MISMATCH ); |
| 3638 | } |
| 3639 | p += sizeof( ssl_serialized_session_header ); |
| 3640 | } |
| 3641 | |
| 3642 | /* |
| 3643 | * TLS version identifier |
| 3644 | */ |
| 3645 | if( 1 > (size_t)( end - p ) ) |
| 3646 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 3647 | session->tls_version = 0x0300 | *p++; |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3648 | |
| 3649 | /* Dispatch according to TLS version. */ |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 3650 | remaining_len = ( end - p ); |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 3651 | switch( session->tls_version ) |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3652 | { |
| 3653 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 3654 | case MBEDTLS_SSL_VERSION_TLS1_2: |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 3655 | return( ssl_tls12_session_load( session, p, remaining_len ) ); |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3656 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 3657 | |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 3658 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 3659 | case MBEDTLS_SSL_VERSION_TLS1_3: |
| 3660 | return( ssl_tls13_session_load( session, p, remaining_len ) ); |
| 3661 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 3662 | |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3663 | default: |
| 3664 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3665 | } |
| 3666 | } |
| 3667 | |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 3668 | /* |
Manuel Pégourié-Gonnard | b9dfc9f | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 3669 | * Deserialize session: public wrapper for error cleaning |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 3670 | */ |
| 3671 | int mbedtls_ssl_session_load( mbedtls_ssl_session *session, |
| 3672 | const unsigned char *buf, |
| 3673 | size_t len ) |
| 3674 | { |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 3675 | int ret = ssl_session_load( session, 0, buf, len ); |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 3676 | |
| 3677 | if( ret != 0 ) |
| 3678 | mbedtls_ssl_session_free( session ); |
| 3679 | |
| 3680 | return( ret ); |
| 3681 | } |
| 3682 | |
| 3683 | /* |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3684 | * Perform a single step of the SSL handshake |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3685 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 3686 | MBEDTLS_CHECK_RETURN_CRITICAL |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 3687 | static int ssl_prepare_handshake_step( mbedtls_ssl_context *ssl ) |
| 3688 | { |
| 3689 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 3690 | |
Ronald Cron | 66dbf91 | 2022-02-02 15:33:46 +0100 | [diff] [blame] | 3691 | /* |
| 3692 | * 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] | 3693 | * that were written into the output buffer by the previous handshake step, |
| 3694 | * if the write to the network callback returned with the |
Ronald Cron | 66dbf91 | 2022-02-02 15:33:46 +0100 | [diff] [blame] | 3695 | * #MBEDTLS_ERR_SSL_WANT_WRITE error code. |
| 3696 | * We proceed to the next handshake step only when all data from the |
| 3697 | * previous one have been sent to the peer, thus we make sure that this is |
| 3698 | * the case here by calling `mbedtls_ssl_flush_output()`. The function may |
| 3699 | * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case |
| 3700 | * we have to wait before to go ahead. |
| 3701 | * In the case of TLS 1.3, handshake step handlers do not send data to the |
| 3702 | * peer. Data are only sent here and through |
| 3703 | * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an |
Andrzej Kurek | 5c65c57 | 2022-04-13 14:28:52 -0400 | [diff] [blame] | 3704 | * alert occurred. |
Ronald Cron | 66dbf91 | 2022-02-02 15:33:46 +0100 | [diff] [blame] | 3705 | */ |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 3706 | if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) |
| 3707 | return( ret ); |
| 3708 | |
| 3709 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3710 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 3711 | ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING ) |
| 3712 | { |
| 3713 | if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 ) |
| 3714 | return( ret ); |
| 3715 | } |
| 3716 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 3717 | |
| 3718 | return( ret ); |
| 3719 | } |
| 3720 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3721 | int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3722 | { |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 3723 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3724 | |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 3725 | if( ssl == NULL || |
| 3726 | ssl->conf == NULL || |
| 3727 | ssl->handshake == NULL || |
Jerry Yu | 1fb3299 | 2022-10-27 13:18:19 +0800 | [diff] [blame] | 3728 | ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER ) |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 3729 | { |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 3730 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 3731 | } |
| 3732 | |
| 3733 | ret = ssl_prepare_handshake_step( ssl ); |
| 3734 | if( ret != 0 ) |
| 3735 | return( ret ); |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 3736 | |
Jerry Yu | e704781 | 2021-09-13 19:26:39 +0800 | [diff] [blame] | 3737 | ret = mbedtls_ssl_handle_pending_alert( ssl ); |
| 3738 | if( ret != 0 ) |
| 3739 | goto cleanup; |
| 3740 | |
Tom Cosgrove | 2fdc7b3 | 2022-09-21 12:33:17 +0100 | [diff] [blame] | 3741 | /* If ssl->conf->endpoint is not one of MBEDTLS_SSL_IS_CLIENT or |
| 3742 | * MBEDTLS_SSL_IS_SERVER, this is the return code we give */ |
| 3743 | ret = MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3744 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3745 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3746 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 3747 | { |
Ronald Cron | 27c85e7 | 2022-03-08 11:37:55 +0100 | [diff] [blame] | 3748 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "client state: %s", |
| 3749 | mbedtls_ssl_states_str( ssl->state ) ) ); |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 3750 | |
Ronald Cron | 9f0fba3 | 2022-02-10 16:45:15 +0100 | [diff] [blame] | 3751 | switch( ssl->state ) |
| 3752 | { |
| 3753 | case MBEDTLS_SSL_HELLO_REQUEST: |
| 3754 | ssl->state = MBEDTLS_SSL_CLIENT_HELLO; |
Tom Cosgrove | 87d9c6c | 2022-09-22 09:27:56 +0100 | [diff] [blame] | 3755 | ret = 0; |
Ronald Cron | 9f0fba3 | 2022-02-10 16:45:15 +0100 | [diff] [blame] | 3756 | break; |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 3757 | |
Ronald Cron | 9f0fba3 | 2022-02-10 16:45:15 +0100 | [diff] [blame] | 3758 | case MBEDTLS_SSL_CLIENT_HELLO: |
| 3759 | ret = mbedtls_ssl_write_client_hello( ssl ); |
| 3760 | break; |
| 3761 | |
| 3762 | default: |
| 3763 | #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] | 3764 | if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 ) |
Ronald Cron | 9f0fba3 | 2022-02-10 16:45:15 +0100 | [diff] [blame] | 3765 | ret = mbedtls_ssl_tls13_handshake_client_step( ssl ); |
| 3766 | else |
| 3767 | ret = mbedtls_ssl_handshake_client_step( ssl ); |
| 3768 | #elif defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 3769 | ret = mbedtls_ssl_handshake_client_step( ssl ); |
| 3770 | #else |
| 3771 | ret = mbedtls_ssl_tls13_handshake_client_step( ssl ); |
| 3772 | #endif |
| 3773 | } |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 3774 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3775 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3776 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3777 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 3778 | { |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3779 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 3780 | if( mbedtls_ssl_conf_is_tls13_only( ssl->conf ) ) |
Jerry Yu | 2756193 | 2021-08-27 17:07:38 +0800 | [diff] [blame] | 3781 | ret = mbedtls_ssl_tls13_handshake_server_step( ssl ); |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3782 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 3783 | |
| 3784 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 3785 | if( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) ) |
| 3786 | ret = mbedtls_ssl_handshake_server_step( ssl ); |
| 3787 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 3788 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3789 | #endif |
| 3790 | |
Jerry Yu | e704781 | 2021-09-13 19:26:39 +0800 | [diff] [blame] | 3791 | if( ret != 0 ) |
| 3792 | { |
Jerry Yu | bbd5a3f | 2021-09-18 20:50:22 +0800 | [diff] [blame] | 3793 | /* handshake_step return error. And it is same |
| 3794 | * with alert_reason. |
| 3795 | */ |
Jerry Yu | 3bf1f97 | 2021-09-22 21:37:18 +0800 | [diff] [blame] | 3796 | if( ssl->send_alert ) |
Jerry Yu | e704781 | 2021-09-13 19:26:39 +0800 | [diff] [blame] | 3797 | { |
Jerry Yu | 3bf1f97 | 2021-09-22 21:37:18 +0800 | [diff] [blame] | 3798 | ret = mbedtls_ssl_handle_pending_alert( ssl ); |
Jerry Yu | e704781 | 2021-09-13 19:26:39 +0800 | [diff] [blame] | 3799 | goto cleanup; |
| 3800 | } |
| 3801 | } |
| 3802 | |
| 3803 | cleanup: |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3804 | return( ret ); |
| 3805 | } |
| 3806 | |
| 3807 | /* |
| 3808 | * Perform the SSL handshake |
| 3809 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3810 | int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl ) |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3811 | { |
| 3812 | int ret = 0; |
| 3813 | |
Hanno Becker | a817ea4 | 2020-10-20 15:20:23 +0100 | [diff] [blame] | 3814 | /* Sanity checks */ |
| 3815 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 3816 | if( ssl == NULL || ssl->conf == NULL ) |
| 3817 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3818 | |
Hanno Becker | a817ea4 | 2020-10-20 15:20:23 +0100 | [diff] [blame] | 3819 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3820 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 3821 | ( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL ) ) |
| 3822 | { |
| 3823 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use " |
| 3824 | "mbedtls_ssl_set_timer_cb() for DTLS" ) ); |
| 3825 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3826 | } |
| 3827 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 3828 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3829 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3830 | |
Hanno Becker | a817ea4 | 2020-10-20 15:20:23 +0100 | [diff] [blame] | 3831 | /* Main handshake loop */ |
Jerry Yu | 1fb3299 | 2022-10-27 13:18:19 +0800 | [diff] [blame] | 3832 | while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3833 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3834 | ret = mbedtls_ssl_handshake_step( ssl ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3835 | |
| 3836 | if( ret != 0 ) |
| 3837 | break; |
| 3838 | } |
| 3839 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3840 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3841 | |
| 3842 | return( ret ); |
| 3843 | } |
| 3844 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3845 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 3846 | #if defined(MBEDTLS_SSL_SRV_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3847 | /* |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3848 | * Write HelloRequest to request renegotiation on server |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3849 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 3850 | MBEDTLS_CHECK_RETURN_CRITICAL |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3851 | static int ssl_write_hello_request( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3852 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3853 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3854 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3855 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) ); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3856 | |
| 3857 | ssl->out_msglen = 4; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3858 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 3859 | ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST; |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3860 | |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3861 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3862 | { |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3863 | 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] | 3864 | return( ret ); |
| 3865 | } |
| 3866 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3867 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) ); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3868 | |
| 3869 | return( 0 ); |
| 3870 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3871 | #endif /* MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3872 | |
| 3873 | /* |
| 3874 | * Actually renegotiate current connection, triggered by either: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3875 | * - any side: calling mbedtls_ssl_renegotiate(), |
| 3876 | * - client: receiving a HelloRequest during mbedtls_ssl_read(), |
| 3877 | * - 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] | 3878 | * the initial handshake is completed. |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3879 | * 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] | 3880 | * 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] | 3881 | */ |
Hanno Becker | 40cdaa1 | 2020-02-05 10:48:27 +0000 | [diff] [blame] | 3882 | int mbedtls_ssl_start_renegotiation( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3883 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3884 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3885 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3886 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3887 | |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3888 | if( ( ret = ssl_handshake_init( ssl ) ) != 0 ) |
| 3889 | return( ret ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3890 | |
Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 3891 | /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and |
| 3892 | * the ServerHello will have message_seq = 1" */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3893 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3894 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3895 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ) |
Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 3896 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3897 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 3898 | ssl->handshake->out_msg_seq = 1; |
| 3899 | else |
| 3900 | ssl->handshake->in_msg_seq = 1; |
Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 3901 | } |
| 3902 | #endif |
| 3903 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3904 | ssl->state = MBEDTLS_SSL_HELLO_REQUEST; |
| 3905 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3906 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3907 | if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3908 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3909 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3910 | return( ret ); |
| 3911 | } |
| 3912 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3913 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3914 | |
| 3915 | return( 0 ); |
| 3916 | } |
| 3917 | |
| 3918 | /* |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3919 | * Renegotiate current connection on client, |
| 3920 | * or request renegotiation on server |
| 3921 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3922 | int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3923 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3924 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3925 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 3926 | if( ssl == NULL || ssl->conf == NULL ) |
| 3927 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3928 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3929 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3930 | /* On server, just send the request */ |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3931 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3932 | { |
Jerry Yu | 6848a61 | 2022-10-27 13:03:26 +0800 | [diff] [blame] | 3933 | if( mbedtls_ssl_is_handshake_over( ssl ) == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3934 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3935 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3936 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING; |
Manuel Pégourié-Gonnard | f07f421 | 2014-08-15 19:04:47 +0200 | [diff] [blame] | 3937 | |
| 3938 | /* Did we already try/start sending HelloRequest? */ |
| 3939 | if( ssl->out_left != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3940 | return( mbedtls_ssl_flush_output( ssl ) ); |
Manuel Pégourié-Gonnard | f07f421 | 2014-08-15 19:04:47 +0200 | [diff] [blame] | 3941 | |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3942 | return( ssl_write_hello_request( ssl ) ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3943 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3944 | #endif /* MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3945 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3946 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3947 | /* |
| 3948 | * On client, either start the renegotiation process or, |
| 3949 | * if already in progress, continue the handshake |
| 3950 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3951 | if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3952 | { |
Jerry Yu | 6848a61 | 2022-10-27 13:03:26 +0800 | [diff] [blame] | 3953 | if( mbedtls_ssl_is_handshake_over( ssl ) == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3954 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3955 | |
Hanno Becker | 40cdaa1 | 2020-02-05 10:48:27 +0000 | [diff] [blame] | 3956 | if( ( ret = mbedtls_ssl_start_renegotiation( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3957 | { |
Hanno Becker | 40cdaa1 | 2020-02-05 10:48:27 +0000 | [diff] [blame] | 3958 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_start_renegotiation", ret ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3959 | return( ret ); |
| 3960 | } |
| 3961 | } |
| 3962 | else |
| 3963 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3964 | if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3965 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3966 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3967 | return( ret ); |
| 3968 | } |
| 3969 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3970 | #endif /* MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3971 | |
Paul Bakker | 37ce0ff | 2013-10-31 14:32:04 +0100 | [diff] [blame] | 3972 | return( ret ); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3973 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3974 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3975 | |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 3976 | void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3977 | { |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 3978 | mbedtls_ssl_handshake_params *handshake = ssl->handshake; |
| 3979 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3980 | if( handshake == NULL ) |
| 3981 | return; |
| 3982 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 3983 | #if defined(MBEDTLS_ECP_C) |
| 3984 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 3985 | if ( ssl->handshake->group_list_heap_allocated ) |
| 3986 | mbedtls_free( (void*) handshake->group_list ); |
| 3987 | handshake->group_list = NULL; |
| 3988 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
| 3989 | #endif /* MBEDTLS_ECP_C */ |
| 3990 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 3991 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 3992 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 3993 | if ( ssl->handshake->sig_algs_heap_allocated ) |
| 3994 | mbedtls_free( (void*) handshake->sig_algs ); |
| 3995 | handshake->sig_algs = NULL; |
| 3996 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
Xiaofei Bai | c234ecf | 2022-02-08 09:59:23 +0000 | [diff] [blame] | 3997 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 3998 | if( ssl->handshake->certificate_request_context ) |
| 3999 | { |
| 4000 | mbedtls_free( (void*) handshake->certificate_request_context ); |
| 4001 | } |
| 4002 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 4003 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 4004 | |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 4005 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
| 4006 | if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 ) |
| 4007 | { |
Gilles Peskine | 8f97af7 | 2018-04-26 11:46:10 +0200 | [diff] [blame] | 4008 | ssl->conf->f_async_cancel( ssl ); |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 4009 | handshake->async_in_progress = 0; |
| 4010 | } |
| 4011 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
| 4012 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4013 | #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] | 4014 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 4015 | psa_hash_abort( &handshake->fin_sha256_psa ); |
| 4016 | #else |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 4017 | mbedtls_sha256_free( &handshake->fin_sha256 ); |
| 4018 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 4019 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4020 | #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] | 4021 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 4022 | psa_hash_abort( &handshake->fin_sha384_psa ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 4023 | #else |
Andrzej Kurek | a242e83 | 2022-08-11 10:03:14 -0400 | [diff] [blame] | 4024 | mbedtls_sha512_free( &handshake->fin_sha384 ); |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 4025 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 4026 | #endif |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 4027 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4028 | #if defined(MBEDTLS_DHM_C) |
| 4029 | mbedtls_dhm_free( &handshake->dhm_ctx ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4030 | #endif |
Neil Armstrong | f3f4641 | 2022-04-12 14:43:39 +0200 | [diff] [blame] | 4031 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4032 | mbedtls_ecdh_free( &handshake->ecdh_ctx ); |
Paul Bakker | 61d113b | 2013-07-04 11:51:43 +0200 | [diff] [blame] | 4033 | #endif |
Valerio Setti | 02c25b5 | 2022-11-15 14:08:42 +0100 | [diff] [blame] | 4034 | |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 4035 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 4036 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 4037 | psa_pake_abort( &handshake->psa_pake_ctx ); |
Valerio Setti | eb3f788 | 2022-12-08 18:42:58 +0100 | [diff] [blame^] | 4038 | /* |
| 4039 | * Opaque keys are not stored in the handshake's data and it's the user |
| 4040 | * responsibility to destroy them. Clear ones, instead, are created by |
| 4041 | * the TLS library and should be destroyed at the same level |
| 4042 | */ |
| 4043 | if( ! mbedtls_svc_key_id_is_null( handshake->psa_pake_password ) ) |
| 4044 | { |
| 4045 | psa_destroy_key( handshake->psa_pake_password ); |
| 4046 | } |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 4047 | handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT; |
| 4048 | #else |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 4049 | mbedtls_ecjpake_free( &handshake->ecjpake_ctx ); |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 4050 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | 77c0646 | 2015-09-17 13:59:49 +0200 | [diff] [blame] | 4051 | #if defined(MBEDTLS_SSL_CLI_C) |
| 4052 | mbedtls_free( handshake->ecjpake_cache ); |
| 4053 | handshake->ecjpake_cache = NULL; |
| 4054 | handshake->ecjpake_cache_len = 0; |
| 4055 | #endif |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 4056 | #endif |
Paul Bakker | 61d113b | 2013-07-04 11:51:43 +0200 | [diff] [blame] | 4057 | |
Janos Follath | 4ae5c29 | 2016-02-10 11:27:43 +0000 | [diff] [blame] | 4058 | #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ |
| 4059 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 4060 | /* explicit void pointer cast for buggy MS compiler */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4061 | mbedtls_free( (void *) handshake->curves ); |
Manuel Pégourié-Gonnard | d09453c | 2013-09-23 19:11:32 +0200 | [diff] [blame] | 4062 | #endif |
| 4063 | |
Ronald Cron | 73fe8df | 2022-10-05 14:31:43 +0200 | [diff] [blame] | 4064 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED) |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 4065 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 4066 | if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) ) |
| 4067 | { |
| 4068 | /* The maintenance of the external PSK key slot is the |
| 4069 | * user's responsibility. */ |
| 4070 | if( ssl->handshake->psk_opaque_is_internal ) |
| 4071 | { |
| 4072 | psa_destroy_key( ssl->handshake->psk_opaque ); |
| 4073 | ssl->handshake->psk_opaque_is_internal = 0; |
| 4074 | } |
| 4075 | ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT; |
| 4076 | } |
Neil Armstrong | e952a30 | 2022-05-03 10:22:14 +0200 | [diff] [blame] | 4077 | #else |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 4078 | if( handshake->psk != NULL ) |
| 4079 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 4080 | mbedtls_platform_zeroize( handshake->psk, handshake->psk_len ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 4081 | mbedtls_free( handshake->psk ); |
| 4082 | } |
Neil Armstrong | e952a30 | 2022-05-03 10:22:14 +0200 | [diff] [blame] | 4083 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Ronald Cron | 73fe8df | 2022-10-05 14:31:43 +0200 | [diff] [blame] | 4084 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */ |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 4085 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4086 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 4087 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Manuel Pégourié-Gonnard | 8372454 | 2013-09-24 22:30:56 +0200 | [diff] [blame] | 4088 | /* |
| 4089 | * Free only the linked list wrapper, not the keys themselves |
| 4090 | * since the belong to the SNI callback |
| 4091 | */ |
Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 4092 | ssl_key_cert_free( handshake->sni_key_cert ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4093 | #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] | 4094 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 4095 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Manuel Pégourié-Gonnard | 6b7301c | 2017-08-15 12:08:45 +0200 | [diff] [blame] | 4096 | mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx ); |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 4097 | if( handshake->ecrs_peer_cert != NULL ) |
| 4098 | { |
| 4099 | mbedtls_x509_crt_free( handshake->ecrs_peer_cert ); |
| 4100 | mbedtls_free( handshake->ecrs_peer_cert ); |
| 4101 | } |
Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 4102 | #endif |
| 4103 | |
Hanno Becker | 7517312 | 2019-02-06 16:18:31 +0000 | [diff] [blame] | 4104 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 4105 | !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 4106 | mbedtls_pk_free( &handshake->peer_pubkey ); |
| 4107 | #endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 4108 | |
XiaokangQian | 9b93c0d | 2022-02-09 06:02:25 +0000 | [diff] [blame] | 4109 | #if defined(MBEDTLS_SSL_CLI_C) && \ |
| 4110 | ( defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3) ) |
| 4111 | mbedtls_free( handshake->cookie ); |
| 4112 | #endif /* MBEDTLS_SSL_CLI_C && |
| 4113 | ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */ |
XiaokangQian | 8499b6c | 2022-01-27 09:00:11 +0000 | [diff] [blame] | 4114 | |
| 4115 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 533ab5f | 2020-02-05 10:49:13 +0000 | [diff] [blame] | 4116 | mbedtls_ssl_flight_free( handshake->flight ); |
| 4117 | mbedtls_ssl_buffering_free( ssl ); |
XiaokangQian | 8499b6c | 2022-01-27 09:00:11 +0000 | [diff] [blame] | 4118 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 4119 | |
Ronald Cron | f12b81d | 2022-03-15 10:42:41 +0100 | [diff] [blame] | 4120 | #if defined(MBEDTLS_ECDH_C) && \ |
| 4121 | ( defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3) ) |
Neil Armstrong | f716a70 | 2022-04-04 11:23:46 +0200 | [diff] [blame] | 4122 | if( handshake->ecdh_psa_privkey_is_external == 0 ) |
Neil Armstrong | 8113d25 | 2022-03-23 10:57:04 +0100 | [diff] [blame] | 4123 | psa_destroy_key( handshake->ecdh_psa_privkey ); |
Hanno Becker | 4a63ed4 | 2019-01-08 11:39:35 +0000 | [diff] [blame] | 4124 | #endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */ |
| 4125 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4126 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | a1a568c | 2021-11-09 10:17:21 +0800 | [diff] [blame] | 4127 | mbedtls_ssl_transform_free( handshake->transform_handshake ); |
| 4128 | mbedtls_ssl_transform_free( handshake->transform_earlydata ); |
Jerry Yu | ba9c727 | 2021-10-30 11:54:10 +0800 | [diff] [blame] | 4129 | mbedtls_free( handshake->transform_earlydata ); |
| 4130 | mbedtls_free( handshake->transform_handshake ); |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4131 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Jerry Yu | ba9c727 | 2021-10-30 11:54:10 +0800 | [diff] [blame] | 4132 | |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 4133 | |
| 4134 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 4135 | /* If the buffers are too big - reallocate. Because of the way Mbed TLS |
| 4136 | * processes datagrams and the fact that a datagram is allowed to have |
| 4137 | * several records in it, it is possible that the I/O buffers are not |
| 4138 | * empty at this stage */ |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 4139 | handle_buffer_resizing( ssl, 1, mbedtls_ssl_get_input_buflen( ssl ), |
| 4140 | mbedtls_ssl_get_output_buflen( ssl ) ); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 4141 | #endif |
Hanno Becker | 3aa186f | 2021-08-10 09:24:19 +0100 | [diff] [blame] | 4142 | |
Jerry Yu | ba9c727 | 2021-10-30 11:54:10 +0800 | [diff] [blame] | 4143 | /* mbedtls_platform_zeroize MUST be last one in this function */ |
| 4144 | mbedtls_platform_zeroize( handshake, |
| 4145 | sizeof( mbedtls_ssl_handshake_params ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4146 | } |
| 4147 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4148 | void mbedtls_ssl_session_free( mbedtls_ssl_session *session ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4149 | { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 4150 | if( session == NULL ) |
| 4151 | return; |
| 4152 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4153 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Hanno Becker | 1294a0b | 2019-02-05 12:38:15 +0000 | [diff] [blame] | 4154 | ssl_clear_peer_cert( session ); |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 4155 | #endif |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 4156 | |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 4157 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
Xiaokang Qian | bc663a0 | 2022-10-09 11:14:39 +0000 | [diff] [blame] | 4158 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \ |
| 4159 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Xiaokang Qian | 281fd1b | 2022-09-20 11:35:41 +0000 | [diff] [blame] | 4160 | mbedtls_free( session->hostname ); |
| 4161 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4162 | mbedtls_free( session->ticket ); |
Paul Bakker | a503a63 | 2013-08-14 13:48:06 +0200 | [diff] [blame] | 4163 | #endif |
Manuel Pégourié-Gonnard | 75d4401 | 2013-08-02 14:44:04 +0200 | [diff] [blame] | 4164 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 4165 | mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4166 | } |
| 4167 | |
Manuel Pégourié-Gonnard | 5c0e377 | 2019-07-23 16:13:17 +0200 | [diff] [blame] | 4168 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 4169 | |
| 4170 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 4171 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u |
| 4172 | #else |
| 4173 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u |
| 4174 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 4175 | |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 4176 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 4177 | |
| 4178 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 4179 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u |
| 4180 | #else |
| 4181 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u |
| 4182 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
| 4183 | |
| 4184 | #if defined(MBEDTLS_SSL_ALPN) |
| 4185 | #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u |
| 4186 | #else |
| 4187 | #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u |
| 4188 | #endif /* MBEDTLS_SSL_ALPN */ |
| 4189 | |
| 4190 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0 |
| 4191 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1 |
| 4192 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2 |
| 4193 | #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3 |
| 4194 | |
| 4195 | #define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \ |
| 4196 | ( (uint32_t) ( \ |
| 4197 | ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \ |
| 4198 | ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \ |
| 4199 | ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \ |
| 4200 | ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \ |
| 4201 | 0u ) ) |
| 4202 | |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4203 | static unsigned char ssl_serialized_context_header[] = { |
| 4204 | MBEDTLS_VERSION_MAJOR, |
| 4205 | MBEDTLS_VERSION_MINOR, |
| 4206 | MBEDTLS_VERSION_PATCH, |
Joe Subbiani | 2194dc4 | 2021-07-14 12:31:31 +0100 | [diff] [blame] | 4207 | MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ), |
| 4208 | MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ), |
| 4209 | MBEDTLS_BYTE_2( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ), |
| 4210 | MBEDTLS_BYTE_1( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ), |
| 4211 | MBEDTLS_BYTE_0( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ), |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4212 | }; |
| 4213 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4214 | /* |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 4215 | * Serialize a full SSL context |
Manuel Pégourié-Gonnard | 00400c2 | 2019-07-10 14:58:45 +0200 | [diff] [blame] | 4216 | * |
| 4217 | * The format of the serialized data is: |
| 4218 | * (in the presentation language of TLS, RFC 8446 section 3) |
| 4219 | * |
| 4220 | * // header |
| 4221 | * opaque mbedtls_version[3]; // major, minor, patch |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4222 | * opaque context_format[5]; // version-specific field determining |
Manuel Pégourié-Gonnard | 00400c2 | 2019-07-10 14:58:45 +0200 | [diff] [blame] | 4223 | * // the format of the remaining |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4224 | * // serialized data. |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 4225 | * Note: When updating the format, remember to keep these |
| 4226 | * 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] | 4227 | * |
| 4228 | * // session sub-structure |
| 4229 | * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save() |
| 4230 | * // transform sub-structure |
| 4231 | * uint8 random[64]; // ServerHello.random+ClientHello.random |
| 4232 | * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value |
| 4233 | * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use |
| 4234 | * // fields from ssl_context |
| 4235 | * uint32 badmac_seen; // DTLS: number of records with failing MAC |
| 4236 | * uint64 in_window_top; // DTLS: last validated record seq_num |
| 4237 | * uint64 in_window; // DTLS: bitmask for replay protection |
| 4238 | * uint8 disable_datagram_packing; // DTLS: only one record per datagram |
| 4239 | * uint64 cur_out_ctr; // Record layer: outgoing sequence number |
| 4240 | * uint16 mtu; // DTLS: path mtu (max outgoing fragment size) |
| 4241 | * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol |
| 4242 | * |
| 4243 | * Note that many fields of the ssl_context or sub-structures are not |
| 4244 | * serialized, as they fall in one of the following categories: |
| 4245 | * |
| 4246 | * 1. forced value (eg in_left must be 0) |
| 4247 | * 2. pointer to dynamically-allocated memory (eg session, transform) |
| 4248 | * 3. value can be re-derived from other data (eg session keys from MS) |
| 4249 | * 4. value was temporary (eg content of input buffer) |
| 4250 | * 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] | 4251 | */ |
| 4252 | int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl, |
| 4253 | unsigned char *buf, |
| 4254 | size_t buf_len, |
| 4255 | size_t *olen ) |
| 4256 | { |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4257 | unsigned char *p = buf; |
| 4258 | size_t used = 0; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4259 | size_t session_len; |
| 4260 | int ret = 0; |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4261 | |
Manuel Pégourié-Gonnard | 1aaf669 | 2019-07-10 14:14:05 +0200 | [diff] [blame] | 4262 | /* |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 4263 | * Enforce usage restrictions, see "return BAD_INPUT_DATA" in |
| 4264 | * this function's documentation. |
| 4265 | * |
| 4266 | * These are due to assumptions/limitations in the implementation. Some of |
| 4267 | * them are likely to stay (no handshake in progress) some might go away |
| 4268 | * (only DTLS) but are currently used to simplify the implementation. |
Manuel Pégourié-Gonnard | 1aaf669 | 2019-07-10 14:14:05 +0200 | [diff] [blame] | 4269 | */ |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 4270 | /* The initial handshake must be over */ |
Jerry Yu | 6848a61 | 2022-10-27 13:03:26 +0800 | [diff] [blame] | 4271 | if( mbedtls_ssl_is_handshake_over( ssl ) == 0 ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 4272 | { |
| 4273 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Initial handshake isn't over" ) ); |
Manuel Pégourié-Gonnard | 1aaf669 | 2019-07-10 14:14:05 +0200 | [diff] [blame] | 4274 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 4275 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 4276 | if( ssl->handshake != NULL ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 4277 | { |
| 4278 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Handshake isn't completed" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 4279 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 4280 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 4281 | /* Double-check that sub-structures are indeed ready */ |
| 4282 | if( ssl->transform == NULL || ssl->session == NULL ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 4283 | { |
| 4284 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Serialised structures aren't ready" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 4285 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 4286 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 4287 | /* There must be no pending incoming or outgoing data */ |
| 4288 | if( mbedtls_ssl_check_pending( ssl ) != 0 ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 4289 | { |
| 4290 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending incoming data" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 4291 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 4292 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 4293 | if( ssl->out_left != 0 ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 4294 | { |
| 4295 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending outgoing data" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 4296 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 4297 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 4298 | /* Protocol must be DLTS, not TLS */ |
| 4299 | if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 4300 | { |
| 4301 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only DTLS is supported" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 4302 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 4303 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 4304 | /* Version must be 1.2 */ |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 4305 | if( ssl->tls_version != MBEDTLS_SSL_VERSION_TLS1_2 ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 4306 | { |
| 4307 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only version 1.2 supported" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 4308 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 4309 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 4310 | /* We must be using an AEAD ciphersuite */ |
| 4311 | if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 4312 | { |
| 4313 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only AEAD ciphersuites supported" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 4314 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 4315 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 4316 | /* Renegotiation must not be enabled */ |
| 4317 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 4318 | if( ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 4319 | { |
| 4320 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Renegotiation must not be enabled" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 4321 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 4322 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 4323 | #endif |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 4324 | |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4325 | /* |
| 4326 | * Version and format identifier |
| 4327 | */ |
| 4328 | used += sizeof( ssl_serialized_context_header ); |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 4329 | |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4330 | if( used <= buf_len ) |
| 4331 | { |
| 4332 | memcpy( p, ssl_serialized_context_header, |
| 4333 | sizeof( ssl_serialized_context_header ) ); |
| 4334 | p += sizeof( ssl_serialized_context_header ); |
| 4335 | } |
| 4336 | |
| 4337 | /* |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4338 | * Session (length + data) |
| 4339 | */ |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 4340 | 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] | 4341 | if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ) |
| 4342 | return( ret ); |
| 4343 | |
| 4344 | used += 4 + session_len; |
| 4345 | if( used <= buf_len ) |
| 4346 | { |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 4347 | MBEDTLS_PUT_UINT32_BE( session_len, p, 0 ); |
| 4348 | p += 4; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4349 | |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 4350 | ret = ssl_session_save( ssl->session, 1, |
| 4351 | p, session_len, &session_len ); |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4352 | if( ret != 0 ) |
| 4353 | return( ret ); |
| 4354 | |
| 4355 | p += session_len; |
| 4356 | } |
| 4357 | |
| 4358 | /* |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4359 | * Transform |
| 4360 | */ |
| 4361 | used += sizeof( ssl->transform->randbytes ); |
| 4362 | if( used <= buf_len ) |
| 4363 | { |
| 4364 | memcpy( p, ssl->transform->randbytes, |
| 4365 | sizeof( ssl->transform->randbytes ) ); |
| 4366 | p += sizeof( ssl->transform->randbytes ); |
| 4367 | } |
| 4368 | |
| 4369 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 4370 | used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len; |
| 4371 | if( used <= buf_len ) |
| 4372 | { |
| 4373 | *p++ = ssl->transform->in_cid_len; |
| 4374 | memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len ); |
| 4375 | p += ssl->transform->in_cid_len; |
| 4376 | |
| 4377 | *p++ = ssl->transform->out_cid_len; |
| 4378 | memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len ); |
| 4379 | p += ssl->transform->out_cid_len; |
| 4380 | } |
| 4381 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 4382 | |
| 4383 | /* |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4384 | * Saved fields from top-level ssl_context structure |
| 4385 | */ |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4386 | used += 4; |
| 4387 | if( used <= buf_len ) |
| 4388 | { |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 4389 | MBEDTLS_PUT_UINT32_BE( ssl->badmac_seen, p, 0 ); |
| 4390 | p += 4; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4391 | } |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4392 | |
| 4393 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 4394 | used += 16; |
| 4395 | if( used <= buf_len ) |
| 4396 | { |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 4397 | MBEDTLS_PUT_UINT64_BE( ssl->in_window_top, p, 0 ); |
| 4398 | p += 8; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4399 | |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 4400 | MBEDTLS_PUT_UINT64_BE( ssl->in_window, p, 0 ); |
| 4401 | p += 8; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4402 | } |
| 4403 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
| 4404 | |
| 4405 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4406 | used += 1; |
| 4407 | if( used <= buf_len ) |
| 4408 | { |
| 4409 | *p++ = ssl->disable_datagram_packing; |
| 4410 | } |
| 4411 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 4412 | |
Jerry Yu | ae0b2e2 | 2021-10-08 15:21:19 +0800 | [diff] [blame] | 4413 | used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4414 | if( used <= buf_len ) |
| 4415 | { |
Jerry Yu | ae0b2e2 | 2021-10-08 15:21:19 +0800 | [diff] [blame] | 4416 | memcpy( p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN ); |
| 4417 | p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4418 | } |
| 4419 | |
| 4420 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4421 | used += 2; |
| 4422 | if( used <= buf_len ) |
| 4423 | { |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 4424 | MBEDTLS_PUT_UINT16_BE( ssl->mtu, p, 0 ); |
| 4425 | p += 2; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4426 | } |
| 4427 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 4428 | |
| 4429 | #if defined(MBEDTLS_SSL_ALPN) |
| 4430 | { |
| 4431 | const uint8_t alpn_len = ssl->alpn_chosen |
Manuel Pégourié-Gonnard | f041f4e | 2019-07-24 00:58:27 +0200 | [diff] [blame] | 4432 | ? (uint8_t) strlen( ssl->alpn_chosen ) |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4433 | : 0; |
| 4434 | |
| 4435 | used += 1 + alpn_len; |
| 4436 | if( used <= buf_len ) |
| 4437 | { |
| 4438 | *p++ = alpn_len; |
| 4439 | |
| 4440 | if( ssl->alpn_chosen != NULL ) |
| 4441 | { |
| 4442 | memcpy( p, ssl->alpn_chosen, alpn_len ); |
| 4443 | p += alpn_len; |
| 4444 | } |
| 4445 | } |
| 4446 | } |
| 4447 | #endif /* MBEDTLS_SSL_ALPN */ |
| 4448 | |
| 4449 | /* |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4450 | * Done |
| 4451 | */ |
| 4452 | *olen = used; |
| 4453 | |
| 4454 | if( used > buf_len ) |
| 4455 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 4456 | |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4457 | MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used ); |
| 4458 | |
Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 4459 | return( mbedtls_ssl_session_reset_int( ssl, 0 ) ); |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 4460 | } |
| 4461 | |
| 4462 | /* |
Manuel Pégourié-Gonnard | b9dfc9f | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 4463 | * Deserialize context, see mbedtls_ssl_context_save() for format. |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4464 | * |
| 4465 | * This internal version is wrapped by a public function that cleans up in |
| 4466 | * case of error. |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 4467 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 4468 | MBEDTLS_CHECK_RETURN_CRITICAL |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4469 | static int ssl_context_load( mbedtls_ssl_context *ssl, |
| 4470 | const unsigned char *buf, |
| 4471 | size_t len ) |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 4472 | { |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4473 | const unsigned char *p = buf; |
| 4474 | const unsigned char * const end = buf + len; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4475 | size_t session_len; |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 4476 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Andrzej Kurek | 2d59dbc | 2022-10-13 08:34:38 -0400 | [diff] [blame] | 4477 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Andrzej Kurek | 894edde | 2022-09-29 06:31:14 -0400 | [diff] [blame] | 4478 | tls_prf_fn prf_func = NULL; |
Andrzej Kurek | 2d59dbc | 2022-10-13 08:34:38 -0400 | [diff] [blame] | 4479 | #endif |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4480 | |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 4481 | /* |
| 4482 | * The context should have been freshly setup or reset. |
| 4483 | * Give the user an error in case of obvious misuse. |
Manuel Pégourié-Gonnard | 4ca930f | 2019-07-26 16:31:53 +0200 | [diff] [blame] | 4484 | * (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] | 4485 | * renegotiating, or if the user mistakenly loaded a session first.) |
| 4486 | */ |
| 4487 | if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST || |
| 4488 | ssl->session != NULL ) |
| 4489 | { |
| 4490 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4491 | } |
| 4492 | |
| 4493 | /* |
| 4494 | * We can't check that the config matches the initial one, but we can at |
| 4495 | * least check it matches the requirements for serializing. |
| 4496 | */ |
| 4497 | if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM || |
Glenn Strauss | 2dfcea2 | 2022-03-14 17:26:42 -0400 | [diff] [blame] | 4498 | ssl->conf->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2 || |
| 4499 | ssl->conf->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 || |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 4500 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | 9a96fd7 | 2019-07-23 17:11:24 +0200 | [diff] [blame] | 4501 | ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED || |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 4502 | #endif |
Manuel Pégourié-Gonnard | 9a96fd7 | 2019-07-23 17:11:24 +0200 | [diff] [blame] | 4503 | 0 ) |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 4504 | { |
| 4505 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4506 | } |
| 4507 | |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4508 | MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len ); |
| 4509 | |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4510 | /* |
| 4511 | * Check version identifier |
| 4512 | */ |
| 4513 | if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) ) |
| 4514 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4515 | |
| 4516 | if( memcmp( p, ssl_serialized_context_header, |
| 4517 | sizeof( ssl_serialized_context_header ) ) != 0 ) |
| 4518 | { |
| 4519 | return( MBEDTLS_ERR_SSL_VERSION_MISMATCH ); |
| 4520 | } |
| 4521 | p += sizeof( ssl_serialized_context_header ); |
| 4522 | |
| 4523 | /* |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4524 | * Session |
| 4525 | */ |
| 4526 | if( (size_t)( end - p ) < 4 ) |
| 4527 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4528 | |
| 4529 | session_len = ( (size_t) p[0] << 24 ) | |
| 4530 | ( (size_t) p[1] << 16 ) | |
| 4531 | ( (size_t) p[2] << 8 ) | |
| 4532 | ( (size_t) p[3] ); |
| 4533 | p += 4; |
| 4534 | |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 4535 | /* This has been allocated by ssl_handshake_init(), called by |
Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 4536 | * 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] | 4537 | ssl->session = ssl->session_negotiate; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4538 | ssl->session_in = ssl->session; |
| 4539 | ssl->session_out = ssl->session; |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 4540 | ssl->session_negotiate = NULL; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4541 | |
| 4542 | if( (size_t)( end - p ) < session_len ) |
| 4543 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4544 | |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 4545 | ret = ssl_session_load( ssl->session, 1, p, session_len ); |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4546 | if( ret != 0 ) |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 4547 | { |
| 4548 | mbedtls_ssl_session_free( ssl->session ); |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4549 | return( ret ); |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 4550 | } |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4551 | |
| 4552 | p += session_len; |
| 4553 | |
| 4554 | /* |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4555 | * Transform |
| 4556 | */ |
| 4557 | |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 4558 | /* This has been allocated by ssl_handshake_init(), called by |
Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 4559 | * 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] | 4560 | ssl->transform = ssl->transform_negotiate; |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4561 | ssl->transform_in = ssl->transform; |
| 4562 | ssl->transform_out = ssl->transform; |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 4563 | ssl->transform_negotiate = NULL; |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4564 | |
Andrzej Kurek | 2d59dbc | 2022-10-13 08:34:38 -0400 | [diff] [blame] | 4565 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Andrzej Kurek | 894edde | 2022-09-29 06:31:14 -0400 | [diff] [blame] | 4566 | prf_func = ssl_tls12prf_from_cs( ssl->session->ciphersuite ); |
| 4567 | if( prf_func == NULL ) |
| 4568 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4569 | |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4570 | /* Read random bytes and populate structure */ |
| 4571 | if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) ) |
| 4572 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Andrzej Kurek | 2d59dbc | 2022-10-13 08:34:38 -0400 | [diff] [blame] | 4573 | |
Hanno Becker | bd25755 | 2021-03-22 06:59:27 +0000 | [diff] [blame] | 4574 | ret = ssl_tls12_populate_transform( ssl->transform, |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4575 | ssl->session->ciphersuite, |
| 4576 | ssl->session->master, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 4577 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4578 | ssl->session->encrypt_then_mac, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 4579 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Andrzej Kurek | 894edde | 2022-09-29 06:31:14 -0400 | [diff] [blame] | 4580 | prf_func, |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4581 | p, /* currently pointing to randbytes */ |
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 4582 | 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] | 4583 | ssl->conf->endpoint, |
| 4584 | ssl ); |
| 4585 | if( ret != 0 ) |
| 4586 | return( ret ); |
Jerry Yu | 840fbb2 | 2022-02-17 14:59:29 +0800 | [diff] [blame] | 4587 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4588 | p += sizeof( ssl->transform->randbytes ); |
| 4589 | |
| 4590 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 4591 | /* Read connection IDs and store them */ |
| 4592 | if( (size_t)( end - p ) < 1 ) |
| 4593 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4594 | |
| 4595 | ssl->transform->in_cid_len = *p++; |
| 4596 | |
Manuel Pégourié-Gonnard | 5ea13b8 | 2019-07-23 15:02:54 +0200 | [diff] [blame] | 4597 | 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] | 4598 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4599 | |
| 4600 | memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len ); |
| 4601 | p += ssl->transform->in_cid_len; |
| 4602 | |
| 4603 | ssl->transform->out_cid_len = *p++; |
| 4604 | |
| 4605 | if( (size_t)( end - p ) < ssl->transform->out_cid_len ) |
| 4606 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4607 | |
| 4608 | memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len ); |
| 4609 | p += ssl->transform->out_cid_len; |
| 4610 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 4611 | |
| 4612 | /* |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4613 | * Saved fields from top-level ssl_context structure |
| 4614 | */ |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4615 | if( (size_t)( end - p ) < 4 ) |
| 4616 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4617 | |
| 4618 | ssl->badmac_seen = ( (uint32_t) p[0] << 24 ) | |
| 4619 | ( (uint32_t) p[1] << 16 ) | |
| 4620 | ( (uint32_t) p[2] << 8 ) | |
| 4621 | ( (uint32_t) p[3] ); |
| 4622 | p += 4; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4623 | |
| 4624 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 4625 | if( (size_t)( end - p ) < 16 ) |
| 4626 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4627 | |
| 4628 | ssl->in_window_top = ( (uint64_t) p[0] << 56 ) | |
| 4629 | ( (uint64_t) p[1] << 48 ) | |
| 4630 | ( (uint64_t) p[2] << 40 ) | |
| 4631 | ( (uint64_t) p[3] << 32 ) | |
| 4632 | ( (uint64_t) p[4] << 24 ) | |
| 4633 | ( (uint64_t) p[5] << 16 ) | |
| 4634 | ( (uint64_t) p[6] << 8 ) | |
| 4635 | ( (uint64_t) p[7] ); |
| 4636 | p += 8; |
| 4637 | |
| 4638 | ssl->in_window = ( (uint64_t) p[0] << 56 ) | |
| 4639 | ( (uint64_t) p[1] << 48 ) | |
| 4640 | ( (uint64_t) p[2] << 40 ) | |
| 4641 | ( (uint64_t) p[3] << 32 ) | |
| 4642 | ( (uint64_t) p[4] << 24 ) | |
| 4643 | ( (uint64_t) p[5] << 16 ) | |
| 4644 | ( (uint64_t) p[6] << 8 ) | |
| 4645 | ( (uint64_t) p[7] ); |
| 4646 | p += 8; |
| 4647 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
| 4648 | |
| 4649 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4650 | if( (size_t)( end - p ) < 1 ) |
| 4651 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4652 | |
| 4653 | ssl->disable_datagram_packing = *p++; |
| 4654 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 4655 | |
Jerry Yu | d9a94fe | 2021-09-28 18:58:59 +0800 | [diff] [blame] | 4656 | if( (size_t)( end - p ) < sizeof( ssl->cur_out_ctr ) ) |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4657 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jerry Yu | d9a94fe | 2021-09-28 18:58:59 +0800 | [diff] [blame] | 4658 | memcpy( ssl->cur_out_ctr, p, sizeof( ssl->cur_out_ctr ) ); |
| 4659 | p += sizeof( ssl->cur_out_ctr ); |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4660 | |
| 4661 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4662 | if( (size_t)( end - p ) < 2 ) |
| 4663 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4664 | |
| 4665 | ssl->mtu = ( p[0] << 8 ) | p[1]; |
| 4666 | p += 2; |
| 4667 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 4668 | |
| 4669 | #if defined(MBEDTLS_SSL_ALPN) |
| 4670 | { |
| 4671 | uint8_t alpn_len; |
| 4672 | const char **cur; |
| 4673 | |
| 4674 | if( (size_t)( end - p ) < 1 ) |
| 4675 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4676 | |
| 4677 | alpn_len = *p++; |
| 4678 | |
| 4679 | if( alpn_len != 0 && ssl->conf->alpn_list != NULL ) |
| 4680 | { |
| 4681 | /* alpn_chosen should point to an item in the configured list */ |
| 4682 | for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ ) |
| 4683 | { |
| 4684 | if( strlen( *cur ) == alpn_len && |
| 4685 | memcmp( p, cur, alpn_len ) == 0 ) |
| 4686 | { |
| 4687 | ssl->alpn_chosen = *cur; |
| 4688 | break; |
| 4689 | } |
| 4690 | } |
| 4691 | } |
| 4692 | |
| 4693 | /* can only happen on conf mismatch */ |
| 4694 | if( alpn_len != 0 && ssl->alpn_chosen == NULL ) |
| 4695 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4696 | |
| 4697 | p += alpn_len; |
| 4698 | } |
| 4699 | #endif /* MBEDTLS_SSL_ALPN */ |
| 4700 | |
| 4701 | /* |
Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 4702 | * Forced fields from top-level ssl_context structure |
| 4703 | * |
| 4704 | * Most of them already set to the correct value by mbedtls_ssl_init() and |
| 4705 | * mbedtls_ssl_reset(), so we only need to set the remaining ones. |
| 4706 | */ |
| 4707 | ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER; |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 4708 | ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 4709 | |
Hanno Becker | 361b10d | 2019-08-30 10:42:49 +0100 | [diff] [blame] | 4710 | /* Adjust pointers for header fields of outgoing records to |
| 4711 | * the given transform, accounting for explicit IV and CID. */ |
Hanno Becker | 3e6f8ab | 2020-02-05 10:40:57 +0000 | [diff] [blame] | 4712 | mbedtls_ssl_update_out_pointers( ssl, ssl->transform ); |
Hanno Becker | 361b10d | 2019-08-30 10:42:49 +0100 | [diff] [blame] | 4713 | |
Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 4714 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4715 | ssl->in_epoch = 1; |
| 4716 | #endif |
| 4717 | |
| 4718 | /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated, |
| 4719 | * 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] | 4720 | * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform() |
| 4721 | * inappropriately. */ |
Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 4722 | if( ssl->handshake != NULL ) |
| 4723 | { |
| 4724 | mbedtls_ssl_handshake_free( ssl ); |
| 4725 | mbedtls_free( ssl->handshake ); |
| 4726 | ssl->handshake = NULL; |
| 4727 | } |
| 4728 | |
| 4729 | /* |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4730 | * Done - should have consumed entire buffer |
| 4731 | */ |
| 4732 | if( p != end ) |
| 4733 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 4734 | |
| 4735 | return( 0 ); |
| 4736 | } |
| 4737 | |
| 4738 | /* |
Manuel Pégourié-Gonnard | b9dfc9f | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 4739 | * Deserialize context: public wrapper for error cleaning |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4740 | */ |
| 4741 | int mbedtls_ssl_context_load( mbedtls_ssl_context *context, |
| 4742 | const unsigned char *buf, |
| 4743 | size_t len ) |
| 4744 | { |
| 4745 | int ret = ssl_context_load( context, buf, len ); |
| 4746 | |
| 4747 | if( ret != 0 ) |
| 4748 | mbedtls_ssl_free( context ); |
| 4749 | |
| 4750 | return( ret ); |
| 4751 | } |
Manuel Pégourié-Gonnard | 5c0e377 | 2019-07-23 16:13:17 +0200 | [diff] [blame] | 4752 | #endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */ |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4753 | |
| 4754 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4755 | * Free an SSL context |
| 4756 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4757 | void mbedtls_ssl_free( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4758 | { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 4759 | if( ssl == NULL ) |
| 4760 | return; |
| 4761 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4762 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4763 | |
Manuel Pégourié-Gonnard | 7ee6f0e | 2014-02-13 10:54:07 +0100 | [diff] [blame] | 4764 | if( ssl->out_buf != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4765 | { |
sander-visser | b8aa207 | 2020-05-06 22:05:13 +0200 | [diff] [blame] | 4766 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 4767 | size_t out_buf_len = ssl->out_buf_len; |
| 4768 | #else |
| 4769 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; |
| 4770 | #endif |
| 4771 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 4772 | mbedtls_platform_zeroize( ssl->out_buf, out_buf_len ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4773 | mbedtls_free( ssl->out_buf ); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 4774 | ssl->out_buf = NULL; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4775 | } |
| 4776 | |
Manuel Pégourié-Gonnard | 7ee6f0e | 2014-02-13 10:54:07 +0100 | [diff] [blame] | 4777 | if( ssl->in_buf != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4778 | { |
sander-visser | b8aa207 | 2020-05-06 22:05:13 +0200 | [diff] [blame] | 4779 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 4780 | size_t in_buf_len = ssl->in_buf_len; |
| 4781 | #else |
| 4782 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; |
| 4783 | #endif |
| 4784 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 4785 | mbedtls_platform_zeroize( ssl->in_buf, in_buf_len ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4786 | mbedtls_free( ssl->in_buf ); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 4787 | ssl->in_buf = NULL; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4788 | } |
| 4789 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4790 | if( ssl->transform ) |
| 4791 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4792 | mbedtls_ssl_transform_free( ssl->transform ); |
| 4793 | mbedtls_free( ssl->transform ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4794 | } |
| 4795 | |
| 4796 | if( ssl->handshake ) |
| 4797 | { |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 4798 | mbedtls_ssl_handshake_free( ssl ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4799 | mbedtls_ssl_transform_free( ssl->transform_negotiate ); |
| 4800 | mbedtls_ssl_session_free( ssl->session_negotiate ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4801 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4802 | mbedtls_free( ssl->handshake ); |
| 4803 | mbedtls_free( ssl->transform_negotiate ); |
| 4804 | mbedtls_free( ssl->session_negotiate ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4805 | } |
| 4806 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4807 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Hanno Becker | 3aa186f | 2021-08-10 09:24:19 +0100 | [diff] [blame] | 4808 | mbedtls_ssl_transform_free( ssl->transform_application ); |
| 4809 | mbedtls_free( ssl->transform_application ); |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4810 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | 3aa186f | 2021-08-10 09:24:19 +0100 | [diff] [blame] | 4811 | |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 4812 | if( ssl->session ) |
| 4813 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4814 | mbedtls_ssl_session_free( ssl->session ); |
| 4815 | mbedtls_free( ssl->session ); |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 4816 | } |
| 4817 | |
Manuel Pégourié-Gonnard | 55fab2d | 2015-05-11 16:15:19 +0200 | [diff] [blame] | 4818 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 4819 | if( ssl->hostname != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4820 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 4821 | mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4822 | mbedtls_free( ssl->hostname ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4823 | } |
Paul Bakker | 0be444a | 2013-08-27 21:55:01 +0200 | [diff] [blame] | 4824 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4825 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 4826 | #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] | 4827 | mbedtls_free( ssl->cli_id ); |
Manuel Pégourié-Gonnard | 43c0218 | 2014-07-22 17:32:01 +0200 | [diff] [blame] | 4828 | #endif |
| 4829 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4830 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) ); |
Paul Bakker | 2da561c | 2009-02-05 18:00:28 +0000 | [diff] [blame] | 4831 | |
Paul Bakker | 86f04f4 | 2013-02-14 11:20:09 +0100 | [diff] [blame] | 4832 | /* Actually clear after last debug message */ |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 4833 | mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4834 | } |
| 4835 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4836 | /* |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 4837 | * Initialize mbedtls_ssl_config |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4838 | */ |
| 4839 | void mbedtls_ssl_config_init( mbedtls_ssl_config *conf ) |
| 4840 | { |
| 4841 | memset( conf, 0, sizeof( mbedtls_ssl_config ) ); |
| 4842 | } |
| 4843 | |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4844 | /* The selection should be the same as mbedtls_x509_crt_profile_default in |
| 4845 | * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters: |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4846 | * curves with a lower resource usage come first. |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4847 | * See the documentation of mbedtls_ssl_conf_curves() for what we promise |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4848 | * about this list. |
| 4849 | */ |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4850 | static uint16_t ssl_preset_default_groups[] = { |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4851 | #if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4852 | MBEDTLS_SSL_IANA_TLS_GROUP_X25519, |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4853 | #endif |
| 4854 | #if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4855 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1, |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4856 | #endif |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4857 | #if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4858 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4859 | #endif |
| 4860 | #if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4861 | MBEDTLS_SSL_IANA_TLS_GROUP_X448, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4862 | #endif |
| 4863 | #if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4864 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4865 | #endif |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4866 | #if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4867 | MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1, |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4868 | #endif |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4869 | #if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4870 | MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4871 | #endif |
| 4872 | #if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4873 | MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4874 | #endif |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4875 | MBEDTLS_SSL_IANA_TLS_GROUP_NONE |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4876 | }; |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4877 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4878 | static int ssl_preset_suiteb_ciphersuites[] = { |
| 4879 | MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, |
| 4880 | MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, |
| 4881 | 0 |
| 4882 | }; |
| 4883 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 4884 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Hanno Becker | 9c6aa7b | 2021-08-10 13:50:43 +0100 | [diff] [blame] | 4885 | |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4886 | /* NOTICE: |
Jerry Yu | 0b994b8 | 2022-01-25 17:22:12 +0800 | [diff] [blame] | 4887 | * 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] | 4888 | * rules SHOULD be upheld. |
| 4889 | * - No duplicate entries. |
| 4890 | * - 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] | 4891 | * - ssl_tls12_preset* is for TLS 1.2 use only. |
Jerry Yu | 370e146 | 2022-01-25 10:36:53 +0800 | [diff] [blame] | 4892 | * - 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] | 4893 | */ |
Hanno Becker | 9c6aa7b | 2021-08-10 13:50:43 +0100 | [diff] [blame] | 4894 | static uint16_t ssl_preset_default_sig_algs[] = { |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4895 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4896 | #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] | 4897 | defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) |
| 4898 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256, |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4899 | #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] | 4900 | MBEDTLS_ECP_DP_SECP256R1_ENABLED */ |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4901 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4902 | #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] | 4903 | defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) |
| 4904 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384, |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4905 | #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] | 4906 | MBEDTLS_ECP_DP_SECP384R1_ENABLED */ |
| 4907 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4908 | #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] | 4909 | defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) |
| 4910 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512, |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4911 | #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] | 4912 | MBEDTLS_ECP_DP_SECP521R1_ENABLED */ |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4913 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4914 | #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] | 4915 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512, |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4916 | #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] | 4917 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4918 | #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] | 4919 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384, |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4920 | #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] | 4921 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4922 | #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] | 4923 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256, |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4924 | #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] | 4925 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4926 | #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] | 4927 | MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512, |
| 4928 | #endif /* MBEDTLS_RSA_C && MBEDTLS_SHA512_C */ |
| 4929 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4930 | #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] | 4931 | MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384, |
| 4932 | #endif /* MBEDTLS_RSA_C && MBEDTLS_SHA384_C */ |
| 4933 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4934 | #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] | 4935 | MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256, |
| 4936 | #endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */ |
| 4937 | |
Gabor Mezei | 15b95a6 | 2022-05-09 16:37:58 +0200 | [diff] [blame] | 4938 | MBEDTLS_TLS_SIG_NONE |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4939 | }; |
| 4940 | |
| 4941 | /* NOTICE: see above */ |
| 4942 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 4943 | static uint16_t ssl_tls12_preset_default_sig_algs[] = { |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4944 | #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] | 4945 | #if defined(MBEDTLS_ECDSA_C) |
| 4946 | 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] | 4947 | #endif |
Jerry Yu | 09a99fc | 2022-07-28 14:22:17 +0800 | [diff] [blame] | 4948 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) |
| 4949 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512, |
| 4950 | #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */ |
Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 4951 | #if defined(MBEDTLS_RSA_C) |
| 4952 | MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA512 ), |
| 4953 | #endif |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4954 | #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] | 4955 | #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] | 4956 | #if defined(MBEDTLS_ECDSA_C) |
| 4957 | 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] | 4958 | #endif |
Jerry Yu | 09a99fc | 2022-07-28 14:22:17 +0800 | [diff] [blame] | 4959 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) |
| 4960 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384, |
| 4961 | #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */ |
Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 4962 | #if defined(MBEDTLS_RSA_C) |
| 4963 | MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ), |
| 4964 | #endif |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4965 | #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] | 4966 | #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] | 4967 | #if defined(MBEDTLS_ECDSA_C) |
| 4968 | 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] | 4969 | #endif |
Jerry Yu | 09a99fc | 2022-07-28 14:22:17 +0800 | [diff] [blame] | 4970 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) |
| 4971 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256, |
| 4972 | #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */ |
Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 4973 | #if defined(MBEDTLS_RSA_C) |
| 4974 | MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ), |
| 4975 | #endif |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4976 | #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] | 4977 | MBEDTLS_TLS_SIG_NONE |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4978 | }; |
| 4979 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 4980 | /* NOTICE: see above */ |
| 4981 | static uint16_t ssl_preset_suiteb_sig_algs[] = { |
| 4982 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4983 | #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] | 4984 | defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) |
| 4985 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256, |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4986 | #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] | 4987 | MBEDTLS_ECP_DP_SECP256R1_ENABLED */ |
| 4988 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4989 | #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] | 4990 | defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) |
| 4991 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384, |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4992 | #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] | 4993 | MBEDTLS_ECP_DP_SECP384R1_ENABLED */ |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4994 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4995 | #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] | 4996 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256, |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4997 | #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] | 4998 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4999 | #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] | 5000 | MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256, |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 5001 | #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] | 5002 | |
Gabor Mezei | 15b95a6 | 2022-05-09 16:37:58 +0200 | [diff] [blame] | 5003 | MBEDTLS_TLS_SIG_NONE |
Jerry Yu | 713013f | 2022-01-17 18:16:35 +0800 | [diff] [blame] | 5004 | }; |
Jerry Yu | 6106fdc | 2022-01-12 16:36:14 +0800 | [diff] [blame] | 5005 | |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5006 | /* NOTICE: see above */ |
| 5007 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 5008 | static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = { |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5009 | #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] | 5010 | #if defined(MBEDTLS_ECDSA_C) |
| 5011 | 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] | 5012 | #endif |
Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 5013 | #if defined(MBEDTLS_RSA_C) |
| 5014 | MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ), |
| 5015 | #endif |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 5016 | #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] | 5017 | #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] | 5018 | #if defined(MBEDTLS_ECDSA_C) |
| 5019 | 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] | 5020 | #endif |
Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 5021 | #if defined(MBEDTLS_RSA_C) |
| 5022 | MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ), |
| 5023 | #endif |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 5024 | #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] | 5025 | MBEDTLS_TLS_SIG_NONE |
Hanno Becker | 9c6aa7b | 2021-08-10 13:50:43 +0100 | [diff] [blame] | 5026 | }; |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5027 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 5028 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5029 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5030 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5031 | static uint16_t ssl_preset_suiteb_groups[] = { |
Jaeden Amero | d431104 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 5032 | #if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5033 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1, |
Jaeden Amero | d431104 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 5034 | #endif |
| 5035 | #if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5036 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1, |
Jaeden Amero | d431104 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 5037 | #endif |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5038 | MBEDTLS_SSL_IANA_TLS_GROUP_NONE |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5039 | }; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5040 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5041 | #if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5042 | /* 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] | 5043 | * to make sure there are no duplicated signature algorithm entries. */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 5044 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 5045 | static int ssl_check_no_sig_alg_duplication( uint16_t * sig_algs ) |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 5046 | { |
| 5047 | size_t i, j; |
| 5048 | int ret = 0; |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5049 | |
Gabor Mezei | 15b95a6 | 2022-05-09 16:37:58 +0200 | [diff] [blame] | 5050 | for( i = 0; sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ ) |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 5051 | { |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 5052 | for( j = 0; j < i; j++ ) |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 5053 | { |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 5054 | if( sig_algs[i] != sig_algs[j] ) |
| 5055 | continue; |
| 5056 | mbedtls_printf( " entry(%04x,%" MBEDTLS_PRINTF_SIZET |
| 5057 | ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n", |
| 5058 | sig_algs[i], j, i ); |
| 5059 | ret = -1; |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 5060 | } |
| 5061 | } |
| 5062 | return( ret ); |
| 5063 | } |
| 5064 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5065 | #endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 5066 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5067 | /* |
Tillmann Karras | 588ad50 | 2015-09-25 04:27:22 +0200 | [diff] [blame] | 5068 | * Load default in mbedtls_ssl_config |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5069 | */ |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 5070 | int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5071 | int endpoint, int transport, int preset ) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5072 | { |
Manuel Pégourié-Gonnard | 8b431fb | 2015-05-11 12:54:52 +0200 | [diff] [blame] | 5073 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 5074 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 8b431fb | 2015-05-11 12:54:52 +0200 | [diff] [blame] | 5075 | #endif |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5076 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5077 | #if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 5078 | if( ssl_check_no_sig_alg_duplication( ssl_preset_suiteb_sig_algs ) ) |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 5079 | { |
| 5080 | mbedtls_printf( "ssl_preset_suiteb_sig_algs has duplicated entries\n" ); |
| 5081 | return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED ); |
| 5082 | } |
| 5083 | |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 5084 | if( ssl_check_no_sig_alg_duplication( ssl_preset_default_sig_algs ) ) |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 5085 | { |
| 5086 | mbedtls_printf( "ssl_preset_default_sig_algs has duplicated entries\n" ); |
| 5087 | return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED ); |
| 5088 | } |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5089 | |
| 5090 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 5091 | 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] | 5092 | { |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5093 | mbedtls_printf( "ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n" ); |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5094 | return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED ); |
| 5095 | } |
| 5096 | |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 5097 | 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] | 5098 | { |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5099 | mbedtls_printf( "ssl_tls12_preset_default_sig_algs has duplicated entries\n" ); |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5100 | return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED ); |
| 5101 | } |
| 5102 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5103 | #endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 5104 | |
Manuel Pégourié-Gonnard | 0de074f | 2015-05-14 12:58:01 +0200 | [diff] [blame] | 5105 | /* Use the functions here so that they are covered in tests, |
| 5106 | * but otherwise access member directly for efficiency */ |
| 5107 | mbedtls_ssl_conf_endpoint( conf, endpoint ); |
| 5108 | mbedtls_ssl_conf_transport( conf, transport ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5109 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5110 | /* |
| 5111 | * Things that are common to all presets |
| 5112 | */ |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 5113 | #if defined(MBEDTLS_SSL_CLI_C) |
| 5114 | if( endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 5115 | { |
| 5116 | conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED; |
| 5117 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 5118 | conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED; |
| 5119 | #endif |
| 5120 | } |
| 5121 | #endif |
| 5122 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5123 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 5124 | conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED; |
| 5125 | #endif |
| 5126 | |
| 5127 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 5128 | conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED; |
| 5129 | #endif |
| 5130 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 5131 | #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] | 5132 | conf->f_cookie_write = ssl_cookie_write_dummy; |
| 5133 | conf->f_cookie_check = ssl_cookie_check_dummy; |
| 5134 | #endif |
| 5135 | |
| 5136 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 5137 | conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED; |
| 5138 | #endif |
| 5139 | |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 5140 | #if defined(MBEDTLS_SSL_SRV_C) |
| 5141 | conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED; |
TRodziewicz | 3946f79 | 2021-06-14 12:11:18 +0200 | [diff] [blame] | 5142 | conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER; |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 5143 | #endif |
| 5144 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5145 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 5146 | conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN; |
| 5147 | conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX; |
| 5148 | #endif |
| 5149 | |
| 5150 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 5151 | conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT; |
Andres AG | 2196c7f | 2016-12-15 17:01:16 +0000 | [diff] [blame] | 5152 | memset( conf->renego_period, 0x00, 2 ); |
| 5153 | memset( conf->renego_period + 2, 0xFF, 6 ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5154 | #endif |
| 5155 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5156 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
Hanno Becker | e2defad | 2021-07-24 05:59:17 +0100 | [diff] [blame] | 5157 | if( endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 5158 | { |
| 5159 | const unsigned char dhm_p[] = |
| 5160 | MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN; |
| 5161 | const unsigned char dhm_g[] = |
| 5162 | MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN; |
Hanno Becker | 00d0a68 | 2017-10-04 13:14:29 +0100 | [diff] [blame] | 5163 | |
Hanno Becker | e2defad | 2021-07-24 05:59:17 +0100 | [diff] [blame] | 5164 | if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf, |
| 5165 | dhm_p, sizeof( dhm_p ), |
| 5166 | dhm_g, sizeof( dhm_g ) ) ) != 0 ) |
| 5167 | { |
| 5168 | return( ret ); |
| 5169 | } |
| 5170 | } |
Manuel Pégourié-Gonnard | bd990d6 | 2015-06-11 14:49:42 +0200 | [diff] [blame] | 5171 | #endif |
| 5172 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 5173 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | d0766ec | 2022-09-22 10:46:57 +0800 | [diff] [blame] | 5174 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SESSION_TICKETS) |
Jerry Yu | 1ad7ace | 2022-08-09 13:28:39 +0800 | [diff] [blame] | 5175 | mbedtls_ssl_conf_new_session_tickets( |
| 5176 | conf, MBEDTLS_SSL_TLS1_3_DEFAULT_NEW_SESSION_TICKETS ); |
| 5177 | #endif |
Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 5178 | /* |
| 5179 | * Allow all TLS 1.3 key exchange modes by default. |
| 5180 | */ |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 5181 | conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL; |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 5182 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 5183 | |
XiaokangQian | 4d3a604 | 2022-04-21 13:46:17 +0000 | [diff] [blame] | 5184 | if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 5185 | { |
Glenn Strauss | 2dfcea2 | 2022-03-14 17:26:42 -0400 | [diff] [blame] | 5186 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
XiaokangQian | 4d3a604 | 2022-04-21 13:46:17 +0000 | [diff] [blame] | 5187 | conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
XiaokangQian | 060d867 | 2022-04-21 09:24:56 +0000 | [diff] [blame] | 5188 | conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
XiaokangQian | 4d3a604 | 2022-04-21 13:46:17 +0000 | [diff] [blame] | 5189 | #else |
XiaokangQian | 060d867 | 2022-04-21 09:24:56 +0000 | [diff] [blame] | 5190 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
XiaokangQian | 060d867 | 2022-04-21 09:24:56 +0000 | [diff] [blame] | 5191 | #endif |
XiaokangQian | 4d3a604 | 2022-04-21 13:46:17 +0000 | [diff] [blame] | 5192 | } |
| 5193 | else |
| 5194 | { |
| 5195 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 5196 | if( endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 5197 | { |
| 5198 | conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 5199 | conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3; |
| 5200 | } |
| 5201 | else |
| 5202 | /* Hybrid TLS 1.2 / 1.3 is not supported on server side yet */ |
| 5203 | { |
| 5204 | conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 5205 | conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 5206 | } |
| 5207 | #elif defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 5208 | conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_3; |
| 5209 | conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3; |
| 5210 | #elif defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 5211 | conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 5212 | conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 5213 | #else |
| 5214 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 5215 | #endif |
| 5216 | } |
Glenn Strauss | 2dfcea2 | 2022-03-14 17:26:42 -0400 | [diff] [blame] | 5217 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5218 | /* |
| 5219 | * Preset-specific defaults |
| 5220 | */ |
| 5221 | switch( preset ) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5222 | { |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5223 | /* |
| 5224 | * NSA Suite B |
| 5225 | */ |
| 5226 | case MBEDTLS_SSL_PRESET_SUITEB: |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5227 | |
Hanno Becker | d60b6c6 | 2021-04-29 12:04:11 +0100 | [diff] [blame] | 5228 | conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5229 | |
| 5230 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 5231 | conf->cert_profile = &mbedtls_x509_crt_profile_suiteb; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5232 | #endif |
| 5233 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5234 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5235 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 5236 | if( mbedtls_ssl_conf_is_tls12_only( conf ) ) |
| 5237 | conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs; |
| 5238 | else |
| 5239 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 5240 | conf->sig_algs = ssl_preset_suiteb_sig_algs; |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5241 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5242 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5243 | #if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 5244 | conf->curve_list = NULL; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5245 | #endif |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5246 | conf->group_list = ssl_preset_suiteb_groups; |
Manuel Pégourié-Gonnard | c98204e | 2015-08-11 04:21:01 +0200 | [diff] [blame] | 5247 | break; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5248 | |
| 5249 | /* |
| 5250 | * Default |
| 5251 | */ |
| 5252 | default: |
Ronald Cron | f660655 | 2022-03-15 11:23:25 +0100 | [diff] [blame] | 5253 | |
Hanno Becker | d60b6c6 | 2021-04-29 12:04:11 +0100 | [diff] [blame] | 5254 | conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites(); |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5255 | |
| 5256 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 5257 | conf->cert_profile = &mbedtls_x509_crt_profile_default; |
| 5258 | #endif |
| 5259 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5260 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5261 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 5262 | if( mbedtls_ssl_conf_is_tls12_only( conf ) ) |
| 5263 | conf->sig_algs = ssl_tls12_preset_default_sig_algs; |
| 5264 | else |
| 5265 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 5266 | conf->sig_algs = ssl_preset_default_sig_algs; |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5267 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5268 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5269 | #if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 5270 | conf->curve_list = NULL; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5271 | #endif |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5272 | conf->group_list = ssl_preset_default_groups; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5273 | |
| 5274 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C) |
| 5275 | conf->dhm_min_bitlen = 1024; |
| 5276 | #endif |
| 5277 | } |
| 5278 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5279 | return( 0 ); |
| 5280 | } |
| 5281 | |
| 5282 | /* |
| 5283 | * Free mbedtls_ssl_config |
| 5284 | */ |
| 5285 | void mbedtls_ssl_config_free( mbedtls_ssl_config *conf ) |
| 5286 | { |
| 5287 | #if defined(MBEDTLS_DHM_C) |
| 5288 | mbedtls_mpi_free( &conf->dhm_P ); |
| 5289 | mbedtls_mpi_free( &conf->dhm_G ); |
| 5290 | #endif |
| 5291 | |
Ronald Cron | 73fe8df | 2022-10-05 14:31:43 +0200 | [diff] [blame] | 5292 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED) |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 5293 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 5294 | if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) ) |
| 5295 | { |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 5296 | conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT; |
| 5297 | } |
Neil Armstrong | 8ecd668 | 2022-05-05 11:40:35 +0200 | [diff] [blame] | 5298 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5299 | if( conf->psk != NULL ) |
| 5300 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 5301 | mbedtls_platform_zeroize( conf->psk, conf->psk_len ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5302 | mbedtls_free( conf->psk ); |
Azim Khan | 27e8a12 | 2018-03-21 14:24:11 +0000 | [diff] [blame] | 5303 | conf->psk = NULL; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5304 | conf->psk_len = 0; |
junyeonLEE | 316b162 | 2017-12-20 16:29:30 +0900 | [diff] [blame] | 5305 | } |
| 5306 | |
| 5307 | if( conf->psk_identity != NULL ) |
| 5308 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 5309 | mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len ); |
junyeonLEE | 316b162 | 2017-12-20 16:29:30 +0900 | [diff] [blame] | 5310 | mbedtls_free( conf->psk_identity ); |
Azim Khan | 27e8a12 | 2018-03-21 14:24:11 +0000 | [diff] [blame] | 5311 | conf->psk_identity = NULL; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5312 | conf->psk_identity_len = 0; |
| 5313 | } |
Ronald Cron | 73fe8df | 2022-10-05 14:31:43 +0200 | [diff] [blame] | 5314 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */ |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5315 | |
| 5316 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 5317 | ssl_key_cert_free( conf->key_cert ); |
| 5318 | #endif |
| 5319 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 5320 | mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5321 | } |
| 5322 | |
Manuel Pégourié-Gonnard | 5674a97 | 2015-10-19 15:14:03 +0200 | [diff] [blame] | 5323 | #if defined(MBEDTLS_PK_C) && \ |
| 5324 | ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) ) |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 5325 | /* |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5326 | * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 5327 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5328 | 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] | 5329 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5330 | #if defined(MBEDTLS_RSA_C) |
| 5331 | if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) ) |
| 5332 | return( MBEDTLS_SSL_SIG_RSA ); |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 5333 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5334 | #if defined(MBEDTLS_ECDSA_C) |
| 5335 | if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) ) |
| 5336 | return( MBEDTLS_SSL_SIG_ECDSA ); |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 5337 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5338 | return( MBEDTLS_SSL_SIG_ANON ); |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 5339 | } |
| 5340 | |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 5341 | unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type ) |
| 5342 | { |
| 5343 | switch( type ) { |
| 5344 | case MBEDTLS_PK_RSA: |
| 5345 | return( MBEDTLS_SSL_SIG_RSA ); |
| 5346 | case MBEDTLS_PK_ECDSA: |
| 5347 | case MBEDTLS_PK_ECKEY: |
| 5348 | return( MBEDTLS_SSL_SIG_ECDSA ); |
| 5349 | default: |
| 5350 | return( MBEDTLS_SSL_SIG_ANON ); |
| 5351 | } |
| 5352 | } |
| 5353 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5354 | 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] | 5355 | { |
| 5356 | switch( sig ) |
| 5357 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5358 | #if defined(MBEDTLS_RSA_C) |
| 5359 | case MBEDTLS_SSL_SIG_RSA: |
| 5360 | return( MBEDTLS_PK_RSA ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 5361 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5362 | #if defined(MBEDTLS_ECDSA_C) |
| 5363 | case MBEDTLS_SSL_SIG_ECDSA: |
| 5364 | return( MBEDTLS_PK_ECDSA ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 5365 | #endif |
| 5366 | default: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5367 | return( MBEDTLS_PK_NONE ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 5368 | } |
| 5369 | } |
Manuel Pégourié-Gonnard | 5674a97 | 2015-10-19 15:14:03 +0200 | [diff] [blame] | 5370 | #endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */ |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 5371 | |
Manuel Pégourié-Gonnard | 1a48383 | 2013-09-20 12:29:15 +0200 | [diff] [blame] | 5372 | /* |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 5373 | * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX |
Manuel Pégourié-Gonnard | 1a48383 | 2013-09-20 12:29:15 +0200 | [diff] [blame] | 5374 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5375 | 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] | 5376 | { |
| 5377 | switch( hash ) |
| 5378 | { |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5379 | #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] | 5380 | case MBEDTLS_SSL_HASH_MD5: |
| 5381 | return( MBEDTLS_MD_MD5 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 5382 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5383 | #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] | 5384 | case MBEDTLS_SSL_HASH_SHA1: |
| 5385 | return( MBEDTLS_MD_SHA1 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 5386 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5387 | #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] | 5388 | case MBEDTLS_SSL_HASH_SHA224: |
| 5389 | return( MBEDTLS_MD_SHA224 ); |
Mateusz Starzyk | e3c48b4 | 2021-04-19 16:46:28 +0200 | [diff] [blame] | 5390 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5391 | #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] | 5392 | case MBEDTLS_SSL_HASH_SHA256: |
| 5393 | return( MBEDTLS_MD_SHA256 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 5394 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5395 | #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] | 5396 | case MBEDTLS_SSL_HASH_SHA384: |
| 5397 | return( MBEDTLS_MD_SHA384 ); |
Mateusz Starzyk | 3352a53 | 2021-04-06 14:28:22 +0200 | [diff] [blame] | 5398 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5399 | #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] | 5400 | case MBEDTLS_SSL_HASH_SHA512: |
| 5401 | return( MBEDTLS_MD_SHA512 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 5402 | #endif |
| 5403 | default: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5404 | return( MBEDTLS_MD_NONE ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 5405 | } |
| 5406 | } |
| 5407 | |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 5408 | /* |
| 5409 | * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX |
| 5410 | */ |
| 5411 | unsigned char mbedtls_ssl_hash_from_md_alg( int md ) |
| 5412 | { |
| 5413 | switch( md ) |
| 5414 | { |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5415 | #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] | 5416 | case MBEDTLS_MD_MD5: |
| 5417 | return( MBEDTLS_SSL_HASH_MD5 ); |
| 5418 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5419 | #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] | 5420 | case MBEDTLS_MD_SHA1: |
| 5421 | return( MBEDTLS_SSL_HASH_SHA1 ); |
| 5422 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5423 | #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] | 5424 | case MBEDTLS_MD_SHA224: |
| 5425 | return( MBEDTLS_SSL_HASH_SHA224 ); |
Mateusz Starzyk | e3c48b4 | 2021-04-19 16:46:28 +0200 | [diff] [blame] | 5426 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5427 | #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] | 5428 | case MBEDTLS_MD_SHA256: |
| 5429 | return( MBEDTLS_SSL_HASH_SHA256 ); |
| 5430 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5431 | #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] | 5432 | case MBEDTLS_MD_SHA384: |
| 5433 | return( MBEDTLS_SSL_HASH_SHA384 ); |
Mateusz Starzyk | 3352a53 | 2021-04-06 14:28:22 +0200 | [diff] [blame] | 5434 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5435 | #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] | 5436 | case MBEDTLS_MD_SHA512: |
| 5437 | return( MBEDTLS_SSL_HASH_SHA512 ); |
| 5438 | #endif |
| 5439 | default: |
| 5440 | return( MBEDTLS_SSL_HASH_NONE ); |
| 5441 | } |
| 5442 | } |
| 5443 | |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 5444 | /* |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 5445 | * 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] | 5446 | * 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] | 5447 | */ |
Manuel Pégourié-Gonnard | 0d63b84 | 2022-01-18 13:10:56 +0100 | [diff] [blame] | 5448 | 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] | 5449 | { |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5450 | const uint16_t *group_list = mbedtls_ssl_get_groups( ssl ); |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 5451 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5452 | if( group_list == NULL ) |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 5453 | return( -1 ); |
| 5454 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5455 | for( ; *group_list != 0; group_list++ ) |
| 5456 | { |
| 5457 | if( *group_list == tls_id ) |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 5458 | return( 0 ); |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5459 | } |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 5460 | |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 5461 | return( -1 ); |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 5462 | } |
Manuel Pégourié-Gonnard | 0d63b84 | 2022-01-18 13:10:56 +0100 | [diff] [blame] | 5463 | |
| 5464 | #if defined(MBEDTLS_ECP_C) |
| 5465 | /* |
| 5466 | * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id. |
| 5467 | */ |
| 5468 | int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id ) |
| 5469 | { |
Leonid Rozenboim | 19e5973 | 2022-08-08 16:52:38 -0700 | [diff] [blame] | 5470 | const mbedtls_ecp_curve_info *grp_info = |
Tom Cosgrove | bcc13c9 | 2022-08-24 15:08:16 +0100 | [diff] [blame] | 5471 | mbedtls_ecp_curve_info_from_grp_id( grp_id ); |
Leonid Rozenboim | 19e5973 | 2022-08-08 16:52:38 -0700 | [diff] [blame] | 5472 | |
Tom Cosgrove | bcc13c9 | 2022-08-24 15:08:16 +0100 | [diff] [blame] | 5473 | if ( grp_info == NULL ) |
Leonid Rozenboim | 19e5973 | 2022-08-08 16:52:38 -0700 | [diff] [blame] | 5474 | return -1; |
| 5475 | |
| 5476 | uint16_t tls_id = grp_info->tls_id; |
| 5477 | |
Manuel Pégourié-Gonnard | 0d63b84 | 2022-01-18 13:10:56 +0100 | [diff] [blame] | 5478 | return mbedtls_ssl_check_curve_tls_id( ssl, tls_id ); |
| 5479 | } |
Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 5480 | #endif /* MBEDTLS_ECP_C */ |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5481 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5482 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 5483 | int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert, |
| 5484 | const mbedtls_ssl_ciphersuite_t *ciphersuite, |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 5485 | int cert_endpoint, |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 5486 | uint32_t *flags ) |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5487 | { |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 5488 | int ret = 0; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5489 | int usage = 0; |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 5490 | const char *ext_oid; |
| 5491 | size_t ext_len; |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 5492 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5493 | if( cert_endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5494 | { |
| 5495 | /* Server part of the key exchange */ |
| 5496 | switch( ciphersuite->key_exchange ) |
| 5497 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5498 | case MBEDTLS_KEY_EXCHANGE_RSA: |
| 5499 | case MBEDTLS_KEY_EXCHANGE_RSA_PSK: |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 5500 | usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5501 | break; |
| 5502 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5503 | case MBEDTLS_KEY_EXCHANGE_DHE_RSA: |
| 5504 | case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA: |
| 5505 | case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA: |
| 5506 | usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5507 | break; |
| 5508 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5509 | case MBEDTLS_KEY_EXCHANGE_ECDH_RSA: |
| 5510 | case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA: |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 5511 | usage = MBEDTLS_X509_KU_KEY_AGREEMENT; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5512 | break; |
| 5513 | |
| 5514 | /* 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] | 5515 | case MBEDTLS_KEY_EXCHANGE_NONE: |
| 5516 | case MBEDTLS_KEY_EXCHANGE_PSK: |
| 5517 | case MBEDTLS_KEY_EXCHANGE_DHE_PSK: |
| 5518 | case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK: |
Manuel Pégourié-Gonnard | 557535d | 2015-09-15 17:53:32 +0200 | [diff] [blame] | 5519 | case MBEDTLS_KEY_EXCHANGE_ECJPAKE: |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5520 | usage = 0; |
| 5521 | } |
| 5522 | } |
| 5523 | else |
| 5524 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5525 | /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */ |
| 5526 | usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5527 | } |
| 5528 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5529 | if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 ) |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 5530 | { |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 5531 | *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE; |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 5532 | ret = -1; |
| 5533 | } |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5534 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5535 | if( cert_endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 5536 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5537 | ext_oid = MBEDTLS_OID_SERVER_AUTH; |
| 5538 | ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH ); |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 5539 | } |
| 5540 | else |
| 5541 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5542 | ext_oid = MBEDTLS_OID_CLIENT_AUTH; |
| 5543 | ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH ); |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 5544 | } |
| 5545 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5546 | 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] | 5547 | { |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 5548 | *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE; |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 5549 | ret = -1; |
| 5550 | } |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 5551 | |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 5552 | return( ret ); |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5553 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5554 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Manuel Pégourié-Gonnard | 3a306b9 | 2014-04-29 15:11:17 +0200 | [diff] [blame] | 5555 | |
Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 5556 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 5557 | int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl, |
| 5558 | const mbedtls_md_type_t md, |
| 5559 | unsigned char *dst, |
| 5560 | size_t dst_len, |
| 5561 | size_t *olen ) |
| 5562 | { |
Ronald Cron | f6893e1 | 2022-01-07 22:09:01 +0100 | [diff] [blame] | 5563 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 5564 | psa_hash_operation_t *hash_operation_to_clone; |
| 5565 | psa_hash_operation_t hash_operation = psa_hash_operation_init(); |
| 5566 | |
Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 5567 | *olen = 0; |
Ronald Cron | f6893e1 | 2022-01-07 22:09:01 +0100 | [diff] [blame] | 5568 | |
| 5569 | switch( md ) |
| 5570 | { |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5571 | #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] | 5572 | case MBEDTLS_MD_SHA384: |
| 5573 | hash_operation_to_clone = &ssl->handshake->fin_sha384_psa; |
| 5574 | break; |
| 5575 | #endif |
| 5576 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5577 | #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] | 5578 | case MBEDTLS_MD_SHA256: |
| 5579 | hash_operation_to_clone = &ssl->handshake->fin_sha256_psa; |
| 5580 | break; |
| 5581 | #endif |
| 5582 | |
| 5583 | default: |
| 5584 | goto exit; |
| 5585 | } |
| 5586 | |
| 5587 | status = psa_hash_clone( hash_operation_to_clone, &hash_operation ); |
| 5588 | if( status != PSA_SUCCESS ) |
| 5589 | goto exit; |
| 5590 | |
| 5591 | status = psa_hash_finish( &hash_operation, dst, dst_len, olen ); |
| 5592 | if( status != PSA_SUCCESS ) |
| 5593 | goto exit; |
| 5594 | |
| 5595 | exit: |
Andrzej Kurek | eabeb30 | 2022-10-17 07:52:51 -0400 | [diff] [blame] | 5596 | #if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \ |
| 5597 | !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
| 5598 | (void) ssl; |
| 5599 | #endif |
Ronald Cron | b788c04 | 2022-02-15 09:14:15 +0100 | [diff] [blame] | 5600 | return( psa_ssl_status_to_mbedtls( status ) ); |
Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 5601 | } |
| 5602 | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
| 5603 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5604 | #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] | 5605 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | 000f976 | 2021-09-14 11:12:51 +0800 | [diff] [blame] | 5606 | static int ssl_get_handshake_transcript_sha384( mbedtls_ssl_context *ssl, |
| 5607 | unsigned char *dst, |
| 5608 | size_t dst_len, |
| 5609 | size_t *olen ) |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5610 | { |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5611 | int ret; |
| 5612 | mbedtls_sha512_context sha512; |
| 5613 | |
| 5614 | if( dst_len < 48 ) |
| 5615 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 5616 | |
| 5617 | mbedtls_sha512_init( &sha512 ); |
Andrzej Kurek | a242e83 | 2022-08-11 10:03:14 -0400 | [diff] [blame] | 5618 | mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 ); |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5619 | |
| 5620 | if( ( ret = mbedtls_sha512_finish( &sha512, dst ) ) != 0 ) |
| 5621 | { |
| 5622 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha512_finish", ret ); |
| 5623 | goto exit; |
| 5624 | } |
| 5625 | |
| 5626 | *olen = 48; |
| 5627 | |
| 5628 | exit: |
| 5629 | |
| 5630 | mbedtls_sha512_free( &sha512 ); |
| 5631 | return( ret ); |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5632 | } |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5633 | #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] | 5634 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5635 | #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] | 5636 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | 000f976 | 2021-09-14 11:12:51 +0800 | [diff] [blame] | 5637 | static int ssl_get_handshake_transcript_sha256( mbedtls_ssl_context *ssl, |
| 5638 | unsigned char *dst, |
| 5639 | size_t dst_len, |
| 5640 | size_t *olen ) |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5641 | { |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5642 | int ret; |
| 5643 | mbedtls_sha256_context sha256; |
| 5644 | |
| 5645 | if( dst_len < 32 ) |
| 5646 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 5647 | |
| 5648 | mbedtls_sha256_init( &sha256 ); |
| 5649 | mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 ); |
Jerry Yu | c5aef88 | 2021-12-23 20:15:02 +0800 | [diff] [blame] | 5650 | |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5651 | if( ( ret = mbedtls_sha256_finish( &sha256, dst ) ) != 0 ) |
| 5652 | { |
| 5653 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha256_finish", ret ); |
| 5654 | goto exit; |
| 5655 | } |
| 5656 | |
| 5657 | *olen = 32; |
| 5658 | |
| 5659 | exit: |
| 5660 | |
| 5661 | mbedtls_sha256_free( &sha256 ); |
| 5662 | return( ret ); |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5663 | } |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5664 | #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] | 5665 | |
Jerry Yu | 000f976 | 2021-09-14 11:12:51 +0800 | [diff] [blame] | 5666 | int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl, |
| 5667 | const mbedtls_md_type_t md, |
| 5668 | unsigned char *dst, |
| 5669 | size_t dst_len, |
| 5670 | size_t *olen ) |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5671 | { |
Jerry Yu | c1ddeef | 2021-10-08 15:14:45 +0800 | [diff] [blame] | 5672 | switch( md ) |
| 5673 | { |
| 5674 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5675 | #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] | 5676 | case MBEDTLS_MD_SHA384: |
Jerry Yu | c5aef88 | 2021-12-23 20:15:02 +0800 | [diff] [blame] | 5677 | return( ssl_get_handshake_transcript_sha384( ssl, dst, dst_len, olen ) ); |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 5678 | #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] | 5679 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5680 | #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] | 5681 | case MBEDTLS_MD_SHA256: |
Jerry Yu | c5aef88 | 2021-12-23 20:15:02 +0800 | [diff] [blame] | 5682 | return( ssl_get_handshake_transcript_sha256( ssl, dst, dst_len, olen ) ); |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 5683 | #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] | 5684 | |
| 5685 | default: |
Andrzej Kurek | 409248a | 2022-10-24 10:33:21 -0400 | [diff] [blame] | 5686 | #if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \ |
| 5687 | !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
| 5688 | (void) ssl; |
| 5689 | (void) dst; |
| 5690 | (void) dst_len; |
| 5691 | (void) olen; |
| 5692 | #endif |
Jerry Yu | c1ddeef | 2021-10-08 15:14:45 +0800 | [diff] [blame] | 5693 | break; |
| 5694 | } |
Jerry Yu | c5aef88 | 2021-12-23 20:15:02 +0800 | [diff] [blame] | 5695 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5696 | } |
XiaokangQian | 647719a | 2021-12-07 09:16:29 +0000 | [diff] [blame] | 5697 | |
Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 5698 | #endif /* !MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5699 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5700 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 5701 | /* mbedtls_ssl_parse_sig_alg_ext() |
| 5702 | * |
| 5703 | * The `extension_data` field of signature algorithm contains a `SignatureSchemeList` |
| 5704 | * value (TLS 1.3 RFC8446): |
| 5705 | * enum { |
| 5706 | * .... |
| 5707 | * ecdsa_secp256r1_sha256( 0x0403 ), |
| 5708 | * ecdsa_secp384r1_sha384( 0x0503 ), |
| 5709 | * ecdsa_secp521r1_sha512( 0x0603 ), |
| 5710 | * .... |
| 5711 | * } SignatureScheme; |
| 5712 | * |
| 5713 | * struct { |
| 5714 | * SignatureScheme supported_signature_algorithms<2..2^16-2>; |
| 5715 | * } SignatureSchemeList; |
| 5716 | * |
| 5717 | * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm` |
| 5718 | * value (TLS 1.2 RFC5246): |
| 5719 | * enum { |
| 5720 | * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5), |
| 5721 | * sha512(6), (255) |
| 5722 | * } HashAlgorithm; |
| 5723 | * |
| 5724 | * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) } |
| 5725 | * SignatureAlgorithm; |
| 5726 | * |
| 5727 | * struct { |
| 5728 | * HashAlgorithm hash; |
| 5729 | * SignatureAlgorithm signature; |
| 5730 | * } SignatureAndHashAlgorithm; |
| 5731 | * |
| 5732 | * SignatureAndHashAlgorithm |
| 5733 | * supported_signature_algorithms<2..2^16-2>; |
| 5734 | * |
| 5735 | * The TLS 1.3 signature algorithm extension was defined to be a compatible |
| 5736 | * generalization of the TLS 1.2 signature algorithm extension. |
| 5737 | * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by |
| 5738 | * `SignatureScheme` field of TLS 1.3 |
| 5739 | * |
| 5740 | */ |
| 5741 | int mbedtls_ssl_parse_sig_alg_ext( mbedtls_ssl_context *ssl, |
| 5742 | const unsigned char *buf, |
| 5743 | const unsigned char *end ) |
| 5744 | { |
| 5745 | const unsigned char *p = buf; |
| 5746 | size_t supported_sig_algs_len = 0; |
| 5747 | const unsigned char *supported_sig_algs_end; |
| 5748 | uint16_t sig_alg; |
| 5749 | uint32_t common_idx = 0; |
| 5750 | |
| 5751 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 ); |
| 5752 | supported_sig_algs_len = MBEDTLS_GET_UINT16_BE( p, 0 ); |
| 5753 | p += 2; |
| 5754 | |
| 5755 | memset( ssl->handshake->received_sig_algs, 0, |
| 5756 | sizeof(ssl->handshake->received_sig_algs) ); |
| 5757 | |
| 5758 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, supported_sig_algs_len ); |
| 5759 | supported_sig_algs_end = p + supported_sig_algs_len; |
| 5760 | while( p < supported_sig_algs_end ) |
| 5761 | { |
| 5762 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, supported_sig_algs_end, 2 ); |
| 5763 | sig_alg = MBEDTLS_GET_UINT16_BE( p, 0 ); |
| 5764 | p += 2; |
Jerry Yu | f3b46b5 | 2022-06-19 16:52:27 +0800 | [diff] [blame] | 5765 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "received signature algorithm: 0x%x %s", |
| 5766 | sig_alg, |
| 5767 | mbedtls_ssl_sig_alg_to_str( sig_alg ) ) ); |
Jerry Yu | 2fe6c63 | 2022-06-29 10:02:38 +0800 | [diff] [blame] | 5768 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Jerry Yu | 52b7d92 | 2022-07-01 18:03:31 +0800 | [diff] [blame] | 5769 | if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 && |
Jerry Yu | 2fe6c63 | 2022-06-29 10:02:38 +0800 | [diff] [blame] | 5770 | ( ! ( mbedtls_ssl_sig_alg_is_supported( ssl, sig_alg ) && |
| 5771 | mbedtls_ssl_sig_alg_is_offered( ssl, sig_alg ) ) ) ) |
| 5772 | { |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 5773 | continue; |
Jerry Yu | 2fe6c63 | 2022-06-29 10:02:38 +0800 | [diff] [blame] | 5774 | } |
| 5775 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 5776 | |
Jerry Yu | f3b46b5 | 2022-06-19 16:52:27 +0800 | [diff] [blame] | 5777 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "valid signature algorithm: %s", |
| 5778 | mbedtls_ssl_sig_alg_to_str( sig_alg ) ) ); |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 5779 | |
| 5780 | if( common_idx + 1 < MBEDTLS_RECEIVED_SIG_ALGS_SIZE ) |
| 5781 | { |
| 5782 | ssl->handshake->received_sig_algs[common_idx] = sig_alg; |
| 5783 | common_idx += 1; |
| 5784 | } |
| 5785 | } |
| 5786 | /* Check that we consumed all the message. */ |
| 5787 | if( p != end ) |
| 5788 | { |
| 5789 | MBEDTLS_SSL_DEBUG_MSG( 1, |
| 5790 | ( "Signature algorithms extension length misaligned" ) ); |
| 5791 | MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR, |
| 5792 | MBEDTLS_ERR_SSL_DECODE_ERROR ); |
| 5793 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
| 5794 | } |
| 5795 | |
| 5796 | if( common_idx == 0 ) |
| 5797 | { |
| 5798 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "no signature algorithm in common" ) ); |
| 5799 | MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE, |
| 5800 | MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE ); |
| 5801 | return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE ); |
| 5802 | } |
| 5803 | |
Gabor Mezei | 15b95a6 | 2022-05-09 16:37:58 +0200 | [diff] [blame] | 5804 | ssl->handshake->received_sig_algs[common_idx] = MBEDTLS_TLS_SIG_NONE; |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 5805 | return( 0 ); |
| 5806 | } |
| 5807 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5808 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 5809 | |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5810 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 5811 | |
| 5812 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 5813 | |
| 5814 | static psa_status_t setup_psa_key_derivation( psa_key_derivation_operation_t* derivation, |
| 5815 | mbedtls_svc_key_id_t key, |
| 5816 | psa_algorithm_t alg, |
Neil Armstrong | 8ecd668 | 2022-05-05 11:40:35 +0200 | [diff] [blame] | 5817 | const unsigned char* raw_psk, size_t raw_psk_length, |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5818 | const unsigned char* seed, size_t seed_length, |
| 5819 | const unsigned char* label, size_t label_length, |
Przemek Stekiel | 51a1f36 | 2022-04-13 08:57:06 +0200 | [diff] [blame] | 5820 | const unsigned char* other_secret, |
| 5821 | size_t other_secret_length, |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5822 | size_t capacity ) |
| 5823 | { |
| 5824 | psa_status_t status; |
| 5825 | |
| 5826 | status = psa_key_derivation_setup( derivation, alg ); |
| 5827 | if( status != PSA_SUCCESS ) |
| 5828 | return( status ); |
| 5829 | |
| 5830 | if( PSA_ALG_IS_TLS12_PRF( alg ) || PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) ) |
| 5831 | { |
| 5832 | status = psa_key_derivation_input_bytes( derivation, |
| 5833 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 5834 | seed, seed_length ); |
| 5835 | if( status != PSA_SUCCESS ) |
| 5836 | return( status ); |
| 5837 | |
Przemek Stekiel | 51a1f36 | 2022-04-13 08:57:06 +0200 | [diff] [blame] | 5838 | if ( other_secret != NULL ) |
Przemek Stekiel | 1f02703 | 2022-04-05 17:12:11 +0200 | [diff] [blame] | 5839 | { |
| 5840 | status = psa_key_derivation_input_bytes( derivation, |
Przemek Stekiel | 51a1f36 | 2022-04-13 08:57:06 +0200 | [diff] [blame] | 5841 | PSA_KEY_DERIVATION_INPUT_OTHER_SECRET, |
| 5842 | other_secret, other_secret_length ); |
Przemek Stekiel | 1f02703 | 2022-04-05 17:12:11 +0200 | [diff] [blame] | 5843 | if( status != PSA_SUCCESS ) |
| 5844 | return( status ); |
| 5845 | } |
| 5846 | |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5847 | if( mbedtls_svc_key_id_is_null( key ) ) |
| 5848 | { |
| 5849 | status = psa_key_derivation_input_bytes( |
| 5850 | derivation, PSA_KEY_DERIVATION_INPUT_SECRET, |
Neil Armstrong | 8ecd668 | 2022-05-05 11:40:35 +0200 | [diff] [blame] | 5851 | raw_psk, raw_psk_length ); |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5852 | } |
| 5853 | else |
| 5854 | { |
| 5855 | status = psa_key_derivation_input_key( |
| 5856 | derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key ); |
| 5857 | } |
| 5858 | if( status != PSA_SUCCESS ) |
| 5859 | return( status ); |
| 5860 | |
| 5861 | status = psa_key_derivation_input_bytes( derivation, |
| 5862 | PSA_KEY_DERIVATION_INPUT_LABEL, |
| 5863 | label, label_length ); |
| 5864 | if( status != PSA_SUCCESS ) |
| 5865 | return( status ); |
| 5866 | } |
| 5867 | else |
| 5868 | { |
| 5869 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5870 | } |
| 5871 | |
| 5872 | status = psa_key_derivation_set_capacity( derivation, capacity ); |
| 5873 | if( status != PSA_SUCCESS ) |
| 5874 | return( status ); |
| 5875 | |
| 5876 | return( PSA_SUCCESS ); |
| 5877 | } |
| 5878 | |
Andrzej Kurek | 57d1063 | 2022-10-24 10:32:01 -0400 | [diff] [blame] | 5879 | #if defined(PSA_WANT_ALG_SHA_384) || \ |
| 5880 | defined(PSA_WANT_ALG_SHA_256) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 5881 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5882 | static int tls_prf_generic( mbedtls_md_type_t md_type, |
| 5883 | const unsigned char *secret, size_t slen, |
| 5884 | const char *label, |
| 5885 | const unsigned char *random, size_t rlen, |
| 5886 | unsigned char *dstbuf, size_t dlen ) |
| 5887 | { |
| 5888 | psa_status_t status; |
| 5889 | psa_algorithm_t alg; |
| 5890 | mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5891 | psa_key_derivation_operation_t derivation = |
| 5892 | PSA_KEY_DERIVATION_OPERATION_INIT; |
| 5893 | |
| 5894 | if( md_type == MBEDTLS_MD_SHA384 ) |
| 5895 | alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384); |
| 5896 | else |
| 5897 | alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256); |
| 5898 | |
| 5899 | /* Normally a "secret" should be long enough to be impossible to |
| 5900 | * find by brute force, and in particular should not be empty. But |
| 5901 | * this PRF is also used to derive an IV, in particular in EAP-TLS, |
| 5902 | * and for this use case it makes sense to have a 0-length "secret". |
| 5903 | * Since the key API doesn't allow importing a key of length 0, |
| 5904 | * keep master_key=0, which setup_psa_key_derivation() understands |
| 5905 | * to mean a 0-length "secret" input. */ |
| 5906 | if( slen != 0 ) |
| 5907 | { |
| 5908 | psa_key_attributes_t key_attributes = psa_key_attributes_init(); |
| 5909 | psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE ); |
| 5910 | psa_set_key_algorithm( &key_attributes, alg ); |
| 5911 | psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE ); |
| 5912 | |
| 5913 | status = psa_import_key( &key_attributes, secret, slen, &master_key ); |
| 5914 | if( status != PSA_SUCCESS ) |
| 5915 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 5916 | } |
| 5917 | |
| 5918 | status = setup_psa_key_derivation( &derivation, |
| 5919 | master_key, alg, |
Neil Armstrong | 8ecd668 | 2022-05-05 11:40:35 +0200 | [diff] [blame] | 5920 | NULL, 0, |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5921 | random, rlen, |
| 5922 | (unsigned char const *) label, |
| 5923 | (size_t) strlen( label ), |
Przemek Stekiel | 1f02703 | 2022-04-05 17:12:11 +0200 | [diff] [blame] | 5924 | NULL, 0, |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5925 | dlen ); |
| 5926 | if( status != PSA_SUCCESS ) |
| 5927 | { |
| 5928 | psa_key_derivation_abort( &derivation ); |
| 5929 | psa_destroy_key( master_key ); |
| 5930 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 5931 | } |
| 5932 | |
| 5933 | status = psa_key_derivation_output_bytes( &derivation, dstbuf, dlen ); |
| 5934 | if( status != PSA_SUCCESS ) |
| 5935 | { |
| 5936 | psa_key_derivation_abort( &derivation ); |
| 5937 | psa_destroy_key( master_key ); |
| 5938 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 5939 | } |
| 5940 | |
| 5941 | status = psa_key_derivation_abort( &derivation ); |
| 5942 | if( status != PSA_SUCCESS ) |
| 5943 | { |
| 5944 | psa_destroy_key( master_key ); |
| 5945 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 5946 | } |
| 5947 | |
| 5948 | if( ! mbedtls_svc_key_id_is_null( master_key ) ) |
| 5949 | status = psa_destroy_key( master_key ); |
| 5950 | if( status != PSA_SUCCESS ) |
| 5951 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 5952 | |
| 5953 | return( 0 ); |
| 5954 | } |
Andrzej Kurek | 57d1063 | 2022-10-24 10:32:01 -0400 | [diff] [blame] | 5955 | #endif /* PSA_WANT_ALG_SHA_256 || PSA_WANT_ALG_SHA_384 */ |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5956 | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
| 5957 | |
Andrzej Kurek | 57d1063 | 2022-10-24 10:32:01 -0400 | [diff] [blame] | 5958 | #if defined(MBEDTLS_MD_C) && \ |
| 5959 | ( defined(MBEDTLS_SHA256_C) || \ |
| 5960 | defined(MBEDTLS_SHA384_C) ) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 5961 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5962 | static int tls_prf_generic( mbedtls_md_type_t md_type, |
| 5963 | const unsigned char *secret, size_t slen, |
| 5964 | const char *label, |
| 5965 | const unsigned char *random, size_t rlen, |
| 5966 | unsigned char *dstbuf, size_t dlen ) |
| 5967 | { |
| 5968 | size_t nb; |
| 5969 | size_t i, j, k, md_len; |
| 5970 | unsigned char *tmp; |
| 5971 | size_t tmp_len = 0; |
| 5972 | unsigned char h_i[MBEDTLS_MD_MAX_SIZE]; |
| 5973 | const mbedtls_md_info_t *md_info; |
| 5974 | mbedtls_md_context_t md_ctx; |
| 5975 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 5976 | |
| 5977 | mbedtls_md_init( &md_ctx ); |
| 5978 | |
| 5979 | if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL ) |
| 5980 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 5981 | |
| 5982 | md_len = mbedtls_md_get_size( md_info ); |
| 5983 | |
| 5984 | tmp_len = md_len + strlen( label ) + rlen; |
| 5985 | tmp = mbedtls_calloc( 1, tmp_len ); |
| 5986 | if( tmp == NULL ) |
| 5987 | { |
| 5988 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 5989 | goto exit; |
| 5990 | } |
| 5991 | |
| 5992 | nb = strlen( label ); |
| 5993 | memcpy( tmp + md_len, label, nb ); |
| 5994 | memcpy( tmp + md_len + nb, random, rlen ); |
| 5995 | nb += rlen; |
| 5996 | |
| 5997 | /* |
| 5998 | * Compute P_<hash>(secret, label + random)[0..dlen] |
| 5999 | */ |
| 6000 | if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 ) |
| 6001 | goto exit; |
| 6002 | |
| 6003 | ret = mbedtls_md_hmac_starts( &md_ctx, secret, slen ); |
| 6004 | if( ret != 0 ) |
| 6005 | goto exit; |
| 6006 | ret = mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb ); |
| 6007 | if( ret != 0 ) |
| 6008 | goto exit; |
| 6009 | ret = mbedtls_md_hmac_finish( &md_ctx, tmp ); |
| 6010 | if( ret != 0 ) |
| 6011 | goto exit; |
| 6012 | |
| 6013 | for( i = 0; i < dlen; i += md_len ) |
| 6014 | { |
| 6015 | ret = mbedtls_md_hmac_reset ( &md_ctx ); |
| 6016 | if( ret != 0 ) |
| 6017 | goto exit; |
| 6018 | ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb ); |
| 6019 | if( ret != 0 ) |
| 6020 | goto exit; |
| 6021 | ret = mbedtls_md_hmac_finish( &md_ctx, h_i ); |
| 6022 | if( ret != 0 ) |
| 6023 | goto exit; |
| 6024 | |
| 6025 | ret = mbedtls_md_hmac_reset ( &md_ctx ); |
| 6026 | if( ret != 0 ) |
| 6027 | goto exit; |
| 6028 | ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len ); |
| 6029 | if( ret != 0 ) |
| 6030 | goto exit; |
| 6031 | ret = mbedtls_md_hmac_finish( &md_ctx, tmp ); |
| 6032 | if( ret != 0 ) |
| 6033 | goto exit; |
| 6034 | |
| 6035 | k = ( i + md_len > dlen ) ? dlen % md_len : md_len; |
| 6036 | |
| 6037 | for( j = 0; j < k; j++ ) |
| 6038 | dstbuf[i + j] = h_i[j]; |
| 6039 | } |
| 6040 | |
| 6041 | exit: |
| 6042 | mbedtls_md_free( &md_ctx ); |
| 6043 | |
Dave Rodgman | 29b9b2b | 2022-11-01 16:08:14 +0000 | [diff] [blame] | 6044 | if ( tmp != NULL ) |
| 6045 | mbedtls_platform_zeroize( tmp, tmp_len ); |
| 6046 | |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6047 | mbedtls_platform_zeroize( h_i, sizeof( h_i ) ); |
| 6048 | |
| 6049 | mbedtls_free( tmp ); |
| 6050 | |
| 6051 | return( ret ); |
| 6052 | } |
Andrzej Kurek | 57d1063 | 2022-10-24 10:32:01 -0400 | [diff] [blame] | 6053 | #endif /* MBEDTLS_MD_C && ( MBEDTLS_SHA256_C || MBEDTLS_SHA384_C ) */ |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6054 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 6055 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 6056 | #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] | 6057 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6058 | static int tls_prf_sha256( const unsigned char *secret, size_t slen, |
| 6059 | const char *label, |
| 6060 | const unsigned char *random, size_t rlen, |
| 6061 | unsigned char *dstbuf, size_t dlen ) |
| 6062 | { |
| 6063 | return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen, |
| 6064 | label, random, rlen, dstbuf, dlen ) ); |
| 6065 | } |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 6066 | #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] | 6067 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 6068 | #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] | 6069 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6070 | static int tls_prf_sha384( const unsigned char *secret, size_t slen, |
| 6071 | const char *label, |
| 6072 | const unsigned char *random, size_t rlen, |
| 6073 | unsigned char *dstbuf, size_t dlen ) |
| 6074 | { |
| 6075 | return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen, |
| 6076 | label, random, rlen, dstbuf, dlen ) ); |
| 6077 | } |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 6078 | #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] | 6079 | |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 6080 | /* |
| 6081 | * Set appropriate PRF function and other SSL / TLS1.2 functions |
| 6082 | * |
| 6083 | * Inputs: |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 6084 | * - hash associated with the ciphersuite (only used by TLS 1.2) |
| 6085 | * |
| 6086 | * Outputs: |
| 6087 | * - the tls_prf, calc_verify and calc_finished members of handshake structure |
| 6088 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6089 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 6090 | static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake, |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 6091 | mbedtls_md_type_t hash ) |
| 6092 | { |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 6093 | #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] | 6094 | if( hash == MBEDTLS_MD_SHA384 ) |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 6095 | { |
| 6096 | handshake->tls_prf = tls_prf_sha384; |
| 6097 | handshake->calc_verify = ssl_calc_verify_tls_sha384; |
| 6098 | handshake->calc_finished = ssl_calc_finished_tls_sha384; |
| 6099 | } |
| 6100 | else |
| 6101 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 6102 | #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] | 6103 | { |
Ronald Cron | 81591aa | 2022-03-07 09:05:51 +0100 | [diff] [blame] | 6104 | (void) hash; |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 6105 | handshake->tls_prf = tls_prf_sha256; |
| 6106 | handshake->calc_verify = ssl_calc_verify_tls_sha256; |
| 6107 | handshake->calc_finished = ssl_calc_finished_tls_sha256; |
| 6108 | } |
Ronald Cron | 81591aa | 2022-03-07 09:05:51 +0100 | [diff] [blame] | 6109 | #else |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 6110 | { |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 6111 | (void) handshake; |
Ronald Cron | 81591aa | 2022-03-07 09:05:51 +0100 | [diff] [blame] | 6112 | (void) hash; |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 6113 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 6114 | } |
Ronald Cron | 81591aa | 2022-03-07 09:05:51 +0100 | [diff] [blame] | 6115 | #endif |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 6116 | |
| 6117 | return( 0 ); |
| 6118 | } |
Jerry Yu | d6ab235 | 2022-02-17 14:03:43 +0800 | [diff] [blame] | 6119 | |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6120 | /* |
| 6121 | * Compute master secret if needed |
| 6122 | * |
| 6123 | * Parameters: |
| 6124 | * [in/out] handshake |
| 6125 | * [in] resume, premaster, extended_ms, calc_verify, tls_prf |
| 6126 | * (PSA-PSK) ciphersuite_info, psk_opaque |
| 6127 | * [out] premaster (cleared) |
| 6128 | * [out] master |
| 6129 | * [in] ssl: optionally used for debugging, EMS and PSA-PSK |
| 6130 | * debug: conf->f_dbg, conf->p_dbg |
| 6131 | * EMS: passed to calc_verify (debug + session_negotiate) |
Ronald Cron | a25cf58 | 2022-03-07 11:10:36 +0100 | [diff] [blame] | 6132 | * PSA-PSA: conf |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6133 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6134 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6135 | static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake, |
| 6136 | unsigned char *master, |
| 6137 | const mbedtls_ssl_context *ssl ) |
| 6138 | { |
| 6139 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 6140 | |
| 6141 | /* cf. RFC 5246, Section 8.1: |
| 6142 | * "The master secret is always exactly 48 bytes in length." */ |
| 6143 | size_t const master_secret_len = 48; |
| 6144 | |
| 6145 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 6146 | unsigned char session_hash[48]; |
| 6147 | #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */ |
| 6148 | |
| 6149 | /* The label for the KDF used for key expansion. |
| 6150 | * This is either "master secret" or "extended master secret" |
| 6151 | * depending on whether the Extended Master Secret extension |
| 6152 | * is used. */ |
| 6153 | char const *lbl = "master secret"; |
| 6154 | |
Przemek Stekiel | ae4ed30 | 2022-04-05 17:15:55 +0200 | [diff] [blame] | 6155 | /* The seed for the KDF used for key expansion. |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6156 | * - If the Extended Master Secret extension is not used, |
| 6157 | * this is ClientHello.Random + ServerHello.Random |
| 6158 | * (see Sect. 8.1 in RFC 5246). |
| 6159 | * - If the Extended Master Secret extension is used, |
| 6160 | * this is the transcript of the handshake so far. |
| 6161 | * (see Sect. 4 in RFC 7627). */ |
Przemek Stekiel | ae4ed30 | 2022-04-05 17:15:55 +0200 | [diff] [blame] | 6162 | unsigned char const *seed = handshake->randbytes; |
| 6163 | size_t seed_len = 64; |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6164 | |
| 6165 | #if !defined(MBEDTLS_DEBUG_C) && \ |
| 6166 | !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \ |
| 6167 | !(defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
| 6168 | defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)) |
| 6169 | ssl = NULL; /* make sure we don't use it except for those cases */ |
| 6170 | (void) ssl; |
| 6171 | #endif |
| 6172 | |
| 6173 | if( handshake->resume != 0 ) |
| 6174 | { |
| 6175 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) ); |
| 6176 | return( 0 ); |
| 6177 | } |
| 6178 | |
| 6179 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 6180 | if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED ) |
| 6181 | { |
| 6182 | lbl = "extended master secret"; |
Przemek Stekiel | ae4ed30 | 2022-04-05 17:15:55 +0200 | [diff] [blame] | 6183 | seed = session_hash; |
| 6184 | handshake->calc_verify( ssl, session_hash, &seed_len ); |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6185 | |
| 6186 | MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret", |
Przemek Stekiel | ae4ed30 | 2022-04-05 17:15:55 +0200 | [diff] [blame] | 6187 | session_hash, seed_len ); |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6188 | } |
Przemek Stekiel | 169bf0b | 2022-04-29 07:53:29 +0200 | [diff] [blame] | 6189 | #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */ |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6190 | |
Przemek Stekiel | 99114f3 | 2022-04-22 11:20:09 +0200 | [diff] [blame] | 6191 | #if defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
Przemek Stekiel | 8a4b7fd | 2022-04-28 09:22:22 +0200 | [diff] [blame] | 6192 | defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Neil Armstrong | e952a30 | 2022-05-03 10:22:14 +0200 | [diff] [blame] | 6193 | if( mbedtls_ssl_ciphersuite_uses_psk( handshake->ciphersuite_info ) == 1 ) |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6194 | { |
| 6195 | /* Perform PSK-to-MS expansion in a single step. */ |
| 6196 | psa_status_t status; |
| 6197 | psa_algorithm_t alg; |
| 6198 | mbedtls_svc_key_id_t psk; |
| 6199 | psa_key_derivation_operation_t derivation = |
| 6200 | PSA_KEY_DERIVATION_OPERATION_INIT; |
| 6201 | mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac; |
| 6202 | |
| 6203 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) ); |
| 6204 | |
| 6205 | psk = mbedtls_ssl_get_opaque_psk( ssl ); |
| 6206 | |
| 6207 | if( hash_alg == MBEDTLS_MD_SHA384 ) |
| 6208 | alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384); |
| 6209 | else |
| 6210 | alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256); |
| 6211 | |
Przemek Stekiel | 51a1f36 | 2022-04-13 08:57:06 +0200 | [diff] [blame] | 6212 | size_t other_secret_len = 0; |
| 6213 | unsigned char* other_secret = NULL; |
Przemek Stekiel | c203340 | 2022-04-05 17:19:41 +0200 | [diff] [blame] | 6214 | |
Przemek Stekiel | 19b80f8 | 2022-04-14 08:29:31 +0200 | [diff] [blame] | 6215 | switch( handshake->ciphersuite_info->key_exchange ) |
Przemek Stekiel | c203340 | 2022-04-05 17:19:41 +0200 | [diff] [blame] | 6216 | { |
Przemek Stekiel | 19b80f8 | 2022-04-14 08:29:31 +0200 | [diff] [blame] | 6217 | /* Provide other secret. |
Przemek Stekiel | 51a1f36 | 2022-04-13 08:57:06 +0200 | [diff] [blame] | 6218 | * Other secret is stored in premaster, where first 2 bytes hold the |
Przemek Stekiel | 19b80f8 | 2022-04-14 08:29:31 +0200 | [diff] [blame] | 6219 | * length of the other key. |
Przemek Stekiel | c203340 | 2022-04-05 17:19:41 +0200 | [diff] [blame] | 6220 | */ |
Przemek Stekiel | 19b80f8 | 2022-04-14 08:29:31 +0200 | [diff] [blame] | 6221 | case MBEDTLS_KEY_EXCHANGE_RSA_PSK: |
Przemek Stekiel | 8abcee9 | 2022-04-28 09:16:28 +0200 | [diff] [blame] | 6222 | /* For RSA-PSK other key length is always 48 bytes. */ |
Przemek Stekiel | 19b80f8 | 2022-04-14 08:29:31 +0200 | [diff] [blame] | 6223 | other_secret_len = 48; |
| 6224 | other_secret = handshake->premaster + 2; |
| 6225 | break; |
| 6226 | case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK: |
Przemek Stekiel | b293aaa | 2022-04-19 12:22:38 +0200 | [diff] [blame] | 6227 | case MBEDTLS_KEY_EXCHANGE_DHE_PSK: |
Przemek Stekiel | 19b80f8 | 2022-04-14 08:29:31 +0200 | [diff] [blame] | 6228 | other_secret_len = MBEDTLS_GET_UINT16_BE(handshake->premaster, 0); |
| 6229 | other_secret = handshake->premaster + 2; |
| 6230 | break; |
| 6231 | default: |
| 6232 | break; |
Przemek Stekiel | c203340 | 2022-04-05 17:19:41 +0200 | [diff] [blame] | 6233 | } |
| 6234 | |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6235 | status = setup_psa_key_derivation( &derivation, psk, alg, |
Neil Armstrong | 8ecd668 | 2022-05-05 11:40:35 +0200 | [diff] [blame] | 6236 | ssl->conf->psk, ssl->conf->psk_len, |
Przemek Stekiel | ae4ed30 | 2022-04-05 17:15:55 +0200 | [diff] [blame] | 6237 | seed, seed_len, |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6238 | (unsigned char const *) lbl, |
| 6239 | (size_t) strlen( lbl ), |
Przemek Stekiel | 51a1f36 | 2022-04-13 08:57:06 +0200 | [diff] [blame] | 6240 | other_secret, other_secret_len, |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6241 | master_secret_len ); |
| 6242 | if( status != PSA_SUCCESS ) |
| 6243 | { |
| 6244 | psa_key_derivation_abort( &derivation ); |
| 6245 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 6246 | } |
| 6247 | |
| 6248 | status = psa_key_derivation_output_bytes( &derivation, |
| 6249 | master, |
| 6250 | master_secret_len ); |
| 6251 | if( status != PSA_SUCCESS ) |
| 6252 | { |
| 6253 | psa_key_derivation_abort( &derivation ); |
| 6254 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 6255 | } |
| 6256 | |
| 6257 | status = psa_key_derivation_abort( &derivation ); |
| 6258 | if( status != PSA_SUCCESS ) |
| 6259 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 6260 | } |
| 6261 | else |
| 6262 | #endif |
| 6263 | { |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 6264 | #if defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
| 6265 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
| 6266 | if( handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE ) |
| 6267 | { |
| 6268 | psa_status_t status; |
| 6269 | psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS; |
| 6270 | psa_key_derivation_operation_t derivation = |
| 6271 | PSA_KEY_DERIVATION_OPERATION_INIT; |
| 6272 | |
| 6273 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PMS KDF for ECJPAKE" ) ); |
| 6274 | |
| 6275 | handshake->pmslen = PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE; |
| 6276 | |
| 6277 | status = psa_key_derivation_setup( &derivation, alg ); |
| 6278 | if( status != PSA_SUCCESS ) |
| 6279 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 6280 | |
| 6281 | status = psa_key_derivation_set_capacity( &derivation, |
| 6282 | PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE ); |
| 6283 | if( status != PSA_SUCCESS ) |
| 6284 | { |
| 6285 | psa_key_derivation_abort( &derivation ); |
| 6286 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 6287 | } |
| 6288 | |
| 6289 | status = psa_pake_get_implicit_key( &handshake->psa_pake_ctx, |
| 6290 | &derivation ); |
| 6291 | if( status != PSA_SUCCESS ) |
| 6292 | { |
| 6293 | psa_key_derivation_abort( &derivation ); |
| 6294 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 6295 | } |
| 6296 | |
| 6297 | status = psa_key_derivation_output_bytes( &derivation, |
| 6298 | handshake->premaster, |
| 6299 | handshake->pmslen ); |
| 6300 | if( status != PSA_SUCCESS ) |
| 6301 | { |
| 6302 | psa_key_derivation_abort( &derivation ); |
| 6303 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 6304 | } |
| 6305 | |
| 6306 | status = psa_key_derivation_abort( &derivation ); |
| 6307 | if( status != PSA_SUCCESS ) |
| 6308 | { |
| 6309 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 6310 | } |
| 6311 | } |
| 6312 | #endif |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6313 | ret = handshake->tls_prf( handshake->premaster, handshake->pmslen, |
Przemek Stekiel | ae4ed30 | 2022-04-05 17:15:55 +0200 | [diff] [blame] | 6314 | lbl, seed, seed_len, |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6315 | master, |
| 6316 | master_secret_len ); |
| 6317 | if( ret != 0 ) |
| 6318 | { |
| 6319 | MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret ); |
| 6320 | return( ret ); |
| 6321 | } |
| 6322 | |
| 6323 | MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", |
| 6324 | handshake->premaster, |
| 6325 | handshake->pmslen ); |
| 6326 | |
| 6327 | mbedtls_platform_zeroize( handshake->premaster, |
| 6328 | sizeof(handshake->premaster) ); |
| 6329 | } |
| 6330 | |
| 6331 | return( 0 ); |
| 6332 | } |
| 6333 | |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 6334 | int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl ) |
| 6335 | { |
| 6336 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 6337 | const mbedtls_ssl_ciphersuite_t * const ciphersuite_info = |
| 6338 | ssl->handshake->ciphersuite_info; |
| 6339 | |
| 6340 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) ); |
| 6341 | |
| 6342 | /* Set PRF, calc_verify and calc_finished function pointers */ |
| 6343 | ret = ssl_set_handshake_prfs( ssl->handshake, |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 6344 | ciphersuite_info->mac ); |
| 6345 | if( ret != 0 ) |
| 6346 | { |
| 6347 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret ); |
| 6348 | return( ret ); |
| 6349 | } |
| 6350 | |
| 6351 | /* Compute master secret if needed */ |
| 6352 | ret = ssl_compute_master( ssl->handshake, |
| 6353 | ssl->session_negotiate->master, |
| 6354 | ssl ); |
| 6355 | if( ret != 0 ) |
| 6356 | { |
| 6357 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret ); |
| 6358 | return( ret ); |
| 6359 | } |
| 6360 | |
| 6361 | /* Swap the client and server random values: |
| 6362 | * - MS derivation wanted client+server (RFC 5246 8.1) |
| 6363 | * - key derivation wants server+client (RFC 5246 6.3) */ |
| 6364 | { |
| 6365 | unsigned char tmp[64]; |
| 6366 | memcpy( tmp, ssl->handshake->randbytes, 64 ); |
| 6367 | memcpy( ssl->handshake->randbytes, tmp + 32, 32 ); |
| 6368 | memcpy( ssl->handshake->randbytes + 32, tmp, 32 ); |
| 6369 | mbedtls_platform_zeroize( tmp, sizeof( tmp ) ); |
| 6370 | } |
| 6371 | |
| 6372 | /* Populate transform structure */ |
| 6373 | ret = ssl_tls12_populate_transform( ssl->transform_negotiate, |
| 6374 | ssl->session_negotiate->ciphersuite, |
| 6375 | ssl->session_negotiate->master, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 6376 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 6377 | ssl->session_negotiate->encrypt_then_mac, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 6378 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 6379 | ssl->handshake->tls_prf, |
| 6380 | ssl->handshake->randbytes, |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 6381 | ssl->tls_version, |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 6382 | ssl->conf->endpoint, |
| 6383 | ssl ); |
| 6384 | if( ret != 0 ) |
| 6385 | { |
| 6386 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls12_populate_transform", ret ); |
| 6387 | return( ret ); |
| 6388 | } |
| 6389 | |
| 6390 | /* We no longer need Server/ClientHello.random values */ |
| 6391 | mbedtls_platform_zeroize( ssl->handshake->randbytes, |
| 6392 | sizeof( ssl->handshake->randbytes ) ); |
| 6393 | |
| 6394 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) ); |
| 6395 | |
| 6396 | return( 0 ); |
| 6397 | } |
Jerry Yu | 8392e0d | 2022-02-17 14:10:24 +0800 | [diff] [blame] | 6398 | |
Ronald Cron | 4dcbca9 | 2022-03-07 10:21:40 +0100 | [diff] [blame] | 6399 | int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md ) |
| 6400 | { |
Ronald Cron | 4dcbca9 | 2022-03-07 10:21:40 +0100 | [diff] [blame] | 6401 | switch( md ) |
| 6402 | { |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 6403 | #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] | 6404 | case MBEDTLS_SSL_HASH_SHA384: |
| 6405 | ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384; |
| 6406 | break; |
| 6407 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 6408 | #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] | 6409 | case MBEDTLS_SSL_HASH_SHA256: |
| 6410 | ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256; |
| 6411 | break; |
| 6412 | #endif |
| 6413 | default: |
| 6414 | return( -1 ); |
| 6415 | } |
Andrzej Kurek | eabeb30 | 2022-10-17 07:52:51 -0400 | [diff] [blame] | 6416 | #if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \ |
| 6417 | !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
| 6418 | (void) ssl; |
| 6419 | #endif |
Ronald Cron | c2f13a0 | 2022-03-07 10:25:24 +0100 | [diff] [blame] | 6420 | return( 0 ); |
Ronald Cron | 4dcbca9 | 2022-03-07 10:21:40 +0100 | [diff] [blame] | 6421 | } |
| 6422 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 6423 | #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] | 6424 | void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl, |
| 6425 | unsigned char *hash, |
| 6426 | size_t *hlen ) |
| 6427 | { |
| 6428 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 6429 | size_t hash_size; |
| 6430 | psa_status_t status; |
| 6431 | psa_hash_operation_t sha256_psa = psa_hash_operation_init(); |
| 6432 | |
| 6433 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) ); |
| 6434 | status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa ); |
| 6435 | if( status != PSA_SUCCESS ) |
| 6436 | { |
| 6437 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); |
| 6438 | return; |
| 6439 | } |
| 6440 | |
| 6441 | status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size ); |
| 6442 | if( status != PSA_SUCCESS ) |
| 6443 | { |
| 6444 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); |
| 6445 | return; |
| 6446 | } |
| 6447 | |
| 6448 | *hlen = 32; |
| 6449 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen ); |
| 6450 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) ); |
| 6451 | #else |
| 6452 | mbedtls_sha256_context sha256; |
| 6453 | |
| 6454 | mbedtls_sha256_init( &sha256 ); |
| 6455 | |
| 6456 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) ); |
| 6457 | |
| 6458 | mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 ); |
| 6459 | mbedtls_sha256_finish( &sha256, hash ); |
| 6460 | |
| 6461 | *hlen = 32; |
| 6462 | |
| 6463 | MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen ); |
| 6464 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) ); |
| 6465 | |
| 6466 | mbedtls_sha256_free( &sha256 ); |
| 6467 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 6468 | return; |
| 6469 | } |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 6470 | #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] | 6471 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 6472 | #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] | 6473 | void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl, |
| 6474 | unsigned char *hash, |
| 6475 | size_t *hlen ) |
| 6476 | { |
| 6477 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 6478 | size_t hash_size; |
| 6479 | psa_status_t status; |
| 6480 | psa_hash_operation_t sha384_psa = psa_hash_operation_init(); |
| 6481 | |
| 6482 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) ); |
| 6483 | status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa ); |
| 6484 | if( status != PSA_SUCCESS ) |
| 6485 | { |
| 6486 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); |
| 6487 | return; |
| 6488 | } |
| 6489 | |
| 6490 | status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size ); |
| 6491 | if( status != PSA_SUCCESS ) |
| 6492 | { |
| 6493 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); |
| 6494 | return; |
| 6495 | } |
| 6496 | |
| 6497 | *hlen = 48; |
| 6498 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen ); |
| 6499 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) ); |
| 6500 | #else |
| 6501 | mbedtls_sha512_context sha512; |
| 6502 | |
| 6503 | mbedtls_sha512_init( &sha512 ); |
| 6504 | |
| 6505 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) ); |
| 6506 | |
Andrzej Kurek | a242e83 | 2022-08-11 10:03:14 -0400 | [diff] [blame] | 6507 | mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 ); |
Jerry Yu | c1cb384 | 2022-02-17 14:13:48 +0800 | [diff] [blame] | 6508 | mbedtls_sha512_finish( &sha512, hash ); |
| 6509 | |
| 6510 | *hlen = 48; |
| 6511 | |
| 6512 | MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen ); |
| 6513 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) ); |
| 6514 | |
| 6515 | mbedtls_sha512_free( &sha512 ); |
| 6516 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 6517 | return; |
| 6518 | } |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 6519 | #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] | 6520 | |
Neil Armstrong | 80f6f32 | 2022-05-03 17:56:38 +0200 | [diff] [blame] | 6521 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
| 6522 | defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6523 | int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex ) |
| 6524 | { |
| 6525 | unsigned char *p = ssl->handshake->premaster; |
| 6526 | unsigned char *end = p + sizeof( ssl->handshake->premaster ); |
| 6527 | const unsigned char *psk = NULL; |
| 6528 | size_t psk_len = 0; |
Przemek Stekiel | b293aaa | 2022-04-19 12:22:38 +0200 | [diff] [blame] | 6529 | int psk_ret = mbedtls_ssl_get_psk( ssl, &psk, &psk_len ); |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6530 | |
Przemek Stekiel | b293aaa | 2022-04-19 12:22:38 +0200 | [diff] [blame] | 6531 | if( psk_ret == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED ) |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6532 | { |
| 6533 | /* |
| 6534 | * This should never happen because the existence of a PSK is always |
Przemek Stekiel | b293aaa | 2022-04-19 12:22:38 +0200 | [diff] [blame] | 6535 | * checked before calling this function. |
| 6536 | * |
| 6537 | * The exception is opaque DHE-PSK. For DHE-PSK fill premaster with |
Przemek Stekiel | 8abcee9 | 2022-04-28 09:16:28 +0200 | [diff] [blame] | 6538 | * the shared secret without PSK. |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6539 | */ |
Przemek Stekiel | b293aaa | 2022-04-19 12:22:38 +0200 | [diff] [blame] | 6540 | if ( key_ex != MBEDTLS_KEY_EXCHANGE_DHE_PSK ) |
| 6541 | { |
| 6542 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 6543 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 6544 | } |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6545 | } |
| 6546 | |
| 6547 | /* |
| 6548 | * PMS = struct { |
| 6549 | * opaque other_secret<0..2^16-1>; |
| 6550 | * opaque psk<0..2^16-1>; |
| 6551 | * }; |
| 6552 | * with "other_secret" depending on the particular key exchange |
| 6553 | */ |
| 6554 | #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) |
| 6555 | if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK ) |
| 6556 | { |
| 6557 | if( end - p < 2 ) |
| 6558 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6559 | |
| 6560 | MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 ); |
| 6561 | p += 2; |
| 6562 | |
| 6563 | if( end < p || (size_t)( end - p ) < psk_len ) |
| 6564 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6565 | |
| 6566 | memset( p, 0, psk_len ); |
| 6567 | p += psk_len; |
| 6568 | } |
| 6569 | else |
| 6570 | #endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */ |
| 6571 | #if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) |
| 6572 | if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK ) |
| 6573 | { |
| 6574 | /* |
| 6575 | * other_secret already set by the ClientKeyExchange message, |
| 6576 | * and is 48 bytes long |
| 6577 | */ |
| 6578 | if( end - p < 2 ) |
| 6579 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6580 | |
| 6581 | *p++ = 0; |
| 6582 | *p++ = 48; |
| 6583 | p += 48; |
| 6584 | } |
| 6585 | else |
| 6586 | #endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */ |
| 6587 | #if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) |
| 6588 | if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK ) |
| 6589 | { |
| 6590 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 6591 | size_t len; |
| 6592 | |
| 6593 | /* Write length only when we know the actual value */ |
| 6594 | if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx, |
| 6595 | p + 2, end - ( p + 2 ), &len, |
| 6596 | ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 ) |
| 6597 | { |
| 6598 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret ); |
| 6599 | return( ret ); |
| 6600 | } |
| 6601 | MBEDTLS_PUT_UINT16_BE( len, p, 0 ); |
| 6602 | p += 2 + len; |
| 6603 | |
| 6604 | MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K ); |
| 6605 | } |
| 6606 | else |
| 6607 | #endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */ |
Neil Armstrong | 80f6f32 | 2022-05-03 17:56:38 +0200 | [diff] [blame] | 6608 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6609 | if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ) |
| 6610 | { |
| 6611 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 6612 | size_t zlen; |
| 6613 | |
| 6614 | if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen, |
| 6615 | p + 2, end - ( p + 2 ), |
| 6616 | ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 ) |
| 6617 | { |
| 6618 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret ); |
| 6619 | return( ret ); |
| 6620 | } |
| 6621 | |
| 6622 | MBEDTLS_PUT_UINT16_BE( zlen, p, 0 ); |
| 6623 | p += 2 + zlen; |
| 6624 | |
| 6625 | MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx, |
| 6626 | MBEDTLS_DEBUG_ECDH_Z ); |
| 6627 | } |
| 6628 | else |
Neil Armstrong | 80f6f32 | 2022-05-03 17:56:38 +0200 | [diff] [blame] | 6629 | #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */ |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6630 | { |
| 6631 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 6632 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 6633 | } |
| 6634 | |
| 6635 | /* opaque psk<0..2^16-1>; */ |
| 6636 | if( end - p < 2 ) |
| 6637 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6638 | |
| 6639 | MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 ); |
| 6640 | p += 2; |
| 6641 | |
| 6642 | if( end < p || (size_t)( end - p ) < psk_len ) |
| 6643 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6644 | |
| 6645 | memcpy( p, psk, psk_len ); |
| 6646 | p += psk_len; |
| 6647 | |
| 6648 | ssl->handshake->pmslen = p - ssl->handshake->premaster; |
| 6649 | |
| 6650 | return( 0 ); |
| 6651 | } |
Neil Armstrong | 80f6f32 | 2022-05-03 17:56:38 +0200 | [diff] [blame] | 6652 | #endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ |
Jerry Yu | c2c673d | 2022-02-17 14:20:39 +0800 | [diff] [blame] | 6653 | |
| 6654 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6655 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | c2c673d | 2022-02-17 14:20:39 +0800 | [diff] [blame] | 6656 | static int ssl_write_hello_request( mbedtls_ssl_context *ssl ); |
| 6657 | |
| 6658 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 6659 | int mbedtls_ssl_resend_hello_request( mbedtls_ssl_context *ssl ) |
| 6660 | { |
| 6661 | /* If renegotiation is not enforced, retransmit until we would reach max |
| 6662 | * timeout if we were using the usual handshake doubling scheme */ |
| 6663 | if( ssl->conf->renego_max_records < 0 ) |
| 6664 | { |
| 6665 | uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1; |
| 6666 | unsigned char doublings = 1; |
| 6667 | |
| 6668 | while( ratio != 0 ) |
| 6669 | { |
| 6670 | ++doublings; |
| 6671 | ratio >>= 1; |
| 6672 | } |
| 6673 | |
| 6674 | if( ++ssl->renego_records_seen > doublings ) |
| 6675 | { |
| 6676 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) ); |
| 6677 | return( 0 ); |
| 6678 | } |
| 6679 | } |
| 6680 | |
| 6681 | return( ssl_write_hello_request( ssl ) ); |
| 6682 | } |
| 6683 | #endif |
| 6684 | #endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */ |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6685 | |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6686 | /* |
| 6687 | * Handshake functions |
| 6688 | */ |
| 6689 | #if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
| 6690 | /* No certificate support -> dummy functions */ |
| 6691 | int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl ) |
| 6692 | { |
| 6693 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 6694 | ssl->handshake->ciphersuite_info; |
| 6695 | |
| 6696 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) ); |
| 6697 | |
| 6698 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) |
| 6699 | { |
| 6700 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) ); |
| 6701 | ssl->state++; |
| 6702 | return( 0 ); |
| 6703 | } |
| 6704 | |
| 6705 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 6706 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 6707 | } |
| 6708 | |
| 6709 | int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl ) |
| 6710 | { |
| 6711 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 6712 | ssl->handshake->ciphersuite_info; |
| 6713 | |
| 6714 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) ); |
| 6715 | |
| 6716 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) |
| 6717 | { |
| 6718 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) ); |
| 6719 | ssl->state++; |
| 6720 | return( 0 ); |
| 6721 | } |
| 6722 | |
| 6723 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 6724 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 6725 | } |
| 6726 | |
| 6727 | #else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 6728 | /* Some certificate support -> implement write and parse */ |
| 6729 | |
| 6730 | int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl ) |
| 6731 | { |
| 6732 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
| 6733 | size_t i, n; |
| 6734 | const mbedtls_x509_crt *crt; |
| 6735 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 6736 | ssl->handshake->ciphersuite_info; |
| 6737 | |
| 6738 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) ); |
| 6739 | |
| 6740 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) |
| 6741 | { |
| 6742 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) ); |
| 6743 | ssl->state++; |
| 6744 | return( 0 ); |
| 6745 | } |
| 6746 | |
| 6747 | #if defined(MBEDTLS_SSL_CLI_C) |
| 6748 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 6749 | { |
| 6750 | if( ssl->handshake->client_auth == 0 ) |
| 6751 | { |
| 6752 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) ); |
| 6753 | ssl->state++; |
| 6754 | return( 0 ); |
| 6755 | } |
| 6756 | } |
| 6757 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 6758 | #if defined(MBEDTLS_SSL_SRV_C) |
| 6759 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 6760 | { |
| 6761 | if( mbedtls_ssl_own_cert( ssl ) == NULL ) |
| 6762 | { |
| 6763 | /* Should never happen because we shouldn't have picked the |
| 6764 | * ciphersuite if we don't have a certificate. */ |
| 6765 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 6766 | } |
| 6767 | } |
| 6768 | #endif |
| 6769 | |
| 6770 | MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) ); |
| 6771 | |
| 6772 | /* |
| 6773 | * 0 . 0 handshake type |
| 6774 | * 1 . 3 handshake length |
| 6775 | * 4 . 6 length of all certs |
| 6776 | * 7 . 9 length of cert. 1 |
| 6777 | * 10 . n-1 peer certificate |
| 6778 | * n . n+2 length of cert. 2 |
| 6779 | * n+3 . ... upper level cert, etc. |
| 6780 | */ |
| 6781 | i = 7; |
| 6782 | crt = mbedtls_ssl_own_cert( ssl ); |
| 6783 | |
| 6784 | while( crt != NULL ) |
| 6785 | { |
| 6786 | n = crt->raw.len; |
| 6787 | if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i ) |
| 6788 | { |
| 6789 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %" MBEDTLS_PRINTF_SIZET |
| 6790 | " > %" MBEDTLS_PRINTF_SIZET, |
| 6791 | i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN ) ); |
| 6792 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 6793 | } |
| 6794 | |
| 6795 | ssl->out_msg[i ] = MBEDTLS_BYTE_2( n ); |
| 6796 | ssl->out_msg[i + 1] = MBEDTLS_BYTE_1( n ); |
| 6797 | ssl->out_msg[i + 2] = MBEDTLS_BYTE_0( n ); |
| 6798 | |
| 6799 | i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n ); |
| 6800 | i += n; crt = crt->next; |
| 6801 | } |
| 6802 | |
| 6803 | ssl->out_msg[4] = MBEDTLS_BYTE_2( i - 7 ); |
| 6804 | ssl->out_msg[5] = MBEDTLS_BYTE_1( i - 7 ); |
| 6805 | ssl->out_msg[6] = MBEDTLS_BYTE_0( i - 7 ); |
| 6806 | |
| 6807 | ssl->out_msglen = i; |
| 6808 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 6809 | ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE; |
| 6810 | |
| 6811 | ssl->state++; |
| 6812 | |
| 6813 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
| 6814 | { |
| 6815 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); |
| 6816 | return( ret ); |
| 6817 | } |
| 6818 | |
| 6819 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) ); |
| 6820 | |
| 6821 | return( ret ); |
| 6822 | } |
| 6823 | |
| 6824 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) |
| 6825 | |
| 6826 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6827 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6828 | static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl, |
| 6829 | unsigned char *crt_buf, |
| 6830 | size_t crt_buf_len ) |
| 6831 | { |
| 6832 | mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert; |
| 6833 | |
| 6834 | if( peer_crt == NULL ) |
| 6835 | return( -1 ); |
| 6836 | |
| 6837 | if( peer_crt->raw.len != crt_buf_len ) |
| 6838 | return( -1 ); |
| 6839 | |
| 6840 | return( memcmp( peer_crt->raw.p, crt_buf, peer_crt->raw.len ) ); |
| 6841 | } |
| 6842 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6843 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6844 | static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl, |
| 6845 | unsigned char *crt_buf, |
| 6846 | size_t crt_buf_len ) |
| 6847 | { |
| 6848 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 6849 | unsigned char const * const peer_cert_digest = |
| 6850 | ssl->session->peer_cert_digest; |
| 6851 | mbedtls_md_type_t const peer_cert_digest_type = |
| 6852 | ssl->session->peer_cert_digest_type; |
| 6853 | mbedtls_md_info_t const * const digest_info = |
| 6854 | mbedtls_md_info_from_type( peer_cert_digest_type ); |
| 6855 | unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN]; |
| 6856 | size_t digest_len; |
| 6857 | |
| 6858 | if( peer_cert_digest == NULL || digest_info == NULL ) |
| 6859 | return( -1 ); |
| 6860 | |
| 6861 | digest_len = mbedtls_md_get_size( digest_info ); |
| 6862 | if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN ) |
| 6863 | return( -1 ); |
| 6864 | |
| 6865 | ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest ); |
| 6866 | if( ret != 0 ) |
| 6867 | return( -1 ); |
| 6868 | |
| 6869 | return( memcmp( tmp_digest, peer_cert_digest, digest_len ) ); |
| 6870 | } |
| 6871 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 6872 | #endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */ |
| 6873 | |
| 6874 | /* |
| 6875 | * Once the certificate message is read, parse it into a cert chain and |
| 6876 | * perform basic checks, but leave actual verification to the caller |
| 6877 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6878 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6879 | static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl, |
| 6880 | mbedtls_x509_crt *chain ) |
| 6881 | { |
| 6882 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 6883 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) |
| 6884 | int crt_cnt=0; |
| 6885 | #endif |
| 6886 | size_t i, n; |
| 6887 | uint8_t alert; |
| 6888 | |
| 6889 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) |
| 6890 | { |
| 6891 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
| 6892 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6893 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
| 6894 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
| 6895 | } |
| 6896 | |
| 6897 | if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ) |
| 6898 | { |
| 6899 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6900 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
| 6901 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
| 6902 | } |
| 6903 | |
| 6904 | if( ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 ) |
| 6905 | { |
| 6906 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
| 6907 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6908 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
| 6909 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
| 6910 | } |
| 6911 | |
| 6912 | i = mbedtls_ssl_hs_hdr_len( ssl ); |
| 6913 | |
| 6914 | /* |
| 6915 | * Same message structure as in mbedtls_ssl_write_certificate() |
| 6916 | */ |
| 6917 | n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2]; |
| 6918 | |
| 6919 | if( ssl->in_msg[i] != 0 || |
| 6920 | ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) ) |
| 6921 | { |
| 6922 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
| 6923 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6924 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
| 6925 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
| 6926 | } |
| 6927 | |
| 6928 | /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */ |
| 6929 | i += 3; |
| 6930 | |
| 6931 | /* Iterate through and parse the CRTs in the provided chain. */ |
| 6932 | while( i < ssl->in_hslen ) |
| 6933 | { |
| 6934 | /* Check that there's room for the next CRT's length fields. */ |
| 6935 | if ( i + 3 > ssl->in_hslen ) { |
| 6936 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
| 6937 | mbedtls_ssl_send_alert_message( ssl, |
| 6938 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6939 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
| 6940 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
| 6941 | } |
| 6942 | /* In theory, the CRT can be up to 2**24 Bytes, but we don't support |
| 6943 | * anything beyond 2**16 ~ 64K. */ |
| 6944 | if( ssl->in_msg[i] != 0 ) |
| 6945 | { |
| 6946 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
| 6947 | mbedtls_ssl_send_alert_message( ssl, |
| 6948 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6949 | MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT ); |
| 6950 | return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE ); |
| 6951 | } |
| 6952 | |
| 6953 | /* Read length of the next CRT in the chain. */ |
| 6954 | n = ( (unsigned int) ssl->in_msg[i + 1] << 8 ) |
| 6955 | | (unsigned int) ssl->in_msg[i + 2]; |
| 6956 | i += 3; |
| 6957 | |
| 6958 | if( n < 128 || i + n > ssl->in_hslen ) |
| 6959 | { |
| 6960 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
| 6961 | mbedtls_ssl_send_alert_message( ssl, |
| 6962 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6963 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
| 6964 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
| 6965 | } |
| 6966 | |
| 6967 | /* Check if we're handling the first CRT in the chain. */ |
| 6968 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) |
| 6969 | if( crt_cnt++ == 0 && |
| 6970 | ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
| 6971 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
| 6972 | { |
| 6973 | /* During client-side renegotiation, check that the server's |
| 6974 | * end-CRTs hasn't changed compared to the initial handshake, |
| 6975 | * mitigating the triple handshake attack. On success, reuse |
| 6976 | * the original end-CRT instead of parsing it again. */ |
| 6977 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) ); |
| 6978 | if( ssl_check_peer_crt_unchanged( ssl, |
| 6979 | &ssl->in_msg[i], |
| 6980 | n ) != 0 ) |
| 6981 | { |
| 6982 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) ); |
| 6983 | mbedtls_ssl_send_alert_message( ssl, |
| 6984 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6985 | MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED ); |
| 6986 | return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE ); |
| 6987 | } |
| 6988 | |
| 6989 | /* Now we can safely free the original chain. */ |
| 6990 | ssl_clear_peer_cert( ssl->session ); |
| 6991 | } |
| 6992 | #endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */ |
| 6993 | |
| 6994 | /* Parse the next certificate in the chain. */ |
| 6995 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 6996 | ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n ); |
| 6997 | #else |
| 6998 | /* If we don't need to store the CRT chain permanently, parse |
| 6999 | * it in-place from the input buffer instead of making a copy. */ |
| 7000 | ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n ); |
| 7001 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 7002 | switch( ret ) |
| 7003 | { |
| 7004 | case 0: /*ok*/ |
| 7005 | case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND: |
| 7006 | /* Ignore certificate with an unknown algorithm: maybe a |
| 7007 | prior certificate was already trusted. */ |
| 7008 | break; |
| 7009 | |
| 7010 | case MBEDTLS_ERR_X509_ALLOC_FAILED: |
| 7011 | alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR; |
| 7012 | goto crt_parse_der_failed; |
| 7013 | |
| 7014 | case MBEDTLS_ERR_X509_UNKNOWN_VERSION: |
| 7015 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 7016 | goto crt_parse_der_failed; |
| 7017 | |
| 7018 | default: |
| 7019 | alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT; |
| 7020 | crt_parse_der_failed: |
| 7021 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert ); |
| 7022 | MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret ); |
| 7023 | return( ret ); |
| 7024 | } |
| 7025 | |
| 7026 | i += n; |
| 7027 | } |
| 7028 | |
| 7029 | MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain ); |
| 7030 | return( 0 ); |
| 7031 | } |
| 7032 | |
| 7033 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 7034 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7035 | static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl ) |
| 7036 | { |
| 7037 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 7038 | return( -1 ); |
| 7039 | |
| 7040 | if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) && |
| 7041 | ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 7042 | ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE && |
| 7043 | memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 ) |
| 7044 | { |
Ronald Cron | 1938588 | 2022-06-15 16:26:13 +0200 | [diff] [blame] | 7045 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "peer has no certificate" ) ); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7046 | return( 0 ); |
| 7047 | } |
| 7048 | return( -1 ); |
| 7049 | } |
| 7050 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 7051 | |
| 7052 | /* Check if a certificate message is expected. |
| 7053 | * Return either |
| 7054 | * - SSL_CERTIFICATE_EXPECTED, or |
| 7055 | * - SSL_CERTIFICATE_SKIP |
| 7056 | * indicating whether a Certificate message is expected or not. |
| 7057 | */ |
| 7058 | #define SSL_CERTIFICATE_EXPECTED 0 |
| 7059 | #define SSL_CERTIFICATE_SKIP 1 |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 7060 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7061 | static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl, |
| 7062 | int authmode ) |
| 7063 | { |
| 7064 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 7065 | ssl->handshake->ciphersuite_info; |
| 7066 | |
| 7067 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) |
| 7068 | return( SSL_CERTIFICATE_SKIP ); |
| 7069 | |
| 7070 | #if defined(MBEDTLS_SSL_SRV_C) |
| 7071 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 7072 | { |
| 7073 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ) |
| 7074 | return( SSL_CERTIFICATE_SKIP ); |
| 7075 | |
| 7076 | if( authmode == MBEDTLS_SSL_VERIFY_NONE ) |
| 7077 | { |
| 7078 | ssl->session_negotiate->verify_result = |
| 7079 | MBEDTLS_X509_BADCERT_SKIP_VERIFY; |
| 7080 | return( SSL_CERTIFICATE_SKIP ); |
| 7081 | } |
| 7082 | } |
| 7083 | #else |
| 7084 | ((void) authmode); |
| 7085 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 7086 | |
| 7087 | return( SSL_CERTIFICATE_EXPECTED ); |
| 7088 | } |
| 7089 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 7090 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7091 | static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl, |
| 7092 | int authmode, |
| 7093 | mbedtls_x509_crt *chain, |
| 7094 | void *rs_ctx ) |
| 7095 | { |
| 7096 | int ret = 0; |
| 7097 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 7098 | ssl->handshake->ciphersuite_info; |
| 7099 | int have_ca_chain = 0; |
| 7100 | |
| 7101 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *); |
| 7102 | void *p_vrfy; |
| 7103 | |
| 7104 | if( authmode == MBEDTLS_SSL_VERIFY_NONE ) |
| 7105 | return( 0 ); |
| 7106 | |
| 7107 | if( ssl->f_vrfy != NULL ) |
| 7108 | { |
| 7109 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) ); |
| 7110 | f_vrfy = ssl->f_vrfy; |
| 7111 | p_vrfy = ssl->p_vrfy; |
| 7112 | } |
| 7113 | else |
| 7114 | { |
| 7115 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) ); |
| 7116 | f_vrfy = ssl->conf->f_vrfy; |
| 7117 | p_vrfy = ssl->conf->p_vrfy; |
| 7118 | } |
| 7119 | |
| 7120 | /* |
| 7121 | * Main check: verify certificate |
| 7122 | */ |
| 7123 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 7124 | if( ssl->conf->f_ca_cb != NULL ) |
| 7125 | { |
| 7126 | ((void) rs_ctx); |
| 7127 | have_ca_chain = 1; |
| 7128 | |
| 7129 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) ); |
| 7130 | ret = mbedtls_x509_crt_verify_with_ca_cb( |
| 7131 | chain, |
| 7132 | ssl->conf->f_ca_cb, |
| 7133 | ssl->conf->p_ca_cb, |
| 7134 | ssl->conf->cert_profile, |
| 7135 | ssl->hostname, |
| 7136 | &ssl->session_negotiate->verify_result, |
| 7137 | f_vrfy, p_vrfy ); |
| 7138 | } |
| 7139 | else |
| 7140 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
| 7141 | { |
| 7142 | mbedtls_x509_crt *ca_chain; |
| 7143 | mbedtls_x509_crl *ca_crl; |
| 7144 | |
| 7145 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 7146 | if( ssl->handshake->sni_ca_chain != NULL ) |
| 7147 | { |
| 7148 | ca_chain = ssl->handshake->sni_ca_chain; |
| 7149 | ca_crl = ssl->handshake->sni_ca_crl; |
| 7150 | } |
| 7151 | else |
| 7152 | #endif |
| 7153 | { |
| 7154 | ca_chain = ssl->conf->ca_chain; |
| 7155 | ca_crl = ssl->conf->ca_crl; |
| 7156 | } |
| 7157 | |
| 7158 | if( ca_chain != NULL ) |
| 7159 | have_ca_chain = 1; |
| 7160 | |
| 7161 | ret = mbedtls_x509_crt_verify_restartable( |
| 7162 | chain, |
| 7163 | ca_chain, ca_crl, |
| 7164 | ssl->conf->cert_profile, |
| 7165 | ssl->hostname, |
| 7166 | &ssl->session_negotiate->verify_result, |
| 7167 | f_vrfy, p_vrfy, rs_ctx ); |
| 7168 | } |
| 7169 | |
| 7170 | if( ret != 0 ) |
| 7171 | { |
| 7172 | MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret ); |
| 7173 | } |
| 7174 | |
| 7175 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
| 7176 | if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS ) |
| 7177 | return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS ); |
| 7178 | #endif |
| 7179 | |
| 7180 | /* |
| 7181 | * Secondary checks: always done, but change 'ret' only if it was 0 |
| 7182 | */ |
| 7183 | |
| 7184 | #if defined(MBEDTLS_ECP_C) |
| 7185 | { |
| 7186 | const mbedtls_pk_context *pk = &chain->pk; |
| 7187 | |
Manuel Pégourié-Gonnard | 66b0d61 | 2022-06-17 10:49:29 +0200 | [diff] [blame] | 7188 | /* If certificate uses an EC key, make sure the curve is OK. |
| 7189 | * This is a public key, so it can't be opaque, so can_do() is a good |
| 7190 | * enough check to ensure pk_ec() is safe to use here. */ |
Leonid Rozenboim | 19e5973 | 2022-08-08 16:52:38 -0700 | [diff] [blame] | 7191 | if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) ) |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7192 | { |
Leonid Rozenboim | 19e5973 | 2022-08-08 16:52:38 -0700 | [diff] [blame] | 7193 | /* and in the unlikely case the above assumption no longer holds |
| 7194 | * we are making sure that pk_ec() here does not return a NULL |
| 7195 | */ |
| 7196 | const mbedtls_ecp_keypair *ec = mbedtls_pk_ec( *pk ); |
| 7197 | if( ec == NULL ) |
| 7198 | { |
Tom Cosgrove | 20c1137 | 2022-08-24 15:06:13 +0100 | [diff] [blame] | 7199 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_pk_ec() returned NULL" ) ); |
Leonid Rozenboim | 19e5973 | 2022-08-08 16:52:38 -0700 | [diff] [blame] | 7200 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 7201 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7202 | |
Leonid Rozenboim | 19e5973 | 2022-08-08 16:52:38 -0700 | [diff] [blame] | 7203 | if( mbedtls_ssl_check_curve( ssl, ec->grp.id ) != 0 ) |
| 7204 | { |
| 7205 | ssl->session_negotiate->verify_result |= |
| 7206 | MBEDTLS_X509_BADCERT_BAD_KEY; |
| 7207 | |
| 7208 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) ); |
| 7209 | if( ret == 0 ) |
| 7210 | ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE; |
| 7211 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7212 | } |
| 7213 | } |
| 7214 | #endif /* MBEDTLS_ECP_C */ |
| 7215 | |
| 7216 | if( mbedtls_ssl_check_cert_usage( chain, |
| 7217 | ciphersuite_info, |
| 7218 | ! ssl->conf->endpoint, |
| 7219 | &ssl->session_negotiate->verify_result ) != 0 ) |
| 7220 | { |
| 7221 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) ); |
| 7222 | if( ret == 0 ) |
| 7223 | ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE; |
| 7224 | } |
| 7225 | |
| 7226 | /* mbedtls_x509_crt_verify_with_profile is supposed to report a |
| 7227 | * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED, |
| 7228 | * with details encoded in the verification flags. All other kinds |
| 7229 | * of error codes, including those from the user provided f_vrfy |
| 7230 | * functions, are treated as fatal and lead to a failure of |
| 7231 | * ssl_parse_certificate even if verification was optional. */ |
| 7232 | if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL && |
| 7233 | ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED || |
| 7234 | ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE ) ) |
| 7235 | { |
| 7236 | ret = 0; |
| 7237 | } |
| 7238 | |
| 7239 | if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED ) |
| 7240 | { |
| 7241 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) ); |
| 7242 | ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED; |
| 7243 | } |
| 7244 | |
| 7245 | if( ret != 0 ) |
| 7246 | { |
| 7247 | uint8_t alert; |
| 7248 | |
| 7249 | /* The certificate may have been rejected for several reasons. |
| 7250 | Pick one and send the corresponding alert. Which alert to send |
| 7251 | may be a subject of debate in some cases. */ |
| 7252 | if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER ) |
| 7253 | alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED; |
| 7254 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH ) |
| 7255 | alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT; |
| 7256 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE ) |
| 7257 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 7258 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE ) |
| 7259 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 7260 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE ) |
| 7261 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 7262 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK ) |
| 7263 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 7264 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY ) |
| 7265 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 7266 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED ) |
| 7267 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED; |
| 7268 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED ) |
| 7269 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED; |
| 7270 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED ) |
| 7271 | alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA; |
| 7272 | else |
| 7273 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN; |
| 7274 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7275 | alert ); |
| 7276 | } |
| 7277 | |
| 7278 | #if defined(MBEDTLS_DEBUG_C) |
| 7279 | if( ssl->session_negotiate->verify_result != 0 ) |
| 7280 | { |
| 7281 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %08x", |
| 7282 | (unsigned int) ssl->session_negotiate->verify_result ) ); |
| 7283 | } |
| 7284 | else |
| 7285 | { |
| 7286 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) ); |
| 7287 | } |
| 7288 | #endif /* MBEDTLS_DEBUG_C */ |
| 7289 | |
| 7290 | return( ret ); |
| 7291 | } |
| 7292 | |
| 7293 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 7294 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7295 | static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl, |
| 7296 | unsigned char *start, size_t len ) |
| 7297 | { |
| 7298 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 7299 | /* Remember digest of the peer's end-CRT. */ |
| 7300 | ssl->session_negotiate->peer_cert_digest = |
| 7301 | mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ); |
| 7302 | if( ssl->session_negotiate->peer_cert_digest == NULL ) |
| 7303 | { |
| 7304 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", |
| 7305 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ); |
| 7306 | mbedtls_ssl_send_alert_message( ssl, |
| 7307 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7308 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
| 7309 | |
| 7310 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 7311 | } |
| 7312 | |
| 7313 | ret = mbedtls_md( mbedtls_md_info_from_type( |
| 7314 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ), |
| 7315 | start, len, |
| 7316 | ssl->session_negotiate->peer_cert_digest ); |
| 7317 | |
| 7318 | ssl->session_negotiate->peer_cert_digest_type = |
| 7319 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE; |
| 7320 | ssl->session_negotiate->peer_cert_digest_len = |
| 7321 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN; |
| 7322 | |
| 7323 | return( ret ); |
| 7324 | } |
| 7325 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 7326 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7327 | static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl, |
| 7328 | unsigned char *start, size_t len ) |
| 7329 | { |
| 7330 | unsigned char *end = start + len; |
| 7331 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 7332 | |
| 7333 | /* Make a copy of the peer's raw public key. */ |
| 7334 | mbedtls_pk_init( &ssl->handshake->peer_pubkey ); |
| 7335 | ret = mbedtls_pk_parse_subpubkey( &start, end, |
| 7336 | &ssl->handshake->peer_pubkey ); |
| 7337 | if( ret != 0 ) |
| 7338 | { |
| 7339 | /* We should have parsed the public key before. */ |
| 7340 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 7341 | } |
| 7342 | |
| 7343 | return( 0 ); |
| 7344 | } |
| 7345 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 7346 | |
| 7347 | int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl ) |
| 7348 | { |
| 7349 | int ret = 0; |
| 7350 | int crt_expected; |
| 7351 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 7352 | const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET |
| 7353 | ? ssl->handshake->sni_authmode |
| 7354 | : ssl->conf->authmode; |
| 7355 | #else |
| 7356 | const int authmode = ssl->conf->authmode; |
| 7357 | #endif |
| 7358 | void *rs_ctx = NULL; |
| 7359 | mbedtls_x509_crt *chain = NULL; |
| 7360 | |
| 7361 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) ); |
| 7362 | |
| 7363 | crt_expected = ssl_parse_certificate_coordinate( ssl, authmode ); |
| 7364 | if( crt_expected == SSL_CERTIFICATE_SKIP ) |
| 7365 | { |
| 7366 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) ); |
| 7367 | goto exit; |
| 7368 | } |
| 7369 | |
| 7370 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
| 7371 | if( ssl->handshake->ecrs_enabled && |
| 7372 | ssl->handshake->ecrs_state == ssl_ecrs_crt_verify ) |
| 7373 | { |
| 7374 | chain = ssl->handshake->ecrs_peer_cert; |
| 7375 | ssl->handshake->ecrs_peer_cert = NULL; |
| 7376 | goto crt_verify; |
| 7377 | } |
| 7378 | #endif |
| 7379 | |
| 7380 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) |
| 7381 | { |
| 7382 | /* mbedtls_ssl_read_record may have sent an alert already. We |
| 7383 | let it decide whether to alert. */ |
| 7384 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
| 7385 | goto exit; |
| 7386 | } |
| 7387 | |
| 7388 | #if defined(MBEDTLS_SSL_SRV_C) |
| 7389 | if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 ) |
| 7390 | { |
| 7391 | ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING; |
| 7392 | |
| 7393 | if( authmode != MBEDTLS_SSL_VERIFY_OPTIONAL ) |
| 7394 | ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE; |
| 7395 | |
| 7396 | goto exit; |
| 7397 | } |
| 7398 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 7399 | |
| 7400 | /* Clear existing peer CRT structure in case we tried to |
| 7401 | * reuse a session but it failed, and allocate a new one. */ |
| 7402 | ssl_clear_peer_cert( ssl->session_negotiate ); |
| 7403 | |
| 7404 | chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ); |
| 7405 | if( chain == NULL ) |
| 7406 | { |
| 7407 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", |
| 7408 | sizeof( mbedtls_x509_crt ) ) ); |
| 7409 | mbedtls_ssl_send_alert_message( ssl, |
| 7410 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7411 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
| 7412 | |
| 7413 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 7414 | goto exit; |
| 7415 | } |
| 7416 | mbedtls_x509_crt_init( chain ); |
| 7417 | |
| 7418 | ret = ssl_parse_certificate_chain( ssl, chain ); |
| 7419 | if( ret != 0 ) |
| 7420 | goto exit; |
| 7421 | |
| 7422 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
| 7423 | if( ssl->handshake->ecrs_enabled) |
| 7424 | ssl->handshake->ecrs_state = ssl_ecrs_crt_verify; |
| 7425 | |
| 7426 | crt_verify: |
| 7427 | if( ssl->handshake->ecrs_enabled) |
| 7428 | rs_ctx = &ssl->handshake->ecrs_ctx; |
| 7429 | #endif |
| 7430 | |
| 7431 | ret = ssl_parse_certificate_verify( ssl, authmode, |
| 7432 | chain, rs_ctx ); |
| 7433 | if( ret != 0 ) |
| 7434 | goto exit; |
| 7435 | |
| 7436 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 7437 | { |
| 7438 | unsigned char *crt_start, *pk_start; |
| 7439 | size_t crt_len, pk_len; |
| 7440 | |
| 7441 | /* We parse the CRT chain without copying, so |
| 7442 | * these pointers point into the input buffer, |
| 7443 | * and are hence still valid after freeing the |
| 7444 | * CRT chain. */ |
| 7445 | |
| 7446 | crt_start = chain->raw.p; |
| 7447 | crt_len = chain->raw.len; |
| 7448 | |
| 7449 | pk_start = chain->pk_raw.p; |
| 7450 | pk_len = chain->pk_raw.len; |
| 7451 | |
| 7452 | /* Free the CRT structures before computing |
| 7453 | * digest and copying the peer's public key. */ |
| 7454 | mbedtls_x509_crt_free( chain ); |
| 7455 | mbedtls_free( chain ); |
| 7456 | chain = NULL; |
| 7457 | |
| 7458 | ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len ); |
| 7459 | if( ret != 0 ) |
| 7460 | goto exit; |
| 7461 | |
| 7462 | ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len ); |
| 7463 | if( ret != 0 ) |
| 7464 | goto exit; |
| 7465 | } |
| 7466 | #else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 7467 | /* Pass ownership to session structure. */ |
| 7468 | ssl->session_negotiate->peer_cert = chain; |
| 7469 | chain = NULL; |
| 7470 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 7471 | |
| 7472 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) ); |
| 7473 | |
| 7474 | exit: |
| 7475 | |
| 7476 | if( ret == 0 ) |
| 7477 | ssl->state++; |
| 7478 | |
| 7479 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
| 7480 | if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS ) |
| 7481 | { |
| 7482 | ssl->handshake->ecrs_peer_cert = chain; |
| 7483 | chain = NULL; |
| 7484 | } |
| 7485 | #endif |
| 7486 | |
| 7487 | if( chain != NULL ) |
| 7488 | { |
| 7489 | mbedtls_x509_crt_free( chain ); |
| 7490 | mbedtls_free( chain ); |
| 7491 | } |
| 7492 | |
| 7493 | return( ret ); |
| 7494 | } |
| 7495 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 7496 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 7497 | #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] | 7498 | static void ssl_calc_finished_tls_sha256( |
| 7499 | mbedtls_ssl_context *ssl, unsigned char *buf, int from ) |
| 7500 | { |
| 7501 | int len = 12; |
| 7502 | const char *sender; |
| 7503 | unsigned char padbuf[32]; |
| 7504 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7505 | size_t hash_size; |
| 7506 | psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT; |
| 7507 | psa_status_t status; |
| 7508 | #else |
| 7509 | mbedtls_sha256_context sha256; |
| 7510 | #endif |
| 7511 | |
| 7512 | mbedtls_ssl_session *session = ssl->session_negotiate; |
| 7513 | if( !session ) |
| 7514 | session = ssl->session; |
| 7515 | |
| 7516 | sender = ( from == MBEDTLS_SSL_IS_CLIENT ) |
| 7517 | ? "client finished" |
| 7518 | : "server finished"; |
| 7519 | |
| 7520 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7521 | sha256_psa = psa_hash_operation_init(); |
| 7522 | |
| 7523 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) ); |
| 7524 | |
| 7525 | status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa ); |
| 7526 | if( status != PSA_SUCCESS ) |
| 7527 | { |
| 7528 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); |
| 7529 | return; |
| 7530 | } |
| 7531 | |
| 7532 | status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size ); |
| 7533 | if( status != PSA_SUCCESS ) |
| 7534 | { |
| 7535 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); |
| 7536 | return; |
| 7537 | } |
| 7538 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 ); |
| 7539 | #else |
| 7540 | |
| 7541 | mbedtls_sha256_init( &sha256 ); |
| 7542 | |
| 7543 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) ); |
| 7544 | |
| 7545 | mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 ); |
| 7546 | |
| 7547 | /* |
| 7548 | * TLSv1.2: |
| 7549 | * hash = PRF( master, finished_label, |
| 7550 | * Hash( handshake ) )[0.11] |
| 7551 | */ |
| 7552 | |
| 7553 | #if !defined(MBEDTLS_SHA256_ALT) |
| 7554 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *) |
| 7555 | sha256.state, sizeof( sha256.state ) ); |
| 7556 | #endif |
| 7557 | |
| 7558 | mbedtls_sha256_finish( &sha256, padbuf ); |
| 7559 | mbedtls_sha256_free( &sha256 ); |
| 7560 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 7561 | |
| 7562 | ssl->handshake->tls_prf( session->master, 48, sender, |
| 7563 | padbuf, 32, buf, len ); |
| 7564 | |
| 7565 | MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len ); |
| 7566 | |
| 7567 | mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) ); |
| 7568 | |
| 7569 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) ); |
| 7570 | } |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 7571 | #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] | 7572 | |
Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 7573 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 7574 | #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] | 7575 | static void ssl_calc_finished_tls_sha384( |
| 7576 | mbedtls_ssl_context *ssl, unsigned char *buf, int from ) |
| 7577 | { |
| 7578 | int len = 12; |
| 7579 | const char *sender; |
| 7580 | unsigned char padbuf[48]; |
| 7581 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7582 | size_t hash_size; |
| 7583 | psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT; |
| 7584 | psa_status_t status; |
| 7585 | #else |
| 7586 | mbedtls_sha512_context sha512; |
| 7587 | #endif |
| 7588 | |
| 7589 | mbedtls_ssl_session *session = ssl->session_negotiate; |
| 7590 | if( !session ) |
| 7591 | session = ssl->session; |
| 7592 | |
| 7593 | sender = ( from == MBEDTLS_SSL_IS_CLIENT ) |
| 7594 | ? "client finished" |
| 7595 | : "server finished"; |
| 7596 | |
| 7597 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7598 | sha384_psa = psa_hash_operation_init(); |
| 7599 | |
| 7600 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) ); |
| 7601 | |
| 7602 | status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa ); |
| 7603 | if( status != PSA_SUCCESS ) |
| 7604 | { |
| 7605 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); |
| 7606 | return; |
| 7607 | } |
| 7608 | |
| 7609 | status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size ); |
| 7610 | if( status != PSA_SUCCESS ) |
| 7611 | { |
| 7612 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); |
| 7613 | return; |
| 7614 | } |
| 7615 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 ); |
| 7616 | #else |
| 7617 | mbedtls_sha512_init( &sha512 ); |
| 7618 | |
| 7619 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) ); |
| 7620 | |
Andrzej Kurek | a242e83 | 2022-08-11 10:03:14 -0400 | [diff] [blame] | 7621 | mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 ); |
Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 7622 | |
| 7623 | /* |
| 7624 | * TLSv1.2: |
| 7625 | * hash = PRF( master, finished_label, |
| 7626 | * Hash( handshake ) )[0.11] |
| 7627 | */ |
| 7628 | |
| 7629 | #if !defined(MBEDTLS_SHA512_ALT) |
| 7630 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *) |
| 7631 | sha512.state, sizeof( sha512.state ) ); |
| 7632 | #endif |
| 7633 | mbedtls_sha512_finish( &sha512, padbuf ); |
| 7634 | |
| 7635 | mbedtls_sha512_free( &sha512 ); |
| 7636 | #endif |
| 7637 | |
| 7638 | ssl->handshake->tls_prf( session->master, 48, sender, |
| 7639 | padbuf, 48, buf, len ); |
| 7640 | |
| 7641 | MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len ); |
| 7642 | |
| 7643 | mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) ); |
| 7644 | |
| 7645 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) ); |
| 7646 | } |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 7647 | #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] | 7648 | |
Jerry Yu | aef0015 | 2022-02-17 14:27:31 +0800 | [diff] [blame] | 7649 | void mbedtls_ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl ) |
| 7650 | { |
| 7651 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) ); |
| 7652 | |
| 7653 | /* |
| 7654 | * Free our handshake params |
| 7655 | */ |
| 7656 | mbedtls_ssl_handshake_free( ssl ); |
| 7657 | mbedtls_free( ssl->handshake ); |
| 7658 | ssl->handshake = NULL; |
| 7659 | |
| 7660 | /* |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 7661 | * Free the previous transform and switch in the current one |
Jerry Yu | aef0015 | 2022-02-17 14:27:31 +0800 | [diff] [blame] | 7662 | */ |
| 7663 | if( ssl->transform ) |
| 7664 | { |
| 7665 | mbedtls_ssl_transform_free( ssl->transform ); |
| 7666 | mbedtls_free( ssl->transform ); |
| 7667 | } |
| 7668 | ssl->transform = ssl->transform_negotiate; |
| 7669 | ssl->transform_negotiate = NULL; |
| 7670 | |
| 7671 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) ); |
| 7672 | } |
| 7673 | |
Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 7674 | void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl ) |
| 7675 | { |
| 7676 | int resume = ssl->handshake->resume; |
| 7677 | |
| 7678 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) ); |
| 7679 | |
| 7680 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 7681 | if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
| 7682 | { |
| 7683 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE; |
| 7684 | ssl->renego_records_seen = 0; |
| 7685 | } |
| 7686 | #endif |
| 7687 | |
| 7688 | /* |
| 7689 | * Free the previous session and switch in the current one |
| 7690 | */ |
| 7691 | if( ssl->session ) |
| 7692 | { |
| 7693 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 7694 | /* RFC 7366 3.1: keep the EtM state */ |
| 7695 | ssl->session_negotiate->encrypt_then_mac = |
| 7696 | ssl->session->encrypt_then_mac; |
| 7697 | #endif |
| 7698 | |
| 7699 | mbedtls_ssl_session_free( ssl->session ); |
| 7700 | mbedtls_free( ssl->session ); |
| 7701 | } |
| 7702 | ssl->session = ssl->session_negotiate; |
| 7703 | ssl->session_negotiate = NULL; |
| 7704 | |
| 7705 | /* |
| 7706 | * Add cache entry |
| 7707 | */ |
| 7708 | if( ssl->conf->f_set_cache != NULL && |
| 7709 | ssl->session->id_len != 0 && |
| 7710 | resume == 0 ) |
| 7711 | { |
| 7712 | if( ssl->conf->f_set_cache( ssl->conf->p_cache, |
| 7713 | ssl->session->id, |
| 7714 | ssl->session->id_len, |
| 7715 | ssl->session ) != 0 ) |
| 7716 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) ); |
| 7717 | } |
| 7718 | |
| 7719 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 7720 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 7721 | ssl->handshake->flight != NULL ) |
| 7722 | { |
| 7723 | /* Cancel handshake timer */ |
| 7724 | mbedtls_ssl_set_timer( ssl, 0 ); |
| 7725 | |
| 7726 | /* Keep last flight around in case we need to resend it: |
| 7727 | * we need the handshake and transform structures for that */ |
| 7728 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) ); |
| 7729 | } |
| 7730 | else |
| 7731 | #endif |
| 7732 | mbedtls_ssl_handshake_wrapup_free_hs_transform( ssl ); |
| 7733 | |
Jerry Yu | 5ed73ff | 2022-10-27 13:08:42 +0800 | [diff] [blame] | 7734 | ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER; |
Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 7735 | |
| 7736 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) ); |
| 7737 | } |
| 7738 | |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 7739 | int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl ) |
| 7740 | { |
| 7741 | int ret, hash_len; |
| 7742 | |
| 7743 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) ); |
| 7744 | |
| 7745 | mbedtls_ssl_update_out_pointers( ssl, ssl->transform_negotiate ); |
| 7746 | |
| 7747 | ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint ); |
| 7748 | |
| 7749 | /* |
| 7750 | * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites |
| 7751 | * may define some other value. Currently (early 2016), no defined |
| 7752 | * ciphersuite does this (and this is unlikely to change as activity has |
| 7753 | * moved to TLS 1.3 now) so we can keep the hardcoded 12 here. |
| 7754 | */ |
| 7755 | hash_len = 12; |
| 7756 | |
| 7757 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 7758 | ssl->verify_data_len = hash_len; |
| 7759 | memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len ); |
| 7760 | #endif |
| 7761 | |
| 7762 | ssl->out_msglen = 4 + hash_len; |
| 7763 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 7764 | ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED; |
| 7765 | |
| 7766 | /* |
| 7767 | * In case of session resuming, invert the client and server |
| 7768 | * ChangeCipherSpec messages order. |
| 7769 | */ |
| 7770 | if( ssl->handshake->resume != 0 ) |
| 7771 | { |
| 7772 | #if defined(MBEDTLS_SSL_CLI_C) |
| 7773 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 7774 | ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP; |
| 7775 | #endif |
| 7776 | #if defined(MBEDTLS_SSL_SRV_C) |
| 7777 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 7778 | ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC; |
| 7779 | #endif |
| 7780 | } |
| 7781 | else |
| 7782 | ssl->state++; |
| 7783 | |
| 7784 | /* |
| 7785 | * Switch to our negotiated transform and session parameters for outbound |
| 7786 | * data. |
| 7787 | */ |
| 7788 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) ); |
| 7789 | |
| 7790 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 7791 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 7792 | { |
| 7793 | unsigned char i; |
| 7794 | |
| 7795 | /* Remember current epoch settings for resending */ |
| 7796 | ssl->handshake->alt_transform_out = ssl->transform_out; |
| 7797 | memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, |
| 7798 | sizeof( ssl->handshake->alt_out_ctr ) ); |
| 7799 | |
| 7800 | /* Set sequence_number to zero */ |
| 7801 | memset( &ssl->cur_out_ctr[2], 0, sizeof( ssl->cur_out_ctr ) - 2 ); |
| 7802 | |
| 7803 | |
| 7804 | /* Increment epoch */ |
| 7805 | for( i = 2; i > 0; i-- ) |
| 7806 | if( ++ssl->cur_out_ctr[i - 1] != 0 ) |
| 7807 | break; |
| 7808 | |
| 7809 | /* The loop goes to its end iff the counter is wrapping */ |
| 7810 | if( i == 0 ) |
| 7811 | { |
| 7812 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) ); |
| 7813 | return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING ); |
| 7814 | } |
| 7815 | } |
| 7816 | else |
| 7817 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 7818 | memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) ); |
| 7819 | |
| 7820 | ssl->transform_out = ssl->transform_negotiate; |
| 7821 | ssl->session_out = ssl->session_negotiate; |
| 7822 | |
| 7823 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 7824 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 7825 | mbedtls_ssl_send_flight_completed( ssl ); |
| 7826 | #endif |
| 7827 | |
| 7828 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
| 7829 | { |
| 7830 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); |
| 7831 | return( ret ); |
| 7832 | } |
| 7833 | |
| 7834 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 7835 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 7836 | ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 ) |
| 7837 | { |
| 7838 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret ); |
| 7839 | return( ret ); |
| 7840 | } |
| 7841 | #endif |
| 7842 | |
| 7843 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) ); |
| 7844 | |
| 7845 | return( 0 ); |
| 7846 | } |
| 7847 | |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 7848 | #define SSL_MAX_HASH_LEN 12 |
| 7849 | |
| 7850 | int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl ) |
| 7851 | { |
| 7852 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 7853 | unsigned int hash_len = 12; |
| 7854 | unsigned char buf[SSL_MAX_HASH_LEN]; |
| 7855 | |
| 7856 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) ); |
| 7857 | |
| 7858 | ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 ); |
| 7859 | |
| 7860 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) |
| 7861 | { |
| 7862 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
| 7863 | goto exit; |
| 7864 | } |
| 7865 | |
| 7866 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) |
| 7867 | { |
| 7868 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) ); |
| 7869 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7870 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
| 7871 | ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; |
| 7872 | goto exit; |
| 7873 | } |
| 7874 | |
| 7875 | if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ) |
| 7876 | { |
| 7877 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7878 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
| 7879 | ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; |
| 7880 | goto exit; |
| 7881 | } |
| 7882 | |
| 7883 | if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len ) |
| 7884 | { |
| 7885 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) ); |
| 7886 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7887 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
| 7888 | ret = MBEDTLS_ERR_SSL_DECODE_ERROR; |
| 7889 | goto exit; |
| 7890 | } |
| 7891 | |
| 7892 | if( mbedtls_ct_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), |
| 7893 | buf, hash_len ) != 0 ) |
| 7894 | { |
| 7895 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) ); |
| 7896 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7897 | MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR ); |
| 7898 | ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE; |
| 7899 | goto exit; |
| 7900 | } |
| 7901 | |
| 7902 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 7903 | ssl->verify_data_len = hash_len; |
| 7904 | memcpy( ssl->peer_verify_data, buf, hash_len ); |
| 7905 | #endif |
| 7906 | |
| 7907 | if( ssl->handshake->resume != 0 ) |
| 7908 | { |
| 7909 | #if defined(MBEDTLS_SSL_CLI_C) |
| 7910 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 7911 | ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC; |
| 7912 | #endif |
| 7913 | #if defined(MBEDTLS_SSL_SRV_C) |
| 7914 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 7915 | ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP; |
| 7916 | #endif |
| 7917 | } |
| 7918 | else |
| 7919 | ssl->state++; |
| 7920 | |
| 7921 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 7922 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 7923 | mbedtls_ssl_recv_flight_completed( ssl ); |
| 7924 | #endif |
| 7925 | |
| 7926 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) ); |
| 7927 | |
| 7928 | exit: |
| 7929 | mbedtls_platform_zeroize( buf, hash_len ); |
| 7930 | return( ret ); |
| 7931 | } |
| 7932 | |
Jerry Yu | 392112c | 2022-02-17 14:34:10 +0800 | [diff] [blame] | 7933 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
| 7934 | /* |
| 7935 | * Helper to get TLS 1.2 PRF from ciphersuite |
| 7936 | * (Duplicates bits of logic from ssl_set_handshake_prfs().) |
| 7937 | */ |
| 7938 | static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id ) |
| 7939 | { |
Jerry Yu | 392112c | 2022-02-17 14:34:10 +0800 | [diff] [blame] | 7940 | const mbedtls_ssl_ciphersuite_t * const ciphersuite_info = |
Andrzej Kurek | 6832774 | 2022-10-03 06:18:18 -0400 | [diff] [blame] | 7941 | mbedtls_ssl_ciphersuite_from_id( ciphersuite_id ); |
| 7942 | #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] | 7943 | if( ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA384 ) |
Jerry Yu | 392112c | 2022-02-17 14:34:10 +0800 | [diff] [blame] | 7944 | return( tls_prf_sha384 ); |
Andrzej Kurek | 894edde | 2022-09-29 06:31:14 -0400 | [diff] [blame] | 7945 | else |
Jerry Yu | 392112c | 2022-02-17 14:34:10 +0800 | [diff] [blame] | 7946 | #endif |
Andrzej Kurek | 894edde | 2022-09-29 06:31:14 -0400 | [diff] [blame] | 7947 | #if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
| 7948 | { |
| 7949 | if( ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA256 ) |
| 7950 | return( tls_prf_sha256 ); |
| 7951 | } |
| 7952 | #endif |
| 7953 | #if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \ |
| 7954 | !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
| 7955 | (void) ciphersuite_info; |
| 7956 | #endif |
Andrzej Kurek | eabeb30 | 2022-10-17 07:52:51 -0400 | [diff] [blame] | 7957 | |
Andrzej Kurek | 894edde | 2022-09-29 06:31:14 -0400 | [diff] [blame] | 7958 | return( NULL ); |
Jerry Yu | 392112c | 2022-02-17 14:34:10 +0800 | [diff] [blame] | 7959 | } |
| 7960 | #endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */ |
Jerry Yu | e93ffcd | 2022-02-17 14:37:06 +0800 | [diff] [blame] | 7961 | |
| 7962 | static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf ) |
| 7963 | { |
| 7964 | ((void) tls_prf); |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 7965 | #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] | 7966 | if( tls_prf == tls_prf_sha384 ) |
| 7967 | { |
| 7968 | return( MBEDTLS_SSL_TLS_PRF_SHA384 ); |
| 7969 | } |
| 7970 | else |
| 7971 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 7972 | #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] | 7973 | if( tls_prf == tls_prf_sha256 ) |
| 7974 | { |
| 7975 | return( MBEDTLS_SSL_TLS_PRF_SHA256 ); |
| 7976 | } |
| 7977 | else |
| 7978 | #endif |
| 7979 | return( MBEDTLS_SSL_TLS_PRF_NONE ); |
| 7980 | } |
| 7981 | |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7982 | /* |
| 7983 | * Populate a transform structure with session keys and all the other |
| 7984 | * necessary information. |
| 7985 | * |
| 7986 | * Parameters: |
| 7987 | * - [in/out]: transform: structure to populate |
| 7988 | * [in] must be just initialised with mbedtls_ssl_transform_init() |
| 7989 | * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf() |
| 7990 | * - [in] ciphersuite |
| 7991 | * - [in] master |
| 7992 | * - [in] encrypt_then_mac |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7993 | * - [in] tls_prf: pointer to PRF to use for key derivation |
| 7994 | * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random |
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 7995 | * - [in] tls_version: TLS version |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7996 | * - [in] endpoint: client or server |
| 7997 | * - [in] ssl: used for: |
| 7998 | * - ssl->conf->{f,p}_export_keys |
| 7999 | * [in] optionally used for: |
| 8000 | * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg |
| 8001 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 8002 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8003 | static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform, |
| 8004 | int ciphersuite, |
| 8005 | const unsigned char master[48], |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 8006 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8007 | int encrypt_then_mac, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 8008 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8009 | ssl_tls_prf_t tls_prf, |
| 8010 | const unsigned char randbytes[64], |
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 8011 | mbedtls_ssl_protocol_version tls_version, |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8012 | unsigned endpoint, |
| 8013 | const mbedtls_ssl_context *ssl ) |
| 8014 | { |
| 8015 | int ret = 0; |
| 8016 | unsigned char keyblk[256]; |
| 8017 | unsigned char *key1; |
| 8018 | unsigned char *key2; |
| 8019 | unsigned char *mac_enc; |
| 8020 | unsigned char *mac_dec; |
| 8021 | size_t mac_key_len = 0; |
| 8022 | size_t iv_copy_len; |
| 8023 | size_t keylen; |
| 8024 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info; |
Neil Armstrong | 7fea33e | 2022-04-01 15:40:25 +0200 | [diff] [blame] | 8025 | mbedtls_ssl_mode_t ssl_mode; |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 8026 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8027 | const mbedtls_cipher_info_t *cipher_info; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8028 | const mbedtls_md_info_t *md_info; |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 8029 | #endif /* !MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8030 | |
| 8031 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 8032 | psa_key_type_t key_type; |
| 8033 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8034 | psa_algorithm_t alg; |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 8035 | psa_algorithm_t mac_alg = 0; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8036 | size_t key_bits; |
| 8037 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 8038 | #endif |
| 8039 | |
| 8040 | #if !defined(MBEDTLS_DEBUG_C) && \ |
| 8041 | !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 8042 | if( ssl->f_export_keys == NULL ) |
| 8043 | { |
| 8044 | ssl = NULL; /* make sure we don't use it except for these cases */ |
| 8045 | (void) ssl; |
| 8046 | } |
| 8047 | #endif |
| 8048 | |
| 8049 | /* |
| 8050 | * Some data just needs copying into the structure |
| 8051 | */ |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 8052 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8053 | transform->encrypt_then_mac = encrypt_then_mac; |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 8054 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 8055 | transform->tls_version = tls_version; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8056 | |
| 8057 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
| 8058 | memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) ); |
| 8059 | #endif |
| 8060 | |
| 8061 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 8062 | if( tls_version == MBEDTLS_SSL_VERSION_TLS1_3 ) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8063 | { |
| 8064 | /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform |
| 8065 | * generation separate. This should never happen. */ |
| 8066 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 8067 | } |
| 8068 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 8069 | |
| 8070 | /* |
| 8071 | * Get various info structures |
| 8072 | */ |
| 8073 | ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite ); |
| 8074 | if( ciphersuite_info == NULL ) |
| 8075 | { |
| 8076 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found", |
| 8077 | ciphersuite ) ); |
| 8078 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8079 | } |
| 8080 | |
Neil Armstrong | ab555e0 | 2022-04-04 11:07:59 +0200 | [diff] [blame] | 8081 | ssl_mode = mbedtls_ssl_get_mode_from_ciphersuite( |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 8082 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Neil Armstrong | 7fea33e | 2022-04-01 15:40:25 +0200 | [diff] [blame] | 8083 | encrypt_then_mac, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 8084 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Neil Armstrong | 7fea33e | 2022-04-01 15:40:25 +0200 | [diff] [blame] | 8085 | ciphersuite_info ); |
| 8086 | |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8087 | if( ssl_mode == MBEDTLS_SSL_MODE_AEAD ) |
| 8088 | transform->taglen = |
| 8089 | ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16; |
| 8090 | |
| 8091 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 8092 | if( ( status = mbedtls_ssl_cipher_to_psa( ciphersuite_info->cipher, |
| 8093 | transform->taglen, |
| 8094 | &alg, |
| 8095 | &key_type, |
| 8096 | &key_bits ) ) != PSA_SUCCESS ) |
| 8097 | { |
| 8098 | ret = psa_ssl_status_to_mbedtls( status ); |
| 8099 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_cipher_to_psa", ret ); |
| 8100 | goto end; |
| 8101 | } |
| 8102 | #else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8103 | cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher ); |
| 8104 | if( cipher_info == NULL ) |
| 8105 | { |
| 8106 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %u not found", |
| 8107 | ciphersuite_info->cipher ) ); |
| 8108 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8109 | } |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8110 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8111 | |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 8112 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Manuel Pégourié-Gonnard | abac037 | 2022-07-18 13:41:11 +0200 | [diff] [blame] | 8113 | mac_alg = mbedtls_hash_info_psa_from_md( ciphersuite_info->mac ); |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 8114 | if( mac_alg == 0 ) |
| 8115 | { |
Manuel Pégourié-Gonnard | abac037 | 2022-07-18 13:41:11 +0200 | [diff] [blame] | 8116 | 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] | 8117 | (unsigned) ciphersuite_info->mac ) ); |
| 8118 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8119 | } |
| 8120 | #else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8121 | md_info = mbedtls_md_info_from_type( ciphersuite_info->mac ); |
| 8122 | if( md_info == NULL ) |
| 8123 | { |
| 8124 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %u not found", |
| 8125 | (unsigned) ciphersuite_info->mac ) ); |
| 8126 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8127 | } |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 8128 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8129 | |
| 8130 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 8131 | /* Copy own and peer's CID if the use of the CID |
| 8132 | * extension has been negotiated. */ |
| 8133 | if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED ) |
| 8134 | { |
| 8135 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) ); |
| 8136 | |
| 8137 | transform->in_cid_len = ssl->own_cid_len; |
| 8138 | memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len ); |
| 8139 | MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid, |
| 8140 | transform->in_cid_len ); |
| 8141 | |
| 8142 | transform->out_cid_len = ssl->handshake->peer_cid_len; |
| 8143 | memcpy( transform->out_cid, ssl->handshake->peer_cid, |
| 8144 | ssl->handshake->peer_cid_len ); |
| 8145 | MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid, |
| 8146 | transform->out_cid_len ); |
| 8147 | } |
| 8148 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 8149 | |
| 8150 | /* |
| 8151 | * Compute key block using the PRF |
| 8152 | */ |
| 8153 | ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 ); |
| 8154 | if( ret != 0 ) |
| 8155 | { |
| 8156 | MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret ); |
| 8157 | return( ret ); |
| 8158 | } |
| 8159 | |
| 8160 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s", |
| 8161 | mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) ); |
| 8162 | MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 ); |
| 8163 | MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 ); |
| 8164 | MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 ); |
| 8165 | |
| 8166 | /* |
| 8167 | * Determine the appropriate key, IV and MAC length. |
| 8168 | */ |
| 8169 | |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8170 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 8171 | keylen = PSA_BITS_TO_BYTES(key_bits); |
| 8172 | #else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8173 | keylen = mbedtls_cipher_info_get_key_bitlen( cipher_info ) / 8; |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8174 | #endif |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8175 | |
| 8176 | #if defined(MBEDTLS_GCM_C) || \ |
| 8177 | defined(MBEDTLS_CCM_C) || \ |
| 8178 | defined(MBEDTLS_CHACHAPOLY_C) |
Neil Armstrong | 7fea33e | 2022-04-01 15:40:25 +0200 | [diff] [blame] | 8179 | if( ssl_mode == MBEDTLS_SSL_MODE_AEAD ) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8180 | { |
| 8181 | size_t explicit_ivlen; |
| 8182 | |
| 8183 | transform->maclen = 0; |
| 8184 | mac_key_len = 0; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8185 | |
| 8186 | /* All modes haves 96-bit IVs, but the length of the static parts vary |
| 8187 | * with mode and version: |
| 8188 | * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes |
| 8189 | * (to be concatenated with a dynamically chosen IV of 8 Bytes) |
| 8190 | * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's |
| 8191 | * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record |
| 8192 | * sequence number). |
| 8193 | */ |
| 8194 | transform->ivlen = 12; |
David Horstmann | 3b2276a | 2022-10-06 14:49:08 +0100 | [diff] [blame] | 8195 | |
| 8196 | int is_chachapoly = 0; |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8197 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
David Horstmann | 3b2276a | 2022-10-06 14:49:08 +0100 | [diff] [blame] | 8198 | is_chachapoly = ( key_type == PSA_KEY_TYPE_CHACHA20 ); |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8199 | #else |
David Horstmann | 3b2276a | 2022-10-06 14:49:08 +0100 | [diff] [blame] | 8200 | is_chachapoly = ( mbedtls_cipher_info_get_mode( cipher_info ) |
| 8201 | == MBEDTLS_MODE_CHACHAPOLY ); |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8202 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
David Horstmann | 3b2276a | 2022-10-06 14:49:08 +0100 | [diff] [blame] | 8203 | |
| 8204 | if( is_chachapoly ) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8205 | transform->fixed_ivlen = 12; |
| 8206 | else |
| 8207 | transform->fixed_ivlen = 4; |
| 8208 | |
| 8209 | /* Minimum length of encrypted record */ |
| 8210 | explicit_ivlen = transform->ivlen - transform->fixed_ivlen; |
| 8211 | transform->minlen = explicit_ivlen + transform->taglen; |
| 8212 | } |
| 8213 | else |
| 8214 | #endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */ |
| 8215 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
Neil Armstrong | 7fea33e | 2022-04-01 15:40:25 +0200 | [diff] [blame] | 8216 | if( ssl_mode == MBEDTLS_SSL_MODE_STREAM || |
| 8217 | ssl_mode == MBEDTLS_SSL_MODE_CBC || |
| 8218 | ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM ) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8219 | { |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 8220 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Neil Armstrong | d1be767 | 2022-04-04 11:21:41 +0200 | [diff] [blame] | 8221 | size_t block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ); |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8222 | #else |
| 8223 | size_t block_size = cipher_info->block_size; |
| 8224 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 8225 | |
| 8226 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 8227 | /* Get MAC length */ |
| 8228 | mac_key_len = PSA_HASH_LENGTH(mac_alg); |
| 8229 | #else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8230 | /* Initialize HMAC contexts */ |
| 8231 | if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 || |
| 8232 | ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 ) |
| 8233 | { |
| 8234 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret ); |
| 8235 | goto end; |
| 8236 | } |
| 8237 | |
| 8238 | /* Get MAC length */ |
| 8239 | mac_key_len = mbedtls_md_get_size( md_info ); |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 8240 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8241 | transform->maclen = mac_key_len; |
| 8242 | |
| 8243 | /* IV length */ |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8244 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Neil Armstrong | 2230e6c | 2022-04-27 10:36:14 +0200 | [diff] [blame] | 8245 | transform->ivlen = PSA_CIPHER_IV_LENGTH( key_type, alg ); |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8246 | #else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8247 | transform->ivlen = cipher_info->iv_size; |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8248 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8249 | |
| 8250 | /* Minimum length */ |
Neil Armstrong | 7fea33e | 2022-04-01 15:40:25 +0200 | [diff] [blame] | 8251 | if( ssl_mode == MBEDTLS_SSL_MODE_STREAM ) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8252 | transform->minlen = transform->maclen; |
| 8253 | else |
| 8254 | { |
| 8255 | /* |
| 8256 | * GenericBlockCipher: |
| 8257 | * 1. if EtM is in use: one block plus MAC |
| 8258 | * otherwise: * first multiple of blocklen greater than maclen |
| 8259 | * 2. IV |
| 8260 | */ |
| 8261 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Neil Armstrong | 7fea33e | 2022-04-01 15:40:25 +0200 | [diff] [blame] | 8262 | if( ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM ) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8263 | { |
| 8264 | transform->minlen = transform->maclen |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8265 | + block_size; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8266 | } |
| 8267 | else |
| 8268 | #endif |
| 8269 | { |
| 8270 | transform->minlen = transform->maclen |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8271 | + block_size |
| 8272 | - transform->maclen % block_size; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8273 | } |
| 8274 | |
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 8275 | if( tls_version == MBEDTLS_SSL_VERSION_TLS1_2 ) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8276 | { |
| 8277 | transform->minlen += transform->ivlen; |
| 8278 | } |
| 8279 | else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8280 | { |
| 8281 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 8282 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 8283 | goto end; |
| 8284 | } |
| 8285 | } |
| 8286 | } |
| 8287 | else |
| 8288 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
| 8289 | { |
| 8290 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 8291 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 8292 | } |
| 8293 | |
| 8294 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u", |
| 8295 | (unsigned) keylen, |
| 8296 | (unsigned) transform->minlen, |
| 8297 | (unsigned) transform->ivlen, |
| 8298 | (unsigned) transform->maclen ) ); |
| 8299 | |
| 8300 | /* |
| 8301 | * Finally setup the cipher contexts, IVs and MAC secrets. |
| 8302 | */ |
| 8303 | #if defined(MBEDTLS_SSL_CLI_C) |
| 8304 | if( endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 8305 | { |
| 8306 | key1 = keyblk + mac_key_len * 2; |
| 8307 | key2 = keyblk + mac_key_len * 2 + keylen; |
| 8308 | |
| 8309 | mac_enc = keyblk; |
| 8310 | mac_dec = keyblk + mac_key_len; |
| 8311 | |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8312 | iv_copy_len = ( transform->fixed_ivlen ) ? |
| 8313 | transform->fixed_ivlen : transform->ivlen; |
| 8314 | memcpy( transform->iv_enc, key2 + keylen, iv_copy_len ); |
| 8315 | memcpy( transform->iv_dec, key2 + keylen + iv_copy_len, |
| 8316 | iv_copy_len ); |
| 8317 | } |
| 8318 | else |
| 8319 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 8320 | #if defined(MBEDTLS_SSL_SRV_C) |
| 8321 | if( endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 8322 | { |
| 8323 | key1 = keyblk + mac_key_len * 2 + keylen; |
| 8324 | key2 = keyblk + mac_key_len * 2; |
| 8325 | |
| 8326 | mac_enc = keyblk + mac_key_len; |
| 8327 | mac_dec = keyblk; |
| 8328 | |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8329 | iv_copy_len = ( transform->fixed_ivlen ) ? |
| 8330 | transform->fixed_ivlen : transform->ivlen; |
| 8331 | memcpy( transform->iv_dec, key1 + keylen, iv_copy_len ); |
| 8332 | memcpy( transform->iv_enc, key1 + keylen + iv_copy_len, |
| 8333 | iv_copy_len ); |
| 8334 | } |
| 8335 | else |
| 8336 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 8337 | { |
| 8338 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 8339 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 8340 | goto end; |
| 8341 | } |
| 8342 | |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8343 | if( ssl != NULL && ssl->f_export_keys != NULL ) |
| 8344 | { |
| 8345 | ssl->f_export_keys( ssl->p_export_keys, |
| 8346 | MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET, |
| 8347 | master, 48, |
| 8348 | randbytes + 32, |
| 8349 | randbytes, |
| 8350 | tls_prf_get_type( tls_prf ) ); |
| 8351 | } |
| 8352 | |
| 8353 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8354 | transform->psa_alg = alg; |
| 8355 | |
| 8356 | if ( alg != MBEDTLS_SSL_NULL_CIPHER ) |
| 8357 | { |
| 8358 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 8359 | psa_set_key_algorithm( &attributes, alg ); |
| 8360 | psa_set_key_type( &attributes, key_type ); |
| 8361 | |
| 8362 | if( ( status = psa_import_key( &attributes, |
| 8363 | key1, |
| 8364 | PSA_BITS_TO_BYTES( key_bits ), |
| 8365 | &transform->psa_key_enc ) ) != PSA_SUCCESS ) |
| 8366 | { |
| 8367 | MBEDTLS_SSL_DEBUG_RET( 3, "psa_import_key", (int)status ); |
| 8368 | ret = psa_ssl_status_to_mbedtls( status ); |
| 8369 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret ); |
| 8370 | goto end; |
| 8371 | } |
| 8372 | |
| 8373 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 8374 | |
| 8375 | if( ( status = psa_import_key( &attributes, |
| 8376 | key2, |
| 8377 | PSA_BITS_TO_BYTES( key_bits ), |
| 8378 | &transform->psa_key_dec ) ) != PSA_SUCCESS ) |
| 8379 | { |
| 8380 | ret = psa_ssl_status_to_mbedtls( status ); |
| 8381 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret ); |
| 8382 | goto end; |
| 8383 | } |
| 8384 | } |
| 8385 | #else |
| 8386 | if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc, |
| 8387 | cipher_info ) ) != 0 ) |
| 8388 | { |
| 8389 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret ); |
| 8390 | goto end; |
| 8391 | } |
| 8392 | |
| 8393 | if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec, |
| 8394 | cipher_info ) ) != 0 ) |
| 8395 | { |
| 8396 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret ); |
| 8397 | goto end; |
| 8398 | } |
| 8399 | |
| 8400 | if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1, |
| 8401 | (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ), |
| 8402 | MBEDTLS_ENCRYPT ) ) != 0 ) |
| 8403 | { |
| 8404 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret ); |
| 8405 | goto end; |
| 8406 | } |
| 8407 | |
| 8408 | if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2, |
| 8409 | (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ), |
| 8410 | MBEDTLS_DECRYPT ) ) != 0 ) |
| 8411 | { |
| 8412 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret ); |
| 8413 | goto end; |
| 8414 | } |
| 8415 | |
| 8416 | #if defined(MBEDTLS_CIPHER_MODE_CBC) |
| 8417 | if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CBC ) |
| 8418 | { |
| 8419 | if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc, |
| 8420 | MBEDTLS_PADDING_NONE ) ) != 0 ) |
| 8421 | { |
| 8422 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret ); |
| 8423 | goto end; |
| 8424 | } |
| 8425 | |
| 8426 | if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec, |
| 8427 | MBEDTLS_PADDING_NONE ) ) != 0 ) |
| 8428 | { |
| 8429 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret ); |
| 8430 | goto end; |
| 8431 | } |
| 8432 | } |
| 8433 | #endif /* MBEDTLS_CIPHER_MODE_CBC */ |
| 8434 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 8435 | |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 8436 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
| 8437 | /* For HMAC-based ciphersuites, initialize the HMAC transforms. |
| 8438 | For AEAD-based ciphersuites, there is nothing to do here. */ |
| 8439 | if( mac_key_len != 0 ) |
| 8440 | { |
| 8441 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 8442 | transform->psa_mac_alg = PSA_ALG_HMAC( mac_alg ); |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 8443 | |
| 8444 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE ); |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 8445 | psa_set_key_algorithm( &attributes, PSA_ALG_HMAC( mac_alg ) ); |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 8446 | psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC ); |
| 8447 | |
| 8448 | if( ( status = psa_import_key( &attributes, |
| 8449 | mac_enc, mac_key_len, |
| 8450 | &transform->psa_mac_enc ) ) != PSA_SUCCESS ) |
| 8451 | { |
| 8452 | ret = psa_ssl_status_to_mbedtls( status ); |
| 8453 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret ); |
| 8454 | goto end; |
| 8455 | } |
| 8456 | |
Ronald Cron | fb39f15 | 2022-03-25 14:36:28 +0100 | [diff] [blame] | 8457 | if( ( transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER ) || |
Andrzej Kurek | 8c95ac4 | 2022-08-17 16:17:00 -0400 | [diff] [blame] | 8458 | ( ( transform->psa_alg == PSA_ALG_CBC_NO_PADDING ) |
| 8459 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
| 8460 | && ( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED ) |
| 8461 | #endif |
| 8462 | ) ) |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 8463 | /* mbedtls_ct_hmac() requires the key to be exportable */ |
| 8464 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT | |
| 8465 | PSA_KEY_USAGE_VERIFY_HASH ); |
| 8466 | else |
| 8467 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH ); |
| 8468 | |
| 8469 | if( ( status = psa_import_key( &attributes, |
| 8470 | mac_dec, mac_key_len, |
| 8471 | &transform->psa_mac_dec ) ) != PSA_SUCCESS ) |
| 8472 | { |
| 8473 | ret = psa_ssl_status_to_mbedtls( status ); |
| 8474 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret ); |
| 8475 | goto end; |
| 8476 | } |
| 8477 | #else |
| 8478 | ret = mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len ); |
| 8479 | if( ret != 0 ) |
| 8480 | goto end; |
| 8481 | ret = mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len ); |
| 8482 | if( ret != 0 ) |
| 8483 | goto end; |
| 8484 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 8485 | } |
| 8486 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
| 8487 | |
| 8488 | ((void) mac_dec); |
| 8489 | ((void) mac_enc); |
| 8490 | |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8491 | end: |
| 8492 | mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) ); |
| 8493 | return( ret ); |
| 8494 | } |
| 8495 | |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 8496 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) && \ |
| 8497 | defined(MBEDTLS_USE_PSA_CRYPTO) |
Valerio Setti | 6b3dab0 | 2022-11-17 17:14:54 +0100 | [diff] [blame] | 8498 | int mbedtls_psa_ecjpake_read_round( |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 8499 | psa_pake_operation_t *pake_ctx, |
| 8500 | const unsigned char *buf, |
Valerio Setti | 6b3dab0 | 2022-11-17 17:14:54 +0100 | [diff] [blame] | 8501 | size_t len, mbedtls_ecjpake_rounds_t round ) |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 8502 | { |
| 8503 | psa_status_t status; |
| 8504 | size_t input_offset = 0; |
Valerio Setti | 819de86 | 2022-11-17 18:05:19 +0100 | [diff] [blame] | 8505 | /* |
Valerio Setti | 6b3dab0 | 2022-11-17 17:14:54 +0100 | [diff] [blame] | 8506 | * At round one repeat the KEY_SHARE, ZK_PUBLIC & ZF_PROOF twice |
| 8507 | * At round two perform a single cycle |
| 8508 | */ |
| 8509 | unsigned int remaining_steps = ( round == MBEDTLS_ECJPAKE_ROUND_ONE) ? 2 : 1; |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 8510 | |
Valerio Setti | 6b3dab0 | 2022-11-17 17:14:54 +0100 | [diff] [blame] | 8511 | for( ; remaining_steps > 0; remaining_steps-- ) |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 8512 | { |
| 8513 | for( psa_pake_step_t step = PSA_PAKE_STEP_KEY_SHARE; |
| 8514 | step <= PSA_PAKE_STEP_ZK_PROOF; |
| 8515 | ++step ) |
| 8516 | { |
| 8517 | /* Length is stored at the first byte */ |
| 8518 | size_t length = buf[input_offset]; |
| 8519 | input_offset += 1; |
| 8520 | |
| 8521 | if( input_offset + length > len ) |
| 8522 | { |
| 8523 | return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE; |
| 8524 | } |
| 8525 | |
| 8526 | status = psa_pake_input( pake_ctx, step, |
| 8527 | buf + input_offset, length ); |
| 8528 | if( status != PSA_SUCCESS) |
| 8529 | { |
| 8530 | return psa_ssl_status_to_mbedtls( status ); |
| 8531 | } |
| 8532 | |
| 8533 | input_offset += length; |
| 8534 | } |
| 8535 | } |
| 8536 | |
Valerio Setti | 819de86 | 2022-11-17 18:05:19 +0100 | [diff] [blame] | 8537 | if( input_offset != len ) |
Valerio Setti | 61ea17d | 2022-11-18 12:11:00 +0100 | [diff] [blame] | 8538 | return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE; |
Valerio Setti | 30ebe11 | 2022-11-17 16:23:34 +0100 | [diff] [blame] | 8539 | |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 8540 | return( 0 ); |
| 8541 | } |
| 8542 | |
Valerio Setti | 6b3dab0 | 2022-11-17 17:14:54 +0100 | [diff] [blame] | 8543 | int mbedtls_psa_ecjpake_write_round( |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 8544 | psa_pake_operation_t *pake_ctx, |
| 8545 | unsigned char *buf, |
Valerio Setti | 6b3dab0 | 2022-11-17 17:14:54 +0100 | [diff] [blame] | 8546 | size_t len, size_t *olen, |
| 8547 | mbedtls_ecjpake_rounds_t round ) |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 8548 | { |
| 8549 | psa_status_t status; |
| 8550 | size_t output_offset = 0; |
| 8551 | size_t output_len; |
Valerio Setti | 819de86 | 2022-11-17 18:05:19 +0100 | [diff] [blame] | 8552 | /* |
Valerio Setti | 6b3dab0 | 2022-11-17 17:14:54 +0100 | [diff] [blame] | 8553 | * At round one repeat the KEY_SHARE, ZK_PUBLIC & ZF_PROOF twice |
| 8554 | * At round two perform a single cycle |
| 8555 | */ |
| 8556 | unsigned int remaining_steps = ( round == MBEDTLS_ECJPAKE_ROUND_ONE) ? 2 : 1; |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 8557 | |
Valerio Setti | 6b3dab0 | 2022-11-17 17:14:54 +0100 | [diff] [blame] | 8558 | for( ; remaining_steps > 0; remaining_steps-- ) |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 8559 | { |
Valerio Setti | 6b3dab0 | 2022-11-17 17:14:54 +0100 | [diff] [blame] | 8560 | for( psa_pake_step_t step = PSA_PAKE_STEP_KEY_SHARE; |
| 8561 | step <= PSA_PAKE_STEP_ZK_PROOF; |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 8562 | ++step ) |
| 8563 | { |
Valerio Setti | 79f6b6b | 2022-11-21 14:17:03 +0100 | [diff] [blame] | 8564 | /* |
Valerio Setti | d4a9b1a | 2022-11-22 11:11:10 +0100 | [diff] [blame] | 8565 | * For each step, prepend 1 byte with the length of the data as |
| 8566 | * given by psa_pake_output(). |
Valerio Setti | 79f6b6b | 2022-11-21 14:17:03 +0100 | [diff] [blame] | 8567 | */ |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 8568 | status = psa_pake_output( pake_ctx, step, |
Valerio Setti | 79f6b6b | 2022-11-21 14:17:03 +0100 | [diff] [blame] | 8569 | buf + output_offset + 1, |
| 8570 | len - output_offset - 1, |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 8571 | &output_len ); |
| 8572 | if( status != PSA_SUCCESS ) |
| 8573 | { |
| 8574 | return( psa_ssl_status_to_mbedtls( status ) ); |
| 8575 | } |
| 8576 | |
Valerio Setti | 99d88c1 | 2022-11-22 16:03:43 +0100 | [diff] [blame] | 8577 | *(buf + output_offset) = (uint8_t) output_len; |
Valerio Setti | 79f6b6b | 2022-11-21 14:17:03 +0100 | [diff] [blame] | 8578 | |
| 8579 | output_offset += output_len + 1; |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 8580 | } |
| 8581 | } |
| 8582 | |
| 8583 | *olen = output_offset; |
| 8584 | |
| 8585 | return( 0 ); |
| 8586 | } |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 8587 | #endif //MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED && MBEDTLS_USE_PSA_CRYPTO |
| 8588 | |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 8589 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 8590 | int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl, |
| 8591 | unsigned char *hash, size_t *hashlen, |
| 8592 | unsigned char *data, size_t data_len, |
| 8593 | mbedtls_md_type_t md_alg ) |
| 8594 | { |
| 8595 | psa_status_t status; |
| 8596 | psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT; |
Manuel Pégourié-Gonnard | abac037 | 2022-07-18 13:41:11 +0200 | [diff] [blame] | 8597 | 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] | 8598 | |
| 8599 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) ); |
| 8600 | |
| 8601 | if( ( status = psa_hash_setup( &hash_operation, |
| 8602 | hash_alg ) ) != PSA_SUCCESS ) |
| 8603 | { |
| 8604 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status ); |
| 8605 | goto exit; |
| 8606 | } |
| 8607 | |
| 8608 | if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes, |
| 8609 | 64 ) ) != PSA_SUCCESS ) |
| 8610 | { |
| 8611 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status ); |
| 8612 | goto exit; |
| 8613 | } |
| 8614 | |
| 8615 | if( ( status = psa_hash_update( &hash_operation, |
| 8616 | data, data_len ) ) != PSA_SUCCESS ) |
| 8617 | { |
| 8618 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status ); |
| 8619 | goto exit; |
| 8620 | } |
| 8621 | |
| 8622 | if( ( status = psa_hash_finish( &hash_operation, hash, PSA_HASH_MAX_SIZE, |
| 8623 | hashlen ) ) != PSA_SUCCESS ) |
| 8624 | { |
| 8625 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status ); |
| 8626 | goto exit; |
| 8627 | } |
| 8628 | |
| 8629 | exit: |
| 8630 | if( status != PSA_SUCCESS ) |
| 8631 | { |
| 8632 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 8633 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
| 8634 | switch( status ) |
| 8635 | { |
| 8636 | case PSA_ERROR_NOT_SUPPORTED: |
| 8637 | return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE ); |
| 8638 | case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */ |
| 8639 | case PSA_ERROR_BUFFER_TOO_SMALL: |
| 8640 | return( MBEDTLS_ERR_MD_BAD_INPUT_DATA ); |
| 8641 | case PSA_ERROR_INSUFFICIENT_MEMORY: |
| 8642 | return( MBEDTLS_ERR_MD_ALLOC_FAILED ); |
| 8643 | default: |
| 8644 | return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED ); |
| 8645 | } |
| 8646 | } |
| 8647 | return( 0 ); |
| 8648 | } |
| 8649 | |
| 8650 | #else |
| 8651 | |
| 8652 | int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl, |
| 8653 | unsigned char *hash, size_t *hashlen, |
| 8654 | unsigned char *data, size_t data_len, |
| 8655 | mbedtls_md_type_t md_alg ) |
| 8656 | { |
| 8657 | int ret = 0; |
| 8658 | mbedtls_md_context_t ctx; |
| 8659 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg ); |
| 8660 | *hashlen = mbedtls_md_get_size( md_info ); |
| 8661 | |
| 8662 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) ); |
| 8663 | |
| 8664 | mbedtls_md_init( &ctx ); |
| 8665 | |
| 8666 | /* |
| 8667 | * digitally-signed struct { |
| 8668 | * opaque client_random[32]; |
| 8669 | * opaque server_random[32]; |
| 8670 | * ServerDHParams params; |
| 8671 | * }; |
| 8672 | */ |
| 8673 | if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 ) |
| 8674 | { |
| 8675 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret ); |
| 8676 | goto exit; |
| 8677 | } |
| 8678 | if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 ) |
| 8679 | { |
| 8680 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret ); |
| 8681 | goto exit; |
| 8682 | } |
| 8683 | if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 ) |
| 8684 | { |
| 8685 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret ); |
| 8686 | goto exit; |
| 8687 | } |
| 8688 | if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 ) |
| 8689 | { |
| 8690 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret ); |
| 8691 | goto exit; |
| 8692 | } |
| 8693 | if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 ) |
| 8694 | { |
| 8695 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret ); |
| 8696 | goto exit; |
| 8697 | } |
| 8698 | |
| 8699 | exit: |
| 8700 | mbedtls_md_free( &ctx ); |
| 8701 | |
| 8702 | if( ret != 0 ) |
| 8703 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 8704 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
| 8705 | |
| 8706 | return( ret ); |
| 8707 | } |
| 8708 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 8709 | |
Jerry Yu | d9d91da | 2022-02-17 14:57:06 +0800 | [diff] [blame] | 8710 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
| 8711 | |
Gabor Mezei | a3d016c | 2022-05-10 12:44:09 +0200 | [diff] [blame] | 8712 | /* Find the preferred hash for a given signature algorithm. */ |
| 8713 | unsigned int mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg( |
| 8714 | mbedtls_ssl_context *ssl, |
| 8715 | unsigned int sig_alg ) |
Jerry Yu | d9d91da | 2022-02-17 14:57:06 +0800 | [diff] [blame] | 8716 | { |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 8717 | unsigned int i; |
Gabor Mezei | a3d016c | 2022-05-10 12:44:09 +0200 | [diff] [blame] | 8718 | uint16_t *received_sig_algs = ssl->handshake->received_sig_algs; |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 8719 | |
| 8720 | if( sig_alg == MBEDTLS_SSL_SIG_ANON ) |
Gabor Mezei | a3d016c | 2022-05-10 12:44:09 +0200 | [diff] [blame] | 8721 | return( MBEDTLS_SSL_HASH_NONE ); |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 8722 | |
Gabor Mezei | a3d016c | 2022-05-10 12:44:09 +0200 | [diff] [blame] | 8723 | for( i = 0; received_sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ ) |
Jerry Yu | d9d91da | 2022-02-17 14:57:06 +0800 | [diff] [blame] | 8724 | { |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 8725 | unsigned int hash_alg_received = |
| 8726 | MBEDTLS_SSL_TLS12_HASH_ALG_FROM_SIG_AND_HASH_ALG( |
| 8727 | received_sig_algs[i] ); |
| 8728 | unsigned int sig_alg_received = |
| 8729 | MBEDTLS_SSL_TLS12_SIG_ALG_FROM_SIG_AND_HASH_ALG( |
| 8730 | received_sig_algs[i] ); |
| 8731 | |
| 8732 | if( sig_alg == sig_alg_received ) |
| 8733 | { |
| 8734 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 8735 | if( ssl->handshake->key_cert && ssl->handshake->key_cert->key ) |
| 8736 | { |
Neil Armstrong | 96eceb8 | 2022-06-30 18:05:05 +0200 | [diff] [blame] | 8737 | psa_algorithm_t psa_hash_alg = |
Manuel Pégourié-Gonnard | abac037 | 2022-07-18 13:41:11 +0200 | [diff] [blame] | 8738 | mbedtls_hash_info_psa_from_md( hash_alg_received ); |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 8739 | |
Neil Armstrong | 96eceb8 | 2022-06-30 18:05:05 +0200 | [diff] [blame] | 8740 | if( sig_alg_received == MBEDTLS_SSL_SIG_ECDSA && |
| 8741 | ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key, |
| 8742 | PSA_ALG_ECDSA( psa_hash_alg ), |
| 8743 | PSA_KEY_USAGE_SIGN_HASH ) ) |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 8744 | continue; |
| 8745 | |
Neil Armstrong | 96eceb8 | 2022-06-30 18:05:05 +0200 | [diff] [blame] | 8746 | if( sig_alg_received == MBEDTLS_SSL_SIG_RSA && |
Neil Armstrong | 971f30d | 2022-07-01 16:23:50 +0200 | [diff] [blame] | 8747 | ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key, |
| 8748 | PSA_ALG_RSA_PKCS1V15_SIGN( |
| 8749 | psa_hash_alg ), |
| 8750 | PSA_KEY_USAGE_SIGN_HASH ) ) |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 8751 | continue; |
| 8752 | } |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 8753 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Neil Armstrong | 96eceb8 | 2022-06-30 18:05:05 +0200 | [diff] [blame] | 8754 | |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 8755 | return( hash_alg_received ); |
| 8756 | } |
Jerry Yu | d9d91da | 2022-02-17 14:57:06 +0800 | [diff] [blame] | 8757 | } |
Jerry Yu | d9d91da | 2022-02-17 14:57:06 +0800 | [diff] [blame] | 8758 | |
Gabor Mezei | a3d016c | 2022-05-10 12:44:09 +0200 | [diff] [blame] | 8759 | return( MBEDTLS_SSL_HASH_NONE ); |
Jerry Yu | d9d91da | 2022-02-17 14:57:06 +0800 | [diff] [blame] | 8760 | } |
| 8761 | |
| 8762 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 8763 | |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8764 | /* Serialization of TLS 1.2 sessions: |
| 8765 | * |
| 8766 | * struct { |
| 8767 | * uint64 start_time; |
| 8768 | * uint8 ciphersuite[2]; // defined by the standard |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8769 | * uint8 session_id_len; // at most 32 |
| 8770 | * opaque session_id[32]; |
| 8771 | * opaque master[48]; // fixed length in the standard |
| 8772 | * uint32 verify_result; |
| 8773 | * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert |
| 8774 | * opaque ticket<0..2^24-1>; // length 0 means no ticket |
| 8775 | * uint32 ticket_lifetime; |
| 8776 | * uint8 mfl_code; // up to 255 according to standard |
| 8777 | * uint8 encrypt_then_mac; // 0 or 1 |
| 8778 | * } serialized_session_tls12; |
| 8779 | * |
| 8780 | */ |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 8781 | static size_t ssl_tls12_session_save( const mbedtls_ssl_session *session, |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8782 | unsigned char *buf, |
| 8783 | size_t buf_len ) |
| 8784 | { |
| 8785 | unsigned char *p = buf; |
| 8786 | size_t used = 0; |
| 8787 | |
| 8788 | #if defined(MBEDTLS_HAVE_TIME) |
| 8789 | uint64_t start; |
| 8790 | #endif |
| 8791 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 8792 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 8793 | size_t cert_len; |
| 8794 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 8795 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 8796 | |
| 8797 | /* |
| 8798 | * Time |
| 8799 | */ |
| 8800 | #if defined(MBEDTLS_HAVE_TIME) |
| 8801 | used += 8; |
| 8802 | |
| 8803 | if( used <= buf_len ) |
| 8804 | { |
| 8805 | start = (uint64_t) session->start; |
| 8806 | |
| 8807 | MBEDTLS_PUT_UINT64_BE( start, p, 0 ); |
| 8808 | p += 8; |
| 8809 | } |
| 8810 | #endif /* MBEDTLS_HAVE_TIME */ |
| 8811 | |
| 8812 | /* |
| 8813 | * Basic mandatory fields |
| 8814 | */ |
| 8815 | used += 2 /* ciphersuite */ |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8816 | + 1 /* id_len */ |
| 8817 | + sizeof( session->id ) |
| 8818 | + sizeof( session->master ) |
| 8819 | + 4; /* verify_result */ |
| 8820 | |
| 8821 | if( used <= buf_len ) |
| 8822 | { |
| 8823 | MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 0 ); |
| 8824 | p += 2; |
| 8825 | |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8826 | *p++ = MBEDTLS_BYTE_0( session->id_len ); |
| 8827 | memcpy( p, session->id, 32 ); |
| 8828 | p += 32; |
| 8829 | |
| 8830 | memcpy( p, session->master, 48 ); |
| 8831 | p += 48; |
| 8832 | |
| 8833 | MBEDTLS_PUT_UINT32_BE( session->verify_result, p, 0 ); |
| 8834 | p += 4; |
| 8835 | } |
| 8836 | |
| 8837 | /* |
| 8838 | * Peer's end-entity certificate |
| 8839 | */ |
| 8840 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 8841 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 8842 | if( session->peer_cert == NULL ) |
| 8843 | cert_len = 0; |
| 8844 | else |
| 8845 | cert_len = session->peer_cert->raw.len; |
| 8846 | |
| 8847 | used += 3 + cert_len; |
| 8848 | |
| 8849 | if( used <= buf_len ) |
| 8850 | { |
| 8851 | *p++ = MBEDTLS_BYTE_2( cert_len ); |
| 8852 | *p++ = MBEDTLS_BYTE_1( cert_len ); |
| 8853 | *p++ = MBEDTLS_BYTE_0( cert_len ); |
| 8854 | |
| 8855 | if( session->peer_cert != NULL ) |
| 8856 | { |
| 8857 | memcpy( p, session->peer_cert->raw.p, cert_len ); |
| 8858 | p += cert_len; |
| 8859 | } |
| 8860 | } |
| 8861 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 8862 | if( session->peer_cert_digest != NULL ) |
| 8863 | { |
| 8864 | used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len; |
| 8865 | if( used <= buf_len ) |
| 8866 | { |
| 8867 | *p++ = (unsigned char) session->peer_cert_digest_type; |
| 8868 | *p++ = (unsigned char) session->peer_cert_digest_len; |
| 8869 | memcpy( p, session->peer_cert_digest, |
| 8870 | session->peer_cert_digest_len ); |
| 8871 | p += session->peer_cert_digest_len; |
| 8872 | } |
| 8873 | } |
| 8874 | else |
| 8875 | { |
| 8876 | used += 2; |
| 8877 | if( used <= buf_len ) |
| 8878 | { |
| 8879 | *p++ = (unsigned char) MBEDTLS_MD_NONE; |
| 8880 | *p++ = 0; |
| 8881 | } |
| 8882 | } |
| 8883 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 8884 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 8885 | |
| 8886 | /* |
| 8887 | * Session ticket if any, plus associated data |
| 8888 | */ |
| 8889 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
| 8890 | used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */ |
| 8891 | |
| 8892 | if( used <= buf_len ) |
| 8893 | { |
| 8894 | *p++ = MBEDTLS_BYTE_2( session->ticket_len ); |
| 8895 | *p++ = MBEDTLS_BYTE_1( session->ticket_len ); |
| 8896 | *p++ = MBEDTLS_BYTE_0( session->ticket_len ); |
| 8897 | |
| 8898 | if( session->ticket != NULL ) |
| 8899 | { |
| 8900 | memcpy( p, session->ticket, session->ticket_len ); |
| 8901 | p += session->ticket_len; |
| 8902 | } |
| 8903 | |
| 8904 | MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 ); |
| 8905 | p += 4; |
| 8906 | } |
| 8907 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
| 8908 | |
| 8909 | /* |
| 8910 | * Misc extension-related info |
| 8911 | */ |
| 8912 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 8913 | used += 1; |
| 8914 | |
| 8915 | if( used <= buf_len ) |
| 8916 | *p++ = session->mfl_code; |
| 8917 | #endif |
| 8918 | |
| 8919 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 8920 | used += 1; |
| 8921 | |
| 8922 | if( used <= buf_len ) |
| 8923 | *p++ = MBEDTLS_BYTE_0( session->encrypt_then_mac ); |
| 8924 | #endif |
| 8925 | |
| 8926 | return( used ); |
| 8927 | } |
| 8928 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 8929 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 8930 | static int ssl_tls12_session_load( mbedtls_ssl_session *session, |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8931 | const unsigned char *buf, |
| 8932 | size_t len ) |
| 8933 | { |
| 8934 | #if defined(MBEDTLS_HAVE_TIME) |
| 8935 | uint64_t start; |
| 8936 | #endif |
| 8937 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 8938 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 8939 | size_t cert_len; |
| 8940 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 8941 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 8942 | |
| 8943 | const unsigned char *p = buf; |
| 8944 | const unsigned char * const end = buf + len; |
| 8945 | |
| 8946 | /* |
| 8947 | * Time |
| 8948 | */ |
| 8949 | #if defined(MBEDTLS_HAVE_TIME) |
| 8950 | if( 8 > (size_t)( end - p ) ) |
| 8951 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8952 | |
| 8953 | start = ( (uint64_t) p[0] << 56 ) | |
| 8954 | ( (uint64_t) p[1] << 48 ) | |
| 8955 | ( (uint64_t) p[2] << 40 ) | |
| 8956 | ( (uint64_t) p[3] << 32 ) | |
| 8957 | ( (uint64_t) p[4] << 24 ) | |
| 8958 | ( (uint64_t) p[5] << 16 ) | |
| 8959 | ( (uint64_t) p[6] << 8 ) | |
| 8960 | ( (uint64_t) p[7] ); |
| 8961 | p += 8; |
| 8962 | |
| 8963 | session->start = (time_t) start; |
| 8964 | #endif /* MBEDTLS_HAVE_TIME */ |
| 8965 | |
| 8966 | /* |
| 8967 | * Basic mandatory fields |
| 8968 | */ |
Thomas Daubney | 20f89a9 | 2022-06-20 15:12:19 +0100 | [diff] [blame] | 8969 | if( 2 + 1 + 32 + 48 + 4 > (size_t)( end - p ) ) |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8970 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8971 | |
| 8972 | session->ciphersuite = ( p[0] << 8 ) | p[1]; |
| 8973 | p += 2; |
| 8974 | |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8975 | session->id_len = *p++; |
| 8976 | memcpy( session->id, p, 32 ); |
| 8977 | p += 32; |
| 8978 | |
| 8979 | memcpy( session->master, p, 48 ); |
| 8980 | p += 48; |
| 8981 | |
| 8982 | session->verify_result = ( (uint32_t) p[0] << 24 ) | |
| 8983 | ( (uint32_t) p[1] << 16 ) | |
| 8984 | ( (uint32_t) p[2] << 8 ) | |
| 8985 | ( (uint32_t) p[3] ); |
| 8986 | p += 4; |
| 8987 | |
| 8988 | /* Immediately clear invalid pointer values that have been read, in case |
| 8989 | * we exit early before we replaced them with valid ones. */ |
| 8990 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 8991 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 8992 | session->peer_cert = NULL; |
| 8993 | #else |
| 8994 | session->peer_cert_digest = NULL; |
| 8995 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 8996 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 8997 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
| 8998 | session->ticket = NULL; |
| 8999 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
| 9000 | |
| 9001 | /* |
| 9002 | * Peer certificate |
| 9003 | */ |
| 9004 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 9005 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 9006 | /* Deserialize CRT from the end of the ticket. */ |
| 9007 | if( 3 > (size_t)( end - p ) ) |
| 9008 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 9009 | |
| 9010 | cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2]; |
| 9011 | p += 3; |
| 9012 | |
| 9013 | if( cert_len != 0 ) |
| 9014 | { |
| 9015 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 9016 | |
| 9017 | if( cert_len > (size_t)( end - p ) ) |
| 9018 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 9019 | |
| 9020 | session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ); |
| 9021 | |
| 9022 | if( session->peer_cert == NULL ) |
| 9023 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 9024 | |
| 9025 | mbedtls_x509_crt_init( session->peer_cert ); |
| 9026 | |
| 9027 | if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert, |
| 9028 | p, cert_len ) ) != 0 ) |
| 9029 | { |
| 9030 | mbedtls_x509_crt_free( session->peer_cert ); |
| 9031 | mbedtls_free( session->peer_cert ); |
| 9032 | session->peer_cert = NULL; |
| 9033 | return( ret ); |
| 9034 | } |
| 9035 | |
| 9036 | p += cert_len; |
| 9037 | } |
| 9038 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 9039 | /* Deserialize CRT digest from the end of the ticket. */ |
| 9040 | if( 2 > (size_t)( end - p ) ) |
| 9041 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 9042 | |
| 9043 | session->peer_cert_digest_type = (mbedtls_md_type_t) *p++; |
| 9044 | session->peer_cert_digest_len = (size_t) *p++; |
| 9045 | |
| 9046 | if( session->peer_cert_digest_len != 0 ) |
| 9047 | { |
| 9048 | const mbedtls_md_info_t *md_info = |
| 9049 | mbedtls_md_info_from_type( session->peer_cert_digest_type ); |
| 9050 | if( md_info == NULL ) |
| 9051 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 9052 | if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) ) |
| 9053 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 9054 | |
| 9055 | if( session->peer_cert_digest_len > (size_t)( end - p ) ) |
| 9056 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 9057 | |
| 9058 | session->peer_cert_digest = |
| 9059 | mbedtls_calloc( 1, session->peer_cert_digest_len ); |
| 9060 | if( session->peer_cert_digest == NULL ) |
| 9061 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 9062 | |
| 9063 | memcpy( session->peer_cert_digest, p, |
| 9064 | session->peer_cert_digest_len ); |
| 9065 | p += session->peer_cert_digest_len; |
| 9066 | } |
| 9067 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 9068 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 9069 | |
| 9070 | /* |
| 9071 | * Session ticket and associated data |
| 9072 | */ |
| 9073 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
| 9074 | if( 3 > (size_t)( end - p ) ) |
| 9075 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 9076 | |
| 9077 | session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2]; |
| 9078 | p += 3; |
| 9079 | |
| 9080 | if( session->ticket_len != 0 ) |
| 9081 | { |
| 9082 | if( session->ticket_len > (size_t)( end - p ) ) |
| 9083 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 9084 | |
| 9085 | session->ticket = mbedtls_calloc( 1, session->ticket_len ); |
| 9086 | if( session->ticket == NULL ) |
| 9087 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 9088 | |
| 9089 | memcpy( session->ticket, p, session->ticket_len ); |
| 9090 | p += session->ticket_len; |
| 9091 | } |
| 9092 | |
| 9093 | if( 4 > (size_t)( end - p ) ) |
| 9094 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 9095 | |
| 9096 | session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) | |
| 9097 | ( (uint32_t) p[1] << 16 ) | |
| 9098 | ( (uint32_t) p[2] << 8 ) | |
| 9099 | ( (uint32_t) p[3] ); |
| 9100 | p += 4; |
| 9101 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
| 9102 | |
| 9103 | /* |
| 9104 | * Misc extension-related info |
| 9105 | */ |
| 9106 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 9107 | if( 1 > (size_t)( end - p ) ) |
| 9108 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 9109 | |
| 9110 | session->mfl_code = *p++; |
| 9111 | #endif |
| 9112 | |
| 9113 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 9114 | if( 1 > (size_t)( end - p ) ) |
| 9115 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 9116 | |
| 9117 | session->encrypt_then_mac = *p++; |
| 9118 | #endif |
| 9119 | |
| 9120 | /* Done, should have consumed entire buffer */ |
| 9121 | if( p != end ) |
| 9122 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 9123 | |
| 9124 | return( 0 ); |
| 9125 | } |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 9126 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 9127 | |
XiaokangQian | 75d40ef | 2022-04-20 11:05:24 +0000 | [diff] [blame] | 9128 | int mbedtls_ssl_validate_ciphersuite( |
| 9129 | const mbedtls_ssl_context *ssl, |
| 9130 | const mbedtls_ssl_ciphersuite_t *suite_info, |
| 9131 | mbedtls_ssl_protocol_version min_tls_version, |
| 9132 | mbedtls_ssl_protocol_version max_tls_version ) |
| 9133 | { |
| 9134 | (void) ssl; |
| 9135 | |
| 9136 | if( suite_info == NULL ) |
| 9137 | return( -1 ); |
| 9138 | |
| 9139 | if( ( suite_info->min_tls_version > max_tls_version ) || |
| 9140 | ( suite_info->max_tls_version < min_tls_version ) ) |
| 9141 | { |
| 9142 | return( -1 ); |
| 9143 | } |
| 9144 | |
XiaokangQian | 060d867 | 2022-04-21 09:24:56 +0000 | [diff] [blame] | 9145 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_CLI_C) |
XiaokangQian | 75d40ef | 2022-04-20 11:05:24 +0000 | [diff] [blame] | 9146 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 9147 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 9148 | if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE && |
| 9149 | ssl->handshake->psa_pake_ctx_is_ok != 1 ) |
| 9150 | #else |
XiaokangQian | 75d40ef | 2022-04-20 11:05:24 +0000 | [diff] [blame] | 9151 | if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE && |
| 9152 | mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 ) |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 9153 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
XiaokangQian | 75d40ef | 2022-04-20 11:05:24 +0000 | [diff] [blame] | 9154 | { |
| 9155 | return( -1 ); |
| 9156 | } |
| 9157 | #endif |
| 9158 | |
| 9159 | /* Don't suggest PSK-based ciphersuite if no PSK is available. */ |
| 9160 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
| 9161 | if( mbedtls_ssl_ciphersuite_uses_psk( suite_info ) && |
| 9162 | mbedtls_ssl_conf_has_static_psk( ssl->conf ) == 0 ) |
| 9163 | { |
| 9164 | return( -1 ); |
| 9165 | } |
| 9166 | #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ |
| 9167 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 9168 | |
| 9169 | return( 0 ); |
| 9170 | } |
| 9171 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 9172 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9173 | /* |
| 9174 | * Function for writing a signature algorithm extension. |
| 9175 | * |
| 9176 | * The `extension_data` field of signature algorithm contains a `SignatureSchemeList` |
| 9177 | * value (TLS 1.3 RFC8446): |
| 9178 | * enum { |
| 9179 | * .... |
| 9180 | * ecdsa_secp256r1_sha256( 0x0403 ), |
| 9181 | * ecdsa_secp384r1_sha384( 0x0503 ), |
| 9182 | * ecdsa_secp521r1_sha512( 0x0603 ), |
| 9183 | * .... |
| 9184 | * } SignatureScheme; |
| 9185 | * |
| 9186 | * struct { |
| 9187 | * SignatureScheme supported_signature_algorithms<2..2^16-2>; |
| 9188 | * } SignatureSchemeList; |
| 9189 | * |
| 9190 | * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm` |
| 9191 | * value (TLS 1.2 RFC5246): |
| 9192 | * enum { |
| 9193 | * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5), |
| 9194 | * sha512(6), (255) |
| 9195 | * } HashAlgorithm; |
| 9196 | * |
| 9197 | * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) } |
| 9198 | * SignatureAlgorithm; |
| 9199 | * |
| 9200 | * struct { |
| 9201 | * HashAlgorithm hash; |
| 9202 | * SignatureAlgorithm signature; |
| 9203 | * } SignatureAndHashAlgorithm; |
| 9204 | * |
| 9205 | * SignatureAndHashAlgorithm |
| 9206 | * supported_signature_algorithms<2..2^16-2>; |
| 9207 | * |
| 9208 | * The TLS 1.3 signature algorithm extension was defined to be a compatible |
| 9209 | * generalization of the TLS 1.2 signature algorithm extension. |
| 9210 | * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by |
| 9211 | * `SignatureScheme` field of TLS 1.3 |
| 9212 | * |
| 9213 | */ |
| 9214 | int mbedtls_ssl_write_sig_alg_ext( mbedtls_ssl_context *ssl, unsigned char *buf, |
| 9215 | const unsigned char *end, size_t *out_len ) |
| 9216 | { |
| 9217 | unsigned char *p = buf; |
| 9218 | unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */ |
| 9219 | size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */ |
| 9220 | |
| 9221 | *out_len = 0; |
| 9222 | |
| 9223 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding signature_algorithms extension" ) ); |
| 9224 | |
| 9225 | /* Check if we have space for header and length field: |
| 9226 | * - extension_type (2 bytes) |
| 9227 | * - extension_data_length (2 bytes) |
| 9228 | * - supported_signature_algorithms_length (2 bytes) |
| 9229 | */ |
| 9230 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 ); |
| 9231 | p += 6; |
| 9232 | |
| 9233 | /* |
| 9234 | * Write supported_signature_algorithms |
| 9235 | */ |
| 9236 | supported_sig_alg = p; |
| 9237 | const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs( ssl ); |
| 9238 | if( sig_alg == NULL ) |
| 9239 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); |
| 9240 | |
| 9241 | for( ; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++ ) |
| 9242 | { |
Jerry Yu | 53f5c15 | 2022-06-22 20:24:38 +0800 | [diff] [blame] | 9243 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "got signature scheme [%x] %s", |
| 9244 | *sig_alg, |
| 9245 | mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) ); |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9246 | if( ! mbedtls_ssl_sig_alg_is_supported( ssl, *sig_alg ) ) |
| 9247 | continue; |
| 9248 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 ); |
| 9249 | MBEDTLS_PUT_UINT16_BE( *sig_alg, p, 0 ); |
| 9250 | p += 2; |
Jerry Yu | 80dd5db | 2022-06-22 19:30:32 +0800 | [diff] [blame] | 9251 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "sent signature scheme [%x] %s", |
Jerry Yu | f3b46b5 | 2022-06-19 16:52:27 +0800 | [diff] [blame] | 9252 | *sig_alg, |
| 9253 | mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) ); |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9254 | } |
| 9255 | |
| 9256 | /* Length of supported_signature_algorithms */ |
| 9257 | supported_sig_alg_len = p - supported_sig_alg; |
| 9258 | if( supported_sig_alg_len == 0 ) |
| 9259 | { |
| 9260 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "No signature algorithms defined." ) ); |
| 9261 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 9262 | } |
| 9263 | |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9264 | MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SIG_ALG, buf, 0 ); |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9265 | MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len + 2, buf, 2 ); |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9266 | MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len, buf, 4 ); |
| 9267 | |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9268 | *out_len = p - buf; |
| 9269 | |
| 9270 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | c4bf5d6 | 2022-10-29 09:08:47 +0800 | [diff] [blame] | 9271 | mbedtls_ssl_tls13_set_hs_sent_ext_mask( ssl, MBEDTLS_TLS_EXT_SIG_ALG ); |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9272 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Jerry Yu | 0c354a2 | 2022-08-29 15:25:36 +0800 | [diff] [blame] | 9273 | |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9274 | return( 0 ); |
| 9275 | } |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 9276 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9277 | |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 9278 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 9279 | /* |
| 9280 | * mbedtls_ssl_parse_server_name_ext |
| 9281 | * |
| 9282 | * Structure of server_name extension: |
| 9283 | * |
| 9284 | * enum { |
| 9285 | * host_name(0), (255) |
| 9286 | * } NameType; |
| 9287 | * opaque HostName<1..2^16-1>; |
| 9288 | * |
| 9289 | * struct { |
| 9290 | * NameType name_type; |
| 9291 | * select (name_type) { |
| 9292 | * case host_name: HostName; |
| 9293 | * } name; |
| 9294 | * } ServerName; |
| 9295 | * struct { |
| 9296 | * ServerName server_name_list<1..2^16-1> |
| 9297 | * } ServerNameList; |
| 9298 | */ |
Ronald Cron | ce7d76e | 2022-07-08 18:56:49 +0200 | [diff] [blame] | 9299 | MBEDTLS_CHECK_RETURN_CRITICAL |
XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 9300 | int mbedtls_ssl_parse_server_name_ext( mbedtls_ssl_context *ssl, |
| 9301 | const unsigned char *buf, |
| 9302 | const unsigned char *end ) |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 9303 | { |
| 9304 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 9305 | const unsigned char *p = buf; |
XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 9306 | size_t server_name_list_len, hostname_len; |
| 9307 | const unsigned char *server_name_list_end; |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 9308 | |
XiaokangQian | f2a9420 | 2022-05-20 06:44:24 +0000 | [diff] [blame] | 9309 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "parse ServerName extension" ) ); |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 9310 | |
| 9311 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 ); |
XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 9312 | server_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 ); |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 9313 | p += 2; |
| 9314 | |
XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 9315 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, server_name_list_len ); |
| 9316 | server_name_list_end = p + server_name_list_len; |
XiaokangQian | 75fe8c7 | 2022-06-15 09:42:45 +0000 | [diff] [blame] | 9317 | while( p < server_name_list_end ) |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 9318 | { |
XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 9319 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end, 3 ); |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 9320 | hostname_len = MBEDTLS_GET_UINT16_BE( p, 1 ); |
XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 9321 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end, |
| 9322 | hostname_len + 3 ); |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 9323 | |
| 9324 | if( p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME ) |
| 9325 | { |
XiaokangQian | 75fe8c7 | 2022-06-15 09:42:45 +0000 | [diff] [blame] | 9326 | /* sni_name is intended to be used only during the parsing of the |
| 9327 | * ClientHello message (it is reset to NULL before the end of |
| 9328 | * the message parsing). Thus it is ok to just point to the |
| 9329 | * reception buffer and not make a copy of it. |
| 9330 | */ |
XiaokangQian | f2a9420 | 2022-05-20 06:44:24 +0000 | [diff] [blame] | 9331 | ssl->handshake->sni_name = p + 3; |
| 9332 | ssl->handshake->sni_name_len = hostname_len; |
| 9333 | if( ssl->conf->f_sni == NULL ) |
| 9334 | return( 0 ); |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 9335 | ret = ssl->conf->f_sni( ssl->conf->p_sni, |
XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 9336 | ssl, p + 3, hostname_len ); |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 9337 | if( ret != 0 ) |
| 9338 | { |
XiaokangQian | f2a9420 | 2022-05-20 06:44:24 +0000 | [diff] [blame] | 9339 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_sni_wrapper", ret ); |
XiaokangQian | 129aeb9 | 2022-06-02 09:29:18 +0000 | [diff] [blame] | 9340 | MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME, |
| 9341 | MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME ); |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 9342 | return( MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME ); |
| 9343 | } |
| 9344 | return( 0 ); |
| 9345 | } |
| 9346 | |
| 9347 | p += hostname_len + 3; |
| 9348 | } |
| 9349 | |
| 9350 | return( 0 ); |
| 9351 | } |
| 9352 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
| 9353 | |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9354 | #if defined(MBEDTLS_SSL_ALPN) |
Ronald Cron | ce7d76e | 2022-07-08 18:56:49 +0200 | [diff] [blame] | 9355 | MBEDTLS_CHECK_RETURN_CRITICAL |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9356 | int mbedtls_ssl_parse_alpn_ext( mbedtls_ssl_context *ssl, |
| 9357 | const unsigned char *buf, |
| 9358 | const unsigned char *end ) |
| 9359 | { |
| 9360 | const unsigned char *p = buf; |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 9361 | size_t protocol_name_list_len; |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9362 | const unsigned char *protocol_name_list; |
| 9363 | const unsigned char *protocol_name_list_end; |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 9364 | size_t protocol_name_len; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9365 | |
| 9366 | /* If ALPN not configured, just ignore the extension */ |
| 9367 | if( ssl->conf->alpn_list == NULL ) |
| 9368 | return( 0 ); |
| 9369 | |
| 9370 | /* |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 9371 | * RFC7301, section 3.1 |
| 9372 | * opaque ProtocolName<1..2^8-1>; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9373 | * |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 9374 | * struct { |
| 9375 | * ProtocolName protocol_name_list<2..2^16-1> |
| 9376 | * } ProtocolNameList; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9377 | */ |
| 9378 | |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 9379 | /* |
XiaokangQian | 0b776e2 | 2022-06-24 09:04:59 +0000 | [diff] [blame] | 9380 | * protocol_name_list_len 2 bytes |
| 9381 | * protocol_name_len 1 bytes |
| 9382 | * protocol_name >=1 byte |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 9383 | */ |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9384 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 ); |
| 9385 | |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 9386 | protocol_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 ); |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9387 | p += 2; |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 9388 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, protocol_name_list_len ); |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9389 | protocol_name_list = p; |
| 9390 | protocol_name_list_end = p + protocol_name_list_len; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9391 | |
| 9392 | /* Validate peer's list (lengths) */ |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9393 | while( p < protocol_name_list_end ) |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9394 | { |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9395 | protocol_name_len = *p++; |
| 9396 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, protocol_name_list_end, |
| 9397 | protocol_name_len ); |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 9398 | if( protocol_name_len == 0 ) |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9399 | { |
| 9400 | MBEDTLS_SSL_PEND_FATAL_ALERT( |
| 9401 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER, |
| 9402 | MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER ); |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9403 | return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER ); |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9404 | } |
| 9405 | |
| 9406 | p += protocol_name_len; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9407 | } |
| 9408 | |
| 9409 | /* Use our order of preference */ |
| 9410 | for( const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++ ) |
| 9411 | { |
| 9412 | size_t const alpn_len = strlen( *alpn ); |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9413 | p = protocol_name_list; |
| 9414 | while( p < protocol_name_list_end ) |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9415 | { |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9416 | protocol_name_len = *p++; |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 9417 | if( protocol_name_len == alpn_len && |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9418 | memcmp( p, *alpn, alpn_len ) == 0 ) |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9419 | { |
| 9420 | ssl->alpn_chosen = *alpn; |
| 9421 | return( 0 ); |
| 9422 | } |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9423 | |
| 9424 | p += protocol_name_len; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9425 | } |
| 9426 | } |
| 9427 | |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9428 | /* If we get here, no match was found */ |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9429 | MBEDTLS_SSL_PEND_FATAL_ALERT( |
| 9430 | MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL, |
| 9431 | MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL ); |
| 9432 | return( MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL ); |
| 9433 | } |
| 9434 | |
| 9435 | int mbedtls_ssl_write_alpn_ext( mbedtls_ssl_context *ssl, |
| 9436 | unsigned char *buf, |
| 9437 | unsigned char *end, |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 9438 | size_t *out_len ) |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9439 | { |
| 9440 | unsigned char *p = buf; |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9441 | size_t protocol_name_len; |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 9442 | *out_len = 0; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9443 | |
| 9444 | if( ssl->alpn_chosen == NULL ) |
| 9445 | { |
| 9446 | return( 0 ); |
| 9447 | } |
| 9448 | |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9449 | protocol_name_len = strlen( ssl->alpn_chosen ); |
| 9450 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 7 + protocol_name_len ); |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9451 | |
| 9452 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "server side, adding alpn extension" ) ); |
| 9453 | /* |
| 9454 | * 0 . 1 ext identifier |
| 9455 | * 2 . 3 ext length |
| 9456 | * 4 . 5 protocol list length |
| 9457 | * 6 . 6 protocol name length |
| 9458 | * 7 . 7+n protocol name |
| 9459 | */ |
| 9460 | MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ALPN, p, 0 ); |
| 9461 | |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9462 | *out_len = 7 + protocol_name_len; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9463 | |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9464 | MBEDTLS_PUT_UINT16_BE( protocol_name_len + 3, p, 2 ); |
| 9465 | MBEDTLS_PUT_UINT16_BE( protocol_name_len + 1, p, 4 ); |
XiaokangQian | 0b776e2 | 2022-06-24 09:04:59 +0000 | [diff] [blame] | 9466 | /* Note: the length of the chosen protocol has been checked to be less |
| 9467 | * than 255 bytes in `mbedtls_ssl_conf_alpn_protocols`. |
| 9468 | */ |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9469 | p[6] = MBEDTLS_BYTE_0( protocol_name_len ); |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9470 | |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9471 | memcpy( p + 7, ssl->alpn_chosen, protocol_name_len ); |
Jerry Yu | b95dd36 | 2022-11-08 21:19:34 +0800 | [diff] [blame] | 9472 | |
| 9473 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 9474 | mbedtls_ssl_tls13_set_hs_sent_ext_mask( ssl, MBEDTLS_TLS_EXT_ALPN ); |
| 9475 | #endif |
| 9476 | |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9477 | return ( 0 ); |
| 9478 | } |
| 9479 | #endif /* MBEDTLS_SSL_ALPN */ |
| 9480 | |
Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 9481 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \ |
Xiaokang Qian | 0340929 | 2022-10-12 02:49:52 +0000 | [diff] [blame] | 9482 | defined(MBEDTLS_SSL_SESSION_TICKETS) && \ |
Xiaokang Qian | ed0620c | 2022-10-12 06:58:13 +0000 | [diff] [blame] | 9483 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \ |
Xiaokang Qian | ed3afcd | 2022-10-12 08:31:11 +0000 | [diff] [blame] | 9484 | defined(MBEDTLS_SSL_CLI_C) |
Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 9485 | int mbedtls_ssl_session_set_hostname( mbedtls_ssl_session *session, |
| 9486 | const char *hostname ) |
| 9487 | { |
| 9488 | /* Initialize to suppress unnecessary compiler warning */ |
| 9489 | size_t hostname_len = 0; |
| 9490 | |
| 9491 | /* Check if new hostname is valid before |
| 9492 | * making any change to current one */ |
| 9493 | if( hostname != NULL ) |
| 9494 | { |
| 9495 | hostname_len = strlen( hostname ); |
| 9496 | |
| 9497 | if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN ) |
| 9498 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 9499 | } |
| 9500 | |
| 9501 | /* Now it's clear that we will overwrite the old hostname, |
| 9502 | * so we can free it safely */ |
| 9503 | if( session->hostname != NULL ) |
| 9504 | { |
| 9505 | mbedtls_platform_zeroize( session->hostname, |
| 9506 | strlen( session->hostname ) ); |
| 9507 | mbedtls_free( session->hostname ); |
| 9508 | } |
| 9509 | |
| 9510 | /* Passing NULL as hostname shall clear the old one */ |
| 9511 | if( hostname == NULL ) |
| 9512 | { |
| 9513 | session->hostname = NULL; |
| 9514 | } |
| 9515 | else |
| 9516 | { |
| 9517 | session->hostname = mbedtls_calloc( 1, hostname_len + 1 ); |
| 9518 | if( session->hostname == NULL ) |
| 9519 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 9520 | |
| 9521 | memcpy( session->hostname, hostname, hostname_len ); |
| 9522 | } |
| 9523 | |
| 9524 | return( 0 ); |
| 9525 | } |
Xiaokang Qian | 0340929 | 2022-10-12 02:49:52 +0000 | [diff] [blame] | 9526 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 && |
| 9527 | MBEDTLS_SSL_SESSION_TICKETS && |
Xiaokang Qian | ed0620c | 2022-10-12 06:58:13 +0000 | [diff] [blame] | 9528 | MBEDTLS_SSL_SERVER_NAME_INDICATION && |
Xiaokang Qian | ed3afcd | 2022-10-12 08:31:11 +0000 | [diff] [blame] | 9529 | MBEDTLS_SSL_CLI_C */ |
Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 9530 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9531 | #endif /* MBEDTLS_SSL_TLS_C */ |