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 */ |
Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 968 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 969 | if( ssl->transform_negotiate ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 970 | mbedtls_ssl_transform_free( ssl->transform_negotiate ); |
Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 971 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 972 | if( ssl->session_negotiate ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 973 | mbedtls_ssl_session_free( ssl->session_negotiate ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 974 | if( ssl->handshake ) |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 975 | mbedtls_ssl_handshake_free( ssl ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 976 | |
Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 977 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 978 | /* |
| 979 | * Either the pointers are now NULL or cleared properly and can be freed. |
| 980 | * Now allocate missing structures. |
| 981 | */ |
| 982 | if( ssl->transform_negotiate == NULL ) |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 983 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 984 | ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 985 | } |
Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 986 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 987 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 988 | if( ssl->session_negotiate == NULL ) |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 989 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 990 | ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 991 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 992 | |
Paul Bakker | 82788fb | 2014-10-20 13:59:19 +0200 | [diff] [blame] | 993 | if( ssl->handshake == NULL ) |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 994 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 995 | ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 996 | } |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 997 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 998 | /* If the buffers are too small - reallocate */ |
Andrzej Kurek | 8ea6872 | 2020-04-03 06:40:47 -0400 | [diff] [blame] | 999 | |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 1000 | handle_buffer_resizing( ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN, |
| 1001 | MBEDTLS_SSL_OUT_BUFFER_LEN ); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 1002 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1003 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 1004 | /* All pointers should exist and can be directly freed without issue */ |
Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 1005 | if( ssl->handshake == NULL || |
| 1006 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1007 | ssl->transform_negotiate == NULL || |
Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 1008 | #endif |
| 1009 | ssl->session_negotiate == NULL ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1010 | { |
Manuel Pégourié-Gonnard | b2a18a2 | 2015-05-27 16:29:56 +0200 | [diff] [blame] | 1011 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 1012 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1013 | mbedtls_free( ssl->handshake ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 1014 | ssl->handshake = NULL; |
Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 1015 | |
| 1016 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 1017 | mbedtls_free( ssl->transform_negotiate ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 1018 | ssl->transform_negotiate = NULL; |
Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 1019 | #endif |
| 1020 | |
| 1021 | mbedtls_free( ssl->session_negotiate ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 1022 | ssl->session_negotiate = NULL; |
| 1023 | |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 1024 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1025 | } |
| 1026 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 1027 | /* Initialize structures */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1028 | mbedtls_ssl_session_init( ssl->session_negotiate ); |
Paul Bakker | 968afaa | 2014-07-09 11:09:24 +0200 | [diff] [blame] | 1029 | ssl_handshake_params_init( ssl->handshake ); |
| 1030 | |
Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 1031 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 1032 | mbedtls_ssl_transform_init( ssl->transform_negotiate ); |
| 1033 | #endif |
| 1034 | |
Jerry Yu | d0766ec | 2022-09-22 10:46:57 +0800 | [diff] [blame] | 1035 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \ |
| 1036 | defined(MBEDTLS_SSL_SRV_C) && \ |
| 1037 | defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 1038 | ssl->handshake->new_session_tickets_count = |
| 1039 | ssl->conf->new_session_tickets_count ; |
| 1040 | #endif |
| 1041 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1042 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 1043 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 1044 | { |
| 1045 | ssl->handshake->alt_transform_out = ssl->transform_out; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 1046 | |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 1047 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 1048 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING; |
| 1049 | else |
| 1050 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING; |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 1051 | |
Hanno Becker | 0f57a65 | 2020-02-05 10:37:26 +0000 | [diff] [blame] | 1052 | mbedtls_ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 1053 | } |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 1054 | #endif |
| 1055 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 1056 | /* |
| 1057 | * curve_list is translated to IANA TLS group identifiers here because |
| 1058 | * mbedtls_ssl_conf_curves returns void and so can't return |
| 1059 | * any error codes. |
| 1060 | */ |
| 1061 | #if defined(MBEDTLS_ECP_C) |
| 1062 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 1063 | /* Heap allocate and translate curve_list from internal to IANA group ids */ |
| 1064 | if ( ssl->conf->curve_list != NULL ) |
| 1065 | { |
| 1066 | size_t length; |
| 1067 | const mbedtls_ecp_group_id *curve_list = ssl->conf->curve_list; |
| 1068 | |
| 1069 | for( length = 0; ( curve_list[length] != MBEDTLS_ECP_DP_NONE ) && |
| 1070 | ( length < MBEDTLS_ECP_DP_MAX ); length++ ) {} |
| 1071 | |
| 1072 | /* Leave room for zero termination */ |
| 1073 | uint16_t *group_list = mbedtls_calloc( length + 1, sizeof(uint16_t) ); |
| 1074 | if ( group_list == NULL ) |
| 1075 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 1076 | |
| 1077 | for( size_t i = 0; i < length; i++ ) |
| 1078 | { |
| 1079 | const mbedtls_ecp_curve_info *info = |
| 1080 | mbedtls_ecp_curve_info_from_grp_id( curve_list[i] ); |
| 1081 | if ( info == NULL ) |
| 1082 | { |
| 1083 | mbedtls_free( group_list ); |
| 1084 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); |
| 1085 | } |
| 1086 | group_list[i] = info->tls_id; |
| 1087 | } |
| 1088 | |
| 1089 | group_list[length] = 0; |
| 1090 | |
| 1091 | ssl->handshake->group_list = group_list; |
| 1092 | ssl->handshake->group_list_heap_allocated = 1; |
| 1093 | } |
| 1094 | else |
| 1095 | { |
| 1096 | ssl->handshake->group_list = ssl->conf->group_list; |
| 1097 | ssl->handshake->group_list_heap_allocated = 0; |
| 1098 | } |
| 1099 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
| 1100 | #endif /* MBEDTLS_ECP_C */ |
| 1101 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 1102 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1103 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Jerry Yu | a69269a | 2022-01-17 21:06:01 +0800 | [diff] [blame] | 1104 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Jerry Yu | 713013f | 2022-01-17 18:16:35 +0800 | [diff] [blame] | 1105 | /* Heap allocate and translate sig_hashes from internal hash identifiers to |
| 1106 | signature algorithms IANA identifiers. */ |
| 1107 | if ( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) && |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1108 | ssl->conf->sig_hashes != NULL ) |
| 1109 | { |
| 1110 | const int *md; |
| 1111 | const int *sig_hashes = ssl->conf->sig_hashes; |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 1112 | size_t sig_algs_len = 0; |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1113 | uint16_t *p; |
| 1114 | |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 1115 | #if defined(static_assert) |
| 1116 | static_assert( MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN |
| 1117 | <= ( SIZE_MAX - ( 2 * sizeof(uint16_t) ) ), |
| 1118 | "MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN too big" ); |
Jerry Yu | a68dca2 | 2022-01-20 16:28:27 +0800 | [diff] [blame] | 1119 | #endif |
| 1120 | |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1121 | for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ ) |
| 1122 | { |
| 1123 | if( mbedtls_ssl_hash_from_md_alg( *md ) == MBEDTLS_SSL_HASH_NONE ) |
| 1124 | continue; |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 1125 | #if defined(MBEDTLS_ECDSA_C) |
| 1126 | sig_algs_len += sizeof( uint16_t ); |
| 1127 | #endif |
Jerry Yu | a68dca2 | 2022-01-20 16:28:27 +0800 | [diff] [blame] | 1128 | |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 1129 | #if defined(MBEDTLS_RSA_C) |
| 1130 | sig_algs_len += sizeof( uint16_t ); |
| 1131 | #endif |
| 1132 | if( sig_algs_len > MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN ) |
| 1133 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1134 | } |
| 1135 | |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 1136 | if( sig_algs_len < MBEDTLS_SSL_MIN_SIG_ALG_LIST_LEN ) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1137 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); |
| 1138 | |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 1139 | ssl->handshake->sig_algs = mbedtls_calloc( 1, sig_algs_len + |
| 1140 | sizeof( uint16_t )); |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1141 | if( ssl->handshake->sig_algs == NULL ) |
| 1142 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 1143 | |
| 1144 | p = (uint16_t *)ssl->handshake->sig_algs; |
| 1145 | for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ ) |
| 1146 | { |
| 1147 | unsigned char hash = mbedtls_ssl_hash_from_md_alg( *md ); |
| 1148 | if( hash == MBEDTLS_SSL_HASH_NONE ) |
| 1149 | continue; |
Jerry Yu | 6106fdc | 2022-01-12 16:36:14 +0800 | [diff] [blame] | 1150 | #if defined(MBEDTLS_ECDSA_C) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1151 | *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_ECDSA); |
| 1152 | p++; |
Jerry Yu | 6106fdc | 2022-01-12 16:36:14 +0800 | [diff] [blame] | 1153 | #endif |
| 1154 | #if defined(MBEDTLS_RSA_C) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1155 | *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_RSA); |
| 1156 | p++; |
Jerry Yu | 6106fdc | 2022-01-12 16:36:14 +0800 | [diff] [blame] | 1157 | #endif |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1158 | } |
Gabor Mezei | 15b95a6 | 2022-05-09 16:37:58 +0200 | [diff] [blame] | 1159 | *p = MBEDTLS_TLS_SIG_NONE; |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1160 | ssl->handshake->sig_algs_heap_allocated = 1; |
| 1161 | } |
| 1162 | else |
Jerry Yu | a69269a | 2022-01-17 21:06:01 +0800 | [diff] [blame] | 1163 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1164 | { |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1165 | ssl->handshake->sig_algs_heap_allocated = 0; |
| 1166 | } |
Jerry Yu | cc53910 | 2022-06-27 16:27:35 +0800 | [diff] [blame] | 1167 | #endif /* !MBEDTLS_DEPRECATED_REMOVED */ |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 1168 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1169 | return( 0 ); |
| 1170 | } |
| 1171 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 1172 | #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] | 1173 | /* Dummy cookie callbacks for defaults */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 1174 | MBEDTLS_CHECK_RETURN_CRITICAL |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 1175 | static int ssl_cookie_write_dummy( void *ctx, |
| 1176 | unsigned char **p, unsigned char *end, |
| 1177 | const unsigned char *cli_id, size_t cli_id_len ) |
| 1178 | { |
| 1179 | ((void) ctx); |
| 1180 | ((void) p); |
| 1181 | ((void) end); |
| 1182 | ((void) cli_id); |
| 1183 | ((void) cli_id_len); |
| 1184 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1185 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 1186 | } |
| 1187 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 1188 | MBEDTLS_CHECK_RETURN_CRITICAL |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 1189 | static int ssl_cookie_check_dummy( void *ctx, |
| 1190 | const unsigned char *cookie, size_t cookie_len, |
| 1191 | const unsigned char *cli_id, size_t cli_id_len ) |
| 1192 | { |
| 1193 | ((void) ctx); |
| 1194 | ((void) cookie); |
| 1195 | ((void) cookie_len); |
| 1196 | ((void) cli_id); |
| 1197 | ((void) cli_id_len); |
| 1198 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1199 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 1200 | } |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 1201 | #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 1202 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1203 | /* |
| 1204 | * Initialize an SSL context |
| 1205 | */ |
Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 1206 | void mbedtls_ssl_init( mbedtls_ssl_context *ssl ) |
| 1207 | { |
| 1208 | memset( ssl, 0, sizeof( mbedtls_ssl_context ) ); |
| 1209 | } |
| 1210 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 1211 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1212 | static int ssl_conf_version_check( const mbedtls_ssl_context *ssl ) |
| 1213 | { |
Ronald Cron | 086ee0b | 2022-03-15 15:18:51 +0100 | [diff] [blame] | 1214 | const mbedtls_ssl_config *conf = ssl->conf; |
| 1215 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 1216 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Ronald Cron | 086ee0b | 2022-03-15 15:18:51 +0100 | [diff] [blame] | 1217 | if( mbedtls_ssl_conf_is_tls13_only( conf ) ) |
| 1218 | { |
XiaokangQian | ed582dd | 2022-04-13 08:21:05 +0000 | [diff] [blame] | 1219 | if( conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 1220 | { |
| 1221 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS 1.3 is not yet supported." ) ); |
| 1222 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 1223 | } |
| 1224 | |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1225 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls13 only." ) ); |
| 1226 | return( 0 ); |
| 1227 | } |
| 1228 | #endif |
| 1229 | |
| 1230 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Ronald Cron | 086ee0b | 2022-03-15 15:18:51 +0100 | [diff] [blame] | 1231 | if( mbedtls_ssl_conf_is_tls12_only( conf ) ) |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1232 | { |
| 1233 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls12 only." ) ); |
| 1234 | return( 0 ); |
| 1235 | } |
| 1236 | #endif |
| 1237 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 1238 | #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] | 1239 | if( mbedtls_ssl_conf_is_hybrid_tls12_tls13( conf ) ) |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1240 | { |
XiaokangQian | ed582dd | 2022-04-13 08:21:05 +0000 | [diff] [blame] | 1241 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 1242 | { |
| 1243 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS not yet supported in Hybrid TLS 1.3 + TLS 1.2" ) ); |
| 1244 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 1245 | } |
| 1246 | |
XiaokangQian | 8f9dfe4 | 2022-04-15 02:52:39 +0000 | [diff] [blame] | 1247 | if( conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 1248 | { |
| 1249 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS 1.3 server is not supported yet." ) ); |
| 1250 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 1251 | } |
| 1252 | |
| 1253 | |
Ronald Cron | e1d3f06 | 2022-02-10 14:50:54 +0100 | [diff] [blame] | 1254 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is TLS 1.3 or TLS 1.2." ) ); |
| 1255 | return( 0 ); |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1256 | } |
| 1257 | #endif |
| 1258 | |
| 1259 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "The SSL configuration is invalid." ) ); |
| 1260 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); |
| 1261 | } |
| 1262 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 1263 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1264 | static int ssl_conf_check(const mbedtls_ssl_context *ssl) |
| 1265 | { |
| 1266 | int ret; |
| 1267 | ret = ssl_conf_version_check( ssl ); |
| 1268 | if( ret != 0 ) |
| 1269 | return( ret ); |
| 1270 | |
Jerry Yu | def7ae4 | 2022-10-30 14:13:19 +0800 | [diff] [blame] | 1271 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 1272 | /* RFC 8446 section 4.4.3 |
| 1273 | * |
| 1274 | * If the verification fails, the receiver MUST terminate the handshake with |
| 1275 | * a "decrypt_error" alert. |
| 1276 | * |
| 1277 | * If the client is configured as TLS 1.3 only with optional verify, return |
| 1278 | * bad config. |
| 1279 | * |
| 1280 | */ |
| 1281 | if( mbedtls_ssl_conf_tls13_ephemeral_enabled( |
| 1282 | (mbedtls_ssl_context *)ssl ) && |
| 1283 | ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
| 1284 | ssl->conf->max_tls_version == MBEDTLS_SSL_VERSION_TLS1_3 && |
| 1285 | ssl->conf->min_tls_version == MBEDTLS_SSL_VERSION_TLS1_3 && |
| 1286 | ssl->conf->authmode == MBEDTLS_SSL_VERIFY_OPTIONAL ) |
| 1287 | { |
| 1288 | MBEDTLS_SSL_DEBUG_MSG( |
Dave Rodgman | e8734d8 | 2022-10-31 14:30:24 +0000 | [diff] [blame] | 1289 | 1, ( "Optional verify auth mode " |
Jerry Yu | def7ae4 | 2022-10-30 14:13:19 +0800 | [diff] [blame] | 1290 | "is not available for TLS 1.3 client" ) ); |
| 1291 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); |
| 1292 | } |
| 1293 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 1294 | |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1295 | /* Space for further checks */ |
| 1296 | |
| 1297 | return( 0 ); |
| 1298 | } |
| 1299 | |
Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 1300 | /* |
| 1301 | * Setup an SSL context |
| 1302 | */ |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 1303 | |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 1304 | int mbedtls_ssl_setup( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 1897af9 | 2015-05-10 23:27:38 +0200 | [diff] [blame] | 1305 | const mbedtls_ssl_config *conf ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1306 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1307 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1308 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; |
| 1309 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1310 | |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 1311 | ssl->conf = conf; |
Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 1312 | |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1313 | if( ( ret = ssl_conf_check( ssl ) ) != 0 ) |
| 1314 | return( ret ); |
| 1315 | |
Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 1316 | /* |
Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 1317 | * Prepare base structures |
Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 1318 | */ |
k-stachowiak | c9a5f02 | 2018-07-24 13:53:31 +0200 | [diff] [blame] | 1319 | |
| 1320 | /* Set to NULL in case of an error condition */ |
| 1321 | ssl->out_buf = NULL; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1322 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1323 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 1324 | ssl->in_buf_len = in_buf_len; |
| 1325 | #endif |
| 1326 | ssl->in_buf = mbedtls_calloc( 1, in_buf_len ); |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 1327 | if( ssl->in_buf == NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1328 | { |
Paul Elliott | d48d5c6 | 2021-01-07 14:47:05 +0000 | [diff] [blame] | 1329 | 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] | 1330 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1331 | goto error; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 1332 | } |
| 1333 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1334 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 1335 | ssl->out_buf_len = out_buf_len; |
| 1336 | #endif |
| 1337 | ssl->out_buf = mbedtls_calloc( 1, out_buf_len ); |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 1338 | if( ssl->out_buf == NULL ) |
| 1339 | { |
Paul Elliott | d48d5c6 | 2021-01-07 14:47:05 +0000 | [diff] [blame] | 1340 | 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] | 1341 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1342 | goto error; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1343 | } |
| 1344 | |
Hanno Becker | 3e6f8ab | 2020-02-05 10:40:57 +0000 | [diff] [blame] | 1345 | mbedtls_ssl_reset_in_out_pointers( ssl ); |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 1346 | |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 1347 | #if defined(MBEDTLS_SSL_DTLS_SRTP) |
Ron Eldor | 3adb992 | 2017-12-21 10:15:08 +0200 | [diff] [blame] | 1348 | memset( &ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info) ); |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 1349 | #endif |
| 1350 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1351 | if( ( ret = ssl_handshake_init( ssl ) ) != 0 ) |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1352 | goto error; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1353 | |
| 1354 | return( 0 ); |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1355 | |
| 1356 | error: |
| 1357 | mbedtls_free( ssl->in_buf ); |
| 1358 | mbedtls_free( ssl->out_buf ); |
| 1359 | |
| 1360 | ssl->conf = NULL; |
| 1361 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1362 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 1363 | ssl->in_buf_len = 0; |
| 1364 | ssl->out_buf_len = 0; |
| 1365 | #endif |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1366 | ssl->in_buf = NULL; |
| 1367 | ssl->out_buf = NULL; |
| 1368 | |
| 1369 | ssl->in_hdr = NULL; |
| 1370 | ssl->in_ctr = NULL; |
| 1371 | ssl->in_len = NULL; |
| 1372 | ssl->in_iv = NULL; |
| 1373 | ssl->in_msg = NULL; |
| 1374 | |
| 1375 | ssl->out_hdr = NULL; |
| 1376 | ssl->out_ctr = NULL; |
| 1377 | ssl->out_len = NULL; |
| 1378 | ssl->out_iv = NULL; |
| 1379 | ssl->out_msg = NULL; |
| 1380 | |
k-stachowiak | 9f7798e | 2018-07-31 16:52:32 +0200 | [diff] [blame] | 1381 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1382 | } |
| 1383 | |
| 1384 | /* |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 1385 | * Reset an initialized and used SSL context for re-use while retaining |
| 1386 | * all application-set variables, function pointers and data. |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 1387 | * |
| 1388 | * If partial is non-zero, keep data in the input buffer and client ID. |
| 1389 | * (Use when a DTLS client reconnects from the same port.) |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 1390 | */ |
XiaokangQian | 78b1fa7 | 2022-01-19 06:56:30 +0000 | [diff] [blame] | 1391 | void mbedtls_ssl_session_reset_msg_layer( mbedtls_ssl_context *ssl, |
| 1392 | int partial ) |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 1393 | { |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1394 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 1395 | size_t in_buf_len = ssl->in_buf_len; |
| 1396 | size_t out_buf_len = ssl->out_buf_len; |
| 1397 | #else |
| 1398 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; |
| 1399 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; |
| 1400 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1401 | |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1402 | #if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || !defined(MBEDTLS_SSL_SRV_C) |
| 1403 | partial = 0; |
Hanno Becker | 7e77213 | 2018-08-10 12:38:21 +0100 | [diff] [blame] | 1404 | #endif |
| 1405 | |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 1406 | /* Cancel any possibly running timer */ |
Hanno Becker | 0f57a65 | 2020-02-05 10:37:26 +0000 | [diff] [blame] | 1407 | mbedtls_ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 1408 | |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1409 | mbedtls_ssl_reset_in_out_pointers( ssl ); |
| 1410 | |
| 1411 | /* Reset incoming message parsing */ |
| 1412 | ssl->in_offt = NULL; |
| 1413 | ssl->nb_zero = 0; |
| 1414 | ssl->in_msgtype = 0; |
| 1415 | ssl->in_msglen = 0; |
| 1416 | ssl->in_hslen = 0; |
| 1417 | ssl->keep_current_message = 0; |
| 1418 | ssl->transform_in = NULL; |
| 1419 | |
| 1420 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 1421 | ssl->next_record_offset = 0; |
| 1422 | ssl->in_epoch = 0; |
| 1423 | #endif |
| 1424 | |
| 1425 | /* Keep current datagram if partial == 1 */ |
| 1426 | if( partial == 0 ) |
| 1427 | { |
| 1428 | ssl->in_left = 0; |
| 1429 | memset( ssl->in_buf, 0, in_buf_len ); |
| 1430 | } |
| 1431 | |
Ronald Cron | ad8c17b | 2022-06-10 17:18:09 +0200 | [diff] [blame] | 1432 | ssl->send_alert = 0; |
| 1433 | |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1434 | /* Reset outgoing message writing */ |
| 1435 | ssl->out_msgtype = 0; |
| 1436 | ssl->out_msglen = 0; |
| 1437 | ssl->out_left = 0; |
| 1438 | memset( ssl->out_buf, 0, out_buf_len ); |
| 1439 | memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) ); |
| 1440 | ssl->transform_out = NULL; |
| 1441 | |
| 1442 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 1443 | mbedtls_ssl_dtls_replay_reset( ssl ); |
| 1444 | #endif |
| 1445 | |
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1446 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1447 | if( ssl->transform ) |
| 1448 | { |
| 1449 | mbedtls_ssl_transform_free( ssl->transform ); |
| 1450 | mbedtls_free( ssl->transform ); |
| 1451 | ssl->transform = NULL; |
| 1452 | } |
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1453 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 1454 | |
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1455 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 1456 | mbedtls_ssl_transform_free( ssl->transform_application ); |
| 1457 | mbedtls_free( ssl->transform_application ); |
| 1458 | ssl->transform_application = NULL; |
| 1459 | |
| 1460 | if( ssl->handshake != NULL ) |
| 1461 | { |
Jerry Yu | 3d9b590 | 2022-11-04 14:07:25 +0800 | [diff] [blame] | 1462 | #if defined(MBEDTLS_SSL_EARLY_DATA) |
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1463 | mbedtls_ssl_transform_free( ssl->handshake->transform_earlydata ); |
| 1464 | mbedtls_free( ssl->handshake->transform_earlydata ); |
| 1465 | ssl->handshake->transform_earlydata = NULL; |
Jerry Yu | 3d9b590 | 2022-11-04 14:07:25 +0800 | [diff] [blame] | 1466 | #endif |
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1467 | |
| 1468 | mbedtls_ssl_transform_free( ssl->handshake->transform_handshake ); |
| 1469 | mbedtls_free( ssl->handshake->transform_handshake ); |
| 1470 | ssl->handshake->transform_handshake = NULL; |
| 1471 | } |
| 1472 | |
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1473 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1474 | } |
| 1475 | |
| 1476 | int mbedtls_ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial ) |
| 1477 | { |
| 1478 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 1479 | |
| 1480 | ssl->state = MBEDTLS_SSL_HELLO_REQUEST; |
| 1481 | |
XiaokangQian | 78b1fa7 | 2022-01-19 06:56:30 +0000 | [diff] [blame] | 1482 | mbedtls_ssl_session_reset_msg_layer( ssl, partial ); |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1483 | |
| 1484 | /* Reset renegotiation state */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1485 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 1486 | ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE; |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 1487 | ssl->renego_records_seen = 0; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1488 | |
| 1489 | ssl->verify_data_len = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1490 | memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN ); |
| 1491 | 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] | 1492 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1493 | ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1494 | |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1495 | ssl->session_in = NULL; |
Hanno Becker | 7864090 | 2018-08-13 16:35:15 +0100 | [diff] [blame] | 1496 | ssl->session_out = NULL; |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 1497 | if( ssl->session ) |
| 1498 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1499 | mbedtls_ssl_session_free( ssl->session ); |
| 1500 | mbedtls_free( ssl->session ); |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 1501 | ssl->session = NULL; |
| 1502 | } |
| 1503 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1504 | #if defined(MBEDTLS_SSL_ALPN) |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 1505 | ssl->alpn_chosen = NULL; |
| 1506 | #endif |
| 1507 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 1508 | #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C) |
David Horstmann | 3b2276a | 2022-10-06 14:49:08 +0100 | [diff] [blame] | 1509 | int free_cli_id = 1; |
Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 1510 | #if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) |
David Horstmann | 3b2276a | 2022-10-06 14:49:08 +0100 | [diff] [blame] | 1511 | free_cli_id = ( partial == 0 ); |
Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 1512 | #endif |
David Horstmann | 3b2276a | 2022-10-06 14:49:08 +0100 | [diff] [blame] | 1513 | if( free_cli_id ) |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 1514 | { |
| 1515 | mbedtls_free( ssl->cli_id ); |
| 1516 | ssl->cli_id = NULL; |
| 1517 | ssl->cli_id_len = 0; |
| 1518 | } |
Manuel Pégourié-Gonnard | 43c0218 | 2014-07-22 17:32:01 +0200 | [diff] [blame] | 1519 | #endif |
| 1520 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1521 | if( ( ret = ssl_handshake_init( ssl ) ) != 0 ) |
| 1522 | return( ret ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1523 | |
| 1524 | return( 0 ); |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 1525 | } |
| 1526 | |
Manuel Pégourié-Gonnard | 779e429 | 2013-08-03 13:50:48 +0200 | [diff] [blame] | 1527 | /* |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 1528 | * Reset an initialized and used SSL context for re-use while retaining |
| 1529 | * all application-set variables, function pointers and data. |
| 1530 | */ |
| 1531 | int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl ) |
| 1532 | { |
Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 1533 | return( mbedtls_ssl_session_reset_int( ssl, 0 ) ); |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 1534 | } |
| 1535 | |
| 1536 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1537 | * SSL set accessors |
| 1538 | */ |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1539 | void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1540 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1541 | conf->endpoint = endpoint; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1542 | } |
| 1543 | |
Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 1544 | 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] | 1545 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1546 | conf->transport = transport; |
Manuel Pégourié-Gonnard | 0b1ff29 | 2014-02-06 13:04:16 +0100 | [diff] [blame] | 1547 | } |
| 1548 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1549 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1550 | 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] | 1551 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1552 | conf->anti_replay = mode; |
Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 1553 | } |
| 1554 | #endif |
| 1555 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1556 | 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] | 1557 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1558 | conf->badmac_limit = limit; |
Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 1559 | } |
Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 1560 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1561 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 1562 | |
Hanno Becker | 1841b0a | 2018-08-24 11:13:57 +0100 | [diff] [blame] | 1563 | void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl, |
| 1564 | unsigned allow_packing ) |
Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 1565 | { |
| 1566 | ssl->disable_datagram_packing = !allow_packing; |
| 1567 | } |
| 1568 | |
| 1569 | void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf, |
| 1570 | uint32_t min, uint32_t max ) |
Manuel Pégourié-Gonnard | 905dd24 | 2014-10-01 12:03:55 +0200 | [diff] [blame] | 1571 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1572 | conf->hs_timeout_min = min; |
| 1573 | conf->hs_timeout_max = max; |
Manuel Pégourié-Gonnard | 905dd24 | 2014-10-01 12:03:55 +0200 | [diff] [blame] | 1574 | } |
| 1575 | #endif |
| 1576 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1577 | void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1578 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1579 | conf->authmode = authmode; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1580 | } |
| 1581 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1582 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1583 | void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 1584 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 1585 | void *p_vrfy ) |
| 1586 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1587 | conf->f_vrfy = f_vrfy; |
| 1588 | conf->p_vrfy = p_vrfy; |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 1589 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1590 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 1591 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1592 | void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf, |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 1593 | int (*f_rng)(void *, unsigned char *, size_t), |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1594 | void *p_rng ) |
| 1595 | { |
Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 1596 | conf->f_rng = f_rng; |
| 1597 | conf->p_rng = p_rng; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1598 | } |
| 1599 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1600 | void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | fd47423 | 2015-06-23 16:34:24 +0200 | [diff] [blame] | 1601 | void (*f_dbg)(void *, int, const char *, int, const char *), |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1602 | void *p_dbg ) |
| 1603 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1604 | conf->f_dbg = f_dbg; |
| 1605 | conf->p_dbg = p_dbg; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1606 | } |
| 1607 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1608 | void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 1609 | void *p_bio, |
Simon Butcher | e846b51 | 2016-03-01 17:31:49 +0000 | [diff] [blame] | 1610 | mbedtls_ssl_send_t *f_send, |
| 1611 | mbedtls_ssl_recv_t *f_recv, |
| 1612 | mbedtls_ssl_recv_timeout_t *f_recv_timeout ) |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 1613 | { |
| 1614 | ssl->p_bio = p_bio; |
| 1615 | ssl->f_send = f_send; |
| 1616 | ssl->f_recv = f_recv; |
| 1617 | ssl->f_recv_timeout = f_recv_timeout; |
Manuel Pégourié-Gonnard | 97fd52c | 2015-05-06 15:38:52 +0100 | [diff] [blame] | 1618 | } |
| 1619 | |
Manuel Pégourié-Gonnard | 6e7aaca | 2018-08-20 10:37:23 +0200 | [diff] [blame] | 1620 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 1621 | void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu ) |
| 1622 | { |
| 1623 | ssl->mtu = mtu; |
| 1624 | } |
| 1625 | #endif |
| 1626 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1627 | 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] | 1628 | { |
| 1629 | conf->read_timeout = timeout; |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 1630 | } |
| 1631 | |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 1632 | void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl, |
| 1633 | void *p_timer, |
Simon Butcher | e846b51 | 2016-03-01 17:31:49 +0000 | [diff] [blame] | 1634 | mbedtls_ssl_set_timer_t *f_set_timer, |
| 1635 | mbedtls_ssl_get_timer_t *f_get_timer ) |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 1636 | { |
| 1637 | ssl->p_timer = p_timer; |
| 1638 | ssl->f_set_timer = f_set_timer; |
| 1639 | ssl->f_get_timer = f_get_timer; |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 1640 | |
| 1641 | /* Make sure we start with no timer running */ |
Hanno Becker | 0f57a65 | 2020-02-05 10:37:26 +0000 | [diff] [blame] | 1642 | mbedtls_ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 1643 | } |
| 1644 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1645 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1646 | void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf, |
Hanno Becker | a637ff6 | 2021-04-15 08:42:48 +0100 | [diff] [blame] | 1647 | void *p_cache, |
| 1648 | mbedtls_ssl_cache_get_t *f_get_cache, |
| 1649 | mbedtls_ssl_cache_set_t *f_set_cache ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1650 | { |
Manuel Pégourié-Gonnard | 5cb3308 | 2015-05-06 18:06:26 +0100 | [diff] [blame] | 1651 | conf->p_cache = p_cache; |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1652 | conf->f_get_cache = f_get_cache; |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1653 | conf->f_set_cache = f_set_cache; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1654 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1655 | #endif /* MBEDTLS_SSL_SRV_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1656 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1657 | #if defined(MBEDTLS_SSL_CLI_C) |
| 1658 | 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] | 1659 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1660 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1661 | |
| 1662 | if( ssl == NULL || |
| 1663 | session == NULL || |
| 1664 | ssl->session_negotiate == NULL || |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1665 | ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT ) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1666 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1667 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1668 | } |
| 1669 | |
Hanno Becker | e810bbc | 2021-05-14 16:01:05 +0100 | [diff] [blame] | 1670 | if( ssl->handshake->resume == 1 ) |
| 1671 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 1672 | |
Jerry Yu | 2109206 | 2022-10-10 21:21:31 +0800 | [diff] [blame] | 1673 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 1674 | if( session->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 ) |
Jerry Yu | 40afab6 | 2022-10-08 10:42:13 +0800 | [diff] [blame] | 1675 | { |
Jerry Yu | 2109206 | 2022-10-10 21:21:31 +0800 | [diff] [blame] | 1676 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 1677 | mbedtls_ssl_ciphersuite_from_id( session->ciphersuite ); |
| 1678 | |
| 1679 | if( mbedtls_ssl_validate_ciphersuite( |
| 1680 | ssl, ciphersuite_info, MBEDTLS_SSL_VERSION_TLS1_3, |
| 1681 | MBEDTLS_SSL_VERSION_TLS1_3 ) != 0 ) |
| 1682 | { |
| 1683 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "%d is not a valid TLS 1.3 ciphersuite.", |
| 1684 | session->ciphersuite ) ); |
| 1685 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1686 | } |
Jerry Yu | 40afab6 | 2022-10-08 10:42:13 +0800 | [diff] [blame] | 1687 | } |
Jerry Yu | 2109206 | 2022-10-10 21:21:31 +0800 | [diff] [blame] | 1688 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Jerry Yu | 40afab6 | 2022-10-08 10:42:13 +0800 | [diff] [blame] | 1689 | |
Hanno Becker | 52055ae | 2019-02-06 14:30:46 +0000 | [diff] [blame] | 1690 | if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate, |
| 1691 | session ) ) != 0 ) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1692 | return( ret ); |
| 1693 | |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 1694 | ssl->handshake->resume = 1; |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1695 | |
| 1696 | return( 0 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1697 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1698 | #endif /* MBEDTLS_SSL_CLI_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1699 | |
Mateusz Starzyk | 06b07fb | 2021-02-18 13:55:21 +0100 | [diff] [blame] | 1700 | void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf, |
| 1701 | const int *ciphersuites ) |
| 1702 | { |
Hanno Becker | d60b6c6 | 2021-04-29 12:04:11 +0100 | [diff] [blame] | 1703 | conf->ciphersuite_list = ciphersuites; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1704 | } |
| 1705 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 1706 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | cadebe5 | 2021-08-24 10:36:45 +0800 | [diff] [blame] | 1707 | void mbedtls_ssl_conf_tls13_key_exchange_modes( mbedtls_ssl_config *conf, |
Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 1708 | const int kex_modes ) |
| 1709 | { |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 1710 | 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] | 1711 | } |
Xiaokang Qian | 72de95d | 2022-10-25 02:54:33 +0000 | [diff] [blame] | 1712 | |
| 1713 | #if defined(MBEDTLS_SSL_EARLY_DATA) |
| 1714 | void mbedtls_ssl_tls13_conf_early_data( mbedtls_ssl_config *conf, |
Xiaokang Qian | 72dbfef | 2022-10-26 06:33:57 +0000 | [diff] [blame] | 1715 | int early_data_enabled ) |
Xiaokang Qian | 72de95d | 2022-10-25 02:54:33 +0000 | [diff] [blame] | 1716 | { |
| 1717 | conf->early_data_enabled = early_data_enabled; |
| 1718 | } |
Jerry Yu | cc4e007 | 2022-11-22 17:22:22 +0800 | [diff] [blame] | 1719 | |
| 1720 | #if defined(MBEDTLS_SSL_SRV_C) |
| 1721 | void mbedtls_ssl_tls13_conf_max_early_data_size( |
| 1722 | mbedtls_ssl_config *conf, uint32_t max_early_data_size ) |
| 1723 | { |
Jerry Yu | 39da985 | 2022-12-06 16:58:36 +0800 | [diff] [blame] | 1724 | conf->max_early_data_size = max_early_data_size; |
Jerry Yu | cc4e007 | 2022-11-22 17:22:22 +0800 | [diff] [blame] | 1725 | } |
| 1726 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 1727 | |
Xiaokang Qian | 72de95d | 2022-10-25 02:54:33 +0000 | [diff] [blame] | 1728 | #endif /* MBEDTLS_SSL_EARLY_DATA */ |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 1729 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 1730 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1731 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 6e3ee3a | 2015-06-17 10:58:20 +0200 | [diff] [blame] | 1732 | void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf, |
Nicholas Wilson | 2088e2e | 2015-09-08 16:53:18 +0100 | [diff] [blame] | 1733 | const mbedtls_x509_crt_profile *profile ) |
Manuel Pégourié-Gonnard | 6e3ee3a | 2015-06-17 10:58:20 +0200 | [diff] [blame] | 1734 | { |
| 1735 | conf->cert_profile = profile; |
| 1736 | } |
| 1737 | |
Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 1738 | static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert ) |
| 1739 | { |
| 1740 | mbedtls_ssl_key_cert *cur = key_cert, *next; |
| 1741 | |
| 1742 | while( cur != NULL ) |
| 1743 | { |
| 1744 | next = cur->next; |
| 1745 | mbedtls_free( cur ); |
| 1746 | cur = next; |
| 1747 | } |
| 1748 | } |
| 1749 | |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1750 | /* Append a new keycert entry to a (possibly empty) list */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 1751 | MBEDTLS_CHECK_RETURN_CRITICAL |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1752 | static int ssl_append_key_cert( mbedtls_ssl_key_cert **head, |
| 1753 | mbedtls_x509_crt *cert, |
| 1754 | mbedtls_pk_context *key ) |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1755 | { |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1756 | mbedtls_ssl_key_cert *new_cert; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1757 | |
Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 1758 | if( cert == NULL ) |
| 1759 | { |
| 1760 | /* Free list if cert is null */ |
| 1761 | ssl_key_cert_free( *head ); |
| 1762 | *head = NULL; |
| 1763 | return( 0 ); |
| 1764 | } |
| 1765 | |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1766 | new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) ); |
| 1767 | if( new_cert == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 1768 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1769 | |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1770 | new_cert->cert = cert; |
| 1771 | new_cert->key = key; |
| 1772 | new_cert->next = NULL; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1773 | |
Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 1774 | /* 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] | 1775 | if( *head == NULL ) |
Paul Bakker | 0333b97 | 2013-11-04 17:08:28 +0100 | [diff] [blame] | 1776 | { |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1777 | *head = new_cert; |
Paul Bakker | 0333b97 | 2013-11-04 17:08:28 +0100 | [diff] [blame] | 1778 | } |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1779 | else |
| 1780 | { |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1781 | mbedtls_ssl_key_cert *cur = *head; |
| 1782 | while( cur->next != NULL ) |
| 1783 | cur = cur->next; |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1784 | cur->next = new_cert; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1785 | } |
| 1786 | |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1787 | return( 0 ); |
| 1788 | } |
| 1789 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1790 | int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1791 | mbedtls_x509_crt *own_cert, |
| 1792 | mbedtls_pk_context *pk_key ) |
| 1793 | { |
Manuel Pégourié-Gonnard | 17a40cd | 2015-05-10 23:17:17 +0200 | [diff] [blame] | 1794 | 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] | 1795 | } |
| 1796 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1797 | void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 1798 | mbedtls_x509_crt *ca_chain, |
| 1799 | mbedtls_x509_crl *ca_crl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1800 | { |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 1801 | conf->ca_chain = ca_chain; |
| 1802 | conf->ca_crl = ca_crl; |
Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 1803 | |
| 1804 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 1805 | /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb() |
| 1806 | * cannot be used together. */ |
| 1807 | conf->f_ca_cb = NULL; |
| 1808 | conf->p_ca_cb = NULL; |
| 1809 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1810 | } |
Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 1811 | |
| 1812 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 1813 | void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf, |
Hanno Becker | afd0b0a | 2019-03-27 16:55:01 +0000 | [diff] [blame] | 1814 | mbedtls_x509_crt_ca_cb_t f_ca_cb, |
Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 1815 | void *p_ca_cb ) |
| 1816 | { |
| 1817 | conf->f_ca_cb = f_ca_cb; |
| 1818 | conf->p_ca_cb = p_ca_cb; |
| 1819 | |
| 1820 | /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb() |
| 1821 | * cannot be used together. */ |
| 1822 | conf->ca_chain = NULL; |
| 1823 | conf->ca_crl = NULL; |
| 1824 | } |
| 1825 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1826 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | eb2c658 | 2012-09-27 19:15:01 +0000 | [diff] [blame] | 1827 | |
Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 1828 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Glenn Strauss | 6989407 | 2022-01-24 12:58:00 -0500 | [diff] [blame] | 1829 | const unsigned char *mbedtls_ssl_get_hs_sni( mbedtls_ssl_context *ssl, |
| 1830 | size_t *name_len ) |
| 1831 | { |
| 1832 | *name_len = ssl->handshake->sni_name_len; |
| 1833 | return( ssl->handshake->sni_name ); |
| 1834 | } |
| 1835 | |
Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 1836 | int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl, |
| 1837 | mbedtls_x509_crt *own_cert, |
| 1838 | mbedtls_pk_context *pk_key ) |
| 1839 | { |
| 1840 | return( ssl_append_key_cert( &ssl->handshake->sni_key_cert, |
| 1841 | own_cert, pk_key ) ); |
| 1842 | } |
Manuel Pégourié-Gonnard | 22bfa4b | 2015-05-11 08:46:37 +0200 | [diff] [blame] | 1843 | |
| 1844 | void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl, |
| 1845 | mbedtls_x509_crt *ca_chain, |
| 1846 | mbedtls_x509_crl *ca_crl ) |
| 1847 | { |
| 1848 | ssl->handshake->sni_ca_chain = ca_chain; |
| 1849 | ssl->handshake->sni_ca_crl = ca_crl; |
| 1850 | } |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 1851 | |
Glenn Strauss | 999ef70 | 2022-03-11 01:37:23 -0500 | [diff] [blame] | 1852 | #if defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED) |
| 1853 | void mbedtls_ssl_set_hs_dn_hints( mbedtls_ssl_context *ssl, |
| 1854 | const mbedtls_x509_crt *crt) |
| 1855 | { |
| 1856 | ssl->handshake->dn_hints = crt; |
| 1857 | } |
| 1858 | #endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */ |
| 1859 | |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 1860 | void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl, |
| 1861 | int authmode ) |
| 1862 | { |
| 1863 | ssl->handshake->sni_authmode = authmode; |
| 1864 | } |
Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 1865 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
| 1866 | |
Hanno Becker | 8927c83 | 2019-04-03 12:52:50 +0100 | [diff] [blame] | 1867 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 1868 | void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl, |
| 1869 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), |
| 1870 | void *p_vrfy ) |
| 1871 | { |
| 1872 | ssl->f_vrfy = f_vrfy; |
| 1873 | ssl->p_vrfy = p_vrfy; |
| 1874 | } |
| 1875 | #endif |
| 1876 | |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 1877 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 1878 | /* |
| 1879 | * Set EC J-PAKE password for current handshake |
| 1880 | */ |
Valerio Setti | 02c25b5 | 2022-11-15 14:08:42 +0100 | [diff] [blame] | 1881 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 1882 | int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl, |
| 1883 | const unsigned char *pw, |
| 1884 | size_t pw_len ) |
| 1885 | { |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 1886 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 1887 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1888 | psa_pake_role_t psa_role; |
| 1889 | psa_status_t status; |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 1890 | |
Janos Follath | 8eb6413 | 2016-06-03 15:40:57 +0100 | [diff] [blame] | 1891 | if( ssl->handshake == NULL || ssl->conf == NULL ) |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 1892 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1893 | |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 1894 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 1895 | psa_role = PSA_PAKE_ROLE_SERVER; |
| 1896 | else |
| 1897 | psa_role = PSA_PAKE_ROLE_CLIENT; |
| 1898 | |
Valerio Setti | aca21b7 | 2022-11-17 18:17:01 +0100 | [diff] [blame] | 1899 | /* Empty password is not valid */ |
| 1900 | if( ( pw == NULL) || ( pw_len == 0 ) ) |
| 1901 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 1902 | |
Valerio Setti | aca21b7 | 2022-11-17 18:17:01 +0100 | [diff] [blame] | 1903 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 1904 | psa_set_key_algorithm( &attributes, PSA_ALG_JPAKE ); |
| 1905 | psa_set_key_type( &attributes, PSA_KEY_TYPE_PASSWORD ); |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 1906 | |
Valerio Setti | aca21b7 | 2022-11-17 18:17:01 +0100 | [diff] [blame] | 1907 | status = psa_import_key( &attributes, pw, pw_len, |
| 1908 | &ssl->handshake->psa_pake_password ); |
| 1909 | if( status != PSA_SUCCESS ) |
| 1910 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 1911 | |
| 1912 | psa_pake_cs_set_algorithm( &cipher_suite, PSA_ALG_JPAKE ); |
| 1913 | psa_pake_cs_set_primitive( &cipher_suite, |
| 1914 | PSA_PAKE_PRIMITIVE( PSA_PAKE_PRIMITIVE_TYPE_ECC, |
| 1915 | PSA_ECC_FAMILY_SECP_R1, |
| 1916 | 256) ); |
| 1917 | psa_pake_cs_set_hash( &cipher_suite, PSA_ALG_SHA_256 ); |
| 1918 | |
| 1919 | status = psa_pake_setup( &ssl->handshake->psa_pake_ctx, &cipher_suite ); |
| 1920 | if( status != PSA_SUCCESS ) |
| 1921 | { |
| 1922 | psa_destroy_key( ssl->handshake->psa_pake_password ); |
| 1923 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 1924 | } |
| 1925 | |
| 1926 | status = psa_pake_set_role( &ssl->handshake->psa_pake_ctx, psa_role ); |
| 1927 | if( status != PSA_SUCCESS ) |
| 1928 | { |
| 1929 | psa_destroy_key( ssl->handshake->psa_pake_password ); |
| 1930 | psa_pake_abort( &ssl->handshake->psa_pake_ctx ); |
| 1931 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 1932 | } |
| 1933 | |
Valerio Setti | aca21b7 | 2022-11-17 18:17:01 +0100 | [diff] [blame] | 1934 | psa_pake_set_password_key( &ssl->handshake->psa_pake_ctx, |
| 1935 | ssl->handshake->psa_pake_password ); |
| 1936 | if( status != PSA_SUCCESS ) |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 1937 | { |
Valerio Setti | aca21b7 | 2022-11-17 18:17:01 +0100 | [diff] [blame] | 1938 | psa_destroy_key( ssl->handshake->psa_pake_password ); |
| 1939 | psa_pake_abort( &ssl->handshake->psa_pake_ctx ); |
| 1940 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 1941 | } |
| 1942 | |
| 1943 | ssl->handshake->psa_pake_ctx_is_ok = 1; |
| 1944 | |
| 1945 | return( 0 ); |
Valerio Setti | 02c25b5 | 2022-11-15 14:08:42 +0100 | [diff] [blame] | 1946 | } |
| 1947 | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 1948 | int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl, |
| 1949 | const unsigned char *pw, |
| 1950 | size_t pw_len ) |
| 1951 | { |
| 1952 | mbedtls_ecjpake_role role; |
| 1953 | |
| 1954 | if( ssl->handshake == NULL || ssl->conf == NULL ) |
| 1955 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1956 | |
| 1957 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 1958 | role = MBEDTLS_ECJPAKE_SERVER; |
| 1959 | else |
| 1960 | role = MBEDTLS_ECJPAKE_CLIENT; |
| 1961 | |
| 1962 | return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx, |
| 1963 | role, |
| 1964 | MBEDTLS_MD_SHA256, |
| 1965 | MBEDTLS_ECP_DP_SECP256R1, |
| 1966 | pw, pw_len ) ); |
| 1967 | } |
Valerio Setti | 02c25b5 | 2022-11-15 14:08:42 +0100 | [diff] [blame] | 1968 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 1969 | #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 1970 | |
Ronald Cron | 73fe8df | 2022-10-05 14:31:43 +0200 | [diff] [blame] | 1971 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED) |
Ronald Cron | d29e13e | 2022-10-19 10:33:48 +0200 | [diff] [blame] | 1972 | int mbedtls_ssl_conf_has_static_psk( mbedtls_ssl_config const *conf ) |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 1973 | { |
Ronald Cron | d29e13e | 2022-10-19 10:33:48 +0200 | [diff] [blame] | 1974 | if( conf->psk_identity == NULL || |
| 1975 | conf->psk_identity_len == 0 ) |
| 1976 | { |
| 1977 | return( 0 ); |
| 1978 | } |
| 1979 | |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 1980 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Ronald Cron | d29e13e | 2022-10-19 10:33:48 +0200 | [diff] [blame] | 1981 | if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) ) |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 1982 | return( 1 ); |
| 1983 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Ronald Cron | d29e13e | 2022-10-19 10:33:48 +0200 | [diff] [blame] | 1984 | |
| 1985 | if( conf->psk != NULL && conf->psk_len != 0 ) |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 1986 | return( 1 ); |
| 1987 | |
| 1988 | return( 0 ); |
| 1989 | } |
| 1990 | |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1991 | static void ssl_conf_remove_psk( mbedtls_ssl_config *conf ) |
| 1992 | { |
| 1993 | /* Remove reference to existing PSK, if any. */ |
| 1994 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1995 | if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) ) |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1996 | { |
| 1997 | /* The maintenance of the PSK key slot is the |
| 1998 | * user's responsibility. */ |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1999 | conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT; |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2000 | } |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2001 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 2002 | if( conf->psk != NULL ) |
| 2003 | { |
| 2004 | mbedtls_platform_zeroize( conf->psk, conf->psk_len ); |
| 2005 | |
| 2006 | mbedtls_free( conf->psk ); |
| 2007 | conf->psk = NULL; |
| 2008 | conf->psk_len = 0; |
| 2009 | } |
| 2010 | |
| 2011 | /* Remove reference to PSK identity, if any. */ |
| 2012 | if( conf->psk_identity != NULL ) |
| 2013 | { |
| 2014 | mbedtls_free( conf->psk_identity ); |
| 2015 | conf->psk_identity = NULL; |
| 2016 | conf->psk_identity_len = 0; |
| 2017 | } |
| 2018 | } |
| 2019 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 2020 | /* This function assumes that PSK identity in the SSL config is unset. |
| 2021 | * It checks that the provided identity is well-formed and attempts |
| 2022 | * to make a copy of it in the SSL config. |
| 2023 | * On failure, the PSK identity in the config remains unset. */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 2024 | MBEDTLS_CHECK_RETURN_CRITICAL |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 2025 | static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf, |
| 2026 | unsigned char const *psk_identity, |
| 2027 | size_t psk_identity_len ) |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 2028 | { |
Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 2029 | /* Identity len will be encoded on two bytes */ |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 2030 | if( psk_identity == NULL || |
Ronald Cron | 2a87e9b | 2022-10-19 10:55:26 +0200 | [diff] [blame] | 2031 | psk_identity_len == 0 || |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 2032 | ( psk_identity_len >> 16 ) != 0 || |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 2033 | psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN ) |
Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 2034 | { |
| 2035 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2036 | } |
| 2037 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 2038 | conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ); |
| 2039 | if( conf->psk_identity == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 2040 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 2041 | |
Manuel Pégourié-Gonnard | 120fdbd | 2015-05-07 17:07:50 +0100 | [diff] [blame] | 2042 | conf->psk_identity_len = psk_identity_len; |
Manuel Pégourié-Gonnard | 120fdbd | 2015-05-07 17:07:50 +0100 | [diff] [blame] | 2043 | memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len ); |
Paul Bakker | 5ad403f | 2013-09-18 21:21:30 +0200 | [diff] [blame] | 2044 | |
| 2045 | return( 0 ); |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 2046 | } |
| 2047 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 2048 | int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf, |
| 2049 | const unsigned char *psk, size_t psk_len, |
| 2050 | const unsigned char *psk_identity, size_t psk_identity_len ) |
| 2051 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2052 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 2053 | |
| 2054 | /* We currently only support one PSK, raw or opaque. */ |
Ronald Cron | d29e13e | 2022-10-19 10:33:48 +0200 | [diff] [blame] | 2055 | if( mbedtls_ssl_conf_has_static_psk( conf ) ) |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 2056 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 2057 | |
| 2058 | /* Check and set raw PSK */ |
Piotr Nowicki | 9926eaf | 2019-11-20 14:54:36 +0100 | [diff] [blame] | 2059 | if( psk == NULL ) |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 2060 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Piotr Nowicki | 9926eaf | 2019-11-20 14:54:36 +0100 | [diff] [blame] | 2061 | if( psk_len == 0 ) |
| 2062 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2063 | if( psk_len > MBEDTLS_PSK_MAX_LEN ) |
| 2064 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2065 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 2066 | if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ) |
| 2067 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 2068 | conf->psk_len = psk_len; |
| 2069 | memcpy( conf->psk, psk, conf->psk_len ); |
| 2070 | |
| 2071 | /* Check and set PSK Identity */ |
| 2072 | ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len ); |
| 2073 | if( ret != 0 ) |
| 2074 | ssl_conf_remove_psk( conf ); |
| 2075 | |
| 2076 | return( ret ); |
| 2077 | } |
| 2078 | |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2079 | static void ssl_remove_psk( mbedtls_ssl_context *ssl ) |
| 2080 | { |
| 2081 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2082 | if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) ) |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2083 | { |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 2084 | /* The maintenance of the external PSK key slot is the |
| 2085 | * user's responsibility. */ |
| 2086 | if( ssl->handshake->psk_opaque_is_internal ) |
| 2087 | { |
| 2088 | psa_destroy_key( ssl->handshake->psk_opaque ); |
| 2089 | ssl->handshake->psk_opaque_is_internal = 0; |
| 2090 | } |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2091 | ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT; |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2092 | } |
Neil Armstrong | e952a30 | 2022-05-03 10:22:14 +0200 | [diff] [blame] | 2093 | #else |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2094 | if( ssl->handshake->psk != NULL ) |
| 2095 | { |
| 2096 | mbedtls_platform_zeroize( ssl->handshake->psk, |
| 2097 | ssl->handshake->psk_len ); |
| 2098 | mbedtls_free( ssl->handshake->psk ); |
| 2099 | ssl->handshake->psk_len = 0; |
| 2100 | } |
Neil Armstrong | e952a30 | 2022-05-03 10:22:14 +0200 | [diff] [blame] | 2101 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2102 | } |
| 2103 | |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 2104 | int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl, |
| 2105 | const unsigned char *psk, size_t psk_len ) |
| 2106 | { |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 2107 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 2108 | psa_key_attributes_t key_attributes = psa_key_attributes_init(); |
Jerry Yu | 5d01c05 | 2022-08-17 10:18:10 +0800 | [diff] [blame] | 2109 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Jerry Yu | 24b8c81 | 2022-08-20 19:06:56 +0800 | [diff] [blame] | 2110 | psa_algorithm_t alg = PSA_ALG_NONE; |
Jerry Yu | 5d01c05 | 2022-08-17 10:18:10 +0800 | [diff] [blame] | 2111 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 2112 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 2113 | |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 2114 | if( psk == NULL || ssl->handshake == NULL ) |
| 2115 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2116 | |
| 2117 | if( psk_len > MBEDTLS_PSK_MAX_LEN ) |
| 2118 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2119 | |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2120 | ssl_remove_psk( ssl ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 2121 | |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 2122 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Jerry Yu | ccc68a4 | 2022-07-26 16:39:20 +0800 | [diff] [blame] | 2123 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 2124 | if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 ) |
| 2125 | { |
Jerry Yu | 6cf6b47 | 2022-08-16 14:50:28 +0800 | [diff] [blame] | 2126 | if( ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384 ) |
Jerry Yu | ccc68a4 | 2022-07-26 16:39:20 +0800 | [diff] [blame] | 2127 | alg = PSA_ALG_TLS12_PSK_TO_MS( PSA_ALG_SHA_384 ); |
| 2128 | else |
| 2129 | alg = PSA_ALG_TLS12_PSK_TO_MS( PSA_ALG_SHA_256 ); |
| 2130 | psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE ); |
| 2131 | } |
| 2132 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 2133 | |
Jerry Yu | 568ec25 | 2022-07-22 21:27:34 +0800 | [diff] [blame] | 2134 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | ccc68a4 | 2022-07-26 16:39:20 +0800 | [diff] [blame] | 2135 | if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 ) |
| 2136 | { |
| 2137 | alg = PSA_ALG_HKDF_EXTRACT( PSA_ALG_ANY_HASH ); |
| 2138 | psa_set_key_usage_flags( &key_attributes, |
| 2139 | PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT ); |
| 2140 | } |
| 2141 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 2142 | |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 2143 | psa_set_key_algorithm( &key_attributes, alg ); |
| 2144 | psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE ); |
| 2145 | |
| 2146 | status = psa_import_key( &key_attributes, psk, psk_len, &key ); |
| 2147 | if( status != PSA_SUCCESS ) |
| 2148 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 2149 | |
| 2150 | /* Allow calling psa_destroy_key() on psk remove */ |
| 2151 | ssl->handshake->psk_opaque_is_internal = 1; |
| 2152 | return mbedtls_ssl_set_hs_psk_opaque( ssl, key ); |
| 2153 | #else |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 2154 | if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 2155 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 2156 | |
| 2157 | ssl->handshake->psk_len = psk_len; |
| 2158 | memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len ); |
| 2159 | |
| 2160 | return( 0 ); |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 2161 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 2162 | } |
| 2163 | |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2164 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 2165 | int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf, |
Andrzej Kurek | 03e0146 | 2022-01-03 12:53:24 +0100 | [diff] [blame] | 2166 | mbedtls_svc_key_id_t psk, |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2167 | const unsigned char *psk_identity, |
| 2168 | size_t psk_identity_len ) |
| 2169 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2170 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 2171 | |
| 2172 | /* We currently only support one PSK, raw or opaque. */ |
Ronald Cron | d29e13e | 2022-10-19 10:33:48 +0200 | [diff] [blame] | 2173 | if( mbedtls_ssl_conf_has_static_psk( conf ) ) |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 2174 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2175 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 2176 | /* Check and set opaque PSK */ |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2177 | if( mbedtls_svc_key_id_is_null( psk ) ) |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 2178 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2179 | conf->psk_opaque = psk; |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 2180 | |
| 2181 | /* Check and set PSK Identity */ |
| 2182 | ret = ssl_conf_set_psk_identity( conf, psk_identity, |
| 2183 | psk_identity_len ); |
| 2184 | if( ret != 0 ) |
| 2185 | ssl_conf_remove_psk( conf ); |
| 2186 | |
| 2187 | return( ret ); |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2188 | } |
| 2189 | |
Przemyslaw Stekiel | 6928a51 | 2022-02-03 13:50:35 +0100 | [diff] [blame] | 2190 | int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl, |
| 2191 | mbedtls_svc_key_id_t psk ) |
| 2192 | { |
| 2193 | if( ( mbedtls_svc_key_id_is_null( psk ) ) || |
| 2194 | ( ssl->handshake == NULL ) ) |
| 2195 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2196 | |
| 2197 | ssl_remove_psk( ssl ); |
| 2198 | ssl->handshake->psk_opaque = psk; |
| 2199 | return( 0 ); |
| 2200 | } |
| 2201 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 2202 | |
Jerry Yu | 8897c07 | 2022-08-12 13:56:53 +0800 | [diff] [blame] | 2203 | #if defined(MBEDTLS_SSL_SRV_C) |
Przemyslaw Stekiel | 6928a51 | 2022-02-03 13:50:35 +0100 | [diff] [blame] | 2204 | void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf, |
| 2205 | int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *, |
| 2206 | size_t), |
| 2207 | void *p_psk ) |
| 2208 | { |
| 2209 | conf->f_psk = f_psk; |
| 2210 | conf->p_psk = p_psk; |
| 2211 | } |
Jerry Yu | 8897c07 | 2022-08-12 13:56:53 +0800 | [diff] [blame] | 2212 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 2213 | |
Ronald Cron | 73fe8df | 2022-10-05 14:31:43 +0200 | [diff] [blame] | 2214 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */ |
Przemyslaw Stekiel | 6928a51 | 2022-02-03 13:50:35 +0100 | [diff] [blame] | 2215 | |
| 2216 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 301711e | 2022-04-26 16:57:05 +0200 | [diff] [blame] | 2217 | static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode( |
| 2218 | psa_algorithm_t alg ) |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 2219 | { |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 2220 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2221 | if( alg == PSA_ALG_CBC_NO_PADDING ) |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 2222 | return( MBEDTLS_SSL_MODE_CBC ); |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 2223 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2224 | if( PSA_ALG_IS_AEAD( alg ) ) |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 2225 | return( MBEDTLS_SSL_MODE_AEAD ); |
Gilles Peskine | 301711e | 2022-04-26 16:57:05 +0200 | [diff] [blame] | 2226 | return( MBEDTLS_SSL_MODE_STREAM ); |
| 2227 | } |
| 2228 | |
| 2229 | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
| 2230 | |
| 2231 | static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode( |
| 2232 | mbedtls_cipher_mode_t mode ) |
| 2233 | { |
| 2234 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
| 2235 | if( mode == MBEDTLS_MODE_CBC ) |
| 2236 | return( MBEDTLS_SSL_MODE_CBC ); |
| 2237 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
| 2238 | |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 2239 | #if defined(MBEDTLS_GCM_C) || \ |
| 2240 | defined(MBEDTLS_CCM_C) || \ |
| 2241 | defined(MBEDTLS_CHACHAPOLY_C) |
| 2242 | if( mode == MBEDTLS_MODE_GCM || |
| 2243 | mode == MBEDTLS_MODE_CCM || |
| 2244 | mode == MBEDTLS_MODE_CHACHAPOLY ) |
| 2245 | return( MBEDTLS_SSL_MODE_AEAD ); |
| 2246 | #endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */ |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 2247 | |
| 2248 | return( MBEDTLS_SSL_MODE_STREAM ); |
| 2249 | } |
Gilles Peskine | 301711e | 2022-04-26 16:57:05 +0200 | [diff] [blame] | 2250 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 2251 | |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2252 | static mbedtls_ssl_mode_t mbedtls_ssl_get_actual_mode( |
| 2253 | mbedtls_ssl_mode_t base_mode, |
| 2254 | int encrypt_then_mac ) |
| 2255 | { |
| 2256 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
| 2257 | if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED && |
| 2258 | base_mode == MBEDTLS_SSL_MODE_CBC ) |
| 2259 | { |
| 2260 | return( MBEDTLS_SSL_MODE_CBC_ETM ); |
| 2261 | } |
| 2262 | #else |
| 2263 | (void) encrypt_then_mac; |
| 2264 | #endif |
| 2265 | return( base_mode ); |
| 2266 | } |
| 2267 | |
Neil Armstrong | ab555e0 | 2022-04-04 11:07:59 +0200 | [diff] [blame] | 2268 | mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_transform( |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2269 | const mbedtls_ssl_transform *transform ) |
| 2270 | { |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2271 | mbedtls_ssl_mode_t base_mode = mbedtls_ssl_get_base_mode( |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2272 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2273 | transform->psa_alg |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2274 | #else |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2275 | mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) |
| 2276 | #endif |
| 2277 | ); |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2278 | |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2279 | int encrypt_then_mac = 0; |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 2280 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2281 | encrypt_then_mac = transform->encrypt_then_mac; |
| 2282 | #endif |
| 2283 | return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) ); |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2284 | } |
| 2285 | |
Neil Armstrong | ab555e0 | 2022-04-04 11:07:59 +0200 | [diff] [blame] | 2286 | mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_ciphersuite( |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 2287 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2288 | int encrypt_then_mac, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 2289 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2290 | const mbedtls_ssl_ciphersuite_t *suite ) |
| 2291 | { |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2292 | mbedtls_ssl_mode_t base_mode = MBEDTLS_SSL_MODE_STREAM; |
| 2293 | |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2294 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 2295 | psa_status_t status; |
| 2296 | psa_algorithm_t alg; |
| 2297 | psa_key_type_t type; |
| 2298 | size_t size; |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2299 | status = mbedtls_ssl_cipher_to_psa( suite->cipher, 0, &alg, &type, &size ); |
| 2300 | if( status == PSA_SUCCESS ) |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2301 | base_mode = mbedtls_ssl_get_base_mode( alg ); |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2302 | #else |
| 2303 | const mbedtls_cipher_info_t *cipher = |
| 2304 | mbedtls_cipher_info_from_type( suite->cipher ); |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2305 | if( cipher != NULL ) |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2306 | { |
| 2307 | base_mode = |
| 2308 | mbedtls_ssl_get_base_mode( |
| 2309 | mbedtls_cipher_info_get_mode( cipher ) ); |
| 2310 | } |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2311 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 2312 | |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2313 | #if !defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
| 2314 | int encrypt_then_mac = 0; |
| 2315 | #endif |
| 2316 | return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) ); |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2317 | } |
| 2318 | |
Neil Armstrong | 8395d7a | 2022-05-18 11:44:56 +0200 | [diff] [blame] | 2319 | #if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 2320 | |
| 2321 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2322 | /* Serialization of TLS 1.3 sessions: |
| 2323 | * |
| 2324 | * struct { |
Xiaokang Qian | 126bf8e | 2022-10-13 02:22:40 +0000 | [diff] [blame] | 2325 | * opaque hostname<0..2^16-1>; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2326 | * uint64 ticket_received; |
| 2327 | * uint32 ticket_lifetime; |
Jerry Yu | 3419107 | 2022-08-18 10:32:09 +0800 | [diff] [blame] | 2328 | * opaque ticket<1..2^16-1>; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2329 | * } ClientOnlyData; |
| 2330 | * |
| 2331 | * struct { |
| 2332 | * uint8 endpoint; |
| 2333 | * uint8 ciphersuite[2]; |
| 2334 | * uint32 ticket_age_add; |
| 2335 | * uint8 ticket_flags; |
| 2336 | * opaque resumption_key<0..255>; |
| 2337 | * select ( endpoint ) { |
| 2338 | * case client: ClientOnlyData; |
| 2339 | * case server: uint64 start_time; |
| 2340 | * }; |
| 2341 | * } serialized_session_tls13; |
| 2342 | * |
| 2343 | */ |
| 2344 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 2345 | MBEDTLS_CHECK_RETURN_CRITICAL |
| 2346 | static int ssl_tls13_session_save( const mbedtls_ssl_session *session, |
| 2347 | unsigned char *buf, |
| 2348 | size_t buf_len, |
| 2349 | size_t *olen ) |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2350 | { |
| 2351 | unsigned char *p = buf; |
Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 2352 | #if defined(MBEDTLS_SSL_CLI_C) && \ |
| 2353 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 2354 | size_t hostname_len = ( session->hostname == NULL ) ? |
Xiaokang Qian | ed0620c | 2022-10-12 06:58:13 +0000 | [diff] [blame] | 2355 | 0 : strlen( session->hostname ) + 1; |
Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 2356 | #endif |
Jerry Yu | bc7c1a4 | 2022-07-21 22:57:37 +0800 | [diff] [blame] | 2357 | size_t needed = 1 /* endpoint */ |
| 2358 | + 2 /* ciphersuite */ |
| 2359 | + 4 /* ticket_age_add */ |
Jerry Yu | 3419107 | 2022-08-18 10:32:09 +0800 | [diff] [blame] | 2360 | + 1 /* ticket_flags */ |
| 2361 | + 1; /* resumption_key length */ |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 2362 | *olen = 0; |
Jerry Yu | 3419107 | 2022-08-18 10:32:09 +0800 | [diff] [blame] | 2363 | |
| 2364 | if( session->resumption_key_len > MBEDTLS_SSL_TLS1_3_TICKET_RESUMPTION_KEY_LEN ) |
| 2365 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2366 | needed += session->resumption_key_len; /* resumption_key */ |
| 2367 | |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2368 | #if defined(MBEDTLS_HAVE_TIME) |
| 2369 | needed += 8; /* start_time or ticket_received */ |
| 2370 | #endif |
| 2371 | |
| 2372 | #if defined(MBEDTLS_SSL_CLI_C) |
| 2373 | if( session->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 2374 | { |
Xiaokang Qian | 126bf8e | 2022-10-13 02:22:40 +0000 | [diff] [blame] | 2375 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Xiaokang Qian | bc663a0 | 2022-10-09 11:14:39 +0000 | [diff] [blame] | 2376 | needed += 2 /* hostname_len */ |
Xiaokang Qian | 2f9efd3 | 2022-10-10 11:24:08 +0000 | [diff] [blame] | 2377 | + hostname_len; /* hostname */ |
Xiaokang Qian | 281fd1b | 2022-09-20 11:35:41 +0000 | [diff] [blame] | 2378 | #endif |
| 2379 | |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2380 | needed += 4 /* ticket_lifetime */ |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 2381 | + 2; /* ticket_len */ |
Jerry Yu | 3419107 | 2022-08-18 10:32:09 +0800 | [diff] [blame] | 2382 | |
| 2383 | /* Check size_t overflow */ |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 2384 | if( session->ticket_len > SIZE_MAX - needed ) |
| 2385 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jerry Yu | 3419107 | 2022-08-18 10:32:09 +0800 | [diff] [blame] | 2386 | |
Jerry Yu | e28d974 | 2022-08-18 15:44:03 +0800 | [diff] [blame] | 2387 | needed += session->ticket_len; /* ticket */ |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2388 | } |
| 2389 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 2390 | |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 2391 | *olen = needed; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2392 | if( needed > buf_len ) |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 2393 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2394 | |
| 2395 | p[0] = session->endpoint; |
| 2396 | MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 1 ); |
| 2397 | MBEDTLS_PUT_UINT32_BE( session->ticket_age_add, p, 3 ); |
| 2398 | p[7] = session->ticket_flags; |
| 2399 | |
| 2400 | /* save resumption_key */ |
Jerry Yu | bc7c1a4 | 2022-07-21 22:57:37 +0800 | [diff] [blame] | 2401 | p[8] = session->resumption_key_len; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2402 | p += 9; |
Jerry Yu | bc7c1a4 | 2022-07-21 22:57:37 +0800 | [diff] [blame] | 2403 | memcpy( p, session->resumption_key, session->resumption_key_len ); |
| 2404 | p += session->resumption_key_len; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2405 | |
| 2406 | #if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C) |
| 2407 | if( session->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 2408 | { |
| 2409 | MBEDTLS_PUT_UINT64_BE( (uint64_t) session->start, p, 0 ); |
| 2410 | p += 8; |
| 2411 | } |
| 2412 | #endif /* MBEDTLS_HAVE_TIME */ |
| 2413 | |
| 2414 | #if defined(MBEDTLS_SSL_CLI_C) |
| 2415 | if( session->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 2416 | { |
Xiaokang Qian | ed0620c | 2022-10-12 06:58:13 +0000 | [diff] [blame] | 2417 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 2418 | MBEDTLS_PUT_UINT16_BE( hostname_len, p, 0 ); |
| 2419 | p += 2; |
Xiaokang Qian | 126bf8e | 2022-10-13 02:22:40 +0000 | [diff] [blame] | 2420 | if( hostname_len > 0 ) |
Xiaokang Qian | ed0620c | 2022-10-12 06:58:13 +0000 | [diff] [blame] | 2421 | { |
| 2422 | /* save host name */ |
| 2423 | memcpy( p, session->hostname, hostname_len ); |
| 2424 | p += hostname_len; |
| 2425 | } |
| 2426 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
| 2427 | |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2428 | #if defined(MBEDTLS_HAVE_TIME) |
| 2429 | MBEDTLS_PUT_UINT64_BE( (uint64_t) session->ticket_received, p, 0 ); |
| 2430 | p += 8; |
| 2431 | #endif |
| 2432 | MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 ); |
| 2433 | p += 4; |
| 2434 | |
| 2435 | MBEDTLS_PUT_UINT16_BE( session->ticket_len, p, 0 ); |
| 2436 | p += 2; |
Jerry Yu | 3419107 | 2022-08-18 10:32:09 +0800 | [diff] [blame] | 2437 | |
| 2438 | if( session->ticket != NULL && session->ticket_len > 0 ) |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2439 | { |
| 2440 | memcpy( p, session->ticket, session->ticket_len ); |
| 2441 | p += session->ticket_len; |
| 2442 | } |
| 2443 | } |
| 2444 | #endif /* MBEDTLS_SSL_CLI_C */ |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 2445 | return( 0 ); |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2446 | } |
| 2447 | |
| 2448 | MBEDTLS_CHECK_RETURN_CRITICAL |
| 2449 | static int ssl_tls13_session_load( mbedtls_ssl_session *session, |
| 2450 | const unsigned char *buf, |
| 2451 | size_t len ) |
| 2452 | { |
| 2453 | const unsigned char *p = buf; |
| 2454 | const unsigned char *end = buf + len; |
| 2455 | |
| 2456 | if( end - p < 9 ) |
| 2457 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2458 | session->endpoint = p[0]; |
| 2459 | session->ciphersuite = MBEDTLS_GET_UINT16_BE( p, 1 ); |
| 2460 | session->ticket_age_add = MBEDTLS_GET_UINT32_BE( p, 3 ); |
| 2461 | session->ticket_flags = p[7]; |
| 2462 | |
| 2463 | /* load resumption_key */ |
Jerry Yu | bc7c1a4 | 2022-07-21 22:57:37 +0800 | [diff] [blame] | 2464 | session->resumption_key_len = p[8]; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2465 | p += 9; |
| 2466 | |
Jerry Yu | bc7c1a4 | 2022-07-21 22:57:37 +0800 | [diff] [blame] | 2467 | if( end - p < session->resumption_key_len ) |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2468 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2469 | |
Jerry Yu | bc7c1a4 | 2022-07-21 22:57:37 +0800 | [diff] [blame] | 2470 | if( sizeof( session->resumption_key ) < session->resumption_key_len ) |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2471 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jerry Yu | bc7c1a4 | 2022-07-21 22:57:37 +0800 | [diff] [blame] | 2472 | memcpy( session->resumption_key, p, session->resumption_key_len ); |
| 2473 | p += session->resumption_key_len; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2474 | |
| 2475 | #if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C) |
| 2476 | if( session->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 2477 | { |
| 2478 | if( end - p < 8 ) |
| 2479 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2480 | session->start = MBEDTLS_GET_UINT64_BE( p, 0 ); |
| 2481 | p += 8; |
| 2482 | } |
| 2483 | #endif /* MBEDTLS_HAVE_TIME */ |
| 2484 | |
| 2485 | #if defined(MBEDTLS_SSL_CLI_C) |
| 2486 | if( session->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 2487 | { |
Xiaokang Qian | ed0620c | 2022-10-12 06:58:13 +0000 | [diff] [blame] | 2488 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \ |
| 2489 | defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 2490 | size_t hostname_len; |
| 2491 | /* load host name */ |
| 2492 | if( end - p < 2 ) |
| 2493 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2494 | hostname_len = MBEDTLS_GET_UINT16_BE( p, 0 ); |
| 2495 | p += 2; |
| 2496 | |
| 2497 | if( end - p < ( long int )hostname_len ) |
| 2498 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2499 | if( hostname_len > 0 ) |
| 2500 | { |
| 2501 | session->hostname = mbedtls_calloc( 1, hostname_len ); |
| 2502 | if( session->hostname == NULL ) |
| 2503 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 2504 | memcpy( session->hostname, p, hostname_len ); |
| 2505 | p += hostname_len; |
| 2506 | } |
| 2507 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION && |
| 2508 | MBEDTLS_SSL_SESSION_TICKETS */ |
| 2509 | |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2510 | #if defined(MBEDTLS_HAVE_TIME) |
| 2511 | if( end - p < 8 ) |
| 2512 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2513 | session->ticket_received = MBEDTLS_GET_UINT64_BE( p, 0 ); |
| 2514 | p += 8; |
| 2515 | #endif |
| 2516 | if( end - p < 4 ) |
| 2517 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2518 | session->ticket_lifetime = MBEDTLS_GET_UINT32_BE( p, 0 ); |
| 2519 | p += 4; |
| 2520 | |
| 2521 | if( end - p < 2 ) |
| 2522 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2523 | session->ticket_len = MBEDTLS_GET_UINT16_BE( p, 0 ); |
| 2524 | p += 2; |
| 2525 | |
| 2526 | if( end - p < ( long int )session->ticket_len ) |
| 2527 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2528 | if( session->ticket_len > 0 ) |
| 2529 | { |
| 2530 | session->ticket = mbedtls_calloc( 1, session->ticket_len ); |
| 2531 | if( session->ticket == NULL ) |
| 2532 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 2533 | memcpy( session->ticket, p, session->ticket_len ); |
| 2534 | p += session->ticket_len; |
| 2535 | } |
| 2536 | } |
| 2537 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 2538 | |
| 2539 | return( 0 ); |
| 2540 | |
| 2541 | } |
| 2542 | #else /* MBEDTLS_SSL_SESSION_TICKETS */ |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 2543 | MBEDTLS_CHECK_RETURN_CRITICAL |
| 2544 | static int ssl_tls13_session_save( const mbedtls_ssl_session *session, |
| 2545 | unsigned char *buf, |
| 2546 | size_t buf_len, |
| 2547 | size_t *olen ) |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 2548 | { |
| 2549 | ((void) session); |
| 2550 | ((void) buf); |
| 2551 | ((void) buf_len); |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 2552 | *olen = 0; |
| 2553 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 2554 | } |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2555 | |
| 2556 | static int ssl_tls13_session_load( const mbedtls_ssl_session *session, |
| 2557 | unsigned char *buf, |
| 2558 | size_t buf_len ) |
| 2559 | { |
| 2560 | ((void) session); |
| 2561 | ((void) buf); |
| 2562 | ((void) buf_len); |
| 2563 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 2564 | } |
| 2565 | #endif /* !MBEDTLS_SSL_SESSION_TICKETS */ |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 2566 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 2567 | |
Przemyslaw Stekiel | f57b456 | 2022-01-25 00:04:18 +0100 | [diff] [blame] | 2568 | 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] | 2569 | size_t taglen, |
| 2570 | psa_algorithm_t *alg, |
| 2571 | psa_key_type_t *key_type, |
| 2572 | size_t *key_size ) |
| 2573 | { |
| 2574 | switch ( mbedtls_cipher_type ) |
| 2575 | { |
| 2576 | case MBEDTLS_CIPHER_AES_128_CBC: |
| 2577 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 2578 | *key_type = PSA_KEY_TYPE_AES; |
| 2579 | *key_size = 128; |
| 2580 | break; |
| 2581 | case MBEDTLS_CIPHER_AES_128_CCM: |
| 2582 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 2583 | *key_type = PSA_KEY_TYPE_AES; |
| 2584 | *key_size = 128; |
| 2585 | break; |
| 2586 | case MBEDTLS_CIPHER_AES_128_GCM: |
| 2587 | *alg = PSA_ALG_GCM; |
| 2588 | *key_type = PSA_KEY_TYPE_AES; |
| 2589 | *key_size = 128; |
| 2590 | break; |
| 2591 | case MBEDTLS_CIPHER_AES_192_CCM: |
| 2592 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 2593 | *key_type = PSA_KEY_TYPE_AES; |
| 2594 | *key_size = 192; |
| 2595 | break; |
| 2596 | case MBEDTLS_CIPHER_AES_192_GCM: |
| 2597 | *alg = PSA_ALG_GCM; |
| 2598 | *key_type = PSA_KEY_TYPE_AES; |
| 2599 | *key_size = 192; |
| 2600 | break; |
| 2601 | case MBEDTLS_CIPHER_AES_256_CBC: |
| 2602 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 2603 | *key_type = PSA_KEY_TYPE_AES; |
| 2604 | *key_size = 256; |
| 2605 | break; |
| 2606 | case MBEDTLS_CIPHER_AES_256_CCM: |
| 2607 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 2608 | *key_type = PSA_KEY_TYPE_AES; |
| 2609 | *key_size = 256; |
| 2610 | break; |
| 2611 | case MBEDTLS_CIPHER_AES_256_GCM: |
| 2612 | *alg = PSA_ALG_GCM; |
| 2613 | *key_type = PSA_KEY_TYPE_AES; |
| 2614 | *key_size = 256; |
| 2615 | break; |
| 2616 | case MBEDTLS_CIPHER_ARIA_128_CBC: |
| 2617 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 2618 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2619 | *key_size = 128; |
| 2620 | break; |
| 2621 | case MBEDTLS_CIPHER_ARIA_128_CCM: |
| 2622 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 2623 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2624 | *key_size = 128; |
| 2625 | break; |
| 2626 | case MBEDTLS_CIPHER_ARIA_128_GCM: |
| 2627 | *alg = PSA_ALG_GCM; |
| 2628 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2629 | *key_size = 128; |
| 2630 | break; |
| 2631 | case MBEDTLS_CIPHER_ARIA_192_CCM: |
| 2632 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 2633 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2634 | *key_size = 192; |
| 2635 | break; |
| 2636 | case MBEDTLS_CIPHER_ARIA_192_GCM: |
| 2637 | *alg = PSA_ALG_GCM; |
| 2638 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2639 | *key_size = 192; |
| 2640 | break; |
| 2641 | case MBEDTLS_CIPHER_ARIA_256_CBC: |
| 2642 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 2643 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2644 | *key_size = 256; |
| 2645 | break; |
| 2646 | case MBEDTLS_CIPHER_ARIA_256_CCM: |
| 2647 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 2648 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2649 | *key_size = 256; |
| 2650 | break; |
| 2651 | case MBEDTLS_CIPHER_ARIA_256_GCM: |
| 2652 | *alg = PSA_ALG_GCM; |
| 2653 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2654 | *key_size = 256; |
| 2655 | break; |
| 2656 | case MBEDTLS_CIPHER_CAMELLIA_128_CBC: |
| 2657 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 2658 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2659 | *key_size = 128; |
| 2660 | break; |
| 2661 | case MBEDTLS_CIPHER_CAMELLIA_128_CCM: |
| 2662 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 2663 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2664 | *key_size = 128; |
| 2665 | break; |
| 2666 | case MBEDTLS_CIPHER_CAMELLIA_128_GCM: |
| 2667 | *alg = PSA_ALG_GCM; |
| 2668 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2669 | *key_size = 128; |
| 2670 | break; |
| 2671 | case MBEDTLS_CIPHER_CAMELLIA_192_CCM: |
| 2672 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 2673 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2674 | *key_size = 192; |
| 2675 | break; |
| 2676 | case MBEDTLS_CIPHER_CAMELLIA_192_GCM: |
| 2677 | *alg = PSA_ALG_GCM; |
| 2678 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2679 | *key_size = 192; |
| 2680 | break; |
| 2681 | case MBEDTLS_CIPHER_CAMELLIA_256_CBC: |
| 2682 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 2683 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2684 | *key_size = 256; |
| 2685 | break; |
| 2686 | case MBEDTLS_CIPHER_CAMELLIA_256_CCM: |
| 2687 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 2688 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2689 | *key_size = 256; |
| 2690 | break; |
| 2691 | case MBEDTLS_CIPHER_CAMELLIA_256_GCM: |
| 2692 | *alg = PSA_ALG_GCM; |
| 2693 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2694 | *key_size = 256; |
| 2695 | break; |
| 2696 | case MBEDTLS_CIPHER_CHACHA20_POLY1305: |
| 2697 | *alg = PSA_ALG_CHACHA20_POLY1305; |
| 2698 | *key_type = PSA_KEY_TYPE_CHACHA20; |
| 2699 | *key_size = 256; |
| 2700 | break; |
| 2701 | case MBEDTLS_CIPHER_NULL: |
| 2702 | *alg = MBEDTLS_SSL_NULL_CIPHER; |
| 2703 | *key_type = 0; |
| 2704 | *key_size = 0; |
| 2705 | break; |
| 2706 | default: |
| 2707 | return PSA_ERROR_NOT_SUPPORTED; |
| 2708 | } |
| 2709 | |
| 2710 | return PSA_SUCCESS; |
| 2711 | } |
Neil Armstrong | 8395d7a | 2022-05-18 11:44:56 +0200 | [diff] [blame] | 2712 | #endif /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2713 | |
Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 2714 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 2715 | int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf, |
| 2716 | const unsigned char *dhm_P, size_t P_len, |
| 2717 | const unsigned char *dhm_G, size_t G_len ) |
| 2718 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2719 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 2720 | |
Glenn Strauss | cee1129 | 2021-12-20 01:43:17 -0500 | [diff] [blame] | 2721 | mbedtls_mpi_free( &conf->dhm_P ); |
| 2722 | mbedtls_mpi_free( &conf->dhm_G ); |
| 2723 | |
Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 2724 | if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 || |
| 2725 | ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 ) |
| 2726 | { |
| 2727 | mbedtls_mpi_free( &conf->dhm_P ); |
| 2728 | mbedtls_mpi_free( &conf->dhm_G ); |
| 2729 | return( ret ); |
| 2730 | } |
| 2731 | |
| 2732 | return( 0 ); |
| 2733 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2734 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2735 | 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] | 2736 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2737 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 2738 | |
Glenn Strauss | cee1129 | 2021-12-20 01:43:17 -0500 | [diff] [blame] | 2739 | mbedtls_mpi_free( &conf->dhm_P ); |
| 2740 | mbedtls_mpi_free( &conf->dhm_G ); |
| 2741 | |
Gilles Peskine | e570248 | 2021-06-11 21:59:08 +0200 | [diff] [blame] | 2742 | if( ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_P, |
| 2743 | &conf->dhm_P ) ) != 0 || |
| 2744 | ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_G, |
| 2745 | &conf->dhm_G ) ) != 0 ) |
Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 2746 | { |
| 2747 | mbedtls_mpi_free( &conf->dhm_P ); |
| 2748 | mbedtls_mpi_free( &conf->dhm_G ); |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 2749 | return( ret ); |
Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 2750 | } |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 2751 | |
| 2752 | return( 0 ); |
| 2753 | } |
Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 2754 | #endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */ |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 2755 | |
Manuel Pégourié-Gonnard | bd990d6 | 2015-06-11 14:49:42 +0200 | [diff] [blame] | 2756 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C) |
| 2757 | /* |
| 2758 | * Set the minimum length for Diffie-Hellman parameters |
| 2759 | */ |
| 2760 | void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf, |
| 2761 | unsigned int bitlen ) |
| 2762 | { |
| 2763 | conf->dhm_min_bitlen = bitlen; |
| 2764 | } |
| 2765 | #endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */ |
| 2766 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 2767 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Jerry Yu | 7ddc38c | 2022-01-19 11:08:05 +0800 | [diff] [blame] | 2768 | #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] | 2769 | /* |
| 2770 | * Set allowed/preferred hashes for handshake signatures |
| 2771 | */ |
| 2772 | void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf, |
| 2773 | const int *hashes ) |
| 2774 | { |
| 2775 | conf->sig_hashes = hashes; |
| 2776 | } |
Jerry Yu | 7ddc38c | 2022-01-19 11:08:05 +0800 | [diff] [blame] | 2777 | #endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */ |
Hanno Becker | 1cd6e00 | 2021-08-10 13:27:10 +0100 | [diff] [blame] | 2778 | |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 2779 | /* Configure allowed signature algorithms for handshake */ |
Hanno Becker | 1cd6e00 | 2021-08-10 13:27:10 +0100 | [diff] [blame] | 2780 | void mbedtls_ssl_conf_sig_algs( mbedtls_ssl_config *conf, |
| 2781 | const uint16_t* sig_algs ) |
| 2782 | { |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 2783 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 2784 | conf->sig_hashes = NULL; |
| 2785 | #endif /* !MBEDTLS_DEPRECATED_REMOVED */ |
| 2786 | conf->sig_algs = sig_algs; |
Hanno Becker | 1cd6e00 | 2021-08-10 13:27:10 +0100 | [diff] [blame] | 2787 | } |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 2788 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Manuel Pégourié-Gonnard | 36a8b57 | 2015-06-17 12:43:26 +0200 | [diff] [blame] | 2789 | |
Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 2790 | #if defined(MBEDTLS_ECP_C) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 2791 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 2792 | /* |
| 2793 | * Set the allowed elliptic curves |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 2794 | * |
| 2795 | * mbedtls_ssl_setup() takes the provided list |
| 2796 | * and translates it to a list of IANA TLS group identifiers, |
| 2797 | * stored in ssl->handshake->group_list. |
| 2798 | * |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 2799 | */ |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2800 | void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2801 | const mbedtls_ecp_group_id *curve_list ) |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 2802 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2803 | conf->curve_list = curve_list; |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 2804 | conf->group_list = NULL; |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 2805 | } |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 2806 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2807 | #endif /* MBEDTLS_ECP_C */ |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 2808 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 2809 | /* |
| 2810 | * Set the allowed groups |
| 2811 | */ |
| 2812 | void mbedtls_ssl_conf_groups( mbedtls_ssl_config *conf, |
| 2813 | const uint16_t *group_list ) |
| 2814 | { |
| 2815 | #if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 2816 | conf->curve_list = NULL; |
| 2817 | #endif |
| 2818 | conf->group_list = group_list; |
| 2819 | } |
| 2820 | |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 2821 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2822 | int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2823 | { |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2824 | /* Initialize to suppress unnecessary compiler warning */ |
| 2825 | size_t hostname_len = 0; |
| 2826 | |
| 2827 | /* Check if new hostname is valid before |
| 2828 | * making any change to current one */ |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2829 | if( hostname != NULL ) |
| 2830 | { |
| 2831 | hostname_len = strlen( hostname ); |
| 2832 | |
| 2833 | if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN ) |
| 2834 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2835 | } |
| 2836 | |
| 2837 | /* Now it's clear that we will overwrite the old hostname, |
| 2838 | * so we can free it safely */ |
| 2839 | |
| 2840 | if( ssl->hostname != NULL ) |
| 2841 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 2842 | mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) ); |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2843 | mbedtls_free( ssl->hostname ); |
| 2844 | } |
| 2845 | |
| 2846 | /* Passing NULL as hostname shall clear the old one */ |
Manuel Pégourié-Gonnard | ba26c24 | 2015-05-06 10:47:06 +0100 | [diff] [blame] | 2847 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2848 | if( hostname == NULL ) |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2849 | { |
| 2850 | ssl->hostname = NULL; |
| 2851 | } |
| 2852 | else |
| 2853 | { |
| 2854 | ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 ); |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2855 | if( ssl->hostname == NULL ) |
| 2856 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Paul Bakker | 75c1a6f | 2013-08-19 14:25:29 +0200 | [diff] [blame] | 2857 | |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2858 | memcpy( ssl->hostname, hostname, hostname_len ); |
Paul Bakker | 75c1a6f | 2013-08-19 14:25:29 +0200 | [diff] [blame] | 2859 | |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2860 | ssl->hostname[hostname_len] = '\0'; |
| 2861 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2862 | |
| 2863 | return( 0 ); |
| 2864 | } |
Hanno Becker | 1a9a51c | 2017-04-07 13:02:16 +0100 | [diff] [blame] | 2865 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2866 | |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 2867 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2868 | void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2869 | int (*f_sni)(void *, mbedtls_ssl_context *, |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 2870 | const unsigned char *, size_t), |
| 2871 | void *p_sni ) |
| 2872 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2873 | conf->f_sni = f_sni; |
| 2874 | conf->p_sni = p_sni; |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 2875 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2876 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 2877 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2878 | #if defined(MBEDTLS_SSL_ALPN) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2879 | 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] | 2880 | { |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 2881 | size_t cur_len, tot_len; |
| 2882 | const char **p; |
| 2883 | |
| 2884 | /* |
Brian J Murray | 1903fb3 | 2016-11-06 04:45:15 -0800 | [diff] [blame] | 2885 | * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings |
| 2886 | * MUST NOT be truncated." |
| 2887 | * We check lengths now rather than later. |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 2888 | */ |
| 2889 | tot_len = 0; |
| 2890 | for( p = protos; *p != NULL; p++ ) |
| 2891 | { |
| 2892 | cur_len = strlen( *p ); |
| 2893 | tot_len += cur_len; |
| 2894 | |
Ronald Cron | 8216dd3 | 2020-04-23 16:41:44 +0200 | [diff] [blame] | 2895 | if( ( cur_len == 0 ) || |
| 2896 | ( cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN ) || |
| 2897 | ( tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN ) ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2898 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 2899 | } |
| 2900 | |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2901 | conf->alpn_list = protos; |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 2902 | |
| 2903 | return( 0 ); |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 2904 | } |
| 2905 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2906 | 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] | 2907 | { |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 2908 | return( ssl->alpn_chosen ); |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 2909 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2910 | #endif /* MBEDTLS_SSL_ALPN */ |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 2911 | |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2912 | #if defined(MBEDTLS_SSL_DTLS_SRTP) |
Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 2913 | void mbedtls_ssl_conf_srtp_mki_value_supported( mbedtls_ssl_config *conf, |
| 2914 | int support_mki_value ) |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 2915 | { |
| 2916 | conf->dtls_srtp_mki_support = support_mki_value; |
| 2917 | } |
| 2918 | |
Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 2919 | int mbedtls_ssl_dtls_srtp_set_mki_value( mbedtls_ssl_context *ssl, |
| 2920 | unsigned char *mki_value, |
Johan Pascal | f6417ec | 2020-09-22 15:15:19 +0200 | [diff] [blame] | 2921 | uint16_t mki_len ) |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 2922 | { |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 2923 | if( mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH ) |
Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 2924 | { |
Johan Pascal | d576fdb | 2020-09-22 10:39:53 +0200 | [diff] [blame] | 2925 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 2926 | } |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 2927 | |
| 2928 | 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] | 2929 | { |
Johan Pascal | d576fdb | 2020-09-22 10:39:53 +0200 | [diff] [blame] | 2930 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 2931 | } |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 2932 | |
| 2933 | memcpy( ssl->dtls_srtp_info.mki_value, mki_value, mki_len ); |
| 2934 | ssl->dtls_srtp_info.mki_len = mki_len; |
Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 2935 | return( 0 ); |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 2936 | } |
| 2937 | |
Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 2938 | int mbedtls_ssl_conf_dtls_srtp_protection_profiles( mbedtls_ssl_config *conf, |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 2939 | const mbedtls_ssl_srtp_profile *profiles ) |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2940 | { |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 2941 | const mbedtls_ssl_srtp_profile *p; |
| 2942 | size_t list_size = 0; |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2943 | |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 2944 | /* check the profiles list: all entry must be valid, |
| 2945 | * 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] | 2946 | for( p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET && |
| 2947 | list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH; |
| 2948 | p++ ) |
Johan Pascal | d576fdb | 2020-09-22 10:39:53 +0200 | [diff] [blame] | 2949 | { |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 2950 | if( mbedtls_ssl_check_srtp_profile_value( *p ) != MBEDTLS_TLS_SRTP_UNSET ) |
Johan Pascal | d576fdb | 2020-09-22 10:39:53 +0200 | [diff] [blame] | 2951 | { |
Johan Pascal | 76fdf1d | 2020-10-22 23:31:00 +0200 | [diff] [blame] | 2952 | list_size++; |
| 2953 | } |
| 2954 | else |
| 2955 | { |
| 2956 | /* unsupported value, stop parsing and set the size to an error value */ |
| 2957 | list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1; |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2958 | } |
| 2959 | } |
| 2960 | |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 2961 | if( list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH ) |
Johan Pascal | d387aa0 | 2020-09-23 18:47:56 +0200 | [diff] [blame] | 2962 | { |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 2963 | conf->dtls_srtp_profile_list = NULL; |
| 2964 | conf->dtls_srtp_profile_list_len = 0; |
| 2965 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2966 | } |
| 2967 | |
Johan Pascal | 9bc97ca | 2020-09-21 23:44:45 +0200 | [diff] [blame] | 2968 | conf->dtls_srtp_profile_list = profiles; |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 2969 | conf->dtls_srtp_profile_list_len = list_size; |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2970 | |
| 2971 | return( 0 ); |
| 2972 | } |
| 2973 | |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 2974 | void mbedtls_ssl_get_dtls_srtp_negotiation_result( const mbedtls_ssl_context *ssl, |
| 2975 | mbedtls_dtls_srtp_info *dtls_srtp_info ) |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2976 | { |
Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 2977 | dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile; |
| 2978 | /* do not copy the mki value if there is no chosen profile */ |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 2979 | if( dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET ) |
Johan Pascal | 0dbcd1d | 2020-10-28 11:03:07 +0100 | [diff] [blame] | 2980 | { |
Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 2981 | dtls_srtp_info->mki_len = 0; |
Johan Pascal | 0dbcd1d | 2020-10-28 11:03:07 +0100 | [diff] [blame] | 2982 | } |
Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 2983 | else |
| 2984 | { |
| 2985 | dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len; |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 2986 | memcpy( dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value, |
| 2987 | ssl->dtls_srtp_info.mki_len ); |
Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 2988 | } |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2989 | } |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2990 | #endif /* MBEDTLS_SSL_DTLS_SRTP */ |
| 2991 | |
Aditya Patwardhan | 3096f33 | 2022-07-26 14:31:46 +0530 | [diff] [blame] | 2992 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 2993 | 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] | 2994 | { |
Glenn Strauss | 2dfcea2 | 2022-03-14 17:26:42 -0400 | [diff] [blame] | 2995 | conf->max_tls_version = (major << 8) | minor; |
Paul Bakker | 490ecc8 | 2011-10-06 13:04:09 +0000 | [diff] [blame] | 2996 | } |
| 2997 | |
Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 2998 | 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] | 2999 | { |
Glenn Strauss | 2dfcea2 | 2022-03-14 17:26:42 -0400 | [diff] [blame] | 3000 | conf->min_tls_version = (major << 8) | minor; |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 3001 | } |
Aditya Patwardhan | 3096f33 | 2022-07-26 14:31:46 +0530 | [diff] [blame] | 3002 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 3003 | |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 3004 | #if defined(MBEDTLS_SSL_SRV_C) |
| 3005 | void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf, |
| 3006 | char cert_req_ca_list ) |
| 3007 | { |
| 3008 | conf->cert_req_ca_list = cert_req_ca_list; |
| 3009 | } |
| 3010 | #endif |
| 3011 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3012 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 3013 | 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] | 3014 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 3015 | conf->encrypt_then_mac = etm; |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 3016 | } |
| 3017 | #endif |
| 3018 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3019 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 3020 | 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] | 3021 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 3022 | conf->extended_ms = ems; |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 3023 | } |
| 3024 | #endif |
| 3025 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3026 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 3027 | 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] | 3028 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3029 | if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID || |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3030 | 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] | 3031 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3032 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 3033 | } |
| 3034 | |
Manuel Pégourié-Gonnard | 6bf89d6 | 2015-05-05 17:01:57 +0100 | [diff] [blame] | 3035 | conf->mfl_code = mfl_code; |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 3036 | |
| 3037 | return( 0 ); |
| 3038 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3039 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 3040 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 3041 | 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] | 3042 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 3043 | conf->allow_legacy_renegotiation = allow_legacy; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3044 | } |
| 3045 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3046 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 3047 | 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] | 3048 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 3049 | conf->disable_renegotiation = renegotiation; |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 3050 | } |
| 3051 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 3052 | 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] | 3053 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 3054 | conf->renego_max_records = max_records; |
Manuel Pégourié-Gonnard | a9964db | 2014-07-03 19:29:16 +0200 | [diff] [blame] | 3055 | } |
| 3056 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 3057 | void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 837f0fe | 2014-11-05 13:58:53 +0100 | [diff] [blame] | 3058 | const unsigned char period[8] ) |
| 3059 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 3060 | memcpy( conf->renego_period, period, 8 ); |
Manuel Pégourié-Gonnard | 837f0fe | 2014-11-05 13:58:53 +0100 | [diff] [blame] | 3061 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3062 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3063 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3064 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 3065 | #if defined(MBEDTLS_SSL_CLI_C) |
| 3066 | 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] | 3067 | { |
Manuel Pégourié-Gonnard | 2b49445 | 2015-05-06 10:05:11 +0100 | [diff] [blame] | 3068 | conf->session_tickets = use_tickets; |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 3069 | } |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 3070 | #endif |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 3071 | |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 3072 | #if defined(MBEDTLS_SSL_SRV_C) |
Jerry Yu | 1ad7ace | 2022-08-09 13:28:39 +0800 | [diff] [blame] | 3073 | |
Jerry Yu | d0766ec | 2022-09-22 10:46:57 +0800 | [diff] [blame] | 3074 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS) |
Jerry Yu | 1ad7ace | 2022-08-09 13:28:39 +0800 | [diff] [blame] | 3075 | void mbedtls_ssl_conf_new_session_tickets( mbedtls_ssl_config *conf, |
| 3076 | uint16_t num_tickets ) |
| 3077 | { |
Jerry Yu | d0766ec | 2022-09-22 10:46:57 +0800 | [diff] [blame] | 3078 | conf->new_session_tickets_count = num_tickets; |
Jerry Yu | 1ad7ace | 2022-08-09 13:28:39 +0800 | [diff] [blame] | 3079 | } |
| 3080 | #endif |
| 3081 | |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 3082 | void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf, |
| 3083 | mbedtls_ssl_ticket_write_t *f_ticket_write, |
| 3084 | mbedtls_ssl_ticket_parse_t *f_ticket_parse, |
| 3085 | void *p_ticket ) |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 3086 | { |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 3087 | conf->f_ticket_write = f_ticket_write; |
| 3088 | conf->f_ticket_parse = f_ticket_parse; |
| 3089 | conf->p_ticket = p_ticket; |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 3090 | } |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 3091 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3092 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 3093 | |
Hanno Becker | 7e6c178 | 2021-06-08 09:24:55 +0100 | [diff] [blame] | 3094 | void mbedtls_ssl_set_export_keys_cb( mbedtls_ssl_context *ssl, |
| 3095 | mbedtls_ssl_export_keys_t *f_export_keys, |
| 3096 | void *p_export_keys ) |
Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 3097 | { |
Hanno Becker | 7e6c178 | 2021-06-08 09:24:55 +0100 | [diff] [blame] | 3098 | ssl->f_export_keys = f_export_keys; |
| 3099 | ssl->p_export_keys = p_export_keys; |
Ron Eldor | f5cc10d | 2019-05-07 18:33:40 +0300 | [diff] [blame] | 3100 | } |
Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 3101 | |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 3102 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 3103 | void mbedtls_ssl_conf_async_private_cb( |
| 3104 | mbedtls_ssl_config *conf, |
| 3105 | mbedtls_ssl_async_sign_t *f_async_sign, |
| 3106 | mbedtls_ssl_async_decrypt_t *f_async_decrypt, |
| 3107 | mbedtls_ssl_async_resume_t *f_async_resume, |
| 3108 | mbedtls_ssl_async_cancel_t *f_async_cancel, |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3109 | void *async_config_data ) |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 3110 | { |
| 3111 | conf->f_async_sign_start = f_async_sign; |
| 3112 | conf->f_async_decrypt_start = f_async_decrypt; |
| 3113 | conf->f_async_resume = f_async_resume; |
| 3114 | conf->f_async_cancel = f_async_cancel; |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3115 | conf->p_async_config_data = async_config_data; |
| 3116 | } |
| 3117 | |
Gilles Peskine | 8f97af7 | 2018-04-26 11:46:10 +0200 | [diff] [blame] | 3118 | void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf ) |
| 3119 | { |
| 3120 | return( conf->p_async_config_data ); |
| 3121 | } |
| 3122 | |
Gilles Peskine | 1febfef | 2018-04-30 11:54:39 +0200 | [diff] [blame] | 3123 | void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl ) |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3124 | { |
| 3125 | if( ssl->handshake == NULL ) |
| 3126 | return( NULL ); |
| 3127 | else |
| 3128 | return( ssl->handshake->user_async_ctx ); |
| 3129 | } |
| 3130 | |
Gilles Peskine | 1febfef | 2018-04-30 11:54:39 +0200 | [diff] [blame] | 3131 | void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl, |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3132 | void *ctx ) |
| 3133 | { |
| 3134 | if( ssl->handshake != NULL ) |
| 3135 | ssl->handshake->user_async_ctx = ctx; |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 3136 | } |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 3137 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 3138 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3139 | /* |
| 3140 | * SSL get accessors |
| 3141 | */ |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 3142 | uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3143 | { |
Manuel Pégourié-Gonnard | e89163c | 2015-01-23 14:30:57 +0000 | [diff] [blame] | 3144 | if( ssl->session != NULL ) |
| 3145 | return( ssl->session->verify_result ); |
| 3146 | |
| 3147 | if( ssl->session_negotiate != NULL ) |
| 3148 | return( ssl->session_negotiate->verify_result ); |
| 3149 | |
Manuel Pégourié-Gonnard | 6ab9b00 | 2015-05-14 11:25:04 +0200 | [diff] [blame] | 3150 | return( 0xFFFFFFFF ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3151 | } |
| 3152 | |
Glenn Strauss | 8f52690 | 2022-01-13 00:04:49 -0500 | [diff] [blame] | 3153 | int mbedtls_ssl_get_ciphersuite_id_from_ssl( const mbedtls_ssl_context *ssl ) |
| 3154 | { |
| 3155 | if( ssl == NULL || ssl->session == NULL ) |
| 3156 | return( 0 ); |
| 3157 | |
| 3158 | return( ssl->session->ciphersuite ); |
| 3159 | } |
| 3160 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3161 | const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl ) |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 3162 | { |
Paul Bakker | 926c8e4 | 2013-03-06 10:23:34 +0100 | [diff] [blame] | 3163 | if( ssl == NULL || ssl->session == NULL ) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 3164 | return( NULL ); |
Paul Bakker | 926c8e4 | 2013-03-06 10:23:34 +0100 | [diff] [blame] | 3165 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3166 | return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite ); |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 3167 | } |
| 3168 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3169 | const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl ) |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 3170 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3171 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3172 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 3173 | { |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 3174 | switch( ssl->tls_version ) |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 3175 | { |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 3176 | case MBEDTLS_SSL_VERSION_TLS1_2: |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 3177 | return( "DTLSv1.2" ); |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 3178 | default: |
| 3179 | return( "unknown (DTLS)" ); |
| 3180 | } |
| 3181 | } |
| 3182 | #endif |
| 3183 | |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 3184 | switch( ssl->tls_version ) |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 3185 | { |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 3186 | case MBEDTLS_SSL_VERSION_TLS1_2: |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3187 | return( "TLSv1.2" ); |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 3188 | case MBEDTLS_SSL_VERSION_TLS1_3: |
Gilles Peskine | c63a1e0 | 2022-01-13 01:10:24 +0100 | [diff] [blame] | 3189 | return( "TLSv1.3" ); |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 3190 | default: |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 3191 | return( "unknown" ); |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 3192 | } |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 3193 | } |
| 3194 | |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 3195 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 3196 | size_t mbedtls_ssl_get_input_max_frag_len( const mbedtls_ssl_context *ssl ) |
| 3197 | { |
David Horstmann | 95d516f | 2021-05-04 18:36:56 +0100 | [diff] [blame] | 3198 | size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN; |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 3199 | size_t read_mfl; |
| 3200 | |
Jerry Yu | ddda050 | 2022-12-01 19:43:12 +0800 | [diff] [blame] | 3201 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 3202 | /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */ |
| 3203 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
| 3204 | ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE ) |
| 3205 | { |
| 3206 | return ssl_mfl_code_to_length( ssl->conf->mfl_code ); |
| 3207 | } |
Jerry Yu | ddda050 | 2022-12-01 19:43:12 +0800 | [diff] [blame] | 3208 | #endif |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 3209 | |
| 3210 | /* Check if a smaller max length was negotiated */ |
| 3211 | if( ssl->session_out != NULL ) |
| 3212 | { |
| 3213 | read_mfl = ssl_mfl_code_to_length( ssl->session_out->mfl_code ); |
| 3214 | if( read_mfl < max_len ) |
| 3215 | { |
| 3216 | max_len = read_mfl; |
| 3217 | } |
| 3218 | } |
| 3219 | |
Jerry Yu | ddda050 | 2022-12-01 19:43:12 +0800 | [diff] [blame] | 3220 | /* During a handshake, use the value being negotiated */ |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 3221 | if( ssl->session_negotiate != NULL ) |
| 3222 | { |
| 3223 | read_mfl = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ); |
| 3224 | if( read_mfl < max_len ) |
| 3225 | { |
| 3226 | max_len = read_mfl; |
| 3227 | } |
| 3228 | } |
| 3229 | |
| 3230 | return( max_len ); |
| 3231 | } |
| 3232 | |
| 3233 | 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] | 3234 | { |
| 3235 | size_t max_len; |
| 3236 | |
| 3237 | /* |
| 3238 | * Assume mfl_code is correct since it was checked when set |
| 3239 | */ |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3240 | 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] | 3241 | |
Manuel Pégourié-Gonnard | 2cb17e2 | 2017-09-19 13:00:47 +0200 | [diff] [blame] | 3242 | /* Check if a smaller max length was negotiated */ |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 3243 | if( ssl->session_out != NULL && |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3244 | 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] | 3245 | { |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3246 | 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] | 3247 | } |
| 3248 | |
Manuel Pégourié-Gonnard | 2cb17e2 | 2017-09-19 13:00:47 +0200 | [diff] [blame] | 3249 | /* During a handshake, use the value being negotiated */ |
| 3250 | if( ssl->session_negotiate != NULL && |
| 3251 | ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len ) |
| 3252 | { |
| 3253 | max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ); |
| 3254 | } |
| 3255 | |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3256 | return( max_len ); |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 3257 | } |
| 3258 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
| 3259 | |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 3260 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 8949071 | 2020-02-05 10:50:12 +0000 | [diff] [blame] | 3261 | 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] | 3262 | { |
Andrzej Kurek | ef43ce6 | 2018-10-09 08:24:12 -0400 | [diff] [blame] | 3263 | /* Return unlimited mtu for client hello messages to avoid fragmentation. */ |
| 3264 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
| 3265 | ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO || |
| 3266 | ssl->state == MBEDTLS_SSL_SERVER_HELLO ) ) |
| 3267 | return ( 0 ); |
| 3268 | |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 3269 | if( ssl->handshake == NULL || ssl->handshake->mtu == 0 ) |
| 3270 | return( ssl->mtu ); |
| 3271 | |
| 3272 | if( ssl->mtu == 0 ) |
| 3273 | return( ssl->handshake->mtu ); |
| 3274 | |
| 3275 | return( ssl->mtu < ssl->handshake->mtu ? |
| 3276 | ssl->mtu : ssl->handshake->mtu ); |
| 3277 | } |
| 3278 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 3279 | |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3280 | int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl ) |
| 3281 | { |
| 3282 | size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN; |
| 3283 | |
Manuel Pégourié-Gonnard | 000281e | 2018-08-21 11:20:58 +0200 | [diff] [blame] | 3284 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \ |
| 3285 | !defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3286 | (void) ssl; |
| 3287 | #endif |
| 3288 | |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3289 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 3290 | 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] | 3291 | |
| 3292 | if( max_len > mfl ) |
| 3293 | max_len = mfl; |
| 3294 | #endif |
| 3295 | |
| 3296 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 8949071 | 2020-02-05 10:50:12 +0000 | [diff] [blame] | 3297 | if( mbedtls_ssl_get_current_mtu( ssl ) != 0 ) |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3298 | { |
Hanno Becker | 8949071 | 2020-02-05 10:50:12 +0000 | [diff] [blame] | 3299 | const size_t mtu = mbedtls_ssl_get_current_mtu( ssl ); |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3300 | const int ret = mbedtls_ssl_get_record_expansion( ssl ); |
| 3301 | const size_t overhead = (size_t) ret; |
| 3302 | |
| 3303 | if( ret < 0 ) |
| 3304 | return( ret ); |
| 3305 | |
| 3306 | if( mtu <= overhead ) |
| 3307 | { |
| 3308 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) ); |
| 3309 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 3310 | } |
| 3311 | |
| 3312 | if( max_len > mtu - overhead ) |
| 3313 | max_len = mtu - overhead; |
| 3314 | } |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 3315 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3316 | |
Hanno Becker | 0defedb | 2018-08-10 12:35:02 +0100 | [diff] [blame] | 3317 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \ |
| 3318 | !defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3319 | ((void) ssl); |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3320 | #endif |
| 3321 | |
| 3322 | return( (int) max_len ); |
| 3323 | } |
| 3324 | |
Hanno Becker | 2d8e99b | 2021-04-21 06:19:50 +0100 | [diff] [blame] | 3325 | int mbedtls_ssl_get_max_in_record_payload( const mbedtls_ssl_context *ssl ) |
| 3326 | { |
| 3327 | size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN; |
| 3328 | |
| 3329 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 3330 | (void) ssl; |
| 3331 | #endif |
| 3332 | |
| 3333 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 3334 | const size_t mfl = mbedtls_ssl_get_input_max_frag_len( ssl ); |
| 3335 | |
| 3336 | if( max_len > mfl ) |
| 3337 | max_len = mfl; |
| 3338 | #endif |
| 3339 | |
| 3340 | return( (int) max_len ); |
| 3341 | } |
| 3342 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3343 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 3344 | 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] | 3345 | { |
| 3346 | if( ssl == NULL || ssl->session == NULL ) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 3347 | return( NULL ); |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 3348 | |
Hanno Becker | e682457 | 2019-02-07 13:18:46 +0000 | [diff] [blame] | 3349 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 3350 | return( ssl->session->peer_cert ); |
Hanno Becker | e682457 | 2019-02-07 13:18:46 +0000 | [diff] [blame] | 3351 | #else |
| 3352 | return( NULL ); |
| 3353 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 3354 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3355 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 3356 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3357 | #if defined(MBEDTLS_SSL_CLI_C) |
Hanno Becker | f852b1c | 2019-02-05 11:42:30 +0000 | [diff] [blame] | 3358 | int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, |
| 3359 | mbedtls_ssl_session *dst ) |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 3360 | { |
Hanno Becker | e810bbc | 2021-05-14 16:01:05 +0100 | [diff] [blame] | 3361 | int ret; |
| 3362 | |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 3363 | if( ssl == NULL || |
| 3364 | dst == NULL || |
| 3365 | ssl->session == NULL || |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3366 | ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT ) |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 3367 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3368 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 3369 | } |
| 3370 | |
Hanno Becker | e810bbc | 2021-05-14 16:01:05 +0100 | [diff] [blame] | 3371 | /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer |
| 3372 | * idempotent: Each session can only be exported once. |
| 3373 | * |
| 3374 | * (This is in preparation for TLS 1.3 support where we will |
| 3375 | * need the ability to export multiple sessions (aka tickets), |
| 3376 | * which will be achieved by calling mbedtls_ssl_get_session() |
| 3377 | * multiple times until it fails.) |
| 3378 | * |
| 3379 | * Check whether we have already exported the current session, |
| 3380 | * and fail if so. |
| 3381 | */ |
| 3382 | if( ssl->session->exported == 1 ) |
| 3383 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 3384 | |
| 3385 | ret = mbedtls_ssl_session_copy( dst, ssl->session ); |
| 3386 | if( ret != 0 ) |
| 3387 | return( ret ); |
| 3388 | |
| 3389 | /* Remember that we've exported the session. */ |
| 3390 | ssl->session->exported = 1; |
| 3391 | return( 0 ); |
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 | #endif /* MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 3394 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3395 | /* |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 3396 | * Define ticket header determining Mbed TLS version |
| 3397 | * and structure of the ticket. |
| 3398 | */ |
| 3399 | |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3400 | /* |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 3401 | * Define bitflag determining compile-time settings influencing |
| 3402 | * structure of serialized SSL sessions. |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3403 | */ |
| 3404 | |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 3405 | #if defined(MBEDTLS_HAVE_TIME) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3406 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME 1 |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 3407 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3408 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3409 | #endif /* MBEDTLS_HAVE_TIME */ |
| 3410 | |
| 3411 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3412 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3413 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3414 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3415 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 3416 | |
| 3417 | #if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3418 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3419 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3420 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3421 | #endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */ |
| 3422 | |
| 3423 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3424 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3425 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3426 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3427 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
| 3428 | |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3429 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3430 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3431 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3432 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3433 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ |
| 3434 | |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3435 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 3436 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1 |
| 3437 | #else |
| 3438 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0 |
| 3439 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
| 3440 | |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3441 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0 |
| 3442 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1 |
| 3443 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2 |
| 3444 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3 |
Hanno Becker | 37bdbe6 | 2021-08-01 05:38:58 +0100 | [diff] [blame] | 3445 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4 |
| 3446 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5 |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3447 | |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 3448 | #define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \ |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3449 | ( (uint16_t) ( \ |
| 3450 | ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \ |
| 3451 | ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \ |
| 3452 | ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \ |
| 3453 | ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \ |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3454 | ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \ |
Hanno Becker | be34e8e | 2019-06-04 09:43:16 +0100 | [diff] [blame] | 3455 | ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) ) ) |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3456 | |
Hanno Becker | f878707 | 2019-05-16 12:41:07 +0100 | [diff] [blame] | 3457 | static unsigned char ssl_serialized_session_header[] = { |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3458 | MBEDTLS_VERSION_MAJOR, |
| 3459 | MBEDTLS_VERSION_MINOR, |
| 3460 | MBEDTLS_VERSION_PATCH, |
Joe Subbiani | 2194dc4 | 2021-07-14 12:31:31 +0100 | [diff] [blame] | 3461 | MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ), |
| 3462 | MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ), |
Hanno Becker | f878707 | 2019-05-16 12:41:07 +0100 | [diff] [blame] | 3463 | }; |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 3464 | |
| 3465 | /* |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 3466 | * Serialize a session in the following format: |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 3467 | * (in the presentation language of TLS, RFC 8446 section 3) |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 3468 | * |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3469 | * struct { |
Hanno Becker | dc28b6c | 2019-05-29 11:08:00 +0100 | [diff] [blame] | 3470 | * |
Hanno Becker | dce5097 | 2021-08-01 05:39:23 +0100 | [diff] [blame] | 3471 | * opaque mbedtls_version[3]; // library version: major, minor, patch |
| 3472 | * opaque session_format[2]; // library-version specific 16-bit field |
| 3473 | * // determining the format of the remaining |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3474 | * // serialized data. |
Hanno Becker | dc28b6c | 2019-05-29 11:08:00 +0100 | [diff] [blame] | 3475 | * |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3476 | * Note: When updating the format, remember to keep |
| 3477 | * these version+format bytes. |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 3478 | * |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3479 | * // In this version, `session_format` determines |
| 3480 | * // the setting of those compile-time |
| 3481 | * // configuration options which influence |
| 3482 | * // the structure of mbedtls_ssl_session. |
| 3483 | * |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 3484 | * uint8_t minor_ver; // Protocol minor version. Possible values: |
Jerry Yu | 0c2a738 | 2022-12-06 13:27:25 +0800 | [diff] [blame^] | 3485 | * // - TLS 1.2 (0x0303) |
| 3486 | * // - TLS 1.3 (0x0304) |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3487 | * |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 3488 | * select (serialized_session.tls_version) { |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3489 | * |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 3490 | * case MBEDTLS_SSL_VERSION_TLS1_2: |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3491 | * serialized_session_tls12 data; |
Jerry Yu | 0c2a738 | 2022-12-06 13:27:25 +0800 | [diff] [blame^] | 3492 | * case MBEDTLS_SSL_VERSION_TLS1_3: |
Jerry Yu | ddda050 | 2022-12-01 19:43:12 +0800 | [diff] [blame] | 3493 | * serialized_session_tls13 data; |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3494 | * |
| 3495 | * }; |
| 3496 | * |
| 3497 | * } serialized_session; |
| 3498 | * |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 3499 | */ |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3500 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 3501 | MBEDTLS_CHECK_RETURN_CRITICAL |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3502 | static int ssl_session_save( const mbedtls_ssl_session *session, |
| 3503 | unsigned char omit_header, |
| 3504 | unsigned char *buf, |
| 3505 | size_t buf_len, |
| 3506 | size_t *olen ) |
| 3507 | { |
| 3508 | unsigned char *p = buf; |
| 3509 | size_t used = 0; |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 3510 | size_t remaining_len; |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 3511 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 3512 | size_t out_len; |
| 3513 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 3514 | #endif |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 3515 | if( session == NULL ) |
| 3516 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 3517 | |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3518 | if( !omit_header ) |
| 3519 | { |
| 3520 | /* |
| 3521 | * Add Mbed TLS version identifier |
| 3522 | */ |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3523 | used += sizeof( ssl_serialized_session_header ); |
| 3524 | |
| 3525 | if( used <= buf_len ) |
| 3526 | { |
| 3527 | memcpy( p, ssl_serialized_session_header, |
| 3528 | sizeof( ssl_serialized_session_header ) ); |
| 3529 | p += sizeof( ssl_serialized_session_header ); |
| 3530 | } |
| 3531 | } |
| 3532 | |
| 3533 | /* |
| 3534 | * TLS version identifier |
| 3535 | */ |
| 3536 | used += 1; |
| 3537 | if( used <= buf_len ) |
| 3538 | { |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 3539 | *p++ = MBEDTLS_BYTE_0( session->tls_version ); |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3540 | } |
| 3541 | |
| 3542 | /* Forward to version-specific serialization routine. */ |
Jerry Yu | fca4d57 | 2022-07-21 10:37:48 +0800 | [diff] [blame] | 3543 | remaining_len = (buf_len >= used) ? buf_len - used : 0; |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 3544 | switch( session->tls_version ) |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3545 | { |
| 3546 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 3547 | case MBEDTLS_SSL_VERSION_TLS1_2: |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 3548 | used += ssl_tls12_session_save( session, p, remaining_len ); |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3549 | break; |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3550 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 3551 | |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 3552 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 3553 | case MBEDTLS_SSL_VERSION_TLS1_3: |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 3554 | ret = ssl_tls13_session_save( session, p, remaining_len, &out_len ); |
| 3555 | if( ret != 0 && ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ) |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 3556 | return( ret ); |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 3557 | used += out_len; |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 3558 | break; |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 3559 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 3560 | |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3561 | default: |
| 3562 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 3563 | } |
| 3564 | |
| 3565 | *olen = used; |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 3566 | if( used > buf_len ) |
| 3567 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 3568 | |
| 3569 | return( 0 ); |
| 3570 | } |
| 3571 | |
| 3572 | /* |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 3573 | * Public wrapper for ssl_session_save() |
| 3574 | */ |
| 3575 | int mbedtls_ssl_session_save( const mbedtls_ssl_session *session, |
| 3576 | unsigned char *buf, |
| 3577 | size_t buf_len, |
| 3578 | size_t *olen ) |
| 3579 | { |
| 3580 | return( ssl_session_save( session, 0, buf, buf_len, olen ) ); |
| 3581 | } |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 3582 | |
Jerry Yu | f1b23ca | 2022-02-18 11:48:47 +0800 | [diff] [blame] | 3583 | /* |
| 3584 | * Deserialize session, see mbedtls_ssl_session_save() for format. |
| 3585 | * |
| 3586 | * This internal version is wrapped by a public function that cleans up in |
| 3587 | * case of error, and has an extra option omit_header. |
| 3588 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 3589 | MBEDTLS_CHECK_RETURN_CRITICAL |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3590 | static int ssl_session_load( mbedtls_ssl_session *session, |
| 3591 | unsigned char omit_header, |
| 3592 | const unsigned char *buf, |
| 3593 | size_t len ) |
| 3594 | { |
| 3595 | const unsigned char *p = buf; |
| 3596 | const unsigned char * const end = buf + len; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 3597 | size_t remaining_len; |
| 3598 | |
| 3599 | |
| 3600 | if( session == NULL ) |
| 3601 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3602 | |
| 3603 | if( !omit_header ) |
| 3604 | { |
| 3605 | /* |
| 3606 | * Check Mbed TLS version identifier |
| 3607 | */ |
| 3608 | |
| 3609 | if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) ) |
| 3610 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3611 | |
| 3612 | if( memcmp( p, ssl_serialized_session_header, |
| 3613 | sizeof( ssl_serialized_session_header ) ) != 0 ) |
| 3614 | { |
| 3615 | return( MBEDTLS_ERR_SSL_VERSION_MISMATCH ); |
| 3616 | } |
| 3617 | p += sizeof( ssl_serialized_session_header ); |
| 3618 | } |
| 3619 | |
| 3620 | /* |
| 3621 | * TLS version identifier |
| 3622 | */ |
| 3623 | if( 1 > (size_t)( end - p ) ) |
| 3624 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 3625 | session->tls_version = 0x0300 | *p++; |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3626 | |
| 3627 | /* Dispatch according to TLS version. */ |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 3628 | remaining_len = ( end - p ); |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 3629 | switch( session->tls_version ) |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3630 | { |
| 3631 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 3632 | case MBEDTLS_SSL_VERSION_TLS1_2: |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 3633 | return( ssl_tls12_session_load( session, p, remaining_len ) ); |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3634 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 3635 | |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 3636 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 3637 | case MBEDTLS_SSL_VERSION_TLS1_3: |
| 3638 | return( ssl_tls13_session_load( session, p, remaining_len ) ); |
| 3639 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 3640 | |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3641 | default: |
| 3642 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3643 | } |
| 3644 | } |
| 3645 | |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 3646 | /* |
Manuel Pégourié-Gonnard | b9dfc9f | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 3647 | * Deserialize session: public wrapper for error cleaning |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 3648 | */ |
| 3649 | int mbedtls_ssl_session_load( mbedtls_ssl_session *session, |
| 3650 | const unsigned char *buf, |
| 3651 | size_t len ) |
| 3652 | { |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 3653 | int ret = ssl_session_load( session, 0, buf, len ); |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 3654 | |
| 3655 | if( ret != 0 ) |
| 3656 | mbedtls_ssl_session_free( session ); |
| 3657 | |
| 3658 | return( ret ); |
| 3659 | } |
| 3660 | |
| 3661 | /* |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3662 | * Perform a single step of the SSL handshake |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3663 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 3664 | MBEDTLS_CHECK_RETURN_CRITICAL |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 3665 | static int ssl_prepare_handshake_step( mbedtls_ssl_context *ssl ) |
| 3666 | { |
| 3667 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 3668 | |
Ronald Cron | 66dbf91 | 2022-02-02 15:33:46 +0100 | [diff] [blame] | 3669 | /* |
| 3670 | * 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] | 3671 | * that were written into the output buffer by the previous handshake step, |
| 3672 | * if the write to the network callback returned with the |
Ronald Cron | 66dbf91 | 2022-02-02 15:33:46 +0100 | [diff] [blame] | 3673 | * #MBEDTLS_ERR_SSL_WANT_WRITE error code. |
| 3674 | * We proceed to the next handshake step only when all data from the |
| 3675 | * previous one have been sent to the peer, thus we make sure that this is |
| 3676 | * the case here by calling `mbedtls_ssl_flush_output()`. The function may |
| 3677 | * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case |
| 3678 | * we have to wait before to go ahead. |
| 3679 | * In the case of TLS 1.3, handshake step handlers do not send data to the |
| 3680 | * peer. Data are only sent here and through |
| 3681 | * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an |
Andrzej Kurek | 5c65c57 | 2022-04-13 14:28:52 -0400 | [diff] [blame] | 3682 | * alert occurred. |
Ronald Cron | 66dbf91 | 2022-02-02 15:33:46 +0100 | [diff] [blame] | 3683 | */ |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 3684 | if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) |
| 3685 | return( ret ); |
| 3686 | |
| 3687 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3688 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 3689 | ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING ) |
| 3690 | { |
| 3691 | if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 ) |
| 3692 | return( ret ); |
| 3693 | } |
| 3694 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 3695 | |
| 3696 | return( ret ); |
| 3697 | } |
| 3698 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3699 | int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3700 | { |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 3701 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3702 | |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 3703 | if( ssl == NULL || |
| 3704 | ssl->conf == NULL || |
| 3705 | ssl->handshake == NULL || |
Jerry Yu | 1fb3299 | 2022-10-27 13:18:19 +0800 | [diff] [blame] | 3706 | ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER ) |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 3707 | { |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 3708 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 3709 | } |
| 3710 | |
| 3711 | ret = ssl_prepare_handshake_step( ssl ); |
| 3712 | if( ret != 0 ) |
| 3713 | return( ret ); |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 3714 | |
Jerry Yu | e704781 | 2021-09-13 19:26:39 +0800 | [diff] [blame] | 3715 | ret = mbedtls_ssl_handle_pending_alert( ssl ); |
| 3716 | if( ret != 0 ) |
| 3717 | goto cleanup; |
| 3718 | |
Tom Cosgrove | 2fdc7b3 | 2022-09-21 12:33:17 +0100 | [diff] [blame] | 3719 | /* If ssl->conf->endpoint is not one of MBEDTLS_SSL_IS_CLIENT or |
| 3720 | * MBEDTLS_SSL_IS_SERVER, this is the return code we give */ |
| 3721 | ret = MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3722 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3723 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3724 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 3725 | { |
Ronald Cron | 27c85e7 | 2022-03-08 11:37:55 +0100 | [diff] [blame] | 3726 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "client state: %s", |
| 3727 | mbedtls_ssl_states_str( ssl->state ) ) ); |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 3728 | |
Ronald Cron | 9f0fba3 | 2022-02-10 16:45:15 +0100 | [diff] [blame] | 3729 | switch( ssl->state ) |
| 3730 | { |
| 3731 | case MBEDTLS_SSL_HELLO_REQUEST: |
| 3732 | ssl->state = MBEDTLS_SSL_CLIENT_HELLO; |
Tom Cosgrove | 87d9c6c | 2022-09-22 09:27:56 +0100 | [diff] [blame] | 3733 | ret = 0; |
Ronald Cron | 9f0fba3 | 2022-02-10 16:45:15 +0100 | [diff] [blame] | 3734 | break; |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 3735 | |
Ronald Cron | 9f0fba3 | 2022-02-10 16:45:15 +0100 | [diff] [blame] | 3736 | case MBEDTLS_SSL_CLIENT_HELLO: |
| 3737 | ret = mbedtls_ssl_write_client_hello( ssl ); |
| 3738 | break; |
| 3739 | |
| 3740 | default: |
| 3741 | #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] | 3742 | if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 ) |
Ronald Cron | 9f0fba3 | 2022-02-10 16:45:15 +0100 | [diff] [blame] | 3743 | ret = mbedtls_ssl_tls13_handshake_client_step( ssl ); |
| 3744 | else |
| 3745 | ret = mbedtls_ssl_handshake_client_step( ssl ); |
| 3746 | #elif defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 3747 | ret = mbedtls_ssl_handshake_client_step( ssl ); |
| 3748 | #else |
| 3749 | ret = mbedtls_ssl_tls13_handshake_client_step( ssl ); |
| 3750 | #endif |
| 3751 | } |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 3752 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3753 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3754 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3755 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 3756 | { |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3757 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 3758 | if( mbedtls_ssl_conf_is_tls13_only( ssl->conf ) ) |
Jerry Yu | 2756193 | 2021-08-27 17:07:38 +0800 | [diff] [blame] | 3759 | ret = mbedtls_ssl_tls13_handshake_server_step( ssl ); |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3760 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 3761 | |
| 3762 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 3763 | if( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) ) |
| 3764 | ret = mbedtls_ssl_handshake_server_step( ssl ); |
| 3765 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 3766 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3767 | #endif |
| 3768 | |
Jerry Yu | e704781 | 2021-09-13 19:26:39 +0800 | [diff] [blame] | 3769 | if( ret != 0 ) |
| 3770 | { |
Jerry Yu | bbd5a3f | 2021-09-18 20:50:22 +0800 | [diff] [blame] | 3771 | /* handshake_step return error. And it is same |
| 3772 | * with alert_reason. |
| 3773 | */ |
Jerry Yu | 3bf1f97 | 2021-09-22 21:37:18 +0800 | [diff] [blame] | 3774 | if( ssl->send_alert ) |
Jerry Yu | e704781 | 2021-09-13 19:26:39 +0800 | [diff] [blame] | 3775 | { |
Jerry Yu | 3bf1f97 | 2021-09-22 21:37:18 +0800 | [diff] [blame] | 3776 | ret = mbedtls_ssl_handle_pending_alert( ssl ); |
Jerry Yu | e704781 | 2021-09-13 19:26:39 +0800 | [diff] [blame] | 3777 | goto cleanup; |
| 3778 | } |
| 3779 | } |
| 3780 | |
| 3781 | cleanup: |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3782 | return( ret ); |
| 3783 | } |
| 3784 | |
| 3785 | /* |
| 3786 | * Perform the SSL handshake |
| 3787 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3788 | int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl ) |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3789 | { |
| 3790 | int ret = 0; |
| 3791 | |
Hanno Becker | a817ea4 | 2020-10-20 15:20:23 +0100 | [diff] [blame] | 3792 | /* Sanity checks */ |
| 3793 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 3794 | if( ssl == NULL || ssl->conf == NULL ) |
| 3795 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3796 | |
Hanno Becker | a817ea4 | 2020-10-20 15:20:23 +0100 | [diff] [blame] | 3797 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3798 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 3799 | ( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL ) ) |
| 3800 | { |
| 3801 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use " |
| 3802 | "mbedtls_ssl_set_timer_cb() for DTLS" ) ); |
| 3803 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3804 | } |
| 3805 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 3806 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3807 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3808 | |
Hanno Becker | a817ea4 | 2020-10-20 15:20:23 +0100 | [diff] [blame] | 3809 | /* Main handshake loop */ |
Jerry Yu | 1fb3299 | 2022-10-27 13:18:19 +0800 | [diff] [blame] | 3810 | while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3811 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3812 | ret = mbedtls_ssl_handshake_step( ssl ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3813 | |
| 3814 | if( ret != 0 ) |
| 3815 | break; |
| 3816 | } |
| 3817 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3818 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3819 | |
| 3820 | return( ret ); |
| 3821 | } |
| 3822 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3823 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 3824 | #if defined(MBEDTLS_SSL_SRV_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3825 | /* |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3826 | * Write HelloRequest to request renegotiation on server |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3827 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 3828 | MBEDTLS_CHECK_RETURN_CRITICAL |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3829 | static int ssl_write_hello_request( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3830 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3831 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3832 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3833 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) ); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3834 | |
| 3835 | ssl->out_msglen = 4; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3836 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 3837 | ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST; |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3838 | |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3839 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3840 | { |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3841 | 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] | 3842 | return( ret ); |
| 3843 | } |
| 3844 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3845 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) ); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3846 | |
| 3847 | return( 0 ); |
| 3848 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3849 | #endif /* MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3850 | |
| 3851 | /* |
| 3852 | * Actually renegotiate current connection, triggered by either: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3853 | * - any side: calling mbedtls_ssl_renegotiate(), |
| 3854 | * - client: receiving a HelloRequest during mbedtls_ssl_read(), |
| 3855 | * - 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] | 3856 | * the initial handshake is completed. |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3857 | * 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] | 3858 | * 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] | 3859 | */ |
Hanno Becker | 40cdaa1 | 2020-02-05 10:48:27 +0000 | [diff] [blame] | 3860 | int mbedtls_ssl_start_renegotiation( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3861 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3862 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3863 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3864 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3865 | |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3866 | if( ( ret = ssl_handshake_init( ssl ) ) != 0 ) |
| 3867 | return( ret ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3868 | |
Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 3869 | /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and |
| 3870 | * the ServerHello will have message_seq = 1" */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3871 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3872 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3873 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ) |
Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 3874 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3875 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 3876 | ssl->handshake->out_msg_seq = 1; |
| 3877 | else |
| 3878 | ssl->handshake->in_msg_seq = 1; |
Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 3879 | } |
| 3880 | #endif |
| 3881 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3882 | ssl->state = MBEDTLS_SSL_HELLO_REQUEST; |
| 3883 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3884 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3885 | if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3886 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3887 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3888 | return( ret ); |
| 3889 | } |
| 3890 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3891 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3892 | |
| 3893 | return( 0 ); |
| 3894 | } |
| 3895 | |
| 3896 | /* |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3897 | * Renegotiate current connection on client, |
| 3898 | * or request renegotiation on server |
| 3899 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3900 | int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3901 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3902 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3903 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 3904 | if( ssl == NULL || ssl->conf == NULL ) |
| 3905 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3906 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3907 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3908 | /* On server, just send the request */ |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3909 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3910 | { |
Jerry Yu | 6848a61 | 2022-10-27 13:03:26 +0800 | [diff] [blame] | 3911 | if( mbedtls_ssl_is_handshake_over( ssl ) == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3912 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3913 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3914 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING; |
Manuel Pégourié-Gonnard | f07f421 | 2014-08-15 19:04:47 +0200 | [diff] [blame] | 3915 | |
| 3916 | /* Did we already try/start sending HelloRequest? */ |
| 3917 | if( ssl->out_left != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3918 | return( mbedtls_ssl_flush_output( ssl ) ); |
Manuel Pégourié-Gonnard | f07f421 | 2014-08-15 19:04:47 +0200 | [diff] [blame] | 3919 | |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3920 | return( ssl_write_hello_request( ssl ) ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3921 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3922 | #endif /* MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3923 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3924 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3925 | /* |
| 3926 | * On client, either start the renegotiation process or, |
| 3927 | * if already in progress, continue the handshake |
| 3928 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3929 | if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3930 | { |
Jerry Yu | 6848a61 | 2022-10-27 13:03:26 +0800 | [diff] [blame] | 3931 | if( mbedtls_ssl_is_handshake_over( ssl ) == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3932 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3933 | |
Hanno Becker | 40cdaa1 | 2020-02-05 10:48:27 +0000 | [diff] [blame] | 3934 | if( ( ret = mbedtls_ssl_start_renegotiation( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3935 | { |
Hanno Becker | 40cdaa1 | 2020-02-05 10:48:27 +0000 | [diff] [blame] | 3936 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_start_renegotiation", ret ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3937 | return( ret ); |
| 3938 | } |
| 3939 | } |
| 3940 | else |
| 3941 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3942 | if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 ) |
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 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3945 | return( ret ); |
| 3946 | } |
| 3947 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3948 | #endif /* MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3949 | |
Paul Bakker | 37ce0ff | 2013-10-31 14:32:04 +0100 | [diff] [blame] | 3950 | return( ret ); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3951 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3952 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3953 | |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 3954 | void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3955 | { |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 3956 | mbedtls_ssl_handshake_params *handshake = ssl->handshake; |
| 3957 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3958 | if( handshake == NULL ) |
| 3959 | return; |
| 3960 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 3961 | #if defined(MBEDTLS_ECP_C) |
| 3962 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 3963 | if ( ssl->handshake->group_list_heap_allocated ) |
| 3964 | mbedtls_free( (void*) handshake->group_list ); |
| 3965 | handshake->group_list = NULL; |
| 3966 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
| 3967 | #endif /* MBEDTLS_ECP_C */ |
| 3968 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 3969 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 3970 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 3971 | if ( ssl->handshake->sig_algs_heap_allocated ) |
| 3972 | mbedtls_free( (void*) handshake->sig_algs ); |
| 3973 | handshake->sig_algs = NULL; |
| 3974 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
Xiaofei Bai | c234ecf | 2022-02-08 09:59:23 +0000 | [diff] [blame] | 3975 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 3976 | if( ssl->handshake->certificate_request_context ) |
| 3977 | { |
| 3978 | mbedtls_free( (void*) handshake->certificate_request_context ); |
| 3979 | } |
| 3980 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 3981 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 3982 | |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3983 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
| 3984 | if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 ) |
| 3985 | { |
Gilles Peskine | 8f97af7 | 2018-04-26 11:46:10 +0200 | [diff] [blame] | 3986 | ssl->conf->f_async_cancel( ssl ); |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3987 | handshake->async_in_progress = 0; |
| 3988 | } |
| 3989 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
| 3990 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 3991 | #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] | 3992 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 3993 | psa_hash_abort( &handshake->fin_sha256_psa ); |
| 3994 | #else |
Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 3995 | mbedtls_sha256_free( &handshake->fin_sha256 ); |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 3996 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3997 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 3998 | #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] | 3999 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 4000 | psa_hash_abort( &handshake->fin_sha384_psa ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 4001 | #else |
Andrzej Kurek | a242e83 | 2022-08-11 10:03:14 -0400 | [diff] [blame] | 4002 | mbedtls_sha512_free( &handshake->fin_sha384 ); |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 4003 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 4004 | #endif |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 4005 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4006 | #if defined(MBEDTLS_DHM_C) |
| 4007 | mbedtls_dhm_free( &handshake->dhm_ctx ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4008 | #endif |
Neil Armstrong | f3f4641 | 2022-04-12 14:43:39 +0200 | [diff] [blame] | 4009 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4010 | mbedtls_ecdh_free( &handshake->ecdh_ctx ); |
Paul Bakker | 61d113b | 2013-07-04 11:51:43 +0200 | [diff] [blame] | 4011 | #endif |
Valerio Setti | 02c25b5 | 2022-11-15 14:08:42 +0100 | [diff] [blame] | 4012 | |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 4013 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 4014 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 4015 | psa_pake_abort( &handshake->psa_pake_ctx ); |
| 4016 | psa_destroy_key( handshake->psa_pake_password ); |
| 4017 | handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT; |
| 4018 | #else |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 4019 | mbedtls_ecjpake_free( &handshake->ecjpake_ctx ); |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 4020 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | 77c0646 | 2015-09-17 13:59:49 +0200 | [diff] [blame] | 4021 | #if defined(MBEDTLS_SSL_CLI_C) |
| 4022 | mbedtls_free( handshake->ecjpake_cache ); |
| 4023 | handshake->ecjpake_cache = NULL; |
| 4024 | handshake->ecjpake_cache_len = 0; |
| 4025 | #endif |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 4026 | #endif |
Paul Bakker | 61d113b | 2013-07-04 11:51:43 +0200 | [diff] [blame] | 4027 | |
Janos Follath | 4ae5c29 | 2016-02-10 11:27:43 +0000 | [diff] [blame] | 4028 | #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ |
| 4029 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 4030 | /* explicit void pointer cast for buggy MS compiler */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4031 | mbedtls_free( (void *) handshake->curves ); |
Manuel Pégourié-Gonnard | d09453c | 2013-09-23 19:11:32 +0200 | [diff] [blame] | 4032 | #endif |
| 4033 | |
Ronald Cron | 73fe8df | 2022-10-05 14:31:43 +0200 | [diff] [blame] | 4034 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED) |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 4035 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 4036 | if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) ) |
| 4037 | { |
| 4038 | /* The maintenance of the external PSK key slot is the |
| 4039 | * user's responsibility. */ |
| 4040 | if( ssl->handshake->psk_opaque_is_internal ) |
| 4041 | { |
| 4042 | psa_destroy_key( ssl->handshake->psk_opaque ); |
| 4043 | ssl->handshake->psk_opaque_is_internal = 0; |
| 4044 | } |
| 4045 | ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT; |
| 4046 | } |
Neil Armstrong | e952a30 | 2022-05-03 10:22:14 +0200 | [diff] [blame] | 4047 | #else |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 4048 | if( handshake->psk != NULL ) |
| 4049 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 4050 | mbedtls_platform_zeroize( handshake->psk, handshake->psk_len ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 4051 | mbedtls_free( handshake->psk ); |
| 4052 | } |
Neil Armstrong | e952a30 | 2022-05-03 10:22:14 +0200 | [diff] [blame] | 4053 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Ronald Cron | 73fe8df | 2022-10-05 14:31:43 +0200 | [diff] [blame] | 4054 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */ |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 4055 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4056 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 4057 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Manuel Pégourié-Gonnard | 8372454 | 2013-09-24 22:30:56 +0200 | [diff] [blame] | 4058 | /* |
| 4059 | * Free only the linked list wrapper, not the keys themselves |
| 4060 | * since the belong to the SNI callback |
| 4061 | */ |
Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 4062 | ssl_key_cert_free( handshake->sni_key_cert ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4063 | #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] | 4064 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 4065 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Manuel Pégourié-Gonnard | 6b7301c | 2017-08-15 12:08:45 +0200 | [diff] [blame] | 4066 | mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx ); |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 4067 | if( handshake->ecrs_peer_cert != NULL ) |
| 4068 | { |
| 4069 | mbedtls_x509_crt_free( handshake->ecrs_peer_cert ); |
| 4070 | mbedtls_free( handshake->ecrs_peer_cert ); |
| 4071 | } |
Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 4072 | #endif |
| 4073 | |
Hanno Becker | 7517312 | 2019-02-06 16:18:31 +0000 | [diff] [blame] | 4074 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 4075 | !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 4076 | mbedtls_pk_free( &handshake->peer_pubkey ); |
| 4077 | #endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 4078 | |
XiaokangQian | 9b93c0d | 2022-02-09 06:02:25 +0000 | [diff] [blame] | 4079 | #if defined(MBEDTLS_SSL_CLI_C) && \ |
| 4080 | ( defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3) ) |
| 4081 | mbedtls_free( handshake->cookie ); |
| 4082 | #endif /* MBEDTLS_SSL_CLI_C && |
| 4083 | ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */ |
XiaokangQian | 8499b6c | 2022-01-27 09:00:11 +0000 | [diff] [blame] | 4084 | |
| 4085 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 533ab5f | 2020-02-05 10:49:13 +0000 | [diff] [blame] | 4086 | mbedtls_ssl_flight_free( handshake->flight ); |
| 4087 | mbedtls_ssl_buffering_free( ssl ); |
XiaokangQian | 8499b6c | 2022-01-27 09:00:11 +0000 | [diff] [blame] | 4088 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 4089 | |
Ronald Cron | f12b81d | 2022-03-15 10:42:41 +0100 | [diff] [blame] | 4090 | #if defined(MBEDTLS_ECDH_C) && \ |
| 4091 | ( defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3) ) |
Neil Armstrong | f716a70 | 2022-04-04 11:23:46 +0200 | [diff] [blame] | 4092 | if( handshake->ecdh_psa_privkey_is_external == 0 ) |
Neil Armstrong | 8113d25 | 2022-03-23 10:57:04 +0100 | [diff] [blame] | 4093 | psa_destroy_key( handshake->ecdh_psa_privkey ); |
Hanno Becker | 4a63ed4 | 2019-01-08 11:39:35 +0000 | [diff] [blame] | 4094 | #endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */ |
| 4095 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4096 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | a1a568c | 2021-11-09 10:17:21 +0800 | [diff] [blame] | 4097 | mbedtls_ssl_transform_free( handshake->transform_handshake ); |
Jerry Yu | 3d9b590 | 2022-11-04 14:07:25 +0800 | [diff] [blame] | 4098 | mbedtls_free( handshake->transform_handshake ); |
| 4099 | #if defined(MBEDTLS_SSL_EARLY_DATA) |
Jerry Yu | a1a568c | 2021-11-09 10:17:21 +0800 | [diff] [blame] | 4100 | mbedtls_ssl_transform_free( handshake->transform_earlydata ); |
Jerry Yu | ba9c727 | 2021-10-30 11:54:10 +0800 | [diff] [blame] | 4101 | mbedtls_free( handshake->transform_earlydata ); |
Jerry Yu | 3d9b590 | 2022-11-04 14:07:25 +0800 | [diff] [blame] | 4102 | #endif |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4103 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Jerry Yu | ba9c727 | 2021-10-30 11:54:10 +0800 | [diff] [blame] | 4104 | |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 4105 | |
| 4106 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 4107 | /* If the buffers are too big - reallocate. Because of the way Mbed TLS |
| 4108 | * processes datagrams and the fact that a datagram is allowed to have |
| 4109 | * several records in it, it is possible that the I/O buffers are not |
| 4110 | * empty at this stage */ |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 4111 | handle_buffer_resizing( ssl, 1, mbedtls_ssl_get_input_buflen( ssl ), |
| 4112 | mbedtls_ssl_get_output_buflen( ssl ) ); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 4113 | #endif |
Hanno Becker | 3aa186f | 2021-08-10 09:24:19 +0100 | [diff] [blame] | 4114 | |
Jerry Yu | ba9c727 | 2021-10-30 11:54:10 +0800 | [diff] [blame] | 4115 | /* mbedtls_platform_zeroize MUST be last one in this function */ |
| 4116 | mbedtls_platform_zeroize( handshake, |
| 4117 | sizeof( mbedtls_ssl_handshake_params ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4118 | } |
| 4119 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4120 | void mbedtls_ssl_session_free( mbedtls_ssl_session *session ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4121 | { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 4122 | if( session == NULL ) |
| 4123 | return; |
| 4124 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4125 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Hanno Becker | 1294a0b | 2019-02-05 12:38:15 +0000 | [diff] [blame] | 4126 | ssl_clear_peer_cert( session ); |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 4127 | #endif |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 4128 | |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 4129 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
Xiaokang Qian | bc663a0 | 2022-10-09 11:14:39 +0000 | [diff] [blame] | 4130 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \ |
| 4131 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Xiaokang Qian | 281fd1b | 2022-09-20 11:35:41 +0000 | [diff] [blame] | 4132 | mbedtls_free( session->hostname ); |
| 4133 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4134 | mbedtls_free( session->ticket ); |
Paul Bakker | a503a63 | 2013-08-14 13:48:06 +0200 | [diff] [blame] | 4135 | #endif |
Manuel Pégourié-Gonnard | 75d4401 | 2013-08-02 14:44:04 +0200 | [diff] [blame] | 4136 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 4137 | mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4138 | } |
| 4139 | |
Manuel Pégourié-Gonnard | 5c0e377 | 2019-07-23 16:13:17 +0200 | [diff] [blame] | 4140 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 4141 | |
| 4142 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 4143 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u |
| 4144 | #else |
| 4145 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u |
| 4146 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 4147 | |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 4148 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 4149 | |
| 4150 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 4151 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u |
| 4152 | #else |
| 4153 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u |
| 4154 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
| 4155 | |
| 4156 | #if defined(MBEDTLS_SSL_ALPN) |
| 4157 | #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u |
| 4158 | #else |
| 4159 | #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u |
| 4160 | #endif /* MBEDTLS_SSL_ALPN */ |
| 4161 | |
| 4162 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0 |
| 4163 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1 |
| 4164 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2 |
| 4165 | #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3 |
| 4166 | |
| 4167 | #define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \ |
| 4168 | ( (uint32_t) ( \ |
| 4169 | ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \ |
| 4170 | ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \ |
| 4171 | ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \ |
| 4172 | ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \ |
| 4173 | 0u ) ) |
| 4174 | |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4175 | static unsigned char ssl_serialized_context_header[] = { |
| 4176 | MBEDTLS_VERSION_MAJOR, |
| 4177 | MBEDTLS_VERSION_MINOR, |
| 4178 | MBEDTLS_VERSION_PATCH, |
Joe Subbiani | 2194dc4 | 2021-07-14 12:31:31 +0100 | [diff] [blame] | 4179 | MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ), |
| 4180 | MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ), |
| 4181 | MBEDTLS_BYTE_2( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ), |
| 4182 | MBEDTLS_BYTE_1( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ), |
| 4183 | MBEDTLS_BYTE_0( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ), |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4184 | }; |
| 4185 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4186 | /* |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 4187 | * Serialize a full SSL context |
Manuel Pégourié-Gonnard | 00400c2 | 2019-07-10 14:58:45 +0200 | [diff] [blame] | 4188 | * |
| 4189 | * The format of the serialized data is: |
| 4190 | * (in the presentation language of TLS, RFC 8446 section 3) |
| 4191 | * |
| 4192 | * // header |
| 4193 | * opaque mbedtls_version[3]; // major, minor, patch |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4194 | * opaque context_format[5]; // version-specific field determining |
Manuel Pégourié-Gonnard | 00400c2 | 2019-07-10 14:58:45 +0200 | [diff] [blame] | 4195 | * // the format of the remaining |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4196 | * // serialized data. |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 4197 | * Note: When updating the format, remember to keep these |
| 4198 | * 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] | 4199 | * |
| 4200 | * // session sub-structure |
| 4201 | * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save() |
| 4202 | * // transform sub-structure |
| 4203 | * uint8 random[64]; // ServerHello.random+ClientHello.random |
| 4204 | * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value |
| 4205 | * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use |
| 4206 | * // fields from ssl_context |
| 4207 | * uint32 badmac_seen; // DTLS: number of records with failing MAC |
| 4208 | * uint64 in_window_top; // DTLS: last validated record seq_num |
| 4209 | * uint64 in_window; // DTLS: bitmask for replay protection |
| 4210 | * uint8 disable_datagram_packing; // DTLS: only one record per datagram |
| 4211 | * uint64 cur_out_ctr; // Record layer: outgoing sequence number |
| 4212 | * uint16 mtu; // DTLS: path mtu (max outgoing fragment size) |
| 4213 | * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol |
| 4214 | * |
| 4215 | * Note that many fields of the ssl_context or sub-structures are not |
| 4216 | * serialized, as they fall in one of the following categories: |
| 4217 | * |
| 4218 | * 1. forced value (eg in_left must be 0) |
| 4219 | * 2. pointer to dynamically-allocated memory (eg session, transform) |
| 4220 | * 3. value can be re-derived from other data (eg session keys from MS) |
| 4221 | * 4. value was temporary (eg content of input buffer) |
| 4222 | * 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] | 4223 | */ |
| 4224 | int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl, |
| 4225 | unsigned char *buf, |
| 4226 | size_t buf_len, |
| 4227 | size_t *olen ) |
| 4228 | { |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4229 | unsigned char *p = buf; |
| 4230 | size_t used = 0; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4231 | size_t session_len; |
| 4232 | int ret = 0; |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4233 | |
Manuel Pégourié-Gonnard | 1aaf669 | 2019-07-10 14:14:05 +0200 | [diff] [blame] | 4234 | /* |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 4235 | * Enforce usage restrictions, see "return BAD_INPUT_DATA" in |
| 4236 | * this function's documentation. |
| 4237 | * |
| 4238 | * These are due to assumptions/limitations in the implementation. Some of |
| 4239 | * them are likely to stay (no handshake in progress) some might go away |
| 4240 | * (only DTLS) but are currently used to simplify the implementation. |
Manuel Pégourié-Gonnard | 1aaf669 | 2019-07-10 14:14:05 +0200 | [diff] [blame] | 4241 | */ |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 4242 | /* The initial handshake must be over */ |
Jerry Yu | 6848a61 | 2022-10-27 13:03:26 +0800 | [diff] [blame] | 4243 | if( mbedtls_ssl_is_handshake_over( ssl ) == 0 ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 4244 | { |
| 4245 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Initial handshake isn't over" ) ); |
Manuel Pégourié-Gonnard | 1aaf669 | 2019-07-10 14:14:05 +0200 | [diff] [blame] | 4246 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 4247 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 4248 | if( ssl->handshake != NULL ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 4249 | { |
| 4250 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Handshake isn't completed" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 4251 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 4252 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 4253 | /* Double-check that sub-structures are indeed ready */ |
| 4254 | if( ssl->transform == NULL || ssl->session == NULL ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 4255 | { |
| 4256 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Serialised structures aren't ready" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 4257 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 4258 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 4259 | /* There must be no pending incoming or outgoing data */ |
| 4260 | if( mbedtls_ssl_check_pending( ssl ) != 0 ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 4261 | { |
| 4262 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending incoming data" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 4263 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 4264 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 4265 | if( ssl->out_left != 0 ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 4266 | { |
| 4267 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending outgoing data" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 4268 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 4269 | } |
Dave Rodgman | 556e8a3 | 2022-12-06 16:31:25 +0000 | [diff] [blame] | 4270 | /* Protocol must be DTLS, not TLS */ |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 4271 | if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 4272 | { |
| 4273 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only DTLS is supported" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +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 | /* Version must be 1.2 */ |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 4277 | if( ssl->tls_version != MBEDTLS_SSL_VERSION_TLS1_2 ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 4278 | { |
| 4279 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only version 1.2 supported" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 4280 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 4281 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 4282 | /* We must be using an AEAD ciphersuite */ |
| 4283 | if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 4284 | { |
| 4285 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only AEAD ciphersuites supported" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 4286 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 4287 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 4288 | /* Renegotiation must not be enabled */ |
| 4289 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 4290 | if( ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 4291 | { |
| 4292 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Renegotiation must not be enabled" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 4293 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 4294 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 4295 | #endif |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 4296 | |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4297 | /* |
| 4298 | * Version and format identifier |
| 4299 | */ |
| 4300 | used += sizeof( ssl_serialized_context_header ); |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 4301 | |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4302 | if( used <= buf_len ) |
| 4303 | { |
| 4304 | memcpy( p, ssl_serialized_context_header, |
| 4305 | sizeof( ssl_serialized_context_header ) ); |
| 4306 | p += sizeof( ssl_serialized_context_header ); |
| 4307 | } |
| 4308 | |
| 4309 | /* |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4310 | * Session (length + data) |
| 4311 | */ |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 4312 | 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] | 4313 | if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ) |
| 4314 | return( ret ); |
| 4315 | |
| 4316 | used += 4 + session_len; |
| 4317 | if( used <= buf_len ) |
| 4318 | { |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 4319 | MBEDTLS_PUT_UINT32_BE( session_len, p, 0 ); |
| 4320 | p += 4; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4321 | |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 4322 | ret = ssl_session_save( ssl->session, 1, |
| 4323 | p, session_len, &session_len ); |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4324 | if( ret != 0 ) |
| 4325 | return( ret ); |
| 4326 | |
| 4327 | p += session_len; |
| 4328 | } |
| 4329 | |
| 4330 | /* |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4331 | * Transform |
| 4332 | */ |
| 4333 | used += sizeof( ssl->transform->randbytes ); |
| 4334 | if( used <= buf_len ) |
| 4335 | { |
| 4336 | memcpy( p, ssl->transform->randbytes, |
| 4337 | sizeof( ssl->transform->randbytes ) ); |
| 4338 | p += sizeof( ssl->transform->randbytes ); |
| 4339 | } |
| 4340 | |
| 4341 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 4342 | used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len; |
| 4343 | if( used <= buf_len ) |
| 4344 | { |
| 4345 | *p++ = ssl->transform->in_cid_len; |
| 4346 | memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len ); |
| 4347 | p += ssl->transform->in_cid_len; |
| 4348 | |
| 4349 | *p++ = ssl->transform->out_cid_len; |
| 4350 | memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len ); |
| 4351 | p += ssl->transform->out_cid_len; |
| 4352 | } |
| 4353 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 4354 | |
| 4355 | /* |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4356 | * Saved fields from top-level ssl_context structure |
| 4357 | */ |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4358 | used += 4; |
| 4359 | if( used <= buf_len ) |
| 4360 | { |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 4361 | MBEDTLS_PUT_UINT32_BE( ssl->badmac_seen, p, 0 ); |
| 4362 | p += 4; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4363 | } |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4364 | |
| 4365 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 4366 | used += 16; |
| 4367 | if( used <= buf_len ) |
| 4368 | { |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 4369 | MBEDTLS_PUT_UINT64_BE( ssl->in_window_top, p, 0 ); |
| 4370 | p += 8; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4371 | |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 4372 | MBEDTLS_PUT_UINT64_BE( ssl->in_window, p, 0 ); |
| 4373 | p += 8; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4374 | } |
| 4375 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
| 4376 | |
| 4377 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4378 | used += 1; |
| 4379 | if( used <= buf_len ) |
| 4380 | { |
| 4381 | *p++ = ssl->disable_datagram_packing; |
| 4382 | } |
| 4383 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 4384 | |
Jerry Yu | ae0b2e2 | 2021-10-08 15:21:19 +0800 | [diff] [blame] | 4385 | used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4386 | if( used <= buf_len ) |
| 4387 | { |
Jerry Yu | ae0b2e2 | 2021-10-08 15:21:19 +0800 | [diff] [blame] | 4388 | memcpy( p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN ); |
| 4389 | p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4390 | } |
| 4391 | |
| 4392 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4393 | used += 2; |
| 4394 | if( used <= buf_len ) |
| 4395 | { |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 4396 | MBEDTLS_PUT_UINT16_BE( ssl->mtu, p, 0 ); |
| 4397 | p += 2; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4398 | } |
| 4399 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 4400 | |
| 4401 | #if defined(MBEDTLS_SSL_ALPN) |
| 4402 | { |
| 4403 | const uint8_t alpn_len = ssl->alpn_chosen |
Manuel Pégourié-Gonnard | f041f4e | 2019-07-24 00:58:27 +0200 | [diff] [blame] | 4404 | ? (uint8_t) strlen( ssl->alpn_chosen ) |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4405 | : 0; |
| 4406 | |
| 4407 | used += 1 + alpn_len; |
| 4408 | if( used <= buf_len ) |
| 4409 | { |
| 4410 | *p++ = alpn_len; |
| 4411 | |
| 4412 | if( ssl->alpn_chosen != NULL ) |
| 4413 | { |
| 4414 | memcpy( p, ssl->alpn_chosen, alpn_len ); |
| 4415 | p += alpn_len; |
| 4416 | } |
| 4417 | } |
| 4418 | } |
| 4419 | #endif /* MBEDTLS_SSL_ALPN */ |
| 4420 | |
| 4421 | /* |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4422 | * Done |
| 4423 | */ |
| 4424 | *olen = used; |
| 4425 | |
| 4426 | if( used > buf_len ) |
| 4427 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 4428 | |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4429 | MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used ); |
| 4430 | |
Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 4431 | return( mbedtls_ssl_session_reset_int( ssl, 0 ) ); |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 4432 | } |
| 4433 | |
| 4434 | /* |
Manuel Pégourié-Gonnard | b9dfc9f | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 4435 | * Deserialize context, see mbedtls_ssl_context_save() for format. |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4436 | * |
| 4437 | * This internal version is wrapped by a public function that cleans up in |
| 4438 | * case of error. |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 4439 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 4440 | MBEDTLS_CHECK_RETURN_CRITICAL |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4441 | static int ssl_context_load( mbedtls_ssl_context *ssl, |
| 4442 | const unsigned char *buf, |
| 4443 | size_t len ) |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 4444 | { |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4445 | const unsigned char *p = buf; |
| 4446 | const unsigned char * const end = buf + len; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4447 | size_t session_len; |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 4448 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Andrzej Kurek | 2d59dbc | 2022-10-13 08:34:38 -0400 | [diff] [blame] | 4449 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Andrzej Kurek | 894edde | 2022-09-29 06:31:14 -0400 | [diff] [blame] | 4450 | tls_prf_fn prf_func = NULL; |
Andrzej Kurek | 2d59dbc | 2022-10-13 08:34:38 -0400 | [diff] [blame] | 4451 | #endif |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4452 | |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 4453 | /* |
| 4454 | * The context should have been freshly setup or reset. |
| 4455 | * Give the user an error in case of obvious misuse. |
Manuel Pégourié-Gonnard | 4ca930f | 2019-07-26 16:31:53 +0200 | [diff] [blame] | 4456 | * (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] | 4457 | * renegotiating, or if the user mistakenly loaded a session first.) |
| 4458 | */ |
| 4459 | if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST || |
| 4460 | ssl->session != NULL ) |
| 4461 | { |
| 4462 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4463 | } |
| 4464 | |
| 4465 | /* |
| 4466 | * We can't check that the config matches the initial one, but we can at |
| 4467 | * least check it matches the requirements for serializing. |
| 4468 | */ |
| 4469 | if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM || |
Glenn Strauss | 2dfcea2 | 2022-03-14 17:26:42 -0400 | [diff] [blame] | 4470 | ssl->conf->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2 || |
| 4471 | ssl->conf->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 || |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 4472 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | 9a96fd7 | 2019-07-23 17:11:24 +0200 | [diff] [blame] | 4473 | ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED || |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 4474 | #endif |
Manuel Pégourié-Gonnard | 9a96fd7 | 2019-07-23 17:11:24 +0200 | [diff] [blame] | 4475 | 0 ) |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 4476 | { |
| 4477 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4478 | } |
| 4479 | |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4480 | MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len ); |
| 4481 | |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4482 | /* |
| 4483 | * Check version identifier |
| 4484 | */ |
| 4485 | if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) ) |
| 4486 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4487 | |
| 4488 | if( memcmp( p, ssl_serialized_context_header, |
| 4489 | sizeof( ssl_serialized_context_header ) ) != 0 ) |
| 4490 | { |
| 4491 | return( MBEDTLS_ERR_SSL_VERSION_MISMATCH ); |
| 4492 | } |
| 4493 | p += sizeof( ssl_serialized_context_header ); |
| 4494 | |
| 4495 | /* |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4496 | * Session |
| 4497 | */ |
| 4498 | if( (size_t)( end - p ) < 4 ) |
| 4499 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4500 | |
| 4501 | session_len = ( (size_t) p[0] << 24 ) | |
| 4502 | ( (size_t) p[1] << 16 ) | |
| 4503 | ( (size_t) p[2] << 8 ) | |
| 4504 | ( (size_t) p[3] ); |
| 4505 | p += 4; |
| 4506 | |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 4507 | /* This has been allocated by ssl_handshake_init(), called by |
Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 4508 | * 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] | 4509 | ssl->session = ssl->session_negotiate; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4510 | ssl->session_in = ssl->session; |
| 4511 | ssl->session_out = ssl->session; |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 4512 | ssl->session_negotiate = NULL; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4513 | |
| 4514 | if( (size_t)( end - p ) < session_len ) |
| 4515 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4516 | |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 4517 | ret = ssl_session_load( ssl->session, 1, p, session_len ); |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4518 | if( ret != 0 ) |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 4519 | { |
| 4520 | mbedtls_ssl_session_free( ssl->session ); |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4521 | return( ret ); |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 4522 | } |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4523 | |
| 4524 | p += session_len; |
| 4525 | |
| 4526 | /* |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4527 | * Transform |
| 4528 | */ |
| 4529 | |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 4530 | /* This has been allocated by ssl_handshake_init(), called by |
Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 4531 | * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */ |
Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 4532 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Jerry Yu | ddda050 | 2022-12-01 19:43:12 +0800 | [diff] [blame] | 4533 | ssl->transform = ssl->transform_negotiate; |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4534 | ssl->transform_in = ssl->transform; |
| 4535 | ssl->transform_out = ssl->transform; |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 4536 | ssl->transform_negotiate = NULL; |
Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 4537 | #endif |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4538 | |
Andrzej Kurek | 2d59dbc | 2022-10-13 08:34:38 -0400 | [diff] [blame] | 4539 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Andrzej Kurek | 894edde | 2022-09-29 06:31:14 -0400 | [diff] [blame] | 4540 | prf_func = ssl_tls12prf_from_cs( ssl->session->ciphersuite ); |
| 4541 | if( prf_func == NULL ) |
| 4542 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4543 | |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4544 | /* Read random bytes and populate structure */ |
| 4545 | if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) ) |
| 4546 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Andrzej Kurek | 2d59dbc | 2022-10-13 08:34:38 -0400 | [diff] [blame] | 4547 | |
Hanno Becker | bd25755 | 2021-03-22 06:59:27 +0000 | [diff] [blame] | 4548 | ret = ssl_tls12_populate_transform( ssl->transform, |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4549 | ssl->session->ciphersuite, |
| 4550 | ssl->session->master, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 4551 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4552 | ssl->session->encrypt_then_mac, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 4553 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Andrzej Kurek | 894edde | 2022-09-29 06:31:14 -0400 | [diff] [blame] | 4554 | prf_func, |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4555 | p, /* currently pointing to randbytes */ |
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 4556 | 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] | 4557 | ssl->conf->endpoint, |
| 4558 | ssl ); |
| 4559 | if( ret != 0 ) |
| 4560 | return( ret ); |
Jerry Yu | 840fbb2 | 2022-02-17 14:59:29 +0800 | [diff] [blame] | 4561 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4562 | p += sizeof( ssl->transform->randbytes ); |
| 4563 | |
| 4564 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 4565 | /* Read connection IDs and store them */ |
| 4566 | if( (size_t)( end - p ) < 1 ) |
| 4567 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4568 | |
| 4569 | ssl->transform->in_cid_len = *p++; |
| 4570 | |
Manuel Pégourié-Gonnard | 5ea13b8 | 2019-07-23 15:02:54 +0200 | [diff] [blame] | 4571 | 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] | 4572 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4573 | |
| 4574 | memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len ); |
| 4575 | p += ssl->transform->in_cid_len; |
| 4576 | |
| 4577 | ssl->transform->out_cid_len = *p++; |
| 4578 | |
| 4579 | if( (size_t)( end - p ) < ssl->transform->out_cid_len ) |
| 4580 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4581 | |
| 4582 | memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len ); |
| 4583 | p += ssl->transform->out_cid_len; |
| 4584 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 4585 | |
| 4586 | /* |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4587 | * Saved fields from top-level ssl_context structure |
| 4588 | */ |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4589 | if( (size_t)( end - p ) < 4 ) |
| 4590 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4591 | |
| 4592 | ssl->badmac_seen = ( (uint32_t) p[0] << 24 ) | |
| 4593 | ( (uint32_t) p[1] << 16 ) | |
| 4594 | ( (uint32_t) p[2] << 8 ) | |
| 4595 | ( (uint32_t) p[3] ); |
| 4596 | p += 4; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4597 | |
| 4598 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 4599 | if( (size_t)( end - p ) < 16 ) |
| 4600 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4601 | |
| 4602 | ssl->in_window_top = ( (uint64_t) p[0] << 56 ) | |
| 4603 | ( (uint64_t) p[1] << 48 ) | |
| 4604 | ( (uint64_t) p[2] << 40 ) | |
| 4605 | ( (uint64_t) p[3] << 32 ) | |
| 4606 | ( (uint64_t) p[4] << 24 ) | |
| 4607 | ( (uint64_t) p[5] << 16 ) | |
| 4608 | ( (uint64_t) p[6] << 8 ) | |
| 4609 | ( (uint64_t) p[7] ); |
| 4610 | p += 8; |
| 4611 | |
| 4612 | ssl->in_window = ( (uint64_t) p[0] << 56 ) | |
| 4613 | ( (uint64_t) p[1] << 48 ) | |
| 4614 | ( (uint64_t) p[2] << 40 ) | |
| 4615 | ( (uint64_t) p[3] << 32 ) | |
| 4616 | ( (uint64_t) p[4] << 24 ) | |
| 4617 | ( (uint64_t) p[5] << 16 ) | |
| 4618 | ( (uint64_t) p[6] << 8 ) | |
| 4619 | ( (uint64_t) p[7] ); |
| 4620 | p += 8; |
| 4621 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
| 4622 | |
| 4623 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4624 | if( (size_t)( end - p ) < 1 ) |
| 4625 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4626 | |
| 4627 | ssl->disable_datagram_packing = *p++; |
| 4628 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 4629 | |
Jerry Yu | d9a94fe | 2021-09-28 18:58:59 +0800 | [diff] [blame] | 4630 | if( (size_t)( end - p ) < sizeof( ssl->cur_out_ctr ) ) |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4631 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jerry Yu | d9a94fe | 2021-09-28 18:58:59 +0800 | [diff] [blame] | 4632 | memcpy( ssl->cur_out_ctr, p, sizeof( ssl->cur_out_ctr ) ); |
| 4633 | p += sizeof( ssl->cur_out_ctr ); |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4634 | |
| 4635 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4636 | if( (size_t)( end - p ) < 2 ) |
| 4637 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4638 | |
| 4639 | ssl->mtu = ( p[0] << 8 ) | p[1]; |
| 4640 | p += 2; |
| 4641 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 4642 | |
| 4643 | #if defined(MBEDTLS_SSL_ALPN) |
| 4644 | { |
| 4645 | uint8_t alpn_len; |
| 4646 | const char **cur; |
| 4647 | |
| 4648 | if( (size_t)( end - p ) < 1 ) |
| 4649 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4650 | |
| 4651 | alpn_len = *p++; |
| 4652 | |
| 4653 | if( alpn_len != 0 && ssl->conf->alpn_list != NULL ) |
| 4654 | { |
| 4655 | /* alpn_chosen should point to an item in the configured list */ |
| 4656 | for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ ) |
| 4657 | { |
| 4658 | if( strlen( *cur ) == alpn_len && |
| 4659 | memcmp( p, cur, alpn_len ) == 0 ) |
| 4660 | { |
| 4661 | ssl->alpn_chosen = *cur; |
| 4662 | break; |
| 4663 | } |
| 4664 | } |
| 4665 | } |
| 4666 | |
| 4667 | /* can only happen on conf mismatch */ |
| 4668 | if( alpn_len != 0 && ssl->alpn_chosen == NULL ) |
| 4669 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4670 | |
| 4671 | p += alpn_len; |
| 4672 | } |
| 4673 | #endif /* MBEDTLS_SSL_ALPN */ |
| 4674 | |
| 4675 | /* |
Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 4676 | * Forced fields from top-level ssl_context structure |
| 4677 | * |
| 4678 | * Most of them already set to the correct value by mbedtls_ssl_init() and |
| 4679 | * mbedtls_ssl_reset(), so we only need to set the remaining ones. |
| 4680 | */ |
| 4681 | ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER; |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 4682 | ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 4683 | |
Hanno Becker | 361b10d | 2019-08-30 10:42:49 +0100 | [diff] [blame] | 4684 | /* Adjust pointers for header fields of outgoing records to |
| 4685 | * the given transform, accounting for explicit IV and CID. */ |
Hanno Becker | 3e6f8ab | 2020-02-05 10:40:57 +0000 | [diff] [blame] | 4686 | mbedtls_ssl_update_out_pointers( ssl, ssl->transform ); |
Hanno Becker | 361b10d | 2019-08-30 10:42:49 +0100 | [diff] [blame] | 4687 | |
Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 4688 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4689 | ssl->in_epoch = 1; |
| 4690 | #endif |
| 4691 | |
| 4692 | /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated, |
| 4693 | * 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] | 4694 | * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform() |
| 4695 | * inappropriately. */ |
Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 4696 | if( ssl->handshake != NULL ) |
| 4697 | { |
| 4698 | mbedtls_ssl_handshake_free( ssl ); |
| 4699 | mbedtls_free( ssl->handshake ); |
| 4700 | ssl->handshake = NULL; |
| 4701 | } |
| 4702 | |
| 4703 | /* |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4704 | * Done - should have consumed entire buffer |
| 4705 | */ |
| 4706 | if( p != end ) |
| 4707 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 4708 | |
| 4709 | return( 0 ); |
| 4710 | } |
| 4711 | |
| 4712 | /* |
Manuel Pégourié-Gonnard | b9dfc9f | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 4713 | * Deserialize context: public wrapper for error cleaning |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4714 | */ |
| 4715 | int mbedtls_ssl_context_load( mbedtls_ssl_context *context, |
| 4716 | const unsigned char *buf, |
| 4717 | size_t len ) |
| 4718 | { |
| 4719 | int ret = ssl_context_load( context, buf, len ); |
| 4720 | |
| 4721 | if( ret != 0 ) |
| 4722 | mbedtls_ssl_free( context ); |
| 4723 | |
| 4724 | return( ret ); |
| 4725 | } |
Manuel Pégourié-Gonnard | 5c0e377 | 2019-07-23 16:13:17 +0200 | [diff] [blame] | 4726 | #endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */ |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4727 | |
| 4728 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4729 | * Free an SSL context |
| 4730 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4731 | void mbedtls_ssl_free( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4732 | { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 4733 | if( ssl == NULL ) |
| 4734 | return; |
| 4735 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4736 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4737 | |
Manuel Pégourié-Gonnard | 7ee6f0e | 2014-02-13 10:54:07 +0100 | [diff] [blame] | 4738 | if( ssl->out_buf != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4739 | { |
sander-visser | b8aa207 | 2020-05-06 22:05:13 +0200 | [diff] [blame] | 4740 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 4741 | size_t out_buf_len = ssl->out_buf_len; |
| 4742 | #else |
| 4743 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; |
| 4744 | #endif |
| 4745 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 4746 | mbedtls_platform_zeroize( ssl->out_buf, out_buf_len ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4747 | mbedtls_free( ssl->out_buf ); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 4748 | ssl->out_buf = NULL; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4749 | } |
| 4750 | |
Manuel Pégourié-Gonnard | 7ee6f0e | 2014-02-13 10:54:07 +0100 | [diff] [blame] | 4751 | if( ssl->in_buf != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4752 | { |
sander-visser | b8aa207 | 2020-05-06 22:05:13 +0200 | [diff] [blame] | 4753 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 4754 | size_t in_buf_len = ssl->in_buf_len; |
| 4755 | #else |
| 4756 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; |
| 4757 | #endif |
| 4758 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 4759 | mbedtls_platform_zeroize( ssl->in_buf, in_buf_len ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4760 | mbedtls_free( ssl->in_buf ); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 4761 | ssl->in_buf = NULL; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4762 | } |
| 4763 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4764 | if( ssl->transform ) |
| 4765 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4766 | mbedtls_ssl_transform_free( ssl->transform ); |
| 4767 | mbedtls_free( ssl->transform ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4768 | } |
| 4769 | |
| 4770 | if( ssl->handshake ) |
| 4771 | { |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 4772 | mbedtls_ssl_handshake_free( ssl ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4773 | mbedtls_free( ssl->handshake ); |
Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 4774 | |
| 4775 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 4776 | mbedtls_ssl_transform_free( ssl->transform_negotiate ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4777 | mbedtls_free( ssl->transform_negotiate ); |
Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 4778 | #endif |
| 4779 | |
| 4780 | mbedtls_ssl_session_free( ssl->session_negotiate ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4781 | mbedtls_free( ssl->session_negotiate ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4782 | } |
| 4783 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4784 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Hanno Becker | 3aa186f | 2021-08-10 09:24:19 +0100 | [diff] [blame] | 4785 | mbedtls_ssl_transform_free( ssl->transform_application ); |
| 4786 | mbedtls_free( ssl->transform_application ); |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4787 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | 3aa186f | 2021-08-10 09:24:19 +0100 | [diff] [blame] | 4788 | |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 4789 | if( ssl->session ) |
| 4790 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4791 | mbedtls_ssl_session_free( ssl->session ); |
| 4792 | mbedtls_free( ssl->session ); |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 4793 | } |
| 4794 | |
Manuel Pégourié-Gonnard | 55fab2d | 2015-05-11 16:15:19 +0200 | [diff] [blame] | 4795 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 4796 | if( ssl->hostname != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4797 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 4798 | mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4799 | mbedtls_free( ssl->hostname ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4800 | } |
Paul Bakker | 0be444a | 2013-08-27 21:55:01 +0200 | [diff] [blame] | 4801 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4802 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 4803 | #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] | 4804 | mbedtls_free( ssl->cli_id ); |
Manuel Pégourié-Gonnard | 43c0218 | 2014-07-22 17:32:01 +0200 | [diff] [blame] | 4805 | #endif |
| 4806 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4807 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) ); |
Paul Bakker | 2da561c | 2009-02-05 18:00:28 +0000 | [diff] [blame] | 4808 | |
Paul Bakker | 86f04f4 | 2013-02-14 11:20:09 +0100 | [diff] [blame] | 4809 | /* Actually clear after last debug message */ |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 4810 | mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4811 | } |
| 4812 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4813 | /* |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 4814 | * Initialize mbedtls_ssl_config |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4815 | */ |
| 4816 | void mbedtls_ssl_config_init( mbedtls_ssl_config *conf ) |
| 4817 | { |
| 4818 | memset( conf, 0, sizeof( mbedtls_ssl_config ) ); |
| 4819 | } |
| 4820 | |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4821 | /* The selection should be the same as mbedtls_x509_crt_profile_default in |
| 4822 | * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters: |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4823 | * curves with a lower resource usage come first. |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4824 | * See the documentation of mbedtls_ssl_conf_curves() for what we promise |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4825 | * about this list. |
| 4826 | */ |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4827 | static uint16_t ssl_preset_default_groups[] = { |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4828 | #if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4829 | MBEDTLS_SSL_IANA_TLS_GROUP_X25519, |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4830 | #endif |
| 4831 | #if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4832 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1, |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4833 | #endif |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4834 | #if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4835 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4836 | #endif |
| 4837 | #if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4838 | MBEDTLS_SSL_IANA_TLS_GROUP_X448, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4839 | #endif |
| 4840 | #if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4841 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4842 | #endif |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4843 | #if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4844 | MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1, |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4845 | #endif |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4846 | #if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4847 | MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4848 | #endif |
| 4849 | #if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4850 | MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4851 | #endif |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4852 | MBEDTLS_SSL_IANA_TLS_GROUP_NONE |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4853 | }; |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4854 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4855 | static int ssl_preset_suiteb_ciphersuites[] = { |
| 4856 | MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, |
| 4857 | MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, |
| 4858 | 0 |
| 4859 | }; |
| 4860 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 4861 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Hanno Becker | 9c6aa7b | 2021-08-10 13:50:43 +0100 | [diff] [blame] | 4862 | |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4863 | /* NOTICE: |
Jerry Yu | 0b994b8 | 2022-01-25 17:22:12 +0800 | [diff] [blame] | 4864 | * 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] | 4865 | * rules SHOULD be upheld. |
| 4866 | * - No duplicate entries. |
| 4867 | * - 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] | 4868 | * - ssl_tls12_preset* is for TLS 1.2 use only. |
Jerry Yu | 370e146 | 2022-01-25 10:36:53 +0800 | [diff] [blame] | 4869 | * - 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] | 4870 | */ |
Hanno Becker | 9c6aa7b | 2021-08-10 13:50:43 +0100 | [diff] [blame] | 4871 | static uint16_t ssl_preset_default_sig_algs[] = { |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4872 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4873 | #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] | 4874 | defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) |
| 4875 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256, |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4876 | #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] | 4877 | MBEDTLS_ECP_DP_SECP256R1_ENABLED */ |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4878 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4879 | #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] | 4880 | defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) |
| 4881 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384, |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4882 | #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] | 4883 | MBEDTLS_ECP_DP_SECP384R1_ENABLED */ |
| 4884 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4885 | #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] | 4886 | defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) |
| 4887 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512, |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4888 | #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] | 4889 | MBEDTLS_ECP_DP_SECP521R1_ENABLED */ |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4890 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4891 | #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] | 4892 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512, |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4893 | #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] | 4894 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4895 | #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] | 4896 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384, |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4897 | #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] | 4898 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4899 | #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] | 4900 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256, |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4901 | #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] | 4902 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4903 | #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] | 4904 | MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512, |
| 4905 | #endif /* MBEDTLS_RSA_C && MBEDTLS_SHA512_C */ |
| 4906 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4907 | #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] | 4908 | MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384, |
| 4909 | #endif /* MBEDTLS_RSA_C && MBEDTLS_SHA384_C */ |
| 4910 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4911 | #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] | 4912 | MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256, |
| 4913 | #endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */ |
| 4914 | |
Gabor Mezei | 15b95a6 | 2022-05-09 16:37:58 +0200 | [diff] [blame] | 4915 | MBEDTLS_TLS_SIG_NONE |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4916 | }; |
| 4917 | |
| 4918 | /* NOTICE: see above */ |
| 4919 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 4920 | static uint16_t ssl_tls12_preset_default_sig_algs[] = { |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4921 | #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] | 4922 | #if defined(MBEDTLS_ECDSA_C) |
| 4923 | 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] | 4924 | #endif |
Jerry Yu | 09a99fc | 2022-07-28 14:22:17 +0800 | [diff] [blame] | 4925 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) |
| 4926 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512, |
| 4927 | #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */ |
Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 4928 | #if defined(MBEDTLS_RSA_C) |
| 4929 | MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA512 ), |
| 4930 | #endif |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4931 | #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] | 4932 | #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] | 4933 | #if defined(MBEDTLS_ECDSA_C) |
| 4934 | 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] | 4935 | #endif |
Jerry Yu | 09a99fc | 2022-07-28 14:22:17 +0800 | [diff] [blame] | 4936 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) |
| 4937 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384, |
| 4938 | #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */ |
Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 4939 | #if defined(MBEDTLS_RSA_C) |
| 4940 | MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ), |
| 4941 | #endif |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4942 | #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] | 4943 | #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] | 4944 | #if defined(MBEDTLS_ECDSA_C) |
| 4945 | 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] | 4946 | #endif |
Jerry Yu | 09a99fc | 2022-07-28 14:22:17 +0800 | [diff] [blame] | 4947 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) |
| 4948 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256, |
| 4949 | #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */ |
Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 4950 | #if defined(MBEDTLS_RSA_C) |
| 4951 | MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ), |
| 4952 | #endif |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4953 | #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] | 4954 | MBEDTLS_TLS_SIG_NONE |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4955 | }; |
| 4956 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 4957 | /* NOTICE: see above */ |
| 4958 | static uint16_t ssl_preset_suiteb_sig_algs[] = { |
| 4959 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4960 | #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] | 4961 | defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) |
| 4962 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256, |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4963 | #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] | 4964 | MBEDTLS_ECP_DP_SECP256R1_ENABLED */ |
| 4965 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4966 | #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] | 4967 | defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) |
| 4968 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384, |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4969 | #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] | 4970 | MBEDTLS_ECP_DP_SECP384R1_ENABLED */ |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4971 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4972 | #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] | 4973 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256, |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4974 | #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] | 4975 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4976 | #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] | 4977 | MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256, |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4978 | #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] | 4979 | |
Gabor Mezei | 15b95a6 | 2022-05-09 16:37:58 +0200 | [diff] [blame] | 4980 | MBEDTLS_TLS_SIG_NONE |
Jerry Yu | 713013f | 2022-01-17 18:16:35 +0800 | [diff] [blame] | 4981 | }; |
Jerry Yu | 6106fdc | 2022-01-12 16:36:14 +0800 | [diff] [blame] | 4982 | |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4983 | /* NOTICE: see above */ |
| 4984 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 4985 | static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = { |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4986 | #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] | 4987 | #if defined(MBEDTLS_ECDSA_C) |
| 4988 | 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] | 4989 | #endif |
Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 4990 | #if defined(MBEDTLS_RSA_C) |
| 4991 | MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ), |
| 4992 | #endif |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4993 | #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] | 4994 | #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] | 4995 | #if defined(MBEDTLS_ECDSA_C) |
| 4996 | 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] | 4997 | #endif |
Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 4998 | #if defined(MBEDTLS_RSA_C) |
| 4999 | MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ), |
| 5000 | #endif |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 5001 | #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] | 5002 | MBEDTLS_TLS_SIG_NONE |
Hanno Becker | 9c6aa7b | 2021-08-10 13:50:43 +0100 | [diff] [blame] | 5003 | }; |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5004 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 5005 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5006 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5007 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5008 | static uint16_t ssl_preset_suiteb_groups[] = { |
Jaeden Amero | d431104 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 5009 | #if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5010 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1, |
Jaeden Amero | d431104 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 5011 | #endif |
| 5012 | #if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5013 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1, |
Jaeden Amero | d431104 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 5014 | #endif |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5015 | MBEDTLS_SSL_IANA_TLS_GROUP_NONE |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5016 | }; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5017 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5018 | #if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5019 | /* 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] | 5020 | * to make sure there are no duplicated signature algorithm entries. */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 5021 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 5022 | static int ssl_check_no_sig_alg_duplication( uint16_t * sig_algs ) |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 5023 | { |
| 5024 | size_t i, j; |
| 5025 | int ret = 0; |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5026 | |
Gabor Mezei | 15b95a6 | 2022-05-09 16:37:58 +0200 | [diff] [blame] | 5027 | for( i = 0; sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ ) |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 5028 | { |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 5029 | for( j = 0; j < i; j++ ) |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 5030 | { |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 5031 | if( sig_algs[i] != sig_algs[j] ) |
| 5032 | continue; |
| 5033 | mbedtls_printf( " entry(%04x,%" MBEDTLS_PRINTF_SIZET |
| 5034 | ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n", |
| 5035 | sig_algs[i], j, i ); |
| 5036 | ret = -1; |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 5037 | } |
| 5038 | } |
| 5039 | return( ret ); |
| 5040 | } |
| 5041 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5042 | #endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 5043 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5044 | /* |
Tillmann Karras | 588ad50 | 2015-09-25 04:27:22 +0200 | [diff] [blame] | 5045 | * Load default in mbedtls_ssl_config |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5046 | */ |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 5047 | int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5048 | int endpoint, int transport, int preset ) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5049 | { |
Manuel Pégourié-Gonnard | 8b431fb | 2015-05-11 12:54:52 +0200 | [diff] [blame] | 5050 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 5051 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 8b431fb | 2015-05-11 12:54:52 +0200 | [diff] [blame] | 5052 | #endif |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5053 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5054 | #if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 5055 | if( ssl_check_no_sig_alg_duplication( ssl_preset_suiteb_sig_algs ) ) |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 5056 | { |
| 5057 | mbedtls_printf( "ssl_preset_suiteb_sig_algs has duplicated entries\n" ); |
| 5058 | return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED ); |
| 5059 | } |
| 5060 | |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 5061 | if( ssl_check_no_sig_alg_duplication( ssl_preset_default_sig_algs ) ) |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 5062 | { |
| 5063 | mbedtls_printf( "ssl_preset_default_sig_algs has duplicated entries\n" ); |
| 5064 | return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED ); |
| 5065 | } |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5066 | |
| 5067 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 5068 | 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] | 5069 | { |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5070 | mbedtls_printf( "ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n" ); |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5071 | return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED ); |
| 5072 | } |
| 5073 | |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 5074 | 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] | 5075 | { |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5076 | mbedtls_printf( "ssl_tls12_preset_default_sig_algs has duplicated entries\n" ); |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5077 | return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED ); |
| 5078 | } |
| 5079 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5080 | #endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 5081 | |
Manuel Pégourié-Gonnard | 0de074f | 2015-05-14 12:58:01 +0200 | [diff] [blame] | 5082 | /* Use the functions here so that they are covered in tests, |
| 5083 | * but otherwise access member directly for efficiency */ |
| 5084 | mbedtls_ssl_conf_endpoint( conf, endpoint ); |
| 5085 | mbedtls_ssl_conf_transport( conf, transport ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5086 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5087 | /* |
| 5088 | * Things that are common to all presets |
| 5089 | */ |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 5090 | #if defined(MBEDTLS_SSL_CLI_C) |
| 5091 | if( endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 5092 | { |
| 5093 | conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED; |
| 5094 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 5095 | conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED; |
| 5096 | #endif |
| 5097 | } |
| 5098 | #endif |
| 5099 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5100 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 5101 | conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED; |
| 5102 | #endif |
| 5103 | |
| 5104 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 5105 | conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED; |
| 5106 | #endif |
| 5107 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 5108 | #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] | 5109 | conf->f_cookie_write = ssl_cookie_write_dummy; |
| 5110 | conf->f_cookie_check = ssl_cookie_check_dummy; |
| 5111 | #endif |
| 5112 | |
| 5113 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 5114 | conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED; |
| 5115 | #endif |
| 5116 | |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 5117 | #if defined(MBEDTLS_SSL_SRV_C) |
| 5118 | conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED; |
TRodziewicz | 3946f79 | 2021-06-14 12:11:18 +0200 | [diff] [blame] | 5119 | conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER; |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 5120 | #endif |
| 5121 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5122 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 5123 | conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN; |
| 5124 | conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX; |
| 5125 | #endif |
| 5126 | |
| 5127 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 5128 | conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT; |
Andres AG | 2196c7f | 2016-12-15 17:01:16 +0000 | [diff] [blame] | 5129 | memset( conf->renego_period, 0x00, 2 ); |
| 5130 | memset( conf->renego_period + 2, 0xFF, 6 ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5131 | #endif |
| 5132 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5133 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
Hanno Becker | e2defad | 2021-07-24 05:59:17 +0100 | [diff] [blame] | 5134 | if( endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 5135 | { |
| 5136 | const unsigned char dhm_p[] = |
| 5137 | MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN; |
| 5138 | const unsigned char dhm_g[] = |
| 5139 | MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN; |
Hanno Becker | 00d0a68 | 2017-10-04 13:14:29 +0100 | [diff] [blame] | 5140 | |
Hanno Becker | e2defad | 2021-07-24 05:59:17 +0100 | [diff] [blame] | 5141 | if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf, |
| 5142 | dhm_p, sizeof( dhm_p ), |
| 5143 | dhm_g, sizeof( dhm_g ) ) ) != 0 ) |
| 5144 | { |
| 5145 | return( ret ); |
| 5146 | } |
| 5147 | } |
Manuel Pégourié-Gonnard | bd990d6 | 2015-06-11 14:49:42 +0200 | [diff] [blame] | 5148 | #endif |
| 5149 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 5150 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | 6ee56aa | 2022-12-06 17:47:22 +0800 | [diff] [blame] | 5151 | |
| 5152 | #if defined(MBEDTLS_SSL_EARLY_DATA) |
| 5153 | mbedtls_ssl_tls13_conf_early_data( conf, MBEDTLS_SSL_EARLY_DATA_DISABLED ); |
| 5154 | #if defined(MBEDTLS_SSL_SRV_C) |
| 5155 | mbedtls_ssl_tls13_conf_max_early_data_size( |
| 5156 | conf, MBEDTLS_SSL_MAX_EARLY_DATA_SIZE ); |
| 5157 | #endif |
| 5158 | #endif /* MBEDTLS_SSL_EARLY_DATA */ |
| 5159 | |
Jerry Yu | d0766ec | 2022-09-22 10:46:57 +0800 | [diff] [blame] | 5160 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SESSION_TICKETS) |
Jerry Yu | 1ad7ace | 2022-08-09 13:28:39 +0800 | [diff] [blame] | 5161 | mbedtls_ssl_conf_new_session_tickets( |
| 5162 | conf, MBEDTLS_SSL_TLS1_3_DEFAULT_NEW_SESSION_TICKETS ); |
| 5163 | #endif |
Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 5164 | /* |
| 5165 | * Allow all TLS 1.3 key exchange modes by default. |
| 5166 | */ |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 5167 | conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL; |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 5168 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 5169 | |
XiaokangQian | 4d3a604 | 2022-04-21 13:46:17 +0000 | [diff] [blame] | 5170 | if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 5171 | { |
Glenn Strauss | 2dfcea2 | 2022-03-14 17:26:42 -0400 | [diff] [blame] | 5172 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
XiaokangQian | 4d3a604 | 2022-04-21 13:46:17 +0000 | [diff] [blame] | 5173 | conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
XiaokangQian | 060d867 | 2022-04-21 09:24:56 +0000 | [diff] [blame] | 5174 | conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
XiaokangQian | 4d3a604 | 2022-04-21 13:46:17 +0000 | [diff] [blame] | 5175 | #else |
XiaokangQian | 060d867 | 2022-04-21 09:24:56 +0000 | [diff] [blame] | 5176 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
XiaokangQian | 060d867 | 2022-04-21 09:24:56 +0000 | [diff] [blame] | 5177 | #endif |
XiaokangQian | 4d3a604 | 2022-04-21 13:46:17 +0000 | [diff] [blame] | 5178 | } |
| 5179 | else |
| 5180 | { |
| 5181 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 5182 | if( endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 5183 | { |
| 5184 | conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 5185 | conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3; |
| 5186 | } |
| 5187 | else |
| 5188 | /* Hybrid TLS 1.2 / 1.3 is not supported on server side yet */ |
| 5189 | { |
| 5190 | conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 5191 | conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 5192 | } |
| 5193 | #elif defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 5194 | conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_3; |
| 5195 | conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3; |
| 5196 | #elif defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 5197 | conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 5198 | conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 5199 | #else |
| 5200 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 5201 | #endif |
| 5202 | } |
Glenn Strauss | 2dfcea2 | 2022-03-14 17:26:42 -0400 | [diff] [blame] | 5203 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5204 | /* |
| 5205 | * Preset-specific defaults |
| 5206 | */ |
| 5207 | switch( preset ) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5208 | { |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5209 | /* |
| 5210 | * NSA Suite B |
| 5211 | */ |
| 5212 | case MBEDTLS_SSL_PRESET_SUITEB: |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5213 | |
Hanno Becker | d60b6c6 | 2021-04-29 12:04:11 +0100 | [diff] [blame] | 5214 | conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5215 | |
| 5216 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 5217 | conf->cert_profile = &mbedtls_x509_crt_profile_suiteb; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5218 | #endif |
| 5219 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5220 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5221 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 5222 | if( mbedtls_ssl_conf_is_tls12_only( conf ) ) |
| 5223 | conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs; |
| 5224 | else |
| 5225 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 5226 | conf->sig_algs = ssl_preset_suiteb_sig_algs; |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5227 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5228 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5229 | #if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 5230 | conf->curve_list = NULL; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5231 | #endif |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5232 | conf->group_list = ssl_preset_suiteb_groups; |
Manuel Pégourié-Gonnard | c98204e | 2015-08-11 04:21:01 +0200 | [diff] [blame] | 5233 | break; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5234 | |
| 5235 | /* |
| 5236 | * Default |
| 5237 | */ |
| 5238 | default: |
Ronald Cron | f660655 | 2022-03-15 11:23:25 +0100 | [diff] [blame] | 5239 | |
Hanno Becker | d60b6c6 | 2021-04-29 12:04:11 +0100 | [diff] [blame] | 5240 | conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites(); |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5241 | |
| 5242 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 5243 | conf->cert_profile = &mbedtls_x509_crt_profile_default; |
| 5244 | #endif |
| 5245 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5246 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5247 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 5248 | if( mbedtls_ssl_conf_is_tls12_only( conf ) ) |
| 5249 | conf->sig_algs = ssl_tls12_preset_default_sig_algs; |
| 5250 | else |
| 5251 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 5252 | conf->sig_algs = ssl_preset_default_sig_algs; |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5253 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5254 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5255 | #if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 5256 | conf->curve_list = NULL; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5257 | #endif |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5258 | conf->group_list = ssl_preset_default_groups; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5259 | |
| 5260 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C) |
| 5261 | conf->dhm_min_bitlen = 1024; |
| 5262 | #endif |
| 5263 | } |
| 5264 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5265 | return( 0 ); |
| 5266 | } |
| 5267 | |
| 5268 | /* |
| 5269 | * Free mbedtls_ssl_config |
| 5270 | */ |
| 5271 | void mbedtls_ssl_config_free( mbedtls_ssl_config *conf ) |
| 5272 | { |
| 5273 | #if defined(MBEDTLS_DHM_C) |
| 5274 | mbedtls_mpi_free( &conf->dhm_P ); |
| 5275 | mbedtls_mpi_free( &conf->dhm_G ); |
| 5276 | #endif |
| 5277 | |
Ronald Cron | 73fe8df | 2022-10-05 14:31:43 +0200 | [diff] [blame] | 5278 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED) |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 5279 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 5280 | if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) ) |
| 5281 | { |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 5282 | conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT; |
| 5283 | } |
Neil Armstrong | 8ecd668 | 2022-05-05 11:40:35 +0200 | [diff] [blame] | 5284 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5285 | if( conf->psk != NULL ) |
| 5286 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 5287 | mbedtls_platform_zeroize( conf->psk, conf->psk_len ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5288 | mbedtls_free( conf->psk ); |
Azim Khan | 27e8a12 | 2018-03-21 14:24:11 +0000 | [diff] [blame] | 5289 | conf->psk = NULL; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5290 | conf->psk_len = 0; |
junyeonLEE | 316b162 | 2017-12-20 16:29:30 +0900 | [diff] [blame] | 5291 | } |
| 5292 | |
| 5293 | if( conf->psk_identity != NULL ) |
| 5294 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 5295 | mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len ); |
junyeonLEE | 316b162 | 2017-12-20 16:29:30 +0900 | [diff] [blame] | 5296 | mbedtls_free( conf->psk_identity ); |
Azim Khan | 27e8a12 | 2018-03-21 14:24:11 +0000 | [diff] [blame] | 5297 | conf->psk_identity = NULL; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5298 | conf->psk_identity_len = 0; |
| 5299 | } |
Ronald Cron | 73fe8df | 2022-10-05 14:31:43 +0200 | [diff] [blame] | 5300 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */ |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5301 | |
| 5302 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 5303 | ssl_key_cert_free( conf->key_cert ); |
| 5304 | #endif |
| 5305 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 5306 | mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5307 | } |
| 5308 | |
Manuel Pégourié-Gonnard | 5674a97 | 2015-10-19 15:14:03 +0200 | [diff] [blame] | 5309 | #if defined(MBEDTLS_PK_C) && \ |
| 5310 | ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) ) |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 5311 | /* |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5312 | * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 5313 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5314 | 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] | 5315 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5316 | #if defined(MBEDTLS_RSA_C) |
| 5317 | if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) ) |
| 5318 | return( MBEDTLS_SSL_SIG_RSA ); |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 5319 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5320 | #if defined(MBEDTLS_ECDSA_C) |
| 5321 | if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) ) |
| 5322 | return( MBEDTLS_SSL_SIG_ECDSA ); |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 5323 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5324 | return( MBEDTLS_SSL_SIG_ANON ); |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 5325 | } |
| 5326 | |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 5327 | unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type ) |
| 5328 | { |
| 5329 | switch( type ) { |
| 5330 | case MBEDTLS_PK_RSA: |
| 5331 | return( MBEDTLS_SSL_SIG_RSA ); |
| 5332 | case MBEDTLS_PK_ECDSA: |
| 5333 | case MBEDTLS_PK_ECKEY: |
| 5334 | return( MBEDTLS_SSL_SIG_ECDSA ); |
| 5335 | default: |
| 5336 | return( MBEDTLS_SSL_SIG_ANON ); |
| 5337 | } |
| 5338 | } |
| 5339 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5340 | 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] | 5341 | { |
| 5342 | switch( sig ) |
| 5343 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5344 | #if defined(MBEDTLS_RSA_C) |
| 5345 | case MBEDTLS_SSL_SIG_RSA: |
| 5346 | return( MBEDTLS_PK_RSA ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 5347 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5348 | #if defined(MBEDTLS_ECDSA_C) |
| 5349 | case MBEDTLS_SSL_SIG_ECDSA: |
| 5350 | return( MBEDTLS_PK_ECDSA ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 5351 | #endif |
| 5352 | default: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5353 | return( MBEDTLS_PK_NONE ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 5354 | } |
| 5355 | } |
Manuel Pégourié-Gonnard | 5674a97 | 2015-10-19 15:14:03 +0200 | [diff] [blame] | 5356 | #endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */ |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 5357 | |
Manuel Pégourié-Gonnard | 1a48383 | 2013-09-20 12:29:15 +0200 | [diff] [blame] | 5358 | /* |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 5359 | * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX |
Manuel Pégourié-Gonnard | 1a48383 | 2013-09-20 12:29:15 +0200 | [diff] [blame] | 5360 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5361 | 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] | 5362 | { |
| 5363 | switch( hash ) |
| 5364 | { |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5365 | #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] | 5366 | case MBEDTLS_SSL_HASH_MD5: |
| 5367 | return( MBEDTLS_MD_MD5 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 5368 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5369 | #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] | 5370 | case MBEDTLS_SSL_HASH_SHA1: |
| 5371 | return( MBEDTLS_MD_SHA1 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 5372 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5373 | #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] | 5374 | case MBEDTLS_SSL_HASH_SHA224: |
| 5375 | return( MBEDTLS_MD_SHA224 ); |
Mateusz Starzyk | e3c48b4 | 2021-04-19 16:46:28 +0200 | [diff] [blame] | 5376 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5377 | #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] | 5378 | case MBEDTLS_SSL_HASH_SHA256: |
| 5379 | return( MBEDTLS_MD_SHA256 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 5380 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5381 | #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] | 5382 | case MBEDTLS_SSL_HASH_SHA384: |
| 5383 | return( MBEDTLS_MD_SHA384 ); |
Mateusz Starzyk | 3352a53 | 2021-04-06 14:28:22 +0200 | [diff] [blame] | 5384 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5385 | #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] | 5386 | case MBEDTLS_SSL_HASH_SHA512: |
| 5387 | return( MBEDTLS_MD_SHA512 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 5388 | #endif |
| 5389 | default: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5390 | return( MBEDTLS_MD_NONE ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 5391 | } |
| 5392 | } |
| 5393 | |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 5394 | /* |
| 5395 | * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX |
| 5396 | */ |
| 5397 | unsigned char mbedtls_ssl_hash_from_md_alg( int md ) |
| 5398 | { |
| 5399 | switch( md ) |
| 5400 | { |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5401 | #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] | 5402 | case MBEDTLS_MD_MD5: |
| 5403 | return( MBEDTLS_SSL_HASH_MD5 ); |
| 5404 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5405 | #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] | 5406 | case MBEDTLS_MD_SHA1: |
| 5407 | return( MBEDTLS_SSL_HASH_SHA1 ); |
| 5408 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5409 | #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] | 5410 | case MBEDTLS_MD_SHA224: |
| 5411 | return( MBEDTLS_SSL_HASH_SHA224 ); |
Mateusz Starzyk | e3c48b4 | 2021-04-19 16:46:28 +0200 | [diff] [blame] | 5412 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5413 | #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] | 5414 | case MBEDTLS_MD_SHA256: |
| 5415 | return( MBEDTLS_SSL_HASH_SHA256 ); |
| 5416 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5417 | #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] | 5418 | case MBEDTLS_MD_SHA384: |
| 5419 | return( MBEDTLS_SSL_HASH_SHA384 ); |
Mateusz Starzyk | 3352a53 | 2021-04-06 14:28:22 +0200 | [diff] [blame] | 5420 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5421 | #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] | 5422 | case MBEDTLS_MD_SHA512: |
| 5423 | return( MBEDTLS_SSL_HASH_SHA512 ); |
| 5424 | #endif |
| 5425 | default: |
| 5426 | return( MBEDTLS_SSL_HASH_NONE ); |
| 5427 | } |
| 5428 | } |
| 5429 | |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 5430 | /* |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 5431 | * 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] | 5432 | * 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] | 5433 | */ |
Manuel Pégourié-Gonnard | 0d63b84 | 2022-01-18 13:10:56 +0100 | [diff] [blame] | 5434 | 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] | 5435 | { |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5436 | const uint16_t *group_list = mbedtls_ssl_get_groups( ssl ); |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 5437 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5438 | if( group_list == NULL ) |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 5439 | return( -1 ); |
| 5440 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5441 | for( ; *group_list != 0; group_list++ ) |
| 5442 | { |
| 5443 | if( *group_list == tls_id ) |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 5444 | return( 0 ); |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5445 | } |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 5446 | |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 5447 | return( -1 ); |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 5448 | } |
Manuel Pégourié-Gonnard | 0d63b84 | 2022-01-18 13:10:56 +0100 | [diff] [blame] | 5449 | |
| 5450 | #if defined(MBEDTLS_ECP_C) |
| 5451 | /* |
| 5452 | * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id. |
| 5453 | */ |
| 5454 | int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id ) |
| 5455 | { |
Leonid Rozenboim | 19e5973 | 2022-08-08 16:52:38 -0700 | [diff] [blame] | 5456 | const mbedtls_ecp_curve_info *grp_info = |
Tom Cosgrove | bcc13c9 | 2022-08-24 15:08:16 +0100 | [diff] [blame] | 5457 | mbedtls_ecp_curve_info_from_grp_id( grp_id ); |
Leonid Rozenboim | 19e5973 | 2022-08-08 16:52:38 -0700 | [diff] [blame] | 5458 | |
Tom Cosgrove | bcc13c9 | 2022-08-24 15:08:16 +0100 | [diff] [blame] | 5459 | if ( grp_info == NULL ) |
Leonid Rozenboim | 19e5973 | 2022-08-08 16:52:38 -0700 | [diff] [blame] | 5460 | return -1; |
| 5461 | |
| 5462 | uint16_t tls_id = grp_info->tls_id; |
| 5463 | |
Manuel Pégourié-Gonnard | 0d63b84 | 2022-01-18 13:10:56 +0100 | [diff] [blame] | 5464 | return mbedtls_ssl_check_curve_tls_id( ssl, tls_id ); |
| 5465 | } |
Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 5466 | #endif /* MBEDTLS_ECP_C */ |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5467 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5468 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 5469 | int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert, |
| 5470 | const mbedtls_ssl_ciphersuite_t *ciphersuite, |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 5471 | int cert_endpoint, |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 5472 | uint32_t *flags ) |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5473 | { |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 5474 | int ret = 0; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5475 | int usage = 0; |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 5476 | const char *ext_oid; |
| 5477 | size_t ext_len; |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 5478 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5479 | if( cert_endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5480 | { |
| 5481 | /* Server part of the key exchange */ |
| 5482 | switch( ciphersuite->key_exchange ) |
| 5483 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5484 | case MBEDTLS_KEY_EXCHANGE_RSA: |
| 5485 | case MBEDTLS_KEY_EXCHANGE_RSA_PSK: |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 5486 | usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5487 | break; |
| 5488 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5489 | case MBEDTLS_KEY_EXCHANGE_DHE_RSA: |
| 5490 | case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA: |
| 5491 | case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA: |
| 5492 | usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5493 | break; |
| 5494 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5495 | case MBEDTLS_KEY_EXCHANGE_ECDH_RSA: |
| 5496 | case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA: |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 5497 | usage = MBEDTLS_X509_KU_KEY_AGREEMENT; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5498 | break; |
| 5499 | |
| 5500 | /* 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] | 5501 | case MBEDTLS_KEY_EXCHANGE_NONE: |
| 5502 | case MBEDTLS_KEY_EXCHANGE_PSK: |
| 5503 | case MBEDTLS_KEY_EXCHANGE_DHE_PSK: |
| 5504 | case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK: |
Manuel Pégourié-Gonnard | 557535d | 2015-09-15 17:53:32 +0200 | [diff] [blame] | 5505 | case MBEDTLS_KEY_EXCHANGE_ECJPAKE: |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5506 | usage = 0; |
| 5507 | } |
| 5508 | } |
| 5509 | else |
| 5510 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5511 | /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */ |
| 5512 | usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5513 | } |
| 5514 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5515 | if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 ) |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 5516 | { |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 5517 | *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE; |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 5518 | ret = -1; |
| 5519 | } |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5520 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5521 | if( cert_endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 5522 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5523 | ext_oid = MBEDTLS_OID_SERVER_AUTH; |
| 5524 | ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH ); |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 5525 | } |
| 5526 | else |
| 5527 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5528 | ext_oid = MBEDTLS_OID_CLIENT_AUTH; |
| 5529 | ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH ); |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 5530 | } |
| 5531 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5532 | 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] | 5533 | { |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 5534 | *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE; |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 5535 | ret = -1; |
| 5536 | } |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 5537 | |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 5538 | return( ret ); |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5539 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5540 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Manuel Pégourié-Gonnard | 3a306b9 | 2014-04-29 15:11:17 +0200 | [diff] [blame] | 5541 | |
Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 5542 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 5543 | int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl, |
| 5544 | const mbedtls_md_type_t md, |
| 5545 | unsigned char *dst, |
| 5546 | size_t dst_len, |
| 5547 | size_t *olen ) |
| 5548 | { |
Ronald Cron | f6893e1 | 2022-01-07 22:09:01 +0100 | [diff] [blame] | 5549 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 5550 | psa_hash_operation_t *hash_operation_to_clone; |
| 5551 | psa_hash_operation_t hash_operation = psa_hash_operation_init(); |
| 5552 | |
Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 5553 | *olen = 0; |
Ronald Cron | f6893e1 | 2022-01-07 22:09:01 +0100 | [diff] [blame] | 5554 | |
| 5555 | switch( md ) |
| 5556 | { |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5557 | #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] | 5558 | case MBEDTLS_MD_SHA384: |
| 5559 | hash_operation_to_clone = &ssl->handshake->fin_sha384_psa; |
| 5560 | break; |
| 5561 | #endif |
| 5562 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5563 | #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] | 5564 | case MBEDTLS_MD_SHA256: |
| 5565 | hash_operation_to_clone = &ssl->handshake->fin_sha256_psa; |
| 5566 | break; |
| 5567 | #endif |
| 5568 | |
| 5569 | default: |
| 5570 | goto exit; |
| 5571 | } |
| 5572 | |
| 5573 | status = psa_hash_clone( hash_operation_to_clone, &hash_operation ); |
| 5574 | if( status != PSA_SUCCESS ) |
| 5575 | goto exit; |
| 5576 | |
| 5577 | status = psa_hash_finish( &hash_operation, dst, dst_len, olen ); |
| 5578 | if( status != PSA_SUCCESS ) |
| 5579 | goto exit; |
| 5580 | |
| 5581 | exit: |
Andrzej Kurek | eabeb30 | 2022-10-17 07:52:51 -0400 | [diff] [blame] | 5582 | #if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \ |
| 5583 | !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
| 5584 | (void) ssl; |
| 5585 | #endif |
Ronald Cron | b788c04 | 2022-02-15 09:14:15 +0100 | [diff] [blame] | 5586 | return( psa_ssl_status_to_mbedtls( status ) ); |
Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 5587 | } |
| 5588 | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
| 5589 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5590 | #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] | 5591 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | 000f976 | 2021-09-14 11:12:51 +0800 | [diff] [blame] | 5592 | static int ssl_get_handshake_transcript_sha384( mbedtls_ssl_context *ssl, |
| 5593 | unsigned char *dst, |
| 5594 | size_t dst_len, |
| 5595 | size_t *olen ) |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5596 | { |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5597 | int ret; |
| 5598 | mbedtls_sha512_context sha512; |
| 5599 | |
| 5600 | if( dst_len < 48 ) |
| 5601 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 5602 | |
| 5603 | mbedtls_sha512_init( &sha512 ); |
Andrzej Kurek | a242e83 | 2022-08-11 10:03:14 -0400 | [diff] [blame] | 5604 | mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 ); |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5605 | |
| 5606 | if( ( ret = mbedtls_sha512_finish( &sha512, dst ) ) != 0 ) |
| 5607 | { |
| 5608 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha512_finish", ret ); |
| 5609 | goto exit; |
| 5610 | } |
| 5611 | |
| 5612 | *olen = 48; |
| 5613 | |
| 5614 | exit: |
| 5615 | |
| 5616 | mbedtls_sha512_free( &sha512 ); |
| 5617 | return( ret ); |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5618 | } |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5619 | #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] | 5620 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5621 | #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] | 5622 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | 000f976 | 2021-09-14 11:12:51 +0800 | [diff] [blame] | 5623 | static int ssl_get_handshake_transcript_sha256( mbedtls_ssl_context *ssl, |
| 5624 | unsigned char *dst, |
| 5625 | size_t dst_len, |
| 5626 | size_t *olen ) |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5627 | { |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5628 | int ret; |
| 5629 | mbedtls_sha256_context sha256; |
| 5630 | |
| 5631 | if( dst_len < 32 ) |
| 5632 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 5633 | |
| 5634 | mbedtls_sha256_init( &sha256 ); |
| 5635 | mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 ); |
Jerry Yu | c5aef88 | 2021-12-23 20:15:02 +0800 | [diff] [blame] | 5636 | |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5637 | if( ( ret = mbedtls_sha256_finish( &sha256, dst ) ) != 0 ) |
| 5638 | { |
| 5639 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha256_finish", ret ); |
| 5640 | goto exit; |
| 5641 | } |
| 5642 | |
| 5643 | *olen = 32; |
| 5644 | |
| 5645 | exit: |
| 5646 | |
| 5647 | mbedtls_sha256_free( &sha256 ); |
| 5648 | return( ret ); |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5649 | } |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5650 | #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] | 5651 | |
Jerry Yu | 000f976 | 2021-09-14 11:12:51 +0800 | [diff] [blame] | 5652 | int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl, |
| 5653 | const mbedtls_md_type_t md, |
| 5654 | unsigned char *dst, |
| 5655 | size_t dst_len, |
| 5656 | size_t *olen ) |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5657 | { |
Jerry Yu | c1ddeef | 2021-10-08 15:14:45 +0800 | [diff] [blame] | 5658 | switch( md ) |
| 5659 | { |
| 5660 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5661 | #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] | 5662 | case MBEDTLS_MD_SHA384: |
Jerry Yu | c5aef88 | 2021-12-23 20:15:02 +0800 | [diff] [blame] | 5663 | return( ssl_get_handshake_transcript_sha384( ssl, dst, dst_len, olen ) ); |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 5664 | #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] | 5665 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5666 | #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] | 5667 | case MBEDTLS_MD_SHA256: |
Jerry Yu | c5aef88 | 2021-12-23 20:15:02 +0800 | [diff] [blame] | 5668 | return( ssl_get_handshake_transcript_sha256( ssl, dst, dst_len, olen ) ); |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 5669 | #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] | 5670 | |
| 5671 | default: |
Andrzej Kurek | 409248a | 2022-10-24 10:33:21 -0400 | [diff] [blame] | 5672 | #if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \ |
| 5673 | !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
| 5674 | (void) ssl; |
| 5675 | (void) dst; |
| 5676 | (void) dst_len; |
| 5677 | (void) olen; |
| 5678 | #endif |
Jerry Yu | c1ddeef | 2021-10-08 15:14:45 +0800 | [diff] [blame] | 5679 | break; |
| 5680 | } |
Jerry Yu | c5aef88 | 2021-12-23 20:15:02 +0800 | [diff] [blame] | 5681 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5682 | } |
XiaokangQian | 647719a | 2021-12-07 09:16:29 +0000 | [diff] [blame] | 5683 | |
Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 5684 | #endif /* !MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5685 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5686 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 5687 | /* mbedtls_ssl_parse_sig_alg_ext() |
| 5688 | * |
| 5689 | * The `extension_data` field of signature algorithm contains a `SignatureSchemeList` |
| 5690 | * value (TLS 1.3 RFC8446): |
| 5691 | * enum { |
| 5692 | * .... |
| 5693 | * ecdsa_secp256r1_sha256( 0x0403 ), |
| 5694 | * ecdsa_secp384r1_sha384( 0x0503 ), |
| 5695 | * ecdsa_secp521r1_sha512( 0x0603 ), |
| 5696 | * .... |
| 5697 | * } SignatureScheme; |
| 5698 | * |
| 5699 | * struct { |
| 5700 | * SignatureScheme supported_signature_algorithms<2..2^16-2>; |
| 5701 | * } SignatureSchemeList; |
| 5702 | * |
| 5703 | * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm` |
| 5704 | * value (TLS 1.2 RFC5246): |
| 5705 | * enum { |
| 5706 | * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5), |
| 5707 | * sha512(6), (255) |
| 5708 | * } HashAlgorithm; |
| 5709 | * |
| 5710 | * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) } |
| 5711 | * SignatureAlgorithm; |
| 5712 | * |
| 5713 | * struct { |
| 5714 | * HashAlgorithm hash; |
| 5715 | * SignatureAlgorithm signature; |
| 5716 | * } SignatureAndHashAlgorithm; |
| 5717 | * |
| 5718 | * SignatureAndHashAlgorithm |
| 5719 | * supported_signature_algorithms<2..2^16-2>; |
| 5720 | * |
| 5721 | * The TLS 1.3 signature algorithm extension was defined to be a compatible |
| 5722 | * generalization of the TLS 1.2 signature algorithm extension. |
| 5723 | * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by |
| 5724 | * `SignatureScheme` field of TLS 1.3 |
| 5725 | * |
| 5726 | */ |
| 5727 | int mbedtls_ssl_parse_sig_alg_ext( mbedtls_ssl_context *ssl, |
| 5728 | const unsigned char *buf, |
| 5729 | const unsigned char *end ) |
| 5730 | { |
| 5731 | const unsigned char *p = buf; |
| 5732 | size_t supported_sig_algs_len = 0; |
| 5733 | const unsigned char *supported_sig_algs_end; |
| 5734 | uint16_t sig_alg; |
| 5735 | uint32_t common_idx = 0; |
| 5736 | |
| 5737 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 ); |
| 5738 | supported_sig_algs_len = MBEDTLS_GET_UINT16_BE( p, 0 ); |
| 5739 | p += 2; |
| 5740 | |
| 5741 | memset( ssl->handshake->received_sig_algs, 0, |
| 5742 | sizeof(ssl->handshake->received_sig_algs) ); |
| 5743 | |
| 5744 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, supported_sig_algs_len ); |
| 5745 | supported_sig_algs_end = p + supported_sig_algs_len; |
| 5746 | while( p < supported_sig_algs_end ) |
| 5747 | { |
| 5748 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, supported_sig_algs_end, 2 ); |
| 5749 | sig_alg = MBEDTLS_GET_UINT16_BE( p, 0 ); |
| 5750 | p += 2; |
Jerry Yu | f3b46b5 | 2022-06-19 16:52:27 +0800 | [diff] [blame] | 5751 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "received signature algorithm: 0x%x %s", |
| 5752 | sig_alg, |
| 5753 | mbedtls_ssl_sig_alg_to_str( sig_alg ) ) ); |
Jerry Yu | 2fe6c63 | 2022-06-29 10:02:38 +0800 | [diff] [blame] | 5754 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Jerry Yu | 52b7d92 | 2022-07-01 18:03:31 +0800 | [diff] [blame] | 5755 | if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 && |
Jerry Yu | 2fe6c63 | 2022-06-29 10:02:38 +0800 | [diff] [blame] | 5756 | ( ! ( mbedtls_ssl_sig_alg_is_supported( ssl, sig_alg ) && |
| 5757 | mbedtls_ssl_sig_alg_is_offered( ssl, sig_alg ) ) ) ) |
| 5758 | { |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 5759 | continue; |
Jerry Yu | 2fe6c63 | 2022-06-29 10:02:38 +0800 | [diff] [blame] | 5760 | } |
| 5761 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 5762 | |
Jerry Yu | f3b46b5 | 2022-06-19 16:52:27 +0800 | [diff] [blame] | 5763 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "valid signature algorithm: %s", |
| 5764 | mbedtls_ssl_sig_alg_to_str( sig_alg ) ) ); |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 5765 | |
| 5766 | if( common_idx + 1 < MBEDTLS_RECEIVED_SIG_ALGS_SIZE ) |
| 5767 | { |
| 5768 | ssl->handshake->received_sig_algs[common_idx] = sig_alg; |
| 5769 | common_idx += 1; |
| 5770 | } |
| 5771 | } |
| 5772 | /* Check that we consumed all the message. */ |
| 5773 | if( p != end ) |
| 5774 | { |
| 5775 | MBEDTLS_SSL_DEBUG_MSG( 1, |
| 5776 | ( "Signature algorithms extension length misaligned" ) ); |
| 5777 | MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR, |
| 5778 | MBEDTLS_ERR_SSL_DECODE_ERROR ); |
| 5779 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
| 5780 | } |
| 5781 | |
| 5782 | if( common_idx == 0 ) |
| 5783 | { |
| 5784 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "no signature algorithm in common" ) ); |
| 5785 | MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE, |
| 5786 | MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE ); |
| 5787 | return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE ); |
| 5788 | } |
| 5789 | |
Gabor Mezei | 15b95a6 | 2022-05-09 16:37:58 +0200 | [diff] [blame] | 5790 | ssl->handshake->received_sig_algs[common_idx] = MBEDTLS_TLS_SIG_NONE; |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 5791 | return( 0 ); |
| 5792 | } |
| 5793 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5794 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 5795 | |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5796 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 5797 | |
| 5798 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 5799 | |
| 5800 | static psa_status_t setup_psa_key_derivation( psa_key_derivation_operation_t* derivation, |
| 5801 | mbedtls_svc_key_id_t key, |
| 5802 | psa_algorithm_t alg, |
Neil Armstrong | 8ecd668 | 2022-05-05 11:40:35 +0200 | [diff] [blame] | 5803 | const unsigned char* raw_psk, size_t raw_psk_length, |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5804 | const unsigned char* seed, size_t seed_length, |
| 5805 | const unsigned char* label, size_t label_length, |
Przemek Stekiel | 51a1f36 | 2022-04-13 08:57:06 +0200 | [diff] [blame] | 5806 | const unsigned char* other_secret, |
| 5807 | size_t other_secret_length, |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5808 | size_t capacity ) |
| 5809 | { |
| 5810 | psa_status_t status; |
| 5811 | |
| 5812 | status = psa_key_derivation_setup( derivation, alg ); |
| 5813 | if( status != PSA_SUCCESS ) |
| 5814 | return( status ); |
| 5815 | |
| 5816 | if( PSA_ALG_IS_TLS12_PRF( alg ) || PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) ) |
| 5817 | { |
| 5818 | status = psa_key_derivation_input_bytes( derivation, |
| 5819 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 5820 | seed, seed_length ); |
| 5821 | if( status != PSA_SUCCESS ) |
| 5822 | return( status ); |
| 5823 | |
Przemek Stekiel | 51a1f36 | 2022-04-13 08:57:06 +0200 | [diff] [blame] | 5824 | if ( other_secret != NULL ) |
Przemek Stekiel | 1f02703 | 2022-04-05 17:12:11 +0200 | [diff] [blame] | 5825 | { |
| 5826 | status = psa_key_derivation_input_bytes( derivation, |
Przemek Stekiel | 51a1f36 | 2022-04-13 08:57:06 +0200 | [diff] [blame] | 5827 | PSA_KEY_DERIVATION_INPUT_OTHER_SECRET, |
| 5828 | other_secret, other_secret_length ); |
Przemek Stekiel | 1f02703 | 2022-04-05 17:12:11 +0200 | [diff] [blame] | 5829 | if( status != PSA_SUCCESS ) |
| 5830 | return( status ); |
| 5831 | } |
| 5832 | |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5833 | if( mbedtls_svc_key_id_is_null( key ) ) |
| 5834 | { |
| 5835 | status = psa_key_derivation_input_bytes( |
| 5836 | derivation, PSA_KEY_DERIVATION_INPUT_SECRET, |
Neil Armstrong | 8ecd668 | 2022-05-05 11:40:35 +0200 | [diff] [blame] | 5837 | raw_psk, raw_psk_length ); |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5838 | } |
| 5839 | else |
| 5840 | { |
| 5841 | status = psa_key_derivation_input_key( |
| 5842 | derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key ); |
| 5843 | } |
| 5844 | if( status != PSA_SUCCESS ) |
| 5845 | return( status ); |
| 5846 | |
| 5847 | status = psa_key_derivation_input_bytes( derivation, |
| 5848 | PSA_KEY_DERIVATION_INPUT_LABEL, |
| 5849 | label, label_length ); |
| 5850 | if( status != PSA_SUCCESS ) |
| 5851 | return( status ); |
| 5852 | } |
| 5853 | else |
| 5854 | { |
| 5855 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5856 | } |
| 5857 | |
| 5858 | status = psa_key_derivation_set_capacity( derivation, capacity ); |
| 5859 | if( status != PSA_SUCCESS ) |
| 5860 | return( status ); |
| 5861 | |
| 5862 | return( PSA_SUCCESS ); |
| 5863 | } |
| 5864 | |
Andrzej Kurek | 57d1063 | 2022-10-24 10:32:01 -0400 | [diff] [blame] | 5865 | #if defined(PSA_WANT_ALG_SHA_384) || \ |
| 5866 | defined(PSA_WANT_ALG_SHA_256) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 5867 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5868 | static int tls_prf_generic( mbedtls_md_type_t md_type, |
| 5869 | const unsigned char *secret, size_t slen, |
| 5870 | const char *label, |
| 5871 | const unsigned char *random, size_t rlen, |
| 5872 | unsigned char *dstbuf, size_t dlen ) |
| 5873 | { |
| 5874 | psa_status_t status; |
| 5875 | psa_algorithm_t alg; |
| 5876 | mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5877 | psa_key_derivation_operation_t derivation = |
| 5878 | PSA_KEY_DERIVATION_OPERATION_INIT; |
| 5879 | |
| 5880 | if( md_type == MBEDTLS_MD_SHA384 ) |
| 5881 | alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384); |
| 5882 | else |
| 5883 | alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256); |
| 5884 | |
| 5885 | /* Normally a "secret" should be long enough to be impossible to |
| 5886 | * find by brute force, and in particular should not be empty. But |
| 5887 | * this PRF is also used to derive an IV, in particular in EAP-TLS, |
| 5888 | * and for this use case it makes sense to have a 0-length "secret". |
| 5889 | * Since the key API doesn't allow importing a key of length 0, |
| 5890 | * keep master_key=0, which setup_psa_key_derivation() understands |
| 5891 | * to mean a 0-length "secret" input. */ |
| 5892 | if( slen != 0 ) |
| 5893 | { |
| 5894 | psa_key_attributes_t key_attributes = psa_key_attributes_init(); |
| 5895 | psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE ); |
| 5896 | psa_set_key_algorithm( &key_attributes, alg ); |
| 5897 | psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE ); |
| 5898 | |
| 5899 | status = psa_import_key( &key_attributes, secret, slen, &master_key ); |
| 5900 | if( status != PSA_SUCCESS ) |
| 5901 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 5902 | } |
| 5903 | |
| 5904 | status = setup_psa_key_derivation( &derivation, |
| 5905 | master_key, alg, |
Neil Armstrong | 8ecd668 | 2022-05-05 11:40:35 +0200 | [diff] [blame] | 5906 | NULL, 0, |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5907 | random, rlen, |
| 5908 | (unsigned char const *) label, |
| 5909 | (size_t) strlen( label ), |
Przemek Stekiel | 1f02703 | 2022-04-05 17:12:11 +0200 | [diff] [blame] | 5910 | NULL, 0, |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5911 | dlen ); |
| 5912 | if( status != PSA_SUCCESS ) |
| 5913 | { |
| 5914 | psa_key_derivation_abort( &derivation ); |
| 5915 | psa_destroy_key( master_key ); |
| 5916 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 5917 | } |
| 5918 | |
| 5919 | status = psa_key_derivation_output_bytes( &derivation, dstbuf, dlen ); |
| 5920 | if( status != PSA_SUCCESS ) |
| 5921 | { |
| 5922 | psa_key_derivation_abort( &derivation ); |
| 5923 | psa_destroy_key( master_key ); |
| 5924 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 5925 | } |
| 5926 | |
| 5927 | status = psa_key_derivation_abort( &derivation ); |
| 5928 | if( status != PSA_SUCCESS ) |
| 5929 | { |
| 5930 | psa_destroy_key( master_key ); |
| 5931 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 5932 | } |
| 5933 | |
| 5934 | if( ! mbedtls_svc_key_id_is_null( master_key ) ) |
| 5935 | status = psa_destroy_key( master_key ); |
| 5936 | if( status != PSA_SUCCESS ) |
| 5937 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 5938 | |
| 5939 | return( 0 ); |
| 5940 | } |
Andrzej Kurek | 57d1063 | 2022-10-24 10:32:01 -0400 | [diff] [blame] | 5941 | #endif /* PSA_WANT_ALG_SHA_256 || PSA_WANT_ALG_SHA_384 */ |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5942 | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
| 5943 | |
Andrzej Kurek | 57d1063 | 2022-10-24 10:32:01 -0400 | [diff] [blame] | 5944 | #if defined(MBEDTLS_MD_C) && \ |
| 5945 | ( defined(MBEDTLS_SHA256_C) || \ |
| 5946 | defined(MBEDTLS_SHA384_C) ) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 5947 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5948 | static int tls_prf_generic( mbedtls_md_type_t md_type, |
| 5949 | const unsigned char *secret, size_t slen, |
| 5950 | const char *label, |
| 5951 | const unsigned char *random, size_t rlen, |
| 5952 | unsigned char *dstbuf, size_t dlen ) |
| 5953 | { |
| 5954 | size_t nb; |
| 5955 | size_t i, j, k, md_len; |
| 5956 | unsigned char *tmp; |
| 5957 | size_t tmp_len = 0; |
| 5958 | unsigned char h_i[MBEDTLS_MD_MAX_SIZE]; |
| 5959 | const mbedtls_md_info_t *md_info; |
| 5960 | mbedtls_md_context_t md_ctx; |
| 5961 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 5962 | |
| 5963 | mbedtls_md_init( &md_ctx ); |
| 5964 | |
| 5965 | if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL ) |
| 5966 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 5967 | |
| 5968 | md_len = mbedtls_md_get_size( md_info ); |
| 5969 | |
| 5970 | tmp_len = md_len + strlen( label ) + rlen; |
| 5971 | tmp = mbedtls_calloc( 1, tmp_len ); |
| 5972 | if( tmp == NULL ) |
| 5973 | { |
| 5974 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 5975 | goto exit; |
| 5976 | } |
| 5977 | |
| 5978 | nb = strlen( label ); |
| 5979 | memcpy( tmp + md_len, label, nb ); |
| 5980 | memcpy( tmp + md_len + nb, random, rlen ); |
| 5981 | nb += rlen; |
| 5982 | |
| 5983 | /* |
| 5984 | * Compute P_<hash>(secret, label + random)[0..dlen] |
| 5985 | */ |
| 5986 | if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 ) |
| 5987 | goto exit; |
| 5988 | |
| 5989 | ret = mbedtls_md_hmac_starts( &md_ctx, secret, slen ); |
| 5990 | if( ret != 0 ) |
| 5991 | goto exit; |
| 5992 | ret = mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb ); |
| 5993 | if( ret != 0 ) |
| 5994 | goto exit; |
| 5995 | ret = mbedtls_md_hmac_finish( &md_ctx, tmp ); |
| 5996 | if( ret != 0 ) |
| 5997 | goto exit; |
| 5998 | |
| 5999 | for( i = 0; i < dlen; i += md_len ) |
| 6000 | { |
| 6001 | ret = mbedtls_md_hmac_reset ( &md_ctx ); |
| 6002 | if( ret != 0 ) |
| 6003 | goto exit; |
| 6004 | ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb ); |
| 6005 | if( ret != 0 ) |
| 6006 | goto exit; |
| 6007 | ret = mbedtls_md_hmac_finish( &md_ctx, h_i ); |
| 6008 | if( ret != 0 ) |
| 6009 | goto exit; |
| 6010 | |
| 6011 | ret = mbedtls_md_hmac_reset ( &md_ctx ); |
| 6012 | if( ret != 0 ) |
| 6013 | goto exit; |
| 6014 | ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len ); |
| 6015 | if( ret != 0 ) |
| 6016 | goto exit; |
| 6017 | ret = mbedtls_md_hmac_finish( &md_ctx, tmp ); |
| 6018 | if( ret != 0 ) |
| 6019 | goto exit; |
| 6020 | |
| 6021 | k = ( i + md_len > dlen ) ? dlen % md_len : md_len; |
| 6022 | |
| 6023 | for( j = 0; j < k; j++ ) |
| 6024 | dstbuf[i + j] = h_i[j]; |
| 6025 | } |
| 6026 | |
| 6027 | exit: |
| 6028 | mbedtls_md_free( &md_ctx ); |
| 6029 | |
Dave Rodgman | 29b9b2b | 2022-11-01 16:08:14 +0000 | [diff] [blame] | 6030 | if ( tmp != NULL ) |
| 6031 | mbedtls_platform_zeroize( tmp, tmp_len ); |
| 6032 | |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6033 | mbedtls_platform_zeroize( h_i, sizeof( h_i ) ); |
| 6034 | |
| 6035 | mbedtls_free( tmp ); |
| 6036 | |
| 6037 | return( ret ); |
| 6038 | } |
Andrzej Kurek | 57d1063 | 2022-10-24 10:32:01 -0400 | [diff] [blame] | 6039 | #endif /* MBEDTLS_MD_C && ( MBEDTLS_SHA256_C || MBEDTLS_SHA384_C ) */ |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6040 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 6041 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 6042 | #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] | 6043 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6044 | static int tls_prf_sha256( const unsigned char *secret, size_t slen, |
| 6045 | const char *label, |
| 6046 | const unsigned char *random, size_t rlen, |
| 6047 | unsigned char *dstbuf, size_t dlen ) |
| 6048 | { |
| 6049 | return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen, |
| 6050 | label, random, rlen, dstbuf, dlen ) ); |
| 6051 | } |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 6052 | #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] | 6053 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 6054 | #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] | 6055 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6056 | static int tls_prf_sha384( const unsigned char *secret, size_t slen, |
| 6057 | const char *label, |
| 6058 | const unsigned char *random, size_t rlen, |
| 6059 | unsigned char *dstbuf, size_t dlen ) |
| 6060 | { |
| 6061 | return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen, |
| 6062 | label, random, rlen, dstbuf, dlen ) ); |
| 6063 | } |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 6064 | #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] | 6065 | |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 6066 | /* |
| 6067 | * Set appropriate PRF function and other SSL / TLS1.2 functions |
| 6068 | * |
| 6069 | * Inputs: |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 6070 | * - hash associated with the ciphersuite (only used by TLS 1.2) |
| 6071 | * |
| 6072 | * Outputs: |
| 6073 | * - the tls_prf, calc_verify and calc_finished members of handshake structure |
| 6074 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6075 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 6076 | static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake, |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 6077 | mbedtls_md_type_t hash ) |
| 6078 | { |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 6079 | #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] | 6080 | if( hash == MBEDTLS_MD_SHA384 ) |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 6081 | { |
| 6082 | handshake->tls_prf = tls_prf_sha384; |
| 6083 | handshake->calc_verify = ssl_calc_verify_tls_sha384; |
| 6084 | handshake->calc_finished = ssl_calc_finished_tls_sha384; |
| 6085 | } |
| 6086 | else |
| 6087 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 6088 | #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] | 6089 | { |
Ronald Cron | 81591aa | 2022-03-07 09:05:51 +0100 | [diff] [blame] | 6090 | (void) hash; |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 6091 | handshake->tls_prf = tls_prf_sha256; |
| 6092 | handshake->calc_verify = ssl_calc_verify_tls_sha256; |
| 6093 | handshake->calc_finished = ssl_calc_finished_tls_sha256; |
| 6094 | } |
Ronald Cron | 81591aa | 2022-03-07 09:05:51 +0100 | [diff] [blame] | 6095 | #else |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 6096 | { |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 6097 | (void) handshake; |
Ronald Cron | 81591aa | 2022-03-07 09:05:51 +0100 | [diff] [blame] | 6098 | (void) hash; |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 6099 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 6100 | } |
Ronald Cron | 81591aa | 2022-03-07 09:05:51 +0100 | [diff] [blame] | 6101 | #endif |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 6102 | |
| 6103 | return( 0 ); |
| 6104 | } |
Jerry Yu | d6ab235 | 2022-02-17 14:03:43 +0800 | [diff] [blame] | 6105 | |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6106 | /* |
| 6107 | * Compute master secret if needed |
| 6108 | * |
| 6109 | * Parameters: |
| 6110 | * [in/out] handshake |
| 6111 | * [in] resume, premaster, extended_ms, calc_verify, tls_prf |
| 6112 | * (PSA-PSK) ciphersuite_info, psk_opaque |
| 6113 | * [out] premaster (cleared) |
| 6114 | * [out] master |
| 6115 | * [in] ssl: optionally used for debugging, EMS and PSA-PSK |
| 6116 | * debug: conf->f_dbg, conf->p_dbg |
| 6117 | * EMS: passed to calc_verify (debug + session_negotiate) |
Ronald Cron | a25cf58 | 2022-03-07 11:10:36 +0100 | [diff] [blame] | 6118 | * PSA-PSA: conf |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6119 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6120 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6121 | static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake, |
| 6122 | unsigned char *master, |
| 6123 | const mbedtls_ssl_context *ssl ) |
| 6124 | { |
| 6125 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 6126 | |
| 6127 | /* cf. RFC 5246, Section 8.1: |
| 6128 | * "The master secret is always exactly 48 bytes in length." */ |
| 6129 | size_t const master_secret_len = 48; |
| 6130 | |
| 6131 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 6132 | unsigned char session_hash[48]; |
| 6133 | #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */ |
| 6134 | |
| 6135 | /* The label for the KDF used for key expansion. |
| 6136 | * This is either "master secret" or "extended master secret" |
| 6137 | * depending on whether the Extended Master Secret extension |
| 6138 | * is used. */ |
| 6139 | char const *lbl = "master secret"; |
| 6140 | |
Przemek Stekiel | ae4ed30 | 2022-04-05 17:15:55 +0200 | [diff] [blame] | 6141 | /* The seed for the KDF used for key expansion. |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6142 | * - If the Extended Master Secret extension is not used, |
| 6143 | * this is ClientHello.Random + ServerHello.Random |
| 6144 | * (see Sect. 8.1 in RFC 5246). |
| 6145 | * - If the Extended Master Secret extension is used, |
| 6146 | * this is the transcript of the handshake so far. |
| 6147 | * (see Sect. 4 in RFC 7627). */ |
Przemek Stekiel | ae4ed30 | 2022-04-05 17:15:55 +0200 | [diff] [blame] | 6148 | unsigned char const *seed = handshake->randbytes; |
| 6149 | size_t seed_len = 64; |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6150 | |
| 6151 | #if !defined(MBEDTLS_DEBUG_C) && \ |
| 6152 | !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \ |
| 6153 | !(defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
| 6154 | defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)) |
| 6155 | ssl = NULL; /* make sure we don't use it except for those cases */ |
| 6156 | (void) ssl; |
| 6157 | #endif |
| 6158 | |
| 6159 | if( handshake->resume != 0 ) |
| 6160 | { |
| 6161 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) ); |
| 6162 | return( 0 ); |
| 6163 | } |
| 6164 | |
| 6165 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 6166 | if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED ) |
| 6167 | { |
| 6168 | lbl = "extended master secret"; |
Przemek Stekiel | ae4ed30 | 2022-04-05 17:15:55 +0200 | [diff] [blame] | 6169 | seed = session_hash; |
| 6170 | handshake->calc_verify( ssl, session_hash, &seed_len ); |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6171 | |
| 6172 | MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret", |
Przemek Stekiel | ae4ed30 | 2022-04-05 17:15:55 +0200 | [diff] [blame] | 6173 | session_hash, seed_len ); |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6174 | } |
Przemek Stekiel | 169bf0b | 2022-04-29 07:53:29 +0200 | [diff] [blame] | 6175 | #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */ |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6176 | |
Przemek Stekiel | 99114f3 | 2022-04-22 11:20:09 +0200 | [diff] [blame] | 6177 | #if defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
Przemek Stekiel | 8a4b7fd | 2022-04-28 09:22:22 +0200 | [diff] [blame] | 6178 | defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Neil Armstrong | e952a30 | 2022-05-03 10:22:14 +0200 | [diff] [blame] | 6179 | if( mbedtls_ssl_ciphersuite_uses_psk( handshake->ciphersuite_info ) == 1 ) |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6180 | { |
| 6181 | /* Perform PSK-to-MS expansion in a single step. */ |
| 6182 | psa_status_t status; |
| 6183 | psa_algorithm_t alg; |
| 6184 | mbedtls_svc_key_id_t psk; |
| 6185 | psa_key_derivation_operation_t derivation = |
| 6186 | PSA_KEY_DERIVATION_OPERATION_INIT; |
| 6187 | mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac; |
| 6188 | |
| 6189 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) ); |
| 6190 | |
| 6191 | psk = mbedtls_ssl_get_opaque_psk( ssl ); |
| 6192 | |
| 6193 | if( hash_alg == MBEDTLS_MD_SHA384 ) |
| 6194 | alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384); |
| 6195 | else |
| 6196 | alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256); |
| 6197 | |
Przemek Stekiel | 51a1f36 | 2022-04-13 08:57:06 +0200 | [diff] [blame] | 6198 | size_t other_secret_len = 0; |
| 6199 | unsigned char* other_secret = NULL; |
Przemek Stekiel | c203340 | 2022-04-05 17:19:41 +0200 | [diff] [blame] | 6200 | |
Przemek Stekiel | 19b80f8 | 2022-04-14 08:29:31 +0200 | [diff] [blame] | 6201 | switch( handshake->ciphersuite_info->key_exchange ) |
Przemek Stekiel | c203340 | 2022-04-05 17:19:41 +0200 | [diff] [blame] | 6202 | { |
Przemek Stekiel | 19b80f8 | 2022-04-14 08:29:31 +0200 | [diff] [blame] | 6203 | /* Provide other secret. |
Przemek Stekiel | 51a1f36 | 2022-04-13 08:57:06 +0200 | [diff] [blame] | 6204 | * Other secret is stored in premaster, where first 2 bytes hold the |
Przemek Stekiel | 19b80f8 | 2022-04-14 08:29:31 +0200 | [diff] [blame] | 6205 | * length of the other key. |
Przemek Stekiel | c203340 | 2022-04-05 17:19:41 +0200 | [diff] [blame] | 6206 | */ |
Przemek Stekiel | 19b80f8 | 2022-04-14 08:29:31 +0200 | [diff] [blame] | 6207 | case MBEDTLS_KEY_EXCHANGE_RSA_PSK: |
Przemek Stekiel | 8abcee9 | 2022-04-28 09:16:28 +0200 | [diff] [blame] | 6208 | /* For RSA-PSK other key length is always 48 bytes. */ |
Przemek Stekiel | 19b80f8 | 2022-04-14 08:29:31 +0200 | [diff] [blame] | 6209 | other_secret_len = 48; |
| 6210 | other_secret = handshake->premaster + 2; |
| 6211 | break; |
| 6212 | case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK: |
Przemek Stekiel | b293aaa | 2022-04-19 12:22:38 +0200 | [diff] [blame] | 6213 | case MBEDTLS_KEY_EXCHANGE_DHE_PSK: |
Przemek Stekiel | 19b80f8 | 2022-04-14 08:29:31 +0200 | [diff] [blame] | 6214 | other_secret_len = MBEDTLS_GET_UINT16_BE(handshake->premaster, 0); |
| 6215 | other_secret = handshake->premaster + 2; |
| 6216 | break; |
| 6217 | default: |
| 6218 | break; |
Przemek Stekiel | c203340 | 2022-04-05 17:19:41 +0200 | [diff] [blame] | 6219 | } |
| 6220 | |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6221 | status = setup_psa_key_derivation( &derivation, psk, alg, |
Neil Armstrong | 8ecd668 | 2022-05-05 11:40:35 +0200 | [diff] [blame] | 6222 | ssl->conf->psk, ssl->conf->psk_len, |
Przemek Stekiel | ae4ed30 | 2022-04-05 17:15:55 +0200 | [diff] [blame] | 6223 | seed, seed_len, |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6224 | (unsigned char const *) lbl, |
| 6225 | (size_t) strlen( lbl ), |
Przemek Stekiel | 51a1f36 | 2022-04-13 08:57:06 +0200 | [diff] [blame] | 6226 | other_secret, other_secret_len, |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6227 | master_secret_len ); |
| 6228 | if( status != PSA_SUCCESS ) |
| 6229 | { |
| 6230 | psa_key_derivation_abort( &derivation ); |
| 6231 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 6232 | } |
| 6233 | |
| 6234 | status = psa_key_derivation_output_bytes( &derivation, |
| 6235 | master, |
| 6236 | master_secret_len ); |
| 6237 | if( status != PSA_SUCCESS ) |
| 6238 | { |
| 6239 | psa_key_derivation_abort( &derivation ); |
| 6240 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 6241 | } |
| 6242 | |
| 6243 | status = psa_key_derivation_abort( &derivation ); |
| 6244 | if( status != PSA_SUCCESS ) |
| 6245 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 6246 | } |
| 6247 | else |
| 6248 | #endif |
| 6249 | { |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 6250 | #if defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
| 6251 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
| 6252 | if( handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE ) |
| 6253 | { |
| 6254 | psa_status_t status; |
| 6255 | psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS; |
| 6256 | psa_key_derivation_operation_t derivation = |
| 6257 | PSA_KEY_DERIVATION_OPERATION_INIT; |
| 6258 | |
| 6259 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PMS KDF for ECJPAKE" ) ); |
| 6260 | |
| 6261 | handshake->pmslen = PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE; |
| 6262 | |
| 6263 | status = psa_key_derivation_setup( &derivation, alg ); |
| 6264 | if( status != PSA_SUCCESS ) |
| 6265 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 6266 | |
| 6267 | status = psa_key_derivation_set_capacity( &derivation, |
| 6268 | PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE ); |
| 6269 | if( status != PSA_SUCCESS ) |
| 6270 | { |
| 6271 | psa_key_derivation_abort( &derivation ); |
| 6272 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 6273 | } |
| 6274 | |
| 6275 | status = psa_pake_get_implicit_key( &handshake->psa_pake_ctx, |
| 6276 | &derivation ); |
| 6277 | if( status != PSA_SUCCESS ) |
| 6278 | { |
| 6279 | psa_key_derivation_abort( &derivation ); |
| 6280 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 6281 | } |
| 6282 | |
| 6283 | status = psa_key_derivation_output_bytes( &derivation, |
| 6284 | handshake->premaster, |
| 6285 | handshake->pmslen ); |
| 6286 | if( status != PSA_SUCCESS ) |
| 6287 | { |
| 6288 | psa_key_derivation_abort( &derivation ); |
| 6289 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 6290 | } |
| 6291 | |
| 6292 | status = psa_key_derivation_abort( &derivation ); |
| 6293 | if( status != PSA_SUCCESS ) |
| 6294 | { |
| 6295 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 6296 | } |
| 6297 | } |
| 6298 | #endif |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6299 | ret = handshake->tls_prf( handshake->premaster, handshake->pmslen, |
Przemek Stekiel | ae4ed30 | 2022-04-05 17:15:55 +0200 | [diff] [blame] | 6300 | lbl, seed, seed_len, |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6301 | master, |
| 6302 | master_secret_len ); |
| 6303 | if( ret != 0 ) |
| 6304 | { |
| 6305 | MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret ); |
| 6306 | return( ret ); |
| 6307 | } |
| 6308 | |
| 6309 | MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", |
| 6310 | handshake->premaster, |
| 6311 | handshake->pmslen ); |
| 6312 | |
| 6313 | mbedtls_platform_zeroize( handshake->premaster, |
| 6314 | sizeof(handshake->premaster) ); |
| 6315 | } |
| 6316 | |
| 6317 | return( 0 ); |
| 6318 | } |
| 6319 | |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 6320 | int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl ) |
| 6321 | { |
| 6322 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 6323 | const mbedtls_ssl_ciphersuite_t * const ciphersuite_info = |
| 6324 | ssl->handshake->ciphersuite_info; |
| 6325 | |
| 6326 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) ); |
| 6327 | |
| 6328 | /* Set PRF, calc_verify and calc_finished function pointers */ |
| 6329 | ret = ssl_set_handshake_prfs( ssl->handshake, |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 6330 | ciphersuite_info->mac ); |
| 6331 | if( ret != 0 ) |
| 6332 | { |
| 6333 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret ); |
| 6334 | return( ret ); |
| 6335 | } |
| 6336 | |
| 6337 | /* Compute master secret if needed */ |
| 6338 | ret = ssl_compute_master( ssl->handshake, |
| 6339 | ssl->session_negotiate->master, |
| 6340 | ssl ); |
| 6341 | if( ret != 0 ) |
| 6342 | { |
| 6343 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret ); |
| 6344 | return( ret ); |
| 6345 | } |
| 6346 | |
| 6347 | /* Swap the client and server random values: |
| 6348 | * - MS derivation wanted client+server (RFC 5246 8.1) |
| 6349 | * - key derivation wants server+client (RFC 5246 6.3) */ |
| 6350 | { |
| 6351 | unsigned char tmp[64]; |
| 6352 | memcpy( tmp, ssl->handshake->randbytes, 64 ); |
| 6353 | memcpy( ssl->handshake->randbytes, tmp + 32, 32 ); |
| 6354 | memcpy( ssl->handshake->randbytes + 32, tmp, 32 ); |
| 6355 | mbedtls_platform_zeroize( tmp, sizeof( tmp ) ); |
| 6356 | } |
| 6357 | |
| 6358 | /* Populate transform structure */ |
| 6359 | ret = ssl_tls12_populate_transform( ssl->transform_negotiate, |
| 6360 | ssl->session_negotiate->ciphersuite, |
| 6361 | ssl->session_negotiate->master, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 6362 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 6363 | ssl->session_negotiate->encrypt_then_mac, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 6364 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 6365 | ssl->handshake->tls_prf, |
| 6366 | ssl->handshake->randbytes, |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 6367 | ssl->tls_version, |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 6368 | ssl->conf->endpoint, |
| 6369 | ssl ); |
| 6370 | if( ret != 0 ) |
| 6371 | { |
| 6372 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls12_populate_transform", ret ); |
| 6373 | return( ret ); |
| 6374 | } |
| 6375 | |
| 6376 | /* We no longer need Server/ClientHello.random values */ |
| 6377 | mbedtls_platform_zeroize( ssl->handshake->randbytes, |
| 6378 | sizeof( ssl->handshake->randbytes ) ); |
| 6379 | |
| 6380 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) ); |
| 6381 | |
| 6382 | return( 0 ); |
| 6383 | } |
Jerry Yu | 8392e0d | 2022-02-17 14:10:24 +0800 | [diff] [blame] | 6384 | |
Ronald Cron | 4dcbca9 | 2022-03-07 10:21:40 +0100 | [diff] [blame] | 6385 | int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md ) |
| 6386 | { |
Ronald Cron | 4dcbca9 | 2022-03-07 10:21:40 +0100 | [diff] [blame] | 6387 | switch( md ) |
| 6388 | { |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 6389 | #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] | 6390 | case MBEDTLS_SSL_HASH_SHA384: |
| 6391 | ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384; |
| 6392 | break; |
| 6393 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 6394 | #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] | 6395 | case MBEDTLS_SSL_HASH_SHA256: |
| 6396 | ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256; |
| 6397 | break; |
| 6398 | #endif |
| 6399 | default: |
| 6400 | return( -1 ); |
| 6401 | } |
Andrzej Kurek | eabeb30 | 2022-10-17 07:52:51 -0400 | [diff] [blame] | 6402 | #if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \ |
| 6403 | !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
| 6404 | (void) ssl; |
| 6405 | #endif |
Ronald Cron | c2f13a0 | 2022-03-07 10:25:24 +0100 | [diff] [blame] | 6406 | return( 0 ); |
Ronald Cron | 4dcbca9 | 2022-03-07 10:21:40 +0100 | [diff] [blame] | 6407 | } |
| 6408 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 6409 | #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] | 6410 | void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl, |
| 6411 | unsigned char *hash, |
| 6412 | size_t *hlen ) |
| 6413 | { |
| 6414 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 6415 | size_t hash_size; |
| 6416 | psa_status_t status; |
| 6417 | psa_hash_operation_t sha256_psa = psa_hash_operation_init(); |
| 6418 | |
| 6419 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) ); |
| 6420 | status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa ); |
| 6421 | if( status != PSA_SUCCESS ) |
| 6422 | { |
| 6423 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); |
| 6424 | return; |
| 6425 | } |
| 6426 | |
| 6427 | status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size ); |
| 6428 | if( status != PSA_SUCCESS ) |
| 6429 | { |
| 6430 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); |
| 6431 | return; |
| 6432 | } |
| 6433 | |
| 6434 | *hlen = 32; |
| 6435 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen ); |
| 6436 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) ); |
| 6437 | #else |
| 6438 | mbedtls_sha256_context sha256; |
| 6439 | |
| 6440 | mbedtls_sha256_init( &sha256 ); |
| 6441 | |
| 6442 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) ); |
| 6443 | |
| 6444 | mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 ); |
| 6445 | mbedtls_sha256_finish( &sha256, hash ); |
| 6446 | |
| 6447 | *hlen = 32; |
| 6448 | |
| 6449 | MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen ); |
| 6450 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) ); |
| 6451 | |
| 6452 | mbedtls_sha256_free( &sha256 ); |
| 6453 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 6454 | return; |
| 6455 | } |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 6456 | #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] | 6457 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 6458 | #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] | 6459 | void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl, |
| 6460 | unsigned char *hash, |
| 6461 | size_t *hlen ) |
| 6462 | { |
| 6463 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 6464 | size_t hash_size; |
| 6465 | psa_status_t status; |
| 6466 | psa_hash_operation_t sha384_psa = psa_hash_operation_init(); |
| 6467 | |
| 6468 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) ); |
| 6469 | status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa ); |
| 6470 | if( status != PSA_SUCCESS ) |
| 6471 | { |
| 6472 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); |
| 6473 | return; |
| 6474 | } |
| 6475 | |
| 6476 | status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size ); |
| 6477 | if( status != PSA_SUCCESS ) |
| 6478 | { |
| 6479 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); |
| 6480 | return; |
| 6481 | } |
| 6482 | |
| 6483 | *hlen = 48; |
| 6484 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen ); |
| 6485 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) ); |
| 6486 | #else |
| 6487 | mbedtls_sha512_context sha512; |
| 6488 | |
| 6489 | mbedtls_sha512_init( &sha512 ); |
| 6490 | |
| 6491 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) ); |
| 6492 | |
Andrzej Kurek | a242e83 | 2022-08-11 10:03:14 -0400 | [diff] [blame] | 6493 | mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 ); |
Jerry Yu | c1cb384 | 2022-02-17 14:13:48 +0800 | [diff] [blame] | 6494 | mbedtls_sha512_finish( &sha512, hash ); |
| 6495 | |
| 6496 | *hlen = 48; |
| 6497 | |
| 6498 | MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen ); |
| 6499 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) ); |
| 6500 | |
| 6501 | mbedtls_sha512_free( &sha512 ); |
| 6502 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 6503 | return; |
| 6504 | } |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 6505 | #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] | 6506 | |
Neil Armstrong | 80f6f32 | 2022-05-03 17:56:38 +0200 | [diff] [blame] | 6507 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
| 6508 | defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6509 | int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex ) |
| 6510 | { |
| 6511 | unsigned char *p = ssl->handshake->premaster; |
| 6512 | unsigned char *end = p + sizeof( ssl->handshake->premaster ); |
| 6513 | const unsigned char *psk = NULL; |
| 6514 | size_t psk_len = 0; |
Przemek Stekiel | b293aaa | 2022-04-19 12:22:38 +0200 | [diff] [blame] | 6515 | int psk_ret = mbedtls_ssl_get_psk( ssl, &psk, &psk_len ); |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6516 | |
Przemek Stekiel | b293aaa | 2022-04-19 12:22:38 +0200 | [diff] [blame] | 6517 | if( psk_ret == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED ) |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6518 | { |
| 6519 | /* |
| 6520 | * This should never happen because the existence of a PSK is always |
Przemek Stekiel | b293aaa | 2022-04-19 12:22:38 +0200 | [diff] [blame] | 6521 | * checked before calling this function. |
| 6522 | * |
| 6523 | * The exception is opaque DHE-PSK. For DHE-PSK fill premaster with |
Przemek Stekiel | 8abcee9 | 2022-04-28 09:16:28 +0200 | [diff] [blame] | 6524 | * the shared secret without PSK. |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6525 | */ |
Przemek Stekiel | b293aaa | 2022-04-19 12:22:38 +0200 | [diff] [blame] | 6526 | if ( key_ex != MBEDTLS_KEY_EXCHANGE_DHE_PSK ) |
| 6527 | { |
| 6528 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 6529 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 6530 | } |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6531 | } |
| 6532 | |
| 6533 | /* |
| 6534 | * PMS = struct { |
| 6535 | * opaque other_secret<0..2^16-1>; |
| 6536 | * opaque psk<0..2^16-1>; |
| 6537 | * }; |
| 6538 | * with "other_secret" depending on the particular key exchange |
| 6539 | */ |
| 6540 | #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) |
| 6541 | if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK ) |
| 6542 | { |
| 6543 | if( end - p < 2 ) |
| 6544 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6545 | |
| 6546 | MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 ); |
| 6547 | p += 2; |
| 6548 | |
| 6549 | if( end < p || (size_t)( end - p ) < psk_len ) |
| 6550 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6551 | |
| 6552 | memset( p, 0, psk_len ); |
| 6553 | p += psk_len; |
| 6554 | } |
| 6555 | else |
| 6556 | #endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */ |
| 6557 | #if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) |
| 6558 | if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK ) |
| 6559 | { |
| 6560 | /* |
| 6561 | * other_secret already set by the ClientKeyExchange message, |
| 6562 | * and is 48 bytes long |
| 6563 | */ |
| 6564 | if( end - p < 2 ) |
| 6565 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6566 | |
| 6567 | *p++ = 0; |
| 6568 | *p++ = 48; |
| 6569 | p += 48; |
| 6570 | } |
| 6571 | else |
| 6572 | #endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */ |
| 6573 | #if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) |
| 6574 | if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK ) |
| 6575 | { |
| 6576 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 6577 | size_t len; |
| 6578 | |
| 6579 | /* Write length only when we know the actual value */ |
| 6580 | if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx, |
| 6581 | p + 2, end - ( p + 2 ), &len, |
| 6582 | ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 ) |
| 6583 | { |
| 6584 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret ); |
| 6585 | return( ret ); |
| 6586 | } |
| 6587 | MBEDTLS_PUT_UINT16_BE( len, p, 0 ); |
| 6588 | p += 2 + len; |
| 6589 | |
| 6590 | MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K ); |
| 6591 | } |
| 6592 | else |
| 6593 | #endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */ |
Neil Armstrong | 80f6f32 | 2022-05-03 17:56:38 +0200 | [diff] [blame] | 6594 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6595 | if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ) |
| 6596 | { |
| 6597 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 6598 | size_t zlen; |
| 6599 | |
| 6600 | if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen, |
| 6601 | p + 2, end - ( p + 2 ), |
| 6602 | ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 ) |
| 6603 | { |
| 6604 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret ); |
| 6605 | return( ret ); |
| 6606 | } |
| 6607 | |
| 6608 | MBEDTLS_PUT_UINT16_BE( zlen, p, 0 ); |
| 6609 | p += 2 + zlen; |
| 6610 | |
| 6611 | MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx, |
| 6612 | MBEDTLS_DEBUG_ECDH_Z ); |
| 6613 | } |
| 6614 | else |
Neil Armstrong | 80f6f32 | 2022-05-03 17:56:38 +0200 | [diff] [blame] | 6615 | #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */ |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6616 | { |
| 6617 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 6618 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 6619 | } |
| 6620 | |
| 6621 | /* opaque psk<0..2^16-1>; */ |
| 6622 | if( end - p < 2 ) |
| 6623 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6624 | |
| 6625 | MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 ); |
| 6626 | p += 2; |
| 6627 | |
| 6628 | if( end < p || (size_t)( end - p ) < psk_len ) |
| 6629 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6630 | |
| 6631 | memcpy( p, psk, psk_len ); |
| 6632 | p += psk_len; |
| 6633 | |
| 6634 | ssl->handshake->pmslen = p - ssl->handshake->premaster; |
| 6635 | |
| 6636 | return( 0 ); |
| 6637 | } |
Neil Armstrong | 80f6f32 | 2022-05-03 17:56:38 +0200 | [diff] [blame] | 6638 | #endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ |
Jerry Yu | c2c673d | 2022-02-17 14:20:39 +0800 | [diff] [blame] | 6639 | |
| 6640 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6641 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | c2c673d | 2022-02-17 14:20:39 +0800 | [diff] [blame] | 6642 | static int ssl_write_hello_request( mbedtls_ssl_context *ssl ); |
| 6643 | |
| 6644 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 6645 | int mbedtls_ssl_resend_hello_request( mbedtls_ssl_context *ssl ) |
| 6646 | { |
| 6647 | /* If renegotiation is not enforced, retransmit until we would reach max |
| 6648 | * timeout if we were using the usual handshake doubling scheme */ |
| 6649 | if( ssl->conf->renego_max_records < 0 ) |
| 6650 | { |
| 6651 | uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1; |
| 6652 | unsigned char doublings = 1; |
| 6653 | |
| 6654 | while( ratio != 0 ) |
| 6655 | { |
| 6656 | ++doublings; |
| 6657 | ratio >>= 1; |
| 6658 | } |
| 6659 | |
| 6660 | if( ++ssl->renego_records_seen > doublings ) |
| 6661 | { |
| 6662 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) ); |
| 6663 | return( 0 ); |
| 6664 | } |
| 6665 | } |
| 6666 | |
| 6667 | return( ssl_write_hello_request( ssl ) ); |
| 6668 | } |
| 6669 | #endif |
| 6670 | #endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */ |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6671 | |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6672 | /* |
| 6673 | * Handshake functions |
| 6674 | */ |
| 6675 | #if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
| 6676 | /* No certificate support -> dummy functions */ |
| 6677 | int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl ) |
| 6678 | { |
| 6679 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 6680 | ssl->handshake->ciphersuite_info; |
| 6681 | |
| 6682 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) ); |
| 6683 | |
| 6684 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) |
| 6685 | { |
| 6686 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) ); |
| 6687 | ssl->state++; |
| 6688 | return( 0 ); |
| 6689 | } |
| 6690 | |
| 6691 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 6692 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 6693 | } |
| 6694 | |
| 6695 | int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl ) |
| 6696 | { |
| 6697 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 6698 | ssl->handshake->ciphersuite_info; |
| 6699 | |
| 6700 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) ); |
| 6701 | |
| 6702 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) |
| 6703 | { |
| 6704 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) ); |
| 6705 | ssl->state++; |
| 6706 | return( 0 ); |
| 6707 | } |
| 6708 | |
| 6709 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 6710 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 6711 | } |
| 6712 | |
| 6713 | #else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 6714 | /* Some certificate support -> implement write and parse */ |
| 6715 | |
| 6716 | int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl ) |
| 6717 | { |
| 6718 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
| 6719 | size_t i, n; |
| 6720 | const mbedtls_x509_crt *crt; |
| 6721 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 6722 | ssl->handshake->ciphersuite_info; |
| 6723 | |
| 6724 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) ); |
| 6725 | |
| 6726 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) |
| 6727 | { |
| 6728 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) ); |
| 6729 | ssl->state++; |
| 6730 | return( 0 ); |
| 6731 | } |
| 6732 | |
| 6733 | #if defined(MBEDTLS_SSL_CLI_C) |
| 6734 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 6735 | { |
| 6736 | if( ssl->handshake->client_auth == 0 ) |
| 6737 | { |
| 6738 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) ); |
| 6739 | ssl->state++; |
| 6740 | return( 0 ); |
| 6741 | } |
| 6742 | } |
| 6743 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 6744 | #if defined(MBEDTLS_SSL_SRV_C) |
| 6745 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 6746 | { |
| 6747 | if( mbedtls_ssl_own_cert( ssl ) == NULL ) |
| 6748 | { |
| 6749 | /* Should never happen because we shouldn't have picked the |
| 6750 | * ciphersuite if we don't have a certificate. */ |
| 6751 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 6752 | } |
| 6753 | } |
| 6754 | #endif |
| 6755 | |
| 6756 | MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) ); |
| 6757 | |
| 6758 | /* |
| 6759 | * 0 . 0 handshake type |
| 6760 | * 1 . 3 handshake length |
| 6761 | * 4 . 6 length of all certs |
| 6762 | * 7 . 9 length of cert. 1 |
| 6763 | * 10 . n-1 peer certificate |
| 6764 | * n . n+2 length of cert. 2 |
| 6765 | * n+3 . ... upper level cert, etc. |
| 6766 | */ |
| 6767 | i = 7; |
| 6768 | crt = mbedtls_ssl_own_cert( ssl ); |
| 6769 | |
| 6770 | while( crt != NULL ) |
| 6771 | { |
| 6772 | n = crt->raw.len; |
| 6773 | if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i ) |
| 6774 | { |
| 6775 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %" MBEDTLS_PRINTF_SIZET |
| 6776 | " > %" MBEDTLS_PRINTF_SIZET, |
| 6777 | i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN ) ); |
| 6778 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 6779 | } |
| 6780 | |
| 6781 | ssl->out_msg[i ] = MBEDTLS_BYTE_2( n ); |
| 6782 | ssl->out_msg[i + 1] = MBEDTLS_BYTE_1( n ); |
| 6783 | ssl->out_msg[i + 2] = MBEDTLS_BYTE_0( n ); |
| 6784 | |
| 6785 | i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n ); |
| 6786 | i += n; crt = crt->next; |
| 6787 | } |
| 6788 | |
| 6789 | ssl->out_msg[4] = MBEDTLS_BYTE_2( i - 7 ); |
| 6790 | ssl->out_msg[5] = MBEDTLS_BYTE_1( i - 7 ); |
| 6791 | ssl->out_msg[6] = MBEDTLS_BYTE_0( i - 7 ); |
| 6792 | |
| 6793 | ssl->out_msglen = i; |
| 6794 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 6795 | ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE; |
| 6796 | |
| 6797 | ssl->state++; |
| 6798 | |
| 6799 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
| 6800 | { |
| 6801 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); |
| 6802 | return( ret ); |
| 6803 | } |
| 6804 | |
| 6805 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) ); |
| 6806 | |
| 6807 | return( ret ); |
| 6808 | } |
| 6809 | |
| 6810 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) |
| 6811 | |
| 6812 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6813 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6814 | static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl, |
| 6815 | unsigned char *crt_buf, |
| 6816 | size_t crt_buf_len ) |
| 6817 | { |
| 6818 | mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert; |
| 6819 | |
| 6820 | if( peer_crt == NULL ) |
| 6821 | return( -1 ); |
| 6822 | |
| 6823 | if( peer_crt->raw.len != crt_buf_len ) |
| 6824 | return( -1 ); |
| 6825 | |
| 6826 | return( memcmp( peer_crt->raw.p, crt_buf, peer_crt->raw.len ) ); |
| 6827 | } |
| 6828 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6829 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6830 | static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl, |
| 6831 | unsigned char *crt_buf, |
| 6832 | size_t crt_buf_len ) |
| 6833 | { |
| 6834 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 6835 | unsigned char const * const peer_cert_digest = |
| 6836 | ssl->session->peer_cert_digest; |
| 6837 | mbedtls_md_type_t const peer_cert_digest_type = |
| 6838 | ssl->session->peer_cert_digest_type; |
| 6839 | mbedtls_md_info_t const * const digest_info = |
| 6840 | mbedtls_md_info_from_type( peer_cert_digest_type ); |
| 6841 | unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN]; |
| 6842 | size_t digest_len; |
| 6843 | |
| 6844 | if( peer_cert_digest == NULL || digest_info == NULL ) |
| 6845 | return( -1 ); |
| 6846 | |
| 6847 | digest_len = mbedtls_md_get_size( digest_info ); |
| 6848 | if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN ) |
| 6849 | return( -1 ); |
| 6850 | |
| 6851 | ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest ); |
| 6852 | if( ret != 0 ) |
| 6853 | return( -1 ); |
| 6854 | |
| 6855 | return( memcmp( tmp_digest, peer_cert_digest, digest_len ) ); |
| 6856 | } |
| 6857 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 6858 | #endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */ |
| 6859 | |
| 6860 | /* |
| 6861 | * Once the certificate message is read, parse it into a cert chain and |
| 6862 | * perform basic checks, but leave actual verification to the caller |
| 6863 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6864 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6865 | static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl, |
| 6866 | mbedtls_x509_crt *chain ) |
| 6867 | { |
| 6868 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 6869 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) |
| 6870 | int crt_cnt=0; |
| 6871 | #endif |
| 6872 | size_t i, n; |
| 6873 | uint8_t alert; |
| 6874 | |
| 6875 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) |
| 6876 | { |
| 6877 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
| 6878 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6879 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
| 6880 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
| 6881 | } |
| 6882 | |
| 6883 | if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ) |
| 6884 | { |
| 6885 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6886 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
| 6887 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
| 6888 | } |
| 6889 | |
| 6890 | if( ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 ) |
| 6891 | { |
| 6892 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
| 6893 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6894 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
| 6895 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
| 6896 | } |
| 6897 | |
| 6898 | i = mbedtls_ssl_hs_hdr_len( ssl ); |
| 6899 | |
| 6900 | /* |
| 6901 | * Same message structure as in mbedtls_ssl_write_certificate() |
| 6902 | */ |
| 6903 | n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2]; |
| 6904 | |
| 6905 | if( ssl->in_msg[i] != 0 || |
| 6906 | ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) ) |
| 6907 | { |
| 6908 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
| 6909 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6910 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
| 6911 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
| 6912 | } |
| 6913 | |
| 6914 | /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */ |
| 6915 | i += 3; |
| 6916 | |
| 6917 | /* Iterate through and parse the CRTs in the provided chain. */ |
| 6918 | while( i < ssl->in_hslen ) |
| 6919 | { |
| 6920 | /* Check that there's room for the next CRT's length fields. */ |
| 6921 | if ( i + 3 > ssl->in_hslen ) { |
| 6922 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
| 6923 | mbedtls_ssl_send_alert_message( ssl, |
| 6924 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6925 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
| 6926 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
| 6927 | } |
| 6928 | /* In theory, the CRT can be up to 2**24 Bytes, but we don't support |
| 6929 | * anything beyond 2**16 ~ 64K. */ |
| 6930 | if( ssl->in_msg[i] != 0 ) |
| 6931 | { |
| 6932 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
| 6933 | mbedtls_ssl_send_alert_message( ssl, |
| 6934 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6935 | MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT ); |
| 6936 | return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE ); |
| 6937 | } |
| 6938 | |
| 6939 | /* Read length of the next CRT in the chain. */ |
| 6940 | n = ( (unsigned int) ssl->in_msg[i + 1] << 8 ) |
| 6941 | | (unsigned int) ssl->in_msg[i + 2]; |
| 6942 | i += 3; |
| 6943 | |
| 6944 | if( n < 128 || i + n > ssl->in_hslen ) |
| 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_DECODE_ERROR ); |
| 6950 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
| 6951 | } |
| 6952 | |
| 6953 | /* Check if we're handling the first CRT in the chain. */ |
| 6954 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) |
| 6955 | if( crt_cnt++ == 0 && |
| 6956 | ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
| 6957 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
| 6958 | { |
| 6959 | /* During client-side renegotiation, check that the server's |
| 6960 | * end-CRTs hasn't changed compared to the initial handshake, |
| 6961 | * mitigating the triple handshake attack. On success, reuse |
| 6962 | * the original end-CRT instead of parsing it again. */ |
| 6963 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) ); |
| 6964 | if( ssl_check_peer_crt_unchanged( ssl, |
| 6965 | &ssl->in_msg[i], |
| 6966 | n ) != 0 ) |
| 6967 | { |
| 6968 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) ); |
| 6969 | mbedtls_ssl_send_alert_message( ssl, |
| 6970 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6971 | MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED ); |
| 6972 | return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE ); |
| 6973 | } |
| 6974 | |
| 6975 | /* Now we can safely free the original chain. */ |
| 6976 | ssl_clear_peer_cert( ssl->session ); |
| 6977 | } |
| 6978 | #endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */ |
| 6979 | |
| 6980 | /* Parse the next certificate in the chain. */ |
| 6981 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 6982 | ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n ); |
| 6983 | #else |
| 6984 | /* If we don't need to store the CRT chain permanently, parse |
| 6985 | * it in-place from the input buffer instead of making a copy. */ |
| 6986 | ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n ); |
| 6987 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 6988 | switch( ret ) |
| 6989 | { |
| 6990 | case 0: /*ok*/ |
| 6991 | case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND: |
| 6992 | /* Ignore certificate with an unknown algorithm: maybe a |
| 6993 | prior certificate was already trusted. */ |
| 6994 | break; |
| 6995 | |
| 6996 | case MBEDTLS_ERR_X509_ALLOC_FAILED: |
| 6997 | alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR; |
| 6998 | goto crt_parse_der_failed; |
| 6999 | |
| 7000 | case MBEDTLS_ERR_X509_UNKNOWN_VERSION: |
| 7001 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 7002 | goto crt_parse_der_failed; |
| 7003 | |
| 7004 | default: |
| 7005 | alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT; |
| 7006 | crt_parse_der_failed: |
| 7007 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert ); |
| 7008 | MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret ); |
| 7009 | return( ret ); |
| 7010 | } |
| 7011 | |
| 7012 | i += n; |
| 7013 | } |
| 7014 | |
| 7015 | MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain ); |
| 7016 | return( 0 ); |
| 7017 | } |
| 7018 | |
| 7019 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 7020 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7021 | static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl ) |
| 7022 | { |
| 7023 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 7024 | return( -1 ); |
| 7025 | |
| 7026 | if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) && |
| 7027 | ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 7028 | ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE && |
| 7029 | memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 ) |
| 7030 | { |
Ronald Cron | 1938588 | 2022-06-15 16:26:13 +0200 | [diff] [blame] | 7031 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "peer has no certificate" ) ); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7032 | return( 0 ); |
| 7033 | } |
| 7034 | return( -1 ); |
| 7035 | } |
| 7036 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 7037 | |
| 7038 | /* Check if a certificate message is expected. |
| 7039 | * Return either |
| 7040 | * - SSL_CERTIFICATE_EXPECTED, or |
| 7041 | * - SSL_CERTIFICATE_SKIP |
| 7042 | * indicating whether a Certificate message is expected or not. |
| 7043 | */ |
| 7044 | #define SSL_CERTIFICATE_EXPECTED 0 |
| 7045 | #define SSL_CERTIFICATE_SKIP 1 |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 7046 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7047 | static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl, |
| 7048 | int authmode ) |
| 7049 | { |
| 7050 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 7051 | ssl->handshake->ciphersuite_info; |
| 7052 | |
| 7053 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) |
| 7054 | return( SSL_CERTIFICATE_SKIP ); |
| 7055 | |
| 7056 | #if defined(MBEDTLS_SSL_SRV_C) |
| 7057 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 7058 | { |
| 7059 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ) |
| 7060 | return( SSL_CERTIFICATE_SKIP ); |
| 7061 | |
| 7062 | if( authmode == MBEDTLS_SSL_VERIFY_NONE ) |
| 7063 | { |
| 7064 | ssl->session_negotiate->verify_result = |
| 7065 | MBEDTLS_X509_BADCERT_SKIP_VERIFY; |
| 7066 | return( SSL_CERTIFICATE_SKIP ); |
| 7067 | } |
| 7068 | } |
| 7069 | #else |
| 7070 | ((void) authmode); |
| 7071 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 7072 | |
| 7073 | return( SSL_CERTIFICATE_EXPECTED ); |
| 7074 | } |
| 7075 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 7076 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7077 | static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl, |
| 7078 | int authmode, |
| 7079 | mbedtls_x509_crt *chain, |
| 7080 | void *rs_ctx ) |
| 7081 | { |
| 7082 | int ret = 0; |
| 7083 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 7084 | ssl->handshake->ciphersuite_info; |
| 7085 | int have_ca_chain = 0; |
| 7086 | |
| 7087 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *); |
| 7088 | void *p_vrfy; |
| 7089 | |
| 7090 | if( authmode == MBEDTLS_SSL_VERIFY_NONE ) |
| 7091 | return( 0 ); |
| 7092 | |
| 7093 | if( ssl->f_vrfy != NULL ) |
| 7094 | { |
| 7095 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) ); |
| 7096 | f_vrfy = ssl->f_vrfy; |
| 7097 | p_vrfy = ssl->p_vrfy; |
| 7098 | } |
| 7099 | else |
| 7100 | { |
| 7101 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) ); |
| 7102 | f_vrfy = ssl->conf->f_vrfy; |
| 7103 | p_vrfy = ssl->conf->p_vrfy; |
| 7104 | } |
| 7105 | |
| 7106 | /* |
| 7107 | * Main check: verify certificate |
| 7108 | */ |
| 7109 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 7110 | if( ssl->conf->f_ca_cb != NULL ) |
| 7111 | { |
| 7112 | ((void) rs_ctx); |
| 7113 | have_ca_chain = 1; |
| 7114 | |
| 7115 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) ); |
| 7116 | ret = mbedtls_x509_crt_verify_with_ca_cb( |
| 7117 | chain, |
| 7118 | ssl->conf->f_ca_cb, |
| 7119 | ssl->conf->p_ca_cb, |
| 7120 | ssl->conf->cert_profile, |
| 7121 | ssl->hostname, |
| 7122 | &ssl->session_negotiate->verify_result, |
| 7123 | f_vrfy, p_vrfy ); |
| 7124 | } |
| 7125 | else |
| 7126 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
| 7127 | { |
| 7128 | mbedtls_x509_crt *ca_chain; |
| 7129 | mbedtls_x509_crl *ca_crl; |
| 7130 | |
| 7131 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 7132 | if( ssl->handshake->sni_ca_chain != NULL ) |
| 7133 | { |
| 7134 | ca_chain = ssl->handshake->sni_ca_chain; |
| 7135 | ca_crl = ssl->handshake->sni_ca_crl; |
| 7136 | } |
| 7137 | else |
| 7138 | #endif |
| 7139 | { |
| 7140 | ca_chain = ssl->conf->ca_chain; |
| 7141 | ca_crl = ssl->conf->ca_crl; |
| 7142 | } |
| 7143 | |
| 7144 | if( ca_chain != NULL ) |
| 7145 | have_ca_chain = 1; |
| 7146 | |
| 7147 | ret = mbedtls_x509_crt_verify_restartable( |
| 7148 | chain, |
| 7149 | ca_chain, ca_crl, |
| 7150 | ssl->conf->cert_profile, |
| 7151 | ssl->hostname, |
| 7152 | &ssl->session_negotiate->verify_result, |
| 7153 | f_vrfy, p_vrfy, rs_ctx ); |
| 7154 | } |
| 7155 | |
| 7156 | if( ret != 0 ) |
| 7157 | { |
| 7158 | MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret ); |
| 7159 | } |
| 7160 | |
| 7161 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
| 7162 | if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS ) |
| 7163 | return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS ); |
| 7164 | #endif |
| 7165 | |
| 7166 | /* |
| 7167 | * Secondary checks: always done, but change 'ret' only if it was 0 |
| 7168 | */ |
| 7169 | |
| 7170 | #if defined(MBEDTLS_ECP_C) |
| 7171 | { |
| 7172 | const mbedtls_pk_context *pk = &chain->pk; |
| 7173 | |
Manuel Pégourié-Gonnard | 66b0d61 | 2022-06-17 10:49:29 +0200 | [diff] [blame] | 7174 | /* If certificate uses an EC key, make sure the curve is OK. |
| 7175 | * This is a public key, so it can't be opaque, so can_do() is a good |
| 7176 | * enough check to ensure pk_ec() is safe to use here. */ |
Leonid Rozenboim | 19e5973 | 2022-08-08 16:52:38 -0700 | [diff] [blame] | 7177 | if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) ) |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7178 | { |
Leonid Rozenboim | 19e5973 | 2022-08-08 16:52:38 -0700 | [diff] [blame] | 7179 | /* and in the unlikely case the above assumption no longer holds |
| 7180 | * we are making sure that pk_ec() here does not return a NULL |
| 7181 | */ |
| 7182 | const mbedtls_ecp_keypair *ec = mbedtls_pk_ec( *pk ); |
| 7183 | if( ec == NULL ) |
| 7184 | { |
Tom Cosgrove | 20c1137 | 2022-08-24 15:06:13 +0100 | [diff] [blame] | 7185 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_pk_ec() returned NULL" ) ); |
Leonid Rozenboim | 19e5973 | 2022-08-08 16:52:38 -0700 | [diff] [blame] | 7186 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 7187 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7188 | |
Leonid Rozenboim | 19e5973 | 2022-08-08 16:52:38 -0700 | [diff] [blame] | 7189 | if( mbedtls_ssl_check_curve( ssl, ec->grp.id ) != 0 ) |
| 7190 | { |
| 7191 | ssl->session_negotiate->verify_result |= |
| 7192 | MBEDTLS_X509_BADCERT_BAD_KEY; |
| 7193 | |
| 7194 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) ); |
| 7195 | if( ret == 0 ) |
| 7196 | ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE; |
| 7197 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7198 | } |
| 7199 | } |
| 7200 | #endif /* MBEDTLS_ECP_C */ |
| 7201 | |
| 7202 | if( mbedtls_ssl_check_cert_usage( chain, |
| 7203 | ciphersuite_info, |
| 7204 | ! ssl->conf->endpoint, |
| 7205 | &ssl->session_negotiate->verify_result ) != 0 ) |
| 7206 | { |
| 7207 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) ); |
| 7208 | if( ret == 0 ) |
| 7209 | ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE; |
| 7210 | } |
| 7211 | |
| 7212 | /* mbedtls_x509_crt_verify_with_profile is supposed to report a |
| 7213 | * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED, |
| 7214 | * with details encoded in the verification flags. All other kinds |
| 7215 | * of error codes, including those from the user provided f_vrfy |
| 7216 | * functions, are treated as fatal and lead to a failure of |
| 7217 | * ssl_parse_certificate even if verification was optional. */ |
| 7218 | if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL && |
| 7219 | ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED || |
| 7220 | ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE ) ) |
| 7221 | { |
| 7222 | ret = 0; |
| 7223 | } |
| 7224 | |
| 7225 | if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED ) |
| 7226 | { |
| 7227 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) ); |
| 7228 | ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED; |
| 7229 | } |
| 7230 | |
| 7231 | if( ret != 0 ) |
| 7232 | { |
| 7233 | uint8_t alert; |
| 7234 | |
| 7235 | /* The certificate may have been rejected for several reasons. |
| 7236 | Pick one and send the corresponding alert. Which alert to send |
| 7237 | may be a subject of debate in some cases. */ |
| 7238 | if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER ) |
| 7239 | alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED; |
| 7240 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH ) |
| 7241 | alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT; |
| 7242 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE ) |
| 7243 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 7244 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE ) |
| 7245 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 7246 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE ) |
| 7247 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 7248 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK ) |
| 7249 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 7250 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY ) |
| 7251 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 7252 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED ) |
| 7253 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED; |
| 7254 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED ) |
| 7255 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED; |
| 7256 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED ) |
| 7257 | alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA; |
| 7258 | else |
| 7259 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN; |
| 7260 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7261 | alert ); |
| 7262 | } |
| 7263 | |
| 7264 | #if defined(MBEDTLS_DEBUG_C) |
| 7265 | if( ssl->session_negotiate->verify_result != 0 ) |
| 7266 | { |
| 7267 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %08x", |
| 7268 | (unsigned int) ssl->session_negotiate->verify_result ) ); |
| 7269 | } |
| 7270 | else |
| 7271 | { |
| 7272 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) ); |
| 7273 | } |
| 7274 | #endif /* MBEDTLS_DEBUG_C */ |
| 7275 | |
| 7276 | return( ret ); |
| 7277 | } |
| 7278 | |
| 7279 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 7280 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7281 | static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl, |
| 7282 | unsigned char *start, size_t len ) |
| 7283 | { |
| 7284 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 7285 | /* Remember digest of the peer's end-CRT. */ |
| 7286 | ssl->session_negotiate->peer_cert_digest = |
| 7287 | mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ); |
| 7288 | if( ssl->session_negotiate->peer_cert_digest == NULL ) |
| 7289 | { |
| 7290 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", |
| 7291 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ); |
| 7292 | mbedtls_ssl_send_alert_message( ssl, |
| 7293 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7294 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
| 7295 | |
| 7296 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 7297 | } |
| 7298 | |
| 7299 | ret = mbedtls_md( mbedtls_md_info_from_type( |
| 7300 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ), |
| 7301 | start, len, |
| 7302 | ssl->session_negotiate->peer_cert_digest ); |
| 7303 | |
| 7304 | ssl->session_negotiate->peer_cert_digest_type = |
| 7305 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE; |
| 7306 | ssl->session_negotiate->peer_cert_digest_len = |
| 7307 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN; |
| 7308 | |
| 7309 | return( ret ); |
| 7310 | } |
| 7311 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 7312 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7313 | static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl, |
| 7314 | unsigned char *start, size_t len ) |
| 7315 | { |
| 7316 | unsigned char *end = start + len; |
| 7317 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 7318 | |
| 7319 | /* Make a copy of the peer's raw public key. */ |
| 7320 | mbedtls_pk_init( &ssl->handshake->peer_pubkey ); |
| 7321 | ret = mbedtls_pk_parse_subpubkey( &start, end, |
| 7322 | &ssl->handshake->peer_pubkey ); |
| 7323 | if( ret != 0 ) |
| 7324 | { |
| 7325 | /* We should have parsed the public key before. */ |
| 7326 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 7327 | } |
| 7328 | |
| 7329 | return( 0 ); |
| 7330 | } |
| 7331 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 7332 | |
| 7333 | int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl ) |
| 7334 | { |
| 7335 | int ret = 0; |
| 7336 | int crt_expected; |
| 7337 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 7338 | const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET |
| 7339 | ? ssl->handshake->sni_authmode |
| 7340 | : ssl->conf->authmode; |
| 7341 | #else |
| 7342 | const int authmode = ssl->conf->authmode; |
| 7343 | #endif |
| 7344 | void *rs_ctx = NULL; |
| 7345 | mbedtls_x509_crt *chain = NULL; |
| 7346 | |
| 7347 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) ); |
| 7348 | |
| 7349 | crt_expected = ssl_parse_certificate_coordinate( ssl, authmode ); |
| 7350 | if( crt_expected == SSL_CERTIFICATE_SKIP ) |
| 7351 | { |
| 7352 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) ); |
| 7353 | goto exit; |
| 7354 | } |
| 7355 | |
| 7356 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
| 7357 | if( ssl->handshake->ecrs_enabled && |
| 7358 | ssl->handshake->ecrs_state == ssl_ecrs_crt_verify ) |
| 7359 | { |
| 7360 | chain = ssl->handshake->ecrs_peer_cert; |
| 7361 | ssl->handshake->ecrs_peer_cert = NULL; |
| 7362 | goto crt_verify; |
| 7363 | } |
| 7364 | #endif |
| 7365 | |
| 7366 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) |
| 7367 | { |
| 7368 | /* mbedtls_ssl_read_record may have sent an alert already. We |
| 7369 | let it decide whether to alert. */ |
| 7370 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
| 7371 | goto exit; |
| 7372 | } |
| 7373 | |
| 7374 | #if defined(MBEDTLS_SSL_SRV_C) |
| 7375 | if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 ) |
| 7376 | { |
| 7377 | ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING; |
| 7378 | |
| 7379 | if( authmode != MBEDTLS_SSL_VERIFY_OPTIONAL ) |
| 7380 | ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE; |
| 7381 | |
| 7382 | goto exit; |
| 7383 | } |
| 7384 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 7385 | |
| 7386 | /* Clear existing peer CRT structure in case we tried to |
| 7387 | * reuse a session but it failed, and allocate a new one. */ |
| 7388 | ssl_clear_peer_cert( ssl->session_negotiate ); |
| 7389 | |
| 7390 | chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ); |
| 7391 | if( chain == NULL ) |
| 7392 | { |
| 7393 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", |
| 7394 | sizeof( mbedtls_x509_crt ) ) ); |
| 7395 | mbedtls_ssl_send_alert_message( ssl, |
| 7396 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7397 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
| 7398 | |
| 7399 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 7400 | goto exit; |
| 7401 | } |
| 7402 | mbedtls_x509_crt_init( chain ); |
| 7403 | |
| 7404 | ret = ssl_parse_certificate_chain( ssl, chain ); |
| 7405 | if( ret != 0 ) |
| 7406 | goto exit; |
| 7407 | |
| 7408 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
| 7409 | if( ssl->handshake->ecrs_enabled) |
| 7410 | ssl->handshake->ecrs_state = ssl_ecrs_crt_verify; |
| 7411 | |
| 7412 | crt_verify: |
| 7413 | if( ssl->handshake->ecrs_enabled) |
| 7414 | rs_ctx = &ssl->handshake->ecrs_ctx; |
| 7415 | #endif |
| 7416 | |
| 7417 | ret = ssl_parse_certificate_verify( ssl, authmode, |
| 7418 | chain, rs_ctx ); |
| 7419 | if( ret != 0 ) |
| 7420 | goto exit; |
| 7421 | |
| 7422 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 7423 | { |
| 7424 | unsigned char *crt_start, *pk_start; |
| 7425 | size_t crt_len, pk_len; |
| 7426 | |
| 7427 | /* We parse the CRT chain without copying, so |
| 7428 | * these pointers point into the input buffer, |
| 7429 | * and are hence still valid after freeing the |
| 7430 | * CRT chain. */ |
| 7431 | |
| 7432 | crt_start = chain->raw.p; |
| 7433 | crt_len = chain->raw.len; |
| 7434 | |
| 7435 | pk_start = chain->pk_raw.p; |
| 7436 | pk_len = chain->pk_raw.len; |
| 7437 | |
| 7438 | /* Free the CRT structures before computing |
| 7439 | * digest and copying the peer's public key. */ |
| 7440 | mbedtls_x509_crt_free( chain ); |
| 7441 | mbedtls_free( chain ); |
| 7442 | chain = NULL; |
| 7443 | |
| 7444 | ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len ); |
| 7445 | if( ret != 0 ) |
| 7446 | goto exit; |
| 7447 | |
| 7448 | ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len ); |
| 7449 | if( ret != 0 ) |
| 7450 | goto exit; |
| 7451 | } |
| 7452 | #else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 7453 | /* Pass ownership to session structure. */ |
| 7454 | ssl->session_negotiate->peer_cert = chain; |
| 7455 | chain = NULL; |
| 7456 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 7457 | |
| 7458 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) ); |
| 7459 | |
| 7460 | exit: |
| 7461 | |
| 7462 | if( ret == 0 ) |
| 7463 | ssl->state++; |
| 7464 | |
| 7465 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
| 7466 | if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS ) |
| 7467 | { |
| 7468 | ssl->handshake->ecrs_peer_cert = chain; |
| 7469 | chain = NULL; |
| 7470 | } |
| 7471 | #endif |
| 7472 | |
| 7473 | if( chain != NULL ) |
| 7474 | { |
| 7475 | mbedtls_x509_crt_free( chain ); |
| 7476 | mbedtls_free( chain ); |
| 7477 | } |
| 7478 | |
| 7479 | return( ret ); |
| 7480 | } |
| 7481 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 7482 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 7483 | #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] | 7484 | static void ssl_calc_finished_tls_sha256( |
| 7485 | mbedtls_ssl_context *ssl, unsigned char *buf, int from ) |
| 7486 | { |
| 7487 | int len = 12; |
| 7488 | const char *sender; |
| 7489 | unsigned char padbuf[32]; |
| 7490 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7491 | size_t hash_size; |
| 7492 | psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT; |
| 7493 | psa_status_t status; |
| 7494 | #else |
| 7495 | mbedtls_sha256_context sha256; |
| 7496 | #endif |
| 7497 | |
| 7498 | mbedtls_ssl_session *session = ssl->session_negotiate; |
| 7499 | if( !session ) |
| 7500 | session = ssl->session; |
| 7501 | |
| 7502 | sender = ( from == MBEDTLS_SSL_IS_CLIENT ) |
| 7503 | ? "client finished" |
| 7504 | : "server finished"; |
| 7505 | |
| 7506 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7507 | sha256_psa = psa_hash_operation_init(); |
| 7508 | |
| 7509 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) ); |
| 7510 | |
| 7511 | status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa ); |
| 7512 | if( status != PSA_SUCCESS ) |
| 7513 | { |
| 7514 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); |
| 7515 | return; |
| 7516 | } |
| 7517 | |
| 7518 | status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size ); |
| 7519 | if( status != PSA_SUCCESS ) |
| 7520 | { |
| 7521 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); |
| 7522 | return; |
| 7523 | } |
| 7524 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 ); |
| 7525 | #else |
| 7526 | |
| 7527 | mbedtls_sha256_init( &sha256 ); |
| 7528 | |
| 7529 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) ); |
| 7530 | |
| 7531 | mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 ); |
| 7532 | |
| 7533 | /* |
| 7534 | * TLSv1.2: |
| 7535 | * hash = PRF( master, finished_label, |
| 7536 | * Hash( handshake ) )[0.11] |
| 7537 | */ |
| 7538 | |
| 7539 | #if !defined(MBEDTLS_SHA256_ALT) |
| 7540 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *) |
| 7541 | sha256.state, sizeof( sha256.state ) ); |
| 7542 | #endif |
| 7543 | |
| 7544 | mbedtls_sha256_finish( &sha256, padbuf ); |
| 7545 | mbedtls_sha256_free( &sha256 ); |
| 7546 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 7547 | |
| 7548 | ssl->handshake->tls_prf( session->master, 48, sender, |
| 7549 | padbuf, 32, buf, len ); |
| 7550 | |
| 7551 | MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len ); |
| 7552 | |
| 7553 | mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) ); |
| 7554 | |
| 7555 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) ); |
| 7556 | } |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 7557 | #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] | 7558 | |
Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 7559 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 7560 | #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] | 7561 | static void ssl_calc_finished_tls_sha384( |
| 7562 | mbedtls_ssl_context *ssl, unsigned char *buf, int from ) |
| 7563 | { |
| 7564 | int len = 12; |
| 7565 | const char *sender; |
| 7566 | unsigned char padbuf[48]; |
| 7567 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7568 | size_t hash_size; |
| 7569 | psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT; |
| 7570 | psa_status_t status; |
| 7571 | #else |
| 7572 | mbedtls_sha512_context sha512; |
| 7573 | #endif |
| 7574 | |
| 7575 | mbedtls_ssl_session *session = ssl->session_negotiate; |
| 7576 | if( !session ) |
| 7577 | session = ssl->session; |
| 7578 | |
| 7579 | sender = ( from == MBEDTLS_SSL_IS_CLIENT ) |
| 7580 | ? "client finished" |
| 7581 | : "server finished"; |
| 7582 | |
| 7583 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7584 | sha384_psa = psa_hash_operation_init(); |
| 7585 | |
| 7586 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) ); |
| 7587 | |
| 7588 | status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa ); |
| 7589 | if( status != PSA_SUCCESS ) |
| 7590 | { |
| 7591 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); |
| 7592 | return; |
| 7593 | } |
| 7594 | |
| 7595 | status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size ); |
| 7596 | if( status != PSA_SUCCESS ) |
| 7597 | { |
| 7598 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); |
| 7599 | return; |
| 7600 | } |
| 7601 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 ); |
| 7602 | #else |
| 7603 | mbedtls_sha512_init( &sha512 ); |
| 7604 | |
| 7605 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) ); |
| 7606 | |
Andrzej Kurek | a242e83 | 2022-08-11 10:03:14 -0400 | [diff] [blame] | 7607 | mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 ); |
Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 7608 | |
| 7609 | /* |
| 7610 | * TLSv1.2: |
| 7611 | * hash = PRF( master, finished_label, |
| 7612 | * Hash( handshake ) )[0.11] |
| 7613 | */ |
| 7614 | |
| 7615 | #if !defined(MBEDTLS_SHA512_ALT) |
| 7616 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *) |
| 7617 | sha512.state, sizeof( sha512.state ) ); |
| 7618 | #endif |
| 7619 | mbedtls_sha512_finish( &sha512, padbuf ); |
| 7620 | |
| 7621 | mbedtls_sha512_free( &sha512 ); |
| 7622 | #endif |
| 7623 | |
| 7624 | ssl->handshake->tls_prf( session->master, 48, sender, |
| 7625 | padbuf, 48, buf, len ); |
| 7626 | |
| 7627 | MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len ); |
| 7628 | |
| 7629 | mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) ); |
| 7630 | |
| 7631 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) ); |
| 7632 | } |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 7633 | #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] | 7634 | |
Jerry Yu | aef0015 | 2022-02-17 14:27:31 +0800 | [diff] [blame] | 7635 | void mbedtls_ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl ) |
| 7636 | { |
| 7637 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) ); |
| 7638 | |
| 7639 | /* |
| 7640 | * Free our handshake params |
| 7641 | */ |
| 7642 | mbedtls_ssl_handshake_free( ssl ); |
| 7643 | mbedtls_free( ssl->handshake ); |
| 7644 | ssl->handshake = NULL; |
| 7645 | |
| 7646 | /* |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 7647 | * Free the previous transform and switch in the current one |
Jerry Yu | aef0015 | 2022-02-17 14:27:31 +0800 | [diff] [blame] | 7648 | */ |
| 7649 | if( ssl->transform ) |
| 7650 | { |
| 7651 | mbedtls_ssl_transform_free( ssl->transform ); |
| 7652 | mbedtls_free( ssl->transform ); |
| 7653 | } |
| 7654 | ssl->transform = ssl->transform_negotiate; |
| 7655 | ssl->transform_negotiate = NULL; |
| 7656 | |
| 7657 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) ); |
| 7658 | } |
| 7659 | |
Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 7660 | void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl ) |
| 7661 | { |
| 7662 | int resume = ssl->handshake->resume; |
| 7663 | |
| 7664 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) ); |
| 7665 | |
| 7666 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 7667 | if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
| 7668 | { |
| 7669 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE; |
| 7670 | ssl->renego_records_seen = 0; |
| 7671 | } |
| 7672 | #endif |
| 7673 | |
| 7674 | /* |
| 7675 | * Free the previous session and switch in the current one |
| 7676 | */ |
| 7677 | if( ssl->session ) |
| 7678 | { |
| 7679 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 7680 | /* RFC 7366 3.1: keep the EtM state */ |
| 7681 | ssl->session_negotiate->encrypt_then_mac = |
| 7682 | ssl->session->encrypt_then_mac; |
| 7683 | #endif |
| 7684 | |
| 7685 | mbedtls_ssl_session_free( ssl->session ); |
| 7686 | mbedtls_free( ssl->session ); |
| 7687 | } |
| 7688 | ssl->session = ssl->session_negotiate; |
| 7689 | ssl->session_negotiate = NULL; |
| 7690 | |
| 7691 | /* |
| 7692 | * Add cache entry |
| 7693 | */ |
| 7694 | if( ssl->conf->f_set_cache != NULL && |
| 7695 | ssl->session->id_len != 0 && |
| 7696 | resume == 0 ) |
| 7697 | { |
| 7698 | if( ssl->conf->f_set_cache( ssl->conf->p_cache, |
| 7699 | ssl->session->id, |
| 7700 | ssl->session->id_len, |
| 7701 | ssl->session ) != 0 ) |
| 7702 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) ); |
| 7703 | } |
| 7704 | |
| 7705 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 7706 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 7707 | ssl->handshake->flight != NULL ) |
| 7708 | { |
| 7709 | /* Cancel handshake timer */ |
| 7710 | mbedtls_ssl_set_timer( ssl, 0 ); |
| 7711 | |
| 7712 | /* Keep last flight around in case we need to resend it: |
| 7713 | * we need the handshake and transform structures for that */ |
| 7714 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) ); |
| 7715 | } |
| 7716 | else |
| 7717 | #endif |
| 7718 | mbedtls_ssl_handshake_wrapup_free_hs_transform( ssl ); |
| 7719 | |
Jerry Yu | 5ed73ff | 2022-10-27 13:08:42 +0800 | [diff] [blame] | 7720 | ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER; |
Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 7721 | |
| 7722 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) ); |
| 7723 | } |
| 7724 | |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 7725 | int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl ) |
| 7726 | { |
| 7727 | int ret, hash_len; |
| 7728 | |
| 7729 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) ); |
| 7730 | |
| 7731 | mbedtls_ssl_update_out_pointers( ssl, ssl->transform_negotiate ); |
| 7732 | |
| 7733 | ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint ); |
| 7734 | |
| 7735 | /* |
| 7736 | * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites |
| 7737 | * may define some other value. Currently (early 2016), no defined |
| 7738 | * ciphersuite does this (and this is unlikely to change as activity has |
| 7739 | * moved to TLS 1.3 now) so we can keep the hardcoded 12 here. |
| 7740 | */ |
| 7741 | hash_len = 12; |
| 7742 | |
| 7743 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 7744 | ssl->verify_data_len = hash_len; |
| 7745 | memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len ); |
| 7746 | #endif |
| 7747 | |
| 7748 | ssl->out_msglen = 4 + hash_len; |
| 7749 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 7750 | ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED; |
| 7751 | |
| 7752 | /* |
| 7753 | * In case of session resuming, invert the client and server |
| 7754 | * ChangeCipherSpec messages order. |
| 7755 | */ |
| 7756 | if( ssl->handshake->resume != 0 ) |
| 7757 | { |
| 7758 | #if defined(MBEDTLS_SSL_CLI_C) |
| 7759 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 7760 | ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP; |
| 7761 | #endif |
| 7762 | #if defined(MBEDTLS_SSL_SRV_C) |
| 7763 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 7764 | ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC; |
| 7765 | #endif |
| 7766 | } |
| 7767 | else |
| 7768 | ssl->state++; |
| 7769 | |
| 7770 | /* |
| 7771 | * Switch to our negotiated transform and session parameters for outbound |
| 7772 | * data. |
| 7773 | */ |
| 7774 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) ); |
| 7775 | |
| 7776 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 7777 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 7778 | { |
| 7779 | unsigned char i; |
| 7780 | |
| 7781 | /* Remember current epoch settings for resending */ |
| 7782 | ssl->handshake->alt_transform_out = ssl->transform_out; |
| 7783 | memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, |
| 7784 | sizeof( ssl->handshake->alt_out_ctr ) ); |
| 7785 | |
| 7786 | /* Set sequence_number to zero */ |
| 7787 | memset( &ssl->cur_out_ctr[2], 0, sizeof( ssl->cur_out_ctr ) - 2 ); |
| 7788 | |
| 7789 | |
| 7790 | /* Increment epoch */ |
| 7791 | for( i = 2; i > 0; i-- ) |
| 7792 | if( ++ssl->cur_out_ctr[i - 1] != 0 ) |
| 7793 | break; |
| 7794 | |
| 7795 | /* The loop goes to its end iff the counter is wrapping */ |
| 7796 | if( i == 0 ) |
| 7797 | { |
| 7798 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) ); |
| 7799 | return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING ); |
| 7800 | } |
| 7801 | } |
| 7802 | else |
| 7803 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 7804 | memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) ); |
| 7805 | |
| 7806 | ssl->transform_out = ssl->transform_negotiate; |
| 7807 | ssl->session_out = ssl->session_negotiate; |
| 7808 | |
| 7809 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 7810 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 7811 | mbedtls_ssl_send_flight_completed( ssl ); |
| 7812 | #endif |
| 7813 | |
| 7814 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
| 7815 | { |
| 7816 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); |
| 7817 | return( ret ); |
| 7818 | } |
| 7819 | |
| 7820 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 7821 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 7822 | ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 ) |
| 7823 | { |
| 7824 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret ); |
| 7825 | return( ret ); |
| 7826 | } |
| 7827 | #endif |
| 7828 | |
| 7829 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) ); |
| 7830 | |
| 7831 | return( 0 ); |
| 7832 | } |
| 7833 | |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 7834 | #define SSL_MAX_HASH_LEN 12 |
| 7835 | |
| 7836 | int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl ) |
| 7837 | { |
| 7838 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 7839 | unsigned int hash_len = 12; |
| 7840 | unsigned char buf[SSL_MAX_HASH_LEN]; |
| 7841 | |
| 7842 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) ); |
| 7843 | |
| 7844 | ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 ); |
| 7845 | |
| 7846 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) |
| 7847 | { |
| 7848 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
| 7849 | goto exit; |
| 7850 | } |
| 7851 | |
| 7852 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) |
| 7853 | { |
| 7854 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) ); |
| 7855 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7856 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
| 7857 | ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; |
| 7858 | goto exit; |
| 7859 | } |
| 7860 | |
| 7861 | if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ) |
| 7862 | { |
| 7863 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7864 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
| 7865 | ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; |
| 7866 | goto exit; |
| 7867 | } |
| 7868 | |
| 7869 | if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len ) |
| 7870 | { |
| 7871 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) ); |
| 7872 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7873 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
| 7874 | ret = MBEDTLS_ERR_SSL_DECODE_ERROR; |
| 7875 | goto exit; |
| 7876 | } |
| 7877 | |
| 7878 | if( mbedtls_ct_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), |
| 7879 | buf, hash_len ) != 0 ) |
| 7880 | { |
| 7881 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) ); |
| 7882 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7883 | MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR ); |
| 7884 | ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE; |
| 7885 | goto exit; |
| 7886 | } |
| 7887 | |
| 7888 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 7889 | ssl->verify_data_len = hash_len; |
| 7890 | memcpy( ssl->peer_verify_data, buf, hash_len ); |
| 7891 | #endif |
| 7892 | |
| 7893 | if( ssl->handshake->resume != 0 ) |
| 7894 | { |
| 7895 | #if defined(MBEDTLS_SSL_CLI_C) |
| 7896 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 7897 | ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC; |
| 7898 | #endif |
| 7899 | #if defined(MBEDTLS_SSL_SRV_C) |
| 7900 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 7901 | ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP; |
| 7902 | #endif |
| 7903 | } |
| 7904 | else |
| 7905 | ssl->state++; |
| 7906 | |
| 7907 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 7908 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 7909 | mbedtls_ssl_recv_flight_completed( ssl ); |
| 7910 | #endif |
| 7911 | |
| 7912 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) ); |
| 7913 | |
| 7914 | exit: |
| 7915 | mbedtls_platform_zeroize( buf, hash_len ); |
| 7916 | return( ret ); |
| 7917 | } |
| 7918 | |
Jerry Yu | 392112c | 2022-02-17 14:34:10 +0800 | [diff] [blame] | 7919 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
| 7920 | /* |
| 7921 | * Helper to get TLS 1.2 PRF from ciphersuite |
| 7922 | * (Duplicates bits of logic from ssl_set_handshake_prfs().) |
| 7923 | */ |
| 7924 | static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id ) |
| 7925 | { |
Jerry Yu | 392112c | 2022-02-17 14:34:10 +0800 | [diff] [blame] | 7926 | const mbedtls_ssl_ciphersuite_t * const ciphersuite_info = |
Andrzej Kurek | 6832774 | 2022-10-03 06:18:18 -0400 | [diff] [blame] | 7927 | mbedtls_ssl_ciphersuite_from_id( ciphersuite_id ); |
| 7928 | #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] | 7929 | if( ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA384 ) |
Jerry Yu | 392112c | 2022-02-17 14:34:10 +0800 | [diff] [blame] | 7930 | return( tls_prf_sha384 ); |
Andrzej Kurek | 894edde | 2022-09-29 06:31:14 -0400 | [diff] [blame] | 7931 | else |
Jerry Yu | 392112c | 2022-02-17 14:34:10 +0800 | [diff] [blame] | 7932 | #endif |
Andrzej Kurek | 894edde | 2022-09-29 06:31:14 -0400 | [diff] [blame] | 7933 | #if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
| 7934 | { |
| 7935 | if( ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA256 ) |
| 7936 | return( tls_prf_sha256 ); |
| 7937 | } |
| 7938 | #endif |
| 7939 | #if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \ |
| 7940 | !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
| 7941 | (void) ciphersuite_info; |
| 7942 | #endif |
Andrzej Kurek | eabeb30 | 2022-10-17 07:52:51 -0400 | [diff] [blame] | 7943 | |
Andrzej Kurek | 894edde | 2022-09-29 06:31:14 -0400 | [diff] [blame] | 7944 | return( NULL ); |
Jerry Yu | 392112c | 2022-02-17 14:34:10 +0800 | [diff] [blame] | 7945 | } |
| 7946 | #endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */ |
Jerry Yu | e93ffcd | 2022-02-17 14:37:06 +0800 | [diff] [blame] | 7947 | |
| 7948 | static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf ) |
| 7949 | { |
| 7950 | ((void) tls_prf); |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 7951 | #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] | 7952 | if( tls_prf == tls_prf_sha384 ) |
| 7953 | { |
| 7954 | return( MBEDTLS_SSL_TLS_PRF_SHA384 ); |
| 7955 | } |
| 7956 | else |
| 7957 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 7958 | #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] | 7959 | if( tls_prf == tls_prf_sha256 ) |
| 7960 | { |
| 7961 | return( MBEDTLS_SSL_TLS_PRF_SHA256 ); |
| 7962 | } |
| 7963 | else |
| 7964 | #endif |
| 7965 | return( MBEDTLS_SSL_TLS_PRF_NONE ); |
| 7966 | } |
| 7967 | |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7968 | /* |
| 7969 | * Populate a transform structure with session keys and all the other |
| 7970 | * necessary information. |
| 7971 | * |
| 7972 | * Parameters: |
| 7973 | * - [in/out]: transform: structure to populate |
| 7974 | * [in] must be just initialised with mbedtls_ssl_transform_init() |
| 7975 | * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf() |
| 7976 | * - [in] ciphersuite |
| 7977 | * - [in] master |
| 7978 | * - [in] encrypt_then_mac |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7979 | * - [in] tls_prf: pointer to PRF to use for key derivation |
| 7980 | * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random |
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 7981 | * - [in] tls_version: TLS version |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7982 | * - [in] endpoint: client or server |
| 7983 | * - [in] ssl: used for: |
| 7984 | * - ssl->conf->{f,p}_export_keys |
| 7985 | * [in] optionally used for: |
| 7986 | * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg |
| 7987 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 7988 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7989 | static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform, |
| 7990 | int ciphersuite, |
| 7991 | const unsigned char master[48], |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 7992 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7993 | int encrypt_then_mac, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 7994 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7995 | ssl_tls_prf_t tls_prf, |
| 7996 | const unsigned char randbytes[64], |
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 7997 | mbedtls_ssl_protocol_version tls_version, |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7998 | unsigned endpoint, |
| 7999 | const mbedtls_ssl_context *ssl ) |
| 8000 | { |
| 8001 | int ret = 0; |
| 8002 | unsigned char keyblk[256]; |
| 8003 | unsigned char *key1; |
| 8004 | unsigned char *key2; |
| 8005 | unsigned char *mac_enc; |
| 8006 | unsigned char *mac_dec; |
| 8007 | size_t mac_key_len = 0; |
| 8008 | size_t iv_copy_len; |
| 8009 | size_t keylen; |
| 8010 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info; |
Neil Armstrong | 7fea33e | 2022-04-01 15:40:25 +0200 | [diff] [blame] | 8011 | mbedtls_ssl_mode_t ssl_mode; |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 8012 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8013 | const mbedtls_cipher_info_t *cipher_info; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8014 | const mbedtls_md_info_t *md_info; |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 8015 | #endif /* !MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8016 | |
| 8017 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 8018 | psa_key_type_t key_type; |
| 8019 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8020 | psa_algorithm_t alg; |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 8021 | psa_algorithm_t mac_alg = 0; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8022 | size_t key_bits; |
| 8023 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 8024 | #endif |
| 8025 | |
| 8026 | #if !defined(MBEDTLS_DEBUG_C) && \ |
| 8027 | !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 8028 | if( ssl->f_export_keys == NULL ) |
| 8029 | { |
| 8030 | ssl = NULL; /* make sure we don't use it except for these cases */ |
| 8031 | (void) ssl; |
| 8032 | } |
| 8033 | #endif |
| 8034 | |
| 8035 | /* |
| 8036 | * Some data just needs copying into the structure |
| 8037 | */ |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 8038 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8039 | transform->encrypt_then_mac = encrypt_then_mac; |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 8040 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 8041 | transform->tls_version = tls_version; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8042 | |
| 8043 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
| 8044 | memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) ); |
| 8045 | #endif |
| 8046 | |
| 8047 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 8048 | if( tls_version == MBEDTLS_SSL_VERSION_TLS1_3 ) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8049 | { |
| 8050 | /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform |
| 8051 | * generation separate. This should never happen. */ |
| 8052 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 8053 | } |
| 8054 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 8055 | |
| 8056 | /* |
| 8057 | * Get various info structures |
| 8058 | */ |
| 8059 | ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite ); |
| 8060 | if( ciphersuite_info == NULL ) |
| 8061 | { |
| 8062 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found", |
| 8063 | ciphersuite ) ); |
| 8064 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8065 | } |
| 8066 | |
Neil Armstrong | ab555e0 | 2022-04-04 11:07:59 +0200 | [diff] [blame] | 8067 | ssl_mode = mbedtls_ssl_get_mode_from_ciphersuite( |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 8068 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Neil Armstrong | 7fea33e | 2022-04-01 15:40:25 +0200 | [diff] [blame] | 8069 | encrypt_then_mac, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 8070 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Neil Armstrong | 7fea33e | 2022-04-01 15:40:25 +0200 | [diff] [blame] | 8071 | ciphersuite_info ); |
| 8072 | |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8073 | if( ssl_mode == MBEDTLS_SSL_MODE_AEAD ) |
| 8074 | transform->taglen = |
| 8075 | ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16; |
| 8076 | |
| 8077 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 8078 | if( ( status = mbedtls_ssl_cipher_to_psa( ciphersuite_info->cipher, |
| 8079 | transform->taglen, |
| 8080 | &alg, |
| 8081 | &key_type, |
| 8082 | &key_bits ) ) != PSA_SUCCESS ) |
| 8083 | { |
| 8084 | ret = psa_ssl_status_to_mbedtls( status ); |
| 8085 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_cipher_to_psa", ret ); |
| 8086 | goto end; |
| 8087 | } |
| 8088 | #else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8089 | cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher ); |
| 8090 | if( cipher_info == NULL ) |
| 8091 | { |
| 8092 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %u not found", |
| 8093 | ciphersuite_info->cipher ) ); |
| 8094 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8095 | } |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8096 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8097 | |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 8098 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Manuel Pégourié-Gonnard | abac037 | 2022-07-18 13:41:11 +0200 | [diff] [blame] | 8099 | mac_alg = mbedtls_hash_info_psa_from_md( ciphersuite_info->mac ); |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 8100 | if( mac_alg == 0 ) |
| 8101 | { |
Manuel Pégourié-Gonnard | abac037 | 2022-07-18 13:41:11 +0200 | [diff] [blame] | 8102 | 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] | 8103 | (unsigned) ciphersuite_info->mac ) ); |
| 8104 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8105 | } |
| 8106 | #else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8107 | md_info = mbedtls_md_info_from_type( ciphersuite_info->mac ); |
| 8108 | if( md_info == NULL ) |
| 8109 | { |
| 8110 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %u not found", |
| 8111 | (unsigned) ciphersuite_info->mac ) ); |
| 8112 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8113 | } |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 8114 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8115 | |
| 8116 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 8117 | /* Copy own and peer's CID if the use of the CID |
| 8118 | * extension has been negotiated. */ |
| 8119 | if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED ) |
| 8120 | { |
| 8121 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) ); |
| 8122 | |
| 8123 | transform->in_cid_len = ssl->own_cid_len; |
| 8124 | memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len ); |
| 8125 | MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid, |
| 8126 | transform->in_cid_len ); |
| 8127 | |
| 8128 | transform->out_cid_len = ssl->handshake->peer_cid_len; |
| 8129 | memcpy( transform->out_cid, ssl->handshake->peer_cid, |
| 8130 | ssl->handshake->peer_cid_len ); |
| 8131 | MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid, |
| 8132 | transform->out_cid_len ); |
| 8133 | } |
| 8134 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 8135 | |
| 8136 | /* |
| 8137 | * Compute key block using the PRF |
| 8138 | */ |
| 8139 | ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 ); |
| 8140 | if( ret != 0 ) |
| 8141 | { |
| 8142 | MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret ); |
| 8143 | return( ret ); |
| 8144 | } |
| 8145 | |
| 8146 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s", |
| 8147 | mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) ); |
| 8148 | MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 ); |
| 8149 | MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 ); |
| 8150 | MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 ); |
| 8151 | |
| 8152 | /* |
| 8153 | * Determine the appropriate key, IV and MAC length. |
| 8154 | */ |
| 8155 | |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8156 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 8157 | keylen = PSA_BITS_TO_BYTES(key_bits); |
| 8158 | #else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8159 | keylen = mbedtls_cipher_info_get_key_bitlen( cipher_info ) / 8; |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8160 | #endif |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8161 | |
| 8162 | #if defined(MBEDTLS_GCM_C) || \ |
| 8163 | defined(MBEDTLS_CCM_C) || \ |
| 8164 | defined(MBEDTLS_CHACHAPOLY_C) |
Neil Armstrong | 7fea33e | 2022-04-01 15:40:25 +0200 | [diff] [blame] | 8165 | if( ssl_mode == MBEDTLS_SSL_MODE_AEAD ) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8166 | { |
| 8167 | size_t explicit_ivlen; |
| 8168 | |
| 8169 | transform->maclen = 0; |
| 8170 | mac_key_len = 0; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8171 | |
| 8172 | /* All modes haves 96-bit IVs, but the length of the static parts vary |
| 8173 | * with mode and version: |
| 8174 | * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes |
| 8175 | * (to be concatenated with a dynamically chosen IV of 8 Bytes) |
| 8176 | * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's |
| 8177 | * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record |
| 8178 | * sequence number). |
| 8179 | */ |
| 8180 | transform->ivlen = 12; |
David Horstmann | 3b2276a | 2022-10-06 14:49:08 +0100 | [diff] [blame] | 8181 | |
| 8182 | int is_chachapoly = 0; |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8183 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
David Horstmann | 3b2276a | 2022-10-06 14:49:08 +0100 | [diff] [blame] | 8184 | is_chachapoly = ( key_type == PSA_KEY_TYPE_CHACHA20 ); |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8185 | #else |
David Horstmann | 3b2276a | 2022-10-06 14:49:08 +0100 | [diff] [blame] | 8186 | is_chachapoly = ( mbedtls_cipher_info_get_mode( cipher_info ) |
| 8187 | == MBEDTLS_MODE_CHACHAPOLY ); |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8188 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
David Horstmann | 3b2276a | 2022-10-06 14:49:08 +0100 | [diff] [blame] | 8189 | |
| 8190 | if( is_chachapoly ) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8191 | transform->fixed_ivlen = 12; |
| 8192 | else |
| 8193 | transform->fixed_ivlen = 4; |
| 8194 | |
| 8195 | /* Minimum length of encrypted record */ |
| 8196 | explicit_ivlen = transform->ivlen - transform->fixed_ivlen; |
| 8197 | transform->minlen = explicit_ivlen + transform->taglen; |
| 8198 | } |
| 8199 | else |
| 8200 | #endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */ |
| 8201 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
Neil Armstrong | 7fea33e | 2022-04-01 15:40:25 +0200 | [diff] [blame] | 8202 | if( ssl_mode == MBEDTLS_SSL_MODE_STREAM || |
| 8203 | ssl_mode == MBEDTLS_SSL_MODE_CBC || |
| 8204 | ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM ) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8205 | { |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 8206 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Neil Armstrong | d1be767 | 2022-04-04 11:21:41 +0200 | [diff] [blame] | 8207 | size_t block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ); |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8208 | #else |
| 8209 | size_t block_size = cipher_info->block_size; |
| 8210 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 8211 | |
| 8212 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 8213 | /* Get MAC length */ |
| 8214 | mac_key_len = PSA_HASH_LENGTH(mac_alg); |
| 8215 | #else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8216 | /* Initialize HMAC contexts */ |
| 8217 | if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 || |
| 8218 | ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 ) |
| 8219 | { |
| 8220 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret ); |
| 8221 | goto end; |
| 8222 | } |
| 8223 | |
| 8224 | /* Get MAC length */ |
| 8225 | mac_key_len = mbedtls_md_get_size( md_info ); |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 8226 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8227 | transform->maclen = mac_key_len; |
| 8228 | |
| 8229 | /* IV length */ |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8230 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Neil Armstrong | 2230e6c | 2022-04-27 10:36:14 +0200 | [diff] [blame] | 8231 | transform->ivlen = PSA_CIPHER_IV_LENGTH( key_type, alg ); |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8232 | #else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8233 | transform->ivlen = cipher_info->iv_size; |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8234 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8235 | |
| 8236 | /* Minimum length */ |
Neil Armstrong | 7fea33e | 2022-04-01 15:40:25 +0200 | [diff] [blame] | 8237 | if( ssl_mode == MBEDTLS_SSL_MODE_STREAM ) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8238 | transform->minlen = transform->maclen; |
| 8239 | else |
| 8240 | { |
| 8241 | /* |
| 8242 | * GenericBlockCipher: |
| 8243 | * 1. if EtM is in use: one block plus MAC |
| 8244 | * otherwise: * first multiple of blocklen greater than maclen |
| 8245 | * 2. IV |
| 8246 | */ |
| 8247 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Neil Armstrong | 7fea33e | 2022-04-01 15:40:25 +0200 | [diff] [blame] | 8248 | if( ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM ) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8249 | { |
| 8250 | transform->minlen = transform->maclen |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8251 | + block_size; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8252 | } |
| 8253 | else |
| 8254 | #endif |
| 8255 | { |
| 8256 | transform->minlen = transform->maclen |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8257 | + block_size |
| 8258 | - transform->maclen % block_size; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8259 | } |
| 8260 | |
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 8261 | if( tls_version == MBEDTLS_SSL_VERSION_TLS1_2 ) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8262 | { |
| 8263 | transform->minlen += transform->ivlen; |
| 8264 | } |
| 8265 | else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8266 | { |
| 8267 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 8268 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 8269 | goto end; |
| 8270 | } |
| 8271 | } |
| 8272 | } |
| 8273 | else |
| 8274 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
| 8275 | { |
| 8276 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 8277 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 8278 | } |
| 8279 | |
| 8280 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u", |
| 8281 | (unsigned) keylen, |
| 8282 | (unsigned) transform->minlen, |
| 8283 | (unsigned) transform->ivlen, |
| 8284 | (unsigned) transform->maclen ) ); |
| 8285 | |
| 8286 | /* |
| 8287 | * Finally setup the cipher contexts, IVs and MAC secrets. |
| 8288 | */ |
| 8289 | #if defined(MBEDTLS_SSL_CLI_C) |
| 8290 | if( endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 8291 | { |
| 8292 | key1 = keyblk + mac_key_len * 2; |
| 8293 | key2 = keyblk + mac_key_len * 2 + keylen; |
| 8294 | |
| 8295 | mac_enc = keyblk; |
| 8296 | mac_dec = keyblk + mac_key_len; |
| 8297 | |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8298 | iv_copy_len = ( transform->fixed_ivlen ) ? |
| 8299 | transform->fixed_ivlen : transform->ivlen; |
| 8300 | memcpy( transform->iv_enc, key2 + keylen, iv_copy_len ); |
| 8301 | memcpy( transform->iv_dec, key2 + keylen + iv_copy_len, |
| 8302 | iv_copy_len ); |
| 8303 | } |
| 8304 | else |
| 8305 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 8306 | #if defined(MBEDTLS_SSL_SRV_C) |
| 8307 | if( endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 8308 | { |
| 8309 | key1 = keyblk + mac_key_len * 2 + keylen; |
| 8310 | key2 = keyblk + mac_key_len * 2; |
| 8311 | |
| 8312 | mac_enc = keyblk + mac_key_len; |
| 8313 | mac_dec = keyblk; |
| 8314 | |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8315 | iv_copy_len = ( transform->fixed_ivlen ) ? |
| 8316 | transform->fixed_ivlen : transform->ivlen; |
| 8317 | memcpy( transform->iv_dec, key1 + keylen, iv_copy_len ); |
| 8318 | memcpy( transform->iv_enc, key1 + keylen + iv_copy_len, |
| 8319 | iv_copy_len ); |
| 8320 | } |
| 8321 | else |
| 8322 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 8323 | { |
| 8324 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 8325 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 8326 | goto end; |
| 8327 | } |
| 8328 | |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8329 | if( ssl != NULL && ssl->f_export_keys != NULL ) |
| 8330 | { |
| 8331 | ssl->f_export_keys( ssl->p_export_keys, |
| 8332 | MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET, |
| 8333 | master, 48, |
| 8334 | randbytes + 32, |
| 8335 | randbytes, |
| 8336 | tls_prf_get_type( tls_prf ) ); |
| 8337 | } |
| 8338 | |
| 8339 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8340 | transform->psa_alg = alg; |
| 8341 | |
| 8342 | if ( alg != MBEDTLS_SSL_NULL_CIPHER ) |
| 8343 | { |
| 8344 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 8345 | psa_set_key_algorithm( &attributes, alg ); |
| 8346 | psa_set_key_type( &attributes, key_type ); |
| 8347 | |
| 8348 | if( ( status = psa_import_key( &attributes, |
| 8349 | key1, |
| 8350 | PSA_BITS_TO_BYTES( key_bits ), |
| 8351 | &transform->psa_key_enc ) ) != PSA_SUCCESS ) |
| 8352 | { |
| 8353 | MBEDTLS_SSL_DEBUG_RET( 3, "psa_import_key", (int)status ); |
| 8354 | ret = psa_ssl_status_to_mbedtls( status ); |
| 8355 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret ); |
| 8356 | goto end; |
| 8357 | } |
| 8358 | |
| 8359 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 8360 | |
| 8361 | if( ( status = psa_import_key( &attributes, |
| 8362 | key2, |
| 8363 | PSA_BITS_TO_BYTES( key_bits ), |
| 8364 | &transform->psa_key_dec ) ) != PSA_SUCCESS ) |
| 8365 | { |
| 8366 | ret = psa_ssl_status_to_mbedtls( status ); |
| 8367 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret ); |
| 8368 | goto end; |
| 8369 | } |
| 8370 | } |
| 8371 | #else |
| 8372 | if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc, |
| 8373 | cipher_info ) ) != 0 ) |
| 8374 | { |
| 8375 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret ); |
| 8376 | goto end; |
| 8377 | } |
| 8378 | |
| 8379 | if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec, |
| 8380 | cipher_info ) ) != 0 ) |
| 8381 | { |
| 8382 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret ); |
| 8383 | goto end; |
| 8384 | } |
| 8385 | |
| 8386 | if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1, |
| 8387 | (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ), |
| 8388 | MBEDTLS_ENCRYPT ) ) != 0 ) |
| 8389 | { |
| 8390 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret ); |
| 8391 | goto end; |
| 8392 | } |
| 8393 | |
| 8394 | if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2, |
| 8395 | (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ), |
| 8396 | MBEDTLS_DECRYPT ) ) != 0 ) |
| 8397 | { |
| 8398 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret ); |
| 8399 | goto end; |
| 8400 | } |
| 8401 | |
| 8402 | #if defined(MBEDTLS_CIPHER_MODE_CBC) |
| 8403 | if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CBC ) |
| 8404 | { |
| 8405 | if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc, |
| 8406 | MBEDTLS_PADDING_NONE ) ) != 0 ) |
| 8407 | { |
| 8408 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret ); |
| 8409 | goto end; |
| 8410 | } |
| 8411 | |
| 8412 | if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec, |
| 8413 | MBEDTLS_PADDING_NONE ) ) != 0 ) |
| 8414 | { |
| 8415 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret ); |
| 8416 | goto end; |
| 8417 | } |
| 8418 | } |
| 8419 | #endif /* MBEDTLS_CIPHER_MODE_CBC */ |
| 8420 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 8421 | |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 8422 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
| 8423 | /* For HMAC-based ciphersuites, initialize the HMAC transforms. |
| 8424 | For AEAD-based ciphersuites, there is nothing to do here. */ |
| 8425 | if( mac_key_len != 0 ) |
| 8426 | { |
| 8427 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 8428 | transform->psa_mac_alg = PSA_ALG_HMAC( mac_alg ); |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 8429 | |
| 8430 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE ); |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 8431 | psa_set_key_algorithm( &attributes, PSA_ALG_HMAC( mac_alg ) ); |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 8432 | psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC ); |
| 8433 | |
| 8434 | if( ( status = psa_import_key( &attributes, |
| 8435 | mac_enc, mac_key_len, |
| 8436 | &transform->psa_mac_enc ) ) != PSA_SUCCESS ) |
| 8437 | { |
| 8438 | ret = psa_ssl_status_to_mbedtls( status ); |
| 8439 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret ); |
| 8440 | goto end; |
| 8441 | } |
| 8442 | |
Ronald Cron | fb39f15 | 2022-03-25 14:36:28 +0100 | [diff] [blame] | 8443 | if( ( transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER ) || |
Andrzej Kurek | 8c95ac4 | 2022-08-17 16:17:00 -0400 | [diff] [blame] | 8444 | ( ( transform->psa_alg == PSA_ALG_CBC_NO_PADDING ) |
| 8445 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
| 8446 | && ( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED ) |
| 8447 | #endif |
| 8448 | ) ) |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 8449 | /* mbedtls_ct_hmac() requires the key to be exportable */ |
| 8450 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT | |
| 8451 | PSA_KEY_USAGE_VERIFY_HASH ); |
| 8452 | else |
| 8453 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH ); |
| 8454 | |
| 8455 | if( ( status = psa_import_key( &attributes, |
| 8456 | mac_dec, mac_key_len, |
| 8457 | &transform->psa_mac_dec ) ) != PSA_SUCCESS ) |
| 8458 | { |
| 8459 | ret = psa_ssl_status_to_mbedtls( status ); |
| 8460 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret ); |
| 8461 | goto end; |
| 8462 | } |
| 8463 | #else |
| 8464 | ret = mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len ); |
| 8465 | if( ret != 0 ) |
| 8466 | goto end; |
| 8467 | ret = mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len ); |
| 8468 | if( ret != 0 ) |
| 8469 | goto end; |
| 8470 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 8471 | } |
| 8472 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
| 8473 | |
| 8474 | ((void) mac_dec); |
| 8475 | ((void) mac_enc); |
| 8476 | |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8477 | end: |
| 8478 | mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) ); |
| 8479 | return( ret ); |
| 8480 | } |
| 8481 | |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 8482 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) && \ |
| 8483 | defined(MBEDTLS_USE_PSA_CRYPTO) |
Valerio Setti | 6b3dab0 | 2022-11-17 17:14:54 +0100 | [diff] [blame] | 8484 | int mbedtls_psa_ecjpake_read_round( |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 8485 | psa_pake_operation_t *pake_ctx, |
| 8486 | const unsigned char *buf, |
Valerio Setti | 6b3dab0 | 2022-11-17 17:14:54 +0100 | [diff] [blame] | 8487 | size_t len, mbedtls_ecjpake_rounds_t round ) |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 8488 | { |
| 8489 | psa_status_t status; |
| 8490 | size_t input_offset = 0; |
Valerio Setti | 819de86 | 2022-11-17 18:05:19 +0100 | [diff] [blame] | 8491 | /* |
Valerio Setti | 6b3dab0 | 2022-11-17 17:14:54 +0100 | [diff] [blame] | 8492 | * At round one repeat the KEY_SHARE, ZK_PUBLIC & ZF_PROOF twice |
| 8493 | * At round two perform a single cycle |
| 8494 | */ |
| 8495 | unsigned int remaining_steps = ( round == MBEDTLS_ECJPAKE_ROUND_ONE) ? 2 : 1; |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 8496 | |
Valerio Setti | 6b3dab0 | 2022-11-17 17:14:54 +0100 | [diff] [blame] | 8497 | for( ; remaining_steps > 0; remaining_steps-- ) |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 8498 | { |
| 8499 | for( psa_pake_step_t step = PSA_PAKE_STEP_KEY_SHARE; |
| 8500 | step <= PSA_PAKE_STEP_ZK_PROOF; |
| 8501 | ++step ) |
| 8502 | { |
| 8503 | /* Length is stored at the first byte */ |
| 8504 | size_t length = buf[input_offset]; |
| 8505 | input_offset += 1; |
| 8506 | |
| 8507 | if( input_offset + length > len ) |
| 8508 | { |
| 8509 | return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE; |
| 8510 | } |
| 8511 | |
| 8512 | status = psa_pake_input( pake_ctx, step, |
| 8513 | buf + input_offset, length ); |
| 8514 | if( status != PSA_SUCCESS) |
| 8515 | { |
| 8516 | return psa_ssl_status_to_mbedtls( status ); |
| 8517 | } |
| 8518 | |
| 8519 | input_offset += length; |
| 8520 | } |
| 8521 | } |
| 8522 | |
Valerio Setti | 819de86 | 2022-11-17 18:05:19 +0100 | [diff] [blame] | 8523 | if( input_offset != len ) |
Valerio Setti | 61ea17d | 2022-11-18 12:11:00 +0100 | [diff] [blame] | 8524 | return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE; |
Valerio Setti | 30ebe11 | 2022-11-17 16:23:34 +0100 | [diff] [blame] | 8525 | |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 8526 | return( 0 ); |
| 8527 | } |
| 8528 | |
Valerio Setti | 6b3dab0 | 2022-11-17 17:14:54 +0100 | [diff] [blame] | 8529 | int mbedtls_psa_ecjpake_write_round( |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 8530 | psa_pake_operation_t *pake_ctx, |
| 8531 | unsigned char *buf, |
Valerio Setti | 6b3dab0 | 2022-11-17 17:14:54 +0100 | [diff] [blame] | 8532 | size_t len, size_t *olen, |
| 8533 | mbedtls_ecjpake_rounds_t round ) |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 8534 | { |
| 8535 | psa_status_t status; |
| 8536 | size_t output_offset = 0; |
| 8537 | size_t output_len; |
Valerio Setti | 819de86 | 2022-11-17 18:05:19 +0100 | [diff] [blame] | 8538 | /* |
Valerio Setti | 6b3dab0 | 2022-11-17 17:14:54 +0100 | [diff] [blame] | 8539 | * At round one repeat the KEY_SHARE, ZK_PUBLIC & ZF_PROOF twice |
| 8540 | * At round two perform a single cycle |
| 8541 | */ |
| 8542 | unsigned int remaining_steps = ( round == MBEDTLS_ECJPAKE_ROUND_ONE) ? 2 : 1; |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 8543 | |
Valerio Setti | 6b3dab0 | 2022-11-17 17:14:54 +0100 | [diff] [blame] | 8544 | for( ; remaining_steps > 0; remaining_steps-- ) |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 8545 | { |
Valerio Setti | 6b3dab0 | 2022-11-17 17:14:54 +0100 | [diff] [blame] | 8546 | for( psa_pake_step_t step = PSA_PAKE_STEP_KEY_SHARE; |
| 8547 | step <= PSA_PAKE_STEP_ZK_PROOF; |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 8548 | ++step ) |
| 8549 | { |
Valerio Setti | 79f6b6b | 2022-11-21 14:17:03 +0100 | [diff] [blame] | 8550 | /* |
Valerio Setti | d4a9b1a | 2022-11-22 11:11:10 +0100 | [diff] [blame] | 8551 | * For each step, prepend 1 byte with the length of the data as |
| 8552 | * given by psa_pake_output(). |
Valerio Setti | 79f6b6b | 2022-11-21 14:17:03 +0100 | [diff] [blame] | 8553 | */ |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 8554 | status = psa_pake_output( pake_ctx, step, |
Valerio Setti | 79f6b6b | 2022-11-21 14:17:03 +0100 | [diff] [blame] | 8555 | buf + output_offset + 1, |
| 8556 | len - output_offset - 1, |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 8557 | &output_len ); |
| 8558 | if( status != PSA_SUCCESS ) |
| 8559 | { |
| 8560 | return( psa_ssl_status_to_mbedtls( status ) ); |
| 8561 | } |
| 8562 | |
Valerio Setti | 99d88c1 | 2022-11-22 16:03:43 +0100 | [diff] [blame] | 8563 | *(buf + output_offset) = (uint8_t) output_len; |
Valerio Setti | 79f6b6b | 2022-11-21 14:17:03 +0100 | [diff] [blame] | 8564 | |
| 8565 | output_offset += output_len + 1; |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 8566 | } |
| 8567 | } |
| 8568 | |
| 8569 | *olen = output_offset; |
| 8570 | |
| 8571 | return( 0 ); |
| 8572 | } |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 8573 | #endif //MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED && MBEDTLS_USE_PSA_CRYPTO |
| 8574 | |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 8575 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 8576 | int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl, |
| 8577 | unsigned char *hash, size_t *hashlen, |
| 8578 | unsigned char *data, size_t data_len, |
| 8579 | mbedtls_md_type_t md_alg ) |
| 8580 | { |
| 8581 | psa_status_t status; |
| 8582 | psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT; |
Manuel Pégourié-Gonnard | abac037 | 2022-07-18 13:41:11 +0200 | [diff] [blame] | 8583 | 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] | 8584 | |
| 8585 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) ); |
| 8586 | |
| 8587 | if( ( status = psa_hash_setup( &hash_operation, |
| 8588 | hash_alg ) ) != PSA_SUCCESS ) |
| 8589 | { |
| 8590 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status ); |
| 8591 | goto exit; |
| 8592 | } |
| 8593 | |
| 8594 | if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes, |
| 8595 | 64 ) ) != PSA_SUCCESS ) |
| 8596 | { |
| 8597 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status ); |
| 8598 | goto exit; |
| 8599 | } |
| 8600 | |
| 8601 | if( ( status = psa_hash_update( &hash_operation, |
| 8602 | data, data_len ) ) != PSA_SUCCESS ) |
| 8603 | { |
| 8604 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status ); |
| 8605 | goto exit; |
| 8606 | } |
| 8607 | |
| 8608 | if( ( status = psa_hash_finish( &hash_operation, hash, PSA_HASH_MAX_SIZE, |
| 8609 | hashlen ) ) != PSA_SUCCESS ) |
| 8610 | { |
| 8611 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status ); |
| 8612 | goto exit; |
| 8613 | } |
| 8614 | |
| 8615 | exit: |
| 8616 | if( status != PSA_SUCCESS ) |
| 8617 | { |
| 8618 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 8619 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
| 8620 | switch( status ) |
| 8621 | { |
| 8622 | case PSA_ERROR_NOT_SUPPORTED: |
| 8623 | return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE ); |
| 8624 | case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */ |
| 8625 | case PSA_ERROR_BUFFER_TOO_SMALL: |
| 8626 | return( MBEDTLS_ERR_MD_BAD_INPUT_DATA ); |
| 8627 | case PSA_ERROR_INSUFFICIENT_MEMORY: |
| 8628 | return( MBEDTLS_ERR_MD_ALLOC_FAILED ); |
| 8629 | default: |
| 8630 | return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED ); |
| 8631 | } |
| 8632 | } |
| 8633 | return( 0 ); |
| 8634 | } |
| 8635 | |
| 8636 | #else |
| 8637 | |
| 8638 | int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl, |
| 8639 | unsigned char *hash, size_t *hashlen, |
| 8640 | unsigned char *data, size_t data_len, |
| 8641 | mbedtls_md_type_t md_alg ) |
| 8642 | { |
| 8643 | int ret = 0; |
| 8644 | mbedtls_md_context_t ctx; |
| 8645 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg ); |
| 8646 | *hashlen = mbedtls_md_get_size( md_info ); |
| 8647 | |
| 8648 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) ); |
| 8649 | |
| 8650 | mbedtls_md_init( &ctx ); |
| 8651 | |
| 8652 | /* |
| 8653 | * digitally-signed struct { |
| 8654 | * opaque client_random[32]; |
| 8655 | * opaque server_random[32]; |
| 8656 | * ServerDHParams params; |
| 8657 | * }; |
| 8658 | */ |
| 8659 | if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 ) |
| 8660 | { |
| 8661 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret ); |
| 8662 | goto exit; |
| 8663 | } |
| 8664 | if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 ) |
| 8665 | { |
| 8666 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret ); |
| 8667 | goto exit; |
| 8668 | } |
| 8669 | if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 ) |
| 8670 | { |
| 8671 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret ); |
| 8672 | goto exit; |
| 8673 | } |
| 8674 | if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 ) |
| 8675 | { |
| 8676 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret ); |
| 8677 | goto exit; |
| 8678 | } |
| 8679 | if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 ) |
| 8680 | { |
| 8681 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret ); |
| 8682 | goto exit; |
| 8683 | } |
| 8684 | |
| 8685 | exit: |
| 8686 | mbedtls_md_free( &ctx ); |
| 8687 | |
| 8688 | if( ret != 0 ) |
| 8689 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 8690 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
| 8691 | |
| 8692 | return( ret ); |
| 8693 | } |
| 8694 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 8695 | |
Jerry Yu | d9d91da | 2022-02-17 14:57:06 +0800 | [diff] [blame] | 8696 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
| 8697 | |
Gabor Mezei | a3d016c | 2022-05-10 12:44:09 +0200 | [diff] [blame] | 8698 | /* Find the preferred hash for a given signature algorithm. */ |
| 8699 | unsigned int mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg( |
| 8700 | mbedtls_ssl_context *ssl, |
| 8701 | unsigned int sig_alg ) |
Jerry Yu | d9d91da | 2022-02-17 14:57:06 +0800 | [diff] [blame] | 8702 | { |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 8703 | unsigned int i; |
Gabor Mezei | a3d016c | 2022-05-10 12:44:09 +0200 | [diff] [blame] | 8704 | uint16_t *received_sig_algs = ssl->handshake->received_sig_algs; |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 8705 | |
| 8706 | if( sig_alg == MBEDTLS_SSL_SIG_ANON ) |
Gabor Mezei | a3d016c | 2022-05-10 12:44:09 +0200 | [diff] [blame] | 8707 | return( MBEDTLS_SSL_HASH_NONE ); |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 8708 | |
Gabor Mezei | a3d016c | 2022-05-10 12:44:09 +0200 | [diff] [blame] | 8709 | for( i = 0; received_sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ ) |
Jerry Yu | d9d91da | 2022-02-17 14:57:06 +0800 | [diff] [blame] | 8710 | { |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 8711 | unsigned int hash_alg_received = |
| 8712 | MBEDTLS_SSL_TLS12_HASH_ALG_FROM_SIG_AND_HASH_ALG( |
| 8713 | received_sig_algs[i] ); |
| 8714 | unsigned int sig_alg_received = |
| 8715 | MBEDTLS_SSL_TLS12_SIG_ALG_FROM_SIG_AND_HASH_ALG( |
| 8716 | received_sig_algs[i] ); |
| 8717 | |
| 8718 | if( sig_alg == sig_alg_received ) |
| 8719 | { |
| 8720 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 8721 | if( ssl->handshake->key_cert && ssl->handshake->key_cert->key ) |
| 8722 | { |
Neil Armstrong | 96eceb8 | 2022-06-30 18:05:05 +0200 | [diff] [blame] | 8723 | psa_algorithm_t psa_hash_alg = |
Manuel Pégourié-Gonnard | abac037 | 2022-07-18 13:41:11 +0200 | [diff] [blame] | 8724 | mbedtls_hash_info_psa_from_md( hash_alg_received ); |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 8725 | |
Neil Armstrong | 96eceb8 | 2022-06-30 18:05:05 +0200 | [diff] [blame] | 8726 | if( sig_alg_received == MBEDTLS_SSL_SIG_ECDSA && |
| 8727 | ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key, |
| 8728 | PSA_ALG_ECDSA( psa_hash_alg ), |
| 8729 | PSA_KEY_USAGE_SIGN_HASH ) ) |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 8730 | continue; |
| 8731 | |
Neil Armstrong | 96eceb8 | 2022-06-30 18:05:05 +0200 | [diff] [blame] | 8732 | if( sig_alg_received == MBEDTLS_SSL_SIG_RSA && |
Neil Armstrong | 971f30d | 2022-07-01 16:23:50 +0200 | [diff] [blame] | 8733 | ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key, |
| 8734 | PSA_ALG_RSA_PKCS1V15_SIGN( |
| 8735 | psa_hash_alg ), |
| 8736 | PSA_KEY_USAGE_SIGN_HASH ) ) |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 8737 | continue; |
| 8738 | } |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 8739 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Neil Armstrong | 96eceb8 | 2022-06-30 18:05:05 +0200 | [diff] [blame] | 8740 | |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 8741 | return( hash_alg_received ); |
| 8742 | } |
Jerry Yu | d9d91da | 2022-02-17 14:57:06 +0800 | [diff] [blame] | 8743 | } |
Jerry Yu | d9d91da | 2022-02-17 14:57:06 +0800 | [diff] [blame] | 8744 | |
Gabor Mezei | a3d016c | 2022-05-10 12:44:09 +0200 | [diff] [blame] | 8745 | return( MBEDTLS_SSL_HASH_NONE ); |
Jerry Yu | d9d91da | 2022-02-17 14:57:06 +0800 | [diff] [blame] | 8746 | } |
| 8747 | |
| 8748 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 8749 | |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8750 | /* Serialization of TLS 1.2 sessions: |
| 8751 | * |
| 8752 | * struct { |
| 8753 | * uint64 start_time; |
| 8754 | * uint8 ciphersuite[2]; // defined by the standard |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8755 | * uint8 session_id_len; // at most 32 |
| 8756 | * opaque session_id[32]; |
| 8757 | * opaque master[48]; // fixed length in the standard |
| 8758 | * uint32 verify_result; |
| 8759 | * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert |
| 8760 | * opaque ticket<0..2^24-1>; // length 0 means no ticket |
| 8761 | * uint32 ticket_lifetime; |
| 8762 | * uint8 mfl_code; // up to 255 according to standard |
| 8763 | * uint8 encrypt_then_mac; // 0 or 1 |
| 8764 | * } serialized_session_tls12; |
| 8765 | * |
| 8766 | */ |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 8767 | static size_t ssl_tls12_session_save( const mbedtls_ssl_session *session, |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8768 | unsigned char *buf, |
| 8769 | size_t buf_len ) |
| 8770 | { |
| 8771 | unsigned char *p = buf; |
| 8772 | size_t used = 0; |
| 8773 | |
| 8774 | #if defined(MBEDTLS_HAVE_TIME) |
| 8775 | uint64_t start; |
| 8776 | #endif |
| 8777 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 8778 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 8779 | size_t cert_len; |
| 8780 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 8781 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 8782 | |
| 8783 | /* |
| 8784 | * Time |
| 8785 | */ |
| 8786 | #if defined(MBEDTLS_HAVE_TIME) |
| 8787 | used += 8; |
| 8788 | |
| 8789 | if( used <= buf_len ) |
| 8790 | { |
| 8791 | start = (uint64_t) session->start; |
| 8792 | |
| 8793 | MBEDTLS_PUT_UINT64_BE( start, p, 0 ); |
| 8794 | p += 8; |
| 8795 | } |
| 8796 | #endif /* MBEDTLS_HAVE_TIME */ |
| 8797 | |
| 8798 | /* |
| 8799 | * Basic mandatory fields |
| 8800 | */ |
| 8801 | used += 2 /* ciphersuite */ |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8802 | + 1 /* id_len */ |
| 8803 | + sizeof( session->id ) |
| 8804 | + sizeof( session->master ) |
| 8805 | + 4; /* verify_result */ |
| 8806 | |
| 8807 | if( used <= buf_len ) |
| 8808 | { |
| 8809 | MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 0 ); |
| 8810 | p += 2; |
| 8811 | |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8812 | *p++ = MBEDTLS_BYTE_0( session->id_len ); |
| 8813 | memcpy( p, session->id, 32 ); |
| 8814 | p += 32; |
| 8815 | |
| 8816 | memcpy( p, session->master, 48 ); |
| 8817 | p += 48; |
| 8818 | |
| 8819 | MBEDTLS_PUT_UINT32_BE( session->verify_result, p, 0 ); |
| 8820 | p += 4; |
| 8821 | } |
| 8822 | |
| 8823 | /* |
| 8824 | * Peer's end-entity certificate |
| 8825 | */ |
| 8826 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 8827 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 8828 | if( session->peer_cert == NULL ) |
| 8829 | cert_len = 0; |
| 8830 | else |
| 8831 | cert_len = session->peer_cert->raw.len; |
| 8832 | |
| 8833 | used += 3 + cert_len; |
| 8834 | |
| 8835 | if( used <= buf_len ) |
| 8836 | { |
| 8837 | *p++ = MBEDTLS_BYTE_2( cert_len ); |
| 8838 | *p++ = MBEDTLS_BYTE_1( cert_len ); |
| 8839 | *p++ = MBEDTLS_BYTE_0( cert_len ); |
| 8840 | |
| 8841 | if( session->peer_cert != NULL ) |
| 8842 | { |
| 8843 | memcpy( p, session->peer_cert->raw.p, cert_len ); |
| 8844 | p += cert_len; |
| 8845 | } |
| 8846 | } |
| 8847 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 8848 | if( session->peer_cert_digest != NULL ) |
| 8849 | { |
| 8850 | used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len; |
| 8851 | if( used <= buf_len ) |
| 8852 | { |
| 8853 | *p++ = (unsigned char) session->peer_cert_digest_type; |
| 8854 | *p++ = (unsigned char) session->peer_cert_digest_len; |
| 8855 | memcpy( p, session->peer_cert_digest, |
| 8856 | session->peer_cert_digest_len ); |
| 8857 | p += session->peer_cert_digest_len; |
| 8858 | } |
| 8859 | } |
| 8860 | else |
| 8861 | { |
| 8862 | used += 2; |
| 8863 | if( used <= buf_len ) |
| 8864 | { |
| 8865 | *p++ = (unsigned char) MBEDTLS_MD_NONE; |
| 8866 | *p++ = 0; |
| 8867 | } |
| 8868 | } |
| 8869 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 8870 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 8871 | |
| 8872 | /* |
| 8873 | * Session ticket if any, plus associated data |
| 8874 | */ |
| 8875 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
| 8876 | used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */ |
| 8877 | |
| 8878 | if( used <= buf_len ) |
| 8879 | { |
| 8880 | *p++ = MBEDTLS_BYTE_2( session->ticket_len ); |
| 8881 | *p++ = MBEDTLS_BYTE_1( session->ticket_len ); |
| 8882 | *p++ = MBEDTLS_BYTE_0( session->ticket_len ); |
| 8883 | |
| 8884 | if( session->ticket != NULL ) |
| 8885 | { |
| 8886 | memcpy( p, session->ticket, session->ticket_len ); |
| 8887 | p += session->ticket_len; |
| 8888 | } |
| 8889 | |
| 8890 | MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 ); |
| 8891 | p += 4; |
| 8892 | } |
| 8893 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
| 8894 | |
| 8895 | /* |
| 8896 | * Misc extension-related info |
| 8897 | */ |
| 8898 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 8899 | used += 1; |
| 8900 | |
| 8901 | if( used <= buf_len ) |
| 8902 | *p++ = session->mfl_code; |
| 8903 | #endif |
| 8904 | |
| 8905 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 8906 | used += 1; |
| 8907 | |
| 8908 | if( used <= buf_len ) |
| 8909 | *p++ = MBEDTLS_BYTE_0( session->encrypt_then_mac ); |
| 8910 | #endif |
| 8911 | |
| 8912 | return( used ); |
| 8913 | } |
| 8914 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 8915 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 8916 | static int ssl_tls12_session_load( mbedtls_ssl_session *session, |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8917 | const unsigned char *buf, |
| 8918 | size_t len ) |
| 8919 | { |
| 8920 | #if defined(MBEDTLS_HAVE_TIME) |
| 8921 | uint64_t start; |
| 8922 | #endif |
| 8923 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 8924 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 8925 | size_t cert_len; |
| 8926 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 8927 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 8928 | |
| 8929 | const unsigned char *p = buf; |
| 8930 | const unsigned char * const end = buf + len; |
| 8931 | |
| 8932 | /* |
| 8933 | * Time |
| 8934 | */ |
| 8935 | #if defined(MBEDTLS_HAVE_TIME) |
| 8936 | if( 8 > (size_t)( end - p ) ) |
| 8937 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8938 | |
| 8939 | start = ( (uint64_t) p[0] << 56 ) | |
| 8940 | ( (uint64_t) p[1] << 48 ) | |
| 8941 | ( (uint64_t) p[2] << 40 ) | |
| 8942 | ( (uint64_t) p[3] << 32 ) | |
| 8943 | ( (uint64_t) p[4] << 24 ) | |
| 8944 | ( (uint64_t) p[5] << 16 ) | |
| 8945 | ( (uint64_t) p[6] << 8 ) | |
| 8946 | ( (uint64_t) p[7] ); |
| 8947 | p += 8; |
| 8948 | |
| 8949 | session->start = (time_t) start; |
| 8950 | #endif /* MBEDTLS_HAVE_TIME */ |
| 8951 | |
| 8952 | /* |
| 8953 | * Basic mandatory fields |
| 8954 | */ |
Thomas Daubney | 20f89a9 | 2022-06-20 15:12:19 +0100 | [diff] [blame] | 8955 | if( 2 + 1 + 32 + 48 + 4 > (size_t)( end - p ) ) |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8956 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8957 | |
| 8958 | session->ciphersuite = ( p[0] << 8 ) | p[1]; |
| 8959 | p += 2; |
| 8960 | |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8961 | session->id_len = *p++; |
| 8962 | memcpy( session->id, p, 32 ); |
| 8963 | p += 32; |
| 8964 | |
| 8965 | memcpy( session->master, p, 48 ); |
| 8966 | p += 48; |
| 8967 | |
| 8968 | session->verify_result = ( (uint32_t) p[0] << 24 ) | |
| 8969 | ( (uint32_t) p[1] << 16 ) | |
| 8970 | ( (uint32_t) p[2] << 8 ) | |
| 8971 | ( (uint32_t) p[3] ); |
| 8972 | p += 4; |
| 8973 | |
| 8974 | /* Immediately clear invalid pointer values that have been read, in case |
| 8975 | * we exit early before we replaced them with valid ones. */ |
| 8976 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 8977 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 8978 | session->peer_cert = NULL; |
| 8979 | #else |
| 8980 | session->peer_cert_digest = NULL; |
| 8981 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 8982 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 8983 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
| 8984 | session->ticket = NULL; |
| 8985 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
| 8986 | |
| 8987 | /* |
| 8988 | * Peer certificate |
| 8989 | */ |
| 8990 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 8991 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 8992 | /* Deserialize CRT from the end of the ticket. */ |
| 8993 | if( 3 > (size_t)( end - p ) ) |
| 8994 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8995 | |
| 8996 | cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2]; |
| 8997 | p += 3; |
| 8998 | |
| 8999 | if( cert_len != 0 ) |
| 9000 | { |
| 9001 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 9002 | |
| 9003 | if( cert_len > (size_t)( end - p ) ) |
| 9004 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 9005 | |
| 9006 | session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ); |
| 9007 | |
| 9008 | if( session->peer_cert == NULL ) |
| 9009 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 9010 | |
| 9011 | mbedtls_x509_crt_init( session->peer_cert ); |
| 9012 | |
| 9013 | if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert, |
| 9014 | p, cert_len ) ) != 0 ) |
| 9015 | { |
| 9016 | mbedtls_x509_crt_free( session->peer_cert ); |
| 9017 | mbedtls_free( session->peer_cert ); |
| 9018 | session->peer_cert = NULL; |
| 9019 | return( ret ); |
| 9020 | } |
| 9021 | |
| 9022 | p += cert_len; |
| 9023 | } |
| 9024 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 9025 | /* Deserialize CRT digest from the end of the ticket. */ |
| 9026 | if( 2 > (size_t)( end - p ) ) |
| 9027 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 9028 | |
| 9029 | session->peer_cert_digest_type = (mbedtls_md_type_t) *p++; |
| 9030 | session->peer_cert_digest_len = (size_t) *p++; |
| 9031 | |
| 9032 | if( session->peer_cert_digest_len != 0 ) |
| 9033 | { |
| 9034 | const mbedtls_md_info_t *md_info = |
| 9035 | mbedtls_md_info_from_type( session->peer_cert_digest_type ); |
| 9036 | if( md_info == NULL ) |
| 9037 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 9038 | if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) ) |
| 9039 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 9040 | |
| 9041 | if( session->peer_cert_digest_len > (size_t)( end - p ) ) |
| 9042 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 9043 | |
| 9044 | session->peer_cert_digest = |
| 9045 | mbedtls_calloc( 1, session->peer_cert_digest_len ); |
| 9046 | if( session->peer_cert_digest == NULL ) |
| 9047 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 9048 | |
| 9049 | memcpy( session->peer_cert_digest, p, |
| 9050 | session->peer_cert_digest_len ); |
| 9051 | p += session->peer_cert_digest_len; |
| 9052 | } |
| 9053 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 9054 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 9055 | |
| 9056 | /* |
| 9057 | * Session ticket and associated data |
| 9058 | */ |
| 9059 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
| 9060 | if( 3 > (size_t)( end - p ) ) |
| 9061 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 9062 | |
| 9063 | session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2]; |
| 9064 | p += 3; |
| 9065 | |
| 9066 | if( session->ticket_len != 0 ) |
| 9067 | { |
| 9068 | if( session->ticket_len > (size_t)( end - p ) ) |
| 9069 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 9070 | |
| 9071 | session->ticket = mbedtls_calloc( 1, session->ticket_len ); |
| 9072 | if( session->ticket == NULL ) |
| 9073 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 9074 | |
| 9075 | memcpy( session->ticket, p, session->ticket_len ); |
| 9076 | p += session->ticket_len; |
| 9077 | } |
| 9078 | |
| 9079 | if( 4 > (size_t)( end - p ) ) |
| 9080 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 9081 | |
| 9082 | session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) | |
| 9083 | ( (uint32_t) p[1] << 16 ) | |
| 9084 | ( (uint32_t) p[2] << 8 ) | |
| 9085 | ( (uint32_t) p[3] ); |
| 9086 | p += 4; |
| 9087 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
| 9088 | |
| 9089 | /* |
| 9090 | * Misc extension-related info |
| 9091 | */ |
| 9092 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 9093 | if( 1 > (size_t)( end - p ) ) |
| 9094 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 9095 | |
| 9096 | session->mfl_code = *p++; |
| 9097 | #endif |
| 9098 | |
| 9099 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 9100 | if( 1 > (size_t)( end - p ) ) |
| 9101 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 9102 | |
| 9103 | session->encrypt_then_mac = *p++; |
| 9104 | #endif |
| 9105 | |
| 9106 | /* Done, should have consumed entire buffer */ |
| 9107 | if( p != end ) |
| 9108 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 9109 | |
| 9110 | return( 0 ); |
| 9111 | } |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 9112 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 9113 | |
XiaokangQian | 75d40ef | 2022-04-20 11:05:24 +0000 | [diff] [blame] | 9114 | int mbedtls_ssl_validate_ciphersuite( |
| 9115 | const mbedtls_ssl_context *ssl, |
| 9116 | const mbedtls_ssl_ciphersuite_t *suite_info, |
| 9117 | mbedtls_ssl_protocol_version min_tls_version, |
| 9118 | mbedtls_ssl_protocol_version max_tls_version ) |
| 9119 | { |
| 9120 | (void) ssl; |
| 9121 | |
| 9122 | if( suite_info == NULL ) |
| 9123 | return( -1 ); |
| 9124 | |
| 9125 | if( ( suite_info->min_tls_version > max_tls_version ) || |
| 9126 | ( suite_info->max_tls_version < min_tls_version ) ) |
| 9127 | { |
| 9128 | return( -1 ); |
| 9129 | } |
| 9130 | |
XiaokangQian | 060d867 | 2022-04-21 09:24:56 +0000 | [diff] [blame] | 9131 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_CLI_C) |
XiaokangQian | 75d40ef | 2022-04-20 11:05:24 +0000 | [diff] [blame] | 9132 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 9133 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 9134 | if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE && |
| 9135 | ssl->handshake->psa_pake_ctx_is_ok != 1 ) |
| 9136 | #else |
XiaokangQian | 75d40ef | 2022-04-20 11:05:24 +0000 | [diff] [blame] | 9137 | if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE && |
| 9138 | mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 ) |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 9139 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
XiaokangQian | 75d40ef | 2022-04-20 11:05:24 +0000 | [diff] [blame] | 9140 | { |
| 9141 | return( -1 ); |
| 9142 | } |
| 9143 | #endif |
| 9144 | |
| 9145 | /* Don't suggest PSK-based ciphersuite if no PSK is available. */ |
| 9146 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
| 9147 | if( mbedtls_ssl_ciphersuite_uses_psk( suite_info ) && |
| 9148 | mbedtls_ssl_conf_has_static_psk( ssl->conf ) == 0 ) |
| 9149 | { |
| 9150 | return( -1 ); |
| 9151 | } |
| 9152 | #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ |
| 9153 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 9154 | |
| 9155 | return( 0 ); |
| 9156 | } |
| 9157 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 9158 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9159 | /* |
| 9160 | * Function for writing a signature algorithm extension. |
| 9161 | * |
| 9162 | * The `extension_data` field of signature algorithm contains a `SignatureSchemeList` |
| 9163 | * value (TLS 1.3 RFC8446): |
| 9164 | * enum { |
| 9165 | * .... |
| 9166 | * ecdsa_secp256r1_sha256( 0x0403 ), |
| 9167 | * ecdsa_secp384r1_sha384( 0x0503 ), |
| 9168 | * ecdsa_secp521r1_sha512( 0x0603 ), |
| 9169 | * .... |
| 9170 | * } SignatureScheme; |
| 9171 | * |
| 9172 | * struct { |
| 9173 | * SignatureScheme supported_signature_algorithms<2..2^16-2>; |
| 9174 | * } SignatureSchemeList; |
| 9175 | * |
| 9176 | * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm` |
| 9177 | * value (TLS 1.2 RFC5246): |
| 9178 | * enum { |
| 9179 | * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5), |
| 9180 | * sha512(6), (255) |
| 9181 | * } HashAlgorithm; |
| 9182 | * |
| 9183 | * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) } |
| 9184 | * SignatureAlgorithm; |
| 9185 | * |
| 9186 | * struct { |
| 9187 | * HashAlgorithm hash; |
| 9188 | * SignatureAlgorithm signature; |
| 9189 | * } SignatureAndHashAlgorithm; |
| 9190 | * |
| 9191 | * SignatureAndHashAlgorithm |
| 9192 | * supported_signature_algorithms<2..2^16-2>; |
| 9193 | * |
| 9194 | * The TLS 1.3 signature algorithm extension was defined to be a compatible |
| 9195 | * generalization of the TLS 1.2 signature algorithm extension. |
| 9196 | * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by |
| 9197 | * `SignatureScheme` field of TLS 1.3 |
| 9198 | * |
| 9199 | */ |
| 9200 | int mbedtls_ssl_write_sig_alg_ext( mbedtls_ssl_context *ssl, unsigned char *buf, |
| 9201 | const unsigned char *end, size_t *out_len ) |
| 9202 | { |
| 9203 | unsigned char *p = buf; |
| 9204 | unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */ |
| 9205 | size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */ |
| 9206 | |
| 9207 | *out_len = 0; |
| 9208 | |
| 9209 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding signature_algorithms extension" ) ); |
| 9210 | |
| 9211 | /* Check if we have space for header and length field: |
| 9212 | * - extension_type (2 bytes) |
| 9213 | * - extension_data_length (2 bytes) |
| 9214 | * - supported_signature_algorithms_length (2 bytes) |
| 9215 | */ |
| 9216 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 ); |
| 9217 | p += 6; |
| 9218 | |
| 9219 | /* |
| 9220 | * Write supported_signature_algorithms |
| 9221 | */ |
| 9222 | supported_sig_alg = p; |
| 9223 | const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs( ssl ); |
| 9224 | if( sig_alg == NULL ) |
| 9225 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); |
| 9226 | |
| 9227 | for( ; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++ ) |
| 9228 | { |
Jerry Yu | 53f5c15 | 2022-06-22 20:24:38 +0800 | [diff] [blame] | 9229 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "got signature scheme [%x] %s", |
| 9230 | *sig_alg, |
| 9231 | mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) ); |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9232 | if( ! mbedtls_ssl_sig_alg_is_supported( ssl, *sig_alg ) ) |
| 9233 | continue; |
| 9234 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 ); |
| 9235 | MBEDTLS_PUT_UINT16_BE( *sig_alg, p, 0 ); |
| 9236 | p += 2; |
Jerry Yu | 80dd5db | 2022-06-22 19:30:32 +0800 | [diff] [blame] | 9237 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "sent signature scheme [%x] %s", |
Jerry Yu | f3b46b5 | 2022-06-19 16:52:27 +0800 | [diff] [blame] | 9238 | *sig_alg, |
| 9239 | mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) ); |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9240 | } |
| 9241 | |
| 9242 | /* Length of supported_signature_algorithms */ |
| 9243 | supported_sig_alg_len = p - supported_sig_alg; |
| 9244 | if( supported_sig_alg_len == 0 ) |
| 9245 | { |
| 9246 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "No signature algorithms defined." ) ); |
| 9247 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 9248 | } |
| 9249 | |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9250 | MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SIG_ALG, buf, 0 ); |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9251 | MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len + 2, buf, 2 ); |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9252 | MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len, buf, 4 ); |
| 9253 | |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9254 | *out_len = p - buf; |
| 9255 | |
| 9256 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | c4bf5d6 | 2022-10-29 09:08:47 +0800 | [diff] [blame] | 9257 | 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] | 9258 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Jerry Yu | 0c354a2 | 2022-08-29 15:25:36 +0800 | [diff] [blame] | 9259 | |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9260 | return( 0 ); |
| 9261 | } |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 9262 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9263 | |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 9264 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 9265 | /* |
| 9266 | * mbedtls_ssl_parse_server_name_ext |
| 9267 | * |
| 9268 | * Structure of server_name extension: |
| 9269 | * |
| 9270 | * enum { |
| 9271 | * host_name(0), (255) |
| 9272 | * } NameType; |
| 9273 | * opaque HostName<1..2^16-1>; |
| 9274 | * |
| 9275 | * struct { |
| 9276 | * NameType name_type; |
| 9277 | * select (name_type) { |
| 9278 | * case host_name: HostName; |
| 9279 | * } name; |
| 9280 | * } ServerName; |
| 9281 | * struct { |
| 9282 | * ServerName server_name_list<1..2^16-1> |
| 9283 | * } ServerNameList; |
| 9284 | */ |
Ronald Cron | ce7d76e | 2022-07-08 18:56:49 +0200 | [diff] [blame] | 9285 | MBEDTLS_CHECK_RETURN_CRITICAL |
XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 9286 | int mbedtls_ssl_parse_server_name_ext( mbedtls_ssl_context *ssl, |
| 9287 | const unsigned char *buf, |
| 9288 | const unsigned char *end ) |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 9289 | { |
| 9290 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 9291 | const unsigned char *p = buf; |
XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 9292 | size_t server_name_list_len, hostname_len; |
| 9293 | const unsigned char *server_name_list_end; |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 9294 | |
XiaokangQian | f2a9420 | 2022-05-20 06:44:24 +0000 | [diff] [blame] | 9295 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "parse ServerName extension" ) ); |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 9296 | |
| 9297 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 ); |
XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 9298 | server_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 ); |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 9299 | p += 2; |
| 9300 | |
XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 9301 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, server_name_list_len ); |
| 9302 | server_name_list_end = p + server_name_list_len; |
XiaokangQian | 75fe8c7 | 2022-06-15 09:42:45 +0000 | [diff] [blame] | 9303 | while( p < server_name_list_end ) |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 9304 | { |
XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 9305 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end, 3 ); |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 9306 | hostname_len = MBEDTLS_GET_UINT16_BE( p, 1 ); |
XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 9307 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end, |
| 9308 | hostname_len + 3 ); |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 9309 | |
| 9310 | if( p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME ) |
| 9311 | { |
XiaokangQian | 75fe8c7 | 2022-06-15 09:42:45 +0000 | [diff] [blame] | 9312 | /* sni_name is intended to be used only during the parsing of the |
| 9313 | * ClientHello message (it is reset to NULL before the end of |
| 9314 | * the message parsing). Thus it is ok to just point to the |
| 9315 | * reception buffer and not make a copy of it. |
| 9316 | */ |
XiaokangQian | f2a9420 | 2022-05-20 06:44:24 +0000 | [diff] [blame] | 9317 | ssl->handshake->sni_name = p + 3; |
| 9318 | ssl->handshake->sni_name_len = hostname_len; |
| 9319 | if( ssl->conf->f_sni == NULL ) |
| 9320 | return( 0 ); |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 9321 | ret = ssl->conf->f_sni( ssl->conf->p_sni, |
XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 9322 | ssl, p + 3, hostname_len ); |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 9323 | if( ret != 0 ) |
| 9324 | { |
XiaokangQian | f2a9420 | 2022-05-20 06:44:24 +0000 | [diff] [blame] | 9325 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_sni_wrapper", ret ); |
XiaokangQian | 129aeb9 | 2022-06-02 09:29:18 +0000 | [diff] [blame] | 9326 | MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME, |
| 9327 | MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME ); |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 9328 | return( MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME ); |
| 9329 | } |
| 9330 | return( 0 ); |
| 9331 | } |
| 9332 | |
| 9333 | p += hostname_len + 3; |
| 9334 | } |
| 9335 | |
| 9336 | return( 0 ); |
| 9337 | } |
| 9338 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
| 9339 | |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9340 | #if defined(MBEDTLS_SSL_ALPN) |
Ronald Cron | ce7d76e | 2022-07-08 18:56:49 +0200 | [diff] [blame] | 9341 | MBEDTLS_CHECK_RETURN_CRITICAL |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9342 | int mbedtls_ssl_parse_alpn_ext( mbedtls_ssl_context *ssl, |
| 9343 | const unsigned char *buf, |
| 9344 | const unsigned char *end ) |
| 9345 | { |
| 9346 | const unsigned char *p = buf; |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 9347 | size_t protocol_name_list_len; |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9348 | const unsigned char *protocol_name_list; |
| 9349 | const unsigned char *protocol_name_list_end; |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 9350 | size_t protocol_name_len; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9351 | |
| 9352 | /* If ALPN not configured, just ignore the extension */ |
| 9353 | if( ssl->conf->alpn_list == NULL ) |
| 9354 | return( 0 ); |
| 9355 | |
| 9356 | /* |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 9357 | * RFC7301, section 3.1 |
| 9358 | * opaque ProtocolName<1..2^8-1>; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9359 | * |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 9360 | * struct { |
| 9361 | * ProtocolName protocol_name_list<2..2^16-1> |
| 9362 | * } ProtocolNameList; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9363 | */ |
| 9364 | |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 9365 | /* |
XiaokangQian | 0b776e2 | 2022-06-24 09:04:59 +0000 | [diff] [blame] | 9366 | * protocol_name_list_len 2 bytes |
| 9367 | * protocol_name_len 1 bytes |
| 9368 | * protocol_name >=1 byte |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 9369 | */ |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9370 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 ); |
| 9371 | |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 9372 | protocol_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 ); |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9373 | p += 2; |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 9374 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, protocol_name_list_len ); |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9375 | protocol_name_list = p; |
| 9376 | protocol_name_list_end = p + protocol_name_list_len; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9377 | |
| 9378 | /* Validate peer's list (lengths) */ |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9379 | while( p < protocol_name_list_end ) |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9380 | { |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9381 | protocol_name_len = *p++; |
| 9382 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, protocol_name_list_end, |
| 9383 | protocol_name_len ); |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 9384 | if( protocol_name_len == 0 ) |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9385 | { |
| 9386 | MBEDTLS_SSL_PEND_FATAL_ALERT( |
| 9387 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER, |
| 9388 | MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER ); |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9389 | return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER ); |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9390 | } |
| 9391 | |
| 9392 | p += protocol_name_len; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9393 | } |
| 9394 | |
| 9395 | /* Use our order of preference */ |
| 9396 | for( const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++ ) |
| 9397 | { |
| 9398 | size_t const alpn_len = strlen( *alpn ); |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9399 | p = protocol_name_list; |
| 9400 | while( p < protocol_name_list_end ) |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9401 | { |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9402 | protocol_name_len = *p++; |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 9403 | if( protocol_name_len == alpn_len && |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9404 | memcmp( p, *alpn, alpn_len ) == 0 ) |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9405 | { |
| 9406 | ssl->alpn_chosen = *alpn; |
| 9407 | return( 0 ); |
| 9408 | } |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9409 | |
| 9410 | p += protocol_name_len; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9411 | } |
| 9412 | } |
| 9413 | |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9414 | /* If we get here, no match was found */ |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9415 | MBEDTLS_SSL_PEND_FATAL_ALERT( |
| 9416 | MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL, |
| 9417 | MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL ); |
| 9418 | return( MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL ); |
| 9419 | } |
| 9420 | |
| 9421 | int mbedtls_ssl_write_alpn_ext( mbedtls_ssl_context *ssl, |
| 9422 | unsigned char *buf, |
| 9423 | unsigned char *end, |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 9424 | size_t *out_len ) |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9425 | { |
| 9426 | unsigned char *p = buf; |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9427 | size_t protocol_name_len; |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 9428 | *out_len = 0; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9429 | |
| 9430 | if( ssl->alpn_chosen == NULL ) |
| 9431 | { |
| 9432 | return( 0 ); |
| 9433 | } |
| 9434 | |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9435 | protocol_name_len = strlen( ssl->alpn_chosen ); |
| 9436 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 7 + protocol_name_len ); |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9437 | |
| 9438 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "server side, adding alpn extension" ) ); |
| 9439 | /* |
| 9440 | * 0 . 1 ext identifier |
| 9441 | * 2 . 3 ext length |
| 9442 | * 4 . 5 protocol list length |
| 9443 | * 6 . 6 protocol name length |
| 9444 | * 7 . 7+n protocol name |
| 9445 | */ |
| 9446 | MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ALPN, p, 0 ); |
| 9447 | |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9448 | *out_len = 7 + protocol_name_len; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9449 | |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9450 | MBEDTLS_PUT_UINT16_BE( protocol_name_len + 3, p, 2 ); |
| 9451 | MBEDTLS_PUT_UINT16_BE( protocol_name_len + 1, p, 4 ); |
XiaokangQian | 0b776e2 | 2022-06-24 09:04:59 +0000 | [diff] [blame] | 9452 | /* Note: the length of the chosen protocol has been checked to be less |
| 9453 | * than 255 bytes in `mbedtls_ssl_conf_alpn_protocols`. |
| 9454 | */ |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9455 | p[6] = MBEDTLS_BYTE_0( protocol_name_len ); |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9456 | |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9457 | memcpy( p + 7, ssl->alpn_chosen, protocol_name_len ); |
Jerry Yu | b95dd36 | 2022-11-08 21:19:34 +0800 | [diff] [blame] | 9458 | |
| 9459 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 9460 | mbedtls_ssl_tls13_set_hs_sent_ext_mask( ssl, MBEDTLS_TLS_EXT_ALPN ); |
| 9461 | #endif |
| 9462 | |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9463 | return ( 0 ); |
| 9464 | } |
| 9465 | #endif /* MBEDTLS_SSL_ALPN */ |
| 9466 | |
Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 9467 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \ |
Xiaokang Qian | 0340929 | 2022-10-12 02:49:52 +0000 | [diff] [blame] | 9468 | defined(MBEDTLS_SSL_SESSION_TICKETS) && \ |
Xiaokang Qian | ed0620c | 2022-10-12 06:58:13 +0000 | [diff] [blame] | 9469 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \ |
Xiaokang Qian | ed3afcd | 2022-10-12 08:31:11 +0000 | [diff] [blame] | 9470 | defined(MBEDTLS_SSL_CLI_C) |
Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 9471 | int mbedtls_ssl_session_set_hostname( mbedtls_ssl_session *session, |
| 9472 | const char *hostname ) |
| 9473 | { |
| 9474 | /* Initialize to suppress unnecessary compiler warning */ |
| 9475 | size_t hostname_len = 0; |
| 9476 | |
| 9477 | /* Check if new hostname is valid before |
| 9478 | * making any change to current one */ |
| 9479 | if( hostname != NULL ) |
| 9480 | { |
| 9481 | hostname_len = strlen( hostname ); |
| 9482 | |
| 9483 | if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN ) |
| 9484 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 9485 | } |
| 9486 | |
| 9487 | /* Now it's clear that we will overwrite the old hostname, |
| 9488 | * so we can free it safely */ |
| 9489 | if( session->hostname != NULL ) |
| 9490 | { |
| 9491 | mbedtls_platform_zeroize( session->hostname, |
| 9492 | strlen( session->hostname ) ); |
| 9493 | mbedtls_free( session->hostname ); |
| 9494 | } |
| 9495 | |
| 9496 | /* Passing NULL as hostname shall clear the old one */ |
| 9497 | if( hostname == NULL ) |
| 9498 | { |
| 9499 | session->hostname = NULL; |
| 9500 | } |
| 9501 | else |
| 9502 | { |
| 9503 | session->hostname = mbedtls_calloc( 1, hostname_len + 1 ); |
| 9504 | if( session->hostname == NULL ) |
| 9505 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 9506 | |
| 9507 | memcpy( session->hostname, hostname, hostname_len ); |
| 9508 | } |
| 9509 | |
| 9510 | return( 0 ); |
| 9511 | } |
Xiaokang Qian | 0340929 | 2022-10-12 02:49:52 +0000 | [diff] [blame] | 9512 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 && |
| 9513 | MBEDTLS_SSL_SESSION_TICKETS && |
Xiaokang Qian | ed0620c | 2022-10-12 06:58:13 +0000 | [diff] [blame] | 9514 | MBEDTLS_SSL_SERVER_NAME_INDICATION && |
Xiaokang Qian | ed3afcd | 2022-10-12 08:31:11 +0000 | [diff] [blame] | 9515 | MBEDTLS_SSL_CLI_C */ |
Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 9516 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9517 | #endif /* MBEDTLS_SSL_TLS_C */ |