Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /* |
Mateusz Starzyk | 06b07fb | 2021-02-18 13:55:21 +0100 | [diff] [blame] | 2 | * TLS shared functions |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3 | * |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 4 | * Copyright The Mbed TLS Contributors |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: Apache-2.0 |
| 6 | * |
| 7 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 8 | * not use this file except in compliance with the License. |
| 9 | * You may obtain a copy of the License at |
| 10 | * |
| 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | * |
| 13 | * Unless required by applicable law or agreed to in writing, software |
| 14 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | * See the License for the specific language governing permissions and |
| 17 | * limitations under the License. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 18 | */ |
| 19 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 20 | * http://www.ietf.org/rfc/rfc2246.txt |
| 21 | * http://www.ietf.org/rfc/rfc4346.txt |
| 22 | */ |
| 23 | |
Gilles Peskine | db09ef6 | 2020-06-03 01:43:33 +0200 | [diff] [blame] | 24 | #include "common.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 25 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 26 | #if defined(MBEDTLS_SSL_TLS_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 27 | |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 28 | #include <assert.h> |
| 29 | |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 30 | #include "mbedtls/platform.h" |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 31 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 32 | #include "mbedtls/ssl.h" |
Ronald Cron | 9f0fba3 | 2022-02-10 16:45:15 +0100 | [diff] [blame] | 33 | #include "ssl_client.h" |
Ronald Cron | 27c85e7 | 2022-03-08 11:37:55 +0100 | [diff] [blame] | 34 | #include "ssl_debug_helpers.h" |
Chris Jones | 84a773f | 2021-03-05 18:38:47 +0000 | [diff] [blame] | 35 | #include "ssl_misc.h" |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 36 | |
Janos Follath | 73c616b | 2019-12-18 15:07:04 +0000 | [diff] [blame] | 37 | #include "mbedtls/debug.h" |
| 38 | #include "mbedtls/error.h" |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 39 | #include "mbedtls/platform_util.h" |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 40 | #include "mbedtls/version.h" |
Gabor Mezei | 765862c | 2021-10-19 12:22:25 +0200 | [diff] [blame] | 41 | #include "mbedtls/constant_time.h" |
Paul Bakker | 0be444a | 2013-08-27 21:55:01 +0200 | [diff] [blame] | 42 | |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 43 | #include <string.h> |
| 44 | |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 45 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 46 | #include "mbedtls/psa_util.h" |
| 47 | #include "psa/crypto.h" |
| 48 | #endif |
Manuel Pégourié-Gonnard | 07018f9 | 2022-09-15 11:29:35 +0200 | [diff] [blame] | 49 | #include "mbedtls/legacy_or_psa.h" |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 50 | |
Janos Follath | 23bdca0 | 2016-10-07 14:47:14 +0100 | [diff] [blame] | 51 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 52 | #include "mbedtls/oid.h" |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 53 | #endif |
| 54 | |
Ronald Cron | ad8c17b | 2022-06-10 17:18:09 +0200 | [diff] [blame] | 55 | #if defined(MBEDTLS_TEST_HOOKS) |
| 56 | static mbedtls_ssl_chk_buf_ptr_args chk_buf_ptr_fail_args; |
| 57 | |
| 58 | void mbedtls_ssl_set_chk_buf_ptr_fail_args( |
| 59 | const uint8_t *cur, const uint8_t *end, size_t need ) |
| 60 | { |
| 61 | chk_buf_ptr_fail_args.cur = cur; |
| 62 | chk_buf_ptr_fail_args.end = end; |
| 63 | chk_buf_ptr_fail_args.need = need; |
| 64 | } |
| 65 | |
| 66 | void mbedtls_ssl_reset_chk_buf_ptr_fail_args( void ) |
| 67 | { |
| 68 | memset( &chk_buf_ptr_fail_args, 0, sizeof( chk_buf_ptr_fail_args ) ); |
| 69 | } |
| 70 | |
| 71 | int mbedtls_ssl_cmp_chk_buf_ptr_fail_args( mbedtls_ssl_chk_buf_ptr_args *args ) |
| 72 | { |
| 73 | return( ( chk_buf_ptr_fail_args.cur != args->cur ) || |
| 74 | ( chk_buf_ptr_fail_args.end != args->end ) || |
| 75 | ( chk_buf_ptr_fail_args.need != args->need ) ); |
| 76 | } |
| 77 | #endif /* MBEDTLS_TEST_HOOKS */ |
| 78 | |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 79 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 80 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 81 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 82 | /* Top-level Connection ID API */ |
| 83 | |
Hanno Becker | 8367ccc | 2019-05-14 11:30:10 +0100 | [diff] [blame] | 84 | int mbedtls_ssl_conf_cid( mbedtls_ssl_config *conf, |
| 85 | size_t len, |
| 86 | int ignore_other_cid ) |
Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 87 | { |
| 88 | if( len > MBEDTLS_SSL_CID_IN_LEN_MAX ) |
| 89 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 90 | |
Hanno Becker | 611ac77 | 2019-05-14 11:45:26 +0100 | [diff] [blame] | 91 | if( ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL && |
| 92 | ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE ) |
| 93 | { |
| 94 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 95 | } |
| 96 | |
| 97 | conf->ignore_unexpected_cid = ignore_other_cid; |
Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 98 | conf->cid_len = len; |
| 99 | return( 0 ); |
| 100 | } |
| 101 | |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 102 | int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl, |
| 103 | int enable, |
| 104 | unsigned char const *own_cid, |
| 105 | size_t own_cid_len ) |
| 106 | { |
Hanno Becker | 76a79ab | 2019-05-03 14:38:32 +0100 | [diff] [blame] | 107 | if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 108 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 109 | |
Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 110 | ssl->negotiate_cid = enable; |
| 111 | if( enable == MBEDTLS_SSL_CID_DISABLED ) |
| 112 | { |
| 113 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) ); |
| 114 | return( 0 ); |
| 115 | } |
| 116 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) ); |
Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 117 | MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len ); |
Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 118 | |
Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 119 | if( own_cid_len != ssl->conf->cid_len ) |
Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 120 | { |
Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 121 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID length %u does not match CID length %u in config", |
| 122 | (unsigned) own_cid_len, |
| 123 | (unsigned) ssl->conf->cid_len ) ); |
Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 124 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 125 | } |
| 126 | |
| 127 | memcpy( ssl->own_cid, own_cid, own_cid_len ); |
Hanno Becker | b7ee0cf | 2019-04-30 14:07:31 +0100 | [diff] [blame] | 128 | /* Truncation is not an issue here because |
| 129 | * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */ |
| 130 | ssl->own_cid_len = (uint8_t) own_cid_len; |
Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 131 | |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 132 | return( 0 ); |
| 133 | } |
| 134 | |
Paul Elliott | 0113cf1 | 2022-03-11 20:26:47 +0000 | [diff] [blame] | 135 | int mbedtls_ssl_get_own_cid( mbedtls_ssl_context *ssl, |
| 136 | int *enabled, |
| 137 | unsigned char own_cid[MBEDTLS_SSL_CID_OUT_LEN_MAX], |
| 138 | size_t *own_cid_len ) |
| 139 | { |
| 140 | *enabled = MBEDTLS_SSL_CID_DISABLED; |
| 141 | |
| 142 | if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 143 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 144 | |
| 145 | /* We report MBEDTLS_SSL_CID_DISABLED in case the CID length is |
| 146 | * zero as this is indistinguishable from not requesting to use |
| 147 | * the CID extension. */ |
| 148 | if( ssl->own_cid_len == 0 || ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED ) |
| 149 | return( 0 ); |
| 150 | |
| 151 | if( own_cid_len != NULL ) |
| 152 | { |
| 153 | *own_cid_len = ssl->own_cid_len; |
| 154 | if( own_cid != NULL ) |
| 155 | memcpy( own_cid, ssl->own_cid, ssl->own_cid_len ); |
| 156 | } |
| 157 | |
| 158 | *enabled = MBEDTLS_SSL_CID_ENABLED; |
| 159 | |
| 160 | return( 0 ); |
| 161 | } |
| 162 | |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 163 | int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl, |
| 164 | int *enabled, |
| 165 | unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ], |
| 166 | size_t *peer_cid_len ) |
| 167 | { |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 168 | *enabled = MBEDTLS_SSL_CID_DISABLED; |
Hanno Becker | b1f89cd | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 169 | |
Hanno Becker | 76a79ab | 2019-05-03 14:38:32 +0100 | [diff] [blame] | 170 | if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM || |
Paul Elliott | 27b0d94 | 2022-03-18 21:55:32 +0000 | [diff] [blame] | 171 | mbedtls_ssl_is_handshake_over( ssl ) == 0 ) |
Hanno Becker | 76a79ab | 2019-05-03 14:38:32 +0100 | [diff] [blame] | 172 | { |
Hanno Becker | b1f89cd | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 173 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Hanno Becker | 76a79ab | 2019-05-03 14:38:32 +0100 | [diff] [blame] | 174 | } |
Hanno Becker | b1f89cd | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 175 | |
Hanno Becker | c5f2422 | 2019-05-03 12:54:52 +0100 | [diff] [blame] | 176 | /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions |
| 177 | * were used, but client and server requested the empty CID. |
| 178 | * This is indistinguishable from not using the CID extension |
| 179 | * in the first place. */ |
Hanno Becker | b1f89cd | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 180 | if( ssl->transform_in->in_cid_len == 0 && |
| 181 | ssl->transform_in->out_cid_len == 0 ) |
| 182 | { |
| 183 | return( 0 ); |
| 184 | } |
| 185 | |
Hanno Becker | 615ef17 | 2019-05-22 16:50:35 +0100 | [diff] [blame] | 186 | if( peer_cid_len != NULL ) |
| 187 | { |
| 188 | *peer_cid_len = ssl->transform_in->out_cid_len; |
| 189 | if( peer_cid != NULL ) |
| 190 | { |
| 191 | memcpy( peer_cid, ssl->transform_in->out_cid, |
| 192 | ssl->transform_in->out_cid_len ); |
| 193 | } |
| 194 | } |
Hanno Becker | b1f89cd | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 195 | |
| 196 | *enabled = MBEDTLS_SSL_CID_ENABLED; |
| 197 | |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 198 | return( 0 ); |
| 199 | } |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 200 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 201 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 202 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 203 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 204 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Manuel Pégourié-Gonnard | 581e6b6 | 2013-07-18 12:32:27 +0200 | [diff] [blame] | 205 | /* |
| 206 | * Convert max_fragment_length codes to length. |
| 207 | * RFC 6066 says: |
| 208 | * enum{ |
| 209 | * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255) |
| 210 | * } MaxFragmentLength; |
| 211 | * and we add 0 -> extension unused |
| 212 | */ |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 213 | static unsigned int ssl_mfl_code_to_length( int mfl ) |
Manuel Pégourié-Gonnard | 581e6b6 | 2013-07-18 12:32:27 +0200 | [diff] [blame] | 214 | { |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 215 | switch( mfl ) |
| 216 | { |
| 217 | case MBEDTLS_SSL_MAX_FRAG_LEN_NONE: |
| 218 | return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN ); |
| 219 | case MBEDTLS_SSL_MAX_FRAG_LEN_512: |
| 220 | return 512; |
| 221 | case MBEDTLS_SSL_MAX_FRAG_LEN_1024: |
| 222 | return 1024; |
| 223 | case MBEDTLS_SSL_MAX_FRAG_LEN_2048: |
| 224 | return 2048; |
| 225 | case MBEDTLS_SSL_MAX_FRAG_LEN_4096: |
| 226 | return 4096; |
| 227 | default: |
| 228 | return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN ); |
| 229 | } |
| 230 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 231 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
Manuel Pégourié-Gonnard | 581e6b6 | 2013-07-18 12:32:27 +0200 | [diff] [blame] | 232 | |
Hanno Becker | 52055ae | 2019-02-06 14:30:46 +0000 | [diff] [blame] | 233 | int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst, |
| 234 | const mbedtls_ssl_session *src ) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 235 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 236 | mbedtls_ssl_session_free( dst ); |
| 237 | memcpy( dst, src, sizeof( mbedtls_ssl_session ) ); |
吴敬辉 | 0b71611 | 2021-11-29 10:46:35 +0800 | [diff] [blame] | 238 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
| 239 | dst->ticket = NULL; |
Xiaokang Qian | 8730644 | 2022-10-12 09:47:38 +0000 | [diff] [blame] | 240 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \ |
| 241 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Xiaokang Qian | 126bf8e | 2022-10-13 02:22:40 +0000 | [diff] [blame] | 242 | dst->hostname = NULL; |
吴敬辉 | 0b71611 | 2021-11-29 10:46:35 +0800 | [diff] [blame] | 243 | #endif |
Xiaokang Qian | 8730644 | 2022-10-12 09:47:38 +0000 | [diff] [blame] | 244 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
吴敬辉 | 0b71611 | 2021-11-29 10:46:35 +0800 | [diff] [blame] | 245 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 246 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Hanno Becker | 6d1986e | 2019-02-07 12:27:42 +0000 | [diff] [blame] | 247 | |
| 248 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 249 | if( src->peer_cert != NULL ) |
| 250 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 251 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 2292d1f | 2013-09-15 17:06:49 +0200 | [diff] [blame] | 252 | |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 253 | dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 254 | if( dst->peer_cert == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 255 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 256 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 257 | mbedtls_x509_crt_init( dst->peer_cert ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 258 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 259 | if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p, |
Manuel Pégourié-Gonnard | 4d2a8eb | 2014-06-13 20:33:27 +0200 | [diff] [blame] | 260 | src->peer_cert->raw.len ) ) != 0 ) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 261 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 262 | mbedtls_free( dst->peer_cert ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 263 | dst->peer_cert = NULL; |
| 264 | return( ret ); |
| 265 | } |
| 266 | } |
Hanno Becker | 6d1986e | 2019-02-07 12:27:42 +0000 | [diff] [blame] | 267 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Hanno Becker | 9198ad1 | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 268 | if( src->peer_cert_digest != NULL ) |
| 269 | { |
Hanno Becker | 9198ad1 | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 270 | dst->peer_cert_digest = |
Hanno Becker | accc599 | 2019-02-25 10:06:59 +0000 | [diff] [blame] | 271 | mbedtls_calloc( 1, src->peer_cert_digest_len ); |
Hanno Becker | 9198ad1 | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 272 | if( dst->peer_cert_digest == NULL ) |
| 273 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 274 | |
| 275 | memcpy( dst->peer_cert_digest, src->peer_cert_digest, |
| 276 | src->peer_cert_digest_len ); |
| 277 | dst->peer_cert_digest_type = src->peer_cert_digest_type; |
Hanno Becker | accc599 | 2019-02-25 10:06:59 +0000 | [diff] [blame] | 278 | dst->peer_cert_digest_len = src->peer_cert_digest_len; |
Hanno Becker | 9198ad1 | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 279 | } |
| 280 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 281 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 282 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 283 | |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 284 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 285 | if( src->ticket != NULL ) |
| 286 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 287 | dst->ticket = mbedtls_calloc( 1, src->ticket_len ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 288 | if( dst->ticket == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 289 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 290 | |
| 291 | memcpy( dst->ticket, src->ticket, src->ticket_len ); |
| 292 | } |
Xiaokang Qian | 126bf8e | 2022-10-13 02:22:40 +0000 | [diff] [blame] | 293 | |
| 294 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \ |
| 295 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 296 | if( src->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 297 | { |
| 298 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 299 | ret = mbedtls_ssl_session_set_hostname( dst, src->hostname ); |
| 300 | if( ret != 0 ) |
| 301 | return ( ret ); |
| 302 | } |
| 303 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 && |
| 304 | MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 305 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 306 | |
| 307 | return( 0 ); |
| 308 | } |
| 309 | |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 310 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 311 | MBEDTLS_CHECK_RETURN_CRITICAL |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 312 | static int resize_buffer( unsigned char **buffer, size_t len_new, size_t *len_old ) |
| 313 | { |
| 314 | unsigned char* resized_buffer = mbedtls_calloc( 1, len_new ); |
| 315 | if( resized_buffer == NULL ) |
| 316 | return -1; |
| 317 | |
| 318 | /* We want to copy len_new bytes when downsizing the buffer, and |
| 319 | * len_old bytes when upsizing, so we choose the smaller of two sizes, |
| 320 | * to fit one buffer into another. Size checks, ensuring that no data is |
| 321 | * lost, are done outside of this function. */ |
| 322 | memcpy( resized_buffer, *buffer, |
| 323 | ( len_new < *len_old ) ? len_new : *len_old ); |
| 324 | mbedtls_platform_zeroize( *buffer, *len_old ); |
| 325 | mbedtls_free( *buffer ); |
| 326 | |
| 327 | *buffer = resized_buffer; |
| 328 | *len_old = len_new; |
| 329 | |
| 330 | return 0; |
| 331 | } |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 332 | |
| 333 | static void handle_buffer_resizing( mbedtls_ssl_context *ssl, int downsizing, |
Andrzej Kurek | 069fa96 | 2021-01-07 08:02:15 -0500 | [diff] [blame] | 334 | size_t in_buf_new_len, |
| 335 | size_t out_buf_new_len ) |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 336 | { |
| 337 | int modified = 0; |
| 338 | size_t written_in = 0, iv_offset_in = 0, len_offset_in = 0; |
| 339 | size_t written_out = 0, iv_offset_out = 0, len_offset_out = 0; |
| 340 | if( ssl->in_buf != NULL ) |
| 341 | { |
| 342 | written_in = ssl->in_msg - ssl->in_buf; |
| 343 | iv_offset_in = ssl->in_iv - ssl->in_buf; |
| 344 | len_offset_in = ssl->in_len - ssl->in_buf; |
| 345 | if( downsizing ? |
| 346 | ssl->in_buf_len > in_buf_new_len && ssl->in_left < in_buf_new_len : |
| 347 | ssl->in_buf_len < in_buf_new_len ) |
| 348 | { |
| 349 | if( resize_buffer( &ssl->in_buf, in_buf_new_len, &ssl->in_buf_len ) != 0 ) |
| 350 | { |
| 351 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "input buffer resizing failed - out of memory" ) ); |
| 352 | } |
| 353 | else |
| 354 | { |
Paul Elliott | b744990 | 2021-03-10 18:14:58 +0000 | [diff] [blame] | 355 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reallocating in_buf to %" MBEDTLS_PRINTF_SIZET, |
| 356 | in_buf_new_len ) ); |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 357 | modified = 1; |
| 358 | } |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | if( ssl->out_buf != NULL ) |
| 363 | { |
| 364 | written_out = ssl->out_msg - ssl->out_buf; |
| 365 | iv_offset_out = ssl->out_iv - ssl->out_buf; |
| 366 | len_offset_out = ssl->out_len - ssl->out_buf; |
| 367 | if( downsizing ? |
| 368 | ssl->out_buf_len > out_buf_new_len && ssl->out_left < out_buf_new_len : |
| 369 | ssl->out_buf_len < out_buf_new_len ) |
| 370 | { |
| 371 | if( resize_buffer( &ssl->out_buf, out_buf_new_len, &ssl->out_buf_len ) != 0 ) |
| 372 | { |
| 373 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "output buffer resizing failed - out of memory" ) ); |
| 374 | } |
| 375 | else |
| 376 | { |
Paul Elliott | b744990 | 2021-03-10 18:14:58 +0000 | [diff] [blame] | 377 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reallocating out_buf to %" MBEDTLS_PRINTF_SIZET, |
| 378 | out_buf_new_len ) ); |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 379 | modified = 1; |
| 380 | } |
| 381 | } |
| 382 | } |
| 383 | if( modified ) |
| 384 | { |
| 385 | /* Update pointers here to avoid doing it twice. */ |
| 386 | mbedtls_ssl_reset_in_out_pointers( ssl ); |
| 387 | /* Fields below might not be properly updated with record |
| 388 | * splitting or with CID, so they are manually updated here. */ |
| 389 | ssl->out_msg = ssl->out_buf + written_out; |
| 390 | ssl->out_len = ssl->out_buf + len_offset_out; |
| 391 | ssl->out_iv = ssl->out_buf + iv_offset_out; |
| 392 | |
| 393 | ssl->in_msg = ssl->in_buf + written_in; |
| 394 | ssl->in_len = ssl->in_buf + len_offset_in; |
| 395 | ssl->in_iv = ssl->in_buf + iv_offset_in; |
| 396 | } |
| 397 | } |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 398 | #endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */ |
| 399 | |
Jerry Yu | db8c48a | 2022-01-27 14:54:54 +0800 | [diff] [blame] | 400 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Jerry Yu | ed14c93 | 2022-02-17 13:40:45 +0800 | [diff] [blame] | 401 | |
| 402 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
| 403 | typedef int (*tls_prf_fn)( const unsigned char *secret, size_t slen, |
| 404 | const char *label, |
| 405 | const unsigned char *random, size_t rlen, |
| 406 | unsigned char *dstbuf, size_t dlen ); |
| 407 | |
| 408 | static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id ); |
| 409 | |
| 410 | #endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */ |
| 411 | |
| 412 | /* Type for the TLS PRF */ |
| 413 | typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *, |
| 414 | const unsigned char *, size_t, |
| 415 | unsigned char *, size_t); |
| 416 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 417 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | ed14c93 | 2022-02-17 13:40:45 +0800 | [diff] [blame] | 418 | static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform, |
| 419 | int ciphersuite, |
| 420 | const unsigned char master[48], |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 421 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Jerry Yu | ed14c93 | 2022-02-17 13:40:45 +0800 | [diff] [blame] | 422 | int encrypt_then_mac, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 423 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Jerry Yu | ed14c93 | 2022-02-17 13:40:45 +0800 | [diff] [blame] | 424 | ssl_tls_prf_t tls_prf, |
| 425 | const unsigned char randbytes[64], |
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 426 | mbedtls_ssl_protocol_version tls_version, |
Jerry Yu | ed14c93 | 2022-02-17 13:40:45 +0800 | [diff] [blame] | 427 | unsigned endpoint, |
| 428 | const mbedtls_ssl_context *ssl ); |
| 429 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 430 | #if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 431 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | ed14c93 | 2022-02-17 13:40:45 +0800 | [diff] [blame] | 432 | static int tls_prf_sha256( const unsigned char *secret, size_t slen, |
| 433 | const char *label, |
| 434 | const unsigned char *random, size_t rlen, |
| 435 | unsigned char *dstbuf, size_t dlen ); |
| 436 | static void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *,unsigned char*, size_t * ); |
| 437 | static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int ); |
| 438 | |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 439 | #endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/ |
Jerry Yu | ed14c93 | 2022-02-17 13:40:45 +0800 | [diff] [blame] | 440 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 441 | #if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 442 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | ed14c93 | 2022-02-17 13:40:45 +0800 | [diff] [blame] | 443 | static int tls_prf_sha384( const unsigned char *secret, size_t slen, |
| 444 | const char *label, |
| 445 | const unsigned char *random, size_t rlen, |
| 446 | unsigned char *dstbuf, size_t dlen ); |
| 447 | |
| 448 | static void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *, unsigned char*, size_t * ); |
| 449 | static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int ); |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 450 | #endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/ |
Jerry Yu | ed14c93 | 2022-02-17 13:40:45 +0800 | [diff] [blame] | 451 | |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 452 | static size_t ssl_tls12_session_save( const mbedtls_ssl_session *session, |
Jerry Yu | ed14c93 | 2022-02-17 13:40:45 +0800 | [diff] [blame] | 453 | unsigned char *buf, |
| 454 | size_t buf_len ); |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 455 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 456 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 457 | static int ssl_tls12_session_load( mbedtls_ssl_session *session, |
Jerry Yu | ed14c93 | 2022-02-17 13:40:45 +0800 | [diff] [blame] | 458 | const unsigned char *buf, |
| 459 | size_t len ); |
| 460 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 461 | |
Jerry Yu | 53d23e2 | 2022-02-09 16:25:09 +0800 | [diff] [blame] | 462 | static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t ); |
| 463 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 464 | #if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Jerry Yu | db8c48a | 2022-01-27 14:54:54 +0800 | [diff] [blame] | 465 | static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t ); |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 466 | #endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/ |
Jerry Yu | db8c48a | 2022-01-27 14:54:54 +0800 | [diff] [blame] | 467 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 468 | #if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Jerry Yu | db8c48a | 2022-01-27 14:54:54 +0800 | [diff] [blame] | 469 | static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t ); |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 470 | #endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/ |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 471 | |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 472 | int mbedtls_ssl_tls_prf( const mbedtls_tls_prf_types prf, |
| 473 | const unsigned char *secret, size_t slen, |
| 474 | const char *label, |
| 475 | const unsigned char *random, size_t rlen, |
| 476 | unsigned char *dstbuf, size_t dlen ) |
| 477 | { |
| 478 | mbedtls_ssl_tls_prf_cb *tls_prf = NULL; |
| 479 | |
| 480 | switch( prf ) |
| 481 | { |
Ron Eldor | d2f25f7 | 2019-05-15 14:54:22 +0300 | [diff] [blame] | 482 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 483 | #if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 484 | case MBEDTLS_SSL_TLS_PRF_SHA384: |
| 485 | tls_prf = tls_prf_sha384; |
| 486 | break; |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 487 | #endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/ |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 488 | #if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 489 | case MBEDTLS_SSL_TLS_PRF_SHA256: |
| 490 | tls_prf = tls_prf_sha256; |
| 491 | break; |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 492 | #endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/ |
Ron Eldor | d2f25f7 | 2019-05-15 14:54:22 +0300 | [diff] [blame] | 493 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 494 | default: |
| 495 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 496 | } |
| 497 | |
| 498 | return( tls_prf( secret, slen, label, random, rlen, dstbuf, dlen ) ); |
| 499 | } |
| 500 | |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 501 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 502 | static void ssl_clear_peer_cert( mbedtls_ssl_session *session ) |
| 503 | { |
| 504 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 505 | if( session->peer_cert != NULL ) |
| 506 | { |
| 507 | mbedtls_x509_crt_free( session->peer_cert ); |
| 508 | mbedtls_free( session->peer_cert ); |
| 509 | session->peer_cert = NULL; |
| 510 | } |
| 511 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 512 | if( session->peer_cert_digest != NULL ) |
| 513 | { |
| 514 | /* Zeroization is not necessary. */ |
| 515 | mbedtls_free( session->peer_cert_digest ); |
| 516 | session->peer_cert_digest = NULL; |
| 517 | session->peer_cert_digest_type = MBEDTLS_MD_NONE; |
| 518 | session->peer_cert_digest_len = 0; |
| 519 | } |
| 520 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 521 | } |
| 522 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 523 | |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 524 | uint32_t mbedtls_ssl_get_extension_id( unsigned int extension_type ) |
| 525 | { |
| 526 | switch( extension_type ) |
| 527 | { |
| 528 | case MBEDTLS_TLS_EXT_SERVERNAME: |
| 529 | return( MBEDTLS_SSL_EXT_ID_SERVERNAME ); |
| 530 | |
| 531 | case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH: |
| 532 | return( MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH ); |
| 533 | |
| 534 | case MBEDTLS_TLS_EXT_STATUS_REQUEST: |
| 535 | return( MBEDTLS_SSL_EXT_ID_STATUS_REQUEST ); |
| 536 | |
| 537 | case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS: |
| 538 | return( MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS ); |
| 539 | |
| 540 | case MBEDTLS_TLS_EXT_SIG_ALG: |
| 541 | return( MBEDTLS_SSL_EXT_ID_SIG_ALG ); |
| 542 | |
| 543 | case MBEDTLS_TLS_EXT_USE_SRTP: |
| 544 | return( MBEDTLS_SSL_EXT_ID_USE_SRTP ); |
| 545 | |
| 546 | case MBEDTLS_TLS_EXT_HEARTBEAT: |
| 547 | return( MBEDTLS_SSL_EXT_ID_HEARTBEAT ); |
| 548 | |
| 549 | case MBEDTLS_TLS_EXT_ALPN: |
| 550 | return( MBEDTLS_SSL_EXT_ID_ALPN ); |
| 551 | |
| 552 | case MBEDTLS_TLS_EXT_SCT: |
| 553 | return( MBEDTLS_SSL_EXT_ID_SCT ); |
| 554 | |
| 555 | case MBEDTLS_TLS_EXT_CLI_CERT_TYPE: |
| 556 | return( MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE ); |
| 557 | |
| 558 | case MBEDTLS_TLS_EXT_SERV_CERT_TYPE: |
| 559 | return( MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE ); |
| 560 | |
| 561 | case MBEDTLS_TLS_EXT_PADDING: |
| 562 | return( MBEDTLS_SSL_EXT_ID_PADDING ); |
| 563 | |
| 564 | case MBEDTLS_TLS_EXT_PRE_SHARED_KEY: |
| 565 | return( MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY ); |
| 566 | |
| 567 | case MBEDTLS_TLS_EXT_EARLY_DATA: |
| 568 | return( MBEDTLS_SSL_EXT_ID_EARLY_DATA ); |
| 569 | |
| 570 | case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS: |
| 571 | return( MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS ); |
| 572 | |
| 573 | case MBEDTLS_TLS_EXT_COOKIE: |
| 574 | return( MBEDTLS_SSL_EXT_ID_COOKIE ); |
| 575 | |
| 576 | case MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES: |
| 577 | return( MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES ); |
| 578 | |
| 579 | case MBEDTLS_TLS_EXT_CERT_AUTH: |
| 580 | return( MBEDTLS_SSL_EXT_ID_CERT_AUTH ); |
| 581 | |
| 582 | case MBEDTLS_TLS_EXT_OID_FILTERS: |
| 583 | return( MBEDTLS_SSL_EXT_ID_OID_FILTERS ); |
| 584 | |
| 585 | case MBEDTLS_TLS_EXT_POST_HANDSHAKE_AUTH: |
| 586 | return( MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH ); |
| 587 | |
| 588 | case MBEDTLS_TLS_EXT_SIG_ALG_CERT: |
| 589 | return( MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT ); |
| 590 | |
| 591 | case MBEDTLS_TLS_EXT_KEY_SHARE: |
| 592 | return( MBEDTLS_SSL_EXT_ID_KEY_SHARE ); |
| 593 | |
| 594 | case MBEDTLS_TLS_EXT_TRUNCATED_HMAC: |
| 595 | return( MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC ); |
| 596 | |
| 597 | case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS: |
| 598 | return( MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS ); |
| 599 | |
| 600 | case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC: |
| 601 | return( MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC ); |
| 602 | |
| 603 | case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET: |
| 604 | return( MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET ); |
| 605 | |
| 606 | case MBEDTLS_TLS_EXT_SESSION_TICKET: |
| 607 | return( MBEDTLS_SSL_EXT_ID_SESSION_TICKET ); |
| 608 | |
| 609 | } |
| 610 | |
| 611 | return( MBEDTLS_SSL_EXT_ID_UNRECOGNIZED ); |
| 612 | } |
| 613 | |
| 614 | uint32_t mbedtls_ssl_get_extension_mask( unsigned int extension_type ) |
| 615 | { |
| 616 | return( 1 << mbedtls_ssl_get_extension_id( extension_type ) ); |
| 617 | } |
| 618 | |
Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame^] | 619 | #if defined(MBEDTLS_DEBUG_C) |
| 620 | static const char *extension_name_table[] = { |
| 621 | [MBEDTLS_SSL_EXT_ID_UNRECOGNIZED] = "unreognized", |
| 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 | |
| 651 | static unsigned int extension_type_tbl[]={ |
| 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 | } |
| 707 | return( NULL ); |
| 708 | } |
| 709 | |
| 710 | void mbedtls_ssl_print_extension_type( const mbedtls_ssl_context *ssl, |
| 711 | int level, const char *file, int line, |
| 712 | int hs_msg_type, |
| 713 | unsigned int extension_type, |
| 714 | const char *extra_msg0, |
| 715 | const char *extra_msg1 ) |
| 716 | { |
| 717 | const char *extra_msg; |
| 718 | if( extra_msg0 && extra_msg1 ) |
| 719 | { |
| 720 | mbedtls_debug_print_msg( |
| 721 | ssl, level, file, line, |
| 722 | "%s: %s(%u) extension %s %s.", |
| 723 | ssl_tls13_get_hs_msg_name( hs_msg_type ), |
| 724 | mbedtls_ssl_get_extension_name( extension_type ), |
| 725 | extension_type, |
| 726 | extra_msg0, extra_msg1 ); |
| 727 | return; |
| 728 | } |
| 729 | |
| 730 | extra_msg = extra_msg0 ? extra_msg0 : extra_msg1; |
| 731 | if( extra_msg ) |
| 732 | { |
| 733 | mbedtls_debug_print_msg( |
| 734 | ssl, level, file, line, |
| 735 | "%s: %s(%u) extension %s.", ssl_tls13_get_hs_msg_name( hs_msg_type ), |
| 736 | mbedtls_ssl_get_extension_name( extension_type ), extension_type, |
| 737 | extra_msg ); |
| 738 | return; |
| 739 | } |
| 740 | |
| 741 | mbedtls_debug_print_msg( |
| 742 | ssl, level, file, line, |
| 743 | "%s: %s(%u) extension.", ssl_tls13_get_hs_msg_name( hs_msg_type ), |
| 744 | mbedtls_ssl_get_extension_name( extension_type ), extension_type ); |
| 745 | } |
| 746 | |
| 747 | void mbedtls_ssl_print_extensions( const mbedtls_ssl_context *ssl, |
| 748 | int level, const char *file, int line, |
| 749 | int hs_msg_type, uint32_t extensions_mask, |
| 750 | const char *extra ) |
| 751 | { |
| 752 | |
| 753 | for( unsigned i = 0; |
| 754 | i < sizeof( extension_name_table ) / sizeof( extension_name_table[0] ); |
| 755 | i++ ) |
| 756 | { |
| 757 | mbedtls_ssl_print_extension_type( |
| 758 | ssl, level, file, line, hs_msg_type, extension_type_tbl[i], |
| 759 | extensions_mask & ( 1 << i ) ? "was" : "was not", extra ); |
| 760 | } |
| 761 | } |
| 762 | |
| 763 | #endif /* MBEDTLS_DEBUG_C */ |
| 764 | |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 765 | void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl, |
| 766 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info ) |
| 767 | { |
| 768 | ((void) ciphersuite_info); |
| 769 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 770 | #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] | 771 | if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 ) |
| 772 | ssl->handshake->update_checksum = ssl_update_checksum_sha384; |
| 773 | else |
| 774 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 775 | #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] | 776 | if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 ) |
| 777 | ssl->handshake->update_checksum = ssl_update_checksum_sha256; |
| 778 | else |
| 779 | #endif |
| 780 | { |
| 781 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 782 | return; |
| 783 | } |
| 784 | } |
| 785 | |
XiaokangQian | adab9a6 | 2022-07-18 07:41:26 +0000 | [diff] [blame] | 786 | void mbedtls_ssl_add_hs_hdr_to_checksum( mbedtls_ssl_context *ssl, |
| 787 | unsigned hs_type, |
| 788 | size_t total_hs_len ) |
Ronald Cron | 8f6d39a | 2022-03-10 18:56:50 +0100 | [diff] [blame] | 789 | { |
| 790 | unsigned char hs_hdr[4]; |
| 791 | |
| 792 | /* Build HS header for checksum update. */ |
| 793 | hs_hdr[0] = MBEDTLS_BYTE_0( hs_type ); |
| 794 | hs_hdr[1] = MBEDTLS_BYTE_2( total_hs_len ); |
| 795 | hs_hdr[2] = MBEDTLS_BYTE_1( total_hs_len ); |
| 796 | hs_hdr[3] = MBEDTLS_BYTE_0( total_hs_len ); |
| 797 | |
| 798 | ssl->handshake->update_checksum( ssl, hs_hdr, sizeof( hs_hdr ) ); |
| 799 | } |
| 800 | |
| 801 | void mbedtls_ssl_add_hs_msg_to_checksum( mbedtls_ssl_context *ssl, |
| 802 | unsigned hs_type, |
| 803 | unsigned char const *msg, |
| 804 | size_t msg_len ) |
| 805 | { |
| 806 | mbedtls_ssl_add_hs_hdr_to_checksum( ssl, hs_type, msg_len ); |
| 807 | ssl->handshake->update_checksum( ssl, msg, msg_len ); |
| 808 | } |
| 809 | |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 810 | void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl ) |
| 811 | { |
| 812 | ((void) ssl); |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 813 | #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] | 814 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 815 | psa_hash_abort( &ssl->handshake->fin_sha256_psa ); |
| 816 | psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 ); |
| 817 | #else |
| 818 | mbedtls_sha256_starts( &ssl->handshake->fin_sha256, 0 ); |
| 819 | #endif |
| 820 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 821 | #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] | 822 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 823 | psa_hash_abort( &ssl->handshake->fin_sha384_psa ); |
| 824 | psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 ); |
| 825 | #else |
Andrzej Kurek | a242e83 | 2022-08-11 10:03:14 -0400 | [diff] [blame] | 826 | mbedtls_sha512_starts( &ssl->handshake->fin_sha384, 1 ); |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 827 | #endif |
| 828 | #endif |
| 829 | } |
| 830 | |
| 831 | static void ssl_update_checksum_start( mbedtls_ssl_context *ssl, |
| 832 | const unsigned char *buf, size_t len ) |
| 833 | { |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 834 | #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] | 835 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 836 | psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len ); |
| 837 | #else |
| 838 | mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len ); |
| 839 | #endif |
| 840 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 841 | #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] | 842 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 843 | psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len ); |
| 844 | #else |
Andrzej Kurek | a242e83 | 2022-08-11 10:03:14 -0400 | [diff] [blame] | 845 | mbedtls_sha512_update( &ssl->handshake->fin_sha384, buf, len ); |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 846 | #endif |
| 847 | #endif |
Andrzej Kurek | eabeb30 | 2022-10-17 07:52:51 -0400 | [diff] [blame] | 848 | #if !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \ |
| 849 | !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
| 850 | (void) ssl; |
| 851 | (void) buf; |
| 852 | (void) len; |
| 853 | #endif |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 854 | } |
| 855 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 856 | #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] | 857 | static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl, |
| 858 | const unsigned char *buf, size_t len ) |
| 859 | { |
| 860 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 861 | psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len ); |
| 862 | #else |
| 863 | mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len ); |
| 864 | #endif |
| 865 | } |
| 866 | #endif |
| 867 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 868 | #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] | 869 | static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl, |
| 870 | const unsigned char *buf, size_t len ) |
| 871 | { |
| 872 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 873 | psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len ); |
| 874 | #else |
Andrzej Kurek | a242e83 | 2022-08-11 10:03:14 -0400 | [diff] [blame] | 875 | mbedtls_sha512_update( &ssl->handshake->fin_sha384, buf, len ); |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 876 | #endif |
| 877 | } |
| 878 | #endif |
| 879 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 880 | static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake ) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 881 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 882 | memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 883 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 884 | #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] | 885 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 886 | handshake->fin_sha256_psa = psa_hash_operation_init(); |
| 887 | psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 ); |
| 888 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 889 | mbedtls_sha256_init( &handshake->fin_sha256 ); |
TRodziewicz | 26371e4 | 2021-06-08 16:45:41 +0200 | [diff] [blame] | 890 | mbedtls_sha256_starts( &handshake->fin_sha256, 0 ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 891 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 892 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 893 | #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] | 894 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 895 | handshake->fin_sha384_psa = psa_hash_operation_init(); |
| 896 | psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 897 | #else |
Andrzej Kurek | a242e83 | 2022-08-11 10:03:14 -0400 | [diff] [blame] | 898 | mbedtls_sha512_init( &handshake->fin_sha384 ); |
| 899 | mbedtls_sha512_starts( &handshake->fin_sha384, 1 ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 900 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 901 | #endif |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 902 | |
| 903 | handshake->update_checksum = ssl_update_checksum_start; |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 904 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 905 | #if defined(MBEDTLS_DHM_C) |
| 906 | mbedtls_dhm_init( &handshake->dhm_ctx ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 907 | #endif |
Neil Armstrong | f3f4641 | 2022-04-12 14:43:39 +0200 | [diff] [blame] | 908 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 909 | mbedtls_ecdh_init( &handshake->ecdh_ctx ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 910 | #endif |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 911 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 912 | mbedtls_ecjpake_init( &handshake->ecjpake_ctx ); |
Manuel Pégourié-Gonnard | 77c0646 | 2015-09-17 13:59:49 +0200 | [diff] [blame] | 913 | #if defined(MBEDTLS_SSL_CLI_C) |
| 914 | handshake->ecjpake_cache = NULL; |
| 915 | handshake->ecjpake_cache_len = 0; |
| 916 | #endif |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 917 | #endif |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 918 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 919 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Manuel Pégourié-Gonnard | 6b7301c | 2017-08-15 12:08:45 +0200 | [diff] [blame] | 920 | mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx ); |
Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 921 | #endif |
| 922 | |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 923 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 924 | handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET; |
| 925 | #endif |
Hanno Becker | 7517312 | 2019-02-06 16:18:31 +0000 | [diff] [blame] | 926 | |
| 927 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 928 | !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 929 | mbedtls_pk_init( &handshake->peer_pubkey ); |
| 930 | #endif |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 931 | } |
| 932 | |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 933 | void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform ) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 934 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 935 | memset( transform, 0, sizeof(mbedtls_ssl_transform) ); |
Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 936 | |
Przemyslaw Stekiel | 8f80fb9 | 2022-01-11 08:28:13 +0100 | [diff] [blame] | 937 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 938 | transform->psa_key_enc = MBEDTLS_SVC_KEY_ID_INIT; |
| 939 | transform->psa_key_dec = MBEDTLS_SVC_KEY_ID_INIT; |
Przemyslaw Stekiel | 6be9cf5 | 2022-01-19 16:00:22 +0100 | [diff] [blame] | 940 | #else |
| 941 | mbedtls_cipher_init( &transform->cipher_ctx_enc ); |
| 942 | mbedtls_cipher_init( &transform->cipher_ctx_dec ); |
Przemyslaw Stekiel | 8f80fb9 | 2022-01-11 08:28:13 +0100 | [diff] [blame] | 943 | #endif |
| 944 | |
Hanno Becker | fd86ca8 | 2020-11-30 08:54:23 +0000 | [diff] [blame] | 945 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
Neil Armstrong | 39b8e7d | 2022-02-23 09:24:45 +0100 | [diff] [blame] | 946 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 947 | transform->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT; |
| 948 | transform->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT; |
Neil Armstrong | cf8841a | 2022-02-24 11:17:45 +0100 | [diff] [blame] | 949 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 950 | mbedtls_md_init( &transform->md_ctx_enc ); |
| 951 | mbedtls_md_init( &transform->md_ctx_dec ); |
Hanno Becker | d56ed24 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 952 | #endif |
Neil Armstrong | cf8841a | 2022-02-24 11:17:45 +0100 | [diff] [blame] | 953 | #endif |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 954 | } |
| 955 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 956 | void mbedtls_ssl_session_init( mbedtls_ssl_session *session ) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 957 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 958 | memset( session, 0, sizeof(mbedtls_ssl_session) ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 959 | } |
| 960 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 961 | MBEDTLS_CHECK_RETURN_CRITICAL |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 962 | static int ssl_handshake_init( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 963 | { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 964 | /* Clear old handshake information if present */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 965 | if( ssl->transform_negotiate ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 966 | mbedtls_ssl_transform_free( ssl->transform_negotiate ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 967 | if( ssl->session_negotiate ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 968 | mbedtls_ssl_session_free( ssl->session_negotiate ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 969 | if( ssl->handshake ) |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 970 | mbedtls_ssl_handshake_free( ssl ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 971 | |
| 972 | /* |
| 973 | * Either the pointers are now NULL or cleared properly and can be freed. |
| 974 | * Now allocate missing structures. |
| 975 | */ |
| 976 | if( ssl->transform_negotiate == NULL ) |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 977 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 978 | ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 979 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 980 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 981 | if( ssl->session_negotiate == NULL ) |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 982 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 983 | ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 984 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 985 | |
Paul Bakker | 82788fb | 2014-10-20 13:59:19 +0200 | [diff] [blame] | 986 | if( ssl->handshake == NULL ) |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 987 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 988 | ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 989 | } |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 990 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 991 | /* If the buffers are too small - reallocate */ |
Andrzej Kurek | 8ea6872 | 2020-04-03 06:40:47 -0400 | [diff] [blame] | 992 | |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 993 | handle_buffer_resizing( ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN, |
| 994 | MBEDTLS_SSL_OUT_BUFFER_LEN ); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 995 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 996 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 997 | /* All pointers should exist and can be directly freed without issue */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 998 | if( ssl->handshake == NULL || |
| 999 | ssl->transform_negotiate == NULL || |
| 1000 | ssl->session_negotiate == NULL ) |
| 1001 | { |
Manuel Pégourié-Gonnard | b2a18a2 | 2015-05-27 16:29:56 +0200 | [diff] [blame] | 1002 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 1003 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1004 | mbedtls_free( ssl->handshake ); |
| 1005 | mbedtls_free( ssl->transform_negotiate ); |
| 1006 | mbedtls_free( ssl->session_negotiate ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 1007 | |
| 1008 | ssl->handshake = NULL; |
| 1009 | ssl->transform_negotiate = NULL; |
| 1010 | ssl->session_negotiate = NULL; |
| 1011 | |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 1012 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1013 | } |
| 1014 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 1015 | /* Initialize structures */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1016 | mbedtls_ssl_session_init( ssl->session_negotiate ); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1017 | mbedtls_ssl_transform_init( ssl->transform_negotiate ); |
Paul Bakker | 968afaa | 2014-07-09 11:09:24 +0200 | [diff] [blame] | 1018 | ssl_handshake_params_init( ssl->handshake ); |
| 1019 | |
Jerry Yu | d0766ec | 2022-09-22 10:46:57 +0800 | [diff] [blame] | 1020 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \ |
| 1021 | defined(MBEDTLS_SSL_SRV_C) && \ |
| 1022 | defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 1023 | ssl->handshake->new_session_tickets_count = |
| 1024 | ssl->conf->new_session_tickets_count ; |
| 1025 | #endif |
| 1026 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1027 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 1028 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 1029 | { |
| 1030 | ssl->handshake->alt_transform_out = ssl->transform_out; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 1031 | |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 1032 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 1033 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING; |
| 1034 | else |
| 1035 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING; |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 1036 | |
Hanno Becker | 0f57a65 | 2020-02-05 10:37:26 +0000 | [diff] [blame] | 1037 | mbedtls_ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 1038 | } |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 1039 | #endif |
| 1040 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 1041 | /* |
| 1042 | * curve_list is translated to IANA TLS group identifiers here because |
| 1043 | * mbedtls_ssl_conf_curves returns void and so can't return |
| 1044 | * any error codes. |
| 1045 | */ |
| 1046 | #if defined(MBEDTLS_ECP_C) |
| 1047 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 1048 | /* Heap allocate and translate curve_list from internal to IANA group ids */ |
| 1049 | if ( ssl->conf->curve_list != NULL ) |
| 1050 | { |
| 1051 | size_t length; |
| 1052 | const mbedtls_ecp_group_id *curve_list = ssl->conf->curve_list; |
| 1053 | |
| 1054 | for( length = 0; ( curve_list[length] != MBEDTLS_ECP_DP_NONE ) && |
| 1055 | ( length < MBEDTLS_ECP_DP_MAX ); length++ ) {} |
| 1056 | |
| 1057 | /* Leave room for zero termination */ |
| 1058 | uint16_t *group_list = mbedtls_calloc( length + 1, sizeof(uint16_t) ); |
| 1059 | if ( group_list == NULL ) |
| 1060 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 1061 | |
| 1062 | for( size_t i = 0; i < length; i++ ) |
| 1063 | { |
| 1064 | const mbedtls_ecp_curve_info *info = |
| 1065 | mbedtls_ecp_curve_info_from_grp_id( curve_list[i] ); |
| 1066 | if ( info == NULL ) |
| 1067 | { |
| 1068 | mbedtls_free( group_list ); |
| 1069 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); |
| 1070 | } |
| 1071 | group_list[i] = info->tls_id; |
| 1072 | } |
| 1073 | |
| 1074 | group_list[length] = 0; |
| 1075 | |
| 1076 | ssl->handshake->group_list = group_list; |
| 1077 | ssl->handshake->group_list_heap_allocated = 1; |
| 1078 | } |
| 1079 | else |
| 1080 | { |
| 1081 | ssl->handshake->group_list = ssl->conf->group_list; |
| 1082 | ssl->handshake->group_list_heap_allocated = 0; |
| 1083 | } |
| 1084 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
| 1085 | #endif /* MBEDTLS_ECP_C */ |
| 1086 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 1087 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1088 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Jerry Yu | a69269a | 2022-01-17 21:06:01 +0800 | [diff] [blame] | 1089 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Jerry Yu | 713013f | 2022-01-17 18:16:35 +0800 | [diff] [blame] | 1090 | /* Heap allocate and translate sig_hashes from internal hash identifiers to |
| 1091 | signature algorithms IANA identifiers. */ |
| 1092 | if ( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) && |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1093 | ssl->conf->sig_hashes != NULL ) |
| 1094 | { |
| 1095 | const int *md; |
| 1096 | const int *sig_hashes = ssl->conf->sig_hashes; |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 1097 | size_t sig_algs_len = 0; |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1098 | uint16_t *p; |
| 1099 | |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 1100 | #if defined(static_assert) |
| 1101 | static_assert( MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN |
| 1102 | <= ( SIZE_MAX - ( 2 * sizeof(uint16_t) ) ), |
| 1103 | "MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN too big" ); |
Jerry Yu | a68dca2 | 2022-01-20 16:28:27 +0800 | [diff] [blame] | 1104 | #endif |
| 1105 | |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1106 | for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ ) |
| 1107 | { |
| 1108 | if( mbedtls_ssl_hash_from_md_alg( *md ) == MBEDTLS_SSL_HASH_NONE ) |
| 1109 | continue; |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 1110 | #if defined(MBEDTLS_ECDSA_C) |
| 1111 | sig_algs_len += sizeof( uint16_t ); |
| 1112 | #endif |
Jerry Yu | a68dca2 | 2022-01-20 16:28:27 +0800 | [diff] [blame] | 1113 | |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 1114 | #if defined(MBEDTLS_RSA_C) |
| 1115 | sig_algs_len += sizeof( uint16_t ); |
| 1116 | #endif |
| 1117 | if( sig_algs_len > MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN ) |
| 1118 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1119 | } |
| 1120 | |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 1121 | if( sig_algs_len < MBEDTLS_SSL_MIN_SIG_ALG_LIST_LEN ) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1122 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); |
| 1123 | |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 1124 | ssl->handshake->sig_algs = mbedtls_calloc( 1, sig_algs_len + |
| 1125 | sizeof( uint16_t )); |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1126 | if( ssl->handshake->sig_algs == NULL ) |
| 1127 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 1128 | |
| 1129 | p = (uint16_t *)ssl->handshake->sig_algs; |
| 1130 | for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ ) |
| 1131 | { |
| 1132 | unsigned char hash = mbedtls_ssl_hash_from_md_alg( *md ); |
| 1133 | if( hash == MBEDTLS_SSL_HASH_NONE ) |
| 1134 | continue; |
Jerry Yu | 6106fdc | 2022-01-12 16:36:14 +0800 | [diff] [blame] | 1135 | #if defined(MBEDTLS_ECDSA_C) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1136 | *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_ECDSA); |
| 1137 | p++; |
Jerry Yu | 6106fdc | 2022-01-12 16:36:14 +0800 | [diff] [blame] | 1138 | #endif |
| 1139 | #if defined(MBEDTLS_RSA_C) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1140 | *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_RSA); |
| 1141 | p++; |
Jerry Yu | 6106fdc | 2022-01-12 16:36:14 +0800 | [diff] [blame] | 1142 | #endif |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1143 | } |
Gabor Mezei | 15b95a6 | 2022-05-09 16:37:58 +0200 | [diff] [blame] | 1144 | *p = MBEDTLS_TLS_SIG_NONE; |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1145 | ssl->handshake->sig_algs_heap_allocated = 1; |
| 1146 | } |
| 1147 | else |
Jerry Yu | a69269a | 2022-01-17 21:06:01 +0800 | [diff] [blame] | 1148 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1149 | { |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1150 | ssl->handshake->sig_algs_heap_allocated = 0; |
| 1151 | } |
Jerry Yu | cc53910 | 2022-06-27 16:27:35 +0800 | [diff] [blame] | 1152 | #endif /* !MBEDTLS_DEPRECATED_REMOVED */ |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 1153 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1154 | return( 0 ); |
| 1155 | } |
| 1156 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 1157 | #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] | 1158 | /* Dummy cookie callbacks for defaults */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 1159 | MBEDTLS_CHECK_RETURN_CRITICAL |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 1160 | static int ssl_cookie_write_dummy( void *ctx, |
| 1161 | unsigned char **p, unsigned char *end, |
| 1162 | const unsigned char *cli_id, size_t cli_id_len ) |
| 1163 | { |
| 1164 | ((void) ctx); |
| 1165 | ((void) p); |
| 1166 | ((void) end); |
| 1167 | ((void) cli_id); |
| 1168 | ((void) cli_id_len); |
| 1169 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1170 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 1171 | } |
| 1172 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 1173 | MBEDTLS_CHECK_RETURN_CRITICAL |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 1174 | static int ssl_cookie_check_dummy( void *ctx, |
| 1175 | const unsigned char *cookie, size_t cookie_len, |
| 1176 | const unsigned char *cli_id, size_t cli_id_len ) |
| 1177 | { |
| 1178 | ((void) ctx); |
| 1179 | ((void) cookie); |
| 1180 | ((void) cookie_len); |
| 1181 | ((void) cli_id); |
| 1182 | ((void) cli_id_len); |
| 1183 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1184 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 1185 | } |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 1186 | #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 1187 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1188 | /* |
| 1189 | * Initialize an SSL context |
| 1190 | */ |
Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 1191 | void mbedtls_ssl_init( mbedtls_ssl_context *ssl ) |
| 1192 | { |
| 1193 | memset( ssl, 0, sizeof( mbedtls_ssl_context ) ); |
| 1194 | } |
| 1195 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 1196 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1197 | static int ssl_conf_version_check( const mbedtls_ssl_context *ssl ) |
| 1198 | { |
Ronald Cron | 086ee0b | 2022-03-15 15:18:51 +0100 | [diff] [blame] | 1199 | const mbedtls_ssl_config *conf = ssl->conf; |
| 1200 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 1201 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Ronald Cron | 086ee0b | 2022-03-15 15:18:51 +0100 | [diff] [blame] | 1202 | if( mbedtls_ssl_conf_is_tls13_only( conf ) ) |
| 1203 | { |
XiaokangQian | ed582dd | 2022-04-13 08:21:05 +0000 | [diff] [blame] | 1204 | if( conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 1205 | { |
| 1206 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS 1.3 is not yet supported." ) ); |
| 1207 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 1208 | } |
| 1209 | |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1210 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls13 only." ) ); |
| 1211 | return( 0 ); |
| 1212 | } |
| 1213 | #endif |
| 1214 | |
| 1215 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Ronald Cron | 086ee0b | 2022-03-15 15:18:51 +0100 | [diff] [blame] | 1216 | if( mbedtls_ssl_conf_is_tls12_only( conf ) ) |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1217 | { |
| 1218 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls12 only." ) ); |
| 1219 | return( 0 ); |
| 1220 | } |
| 1221 | #endif |
| 1222 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 1223 | #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] | 1224 | if( mbedtls_ssl_conf_is_hybrid_tls12_tls13( conf ) ) |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1225 | { |
XiaokangQian | ed582dd | 2022-04-13 08:21:05 +0000 | [diff] [blame] | 1226 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 1227 | { |
| 1228 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS not yet supported in Hybrid TLS 1.3 + TLS 1.2" ) ); |
| 1229 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 1230 | } |
| 1231 | |
XiaokangQian | 8f9dfe4 | 2022-04-15 02:52:39 +0000 | [diff] [blame] | 1232 | if( conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 1233 | { |
| 1234 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS 1.3 server is not supported yet." ) ); |
| 1235 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 1236 | } |
| 1237 | |
| 1238 | |
Ronald Cron | e1d3f06 | 2022-02-10 14:50:54 +0100 | [diff] [blame] | 1239 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is TLS 1.3 or TLS 1.2." ) ); |
| 1240 | return( 0 ); |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1241 | } |
| 1242 | #endif |
| 1243 | |
| 1244 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "The SSL configuration is invalid." ) ); |
| 1245 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); |
| 1246 | } |
| 1247 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 1248 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1249 | static int ssl_conf_check(const mbedtls_ssl_context *ssl) |
| 1250 | { |
| 1251 | int ret; |
| 1252 | ret = ssl_conf_version_check( ssl ); |
| 1253 | if( ret != 0 ) |
| 1254 | return( ret ); |
| 1255 | |
| 1256 | /* Space for further checks */ |
| 1257 | |
| 1258 | return( 0 ); |
| 1259 | } |
| 1260 | |
Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 1261 | /* |
| 1262 | * Setup an SSL context |
| 1263 | */ |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 1264 | |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 1265 | int mbedtls_ssl_setup( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 1897af9 | 2015-05-10 23:27:38 +0200 | [diff] [blame] | 1266 | const mbedtls_ssl_config *conf ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1267 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1268 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1269 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; |
| 1270 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1271 | |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 1272 | ssl->conf = conf; |
Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 1273 | |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1274 | if( ( ret = ssl_conf_check( ssl ) ) != 0 ) |
| 1275 | return( ret ); |
| 1276 | |
Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 1277 | /* |
Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 1278 | * Prepare base structures |
Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 1279 | */ |
k-stachowiak | c9a5f02 | 2018-07-24 13:53:31 +0200 | [diff] [blame] | 1280 | |
| 1281 | /* Set to NULL in case of an error condition */ |
| 1282 | ssl->out_buf = NULL; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1283 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1284 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 1285 | ssl->in_buf_len = in_buf_len; |
| 1286 | #endif |
| 1287 | ssl->in_buf = mbedtls_calloc( 1, in_buf_len ); |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 1288 | if( ssl->in_buf == NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1289 | { |
Paul Elliott | d48d5c6 | 2021-01-07 14:47:05 +0000 | [diff] [blame] | 1290 | 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] | 1291 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1292 | goto error; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 1293 | } |
| 1294 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1295 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 1296 | ssl->out_buf_len = out_buf_len; |
| 1297 | #endif |
| 1298 | ssl->out_buf = mbedtls_calloc( 1, out_buf_len ); |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 1299 | if( ssl->out_buf == NULL ) |
| 1300 | { |
Paul Elliott | d48d5c6 | 2021-01-07 14:47:05 +0000 | [diff] [blame] | 1301 | 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] | 1302 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1303 | goto error; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1304 | } |
| 1305 | |
Hanno Becker | 3e6f8ab | 2020-02-05 10:40:57 +0000 | [diff] [blame] | 1306 | mbedtls_ssl_reset_in_out_pointers( ssl ); |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 1307 | |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 1308 | #if defined(MBEDTLS_SSL_DTLS_SRTP) |
Ron Eldor | 3adb992 | 2017-12-21 10:15:08 +0200 | [diff] [blame] | 1309 | memset( &ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info) ); |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 1310 | #endif |
| 1311 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1312 | if( ( ret = ssl_handshake_init( ssl ) ) != 0 ) |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1313 | goto error; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1314 | |
| 1315 | return( 0 ); |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1316 | |
| 1317 | error: |
| 1318 | mbedtls_free( ssl->in_buf ); |
| 1319 | mbedtls_free( ssl->out_buf ); |
| 1320 | |
| 1321 | ssl->conf = NULL; |
| 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 = 0; |
| 1325 | ssl->out_buf_len = 0; |
| 1326 | #endif |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1327 | ssl->in_buf = NULL; |
| 1328 | ssl->out_buf = NULL; |
| 1329 | |
| 1330 | ssl->in_hdr = NULL; |
| 1331 | ssl->in_ctr = NULL; |
| 1332 | ssl->in_len = NULL; |
| 1333 | ssl->in_iv = NULL; |
| 1334 | ssl->in_msg = NULL; |
| 1335 | |
| 1336 | ssl->out_hdr = NULL; |
| 1337 | ssl->out_ctr = NULL; |
| 1338 | ssl->out_len = NULL; |
| 1339 | ssl->out_iv = NULL; |
| 1340 | ssl->out_msg = NULL; |
| 1341 | |
k-stachowiak | 9f7798e | 2018-07-31 16:52:32 +0200 | [diff] [blame] | 1342 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1343 | } |
| 1344 | |
| 1345 | /* |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 1346 | * Reset an initialized and used SSL context for re-use while retaining |
| 1347 | * all application-set variables, function pointers and data. |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 1348 | * |
| 1349 | * If partial is non-zero, keep data in the input buffer and client ID. |
| 1350 | * (Use when a DTLS client reconnects from the same port.) |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 1351 | */ |
XiaokangQian | 78b1fa7 | 2022-01-19 06:56:30 +0000 | [diff] [blame] | 1352 | void mbedtls_ssl_session_reset_msg_layer( mbedtls_ssl_context *ssl, |
| 1353 | int partial ) |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 1354 | { |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1355 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 1356 | size_t in_buf_len = ssl->in_buf_len; |
| 1357 | size_t out_buf_len = ssl->out_buf_len; |
| 1358 | #else |
| 1359 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; |
| 1360 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; |
| 1361 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1362 | |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1363 | #if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || !defined(MBEDTLS_SSL_SRV_C) |
| 1364 | partial = 0; |
Hanno Becker | 7e77213 | 2018-08-10 12:38:21 +0100 | [diff] [blame] | 1365 | #endif |
| 1366 | |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 1367 | /* Cancel any possibly running timer */ |
Hanno Becker | 0f57a65 | 2020-02-05 10:37:26 +0000 | [diff] [blame] | 1368 | mbedtls_ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 1369 | |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1370 | mbedtls_ssl_reset_in_out_pointers( ssl ); |
| 1371 | |
| 1372 | /* Reset incoming message parsing */ |
| 1373 | ssl->in_offt = NULL; |
| 1374 | ssl->nb_zero = 0; |
| 1375 | ssl->in_msgtype = 0; |
| 1376 | ssl->in_msglen = 0; |
| 1377 | ssl->in_hslen = 0; |
| 1378 | ssl->keep_current_message = 0; |
| 1379 | ssl->transform_in = NULL; |
| 1380 | |
| 1381 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 1382 | ssl->next_record_offset = 0; |
| 1383 | ssl->in_epoch = 0; |
| 1384 | #endif |
| 1385 | |
| 1386 | /* Keep current datagram if partial == 1 */ |
| 1387 | if( partial == 0 ) |
| 1388 | { |
| 1389 | ssl->in_left = 0; |
| 1390 | memset( ssl->in_buf, 0, in_buf_len ); |
| 1391 | } |
| 1392 | |
Ronald Cron | ad8c17b | 2022-06-10 17:18:09 +0200 | [diff] [blame] | 1393 | ssl->send_alert = 0; |
| 1394 | |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1395 | /* Reset outgoing message writing */ |
| 1396 | ssl->out_msgtype = 0; |
| 1397 | ssl->out_msglen = 0; |
| 1398 | ssl->out_left = 0; |
| 1399 | memset( ssl->out_buf, 0, out_buf_len ); |
| 1400 | memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) ); |
| 1401 | ssl->transform_out = NULL; |
| 1402 | |
| 1403 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 1404 | mbedtls_ssl_dtls_replay_reset( ssl ); |
| 1405 | #endif |
| 1406 | |
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1407 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1408 | if( ssl->transform ) |
| 1409 | { |
| 1410 | mbedtls_ssl_transform_free( ssl->transform ); |
| 1411 | mbedtls_free( ssl->transform ); |
| 1412 | ssl->transform = NULL; |
| 1413 | } |
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1414 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 1415 | |
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1416 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 1417 | mbedtls_ssl_transform_free( ssl->transform_application ); |
| 1418 | mbedtls_free( ssl->transform_application ); |
| 1419 | ssl->transform_application = NULL; |
| 1420 | |
| 1421 | if( ssl->handshake != NULL ) |
| 1422 | { |
| 1423 | mbedtls_ssl_transform_free( ssl->handshake->transform_earlydata ); |
| 1424 | mbedtls_free( ssl->handshake->transform_earlydata ); |
| 1425 | ssl->handshake->transform_earlydata = NULL; |
| 1426 | |
| 1427 | mbedtls_ssl_transform_free( ssl->handshake->transform_handshake ); |
| 1428 | mbedtls_free( ssl->handshake->transform_handshake ); |
| 1429 | ssl->handshake->transform_handshake = NULL; |
| 1430 | } |
| 1431 | |
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1432 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1433 | } |
| 1434 | |
| 1435 | int mbedtls_ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial ) |
| 1436 | { |
| 1437 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 1438 | |
| 1439 | ssl->state = MBEDTLS_SSL_HELLO_REQUEST; |
| 1440 | |
XiaokangQian | 78b1fa7 | 2022-01-19 06:56:30 +0000 | [diff] [blame] | 1441 | mbedtls_ssl_session_reset_msg_layer( ssl, partial ); |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1442 | |
| 1443 | /* Reset renegotiation state */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1444 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 1445 | ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE; |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 1446 | ssl->renego_records_seen = 0; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1447 | |
| 1448 | ssl->verify_data_len = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1449 | memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN ); |
| 1450 | 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] | 1451 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1452 | ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1453 | |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1454 | ssl->session_in = NULL; |
Hanno Becker | 7864090 | 2018-08-13 16:35:15 +0100 | [diff] [blame] | 1455 | ssl->session_out = NULL; |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 1456 | if( ssl->session ) |
| 1457 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1458 | mbedtls_ssl_session_free( ssl->session ); |
| 1459 | mbedtls_free( ssl->session ); |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 1460 | ssl->session = NULL; |
| 1461 | } |
| 1462 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1463 | #if defined(MBEDTLS_SSL_ALPN) |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 1464 | ssl->alpn_chosen = NULL; |
| 1465 | #endif |
| 1466 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 1467 | #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C) |
David Horstmann | 3b2276a | 2022-10-06 14:49:08 +0100 | [diff] [blame] | 1468 | int free_cli_id = 1; |
Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 1469 | #if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) |
David Horstmann | 3b2276a | 2022-10-06 14:49:08 +0100 | [diff] [blame] | 1470 | free_cli_id = ( partial == 0 ); |
Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 1471 | #endif |
David Horstmann | 3b2276a | 2022-10-06 14:49:08 +0100 | [diff] [blame] | 1472 | if( free_cli_id ) |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 1473 | { |
| 1474 | mbedtls_free( ssl->cli_id ); |
| 1475 | ssl->cli_id = NULL; |
| 1476 | ssl->cli_id_len = 0; |
| 1477 | } |
Manuel Pégourié-Gonnard | 43c0218 | 2014-07-22 17:32:01 +0200 | [diff] [blame] | 1478 | #endif |
| 1479 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1480 | if( ( ret = ssl_handshake_init( ssl ) ) != 0 ) |
| 1481 | return( ret ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1482 | |
| 1483 | return( 0 ); |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 1484 | } |
| 1485 | |
Manuel Pégourié-Gonnard | 779e429 | 2013-08-03 13:50:48 +0200 | [diff] [blame] | 1486 | /* |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 1487 | * Reset an initialized and used SSL context for re-use while retaining |
| 1488 | * all application-set variables, function pointers and data. |
| 1489 | */ |
| 1490 | int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl ) |
| 1491 | { |
Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 1492 | return( mbedtls_ssl_session_reset_int( ssl, 0 ) ); |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 1493 | } |
| 1494 | |
| 1495 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1496 | * SSL set accessors |
| 1497 | */ |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1498 | void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1499 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1500 | conf->endpoint = endpoint; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1501 | } |
| 1502 | |
Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 1503 | 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] | 1504 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1505 | conf->transport = transport; |
Manuel Pégourié-Gonnard | 0b1ff29 | 2014-02-06 13:04:16 +0100 | [diff] [blame] | 1506 | } |
| 1507 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1508 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1509 | 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] | 1510 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1511 | conf->anti_replay = mode; |
Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 1512 | } |
| 1513 | #endif |
| 1514 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1515 | 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] | 1516 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1517 | conf->badmac_limit = limit; |
Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 1518 | } |
Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 1519 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1520 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 1521 | |
Hanno Becker | 1841b0a | 2018-08-24 11:13:57 +0100 | [diff] [blame] | 1522 | void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl, |
| 1523 | unsigned allow_packing ) |
Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 1524 | { |
| 1525 | ssl->disable_datagram_packing = !allow_packing; |
| 1526 | } |
| 1527 | |
| 1528 | void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf, |
| 1529 | uint32_t min, uint32_t max ) |
Manuel Pégourié-Gonnard | 905dd24 | 2014-10-01 12:03:55 +0200 | [diff] [blame] | 1530 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1531 | conf->hs_timeout_min = min; |
| 1532 | conf->hs_timeout_max = max; |
Manuel Pégourié-Gonnard | 905dd24 | 2014-10-01 12:03:55 +0200 | [diff] [blame] | 1533 | } |
| 1534 | #endif |
| 1535 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1536 | void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1537 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1538 | conf->authmode = authmode; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1539 | } |
| 1540 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1541 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1542 | void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 1543 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 1544 | void *p_vrfy ) |
| 1545 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1546 | conf->f_vrfy = f_vrfy; |
| 1547 | conf->p_vrfy = p_vrfy; |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 1548 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1549 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 1550 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1551 | void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf, |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 1552 | int (*f_rng)(void *, unsigned char *, size_t), |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1553 | void *p_rng ) |
| 1554 | { |
Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 1555 | conf->f_rng = f_rng; |
| 1556 | conf->p_rng = p_rng; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1557 | } |
| 1558 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1559 | void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | fd47423 | 2015-06-23 16:34:24 +0200 | [diff] [blame] | 1560 | void (*f_dbg)(void *, int, const char *, int, const char *), |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1561 | void *p_dbg ) |
| 1562 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1563 | conf->f_dbg = f_dbg; |
| 1564 | conf->p_dbg = p_dbg; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1565 | } |
| 1566 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1567 | void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 1568 | void *p_bio, |
Simon Butcher | e846b51 | 2016-03-01 17:31:49 +0000 | [diff] [blame] | 1569 | mbedtls_ssl_send_t *f_send, |
| 1570 | mbedtls_ssl_recv_t *f_recv, |
| 1571 | mbedtls_ssl_recv_timeout_t *f_recv_timeout ) |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 1572 | { |
| 1573 | ssl->p_bio = p_bio; |
| 1574 | ssl->f_send = f_send; |
| 1575 | ssl->f_recv = f_recv; |
| 1576 | ssl->f_recv_timeout = f_recv_timeout; |
Manuel Pégourié-Gonnard | 97fd52c | 2015-05-06 15:38:52 +0100 | [diff] [blame] | 1577 | } |
| 1578 | |
Manuel Pégourié-Gonnard | 6e7aaca | 2018-08-20 10:37:23 +0200 | [diff] [blame] | 1579 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 1580 | void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu ) |
| 1581 | { |
| 1582 | ssl->mtu = mtu; |
| 1583 | } |
| 1584 | #endif |
| 1585 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1586 | 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] | 1587 | { |
| 1588 | conf->read_timeout = timeout; |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 1589 | } |
| 1590 | |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 1591 | void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl, |
| 1592 | void *p_timer, |
Simon Butcher | e846b51 | 2016-03-01 17:31:49 +0000 | [diff] [blame] | 1593 | mbedtls_ssl_set_timer_t *f_set_timer, |
| 1594 | mbedtls_ssl_get_timer_t *f_get_timer ) |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 1595 | { |
| 1596 | ssl->p_timer = p_timer; |
| 1597 | ssl->f_set_timer = f_set_timer; |
| 1598 | ssl->f_get_timer = f_get_timer; |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 1599 | |
| 1600 | /* Make sure we start with no timer running */ |
Hanno Becker | 0f57a65 | 2020-02-05 10:37:26 +0000 | [diff] [blame] | 1601 | mbedtls_ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 1602 | } |
| 1603 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1604 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1605 | void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf, |
Hanno Becker | a637ff6 | 2021-04-15 08:42:48 +0100 | [diff] [blame] | 1606 | void *p_cache, |
| 1607 | mbedtls_ssl_cache_get_t *f_get_cache, |
| 1608 | mbedtls_ssl_cache_set_t *f_set_cache ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1609 | { |
Manuel Pégourié-Gonnard | 5cb3308 | 2015-05-06 18:06:26 +0100 | [diff] [blame] | 1610 | conf->p_cache = p_cache; |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1611 | conf->f_get_cache = f_get_cache; |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1612 | conf->f_set_cache = f_set_cache; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1613 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1614 | #endif /* MBEDTLS_SSL_SRV_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1615 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1616 | #if defined(MBEDTLS_SSL_CLI_C) |
| 1617 | 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] | 1618 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1619 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1620 | |
| 1621 | if( ssl == NULL || |
| 1622 | session == NULL || |
| 1623 | ssl->session_negotiate == NULL || |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1624 | ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT ) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1625 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1626 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1627 | } |
| 1628 | |
Hanno Becker | e810bbc | 2021-05-14 16:01:05 +0100 | [diff] [blame] | 1629 | if( ssl->handshake->resume == 1 ) |
| 1630 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 1631 | |
Jerry Yu | 2109206 | 2022-10-10 21:21:31 +0800 | [diff] [blame] | 1632 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 1633 | if( session->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 ) |
Jerry Yu | 40afab6 | 2022-10-08 10:42:13 +0800 | [diff] [blame] | 1634 | { |
Jerry Yu | 2109206 | 2022-10-10 21:21:31 +0800 | [diff] [blame] | 1635 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 1636 | mbedtls_ssl_ciphersuite_from_id( session->ciphersuite ); |
| 1637 | |
| 1638 | if( mbedtls_ssl_validate_ciphersuite( |
| 1639 | ssl, ciphersuite_info, MBEDTLS_SSL_VERSION_TLS1_3, |
| 1640 | MBEDTLS_SSL_VERSION_TLS1_3 ) != 0 ) |
| 1641 | { |
| 1642 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "%d is not a valid TLS 1.3 ciphersuite.", |
| 1643 | session->ciphersuite ) ); |
| 1644 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1645 | } |
Jerry Yu | 40afab6 | 2022-10-08 10:42:13 +0800 | [diff] [blame] | 1646 | } |
Jerry Yu | 2109206 | 2022-10-10 21:21:31 +0800 | [diff] [blame] | 1647 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Jerry Yu | 40afab6 | 2022-10-08 10:42:13 +0800 | [diff] [blame] | 1648 | |
Hanno Becker | 52055ae | 2019-02-06 14:30:46 +0000 | [diff] [blame] | 1649 | if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate, |
| 1650 | session ) ) != 0 ) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1651 | return( ret ); |
| 1652 | |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 1653 | ssl->handshake->resume = 1; |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1654 | |
| 1655 | return( 0 ); |
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 | #endif /* MBEDTLS_SSL_CLI_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1658 | |
Mateusz Starzyk | 06b07fb | 2021-02-18 13:55:21 +0100 | [diff] [blame] | 1659 | void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf, |
| 1660 | const int *ciphersuites ) |
| 1661 | { |
Hanno Becker | d60b6c6 | 2021-04-29 12:04:11 +0100 | [diff] [blame] | 1662 | conf->ciphersuite_list = ciphersuites; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1663 | } |
| 1664 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 1665 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | cadebe5 | 2021-08-24 10:36:45 +0800 | [diff] [blame] | 1666 | void mbedtls_ssl_conf_tls13_key_exchange_modes( mbedtls_ssl_config *conf, |
Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 1667 | const int kex_modes ) |
| 1668 | { |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 1669 | 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] | 1670 | } |
Xiaokang Qian | 72de95d | 2022-10-25 02:54:33 +0000 | [diff] [blame] | 1671 | |
| 1672 | #if defined(MBEDTLS_SSL_EARLY_DATA) |
| 1673 | void mbedtls_ssl_tls13_conf_early_data( mbedtls_ssl_config *conf, |
Xiaokang Qian | 72dbfef | 2022-10-26 06:33:57 +0000 | [diff] [blame] | 1674 | int early_data_enabled ) |
Xiaokang Qian | 72de95d | 2022-10-25 02:54:33 +0000 | [diff] [blame] | 1675 | { |
| 1676 | conf->early_data_enabled = early_data_enabled; |
| 1677 | } |
| 1678 | #endif /* MBEDTLS_SSL_EARLY_DATA */ |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 1679 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 1680 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1681 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 6e3ee3a | 2015-06-17 10:58:20 +0200 | [diff] [blame] | 1682 | void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf, |
Nicholas Wilson | 2088e2e | 2015-09-08 16:53:18 +0100 | [diff] [blame] | 1683 | const mbedtls_x509_crt_profile *profile ) |
Manuel Pégourié-Gonnard | 6e3ee3a | 2015-06-17 10:58:20 +0200 | [diff] [blame] | 1684 | { |
| 1685 | conf->cert_profile = profile; |
| 1686 | } |
| 1687 | |
Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 1688 | static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert ) |
| 1689 | { |
| 1690 | mbedtls_ssl_key_cert *cur = key_cert, *next; |
| 1691 | |
| 1692 | while( cur != NULL ) |
| 1693 | { |
| 1694 | next = cur->next; |
| 1695 | mbedtls_free( cur ); |
| 1696 | cur = next; |
| 1697 | } |
| 1698 | } |
| 1699 | |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1700 | /* Append a new keycert entry to a (possibly empty) list */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 1701 | MBEDTLS_CHECK_RETURN_CRITICAL |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1702 | static int ssl_append_key_cert( mbedtls_ssl_key_cert **head, |
| 1703 | mbedtls_x509_crt *cert, |
| 1704 | mbedtls_pk_context *key ) |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1705 | { |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1706 | mbedtls_ssl_key_cert *new_cert; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1707 | |
Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 1708 | if( cert == NULL ) |
| 1709 | { |
| 1710 | /* Free list if cert is null */ |
| 1711 | ssl_key_cert_free( *head ); |
| 1712 | *head = NULL; |
| 1713 | return( 0 ); |
| 1714 | } |
| 1715 | |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1716 | new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) ); |
| 1717 | if( new_cert == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 1718 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1719 | |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1720 | new_cert->cert = cert; |
| 1721 | new_cert->key = key; |
| 1722 | new_cert->next = NULL; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1723 | |
Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 1724 | /* 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] | 1725 | if( *head == NULL ) |
Paul Bakker | 0333b97 | 2013-11-04 17:08:28 +0100 | [diff] [blame] | 1726 | { |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1727 | *head = new_cert; |
Paul Bakker | 0333b97 | 2013-11-04 17:08:28 +0100 | [diff] [blame] | 1728 | } |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1729 | else |
| 1730 | { |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1731 | mbedtls_ssl_key_cert *cur = *head; |
| 1732 | while( cur->next != NULL ) |
| 1733 | cur = cur->next; |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1734 | cur->next = new_cert; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1735 | } |
| 1736 | |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1737 | return( 0 ); |
| 1738 | } |
| 1739 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1740 | int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1741 | mbedtls_x509_crt *own_cert, |
| 1742 | mbedtls_pk_context *pk_key ) |
| 1743 | { |
Manuel Pégourié-Gonnard | 17a40cd | 2015-05-10 23:17:17 +0200 | [diff] [blame] | 1744 | 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] | 1745 | } |
| 1746 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1747 | void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 1748 | mbedtls_x509_crt *ca_chain, |
| 1749 | mbedtls_x509_crl *ca_crl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1750 | { |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 1751 | conf->ca_chain = ca_chain; |
| 1752 | conf->ca_crl = ca_crl; |
Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 1753 | |
| 1754 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 1755 | /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb() |
| 1756 | * cannot be used together. */ |
| 1757 | conf->f_ca_cb = NULL; |
| 1758 | conf->p_ca_cb = NULL; |
| 1759 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1760 | } |
Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 1761 | |
| 1762 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 1763 | void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf, |
Hanno Becker | afd0b0a | 2019-03-27 16:55:01 +0000 | [diff] [blame] | 1764 | mbedtls_x509_crt_ca_cb_t f_ca_cb, |
Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 1765 | void *p_ca_cb ) |
| 1766 | { |
| 1767 | conf->f_ca_cb = f_ca_cb; |
| 1768 | conf->p_ca_cb = p_ca_cb; |
| 1769 | |
| 1770 | /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb() |
| 1771 | * cannot be used together. */ |
| 1772 | conf->ca_chain = NULL; |
| 1773 | conf->ca_crl = NULL; |
| 1774 | } |
| 1775 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1776 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | eb2c658 | 2012-09-27 19:15:01 +0000 | [diff] [blame] | 1777 | |
Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 1778 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Glenn Strauss | 6989407 | 2022-01-24 12:58:00 -0500 | [diff] [blame] | 1779 | const unsigned char *mbedtls_ssl_get_hs_sni( mbedtls_ssl_context *ssl, |
| 1780 | size_t *name_len ) |
| 1781 | { |
| 1782 | *name_len = ssl->handshake->sni_name_len; |
| 1783 | return( ssl->handshake->sni_name ); |
| 1784 | } |
| 1785 | |
Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 1786 | int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl, |
| 1787 | mbedtls_x509_crt *own_cert, |
| 1788 | mbedtls_pk_context *pk_key ) |
| 1789 | { |
| 1790 | return( ssl_append_key_cert( &ssl->handshake->sni_key_cert, |
| 1791 | own_cert, pk_key ) ); |
| 1792 | } |
Manuel Pégourié-Gonnard | 22bfa4b | 2015-05-11 08:46:37 +0200 | [diff] [blame] | 1793 | |
| 1794 | void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl, |
| 1795 | mbedtls_x509_crt *ca_chain, |
| 1796 | mbedtls_x509_crl *ca_crl ) |
| 1797 | { |
| 1798 | ssl->handshake->sni_ca_chain = ca_chain; |
| 1799 | ssl->handshake->sni_ca_crl = ca_crl; |
| 1800 | } |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 1801 | |
Glenn Strauss | 999ef70 | 2022-03-11 01:37:23 -0500 | [diff] [blame] | 1802 | #if defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED) |
| 1803 | void mbedtls_ssl_set_hs_dn_hints( mbedtls_ssl_context *ssl, |
| 1804 | const mbedtls_x509_crt *crt) |
| 1805 | { |
| 1806 | ssl->handshake->dn_hints = crt; |
| 1807 | } |
| 1808 | #endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */ |
| 1809 | |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 1810 | void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl, |
| 1811 | int authmode ) |
| 1812 | { |
| 1813 | ssl->handshake->sni_authmode = authmode; |
| 1814 | } |
Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 1815 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
| 1816 | |
Hanno Becker | 8927c83 | 2019-04-03 12:52:50 +0100 | [diff] [blame] | 1817 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 1818 | void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl, |
| 1819 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), |
| 1820 | void *p_vrfy ) |
| 1821 | { |
| 1822 | ssl->f_vrfy = f_vrfy; |
| 1823 | ssl->p_vrfy = p_vrfy; |
| 1824 | } |
| 1825 | #endif |
| 1826 | |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 1827 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 1828 | /* |
| 1829 | * Set EC J-PAKE password for current handshake |
| 1830 | */ |
| 1831 | int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl, |
| 1832 | const unsigned char *pw, |
| 1833 | size_t pw_len ) |
| 1834 | { |
| 1835 | mbedtls_ecjpake_role role; |
| 1836 | |
Janos Follath | 8eb6413 | 2016-06-03 15:40:57 +0100 | [diff] [blame] | 1837 | if( ssl->handshake == NULL || ssl->conf == NULL ) |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 1838 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1839 | |
| 1840 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 1841 | role = MBEDTLS_ECJPAKE_SERVER; |
| 1842 | else |
| 1843 | role = MBEDTLS_ECJPAKE_CLIENT; |
| 1844 | |
| 1845 | return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx, |
| 1846 | role, |
| 1847 | MBEDTLS_MD_SHA256, |
| 1848 | MBEDTLS_ECP_DP_SECP256R1, |
| 1849 | pw, pw_len ) ); |
| 1850 | } |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 1851 | #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 1852 | |
Ronald Cron | 73fe8df | 2022-10-05 14:31:43 +0200 | [diff] [blame] | 1853 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED) |
Ronald Cron | d29e13e | 2022-10-19 10:33:48 +0200 | [diff] [blame] | 1854 | int mbedtls_ssl_conf_has_static_psk( mbedtls_ssl_config const *conf ) |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 1855 | { |
Ronald Cron | d29e13e | 2022-10-19 10:33:48 +0200 | [diff] [blame] | 1856 | if( conf->psk_identity == NULL || |
| 1857 | conf->psk_identity_len == 0 ) |
| 1858 | { |
| 1859 | return( 0 ); |
| 1860 | } |
| 1861 | |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 1862 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Ronald Cron | d29e13e | 2022-10-19 10:33:48 +0200 | [diff] [blame] | 1863 | if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) ) |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 1864 | return( 1 ); |
| 1865 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Ronald Cron | d29e13e | 2022-10-19 10:33:48 +0200 | [diff] [blame] | 1866 | |
| 1867 | if( conf->psk != NULL && conf->psk_len != 0 ) |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 1868 | return( 1 ); |
| 1869 | |
| 1870 | return( 0 ); |
| 1871 | } |
| 1872 | |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1873 | static void ssl_conf_remove_psk( mbedtls_ssl_config *conf ) |
| 1874 | { |
| 1875 | /* Remove reference to existing PSK, if any. */ |
| 1876 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1877 | if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) ) |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1878 | { |
| 1879 | /* The maintenance of the PSK key slot is the |
| 1880 | * user's responsibility. */ |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1881 | conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT; |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1882 | } |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1883 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 1884 | if( conf->psk != NULL ) |
| 1885 | { |
| 1886 | mbedtls_platform_zeroize( conf->psk, conf->psk_len ); |
| 1887 | |
| 1888 | mbedtls_free( conf->psk ); |
| 1889 | conf->psk = NULL; |
| 1890 | conf->psk_len = 0; |
| 1891 | } |
| 1892 | |
| 1893 | /* Remove reference to PSK identity, if any. */ |
| 1894 | if( conf->psk_identity != NULL ) |
| 1895 | { |
| 1896 | mbedtls_free( conf->psk_identity ); |
| 1897 | conf->psk_identity = NULL; |
| 1898 | conf->psk_identity_len = 0; |
| 1899 | } |
| 1900 | } |
| 1901 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1902 | /* This function assumes that PSK identity in the SSL config is unset. |
| 1903 | * It checks that the provided identity is well-formed and attempts |
| 1904 | * to make a copy of it in the SSL config. |
| 1905 | * On failure, the PSK identity in the config remains unset. */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 1906 | MBEDTLS_CHECK_RETURN_CRITICAL |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1907 | static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf, |
| 1908 | unsigned char const *psk_identity, |
| 1909 | size_t psk_identity_len ) |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 1910 | { |
Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 1911 | /* Identity len will be encoded on two bytes */ |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1912 | if( psk_identity == NULL || |
Ronald Cron | 2a87e9b | 2022-10-19 10:55:26 +0200 | [diff] [blame] | 1913 | psk_identity_len == 0 || |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1914 | ( psk_identity_len >> 16 ) != 0 || |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 1915 | psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN ) |
Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 1916 | { |
| 1917 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1918 | } |
| 1919 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1920 | conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ); |
| 1921 | if( conf->psk_identity == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 1922 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 1923 | |
Manuel Pégourié-Gonnard | 120fdbd | 2015-05-07 17:07:50 +0100 | [diff] [blame] | 1924 | conf->psk_identity_len = psk_identity_len; |
Manuel Pégourié-Gonnard | 120fdbd | 2015-05-07 17:07:50 +0100 | [diff] [blame] | 1925 | memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len ); |
Paul Bakker | 5ad403f | 2013-09-18 21:21:30 +0200 | [diff] [blame] | 1926 | |
| 1927 | return( 0 ); |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 1928 | } |
| 1929 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1930 | int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf, |
| 1931 | const unsigned char *psk, size_t psk_len, |
| 1932 | const unsigned char *psk_identity, size_t psk_identity_len ) |
| 1933 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1934 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 1935 | |
| 1936 | /* We currently only support one PSK, raw or opaque. */ |
Ronald Cron | d29e13e | 2022-10-19 10:33:48 +0200 | [diff] [blame] | 1937 | if( mbedtls_ssl_conf_has_static_psk( conf ) ) |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 1938 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1939 | |
| 1940 | /* Check and set raw PSK */ |
Piotr Nowicki | 9926eaf | 2019-11-20 14:54:36 +0100 | [diff] [blame] | 1941 | if( psk == NULL ) |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1942 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Piotr Nowicki | 9926eaf | 2019-11-20 14:54:36 +0100 | [diff] [blame] | 1943 | if( psk_len == 0 ) |
| 1944 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1945 | if( psk_len > MBEDTLS_PSK_MAX_LEN ) |
| 1946 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1947 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1948 | if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ) |
| 1949 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 1950 | conf->psk_len = psk_len; |
| 1951 | memcpy( conf->psk, psk, conf->psk_len ); |
| 1952 | |
| 1953 | /* Check and set PSK Identity */ |
| 1954 | ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len ); |
| 1955 | if( ret != 0 ) |
| 1956 | ssl_conf_remove_psk( conf ); |
| 1957 | |
| 1958 | return( ret ); |
| 1959 | } |
| 1960 | |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1961 | static void ssl_remove_psk( mbedtls_ssl_context *ssl ) |
| 1962 | { |
| 1963 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1964 | if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) ) |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1965 | { |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 1966 | /* The maintenance of the external PSK key slot is the |
| 1967 | * user's responsibility. */ |
| 1968 | if( ssl->handshake->psk_opaque_is_internal ) |
| 1969 | { |
| 1970 | psa_destroy_key( ssl->handshake->psk_opaque ); |
| 1971 | ssl->handshake->psk_opaque_is_internal = 0; |
| 1972 | } |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1973 | ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT; |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1974 | } |
Neil Armstrong | e952a30 | 2022-05-03 10:22:14 +0200 | [diff] [blame] | 1975 | #else |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1976 | if( ssl->handshake->psk != NULL ) |
| 1977 | { |
| 1978 | mbedtls_platform_zeroize( ssl->handshake->psk, |
| 1979 | ssl->handshake->psk_len ); |
| 1980 | mbedtls_free( ssl->handshake->psk ); |
| 1981 | ssl->handshake->psk_len = 0; |
| 1982 | } |
Neil Armstrong | e952a30 | 2022-05-03 10:22:14 +0200 | [diff] [blame] | 1983 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1984 | } |
| 1985 | |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1986 | int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl, |
| 1987 | const unsigned char *psk, size_t psk_len ) |
| 1988 | { |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 1989 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1990 | psa_key_attributes_t key_attributes = psa_key_attributes_init(); |
Jerry Yu | 5d01c05 | 2022-08-17 10:18:10 +0800 | [diff] [blame] | 1991 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Jerry Yu | 24b8c81 | 2022-08-20 19:06:56 +0800 | [diff] [blame] | 1992 | psa_algorithm_t alg = PSA_ALG_NONE; |
Jerry Yu | 5d01c05 | 2022-08-17 10:18:10 +0800 | [diff] [blame] | 1993 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 1994 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 1995 | |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1996 | if( psk == NULL || ssl->handshake == NULL ) |
| 1997 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1998 | |
| 1999 | if( psk_len > MBEDTLS_PSK_MAX_LEN ) |
| 2000 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2001 | |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2002 | ssl_remove_psk( ssl ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 2003 | |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 2004 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Jerry Yu | ccc68a4 | 2022-07-26 16:39:20 +0800 | [diff] [blame] | 2005 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 2006 | if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 ) |
| 2007 | { |
Jerry Yu | 6cf6b47 | 2022-08-16 14:50:28 +0800 | [diff] [blame] | 2008 | if( ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384 ) |
Jerry Yu | ccc68a4 | 2022-07-26 16:39:20 +0800 | [diff] [blame] | 2009 | alg = PSA_ALG_TLS12_PSK_TO_MS( PSA_ALG_SHA_384 ); |
| 2010 | else |
| 2011 | alg = PSA_ALG_TLS12_PSK_TO_MS( PSA_ALG_SHA_256 ); |
| 2012 | psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE ); |
| 2013 | } |
| 2014 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 2015 | |
Jerry Yu | 568ec25 | 2022-07-22 21:27:34 +0800 | [diff] [blame] | 2016 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | ccc68a4 | 2022-07-26 16:39:20 +0800 | [diff] [blame] | 2017 | if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 ) |
| 2018 | { |
| 2019 | alg = PSA_ALG_HKDF_EXTRACT( PSA_ALG_ANY_HASH ); |
| 2020 | psa_set_key_usage_flags( &key_attributes, |
| 2021 | PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT ); |
| 2022 | } |
| 2023 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 2024 | |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 2025 | psa_set_key_algorithm( &key_attributes, alg ); |
| 2026 | psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE ); |
| 2027 | |
| 2028 | status = psa_import_key( &key_attributes, psk, psk_len, &key ); |
| 2029 | if( status != PSA_SUCCESS ) |
| 2030 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 2031 | |
| 2032 | /* Allow calling psa_destroy_key() on psk remove */ |
| 2033 | ssl->handshake->psk_opaque_is_internal = 1; |
| 2034 | return mbedtls_ssl_set_hs_psk_opaque( ssl, key ); |
| 2035 | #else |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 2036 | if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 2037 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 2038 | |
| 2039 | ssl->handshake->psk_len = psk_len; |
| 2040 | memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len ); |
| 2041 | |
| 2042 | return( 0 ); |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 2043 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 2044 | } |
| 2045 | |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2046 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 2047 | int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf, |
Andrzej Kurek | 03e0146 | 2022-01-03 12:53:24 +0100 | [diff] [blame] | 2048 | mbedtls_svc_key_id_t psk, |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2049 | const unsigned char *psk_identity, |
| 2050 | 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 | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2057 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 2058 | /* Check and set opaque PSK */ |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2059 | if( mbedtls_svc_key_id_is_null( psk ) ) |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 2060 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2061 | conf->psk_opaque = psk; |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 2062 | |
| 2063 | /* Check and set PSK Identity */ |
| 2064 | ret = ssl_conf_set_psk_identity( conf, psk_identity, |
| 2065 | psk_identity_len ); |
| 2066 | if( ret != 0 ) |
| 2067 | ssl_conf_remove_psk( conf ); |
| 2068 | |
| 2069 | return( ret ); |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2070 | } |
| 2071 | |
Przemyslaw Stekiel | 6928a51 | 2022-02-03 13:50:35 +0100 | [diff] [blame] | 2072 | int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl, |
| 2073 | mbedtls_svc_key_id_t psk ) |
| 2074 | { |
| 2075 | if( ( mbedtls_svc_key_id_is_null( psk ) ) || |
| 2076 | ( ssl->handshake == NULL ) ) |
| 2077 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2078 | |
| 2079 | ssl_remove_psk( ssl ); |
| 2080 | ssl->handshake->psk_opaque = psk; |
| 2081 | return( 0 ); |
| 2082 | } |
| 2083 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 2084 | |
Jerry Yu | 8897c07 | 2022-08-12 13:56:53 +0800 | [diff] [blame] | 2085 | #if defined(MBEDTLS_SSL_SRV_C) |
Przemyslaw Stekiel | 6928a51 | 2022-02-03 13:50:35 +0100 | [diff] [blame] | 2086 | void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf, |
| 2087 | int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *, |
| 2088 | size_t), |
| 2089 | void *p_psk ) |
| 2090 | { |
| 2091 | conf->f_psk = f_psk; |
| 2092 | conf->p_psk = p_psk; |
| 2093 | } |
Jerry Yu | 8897c07 | 2022-08-12 13:56:53 +0800 | [diff] [blame] | 2094 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 2095 | |
Ronald Cron | 73fe8df | 2022-10-05 14:31:43 +0200 | [diff] [blame] | 2096 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */ |
Przemyslaw Stekiel | 6928a51 | 2022-02-03 13:50:35 +0100 | [diff] [blame] | 2097 | |
| 2098 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 301711e | 2022-04-26 16:57:05 +0200 | [diff] [blame] | 2099 | static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode( |
| 2100 | psa_algorithm_t alg ) |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 2101 | { |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 2102 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2103 | if( alg == PSA_ALG_CBC_NO_PADDING ) |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 2104 | return( MBEDTLS_SSL_MODE_CBC ); |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 2105 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2106 | if( PSA_ALG_IS_AEAD( alg ) ) |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 2107 | return( MBEDTLS_SSL_MODE_AEAD ); |
Gilles Peskine | 301711e | 2022-04-26 16:57:05 +0200 | [diff] [blame] | 2108 | return( MBEDTLS_SSL_MODE_STREAM ); |
| 2109 | } |
| 2110 | |
| 2111 | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
| 2112 | |
| 2113 | static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode( |
| 2114 | mbedtls_cipher_mode_t mode ) |
| 2115 | { |
| 2116 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
| 2117 | if( mode == MBEDTLS_MODE_CBC ) |
| 2118 | return( MBEDTLS_SSL_MODE_CBC ); |
| 2119 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
| 2120 | |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 2121 | #if defined(MBEDTLS_GCM_C) || \ |
| 2122 | defined(MBEDTLS_CCM_C) || \ |
| 2123 | defined(MBEDTLS_CHACHAPOLY_C) |
| 2124 | if( mode == MBEDTLS_MODE_GCM || |
| 2125 | mode == MBEDTLS_MODE_CCM || |
| 2126 | mode == MBEDTLS_MODE_CHACHAPOLY ) |
| 2127 | return( MBEDTLS_SSL_MODE_AEAD ); |
| 2128 | #endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */ |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 2129 | |
| 2130 | return( MBEDTLS_SSL_MODE_STREAM ); |
| 2131 | } |
Gilles Peskine | 301711e | 2022-04-26 16:57:05 +0200 | [diff] [blame] | 2132 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 2133 | |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2134 | static mbedtls_ssl_mode_t mbedtls_ssl_get_actual_mode( |
| 2135 | mbedtls_ssl_mode_t base_mode, |
| 2136 | int encrypt_then_mac ) |
| 2137 | { |
| 2138 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
| 2139 | if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED && |
| 2140 | base_mode == MBEDTLS_SSL_MODE_CBC ) |
| 2141 | { |
| 2142 | return( MBEDTLS_SSL_MODE_CBC_ETM ); |
| 2143 | } |
| 2144 | #else |
| 2145 | (void) encrypt_then_mac; |
| 2146 | #endif |
| 2147 | return( base_mode ); |
| 2148 | } |
| 2149 | |
Neil Armstrong | ab555e0 | 2022-04-04 11:07:59 +0200 | [diff] [blame] | 2150 | mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_transform( |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2151 | const mbedtls_ssl_transform *transform ) |
| 2152 | { |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2153 | mbedtls_ssl_mode_t base_mode = mbedtls_ssl_get_base_mode( |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2154 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2155 | transform->psa_alg |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2156 | #else |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2157 | mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) |
| 2158 | #endif |
| 2159 | ); |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2160 | |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2161 | int encrypt_then_mac = 0; |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 2162 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2163 | encrypt_then_mac = transform->encrypt_then_mac; |
| 2164 | #endif |
| 2165 | return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) ); |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2166 | } |
| 2167 | |
Neil Armstrong | ab555e0 | 2022-04-04 11:07:59 +0200 | [diff] [blame] | 2168 | mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_ciphersuite( |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 2169 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2170 | int encrypt_then_mac, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 2171 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2172 | const mbedtls_ssl_ciphersuite_t *suite ) |
| 2173 | { |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2174 | mbedtls_ssl_mode_t base_mode = MBEDTLS_SSL_MODE_STREAM; |
| 2175 | |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2176 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 2177 | psa_status_t status; |
| 2178 | psa_algorithm_t alg; |
| 2179 | psa_key_type_t type; |
| 2180 | size_t size; |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2181 | status = mbedtls_ssl_cipher_to_psa( suite->cipher, 0, &alg, &type, &size ); |
| 2182 | if( status == PSA_SUCCESS ) |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2183 | base_mode = mbedtls_ssl_get_base_mode( alg ); |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2184 | #else |
| 2185 | const mbedtls_cipher_info_t *cipher = |
| 2186 | mbedtls_cipher_info_from_type( suite->cipher ); |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2187 | if( cipher != NULL ) |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2188 | { |
| 2189 | base_mode = |
| 2190 | mbedtls_ssl_get_base_mode( |
| 2191 | mbedtls_cipher_info_get_mode( cipher ) ); |
| 2192 | } |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2193 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 2194 | |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2195 | #if !defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
| 2196 | int encrypt_then_mac = 0; |
| 2197 | #endif |
| 2198 | return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) ); |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2199 | } |
| 2200 | |
Neil Armstrong | 8395d7a | 2022-05-18 11:44:56 +0200 | [diff] [blame] | 2201 | #if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 2202 | |
| 2203 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2204 | /* Serialization of TLS 1.3 sessions: |
| 2205 | * |
| 2206 | * struct { |
Xiaokang Qian | 126bf8e | 2022-10-13 02:22:40 +0000 | [diff] [blame] | 2207 | * opaque hostname<0..2^16-1>; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2208 | * uint64 ticket_received; |
| 2209 | * uint32 ticket_lifetime; |
Jerry Yu | 3419107 | 2022-08-18 10:32:09 +0800 | [diff] [blame] | 2210 | * opaque ticket<1..2^16-1>; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2211 | * } ClientOnlyData; |
| 2212 | * |
| 2213 | * struct { |
| 2214 | * uint8 endpoint; |
| 2215 | * uint8 ciphersuite[2]; |
| 2216 | * uint32 ticket_age_add; |
| 2217 | * uint8 ticket_flags; |
| 2218 | * opaque resumption_key<0..255>; |
| 2219 | * select ( endpoint ) { |
| 2220 | * case client: ClientOnlyData; |
| 2221 | * case server: uint64 start_time; |
| 2222 | * }; |
| 2223 | * } serialized_session_tls13; |
| 2224 | * |
| 2225 | */ |
| 2226 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 2227 | MBEDTLS_CHECK_RETURN_CRITICAL |
| 2228 | static int ssl_tls13_session_save( const mbedtls_ssl_session *session, |
| 2229 | unsigned char *buf, |
| 2230 | size_t buf_len, |
| 2231 | size_t *olen ) |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2232 | { |
| 2233 | unsigned char *p = buf; |
Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 2234 | #if defined(MBEDTLS_SSL_CLI_C) && \ |
| 2235 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 2236 | size_t hostname_len = ( session->hostname == NULL ) ? |
Xiaokang Qian | ed0620c | 2022-10-12 06:58:13 +0000 | [diff] [blame] | 2237 | 0 : strlen( session->hostname ) + 1; |
Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 2238 | #endif |
Jerry Yu | bc7c1a4 | 2022-07-21 22:57:37 +0800 | [diff] [blame] | 2239 | size_t needed = 1 /* endpoint */ |
| 2240 | + 2 /* ciphersuite */ |
| 2241 | + 4 /* ticket_age_add */ |
Jerry Yu | 3419107 | 2022-08-18 10:32:09 +0800 | [diff] [blame] | 2242 | + 1 /* ticket_flags */ |
| 2243 | + 1; /* resumption_key length */ |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 2244 | *olen = 0; |
Jerry Yu | 3419107 | 2022-08-18 10:32:09 +0800 | [diff] [blame] | 2245 | |
| 2246 | if( session->resumption_key_len > MBEDTLS_SSL_TLS1_3_TICKET_RESUMPTION_KEY_LEN ) |
| 2247 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2248 | needed += session->resumption_key_len; /* resumption_key */ |
| 2249 | |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2250 | #if defined(MBEDTLS_HAVE_TIME) |
| 2251 | needed += 8; /* start_time or ticket_received */ |
| 2252 | #endif |
| 2253 | |
| 2254 | #if defined(MBEDTLS_SSL_CLI_C) |
| 2255 | if( session->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 2256 | { |
Xiaokang Qian | 126bf8e | 2022-10-13 02:22:40 +0000 | [diff] [blame] | 2257 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Xiaokang Qian | bc663a0 | 2022-10-09 11:14:39 +0000 | [diff] [blame] | 2258 | needed += 2 /* hostname_len */ |
Xiaokang Qian | 2f9efd3 | 2022-10-10 11:24:08 +0000 | [diff] [blame] | 2259 | + hostname_len; /* hostname */ |
Xiaokang Qian | 281fd1b | 2022-09-20 11:35:41 +0000 | [diff] [blame] | 2260 | #endif |
| 2261 | |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2262 | needed += 4 /* ticket_lifetime */ |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 2263 | + 2; /* ticket_len */ |
Jerry Yu | 3419107 | 2022-08-18 10:32:09 +0800 | [diff] [blame] | 2264 | |
| 2265 | /* Check size_t overflow */ |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 2266 | if( session->ticket_len > SIZE_MAX - needed ) |
| 2267 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jerry Yu | 3419107 | 2022-08-18 10:32:09 +0800 | [diff] [blame] | 2268 | |
Jerry Yu | e28d974 | 2022-08-18 15:44:03 +0800 | [diff] [blame] | 2269 | needed += session->ticket_len; /* ticket */ |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2270 | } |
| 2271 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 2272 | |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 2273 | *olen = needed; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2274 | if( needed > buf_len ) |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 2275 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2276 | |
| 2277 | p[0] = session->endpoint; |
| 2278 | MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 1 ); |
| 2279 | MBEDTLS_PUT_UINT32_BE( session->ticket_age_add, p, 3 ); |
| 2280 | p[7] = session->ticket_flags; |
| 2281 | |
| 2282 | /* save resumption_key */ |
Jerry Yu | bc7c1a4 | 2022-07-21 22:57:37 +0800 | [diff] [blame] | 2283 | p[8] = session->resumption_key_len; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2284 | p += 9; |
Jerry Yu | bc7c1a4 | 2022-07-21 22:57:37 +0800 | [diff] [blame] | 2285 | memcpy( p, session->resumption_key, session->resumption_key_len ); |
| 2286 | p += session->resumption_key_len; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2287 | |
| 2288 | #if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C) |
| 2289 | if( session->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 2290 | { |
| 2291 | MBEDTLS_PUT_UINT64_BE( (uint64_t) session->start, p, 0 ); |
| 2292 | p += 8; |
| 2293 | } |
| 2294 | #endif /* MBEDTLS_HAVE_TIME */ |
| 2295 | |
| 2296 | #if defined(MBEDTLS_SSL_CLI_C) |
| 2297 | if( session->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 2298 | { |
Xiaokang Qian | ed0620c | 2022-10-12 06:58:13 +0000 | [diff] [blame] | 2299 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 2300 | MBEDTLS_PUT_UINT16_BE( hostname_len, p, 0 ); |
| 2301 | p += 2; |
Xiaokang Qian | 126bf8e | 2022-10-13 02:22:40 +0000 | [diff] [blame] | 2302 | if( hostname_len > 0 ) |
Xiaokang Qian | ed0620c | 2022-10-12 06:58:13 +0000 | [diff] [blame] | 2303 | { |
| 2304 | /* save host name */ |
| 2305 | memcpy( p, session->hostname, hostname_len ); |
| 2306 | p += hostname_len; |
| 2307 | } |
| 2308 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
| 2309 | |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2310 | #if defined(MBEDTLS_HAVE_TIME) |
| 2311 | MBEDTLS_PUT_UINT64_BE( (uint64_t) session->ticket_received, p, 0 ); |
| 2312 | p += 8; |
| 2313 | #endif |
| 2314 | MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 ); |
| 2315 | p += 4; |
| 2316 | |
| 2317 | MBEDTLS_PUT_UINT16_BE( session->ticket_len, p, 0 ); |
| 2318 | p += 2; |
Jerry Yu | 3419107 | 2022-08-18 10:32:09 +0800 | [diff] [blame] | 2319 | |
| 2320 | if( session->ticket != NULL && session->ticket_len > 0 ) |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2321 | { |
| 2322 | memcpy( p, session->ticket, session->ticket_len ); |
| 2323 | p += session->ticket_len; |
| 2324 | } |
| 2325 | } |
| 2326 | #endif /* MBEDTLS_SSL_CLI_C */ |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 2327 | return( 0 ); |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2328 | } |
| 2329 | |
| 2330 | MBEDTLS_CHECK_RETURN_CRITICAL |
| 2331 | static int ssl_tls13_session_load( mbedtls_ssl_session *session, |
| 2332 | const unsigned char *buf, |
| 2333 | size_t len ) |
| 2334 | { |
| 2335 | const unsigned char *p = buf; |
| 2336 | const unsigned char *end = buf + len; |
| 2337 | |
| 2338 | if( end - p < 9 ) |
| 2339 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2340 | session->endpoint = p[0]; |
| 2341 | session->ciphersuite = MBEDTLS_GET_UINT16_BE( p, 1 ); |
| 2342 | session->ticket_age_add = MBEDTLS_GET_UINT32_BE( p, 3 ); |
| 2343 | session->ticket_flags = p[7]; |
| 2344 | |
| 2345 | /* load resumption_key */ |
Jerry Yu | bc7c1a4 | 2022-07-21 22:57:37 +0800 | [diff] [blame] | 2346 | session->resumption_key_len = p[8]; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2347 | p += 9; |
| 2348 | |
Jerry Yu | bc7c1a4 | 2022-07-21 22:57:37 +0800 | [diff] [blame] | 2349 | if( end - p < session->resumption_key_len ) |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2350 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2351 | |
Jerry Yu | bc7c1a4 | 2022-07-21 22:57:37 +0800 | [diff] [blame] | 2352 | if( sizeof( session->resumption_key ) < session->resumption_key_len ) |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2353 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jerry Yu | bc7c1a4 | 2022-07-21 22:57:37 +0800 | [diff] [blame] | 2354 | memcpy( session->resumption_key, p, session->resumption_key_len ); |
| 2355 | p += session->resumption_key_len; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2356 | |
| 2357 | #if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C) |
| 2358 | if( session->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 2359 | { |
| 2360 | if( end - p < 8 ) |
| 2361 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2362 | session->start = MBEDTLS_GET_UINT64_BE( p, 0 ); |
| 2363 | p += 8; |
| 2364 | } |
| 2365 | #endif /* MBEDTLS_HAVE_TIME */ |
| 2366 | |
| 2367 | #if defined(MBEDTLS_SSL_CLI_C) |
| 2368 | if( session->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 2369 | { |
Xiaokang Qian | ed0620c | 2022-10-12 06:58:13 +0000 | [diff] [blame] | 2370 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \ |
| 2371 | defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 2372 | size_t hostname_len; |
| 2373 | /* load host name */ |
| 2374 | if( end - p < 2 ) |
| 2375 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2376 | hostname_len = MBEDTLS_GET_UINT16_BE( p, 0 ); |
| 2377 | p += 2; |
| 2378 | |
| 2379 | if( end - p < ( long int )hostname_len ) |
| 2380 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2381 | if( hostname_len > 0 ) |
| 2382 | { |
| 2383 | session->hostname = mbedtls_calloc( 1, hostname_len ); |
| 2384 | if( session->hostname == NULL ) |
| 2385 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 2386 | memcpy( session->hostname, p, hostname_len ); |
| 2387 | p += hostname_len; |
| 2388 | } |
| 2389 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION && |
| 2390 | MBEDTLS_SSL_SESSION_TICKETS */ |
| 2391 | |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2392 | #if defined(MBEDTLS_HAVE_TIME) |
| 2393 | if( end - p < 8 ) |
| 2394 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2395 | session->ticket_received = MBEDTLS_GET_UINT64_BE( p, 0 ); |
| 2396 | p += 8; |
| 2397 | #endif |
| 2398 | if( end - p < 4 ) |
| 2399 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2400 | session->ticket_lifetime = MBEDTLS_GET_UINT32_BE( p, 0 ); |
| 2401 | p += 4; |
| 2402 | |
| 2403 | if( end - p < 2 ) |
| 2404 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2405 | session->ticket_len = MBEDTLS_GET_UINT16_BE( p, 0 ); |
| 2406 | p += 2; |
| 2407 | |
| 2408 | if( end - p < ( long int )session->ticket_len ) |
| 2409 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2410 | if( session->ticket_len > 0 ) |
| 2411 | { |
| 2412 | session->ticket = mbedtls_calloc( 1, session->ticket_len ); |
| 2413 | if( session->ticket == NULL ) |
| 2414 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 2415 | memcpy( session->ticket, p, session->ticket_len ); |
| 2416 | p += session->ticket_len; |
| 2417 | } |
| 2418 | } |
| 2419 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 2420 | |
| 2421 | return( 0 ); |
| 2422 | |
| 2423 | } |
| 2424 | #else /* MBEDTLS_SSL_SESSION_TICKETS */ |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 2425 | MBEDTLS_CHECK_RETURN_CRITICAL |
| 2426 | static int ssl_tls13_session_save( const mbedtls_ssl_session *session, |
| 2427 | unsigned char *buf, |
| 2428 | size_t buf_len, |
| 2429 | size_t *olen ) |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 2430 | { |
| 2431 | ((void) session); |
| 2432 | ((void) buf); |
| 2433 | ((void) buf_len); |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 2434 | *olen = 0; |
| 2435 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 2436 | } |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2437 | |
| 2438 | static int ssl_tls13_session_load( const mbedtls_ssl_session *session, |
| 2439 | unsigned char *buf, |
| 2440 | size_t buf_len ) |
| 2441 | { |
| 2442 | ((void) session); |
| 2443 | ((void) buf); |
| 2444 | ((void) buf_len); |
| 2445 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 2446 | } |
| 2447 | #endif /* !MBEDTLS_SSL_SESSION_TICKETS */ |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 2448 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 2449 | |
Przemyslaw Stekiel | f57b456 | 2022-01-25 00:04:18 +0100 | [diff] [blame] | 2450 | 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] | 2451 | size_t taglen, |
| 2452 | psa_algorithm_t *alg, |
| 2453 | psa_key_type_t *key_type, |
| 2454 | size_t *key_size ) |
| 2455 | { |
| 2456 | switch ( mbedtls_cipher_type ) |
| 2457 | { |
| 2458 | case MBEDTLS_CIPHER_AES_128_CBC: |
| 2459 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 2460 | *key_type = PSA_KEY_TYPE_AES; |
| 2461 | *key_size = 128; |
| 2462 | break; |
| 2463 | case MBEDTLS_CIPHER_AES_128_CCM: |
| 2464 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 2465 | *key_type = PSA_KEY_TYPE_AES; |
| 2466 | *key_size = 128; |
| 2467 | break; |
| 2468 | case MBEDTLS_CIPHER_AES_128_GCM: |
| 2469 | *alg = PSA_ALG_GCM; |
| 2470 | *key_type = PSA_KEY_TYPE_AES; |
| 2471 | *key_size = 128; |
| 2472 | break; |
| 2473 | case MBEDTLS_CIPHER_AES_192_CCM: |
| 2474 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 2475 | *key_type = PSA_KEY_TYPE_AES; |
| 2476 | *key_size = 192; |
| 2477 | break; |
| 2478 | case MBEDTLS_CIPHER_AES_192_GCM: |
| 2479 | *alg = PSA_ALG_GCM; |
| 2480 | *key_type = PSA_KEY_TYPE_AES; |
| 2481 | *key_size = 192; |
| 2482 | break; |
| 2483 | case MBEDTLS_CIPHER_AES_256_CBC: |
| 2484 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 2485 | *key_type = PSA_KEY_TYPE_AES; |
| 2486 | *key_size = 256; |
| 2487 | break; |
| 2488 | case MBEDTLS_CIPHER_AES_256_CCM: |
| 2489 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 2490 | *key_type = PSA_KEY_TYPE_AES; |
| 2491 | *key_size = 256; |
| 2492 | break; |
| 2493 | case MBEDTLS_CIPHER_AES_256_GCM: |
| 2494 | *alg = PSA_ALG_GCM; |
| 2495 | *key_type = PSA_KEY_TYPE_AES; |
| 2496 | *key_size = 256; |
| 2497 | break; |
| 2498 | case MBEDTLS_CIPHER_ARIA_128_CBC: |
| 2499 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 2500 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2501 | *key_size = 128; |
| 2502 | break; |
| 2503 | case MBEDTLS_CIPHER_ARIA_128_CCM: |
| 2504 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 2505 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2506 | *key_size = 128; |
| 2507 | break; |
| 2508 | case MBEDTLS_CIPHER_ARIA_128_GCM: |
| 2509 | *alg = PSA_ALG_GCM; |
| 2510 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2511 | *key_size = 128; |
| 2512 | break; |
| 2513 | case MBEDTLS_CIPHER_ARIA_192_CCM: |
| 2514 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 2515 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2516 | *key_size = 192; |
| 2517 | break; |
| 2518 | case MBEDTLS_CIPHER_ARIA_192_GCM: |
| 2519 | *alg = PSA_ALG_GCM; |
| 2520 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2521 | *key_size = 192; |
| 2522 | break; |
| 2523 | case MBEDTLS_CIPHER_ARIA_256_CBC: |
| 2524 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 2525 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2526 | *key_size = 256; |
| 2527 | break; |
| 2528 | case MBEDTLS_CIPHER_ARIA_256_CCM: |
| 2529 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 2530 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2531 | *key_size = 256; |
| 2532 | break; |
| 2533 | case MBEDTLS_CIPHER_ARIA_256_GCM: |
| 2534 | *alg = PSA_ALG_GCM; |
| 2535 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2536 | *key_size = 256; |
| 2537 | break; |
| 2538 | case MBEDTLS_CIPHER_CAMELLIA_128_CBC: |
| 2539 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 2540 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2541 | *key_size = 128; |
| 2542 | break; |
| 2543 | case MBEDTLS_CIPHER_CAMELLIA_128_CCM: |
| 2544 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 2545 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2546 | *key_size = 128; |
| 2547 | break; |
| 2548 | case MBEDTLS_CIPHER_CAMELLIA_128_GCM: |
| 2549 | *alg = PSA_ALG_GCM; |
| 2550 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2551 | *key_size = 128; |
| 2552 | break; |
| 2553 | case MBEDTLS_CIPHER_CAMELLIA_192_CCM: |
| 2554 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 2555 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2556 | *key_size = 192; |
| 2557 | break; |
| 2558 | case MBEDTLS_CIPHER_CAMELLIA_192_GCM: |
| 2559 | *alg = PSA_ALG_GCM; |
| 2560 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2561 | *key_size = 192; |
| 2562 | break; |
| 2563 | case MBEDTLS_CIPHER_CAMELLIA_256_CBC: |
| 2564 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 2565 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2566 | *key_size = 256; |
| 2567 | break; |
| 2568 | case MBEDTLS_CIPHER_CAMELLIA_256_CCM: |
| 2569 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 2570 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2571 | *key_size = 256; |
| 2572 | break; |
| 2573 | case MBEDTLS_CIPHER_CAMELLIA_256_GCM: |
| 2574 | *alg = PSA_ALG_GCM; |
| 2575 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2576 | *key_size = 256; |
| 2577 | break; |
| 2578 | case MBEDTLS_CIPHER_CHACHA20_POLY1305: |
| 2579 | *alg = PSA_ALG_CHACHA20_POLY1305; |
| 2580 | *key_type = PSA_KEY_TYPE_CHACHA20; |
| 2581 | *key_size = 256; |
| 2582 | break; |
| 2583 | case MBEDTLS_CIPHER_NULL: |
| 2584 | *alg = MBEDTLS_SSL_NULL_CIPHER; |
| 2585 | *key_type = 0; |
| 2586 | *key_size = 0; |
| 2587 | break; |
| 2588 | default: |
| 2589 | return PSA_ERROR_NOT_SUPPORTED; |
| 2590 | } |
| 2591 | |
| 2592 | return PSA_SUCCESS; |
| 2593 | } |
Neil Armstrong | 8395d7a | 2022-05-18 11:44:56 +0200 | [diff] [blame] | 2594 | #endif /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2595 | |
Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 2596 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 2597 | int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf, |
| 2598 | const unsigned char *dhm_P, size_t P_len, |
| 2599 | const unsigned char *dhm_G, size_t G_len ) |
| 2600 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2601 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 2602 | |
Glenn Strauss | cee1129 | 2021-12-20 01:43:17 -0500 | [diff] [blame] | 2603 | mbedtls_mpi_free( &conf->dhm_P ); |
| 2604 | mbedtls_mpi_free( &conf->dhm_G ); |
| 2605 | |
Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 2606 | if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 || |
| 2607 | ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 ) |
| 2608 | { |
| 2609 | mbedtls_mpi_free( &conf->dhm_P ); |
| 2610 | mbedtls_mpi_free( &conf->dhm_G ); |
| 2611 | return( ret ); |
| 2612 | } |
| 2613 | |
| 2614 | return( 0 ); |
| 2615 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2616 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2617 | 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] | 2618 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2619 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 2620 | |
Glenn Strauss | cee1129 | 2021-12-20 01:43:17 -0500 | [diff] [blame] | 2621 | mbedtls_mpi_free( &conf->dhm_P ); |
| 2622 | mbedtls_mpi_free( &conf->dhm_G ); |
| 2623 | |
Gilles Peskine | e570248 | 2021-06-11 21:59:08 +0200 | [diff] [blame] | 2624 | if( ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_P, |
| 2625 | &conf->dhm_P ) ) != 0 || |
| 2626 | ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_G, |
| 2627 | &conf->dhm_G ) ) != 0 ) |
Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 2628 | { |
| 2629 | mbedtls_mpi_free( &conf->dhm_P ); |
| 2630 | mbedtls_mpi_free( &conf->dhm_G ); |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 2631 | return( ret ); |
Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 2632 | } |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 2633 | |
| 2634 | return( 0 ); |
| 2635 | } |
Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 2636 | #endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */ |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 2637 | |
Manuel Pégourié-Gonnard | bd990d6 | 2015-06-11 14:49:42 +0200 | [diff] [blame] | 2638 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C) |
| 2639 | /* |
| 2640 | * Set the minimum length for Diffie-Hellman parameters |
| 2641 | */ |
| 2642 | void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf, |
| 2643 | unsigned int bitlen ) |
| 2644 | { |
| 2645 | conf->dhm_min_bitlen = bitlen; |
| 2646 | } |
| 2647 | #endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */ |
| 2648 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 2649 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Jerry Yu | 7ddc38c | 2022-01-19 11:08:05 +0800 | [diff] [blame] | 2650 | #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] | 2651 | /* |
| 2652 | * Set allowed/preferred hashes for handshake signatures |
| 2653 | */ |
| 2654 | void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf, |
| 2655 | const int *hashes ) |
| 2656 | { |
| 2657 | conf->sig_hashes = hashes; |
| 2658 | } |
Jerry Yu | 7ddc38c | 2022-01-19 11:08:05 +0800 | [diff] [blame] | 2659 | #endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */ |
Hanno Becker | 1cd6e00 | 2021-08-10 13:27:10 +0100 | [diff] [blame] | 2660 | |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 2661 | /* Configure allowed signature algorithms for handshake */ |
Hanno Becker | 1cd6e00 | 2021-08-10 13:27:10 +0100 | [diff] [blame] | 2662 | void mbedtls_ssl_conf_sig_algs( mbedtls_ssl_config *conf, |
| 2663 | const uint16_t* sig_algs ) |
| 2664 | { |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 2665 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 2666 | conf->sig_hashes = NULL; |
| 2667 | #endif /* !MBEDTLS_DEPRECATED_REMOVED */ |
| 2668 | conf->sig_algs = sig_algs; |
Hanno Becker | 1cd6e00 | 2021-08-10 13:27:10 +0100 | [diff] [blame] | 2669 | } |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 2670 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Manuel Pégourié-Gonnard | 36a8b57 | 2015-06-17 12:43:26 +0200 | [diff] [blame] | 2671 | |
Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 2672 | #if defined(MBEDTLS_ECP_C) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 2673 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 2674 | /* |
| 2675 | * Set the allowed elliptic curves |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 2676 | * |
| 2677 | * mbedtls_ssl_setup() takes the provided list |
| 2678 | * and translates it to a list of IANA TLS group identifiers, |
| 2679 | * stored in ssl->handshake->group_list. |
| 2680 | * |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 2681 | */ |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2682 | void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2683 | const mbedtls_ecp_group_id *curve_list ) |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 2684 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2685 | conf->curve_list = curve_list; |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 2686 | conf->group_list = NULL; |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 2687 | } |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 2688 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2689 | #endif /* MBEDTLS_ECP_C */ |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 2690 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 2691 | /* |
| 2692 | * Set the allowed groups |
| 2693 | */ |
| 2694 | void mbedtls_ssl_conf_groups( mbedtls_ssl_config *conf, |
| 2695 | const uint16_t *group_list ) |
| 2696 | { |
| 2697 | #if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 2698 | conf->curve_list = NULL; |
| 2699 | #endif |
| 2700 | conf->group_list = group_list; |
| 2701 | } |
| 2702 | |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 2703 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2704 | int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2705 | { |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2706 | /* Initialize to suppress unnecessary compiler warning */ |
| 2707 | size_t hostname_len = 0; |
| 2708 | |
| 2709 | /* Check if new hostname is valid before |
| 2710 | * making any change to current one */ |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2711 | if( hostname != NULL ) |
| 2712 | { |
| 2713 | hostname_len = strlen( hostname ); |
| 2714 | |
| 2715 | if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN ) |
| 2716 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2717 | } |
| 2718 | |
| 2719 | /* Now it's clear that we will overwrite the old hostname, |
| 2720 | * so we can free it safely */ |
| 2721 | |
| 2722 | if( ssl->hostname != NULL ) |
| 2723 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 2724 | mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) ); |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2725 | mbedtls_free( ssl->hostname ); |
| 2726 | } |
| 2727 | |
| 2728 | /* Passing NULL as hostname shall clear the old one */ |
Manuel Pégourié-Gonnard | ba26c24 | 2015-05-06 10:47:06 +0100 | [diff] [blame] | 2729 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2730 | if( hostname == NULL ) |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2731 | { |
| 2732 | ssl->hostname = NULL; |
| 2733 | } |
| 2734 | else |
| 2735 | { |
| 2736 | ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 ); |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2737 | if( ssl->hostname == NULL ) |
| 2738 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Paul Bakker | 75c1a6f | 2013-08-19 14:25:29 +0200 | [diff] [blame] | 2739 | |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2740 | memcpy( ssl->hostname, hostname, hostname_len ); |
Paul Bakker | 75c1a6f | 2013-08-19 14:25:29 +0200 | [diff] [blame] | 2741 | |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2742 | ssl->hostname[hostname_len] = '\0'; |
| 2743 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2744 | |
| 2745 | return( 0 ); |
| 2746 | } |
Hanno Becker | 1a9a51c | 2017-04-07 13:02:16 +0100 | [diff] [blame] | 2747 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2748 | |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 2749 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2750 | void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2751 | int (*f_sni)(void *, mbedtls_ssl_context *, |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 2752 | const unsigned char *, size_t), |
| 2753 | void *p_sni ) |
| 2754 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2755 | conf->f_sni = f_sni; |
| 2756 | conf->p_sni = p_sni; |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 2757 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2758 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 2759 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2760 | #if defined(MBEDTLS_SSL_ALPN) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2761 | 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] | 2762 | { |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 2763 | size_t cur_len, tot_len; |
| 2764 | const char **p; |
| 2765 | |
| 2766 | /* |
Brian J Murray | 1903fb3 | 2016-11-06 04:45:15 -0800 | [diff] [blame] | 2767 | * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings |
| 2768 | * MUST NOT be truncated." |
| 2769 | * We check lengths now rather than later. |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 2770 | */ |
| 2771 | tot_len = 0; |
| 2772 | for( p = protos; *p != NULL; p++ ) |
| 2773 | { |
| 2774 | cur_len = strlen( *p ); |
| 2775 | tot_len += cur_len; |
| 2776 | |
Ronald Cron | 8216dd3 | 2020-04-23 16:41:44 +0200 | [diff] [blame] | 2777 | if( ( cur_len == 0 ) || |
| 2778 | ( cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN ) || |
| 2779 | ( tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN ) ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2780 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 2781 | } |
| 2782 | |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2783 | conf->alpn_list = protos; |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 2784 | |
| 2785 | return( 0 ); |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 2786 | } |
| 2787 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2788 | 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] | 2789 | { |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 2790 | return( ssl->alpn_chosen ); |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 2791 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2792 | #endif /* MBEDTLS_SSL_ALPN */ |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 2793 | |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2794 | #if defined(MBEDTLS_SSL_DTLS_SRTP) |
Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 2795 | void mbedtls_ssl_conf_srtp_mki_value_supported( mbedtls_ssl_config *conf, |
| 2796 | int support_mki_value ) |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 2797 | { |
| 2798 | conf->dtls_srtp_mki_support = support_mki_value; |
| 2799 | } |
| 2800 | |
Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 2801 | int mbedtls_ssl_dtls_srtp_set_mki_value( mbedtls_ssl_context *ssl, |
| 2802 | unsigned char *mki_value, |
Johan Pascal | f6417ec | 2020-09-22 15:15:19 +0200 | [diff] [blame] | 2803 | uint16_t mki_len ) |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 2804 | { |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 2805 | if( mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH ) |
Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 2806 | { |
Johan Pascal | d576fdb | 2020-09-22 10:39:53 +0200 | [diff] [blame] | 2807 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 2808 | } |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 2809 | |
| 2810 | 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] | 2811 | { |
Johan Pascal | d576fdb | 2020-09-22 10:39:53 +0200 | [diff] [blame] | 2812 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 2813 | } |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 2814 | |
| 2815 | memcpy( ssl->dtls_srtp_info.mki_value, mki_value, mki_len ); |
| 2816 | ssl->dtls_srtp_info.mki_len = mki_len; |
Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 2817 | return( 0 ); |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 2818 | } |
| 2819 | |
Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 2820 | int mbedtls_ssl_conf_dtls_srtp_protection_profiles( mbedtls_ssl_config *conf, |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 2821 | const mbedtls_ssl_srtp_profile *profiles ) |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2822 | { |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 2823 | const mbedtls_ssl_srtp_profile *p; |
| 2824 | size_t list_size = 0; |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2825 | |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 2826 | /* check the profiles list: all entry must be valid, |
| 2827 | * 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] | 2828 | for( p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET && |
| 2829 | list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH; |
| 2830 | p++ ) |
Johan Pascal | d576fdb | 2020-09-22 10:39:53 +0200 | [diff] [blame] | 2831 | { |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 2832 | if( mbedtls_ssl_check_srtp_profile_value( *p ) != MBEDTLS_TLS_SRTP_UNSET ) |
Johan Pascal | d576fdb | 2020-09-22 10:39:53 +0200 | [diff] [blame] | 2833 | { |
Johan Pascal | 76fdf1d | 2020-10-22 23:31:00 +0200 | [diff] [blame] | 2834 | list_size++; |
| 2835 | } |
| 2836 | else |
| 2837 | { |
| 2838 | /* unsupported value, stop parsing and set the size to an error value */ |
| 2839 | list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1; |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2840 | } |
| 2841 | } |
| 2842 | |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 2843 | if( list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH ) |
Johan Pascal | d387aa0 | 2020-09-23 18:47:56 +0200 | [diff] [blame] | 2844 | { |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 2845 | conf->dtls_srtp_profile_list = NULL; |
| 2846 | conf->dtls_srtp_profile_list_len = 0; |
| 2847 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2848 | } |
| 2849 | |
Johan Pascal | 9bc97ca | 2020-09-21 23:44:45 +0200 | [diff] [blame] | 2850 | conf->dtls_srtp_profile_list = profiles; |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 2851 | conf->dtls_srtp_profile_list_len = list_size; |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2852 | |
| 2853 | return( 0 ); |
| 2854 | } |
| 2855 | |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 2856 | void mbedtls_ssl_get_dtls_srtp_negotiation_result( const mbedtls_ssl_context *ssl, |
| 2857 | mbedtls_dtls_srtp_info *dtls_srtp_info ) |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2858 | { |
Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 2859 | dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile; |
| 2860 | /* do not copy the mki value if there is no chosen profile */ |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 2861 | if( dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET ) |
Johan Pascal | 0dbcd1d | 2020-10-28 11:03:07 +0100 | [diff] [blame] | 2862 | { |
Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 2863 | dtls_srtp_info->mki_len = 0; |
Johan Pascal | 0dbcd1d | 2020-10-28 11:03:07 +0100 | [diff] [blame] | 2864 | } |
Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 2865 | else |
| 2866 | { |
| 2867 | dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len; |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 2868 | memcpy( dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value, |
| 2869 | ssl->dtls_srtp_info.mki_len ); |
Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 2870 | } |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2871 | } |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2872 | #endif /* MBEDTLS_SSL_DTLS_SRTP */ |
| 2873 | |
Aditya Patwardhan | 3096f33 | 2022-07-26 14:31:46 +0530 | [diff] [blame] | 2874 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 2875 | 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] | 2876 | { |
Glenn Strauss | 2dfcea2 | 2022-03-14 17:26:42 -0400 | [diff] [blame] | 2877 | conf->max_tls_version = (major << 8) | minor; |
Paul Bakker | 490ecc8 | 2011-10-06 13:04:09 +0000 | [diff] [blame] | 2878 | } |
| 2879 | |
Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 2880 | 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] | 2881 | { |
Glenn Strauss | 2dfcea2 | 2022-03-14 17:26:42 -0400 | [diff] [blame] | 2882 | conf->min_tls_version = (major << 8) | minor; |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 2883 | } |
Aditya Patwardhan | 3096f33 | 2022-07-26 14:31:46 +0530 | [diff] [blame] | 2884 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 2885 | |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 2886 | #if defined(MBEDTLS_SSL_SRV_C) |
| 2887 | void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf, |
| 2888 | char cert_req_ca_list ) |
| 2889 | { |
| 2890 | conf->cert_req_ca_list = cert_req_ca_list; |
| 2891 | } |
| 2892 | #endif |
| 2893 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2894 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2895 | 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] | 2896 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2897 | conf->encrypt_then_mac = etm; |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 2898 | } |
| 2899 | #endif |
| 2900 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2901 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2902 | 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] | 2903 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2904 | conf->extended_ms = ems; |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 2905 | } |
| 2906 | #endif |
| 2907 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2908 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2909 | 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] | 2910 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2911 | if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID || |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 2912 | 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] | 2913 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2914 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 2915 | } |
| 2916 | |
Manuel Pégourié-Gonnard | 6bf89d6 | 2015-05-05 17:01:57 +0100 | [diff] [blame] | 2917 | conf->mfl_code = mfl_code; |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 2918 | |
| 2919 | return( 0 ); |
| 2920 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2921 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 2922 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2923 | 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] | 2924 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2925 | conf->allow_legacy_renegotiation = allow_legacy; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2926 | } |
| 2927 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2928 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2929 | 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] | 2930 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2931 | conf->disable_renegotiation = renegotiation; |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 2932 | } |
| 2933 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2934 | 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] | 2935 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2936 | conf->renego_max_records = max_records; |
Manuel Pégourié-Gonnard | a9964db | 2014-07-03 19:29:16 +0200 | [diff] [blame] | 2937 | } |
| 2938 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2939 | void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 837f0fe | 2014-11-05 13:58:53 +0100 | [diff] [blame] | 2940 | const unsigned char period[8] ) |
| 2941 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2942 | memcpy( conf->renego_period, period, 8 ); |
Manuel Pégourié-Gonnard | 837f0fe | 2014-11-05 13:58:53 +0100 | [diff] [blame] | 2943 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2944 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2945 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2946 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 2947 | #if defined(MBEDTLS_SSL_CLI_C) |
| 2948 | 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] | 2949 | { |
Manuel Pégourié-Gonnard | 2b49445 | 2015-05-06 10:05:11 +0100 | [diff] [blame] | 2950 | conf->session_tickets = use_tickets; |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 2951 | } |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 2952 | #endif |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 2953 | |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 2954 | #if defined(MBEDTLS_SSL_SRV_C) |
Jerry Yu | 1ad7ace | 2022-08-09 13:28:39 +0800 | [diff] [blame] | 2955 | |
Jerry Yu | d0766ec | 2022-09-22 10:46:57 +0800 | [diff] [blame] | 2956 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS) |
Jerry Yu | 1ad7ace | 2022-08-09 13:28:39 +0800 | [diff] [blame] | 2957 | void mbedtls_ssl_conf_new_session_tickets( mbedtls_ssl_config *conf, |
| 2958 | uint16_t num_tickets ) |
| 2959 | { |
Jerry Yu | d0766ec | 2022-09-22 10:46:57 +0800 | [diff] [blame] | 2960 | conf->new_session_tickets_count = num_tickets; |
Jerry Yu | 1ad7ace | 2022-08-09 13:28:39 +0800 | [diff] [blame] | 2961 | } |
| 2962 | #endif |
| 2963 | |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 2964 | void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf, |
| 2965 | mbedtls_ssl_ticket_write_t *f_ticket_write, |
| 2966 | mbedtls_ssl_ticket_parse_t *f_ticket_parse, |
| 2967 | void *p_ticket ) |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 2968 | { |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 2969 | conf->f_ticket_write = f_ticket_write; |
| 2970 | conf->f_ticket_parse = f_ticket_parse; |
| 2971 | conf->p_ticket = p_ticket; |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 2972 | } |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 2973 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2974 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 2975 | |
Hanno Becker | 7e6c178 | 2021-06-08 09:24:55 +0100 | [diff] [blame] | 2976 | void mbedtls_ssl_set_export_keys_cb( mbedtls_ssl_context *ssl, |
| 2977 | mbedtls_ssl_export_keys_t *f_export_keys, |
| 2978 | void *p_export_keys ) |
Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 2979 | { |
Hanno Becker | 7e6c178 | 2021-06-08 09:24:55 +0100 | [diff] [blame] | 2980 | ssl->f_export_keys = f_export_keys; |
| 2981 | ssl->p_export_keys = p_export_keys; |
Ron Eldor | f5cc10d | 2019-05-07 18:33:40 +0300 | [diff] [blame] | 2982 | } |
Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 2983 | |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 2984 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 2985 | void mbedtls_ssl_conf_async_private_cb( |
| 2986 | mbedtls_ssl_config *conf, |
| 2987 | mbedtls_ssl_async_sign_t *f_async_sign, |
| 2988 | mbedtls_ssl_async_decrypt_t *f_async_decrypt, |
| 2989 | mbedtls_ssl_async_resume_t *f_async_resume, |
| 2990 | mbedtls_ssl_async_cancel_t *f_async_cancel, |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 2991 | void *async_config_data ) |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 2992 | { |
| 2993 | conf->f_async_sign_start = f_async_sign; |
| 2994 | conf->f_async_decrypt_start = f_async_decrypt; |
| 2995 | conf->f_async_resume = f_async_resume; |
| 2996 | conf->f_async_cancel = f_async_cancel; |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 2997 | conf->p_async_config_data = async_config_data; |
| 2998 | } |
| 2999 | |
Gilles Peskine | 8f97af7 | 2018-04-26 11:46:10 +0200 | [diff] [blame] | 3000 | void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf ) |
| 3001 | { |
| 3002 | return( conf->p_async_config_data ); |
| 3003 | } |
| 3004 | |
Gilles Peskine | 1febfef | 2018-04-30 11:54:39 +0200 | [diff] [blame] | 3005 | void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl ) |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3006 | { |
| 3007 | if( ssl->handshake == NULL ) |
| 3008 | return( NULL ); |
| 3009 | else |
| 3010 | return( ssl->handshake->user_async_ctx ); |
| 3011 | } |
| 3012 | |
Gilles Peskine | 1febfef | 2018-04-30 11:54:39 +0200 | [diff] [blame] | 3013 | void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl, |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3014 | void *ctx ) |
| 3015 | { |
| 3016 | if( ssl->handshake != NULL ) |
| 3017 | ssl->handshake->user_async_ctx = ctx; |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 3018 | } |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 3019 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 3020 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3021 | /* |
| 3022 | * SSL get accessors |
| 3023 | */ |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 3024 | uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3025 | { |
Manuel Pégourié-Gonnard | e89163c | 2015-01-23 14:30:57 +0000 | [diff] [blame] | 3026 | if( ssl->session != NULL ) |
| 3027 | return( ssl->session->verify_result ); |
| 3028 | |
| 3029 | if( ssl->session_negotiate != NULL ) |
| 3030 | return( ssl->session_negotiate->verify_result ); |
| 3031 | |
Manuel Pégourié-Gonnard | 6ab9b00 | 2015-05-14 11:25:04 +0200 | [diff] [blame] | 3032 | return( 0xFFFFFFFF ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3033 | } |
| 3034 | |
Glenn Strauss | 8f52690 | 2022-01-13 00:04:49 -0500 | [diff] [blame] | 3035 | int mbedtls_ssl_get_ciphersuite_id_from_ssl( const mbedtls_ssl_context *ssl ) |
| 3036 | { |
| 3037 | if( ssl == NULL || ssl->session == NULL ) |
| 3038 | return( 0 ); |
| 3039 | |
| 3040 | return( ssl->session->ciphersuite ); |
| 3041 | } |
| 3042 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3043 | const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl ) |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 3044 | { |
Paul Bakker | 926c8e4 | 2013-03-06 10:23:34 +0100 | [diff] [blame] | 3045 | if( ssl == NULL || ssl->session == NULL ) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 3046 | return( NULL ); |
Paul Bakker | 926c8e4 | 2013-03-06 10:23:34 +0100 | [diff] [blame] | 3047 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3048 | return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite ); |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 3049 | } |
| 3050 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3051 | const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl ) |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 3052 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3053 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3054 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 3055 | { |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 3056 | switch( ssl->tls_version ) |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 3057 | { |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 3058 | case MBEDTLS_SSL_VERSION_TLS1_2: |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 3059 | return( "DTLSv1.2" ); |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 3060 | default: |
| 3061 | return( "unknown (DTLS)" ); |
| 3062 | } |
| 3063 | } |
| 3064 | #endif |
| 3065 | |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 3066 | switch( ssl->tls_version ) |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 3067 | { |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 3068 | case MBEDTLS_SSL_VERSION_TLS1_2: |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3069 | return( "TLSv1.2" ); |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 3070 | case MBEDTLS_SSL_VERSION_TLS1_3: |
Gilles Peskine | c63a1e0 | 2022-01-13 01:10:24 +0100 | [diff] [blame] | 3071 | return( "TLSv1.3" ); |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 3072 | default: |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 3073 | return( "unknown" ); |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 3074 | } |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 3075 | } |
| 3076 | |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 3077 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 3078 | size_t mbedtls_ssl_get_input_max_frag_len( const mbedtls_ssl_context *ssl ) |
| 3079 | { |
David Horstmann | 95d516f | 2021-05-04 18:36:56 +0100 | [diff] [blame] | 3080 | size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN; |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 3081 | size_t read_mfl; |
| 3082 | |
| 3083 | /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */ |
| 3084 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
| 3085 | ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE ) |
| 3086 | { |
| 3087 | return ssl_mfl_code_to_length( ssl->conf->mfl_code ); |
| 3088 | } |
| 3089 | |
| 3090 | /* Check if a smaller max length was negotiated */ |
| 3091 | if( ssl->session_out != NULL ) |
| 3092 | { |
| 3093 | read_mfl = ssl_mfl_code_to_length( ssl->session_out->mfl_code ); |
| 3094 | if( read_mfl < max_len ) |
| 3095 | { |
| 3096 | max_len = read_mfl; |
| 3097 | } |
| 3098 | } |
| 3099 | |
| 3100 | // During a handshake, use the value being negotiated |
| 3101 | if( ssl->session_negotiate != NULL ) |
| 3102 | { |
| 3103 | read_mfl = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ); |
| 3104 | if( read_mfl < max_len ) |
| 3105 | { |
| 3106 | max_len = read_mfl; |
| 3107 | } |
| 3108 | } |
| 3109 | |
| 3110 | return( max_len ); |
| 3111 | } |
| 3112 | |
| 3113 | 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] | 3114 | { |
| 3115 | size_t max_len; |
| 3116 | |
| 3117 | /* |
| 3118 | * Assume mfl_code is correct since it was checked when set |
| 3119 | */ |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3120 | 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] | 3121 | |
Manuel Pégourié-Gonnard | 2cb17e2 | 2017-09-19 13:00:47 +0200 | [diff] [blame] | 3122 | /* Check if a smaller max length was negotiated */ |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 3123 | if( ssl->session_out != NULL && |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3124 | 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] | 3125 | { |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3126 | 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] | 3127 | } |
| 3128 | |
Manuel Pégourié-Gonnard | 2cb17e2 | 2017-09-19 13:00:47 +0200 | [diff] [blame] | 3129 | /* During a handshake, use the value being negotiated */ |
| 3130 | if( ssl->session_negotiate != NULL && |
| 3131 | ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len ) |
| 3132 | { |
| 3133 | max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ); |
| 3134 | } |
| 3135 | |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3136 | return( max_len ); |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 3137 | } |
| 3138 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
| 3139 | |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 3140 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 8949071 | 2020-02-05 10:50:12 +0000 | [diff] [blame] | 3141 | 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] | 3142 | { |
Andrzej Kurek | ef43ce6 | 2018-10-09 08:24:12 -0400 | [diff] [blame] | 3143 | /* Return unlimited mtu for client hello messages to avoid fragmentation. */ |
| 3144 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
| 3145 | ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO || |
| 3146 | ssl->state == MBEDTLS_SSL_SERVER_HELLO ) ) |
| 3147 | return ( 0 ); |
| 3148 | |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 3149 | if( ssl->handshake == NULL || ssl->handshake->mtu == 0 ) |
| 3150 | return( ssl->mtu ); |
| 3151 | |
| 3152 | if( ssl->mtu == 0 ) |
| 3153 | return( ssl->handshake->mtu ); |
| 3154 | |
| 3155 | return( ssl->mtu < ssl->handshake->mtu ? |
| 3156 | ssl->mtu : ssl->handshake->mtu ); |
| 3157 | } |
| 3158 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 3159 | |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3160 | int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl ) |
| 3161 | { |
| 3162 | size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN; |
| 3163 | |
Manuel Pégourié-Gonnard | 000281e | 2018-08-21 11:20:58 +0200 | [diff] [blame] | 3164 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \ |
| 3165 | !defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3166 | (void) ssl; |
| 3167 | #endif |
| 3168 | |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3169 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 3170 | 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] | 3171 | |
| 3172 | if( max_len > mfl ) |
| 3173 | max_len = mfl; |
| 3174 | #endif |
| 3175 | |
| 3176 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 8949071 | 2020-02-05 10:50:12 +0000 | [diff] [blame] | 3177 | if( mbedtls_ssl_get_current_mtu( ssl ) != 0 ) |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3178 | { |
Hanno Becker | 8949071 | 2020-02-05 10:50:12 +0000 | [diff] [blame] | 3179 | const size_t mtu = mbedtls_ssl_get_current_mtu( ssl ); |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3180 | const int ret = mbedtls_ssl_get_record_expansion( ssl ); |
| 3181 | const size_t overhead = (size_t) ret; |
| 3182 | |
| 3183 | if( ret < 0 ) |
| 3184 | return( ret ); |
| 3185 | |
| 3186 | if( mtu <= overhead ) |
| 3187 | { |
| 3188 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) ); |
| 3189 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 3190 | } |
| 3191 | |
| 3192 | if( max_len > mtu - overhead ) |
| 3193 | max_len = mtu - overhead; |
| 3194 | } |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 3195 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3196 | |
Hanno Becker | 0defedb | 2018-08-10 12:35:02 +0100 | [diff] [blame] | 3197 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \ |
| 3198 | !defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3199 | ((void) ssl); |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3200 | #endif |
| 3201 | |
| 3202 | return( (int) max_len ); |
| 3203 | } |
| 3204 | |
Hanno Becker | 2d8e99b | 2021-04-21 06:19:50 +0100 | [diff] [blame] | 3205 | int mbedtls_ssl_get_max_in_record_payload( const mbedtls_ssl_context *ssl ) |
| 3206 | { |
| 3207 | size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN; |
| 3208 | |
| 3209 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 3210 | (void) ssl; |
| 3211 | #endif |
| 3212 | |
| 3213 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 3214 | const size_t mfl = mbedtls_ssl_get_input_max_frag_len( ssl ); |
| 3215 | |
| 3216 | if( max_len > mfl ) |
| 3217 | max_len = mfl; |
| 3218 | #endif |
| 3219 | |
| 3220 | return( (int) max_len ); |
| 3221 | } |
| 3222 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3223 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 3224 | 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] | 3225 | { |
| 3226 | if( ssl == NULL || ssl->session == NULL ) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 3227 | return( NULL ); |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 3228 | |
Hanno Becker | e682457 | 2019-02-07 13:18:46 +0000 | [diff] [blame] | 3229 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 3230 | return( ssl->session->peer_cert ); |
Hanno Becker | e682457 | 2019-02-07 13:18:46 +0000 | [diff] [blame] | 3231 | #else |
| 3232 | return( NULL ); |
| 3233 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 3234 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3235 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 3236 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3237 | #if defined(MBEDTLS_SSL_CLI_C) |
Hanno Becker | f852b1c | 2019-02-05 11:42:30 +0000 | [diff] [blame] | 3238 | int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, |
| 3239 | mbedtls_ssl_session *dst ) |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 3240 | { |
Hanno Becker | e810bbc | 2021-05-14 16:01:05 +0100 | [diff] [blame] | 3241 | int ret; |
| 3242 | |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 3243 | if( ssl == NULL || |
| 3244 | dst == NULL || |
| 3245 | ssl->session == NULL || |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3246 | ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT ) |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 3247 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3248 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 3249 | } |
| 3250 | |
Hanno Becker | e810bbc | 2021-05-14 16:01:05 +0100 | [diff] [blame] | 3251 | /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer |
| 3252 | * idempotent: Each session can only be exported once. |
| 3253 | * |
| 3254 | * (This is in preparation for TLS 1.3 support where we will |
| 3255 | * need the ability to export multiple sessions (aka tickets), |
| 3256 | * which will be achieved by calling mbedtls_ssl_get_session() |
| 3257 | * multiple times until it fails.) |
| 3258 | * |
| 3259 | * Check whether we have already exported the current session, |
| 3260 | * and fail if so. |
| 3261 | */ |
| 3262 | if( ssl->session->exported == 1 ) |
| 3263 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 3264 | |
| 3265 | ret = mbedtls_ssl_session_copy( dst, ssl->session ); |
| 3266 | if( ret != 0 ) |
| 3267 | return( ret ); |
| 3268 | |
| 3269 | /* Remember that we've exported the session. */ |
| 3270 | ssl->session->exported = 1; |
| 3271 | return( 0 ); |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 3272 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3273 | #endif /* MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 3274 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3275 | /* |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 3276 | * Define ticket header determining Mbed TLS version |
| 3277 | * and structure of the ticket. |
| 3278 | */ |
| 3279 | |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3280 | /* |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 3281 | * Define bitflag determining compile-time settings influencing |
| 3282 | * structure of serialized SSL sessions. |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3283 | */ |
| 3284 | |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 3285 | #if defined(MBEDTLS_HAVE_TIME) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3286 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME 1 |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 3287 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3288 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3289 | #endif /* MBEDTLS_HAVE_TIME */ |
| 3290 | |
| 3291 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3292 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3293 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3294 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3295 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 3296 | |
| 3297 | #if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3298 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3299 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3300 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3301 | #endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */ |
| 3302 | |
| 3303 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3304 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3305 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3306 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3307 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
| 3308 | |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3309 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3310 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3311 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3312 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3313 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ |
| 3314 | |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3315 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 3316 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1 |
| 3317 | #else |
| 3318 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0 |
| 3319 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
| 3320 | |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3321 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0 |
| 3322 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1 |
| 3323 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2 |
| 3324 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3 |
Hanno Becker | 37bdbe6 | 2021-08-01 05:38:58 +0100 | [diff] [blame] | 3325 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4 |
| 3326 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5 |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3327 | |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 3328 | #define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \ |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3329 | ( (uint16_t) ( \ |
| 3330 | ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \ |
| 3331 | ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \ |
| 3332 | ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \ |
| 3333 | ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \ |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3334 | ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \ |
Hanno Becker | be34e8e | 2019-06-04 09:43:16 +0100 | [diff] [blame] | 3335 | ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) ) ) |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3336 | |
Hanno Becker | f878707 | 2019-05-16 12:41:07 +0100 | [diff] [blame] | 3337 | static unsigned char ssl_serialized_session_header[] = { |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3338 | MBEDTLS_VERSION_MAJOR, |
| 3339 | MBEDTLS_VERSION_MINOR, |
| 3340 | MBEDTLS_VERSION_PATCH, |
Joe Subbiani | 2194dc4 | 2021-07-14 12:31:31 +0100 | [diff] [blame] | 3341 | MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ), |
| 3342 | MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ), |
Hanno Becker | f878707 | 2019-05-16 12:41:07 +0100 | [diff] [blame] | 3343 | }; |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 3344 | |
| 3345 | /* |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 3346 | * Serialize a session in the following format: |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 3347 | * (in the presentation language of TLS, RFC 8446 section 3) |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 3348 | * |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3349 | * struct { |
Hanno Becker | dc28b6c | 2019-05-29 11:08:00 +0100 | [diff] [blame] | 3350 | * |
Hanno Becker | dce5097 | 2021-08-01 05:39:23 +0100 | [diff] [blame] | 3351 | * opaque mbedtls_version[3]; // library version: major, minor, patch |
| 3352 | * opaque session_format[2]; // library-version specific 16-bit field |
| 3353 | * // determining the format of the remaining |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3354 | * // serialized data. |
Hanno Becker | dc28b6c | 2019-05-29 11:08:00 +0100 | [diff] [blame] | 3355 | * |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3356 | * Note: When updating the format, remember to keep |
| 3357 | * these version+format bytes. |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 3358 | * |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3359 | * // In this version, `session_format` determines |
| 3360 | * // the setting of those compile-time |
| 3361 | * // configuration options which influence |
| 3362 | * // the structure of mbedtls_ssl_session. |
| 3363 | * |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 3364 | * uint8_t minor_ver; // Protocol minor version. Possible values: |
| 3365 | * // - TLS 1.2 (3) |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3366 | * |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 3367 | * select (serialized_session.tls_version) { |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3368 | * |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 3369 | * case MBEDTLS_SSL_VERSION_TLS1_2: |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3370 | * serialized_session_tls12 data; |
| 3371 | * |
| 3372 | * }; |
| 3373 | * |
| 3374 | * } serialized_session; |
| 3375 | * |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 3376 | */ |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3377 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 3378 | MBEDTLS_CHECK_RETURN_CRITICAL |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3379 | static int ssl_session_save( const mbedtls_ssl_session *session, |
| 3380 | unsigned char omit_header, |
| 3381 | unsigned char *buf, |
| 3382 | size_t buf_len, |
| 3383 | size_t *olen ) |
| 3384 | { |
| 3385 | unsigned char *p = buf; |
| 3386 | size_t used = 0; |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 3387 | size_t remaining_len; |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 3388 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 3389 | size_t out_len; |
| 3390 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 3391 | #endif |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 3392 | if( session == NULL ) |
| 3393 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 3394 | |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3395 | if( !omit_header ) |
| 3396 | { |
| 3397 | /* |
| 3398 | * Add Mbed TLS version identifier |
| 3399 | */ |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3400 | used += sizeof( ssl_serialized_session_header ); |
| 3401 | |
| 3402 | if( used <= buf_len ) |
| 3403 | { |
| 3404 | memcpy( p, ssl_serialized_session_header, |
| 3405 | sizeof( ssl_serialized_session_header ) ); |
| 3406 | p += sizeof( ssl_serialized_session_header ); |
| 3407 | } |
| 3408 | } |
| 3409 | |
| 3410 | /* |
| 3411 | * TLS version identifier |
| 3412 | */ |
| 3413 | used += 1; |
| 3414 | if( used <= buf_len ) |
| 3415 | { |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 3416 | *p++ = MBEDTLS_BYTE_0( session->tls_version ); |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3417 | } |
| 3418 | |
| 3419 | /* Forward to version-specific serialization routine. */ |
Jerry Yu | fca4d57 | 2022-07-21 10:37:48 +0800 | [diff] [blame] | 3420 | remaining_len = (buf_len >= used) ? buf_len - used : 0; |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 3421 | switch( session->tls_version ) |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3422 | { |
| 3423 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 3424 | case MBEDTLS_SSL_VERSION_TLS1_2: |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 3425 | used += ssl_tls12_session_save( session, p, remaining_len ); |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3426 | break; |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3427 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 3428 | |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 3429 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 3430 | case MBEDTLS_SSL_VERSION_TLS1_3: |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 3431 | ret = ssl_tls13_session_save( session, p, remaining_len, &out_len ); |
| 3432 | if( ret != 0 && ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ) |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 3433 | return( ret ); |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 3434 | used += out_len; |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 3435 | break; |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 3436 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 3437 | |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3438 | default: |
| 3439 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 3440 | } |
| 3441 | |
| 3442 | *olen = used; |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 3443 | if( used > buf_len ) |
| 3444 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 3445 | |
| 3446 | return( 0 ); |
| 3447 | } |
| 3448 | |
| 3449 | /* |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 3450 | * Public wrapper for ssl_session_save() |
| 3451 | */ |
| 3452 | int mbedtls_ssl_session_save( const mbedtls_ssl_session *session, |
| 3453 | unsigned char *buf, |
| 3454 | size_t buf_len, |
| 3455 | size_t *olen ) |
| 3456 | { |
| 3457 | return( ssl_session_save( session, 0, buf, buf_len, olen ) ); |
| 3458 | } |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 3459 | |
Jerry Yu | f1b23ca | 2022-02-18 11:48:47 +0800 | [diff] [blame] | 3460 | /* |
| 3461 | * Deserialize session, see mbedtls_ssl_session_save() for format. |
| 3462 | * |
| 3463 | * This internal version is wrapped by a public function that cleans up in |
| 3464 | * case of error, and has an extra option omit_header. |
| 3465 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 3466 | MBEDTLS_CHECK_RETURN_CRITICAL |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3467 | static int ssl_session_load( mbedtls_ssl_session *session, |
| 3468 | unsigned char omit_header, |
| 3469 | const unsigned char *buf, |
| 3470 | size_t len ) |
| 3471 | { |
| 3472 | const unsigned char *p = buf; |
| 3473 | const unsigned char * const end = buf + len; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 3474 | size_t remaining_len; |
| 3475 | |
| 3476 | |
| 3477 | if( session == NULL ) |
| 3478 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3479 | |
| 3480 | if( !omit_header ) |
| 3481 | { |
| 3482 | /* |
| 3483 | * Check Mbed TLS version identifier |
| 3484 | */ |
| 3485 | |
| 3486 | if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) ) |
| 3487 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3488 | |
| 3489 | if( memcmp( p, ssl_serialized_session_header, |
| 3490 | sizeof( ssl_serialized_session_header ) ) != 0 ) |
| 3491 | { |
| 3492 | return( MBEDTLS_ERR_SSL_VERSION_MISMATCH ); |
| 3493 | } |
| 3494 | p += sizeof( ssl_serialized_session_header ); |
| 3495 | } |
| 3496 | |
| 3497 | /* |
| 3498 | * TLS version identifier |
| 3499 | */ |
| 3500 | if( 1 > (size_t)( end - p ) ) |
| 3501 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 3502 | session->tls_version = 0x0300 | *p++; |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3503 | |
| 3504 | /* Dispatch according to TLS version. */ |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 3505 | remaining_len = ( end - p ); |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 3506 | switch( session->tls_version ) |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3507 | { |
| 3508 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 3509 | case MBEDTLS_SSL_VERSION_TLS1_2: |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 3510 | return( ssl_tls12_session_load( session, p, remaining_len ) ); |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3511 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 3512 | |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 3513 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 3514 | case MBEDTLS_SSL_VERSION_TLS1_3: |
| 3515 | return( ssl_tls13_session_load( session, p, remaining_len ) ); |
| 3516 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 3517 | |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3518 | default: |
| 3519 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3520 | } |
| 3521 | } |
| 3522 | |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 3523 | /* |
Manuel Pégourié-Gonnard | b9dfc9f | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 3524 | * Deserialize session: public wrapper for error cleaning |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 3525 | */ |
| 3526 | int mbedtls_ssl_session_load( mbedtls_ssl_session *session, |
| 3527 | const unsigned char *buf, |
| 3528 | size_t len ) |
| 3529 | { |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 3530 | int ret = ssl_session_load( session, 0, buf, len ); |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 3531 | |
| 3532 | if( ret != 0 ) |
| 3533 | mbedtls_ssl_session_free( session ); |
| 3534 | |
| 3535 | return( ret ); |
| 3536 | } |
| 3537 | |
| 3538 | /* |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3539 | * Perform a single step of the SSL handshake |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3540 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 3541 | MBEDTLS_CHECK_RETURN_CRITICAL |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 3542 | static int ssl_prepare_handshake_step( mbedtls_ssl_context *ssl ) |
| 3543 | { |
| 3544 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 3545 | |
Ronald Cron | 66dbf91 | 2022-02-02 15:33:46 +0100 | [diff] [blame] | 3546 | /* |
| 3547 | * 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] | 3548 | * that were written into the output buffer by the previous handshake step, |
| 3549 | * if the write to the network callback returned with the |
Ronald Cron | 66dbf91 | 2022-02-02 15:33:46 +0100 | [diff] [blame] | 3550 | * #MBEDTLS_ERR_SSL_WANT_WRITE error code. |
| 3551 | * We proceed to the next handshake step only when all data from the |
| 3552 | * previous one have been sent to the peer, thus we make sure that this is |
| 3553 | * the case here by calling `mbedtls_ssl_flush_output()`. The function may |
| 3554 | * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case |
| 3555 | * we have to wait before to go ahead. |
| 3556 | * In the case of TLS 1.3, handshake step handlers do not send data to the |
| 3557 | * peer. Data are only sent here and through |
| 3558 | * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an |
Andrzej Kurek | 5c65c57 | 2022-04-13 14:28:52 -0400 | [diff] [blame] | 3559 | * alert occurred. |
Ronald Cron | 66dbf91 | 2022-02-02 15:33:46 +0100 | [diff] [blame] | 3560 | */ |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 3561 | if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) |
| 3562 | return( ret ); |
| 3563 | |
| 3564 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3565 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 3566 | ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING ) |
| 3567 | { |
| 3568 | if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 ) |
| 3569 | return( ret ); |
| 3570 | } |
| 3571 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 3572 | |
| 3573 | return( ret ); |
| 3574 | } |
| 3575 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3576 | int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3577 | { |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 3578 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3579 | |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 3580 | if( ssl == NULL || |
| 3581 | ssl->conf == NULL || |
| 3582 | ssl->handshake == NULL || |
Paul Elliott | 27b0d94 | 2022-03-18 21:55:32 +0000 | [diff] [blame] | 3583 | mbedtls_ssl_is_handshake_over( ssl ) == 1 ) |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 3584 | { |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 3585 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 3586 | } |
| 3587 | |
| 3588 | ret = ssl_prepare_handshake_step( ssl ); |
| 3589 | if( ret != 0 ) |
| 3590 | return( ret ); |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 3591 | |
Jerry Yu | e704781 | 2021-09-13 19:26:39 +0800 | [diff] [blame] | 3592 | ret = mbedtls_ssl_handle_pending_alert( ssl ); |
| 3593 | if( ret != 0 ) |
| 3594 | goto cleanup; |
| 3595 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3596 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3597 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 3598 | { |
Ronald Cron | 27c85e7 | 2022-03-08 11:37:55 +0100 | [diff] [blame] | 3599 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "client state: %s", |
| 3600 | mbedtls_ssl_states_str( ssl->state ) ) ); |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 3601 | |
Ronald Cron | 9f0fba3 | 2022-02-10 16:45:15 +0100 | [diff] [blame] | 3602 | switch( ssl->state ) |
| 3603 | { |
| 3604 | case MBEDTLS_SSL_HELLO_REQUEST: |
| 3605 | ssl->state = MBEDTLS_SSL_CLIENT_HELLO; |
| 3606 | break; |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 3607 | |
Ronald Cron | 9f0fba3 | 2022-02-10 16:45:15 +0100 | [diff] [blame] | 3608 | case MBEDTLS_SSL_CLIENT_HELLO: |
| 3609 | ret = mbedtls_ssl_write_client_hello( ssl ); |
| 3610 | break; |
| 3611 | |
| 3612 | default: |
| 3613 | #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] | 3614 | if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 ) |
Ronald Cron | 9f0fba3 | 2022-02-10 16:45:15 +0100 | [diff] [blame] | 3615 | ret = mbedtls_ssl_tls13_handshake_client_step( ssl ); |
| 3616 | else |
| 3617 | ret = mbedtls_ssl_handshake_client_step( ssl ); |
| 3618 | #elif defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 3619 | ret = mbedtls_ssl_handshake_client_step( ssl ); |
| 3620 | #else |
| 3621 | ret = mbedtls_ssl_tls13_handshake_client_step( ssl ); |
| 3622 | #endif |
| 3623 | } |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 3624 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3625 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3626 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3627 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 3628 | { |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3629 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 3630 | if( mbedtls_ssl_conf_is_tls13_only( ssl->conf ) ) |
Jerry Yu | 2756193 | 2021-08-27 17:07:38 +0800 | [diff] [blame] | 3631 | ret = mbedtls_ssl_tls13_handshake_server_step( ssl ); |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3632 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 3633 | |
| 3634 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 3635 | if( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) ) |
| 3636 | ret = mbedtls_ssl_handshake_server_step( ssl ); |
| 3637 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 3638 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3639 | #endif |
| 3640 | |
Jerry Yu | e704781 | 2021-09-13 19:26:39 +0800 | [diff] [blame] | 3641 | if( ret != 0 ) |
| 3642 | { |
Jerry Yu | bbd5a3f | 2021-09-18 20:50:22 +0800 | [diff] [blame] | 3643 | /* handshake_step return error. And it is same |
| 3644 | * with alert_reason. |
| 3645 | */ |
Jerry Yu | 3bf1f97 | 2021-09-22 21:37:18 +0800 | [diff] [blame] | 3646 | if( ssl->send_alert ) |
Jerry Yu | e704781 | 2021-09-13 19:26:39 +0800 | [diff] [blame] | 3647 | { |
Jerry Yu | 3bf1f97 | 2021-09-22 21:37:18 +0800 | [diff] [blame] | 3648 | ret = mbedtls_ssl_handle_pending_alert( ssl ); |
Jerry Yu | e704781 | 2021-09-13 19:26:39 +0800 | [diff] [blame] | 3649 | goto cleanup; |
| 3650 | } |
| 3651 | } |
| 3652 | |
| 3653 | cleanup: |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3654 | return( ret ); |
| 3655 | } |
| 3656 | |
| 3657 | /* |
| 3658 | * Perform the SSL handshake |
| 3659 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3660 | int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl ) |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3661 | { |
| 3662 | int ret = 0; |
| 3663 | |
Hanno Becker | a817ea4 | 2020-10-20 15:20:23 +0100 | [diff] [blame] | 3664 | /* Sanity checks */ |
| 3665 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 3666 | if( ssl == NULL || ssl->conf == NULL ) |
| 3667 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3668 | |
Hanno Becker | a817ea4 | 2020-10-20 15:20:23 +0100 | [diff] [blame] | 3669 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3670 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 3671 | ( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL ) ) |
| 3672 | { |
| 3673 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use " |
| 3674 | "mbedtls_ssl_set_timer_cb() for DTLS" ) ); |
| 3675 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3676 | } |
| 3677 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 3678 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3679 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3680 | |
Hanno Becker | a817ea4 | 2020-10-20 15:20:23 +0100 | [diff] [blame] | 3681 | /* Main handshake loop */ |
Paul Elliott | 27b0d94 | 2022-03-18 21:55:32 +0000 | [diff] [blame] | 3682 | while( mbedtls_ssl_is_handshake_over( ssl ) == 0 ) |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3683 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3684 | ret = mbedtls_ssl_handshake_step( ssl ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3685 | |
| 3686 | if( ret != 0 ) |
| 3687 | break; |
| 3688 | } |
| 3689 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3690 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3691 | |
| 3692 | return( ret ); |
| 3693 | } |
| 3694 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3695 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 3696 | #if defined(MBEDTLS_SSL_SRV_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3697 | /* |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3698 | * Write HelloRequest to request renegotiation on server |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3699 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 3700 | MBEDTLS_CHECK_RETURN_CRITICAL |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3701 | static int ssl_write_hello_request( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3702 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3703 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3704 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3705 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) ); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3706 | |
| 3707 | ssl->out_msglen = 4; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3708 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 3709 | ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST; |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3710 | |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3711 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3712 | { |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3713 | 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] | 3714 | return( ret ); |
| 3715 | } |
| 3716 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3717 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) ); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3718 | |
| 3719 | return( 0 ); |
| 3720 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3721 | #endif /* MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3722 | |
| 3723 | /* |
| 3724 | * Actually renegotiate current connection, triggered by either: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3725 | * - any side: calling mbedtls_ssl_renegotiate(), |
| 3726 | * - client: receiving a HelloRequest during mbedtls_ssl_read(), |
| 3727 | * - 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] | 3728 | * the initial handshake is completed. |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3729 | * 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] | 3730 | * 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] | 3731 | */ |
Hanno Becker | 40cdaa1 | 2020-02-05 10:48:27 +0000 | [diff] [blame] | 3732 | int mbedtls_ssl_start_renegotiation( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3733 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3734 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3735 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3736 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3737 | |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3738 | if( ( ret = ssl_handshake_init( ssl ) ) != 0 ) |
| 3739 | return( ret ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3740 | |
Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 3741 | /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and |
| 3742 | * the ServerHello will have message_seq = 1" */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3743 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3744 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3745 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ) |
Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 3746 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3747 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 3748 | ssl->handshake->out_msg_seq = 1; |
| 3749 | else |
| 3750 | ssl->handshake->in_msg_seq = 1; |
Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 3751 | } |
| 3752 | #endif |
| 3753 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3754 | ssl->state = MBEDTLS_SSL_HELLO_REQUEST; |
| 3755 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3756 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3757 | if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3758 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3759 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3760 | return( ret ); |
| 3761 | } |
| 3762 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3763 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3764 | |
| 3765 | return( 0 ); |
| 3766 | } |
| 3767 | |
| 3768 | /* |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3769 | * Renegotiate current connection on client, |
| 3770 | * or request renegotiation on server |
| 3771 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3772 | int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3773 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3774 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3775 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 3776 | if( ssl == NULL || ssl->conf == NULL ) |
| 3777 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3778 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3779 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3780 | /* On server, just send the request */ |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3781 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3782 | { |
Paul Elliott | 27b0d94 | 2022-03-18 21:55:32 +0000 | [diff] [blame] | 3783 | if( mbedtls_ssl_is_handshake_over( ssl ) == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3784 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3785 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3786 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING; |
Manuel Pégourié-Gonnard | f07f421 | 2014-08-15 19:04:47 +0200 | [diff] [blame] | 3787 | |
| 3788 | /* Did we already try/start sending HelloRequest? */ |
| 3789 | if( ssl->out_left != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3790 | return( mbedtls_ssl_flush_output( ssl ) ); |
Manuel Pégourié-Gonnard | f07f421 | 2014-08-15 19:04:47 +0200 | [diff] [blame] | 3791 | |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3792 | return( ssl_write_hello_request( ssl ) ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3793 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3794 | #endif /* MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3795 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3796 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3797 | /* |
| 3798 | * On client, either start the renegotiation process or, |
| 3799 | * if already in progress, continue the handshake |
| 3800 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3801 | if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3802 | { |
Paul Elliott | 27b0d94 | 2022-03-18 21:55:32 +0000 | [diff] [blame] | 3803 | if( mbedtls_ssl_is_handshake_over( ssl ) == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3804 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3805 | |
Hanno Becker | 40cdaa1 | 2020-02-05 10:48:27 +0000 | [diff] [blame] | 3806 | if( ( ret = mbedtls_ssl_start_renegotiation( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3807 | { |
Hanno Becker | 40cdaa1 | 2020-02-05 10:48:27 +0000 | [diff] [blame] | 3808 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_start_renegotiation", ret ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3809 | return( ret ); |
| 3810 | } |
| 3811 | } |
| 3812 | else |
| 3813 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3814 | if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3815 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3816 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3817 | return( ret ); |
| 3818 | } |
| 3819 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3820 | #endif /* MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3821 | |
Paul Bakker | 37ce0ff | 2013-10-31 14:32:04 +0100 | [diff] [blame] | 3822 | return( ret ); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3823 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3824 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3825 | |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 3826 | void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3827 | { |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 3828 | mbedtls_ssl_handshake_params *handshake = ssl->handshake; |
| 3829 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3830 | if( handshake == NULL ) |
| 3831 | return; |
| 3832 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 3833 | #if defined(MBEDTLS_ECP_C) |
| 3834 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 3835 | if ( ssl->handshake->group_list_heap_allocated ) |
| 3836 | mbedtls_free( (void*) handshake->group_list ); |
| 3837 | handshake->group_list = NULL; |
| 3838 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
| 3839 | #endif /* MBEDTLS_ECP_C */ |
| 3840 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 3841 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 3842 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 3843 | if ( ssl->handshake->sig_algs_heap_allocated ) |
| 3844 | mbedtls_free( (void*) handshake->sig_algs ); |
| 3845 | handshake->sig_algs = NULL; |
| 3846 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
Xiaofei Bai | c234ecf | 2022-02-08 09:59:23 +0000 | [diff] [blame] | 3847 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 3848 | if( ssl->handshake->certificate_request_context ) |
| 3849 | { |
| 3850 | mbedtls_free( (void*) handshake->certificate_request_context ); |
| 3851 | } |
| 3852 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 3853 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 3854 | |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3855 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
| 3856 | if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 ) |
| 3857 | { |
Gilles Peskine | 8f97af7 | 2018-04-26 11:46:10 +0200 | [diff] [blame] | 3858 | ssl->conf->f_async_cancel( ssl ); |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3859 | handshake->async_in_progress = 0; |
| 3860 | } |
| 3861 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
| 3862 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 3863 | #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] | 3864 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 3865 | psa_hash_abort( &handshake->fin_sha256_psa ); |
| 3866 | #else |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 3867 | mbedtls_sha256_free( &handshake->fin_sha256 ); |
| 3868 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3869 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 3870 | #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] | 3871 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 3872 | psa_hash_abort( &handshake->fin_sha384_psa ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3873 | #else |
Andrzej Kurek | a242e83 | 2022-08-11 10:03:14 -0400 | [diff] [blame] | 3874 | mbedtls_sha512_free( &handshake->fin_sha384 ); |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 3875 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3876 | #endif |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 3877 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3878 | #if defined(MBEDTLS_DHM_C) |
| 3879 | mbedtls_dhm_free( &handshake->dhm_ctx ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3880 | #endif |
Neil Armstrong | f3f4641 | 2022-04-12 14:43:39 +0200 | [diff] [blame] | 3881 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3882 | mbedtls_ecdh_free( &handshake->ecdh_ctx ); |
Paul Bakker | 61d113b | 2013-07-04 11:51:43 +0200 | [diff] [blame] | 3883 | #endif |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 3884 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 3885 | mbedtls_ecjpake_free( &handshake->ecjpake_ctx ); |
Manuel Pégourié-Gonnard | 77c0646 | 2015-09-17 13:59:49 +0200 | [diff] [blame] | 3886 | #if defined(MBEDTLS_SSL_CLI_C) |
| 3887 | mbedtls_free( handshake->ecjpake_cache ); |
| 3888 | handshake->ecjpake_cache = NULL; |
| 3889 | handshake->ecjpake_cache_len = 0; |
| 3890 | #endif |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 3891 | #endif |
Paul Bakker | 61d113b | 2013-07-04 11:51:43 +0200 | [diff] [blame] | 3892 | |
Janos Follath | 4ae5c29 | 2016-02-10 11:27:43 +0000 | [diff] [blame] | 3893 | #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ |
| 3894 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 3895 | /* explicit void pointer cast for buggy MS compiler */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3896 | mbedtls_free( (void *) handshake->curves ); |
Manuel Pégourié-Gonnard | d09453c | 2013-09-23 19:11:32 +0200 | [diff] [blame] | 3897 | #endif |
| 3898 | |
Ronald Cron | 73fe8df | 2022-10-05 14:31:43 +0200 | [diff] [blame] | 3899 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED) |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 3900 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 3901 | if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) ) |
| 3902 | { |
| 3903 | /* The maintenance of the external PSK key slot is the |
| 3904 | * user's responsibility. */ |
| 3905 | if( ssl->handshake->psk_opaque_is_internal ) |
| 3906 | { |
| 3907 | psa_destroy_key( ssl->handshake->psk_opaque ); |
| 3908 | ssl->handshake->psk_opaque_is_internal = 0; |
| 3909 | } |
| 3910 | ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT; |
| 3911 | } |
Neil Armstrong | e952a30 | 2022-05-03 10:22:14 +0200 | [diff] [blame] | 3912 | #else |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 3913 | if( handshake->psk != NULL ) |
| 3914 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 3915 | mbedtls_platform_zeroize( handshake->psk, handshake->psk_len ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 3916 | mbedtls_free( handshake->psk ); |
| 3917 | } |
Neil Armstrong | e952a30 | 2022-05-03 10:22:14 +0200 | [diff] [blame] | 3918 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Ronald Cron | 73fe8df | 2022-10-05 14:31:43 +0200 | [diff] [blame] | 3919 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */ |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 3920 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3921 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 3922 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Manuel Pégourié-Gonnard | 8372454 | 2013-09-24 22:30:56 +0200 | [diff] [blame] | 3923 | /* |
| 3924 | * Free only the linked list wrapper, not the keys themselves |
| 3925 | * since the belong to the SNI callback |
| 3926 | */ |
Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 3927 | ssl_key_cert_free( handshake->sni_key_cert ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3928 | #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] | 3929 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3930 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Manuel Pégourié-Gonnard | 6b7301c | 2017-08-15 12:08:45 +0200 | [diff] [blame] | 3931 | mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx ); |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 3932 | if( handshake->ecrs_peer_cert != NULL ) |
| 3933 | { |
| 3934 | mbedtls_x509_crt_free( handshake->ecrs_peer_cert ); |
| 3935 | mbedtls_free( handshake->ecrs_peer_cert ); |
| 3936 | } |
Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 3937 | #endif |
| 3938 | |
Hanno Becker | 7517312 | 2019-02-06 16:18:31 +0000 | [diff] [blame] | 3939 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 3940 | !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 3941 | mbedtls_pk_free( &handshake->peer_pubkey ); |
| 3942 | #endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 3943 | |
XiaokangQian | 9b93c0d | 2022-02-09 06:02:25 +0000 | [diff] [blame] | 3944 | #if defined(MBEDTLS_SSL_CLI_C) && \ |
| 3945 | ( defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3) ) |
| 3946 | mbedtls_free( handshake->cookie ); |
| 3947 | #endif /* MBEDTLS_SSL_CLI_C && |
| 3948 | ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */ |
XiaokangQian | 8499b6c | 2022-01-27 09:00:11 +0000 | [diff] [blame] | 3949 | |
| 3950 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 533ab5f | 2020-02-05 10:49:13 +0000 | [diff] [blame] | 3951 | mbedtls_ssl_flight_free( handshake->flight ); |
| 3952 | mbedtls_ssl_buffering_free( ssl ); |
XiaokangQian | 8499b6c | 2022-01-27 09:00:11 +0000 | [diff] [blame] | 3953 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 3954 | |
Ronald Cron | f12b81d | 2022-03-15 10:42:41 +0100 | [diff] [blame] | 3955 | #if defined(MBEDTLS_ECDH_C) && \ |
| 3956 | ( defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3) ) |
Neil Armstrong | f716a70 | 2022-04-04 11:23:46 +0200 | [diff] [blame] | 3957 | if( handshake->ecdh_psa_privkey_is_external == 0 ) |
Neil Armstrong | 8113d25 | 2022-03-23 10:57:04 +0100 | [diff] [blame] | 3958 | psa_destroy_key( handshake->ecdh_psa_privkey ); |
Hanno Becker | 4a63ed4 | 2019-01-08 11:39:35 +0000 | [diff] [blame] | 3959 | #endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */ |
| 3960 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3961 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | a1a568c | 2021-11-09 10:17:21 +0800 | [diff] [blame] | 3962 | mbedtls_ssl_transform_free( handshake->transform_handshake ); |
| 3963 | mbedtls_ssl_transform_free( handshake->transform_earlydata ); |
Jerry Yu | ba9c727 | 2021-10-30 11:54:10 +0800 | [diff] [blame] | 3964 | mbedtls_free( handshake->transform_earlydata ); |
| 3965 | mbedtls_free( handshake->transform_handshake ); |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3966 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Jerry Yu | ba9c727 | 2021-10-30 11:54:10 +0800 | [diff] [blame] | 3967 | |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 3968 | |
| 3969 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 3970 | /* If the buffers are too big - reallocate. Because of the way Mbed TLS |
| 3971 | * processes datagrams and the fact that a datagram is allowed to have |
| 3972 | * several records in it, it is possible that the I/O buffers are not |
| 3973 | * empty at this stage */ |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 3974 | handle_buffer_resizing( ssl, 1, mbedtls_ssl_get_input_buflen( ssl ), |
| 3975 | mbedtls_ssl_get_output_buflen( ssl ) ); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 3976 | #endif |
Hanno Becker | 3aa186f | 2021-08-10 09:24:19 +0100 | [diff] [blame] | 3977 | |
Jerry Yu | ba9c727 | 2021-10-30 11:54:10 +0800 | [diff] [blame] | 3978 | /* mbedtls_platform_zeroize MUST be last one in this function */ |
| 3979 | mbedtls_platform_zeroize( handshake, |
| 3980 | sizeof( mbedtls_ssl_handshake_params ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3981 | } |
| 3982 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3983 | void mbedtls_ssl_session_free( mbedtls_ssl_session *session ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3984 | { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3985 | if( session == NULL ) |
| 3986 | return; |
| 3987 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3988 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Hanno Becker | 1294a0b | 2019-02-05 12:38:15 +0000 | [diff] [blame] | 3989 | ssl_clear_peer_cert( session ); |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 3990 | #endif |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 3991 | |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 3992 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
Xiaokang Qian | bc663a0 | 2022-10-09 11:14:39 +0000 | [diff] [blame] | 3993 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \ |
| 3994 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Xiaokang Qian | 281fd1b | 2022-09-20 11:35:41 +0000 | [diff] [blame] | 3995 | mbedtls_free( session->hostname ); |
| 3996 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3997 | mbedtls_free( session->ticket ); |
Paul Bakker | a503a63 | 2013-08-14 13:48:06 +0200 | [diff] [blame] | 3998 | #endif |
Manuel Pégourié-Gonnard | 75d4401 | 2013-08-02 14:44:04 +0200 | [diff] [blame] | 3999 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 4000 | mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4001 | } |
| 4002 | |
Manuel Pégourié-Gonnard | 5c0e377 | 2019-07-23 16:13:17 +0200 | [diff] [blame] | 4003 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 4004 | |
| 4005 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 4006 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u |
| 4007 | #else |
| 4008 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u |
| 4009 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 4010 | |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 4011 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 4012 | |
| 4013 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 4014 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u |
| 4015 | #else |
| 4016 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u |
| 4017 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
| 4018 | |
| 4019 | #if defined(MBEDTLS_SSL_ALPN) |
| 4020 | #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u |
| 4021 | #else |
| 4022 | #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u |
| 4023 | #endif /* MBEDTLS_SSL_ALPN */ |
| 4024 | |
| 4025 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0 |
| 4026 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1 |
| 4027 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2 |
| 4028 | #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3 |
| 4029 | |
| 4030 | #define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \ |
| 4031 | ( (uint32_t) ( \ |
| 4032 | ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \ |
| 4033 | ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \ |
| 4034 | ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \ |
| 4035 | ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \ |
| 4036 | 0u ) ) |
| 4037 | |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4038 | static unsigned char ssl_serialized_context_header[] = { |
| 4039 | MBEDTLS_VERSION_MAJOR, |
| 4040 | MBEDTLS_VERSION_MINOR, |
| 4041 | MBEDTLS_VERSION_PATCH, |
Joe Subbiani | 2194dc4 | 2021-07-14 12:31:31 +0100 | [diff] [blame] | 4042 | MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ), |
| 4043 | MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ), |
| 4044 | MBEDTLS_BYTE_2( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ), |
| 4045 | MBEDTLS_BYTE_1( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ), |
| 4046 | MBEDTLS_BYTE_0( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ), |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4047 | }; |
| 4048 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4049 | /* |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 4050 | * Serialize a full SSL context |
Manuel Pégourié-Gonnard | 00400c2 | 2019-07-10 14:58:45 +0200 | [diff] [blame] | 4051 | * |
| 4052 | * The format of the serialized data is: |
| 4053 | * (in the presentation language of TLS, RFC 8446 section 3) |
| 4054 | * |
| 4055 | * // header |
| 4056 | * opaque mbedtls_version[3]; // major, minor, patch |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4057 | * opaque context_format[5]; // version-specific field determining |
Manuel Pégourié-Gonnard | 00400c2 | 2019-07-10 14:58:45 +0200 | [diff] [blame] | 4058 | * // the format of the remaining |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4059 | * // serialized data. |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 4060 | * Note: When updating the format, remember to keep these |
| 4061 | * 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] | 4062 | * |
| 4063 | * // session sub-structure |
| 4064 | * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save() |
| 4065 | * // transform sub-structure |
| 4066 | * uint8 random[64]; // ServerHello.random+ClientHello.random |
| 4067 | * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value |
| 4068 | * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use |
| 4069 | * // fields from ssl_context |
| 4070 | * uint32 badmac_seen; // DTLS: number of records with failing MAC |
| 4071 | * uint64 in_window_top; // DTLS: last validated record seq_num |
| 4072 | * uint64 in_window; // DTLS: bitmask for replay protection |
| 4073 | * uint8 disable_datagram_packing; // DTLS: only one record per datagram |
| 4074 | * uint64 cur_out_ctr; // Record layer: outgoing sequence number |
| 4075 | * uint16 mtu; // DTLS: path mtu (max outgoing fragment size) |
| 4076 | * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol |
| 4077 | * |
| 4078 | * Note that many fields of the ssl_context or sub-structures are not |
| 4079 | * serialized, as they fall in one of the following categories: |
| 4080 | * |
| 4081 | * 1. forced value (eg in_left must be 0) |
| 4082 | * 2. pointer to dynamically-allocated memory (eg session, transform) |
| 4083 | * 3. value can be re-derived from other data (eg session keys from MS) |
| 4084 | * 4. value was temporary (eg content of input buffer) |
| 4085 | * 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] | 4086 | */ |
| 4087 | int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl, |
| 4088 | unsigned char *buf, |
| 4089 | size_t buf_len, |
| 4090 | size_t *olen ) |
| 4091 | { |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4092 | unsigned char *p = buf; |
| 4093 | size_t used = 0; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4094 | size_t session_len; |
| 4095 | int ret = 0; |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4096 | |
Manuel Pégourié-Gonnard | 1aaf669 | 2019-07-10 14:14:05 +0200 | [diff] [blame] | 4097 | /* |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 4098 | * Enforce usage restrictions, see "return BAD_INPUT_DATA" in |
| 4099 | * this function's documentation. |
| 4100 | * |
| 4101 | * These are due to assumptions/limitations in the implementation. Some of |
| 4102 | * them are likely to stay (no handshake in progress) some might go away |
| 4103 | * (only DTLS) but are currently used to simplify the implementation. |
Manuel Pégourié-Gonnard | 1aaf669 | 2019-07-10 14:14:05 +0200 | [diff] [blame] | 4104 | */ |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 4105 | /* The initial handshake must be over */ |
Paul Elliott | 27b0d94 | 2022-03-18 21:55:32 +0000 | [diff] [blame] | 4106 | if( mbedtls_ssl_is_handshake_over( ssl ) == 0 ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 4107 | { |
| 4108 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Initial handshake isn't over" ) ); |
Manuel Pégourié-Gonnard | 1aaf669 | 2019-07-10 14:14:05 +0200 | [diff] [blame] | 4109 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 4110 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 4111 | if( ssl->handshake != NULL ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 4112 | { |
| 4113 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Handshake isn't completed" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 4114 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 4115 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 4116 | /* Double-check that sub-structures are indeed ready */ |
| 4117 | if( ssl->transform == NULL || ssl->session == NULL ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 4118 | { |
| 4119 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Serialised structures aren't ready" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 4120 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 4121 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 4122 | /* There must be no pending incoming or outgoing data */ |
| 4123 | if( mbedtls_ssl_check_pending( ssl ) != 0 ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 4124 | { |
| 4125 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending incoming data" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 4126 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 4127 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 4128 | if( ssl->out_left != 0 ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 4129 | { |
| 4130 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending outgoing data" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 4131 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 4132 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 4133 | /* Protocol must be DLTS, not TLS */ |
| 4134 | if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 4135 | { |
| 4136 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only DTLS is supported" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 4137 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 4138 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 4139 | /* Version must be 1.2 */ |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 4140 | if( ssl->tls_version != MBEDTLS_SSL_VERSION_TLS1_2 ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 4141 | { |
| 4142 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only version 1.2 supported" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 4143 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 4144 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 4145 | /* We must be using an AEAD ciphersuite */ |
| 4146 | if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 4147 | { |
| 4148 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only AEAD ciphersuites supported" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 4149 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 4150 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 4151 | /* Renegotiation must not be enabled */ |
| 4152 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 4153 | if( ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 4154 | { |
| 4155 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Renegotiation must not be enabled" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 4156 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 4157 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 4158 | #endif |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 4159 | |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4160 | /* |
| 4161 | * Version and format identifier |
| 4162 | */ |
| 4163 | used += sizeof( ssl_serialized_context_header ); |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 4164 | |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4165 | if( used <= buf_len ) |
| 4166 | { |
| 4167 | memcpy( p, ssl_serialized_context_header, |
| 4168 | sizeof( ssl_serialized_context_header ) ); |
| 4169 | p += sizeof( ssl_serialized_context_header ); |
| 4170 | } |
| 4171 | |
| 4172 | /* |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4173 | * Session (length + data) |
| 4174 | */ |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 4175 | 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] | 4176 | if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ) |
| 4177 | return( ret ); |
| 4178 | |
| 4179 | used += 4 + session_len; |
| 4180 | if( used <= buf_len ) |
| 4181 | { |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 4182 | MBEDTLS_PUT_UINT32_BE( session_len, p, 0 ); |
| 4183 | p += 4; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4184 | |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 4185 | ret = ssl_session_save( ssl->session, 1, |
| 4186 | p, session_len, &session_len ); |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4187 | if( ret != 0 ) |
| 4188 | return( ret ); |
| 4189 | |
| 4190 | p += session_len; |
| 4191 | } |
| 4192 | |
| 4193 | /* |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4194 | * Transform |
| 4195 | */ |
| 4196 | used += sizeof( ssl->transform->randbytes ); |
| 4197 | if( used <= buf_len ) |
| 4198 | { |
| 4199 | memcpy( p, ssl->transform->randbytes, |
| 4200 | sizeof( ssl->transform->randbytes ) ); |
| 4201 | p += sizeof( ssl->transform->randbytes ); |
| 4202 | } |
| 4203 | |
| 4204 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 4205 | used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len; |
| 4206 | if( used <= buf_len ) |
| 4207 | { |
| 4208 | *p++ = ssl->transform->in_cid_len; |
| 4209 | memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len ); |
| 4210 | p += ssl->transform->in_cid_len; |
| 4211 | |
| 4212 | *p++ = ssl->transform->out_cid_len; |
| 4213 | memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len ); |
| 4214 | p += ssl->transform->out_cid_len; |
| 4215 | } |
| 4216 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 4217 | |
| 4218 | /* |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4219 | * Saved fields from top-level ssl_context structure |
| 4220 | */ |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4221 | used += 4; |
| 4222 | if( used <= buf_len ) |
| 4223 | { |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 4224 | MBEDTLS_PUT_UINT32_BE( ssl->badmac_seen, p, 0 ); |
| 4225 | p += 4; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4226 | } |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4227 | |
| 4228 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 4229 | used += 16; |
| 4230 | if( used <= buf_len ) |
| 4231 | { |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 4232 | MBEDTLS_PUT_UINT64_BE( ssl->in_window_top, p, 0 ); |
| 4233 | p += 8; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4234 | |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 4235 | MBEDTLS_PUT_UINT64_BE( ssl->in_window, p, 0 ); |
| 4236 | p += 8; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4237 | } |
| 4238 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
| 4239 | |
| 4240 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4241 | used += 1; |
| 4242 | if( used <= buf_len ) |
| 4243 | { |
| 4244 | *p++ = ssl->disable_datagram_packing; |
| 4245 | } |
| 4246 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 4247 | |
Jerry Yu | ae0b2e2 | 2021-10-08 15:21:19 +0800 | [diff] [blame] | 4248 | used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4249 | if( used <= buf_len ) |
| 4250 | { |
Jerry Yu | ae0b2e2 | 2021-10-08 15:21:19 +0800 | [diff] [blame] | 4251 | memcpy( p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN ); |
| 4252 | p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4253 | } |
| 4254 | |
| 4255 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4256 | used += 2; |
| 4257 | if( used <= buf_len ) |
| 4258 | { |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 4259 | MBEDTLS_PUT_UINT16_BE( ssl->mtu, p, 0 ); |
| 4260 | p += 2; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4261 | } |
| 4262 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 4263 | |
| 4264 | #if defined(MBEDTLS_SSL_ALPN) |
| 4265 | { |
| 4266 | const uint8_t alpn_len = ssl->alpn_chosen |
Manuel Pégourié-Gonnard | f041f4e | 2019-07-24 00:58:27 +0200 | [diff] [blame] | 4267 | ? (uint8_t) strlen( ssl->alpn_chosen ) |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4268 | : 0; |
| 4269 | |
| 4270 | used += 1 + alpn_len; |
| 4271 | if( used <= buf_len ) |
| 4272 | { |
| 4273 | *p++ = alpn_len; |
| 4274 | |
| 4275 | if( ssl->alpn_chosen != NULL ) |
| 4276 | { |
| 4277 | memcpy( p, ssl->alpn_chosen, alpn_len ); |
| 4278 | p += alpn_len; |
| 4279 | } |
| 4280 | } |
| 4281 | } |
| 4282 | #endif /* MBEDTLS_SSL_ALPN */ |
| 4283 | |
| 4284 | /* |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4285 | * Done |
| 4286 | */ |
| 4287 | *olen = used; |
| 4288 | |
| 4289 | if( used > buf_len ) |
| 4290 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 4291 | |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4292 | MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used ); |
| 4293 | |
Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 4294 | return( mbedtls_ssl_session_reset_int( ssl, 0 ) ); |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 4295 | } |
| 4296 | |
| 4297 | /* |
Manuel Pégourié-Gonnard | b9dfc9f | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 4298 | * Deserialize context, see mbedtls_ssl_context_save() for format. |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4299 | * |
| 4300 | * This internal version is wrapped by a public function that cleans up in |
| 4301 | * case of error. |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 4302 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 4303 | MBEDTLS_CHECK_RETURN_CRITICAL |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4304 | static int ssl_context_load( mbedtls_ssl_context *ssl, |
| 4305 | const unsigned char *buf, |
| 4306 | size_t len ) |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 4307 | { |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4308 | const unsigned char *p = buf; |
| 4309 | const unsigned char * const end = buf + len; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4310 | size_t session_len; |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 4311 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Andrzej Kurek | 2d59dbc | 2022-10-13 08:34:38 -0400 | [diff] [blame] | 4312 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Andrzej Kurek | 894edde | 2022-09-29 06:31:14 -0400 | [diff] [blame] | 4313 | tls_prf_fn prf_func = NULL; |
Andrzej Kurek | 2d59dbc | 2022-10-13 08:34:38 -0400 | [diff] [blame] | 4314 | #endif |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4315 | |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 4316 | /* |
| 4317 | * The context should have been freshly setup or reset. |
| 4318 | * Give the user an error in case of obvious misuse. |
Manuel Pégourié-Gonnard | 4ca930f | 2019-07-26 16:31:53 +0200 | [diff] [blame] | 4319 | * (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] | 4320 | * renegotiating, or if the user mistakenly loaded a session first.) |
| 4321 | */ |
| 4322 | if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST || |
| 4323 | ssl->session != NULL ) |
| 4324 | { |
| 4325 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4326 | } |
| 4327 | |
| 4328 | /* |
| 4329 | * We can't check that the config matches the initial one, but we can at |
| 4330 | * least check it matches the requirements for serializing. |
| 4331 | */ |
| 4332 | if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM || |
Glenn Strauss | 2dfcea2 | 2022-03-14 17:26:42 -0400 | [diff] [blame] | 4333 | ssl->conf->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2 || |
| 4334 | ssl->conf->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 || |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 4335 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | 9a96fd7 | 2019-07-23 17:11:24 +0200 | [diff] [blame] | 4336 | ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED || |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 4337 | #endif |
Manuel Pégourié-Gonnard | 9a96fd7 | 2019-07-23 17:11:24 +0200 | [diff] [blame] | 4338 | 0 ) |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 4339 | { |
| 4340 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4341 | } |
| 4342 | |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4343 | MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len ); |
| 4344 | |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4345 | /* |
| 4346 | * Check version identifier |
| 4347 | */ |
| 4348 | if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) ) |
| 4349 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4350 | |
| 4351 | if( memcmp( p, ssl_serialized_context_header, |
| 4352 | sizeof( ssl_serialized_context_header ) ) != 0 ) |
| 4353 | { |
| 4354 | return( MBEDTLS_ERR_SSL_VERSION_MISMATCH ); |
| 4355 | } |
| 4356 | p += sizeof( ssl_serialized_context_header ); |
| 4357 | |
| 4358 | /* |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4359 | * Session |
| 4360 | */ |
| 4361 | if( (size_t)( end - p ) < 4 ) |
| 4362 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4363 | |
| 4364 | session_len = ( (size_t) p[0] << 24 ) | |
| 4365 | ( (size_t) p[1] << 16 ) | |
| 4366 | ( (size_t) p[2] << 8 ) | |
| 4367 | ( (size_t) p[3] ); |
| 4368 | p += 4; |
| 4369 | |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 4370 | /* This has been allocated by ssl_handshake_init(), called by |
Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 4371 | * 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] | 4372 | ssl->session = ssl->session_negotiate; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4373 | ssl->session_in = ssl->session; |
| 4374 | ssl->session_out = ssl->session; |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 4375 | ssl->session_negotiate = NULL; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4376 | |
| 4377 | if( (size_t)( end - p ) < session_len ) |
| 4378 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4379 | |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 4380 | ret = ssl_session_load( ssl->session, 1, p, session_len ); |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4381 | if( ret != 0 ) |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 4382 | { |
| 4383 | mbedtls_ssl_session_free( ssl->session ); |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4384 | return( ret ); |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 4385 | } |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4386 | |
| 4387 | p += session_len; |
| 4388 | |
| 4389 | /* |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4390 | * Transform |
| 4391 | */ |
| 4392 | |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 4393 | /* This has been allocated by ssl_handshake_init(), called by |
Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 4394 | * 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] | 4395 | ssl->transform = ssl->transform_negotiate; |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4396 | ssl->transform_in = ssl->transform; |
| 4397 | ssl->transform_out = ssl->transform; |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 4398 | ssl->transform_negotiate = NULL; |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4399 | |
Andrzej Kurek | 2d59dbc | 2022-10-13 08:34:38 -0400 | [diff] [blame] | 4400 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Andrzej Kurek | 894edde | 2022-09-29 06:31:14 -0400 | [diff] [blame] | 4401 | prf_func = ssl_tls12prf_from_cs( ssl->session->ciphersuite ); |
| 4402 | if( prf_func == NULL ) |
| 4403 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4404 | |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4405 | /* Read random bytes and populate structure */ |
| 4406 | if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) ) |
| 4407 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Andrzej Kurek | 2d59dbc | 2022-10-13 08:34:38 -0400 | [diff] [blame] | 4408 | |
Hanno Becker | bd25755 | 2021-03-22 06:59:27 +0000 | [diff] [blame] | 4409 | ret = ssl_tls12_populate_transform( ssl->transform, |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4410 | ssl->session->ciphersuite, |
| 4411 | ssl->session->master, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 4412 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4413 | ssl->session->encrypt_then_mac, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 4414 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Andrzej Kurek | 894edde | 2022-09-29 06:31:14 -0400 | [diff] [blame] | 4415 | prf_func, |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4416 | p, /* currently pointing to randbytes */ |
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 4417 | 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] | 4418 | ssl->conf->endpoint, |
| 4419 | ssl ); |
| 4420 | if( ret != 0 ) |
| 4421 | return( ret ); |
Jerry Yu | 840fbb2 | 2022-02-17 14:59:29 +0800 | [diff] [blame] | 4422 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4423 | p += sizeof( ssl->transform->randbytes ); |
| 4424 | |
| 4425 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 4426 | /* Read connection IDs and store them */ |
| 4427 | if( (size_t)( end - p ) < 1 ) |
| 4428 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4429 | |
| 4430 | ssl->transform->in_cid_len = *p++; |
| 4431 | |
Manuel Pégourié-Gonnard | 5ea13b8 | 2019-07-23 15:02:54 +0200 | [diff] [blame] | 4432 | 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] | 4433 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4434 | |
| 4435 | memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len ); |
| 4436 | p += ssl->transform->in_cid_len; |
| 4437 | |
| 4438 | ssl->transform->out_cid_len = *p++; |
| 4439 | |
| 4440 | if( (size_t)( end - p ) < ssl->transform->out_cid_len ) |
| 4441 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4442 | |
| 4443 | memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len ); |
| 4444 | p += ssl->transform->out_cid_len; |
| 4445 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 4446 | |
| 4447 | /* |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4448 | * Saved fields from top-level ssl_context structure |
| 4449 | */ |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4450 | if( (size_t)( end - p ) < 4 ) |
| 4451 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4452 | |
| 4453 | ssl->badmac_seen = ( (uint32_t) p[0] << 24 ) | |
| 4454 | ( (uint32_t) p[1] << 16 ) | |
| 4455 | ( (uint32_t) p[2] << 8 ) | |
| 4456 | ( (uint32_t) p[3] ); |
| 4457 | p += 4; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4458 | |
| 4459 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 4460 | if( (size_t)( end - p ) < 16 ) |
| 4461 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4462 | |
| 4463 | ssl->in_window_top = ( (uint64_t) p[0] << 56 ) | |
| 4464 | ( (uint64_t) p[1] << 48 ) | |
| 4465 | ( (uint64_t) p[2] << 40 ) | |
| 4466 | ( (uint64_t) p[3] << 32 ) | |
| 4467 | ( (uint64_t) p[4] << 24 ) | |
| 4468 | ( (uint64_t) p[5] << 16 ) | |
| 4469 | ( (uint64_t) p[6] << 8 ) | |
| 4470 | ( (uint64_t) p[7] ); |
| 4471 | p += 8; |
| 4472 | |
| 4473 | ssl->in_window = ( (uint64_t) p[0] << 56 ) | |
| 4474 | ( (uint64_t) p[1] << 48 ) | |
| 4475 | ( (uint64_t) p[2] << 40 ) | |
| 4476 | ( (uint64_t) p[3] << 32 ) | |
| 4477 | ( (uint64_t) p[4] << 24 ) | |
| 4478 | ( (uint64_t) p[5] << 16 ) | |
| 4479 | ( (uint64_t) p[6] << 8 ) | |
| 4480 | ( (uint64_t) p[7] ); |
| 4481 | p += 8; |
| 4482 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
| 4483 | |
| 4484 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4485 | if( (size_t)( end - p ) < 1 ) |
| 4486 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4487 | |
| 4488 | ssl->disable_datagram_packing = *p++; |
| 4489 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 4490 | |
Jerry Yu | d9a94fe | 2021-09-28 18:58:59 +0800 | [diff] [blame] | 4491 | if( (size_t)( end - p ) < sizeof( ssl->cur_out_ctr ) ) |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4492 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jerry Yu | d9a94fe | 2021-09-28 18:58:59 +0800 | [diff] [blame] | 4493 | memcpy( ssl->cur_out_ctr, p, sizeof( ssl->cur_out_ctr ) ); |
| 4494 | p += sizeof( ssl->cur_out_ctr ); |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4495 | |
| 4496 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4497 | if( (size_t)( end - p ) < 2 ) |
| 4498 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4499 | |
| 4500 | ssl->mtu = ( p[0] << 8 ) | p[1]; |
| 4501 | p += 2; |
| 4502 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 4503 | |
| 4504 | #if defined(MBEDTLS_SSL_ALPN) |
| 4505 | { |
| 4506 | uint8_t alpn_len; |
| 4507 | const char **cur; |
| 4508 | |
| 4509 | if( (size_t)( end - p ) < 1 ) |
| 4510 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4511 | |
| 4512 | alpn_len = *p++; |
| 4513 | |
| 4514 | if( alpn_len != 0 && ssl->conf->alpn_list != NULL ) |
| 4515 | { |
| 4516 | /* alpn_chosen should point to an item in the configured list */ |
| 4517 | for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ ) |
| 4518 | { |
| 4519 | if( strlen( *cur ) == alpn_len && |
| 4520 | memcmp( p, cur, alpn_len ) == 0 ) |
| 4521 | { |
| 4522 | ssl->alpn_chosen = *cur; |
| 4523 | break; |
| 4524 | } |
| 4525 | } |
| 4526 | } |
| 4527 | |
| 4528 | /* can only happen on conf mismatch */ |
| 4529 | if( alpn_len != 0 && ssl->alpn_chosen == NULL ) |
| 4530 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4531 | |
| 4532 | p += alpn_len; |
| 4533 | } |
| 4534 | #endif /* MBEDTLS_SSL_ALPN */ |
| 4535 | |
| 4536 | /* |
Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 4537 | * Forced fields from top-level ssl_context structure |
| 4538 | * |
| 4539 | * Most of them already set to the correct value by mbedtls_ssl_init() and |
| 4540 | * mbedtls_ssl_reset(), so we only need to set the remaining ones. |
| 4541 | */ |
| 4542 | ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER; |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 4543 | ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 4544 | |
Hanno Becker | 361b10d | 2019-08-30 10:42:49 +0100 | [diff] [blame] | 4545 | /* Adjust pointers for header fields of outgoing records to |
| 4546 | * the given transform, accounting for explicit IV and CID. */ |
Hanno Becker | 3e6f8ab | 2020-02-05 10:40:57 +0000 | [diff] [blame] | 4547 | mbedtls_ssl_update_out_pointers( ssl, ssl->transform ); |
Hanno Becker | 361b10d | 2019-08-30 10:42:49 +0100 | [diff] [blame] | 4548 | |
Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 4549 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4550 | ssl->in_epoch = 1; |
| 4551 | #endif |
| 4552 | |
| 4553 | /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated, |
| 4554 | * 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] | 4555 | * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform() |
| 4556 | * inappropriately. */ |
Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 4557 | if( ssl->handshake != NULL ) |
| 4558 | { |
| 4559 | mbedtls_ssl_handshake_free( ssl ); |
| 4560 | mbedtls_free( ssl->handshake ); |
| 4561 | ssl->handshake = NULL; |
| 4562 | } |
| 4563 | |
| 4564 | /* |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4565 | * Done - should have consumed entire buffer |
| 4566 | */ |
| 4567 | if( p != end ) |
| 4568 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 4569 | |
| 4570 | return( 0 ); |
| 4571 | } |
| 4572 | |
| 4573 | /* |
Manuel Pégourié-Gonnard | b9dfc9f | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 4574 | * Deserialize context: public wrapper for error cleaning |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4575 | */ |
| 4576 | int mbedtls_ssl_context_load( mbedtls_ssl_context *context, |
| 4577 | const unsigned char *buf, |
| 4578 | size_t len ) |
| 4579 | { |
| 4580 | int ret = ssl_context_load( context, buf, len ); |
| 4581 | |
| 4582 | if( ret != 0 ) |
| 4583 | mbedtls_ssl_free( context ); |
| 4584 | |
| 4585 | return( ret ); |
| 4586 | } |
Manuel Pégourié-Gonnard | 5c0e377 | 2019-07-23 16:13:17 +0200 | [diff] [blame] | 4587 | #endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */ |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4588 | |
| 4589 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4590 | * Free an SSL context |
| 4591 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4592 | void mbedtls_ssl_free( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4593 | { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 4594 | if( ssl == NULL ) |
| 4595 | return; |
| 4596 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4597 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4598 | |
Manuel Pégourié-Gonnard | 7ee6f0e | 2014-02-13 10:54:07 +0100 | [diff] [blame] | 4599 | if( ssl->out_buf != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4600 | { |
sander-visser | b8aa207 | 2020-05-06 22:05:13 +0200 | [diff] [blame] | 4601 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 4602 | size_t out_buf_len = ssl->out_buf_len; |
| 4603 | #else |
| 4604 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; |
| 4605 | #endif |
| 4606 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 4607 | mbedtls_platform_zeroize( ssl->out_buf, out_buf_len ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4608 | mbedtls_free( ssl->out_buf ); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 4609 | ssl->out_buf = NULL; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4610 | } |
| 4611 | |
Manuel Pégourié-Gonnard | 7ee6f0e | 2014-02-13 10:54:07 +0100 | [diff] [blame] | 4612 | if( ssl->in_buf != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4613 | { |
sander-visser | b8aa207 | 2020-05-06 22:05:13 +0200 | [diff] [blame] | 4614 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 4615 | size_t in_buf_len = ssl->in_buf_len; |
| 4616 | #else |
| 4617 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; |
| 4618 | #endif |
| 4619 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 4620 | mbedtls_platform_zeroize( ssl->in_buf, in_buf_len ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4621 | mbedtls_free( ssl->in_buf ); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 4622 | ssl->in_buf = NULL; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4623 | } |
| 4624 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4625 | if( ssl->transform ) |
| 4626 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4627 | mbedtls_ssl_transform_free( ssl->transform ); |
| 4628 | mbedtls_free( ssl->transform ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4629 | } |
| 4630 | |
| 4631 | if( ssl->handshake ) |
| 4632 | { |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 4633 | mbedtls_ssl_handshake_free( ssl ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4634 | mbedtls_ssl_transform_free( ssl->transform_negotiate ); |
| 4635 | mbedtls_ssl_session_free( ssl->session_negotiate ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4636 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4637 | mbedtls_free( ssl->handshake ); |
| 4638 | mbedtls_free( ssl->transform_negotiate ); |
| 4639 | mbedtls_free( ssl->session_negotiate ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4640 | } |
| 4641 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4642 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Hanno Becker | 3aa186f | 2021-08-10 09:24:19 +0100 | [diff] [blame] | 4643 | mbedtls_ssl_transform_free( ssl->transform_application ); |
| 4644 | mbedtls_free( ssl->transform_application ); |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4645 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | 3aa186f | 2021-08-10 09:24:19 +0100 | [diff] [blame] | 4646 | |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 4647 | if( ssl->session ) |
| 4648 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4649 | mbedtls_ssl_session_free( ssl->session ); |
| 4650 | mbedtls_free( ssl->session ); |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 4651 | } |
| 4652 | |
Manuel Pégourié-Gonnard | 55fab2d | 2015-05-11 16:15:19 +0200 | [diff] [blame] | 4653 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 4654 | if( ssl->hostname != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4655 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 4656 | mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4657 | mbedtls_free( ssl->hostname ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4658 | } |
Paul Bakker | 0be444a | 2013-08-27 21:55:01 +0200 | [diff] [blame] | 4659 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4660 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 4661 | #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] | 4662 | mbedtls_free( ssl->cli_id ); |
Manuel Pégourié-Gonnard | 43c0218 | 2014-07-22 17:32:01 +0200 | [diff] [blame] | 4663 | #endif |
| 4664 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4665 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) ); |
Paul Bakker | 2da561c | 2009-02-05 18:00:28 +0000 | [diff] [blame] | 4666 | |
Paul Bakker | 86f04f4 | 2013-02-14 11:20:09 +0100 | [diff] [blame] | 4667 | /* Actually clear after last debug message */ |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 4668 | mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4669 | } |
| 4670 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4671 | /* |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 4672 | * Initialize mbedtls_ssl_config |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4673 | */ |
| 4674 | void mbedtls_ssl_config_init( mbedtls_ssl_config *conf ) |
| 4675 | { |
| 4676 | memset( conf, 0, sizeof( mbedtls_ssl_config ) ); |
| 4677 | } |
| 4678 | |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4679 | /* The selection should be the same as mbedtls_x509_crt_profile_default in |
| 4680 | * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters: |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4681 | * curves with a lower resource usage come first. |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4682 | * See the documentation of mbedtls_ssl_conf_curves() for what we promise |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4683 | * about this list. |
| 4684 | */ |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4685 | static uint16_t ssl_preset_default_groups[] = { |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4686 | #if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4687 | MBEDTLS_SSL_IANA_TLS_GROUP_X25519, |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4688 | #endif |
| 4689 | #if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4690 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1, |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4691 | #endif |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4692 | #if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4693 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4694 | #endif |
| 4695 | #if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4696 | MBEDTLS_SSL_IANA_TLS_GROUP_X448, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4697 | #endif |
| 4698 | #if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4699 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4700 | #endif |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4701 | #if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4702 | MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1, |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4703 | #endif |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4704 | #if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4705 | MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4706 | #endif |
| 4707 | #if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4708 | MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4709 | #endif |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4710 | MBEDTLS_SSL_IANA_TLS_GROUP_NONE |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4711 | }; |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4712 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4713 | static int ssl_preset_suiteb_ciphersuites[] = { |
| 4714 | MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, |
| 4715 | MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, |
| 4716 | 0 |
| 4717 | }; |
| 4718 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 4719 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Hanno Becker | 9c6aa7b | 2021-08-10 13:50:43 +0100 | [diff] [blame] | 4720 | |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4721 | /* NOTICE: |
Jerry Yu | 0b994b8 | 2022-01-25 17:22:12 +0800 | [diff] [blame] | 4722 | * 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] | 4723 | * rules SHOULD be upheld. |
| 4724 | * - No duplicate entries. |
| 4725 | * - 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] | 4726 | * - ssl_tls12_preset* is for TLS 1.2 use only. |
Jerry Yu | 370e146 | 2022-01-25 10:36:53 +0800 | [diff] [blame] | 4727 | * - 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] | 4728 | */ |
Hanno Becker | 9c6aa7b | 2021-08-10 13:50:43 +0100 | [diff] [blame] | 4729 | static uint16_t ssl_preset_default_sig_algs[] = { |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4730 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4731 | #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] | 4732 | defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) |
| 4733 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256, |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4734 | #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] | 4735 | MBEDTLS_ECP_DP_SECP256R1_ENABLED */ |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4736 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4737 | #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] | 4738 | defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) |
| 4739 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384, |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4740 | #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] | 4741 | MBEDTLS_ECP_DP_SECP384R1_ENABLED */ |
| 4742 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4743 | #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] | 4744 | defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) |
| 4745 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512, |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4746 | #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] | 4747 | MBEDTLS_ECP_DP_SECP521R1_ENABLED */ |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4748 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4749 | #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] | 4750 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512, |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4751 | #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] | 4752 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4753 | #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] | 4754 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384, |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4755 | #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] | 4756 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4757 | #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] | 4758 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256, |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4759 | #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] | 4760 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4761 | #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] | 4762 | MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512, |
| 4763 | #endif /* MBEDTLS_RSA_C && MBEDTLS_SHA512_C */ |
| 4764 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4765 | #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] | 4766 | MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384, |
| 4767 | #endif /* MBEDTLS_RSA_C && MBEDTLS_SHA384_C */ |
| 4768 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4769 | #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] | 4770 | MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256, |
| 4771 | #endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */ |
| 4772 | |
Gabor Mezei | 15b95a6 | 2022-05-09 16:37:58 +0200 | [diff] [blame] | 4773 | MBEDTLS_TLS_SIG_NONE |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4774 | }; |
| 4775 | |
| 4776 | /* NOTICE: see above */ |
| 4777 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 4778 | static uint16_t ssl_tls12_preset_default_sig_algs[] = { |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4779 | #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] | 4780 | #if defined(MBEDTLS_ECDSA_C) |
| 4781 | 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] | 4782 | #endif |
Jerry Yu | 09a99fc | 2022-07-28 14:22:17 +0800 | [diff] [blame] | 4783 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) |
| 4784 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512, |
| 4785 | #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */ |
Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 4786 | #if defined(MBEDTLS_RSA_C) |
| 4787 | MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA512 ), |
| 4788 | #endif |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4789 | #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] | 4790 | #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] | 4791 | #if defined(MBEDTLS_ECDSA_C) |
| 4792 | 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] | 4793 | #endif |
Jerry Yu | 09a99fc | 2022-07-28 14:22:17 +0800 | [diff] [blame] | 4794 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) |
| 4795 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384, |
| 4796 | #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */ |
Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 4797 | #if defined(MBEDTLS_RSA_C) |
| 4798 | MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ), |
| 4799 | #endif |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4800 | #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] | 4801 | #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] | 4802 | #if defined(MBEDTLS_ECDSA_C) |
| 4803 | 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] | 4804 | #endif |
Jerry Yu | 09a99fc | 2022-07-28 14:22:17 +0800 | [diff] [blame] | 4805 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) |
| 4806 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256, |
| 4807 | #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */ |
Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 4808 | #if defined(MBEDTLS_RSA_C) |
| 4809 | MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ), |
| 4810 | #endif |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4811 | #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] | 4812 | MBEDTLS_TLS_SIG_NONE |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4813 | }; |
| 4814 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 4815 | /* NOTICE: see above */ |
| 4816 | static uint16_t ssl_preset_suiteb_sig_algs[] = { |
| 4817 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4818 | #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] | 4819 | defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) |
| 4820 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256, |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4821 | #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] | 4822 | MBEDTLS_ECP_DP_SECP256R1_ENABLED */ |
| 4823 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4824 | #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] | 4825 | defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) |
| 4826 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384, |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4827 | #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] | 4828 | MBEDTLS_ECP_DP_SECP384R1_ENABLED */ |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4829 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4830 | #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] | 4831 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256, |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4832 | #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] | 4833 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4834 | #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] | 4835 | MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256, |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4836 | #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] | 4837 | |
Gabor Mezei | 15b95a6 | 2022-05-09 16:37:58 +0200 | [diff] [blame] | 4838 | MBEDTLS_TLS_SIG_NONE |
Jerry Yu | 713013f | 2022-01-17 18:16:35 +0800 | [diff] [blame] | 4839 | }; |
Jerry Yu | 6106fdc | 2022-01-12 16:36:14 +0800 | [diff] [blame] | 4840 | |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4841 | /* NOTICE: see above */ |
| 4842 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 4843 | static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = { |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4844 | #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] | 4845 | #if defined(MBEDTLS_ECDSA_C) |
| 4846 | 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] | 4847 | #endif |
Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 4848 | #if defined(MBEDTLS_RSA_C) |
| 4849 | MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ), |
| 4850 | #endif |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4851 | #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] | 4852 | #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] | 4853 | #if defined(MBEDTLS_ECDSA_C) |
| 4854 | 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] | 4855 | #endif |
Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 4856 | #if defined(MBEDTLS_RSA_C) |
| 4857 | MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ), |
| 4858 | #endif |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4859 | #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] | 4860 | MBEDTLS_TLS_SIG_NONE |
Hanno Becker | 9c6aa7b | 2021-08-10 13:50:43 +0100 | [diff] [blame] | 4861 | }; |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4862 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 4863 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 4864 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4865 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4866 | static uint16_t ssl_preset_suiteb_groups[] = { |
Jaeden Amero | d431104 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 4867 | #if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4868 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1, |
Jaeden Amero | d431104 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 4869 | #endif |
| 4870 | #if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4871 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1, |
Jaeden Amero | d431104 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 4872 | #endif |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4873 | MBEDTLS_SSL_IANA_TLS_GROUP_NONE |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4874 | }; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4875 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 4876 | #if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4877 | /* 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] | 4878 | * to make sure there are no duplicated signature algorithm entries. */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 4879 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 4880 | static int ssl_check_no_sig_alg_duplication( uint16_t * sig_algs ) |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4881 | { |
| 4882 | size_t i, j; |
| 4883 | int ret = 0; |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4884 | |
Gabor Mezei | 15b95a6 | 2022-05-09 16:37:58 +0200 | [diff] [blame] | 4885 | for( i = 0; sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ ) |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4886 | { |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 4887 | for( j = 0; j < i; j++ ) |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4888 | { |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 4889 | if( sig_algs[i] != sig_algs[j] ) |
| 4890 | continue; |
| 4891 | mbedtls_printf( " entry(%04x,%" MBEDTLS_PRINTF_SIZET |
| 4892 | ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n", |
| 4893 | sig_algs[i], j, i ); |
| 4894 | ret = -1; |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4895 | } |
| 4896 | } |
| 4897 | return( ret ); |
| 4898 | } |
| 4899 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 4900 | #endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4901 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4902 | /* |
Tillmann Karras | 588ad50 | 2015-09-25 04:27:22 +0200 | [diff] [blame] | 4903 | * Load default in mbedtls_ssl_config |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4904 | */ |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 4905 | int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4906 | int endpoint, int transport, int preset ) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4907 | { |
Manuel Pégourié-Gonnard | 8b431fb | 2015-05-11 12:54:52 +0200 | [diff] [blame] | 4908 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 4909 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 8b431fb | 2015-05-11 12:54:52 +0200 | [diff] [blame] | 4910 | #endif |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4911 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 4912 | #if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 4913 | if( ssl_check_no_sig_alg_duplication( ssl_preset_suiteb_sig_algs ) ) |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4914 | { |
| 4915 | mbedtls_printf( "ssl_preset_suiteb_sig_algs has duplicated entries\n" ); |
| 4916 | return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED ); |
| 4917 | } |
| 4918 | |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 4919 | if( ssl_check_no_sig_alg_duplication( ssl_preset_default_sig_algs ) ) |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4920 | { |
| 4921 | mbedtls_printf( "ssl_preset_default_sig_algs has duplicated entries\n" ); |
| 4922 | return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED ); |
| 4923 | } |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4924 | |
| 4925 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 4926 | 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] | 4927 | { |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4928 | mbedtls_printf( "ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n" ); |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4929 | return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED ); |
| 4930 | } |
| 4931 | |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 4932 | 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] | 4933 | { |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4934 | mbedtls_printf( "ssl_tls12_preset_default_sig_algs has duplicated entries\n" ); |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4935 | return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED ); |
| 4936 | } |
| 4937 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 4938 | #endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4939 | |
Manuel Pégourié-Gonnard | 0de074f | 2015-05-14 12:58:01 +0200 | [diff] [blame] | 4940 | /* Use the functions here so that they are covered in tests, |
| 4941 | * but otherwise access member directly for efficiency */ |
| 4942 | mbedtls_ssl_conf_endpoint( conf, endpoint ); |
| 4943 | mbedtls_ssl_conf_transport( conf, transport ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4944 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4945 | /* |
| 4946 | * Things that are common to all presets |
| 4947 | */ |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 4948 | #if defined(MBEDTLS_SSL_CLI_C) |
| 4949 | if( endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 4950 | { |
| 4951 | conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED; |
| 4952 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 4953 | conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED; |
| 4954 | #endif |
| 4955 | } |
| 4956 | #endif |
| 4957 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4958 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 4959 | conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED; |
| 4960 | #endif |
| 4961 | |
| 4962 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 4963 | conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED; |
| 4964 | #endif |
| 4965 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 4966 | #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] | 4967 | conf->f_cookie_write = ssl_cookie_write_dummy; |
| 4968 | conf->f_cookie_check = ssl_cookie_check_dummy; |
| 4969 | #endif |
| 4970 | |
| 4971 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 4972 | conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED; |
| 4973 | #endif |
| 4974 | |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 4975 | #if defined(MBEDTLS_SSL_SRV_C) |
| 4976 | conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED; |
TRodziewicz | 3946f79 | 2021-06-14 12:11:18 +0200 | [diff] [blame] | 4977 | conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER; |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 4978 | #endif |
| 4979 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4980 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4981 | conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN; |
| 4982 | conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX; |
| 4983 | #endif |
| 4984 | |
| 4985 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 4986 | conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT; |
Andres AG | 2196c7f | 2016-12-15 17:01:16 +0000 | [diff] [blame] | 4987 | memset( conf->renego_period, 0x00, 2 ); |
| 4988 | memset( conf->renego_period + 2, 0xFF, 6 ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4989 | #endif |
| 4990 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4991 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
Hanno Becker | e2defad | 2021-07-24 05:59:17 +0100 | [diff] [blame] | 4992 | if( endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 4993 | { |
| 4994 | const unsigned char dhm_p[] = |
| 4995 | MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN; |
| 4996 | const unsigned char dhm_g[] = |
| 4997 | MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN; |
Hanno Becker | 00d0a68 | 2017-10-04 13:14:29 +0100 | [diff] [blame] | 4998 | |
Hanno Becker | e2defad | 2021-07-24 05:59:17 +0100 | [diff] [blame] | 4999 | if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf, |
| 5000 | dhm_p, sizeof( dhm_p ), |
| 5001 | dhm_g, sizeof( dhm_g ) ) ) != 0 ) |
| 5002 | { |
| 5003 | return( ret ); |
| 5004 | } |
| 5005 | } |
Manuel Pégourié-Gonnard | bd990d6 | 2015-06-11 14:49:42 +0200 | [diff] [blame] | 5006 | #endif |
| 5007 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 5008 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | d0766ec | 2022-09-22 10:46:57 +0800 | [diff] [blame] | 5009 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SESSION_TICKETS) |
Jerry Yu | 1ad7ace | 2022-08-09 13:28:39 +0800 | [diff] [blame] | 5010 | mbedtls_ssl_conf_new_session_tickets( |
| 5011 | conf, MBEDTLS_SSL_TLS1_3_DEFAULT_NEW_SESSION_TICKETS ); |
| 5012 | #endif |
Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 5013 | /* |
| 5014 | * Allow all TLS 1.3 key exchange modes by default. |
| 5015 | */ |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 5016 | conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL; |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 5017 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 5018 | |
XiaokangQian | 4d3a604 | 2022-04-21 13:46:17 +0000 | [diff] [blame] | 5019 | if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 5020 | { |
Glenn Strauss | 2dfcea2 | 2022-03-14 17:26:42 -0400 | [diff] [blame] | 5021 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
XiaokangQian | 4d3a604 | 2022-04-21 13:46:17 +0000 | [diff] [blame] | 5022 | conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
XiaokangQian | 060d867 | 2022-04-21 09:24:56 +0000 | [diff] [blame] | 5023 | conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
XiaokangQian | 4d3a604 | 2022-04-21 13:46:17 +0000 | [diff] [blame] | 5024 | #else |
XiaokangQian | 060d867 | 2022-04-21 09:24:56 +0000 | [diff] [blame] | 5025 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
XiaokangQian | 060d867 | 2022-04-21 09:24:56 +0000 | [diff] [blame] | 5026 | #endif |
XiaokangQian | 4d3a604 | 2022-04-21 13:46:17 +0000 | [diff] [blame] | 5027 | } |
| 5028 | else |
| 5029 | { |
| 5030 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 5031 | if( endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 5032 | { |
| 5033 | conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 5034 | conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3; |
| 5035 | } |
| 5036 | else |
| 5037 | /* Hybrid TLS 1.2 / 1.3 is not supported on server side yet */ |
| 5038 | { |
| 5039 | conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 5040 | conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 5041 | } |
| 5042 | #elif defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 5043 | conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_3; |
| 5044 | conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3; |
| 5045 | #elif defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 5046 | conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 5047 | conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 5048 | #else |
| 5049 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 5050 | #endif |
| 5051 | } |
Glenn Strauss | 2dfcea2 | 2022-03-14 17:26:42 -0400 | [diff] [blame] | 5052 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5053 | /* |
| 5054 | * Preset-specific defaults |
| 5055 | */ |
| 5056 | switch( preset ) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5057 | { |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5058 | /* |
| 5059 | * NSA Suite B |
| 5060 | */ |
| 5061 | case MBEDTLS_SSL_PRESET_SUITEB: |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5062 | |
Hanno Becker | d60b6c6 | 2021-04-29 12:04:11 +0100 | [diff] [blame] | 5063 | conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5064 | |
| 5065 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 5066 | conf->cert_profile = &mbedtls_x509_crt_profile_suiteb; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5067 | #endif |
| 5068 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5069 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5070 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 5071 | if( mbedtls_ssl_conf_is_tls12_only( conf ) ) |
| 5072 | conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs; |
| 5073 | else |
| 5074 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 5075 | conf->sig_algs = ssl_preset_suiteb_sig_algs; |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5076 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5077 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5078 | #if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 5079 | conf->curve_list = NULL; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5080 | #endif |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5081 | conf->group_list = ssl_preset_suiteb_groups; |
Manuel Pégourié-Gonnard | c98204e | 2015-08-11 04:21:01 +0200 | [diff] [blame] | 5082 | break; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5083 | |
| 5084 | /* |
| 5085 | * Default |
| 5086 | */ |
| 5087 | default: |
Ronald Cron | f660655 | 2022-03-15 11:23:25 +0100 | [diff] [blame] | 5088 | |
Hanno Becker | d60b6c6 | 2021-04-29 12:04:11 +0100 | [diff] [blame] | 5089 | conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites(); |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5090 | |
| 5091 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 5092 | conf->cert_profile = &mbedtls_x509_crt_profile_default; |
| 5093 | #endif |
| 5094 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5095 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5096 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 5097 | if( mbedtls_ssl_conf_is_tls12_only( conf ) ) |
| 5098 | conf->sig_algs = ssl_tls12_preset_default_sig_algs; |
| 5099 | else |
| 5100 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 5101 | conf->sig_algs = ssl_preset_default_sig_algs; |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5102 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5103 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5104 | #if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 5105 | conf->curve_list = NULL; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5106 | #endif |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5107 | conf->group_list = ssl_preset_default_groups; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5108 | |
| 5109 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C) |
| 5110 | conf->dhm_min_bitlen = 1024; |
| 5111 | #endif |
| 5112 | } |
| 5113 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5114 | return( 0 ); |
| 5115 | } |
| 5116 | |
| 5117 | /* |
| 5118 | * Free mbedtls_ssl_config |
| 5119 | */ |
| 5120 | void mbedtls_ssl_config_free( mbedtls_ssl_config *conf ) |
| 5121 | { |
| 5122 | #if defined(MBEDTLS_DHM_C) |
| 5123 | mbedtls_mpi_free( &conf->dhm_P ); |
| 5124 | mbedtls_mpi_free( &conf->dhm_G ); |
| 5125 | #endif |
| 5126 | |
Ronald Cron | 73fe8df | 2022-10-05 14:31:43 +0200 | [diff] [blame] | 5127 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED) |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 5128 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 5129 | if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) ) |
| 5130 | { |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 5131 | conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT; |
| 5132 | } |
Neil Armstrong | 8ecd668 | 2022-05-05 11:40:35 +0200 | [diff] [blame] | 5133 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5134 | if( conf->psk != NULL ) |
| 5135 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 5136 | mbedtls_platform_zeroize( conf->psk, conf->psk_len ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5137 | mbedtls_free( conf->psk ); |
Azim Khan | 27e8a12 | 2018-03-21 14:24:11 +0000 | [diff] [blame] | 5138 | conf->psk = NULL; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5139 | conf->psk_len = 0; |
junyeonLEE | 316b162 | 2017-12-20 16:29:30 +0900 | [diff] [blame] | 5140 | } |
| 5141 | |
| 5142 | if( conf->psk_identity != NULL ) |
| 5143 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 5144 | mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len ); |
junyeonLEE | 316b162 | 2017-12-20 16:29:30 +0900 | [diff] [blame] | 5145 | mbedtls_free( conf->psk_identity ); |
Azim Khan | 27e8a12 | 2018-03-21 14:24:11 +0000 | [diff] [blame] | 5146 | conf->psk_identity = NULL; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5147 | conf->psk_identity_len = 0; |
| 5148 | } |
Ronald Cron | 73fe8df | 2022-10-05 14:31:43 +0200 | [diff] [blame] | 5149 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */ |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5150 | |
| 5151 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 5152 | ssl_key_cert_free( conf->key_cert ); |
| 5153 | #endif |
| 5154 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 5155 | mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5156 | } |
| 5157 | |
Manuel Pégourié-Gonnard | 5674a97 | 2015-10-19 15:14:03 +0200 | [diff] [blame] | 5158 | #if defined(MBEDTLS_PK_C) && \ |
| 5159 | ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) ) |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 5160 | /* |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5161 | * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 5162 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5163 | 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] | 5164 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5165 | #if defined(MBEDTLS_RSA_C) |
| 5166 | if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) ) |
| 5167 | return( MBEDTLS_SSL_SIG_RSA ); |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 5168 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5169 | #if defined(MBEDTLS_ECDSA_C) |
| 5170 | if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) ) |
| 5171 | return( MBEDTLS_SSL_SIG_ECDSA ); |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 5172 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5173 | return( MBEDTLS_SSL_SIG_ANON ); |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 5174 | } |
| 5175 | |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 5176 | unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type ) |
| 5177 | { |
| 5178 | switch( type ) { |
| 5179 | case MBEDTLS_PK_RSA: |
| 5180 | return( MBEDTLS_SSL_SIG_RSA ); |
| 5181 | case MBEDTLS_PK_ECDSA: |
| 5182 | case MBEDTLS_PK_ECKEY: |
| 5183 | return( MBEDTLS_SSL_SIG_ECDSA ); |
| 5184 | default: |
| 5185 | return( MBEDTLS_SSL_SIG_ANON ); |
| 5186 | } |
| 5187 | } |
| 5188 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5189 | 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] | 5190 | { |
| 5191 | switch( sig ) |
| 5192 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5193 | #if defined(MBEDTLS_RSA_C) |
| 5194 | case MBEDTLS_SSL_SIG_RSA: |
| 5195 | return( MBEDTLS_PK_RSA ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 5196 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5197 | #if defined(MBEDTLS_ECDSA_C) |
| 5198 | case MBEDTLS_SSL_SIG_ECDSA: |
| 5199 | return( MBEDTLS_PK_ECDSA ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 5200 | #endif |
| 5201 | default: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5202 | return( MBEDTLS_PK_NONE ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 5203 | } |
| 5204 | } |
Manuel Pégourié-Gonnard | 5674a97 | 2015-10-19 15:14:03 +0200 | [diff] [blame] | 5205 | #endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */ |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 5206 | |
Manuel Pégourié-Gonnard | 1a48383 | 2013-09-20 12:29:15 +0200 | [diff] [blame] | 5207 | /* |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 5208 | * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX |
Manuel Pégourié-Gonnard | 1a48383 | 2013-09-20 12:29:15 +0200 | [diff] [blame] | 5209 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5210 | 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] | 5211 | { |
| 5212 | switch( hash ) |
| 5213 | { |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5214 | #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] | 5215 | case MBEDTLS_SSL_HASH_MD5: |
| 5216 | return( MBEDTLS_MD_MD5 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 5217 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5218 | #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] | 5219 | case MBEDTLS_SSL_HASH_SHA1: |
| 5220 | return( MBEDTLS_MD_SHA1 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 5221 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5222 | #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] | 5223 | case MBEDTLS_SSL_HASH_SHA224: |
| 5224 | return( MBEDTLS_MD_SHA224 ); |
Mateusz Starzyk | e3c48b4 | 2021-04-19 16:46:28 +0200 | [diff] [blame] | 5225 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5226 | #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] | 5227 | case MBEDTLS_SSL_HASH_SHA256: |
| 5228 | return( MBEDTLS_MD_SHA256 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 5229 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5230 | #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] | 5231 | case MBEDTLS_SSL_HASH_SHA384: |
| 5232 | return( MBEDTLS_MD_SHA384 ); |
Mateusz Starzyk | 3352a53 | 2021-04-06 14:28:22 +0200 | [diff] [blame] | 5233 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5234 | #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] | 5235 | case MBEDTLS_SSL_HASH_SHA512: |
| 5236 | return( MBEDTLS_MD_SHA512 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 5237 | #endif |
| 5238 | default: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5239 | return( MBEDTLS_MD_NONE ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 5240 | } |
| 5241 | } |
| 5242 | |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 5243 | /* |
| 5244 | * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX |
| 5245 | */ |
| 5246 | unsigned char mbedtls_ssl_hash_from_md_alg( int md ) |
| 5247 | { |
| 5248 | switch( md ) |
| 5249 | { |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5250 | #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] | 5251 | case MBEDTLS_MD_MD5: |
| 5252 | return( MBEDTLS_SSL_HASH_MD5 ); |
| 5253 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5254 | #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] | 5255 | case MBEDTLS_MD_SHA1: |
| 5256 | return( MBEDTLS_SSL_HASH_SHA1 ); |
| 5257 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5258 | #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] | 5259 | case MBEDTLS_MD_SHA224: |
| 5260 | return( MBEDTLS_SSL_HASH_SHA224 ); |
Mateusz Starzyk | e3c48b4 | 2021-04-19 16:46:28 +0200 | [diff] [blame] | 5261 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5262 | #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] | 5263 | case MBEDTLS_MD_SHA256: |
| 5264 | return( MBEDTLS_SSL_HASH_SHA256 ); |
| 5265 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5266 | #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] | 5267 | case MBEDTLS_MD_SHA384: |
| 5268 | return( MBEDTLS_SSL_HASH_SHA384 ); |
Mateusz Starzyk | 3352a53 | 2021-04-06 14:28:22 +0200 | [diff] [blame] | 5269 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5270 | #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] | 5271 | case MBEDTLS_MD_SHA512: |
| 5272 | return( MBEDTLS_SSL_HASH_SHA512 ); |
| 5273 | #endif |
| 5274 | default: |
| 5275 | return( MBEDTLS_SSL_HASH_NONE ); |
| 5276 | } |
| 5277 | } |
| 5278 | |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 5279 | /* |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 5280 | * 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] | 5281 | * 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] | 5282 | */ |
Manuel Pégourié-Gonnard | 0d63b84 | 2022-01-18 13:10:56 +0100 | [diff] [blame] | 5283 | 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] | 5284 | { |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5285 | const uint16_t *group_list = mbedtls_ssl_get_groups( ssl ); |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 5286 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5287 | if( group_list == NULL ) |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 5288 | return( -1 ); |
| 5289 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5290 | for( ; *group_list != 0; group_list++ ) |
| 5291 | { |
| 5292 | if( *group_list == tls_id ) |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 5293 | return( 0 ); |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5294 | } |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 5295 | |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 5296 | return( -1 ); |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 5297 | } |
Manuel Pégourié-Gonnard | 0d63b84 | 2022-01-18 13:10:56 +0100 | [diff] [blame] | 5298 | |
| 5299 | #if defined(MBEDTLS_ECP_C) |
| 5300 | /* |
| 5301 | * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id. |
| 5302 | */ |
| 5303 | int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id ) |
| 5304 | { |
Leonid Rozenboim | 19e5973 | 2022-08-08 16:52:38 -0700 | [diff] [blame] | 5305 | const mbedtls_ecp_curve_info *grp_info = |
Tom Cosgrove | bcc13c9 | 2022-08-24 15:08:16 +0100 | [diff] [blame] | 5306 | mbedtls_ecp_curve_info_from_grp_id( grp_id ); |
Leonid Rozenboim | 19e5973 | 2022-08-08 16:52:38 -0700 | [diff] [blame] | 5307 | |
Tom Cosgrove | bcc13c9 | 2022-08-24 15:08:16 +0100 | [diff] [blame] | 5308 | if ( grp_info == NULL ) |
Leonid Rozenboim | 19e5973 | 2022-08-08 16:52:38 -0700 | [diff] [blame] | 5309 | return -1; |
| 5310 | |
| 5311 | uint16_t tls_id = grp_info->tls_id; |
| 5312 | |
Manuel Pégourié-Gonnard | 0d63b84 | 2022-01-18 13:10:56 +0100 | [diff] [blame] | 5313 | return mbedtls_ssl_check_curve_tls_id( ssl, tls_id ); |
| 5314 | } |
Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 5315 | #endif /* MBEDTLS_ECP_C */ |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5316 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5317 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 5318 | int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert, |
| 5319 | const mbedtls_ssl_ciphersuite_t *ciphersuite, |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 5320 | int cert_endpoint, |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 5321 | uint32_t *flags ) |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5322 | { |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 5323 | int ret = 0; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5324 | int usage = 0; |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 5325 | const char *ext_oid; |
| 5326 | size_t ext_len; |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 5327 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5328 | if( cert_endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5329 | { |
| 5330 | /* Server part of the key exchange */ |
| 5331 | switch( ciphersuite->key_exchange ) |
| 5332 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5333 | case MBEDTLS_KEY_EXCHANGE_RSA: |
| 5334 | case MBEDTLS_KEY_EXCHANGE_RSA_PSK: |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 5335 | usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5336 | break; |
| 5337 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5338 | case MBEDTLS_KEY_EXCHANGE_DHE_RSA: |
| 5339 | case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA: |
| 5340 | case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA: |
| 5341 | usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5342 | break; |
| 5343 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5344 | case MBEDTLS_KEY_EXCHANGE_ECDH_RSA: |
| 5345 | case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA: |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 5346 | usage = MBEDTLS_X509_KU_KEY_AGREEMENT; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5347 | break; |
| 5348 | |
| 5349 | /* 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] | 5350 | case MBEDTLS_KEY_EXCHANGE_NONE: |
| 5351 | case MBEDTLS_KEY_EXCHANGE_PSK: |
| 5352 | case MBEDTLS_KEY_EXCHANGE_DHE_PSK: |
| 5353 | case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK: |
Manuel Pégourié-Gonnard | 557535d | 2015-09-15 17:53:32 +0200 | [diff] [blame] | 5354 | case MBEDTLS_KEY_EXCHANGE_ECJPAKE: |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5355 | usage = 0; |
| 5356 | } |
| 5357 | } |
| 5358 | else |
| 5359 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5360 | /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */ |
| 5361 | usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5362 | } |
| 5363 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5364 | if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 ) |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 5365 | { |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 5366 | *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE; |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 5367 | ret = -1; |
| 5368 | } |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5369 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5370 | if( cert_endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 5371 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5372 | ext_oid = MBEDTLS_OID_SERVER_AUTH; |
| 5373 | ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH ); |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 5374 | } |
| 5375 | else |
| 5376 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5377 | ext_oid = MBEDTLS_OID_CLIENT_AUTH; |
| 5378 | ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH ); |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 5379 | } |
| 5380 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5381 | 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] | 5382 | { |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 5383 | *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE; |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 5384 | ret = -1; |
| 5385 | } |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 5386 | |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 5387 | return( ret ); |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5388 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5389 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Manuel Pégourié-Gonnard | 3a306b9 | 2014-04-29 15:11:17 +0200 | [diff] [blame] | 5390 | |
Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 5391 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 5392 | int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl, |
| 5393 | const mbedtls_md_type_t md, |
| 5394 | unsigned char *dst, |
| 5395 | size_t dst_len, |
| 5396 | size_t *olen ) |
| 5397 | { |
Ronald Cron | f6893e1 | 2022-01-07 22:09:01 +0100 | [diff] [blame] | 5398 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 5399 | psa_hash_operation_t *hash_operation_to_clone; |
| 5400 | psa_hash_operation_t hash_operation = psa_hash_operation_init(); |
| 5401 | |
Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 5402 | *olen = 0; |
Ronald Cron | f6893e1 | 2022-01-07 22:09:01 +0100 | [diff] [blame] | 5403 | |
| 5404 | switch( md ) |
| 5405 | { |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5406 | #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] | 5407 | case MBEDTLS_MD_SHA384: |
| 5408 | hash_operation_to_clone = &ssl->handshake->fin_sha384_psa; |
| 5409 | break; |
| 5410 | #endif |
| 5411 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5412 | #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] | 5413 | case MBEDTLS_MD_SHA256: |
| 5414 | hash_operation_to_clone = &ssl->handshake->fin_sha256_psa; |
| 5415 | break; |
| 5416 | #endif |
| 5417 | |
| 5418 | default: |
| 5419 | goto exit; |
| 5420 | } |
| 5421 | |
| 5422 | status = psa_hash_clone( hash_operation_to_clone, &hash_operation ); |
| 5423 | if( status != PSA_SUCCESS ) |
| 5424 | goto exit; |
| 5425 | |
| 5426 | status = psa_hash_finish( &hash_operation, dst, dst_len, olen ); |
| 5427 | if( status != PSA_SUCCESS ) |
| 5428 | goto exit; |
| 5429 | |
| 5430 | exit: |
Andrzej Kurek | eabeb30 | 2022-10-17 07:52:51 -0400 | [diff] [blame] | 5431 | #if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \ |
| 5432 | !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
| 5433 | (void) ssl; |
| 5434 | #endif |
Ronald Cron | b788c04 | 2022-02-15 09:14:15 +0100 | [diff] [blame] | 5435 | return( psa_ssl_status_to_mbedtls( status ) ); |
Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 5436 | } |
| 5437 | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
| 5438 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5439 | #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] | 5440 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | 000f976 | 2021-09-14 11:12:51 +0800 | [diff] [blame] | 5441 | static int ssl_get_handshake_transcript_sha384( mbedtls_ssl_context *ssl, |
| 5442 | unsigned char *dst, |
| 5443 | size_t dst_len, |
| 5444 | size_t *olen ) |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5445 | { |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5446 | int ret; |
| 5447 | mbedtls_sha512_context sha512; |
| 5448 | |
| 5449 | if( dst_len < 48 ) |
| 5450 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 5451 | |
| 5452 | mbedtls_sha512_init( &sha512 ); |
Andrzej Kurek | a242e83 | 2022-08-11 10:03:14 -0400 | [diff] [blame] | 5453 | mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 ); |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5454 | |
| 5455 | if( ( ret = mbedtls_sha512_finish( &sha512, dst ) ) != 0 ) |
| 5456 | { |
| 5457 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha512_finish", ret ); |
| 5458 | goto exit; |
| 5459 | } |
| 5460 | |
| 5461 | *olen = 48; |
| 5462 | |
| 5463 | exit: |
| 5464 | |
| 5465 | mbedtls_sha512_free( &sha512 ); |
| 5466 | return( ret ); |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5467 | } |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5468 | #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] | 5469 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5470 | #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] | 5471 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | 000f976 | 2021-09-14 11:12:51 +0800 | [diff] [blame] | 5472 | static int ssl_get_handshake_transcript_sha256( mbedtls_ssl_context *ssl, |
| 5473 | unsigned char *dst, |
| 5474 | size_t dst_len, |
| 5475 | size_t *olen ) |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5476 | { |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5477 | int ret; |
| 5478 | mbedtls_sha256_context sha256; |
| 5479 | |
| 5480 | if( dst_len < 32 ) |
| 5481 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 5482 | |
| 5483 | mbedtls_sha256_init( &sha256 ); |
| 5484 | mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 ); |
Jerry Yu | c5aef88 | 2021-12-23 20:15:02 +0800 | [diff] [blame] | 5485 | |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5486 | if( ( ret = mbedtls_sha256_finish( &sha256, dst ) ) != 0 ) |
| 5487 | { |
| 5488 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha256_finish", ret ); |
| 5489 | goto exit; |
| 5490 | } |
| 5491 | |
| 5492 | *olen = 32; |
| 5493 | |
| 5494 | exit: |
| 5495 | |
| 5496 | mbedtls_sha256_free( &sha256 ); |
| 5497 | return( ret ); |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5498 | } |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5499 | #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] | 5500 | |
Jerry Yu | 000f976 | 2021-09-14 11:12:51 +0800 | [diff] [blame] | 5501 | int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl, |
| 5502 | const mbedtls_md_type_t md, |
| 5503 | unsigned char *dst, |
| 5504 | size_t dst_len, |
| 5505 | size_t *olen ) |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5506 | { |
Jerry Yu | c1ddeef | 2021-10-08 15:14:45 +0800 | [diff] [blame] | 5507 | switch( md ) |
| 5508 | { |
| 5509 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5510 | #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] | 5511 | case MBEDTLS_MD_SHA384: |
Jerry Yu | c5aef88 | 2021-12-23 20:15:02 +0800 | [diff] [blame] | 5512 | return( ssl_get_handshake_transcript_sha384( ssl, dst, dst_len, olen ) ); |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 5513 | #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] | 5514 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5515 | #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] | 5516 | case MBEDTLS_MD_SHA256: |
Jerry Yu | c5aef88 | 2021-12-23 20:15:02 +0800 | [diff] [blame] | 5517 | return( ssl_get_handshake_transcript_sha256( ssl, dst, dst_len, olen ) ); |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 5518 | #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] | 5519 | |
| 5520 | default: |
Andrzej Kurek | 409248a | 2022-10-24 10:33:21 -0400 | [diff] [blame] | 5521 | #if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \ |
| 5522 | !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
| 5523 | (void) ssl; |
| 5524 | (void) dst; |
| 5525 | (void) dst_len; |
| 5526 | (void) olen; |
| 5527 | #endif |
Jerry Yu | c1ddeef | 2021-10-08 15:14:45 +0800 | [diff] [blame] | 5528 | break; |
| 5529 | } |
Jerry Yu | c5aef88 | 2021-12-23 20:15:02 +0800 | [diff] [blame] | 5530 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5531 | } |
XiaokangQian | 647719a | 2021-12-07 09:16:29 +0000 | [diff] [blame] | 5532 | |
Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 5533 | #endif /* !MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5534 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5535 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 5536 | /* mbedtls_ssl_parse_sig_alg_ext() |
| 5537 | * |
| 5538 | * The `extension_data` field of signature algorithm contains a `SignatureSchemeList` |
| 5539 | * value (TLS 1.3 RFC8446): |
| 5540 | * enum { |
| 5541 | * .... |
| 5542 | * ecdsa_secp256r1_sha256( 0x0403 ), |
| 5543 | * ecdsa_secp384r1_sha384( 0x0503 ), |
| 5544 | * ecdsa_secp521r1_sha512( 0x0603 ), |
| 5545 | * .... |
| 5546 | * } SignatureScheme; |
| 5547 | * |
| 5548 | * struct { |
| 5549 | * SignatureScheme supported_signature_algorithms<2..2^16-2>; |
| 5550 | * } SignatureSchemeList; |
| 5551 | * |
| 5552 | * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm` |
| 5553 | * value (TLS 1.2 RFC5246): |
| 5554 | * enum { |
| 5555 | * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5), |
| 5556 | * sha512(6), (255) |
| 5557 | * } HashAlgorithm; |
| 5558 | * |
| 5559 | * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) } |
| 5560 | * SignatureAlgorithm; |
| 5561 | * |
| 5562 | * struct { |
| 5563 | * HashAlgorithm hash; |
| 5564 | * SignatureAlgorithm signature; |
| 5565 | * } SignatureAndHashAlgorithm; |
| 5566 | * |
| 5567 | * SignatureAndHashAlgorithm |
| 5568 | * supported_signature_algorithms<2..2^16-2>; |
| 5569 | * |
| 5570 | * The TLS 1.3 signature algorithm extension was defined to be a compatible |
| 5571 | * generalization of the TLS 1.2 signature algorithm extension. |
| 5572 | * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by |
| 5573 | * `SignatureScheme` field of TLS 1.3 |
| 5574 | * |
| 5575 | */ |
| 5576 | int mbedtls_ssl_parse_sig_alg_ext( mbedtls_ssl_context *ssl, |
| 5577 | const unsigned char *buf, |
| 5578 | const unsigned char *end ) |
| 5579 | { |
| 5580 | const unsigned char *p = buf; |
| 5581 | size_t supported_sig_algs_len = 0; |
| 5582 | const unsigned char *supported_sig_algs_end; |
| 5583 | uint16_t sig_alg; |
| 5584 | uint32_t common_idx = 0; |
| 5585 | |
| 5586 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 ); |
| 5587 | supported_sig_algs_len = MBEDTLS_GET_UINT16_BE( p, 0 ); |
| 5588 | p += 2; |
| 5589 | |
| 5590 | memset( ssl->handshake->received_sig_algs, 0, |
| 5591 | sizeof(ssl->handshake->received_sig_algs) ); |
| 5592 | |
| 5593 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, supported_sig_algs_len ); |
| 5594 | supported_sig_algs_end = p + supported_sig_algs_len; |
| 5595 | while( p < supported_sig_algs_end ) |
| 5596 | { |
| 5597 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, supported_sig_algs_end, 2 ); |
| 5598 | sig_alg = MBEDTLS_GET_UINT16_BE( p, 0 ); |
| 5599 | p += 2; |
Jerry Yu | f3b46b5 | 2022-06-19 16:52:27 +0800 | [diff] [blame] | 5600 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "received signature algorithm: 0x%x %s", |
| 5601 | sig_alg, |
| 5602 | mbedtls_ssl_sig_alg_to_str( sig_alg ) ) ); |
Jerry Yu | 2fe6c63 | 2022-06-29 10:02:38 +0800 | [diff] [blame] | 5603 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Jerry Yu | 52b7d92 | 2022-07-01 18:03:31 +0800 | [diff] [blame] | 5604 | if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 && |
Jerry Yu | 2fe6c63 | 2022-06-29 10:02:38 +0800 | [diff] [blame] | 5605 | ( ! ( mbedtls_ssl_sig_alg_is_supported( ssl, sig_alg ) && |
| 5606 | mbedtls_ssl_sig_alg_is_offered( ssl, sig_alg ) ) ) ) |
| 5607 | { |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 5608 | continue; |
Jerry Yu | 2fe6c63 | 2022-06-29 10:02:38 +0800 | [diff] [blame] | 5609 | } |
| 5610 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 5611 | |
Jerry Yu | f3b46b5 | 2022-06-19 16:52:27 +0800 | [diff] [blame] | 5612 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "valid signature algorithm: %s", |
| 5613 | mbedtls_ssl_sig_alg_to_str( sig_alg ) ) ); |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 5614 | |
| 5615 | if( common_idx + 1 < MBEDTLS_RECEIVED_SIG_ALGS_SIZE ) |
| 5616 | { |
| 5617 | ssl->handshake->received_sig_algs[common_idx] = sig_alg; |
| 5618 | common_idx += 1; |
| 5619 | } |
| 5620 | } |
| 5621 | /* Check that we consumed all the message. */ |
| 5622 | if( p != end ) |
| 5623 | { |
| 5624 | MBEDTLS_SSL_DEBUG_MSG( 1, |
| 5625 | ( "Signature algorithms extension length misaligned" ) ); |
| 5626 | MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR, |
| 5627 | MBEDTLS_ERR_SSL_DECODE_ERROR ); |
| 5628 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
| 5629 | } |
| 5630 | |
| 5631 | if( common_idx == 0 ) |
| 5632 | { |
| 5633 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "no signature algorithm in common" ) ); |
| 5634 | MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE, |
| 5635 | MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE ); |
| 5636 | return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE ); |
| 5637 | } |
| 5638 | |
Gabor Mezei | 15b95a6 | 2022-05-09 16:37:58 +0200 | [diff] [blame] | 5639 | ssl->handshake->received_sig_algs[common_idx] = MBEDTLS_TLS_SIG_NONE; |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 5640 | return( 0 ); |
| 5641 | } |
| 5642 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5643 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 5644 | |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5645 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 5646 | |
| 5647 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 5648 | |
| 5649 | static psa_status_t setup_psa_key_derivation( psa_key_derivation_operation_t* derivation, |
| 5650 | mbedtls_svc_key_id_t key, |
| 5651 | psa_algorithm_t alg, |
Neil Armstrong | 8ecd668 | 2022-05-05 11:40:35 +0200 | [diff] [blame] | 5652 | const unsigned char* raw_psk, size_t raw_psk_length, |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5653 | const unsigned char* seed, size_t seed_length, |
| 5654 | const unsigned char* label, size_t label_length, |
Przemek Stekiel | 51a1f36 | 2022-04-13 08:57:06 +0200 | [diff] [blame] | 5655 | const unsigned char* other_secret, |
| 5656 | size_t other_secret_length, |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5657 | size_t capacity ) |
| 5658 | { |
| 5659 | psa_status_t status; |
| 5660 | |
| 5661 | status = psa_key_derivation_setup( derivation, alg ); |
| 5662 | if( status != PSA_SUCCESS ) |
| 5663 | return( status ); |
| 5664 | |
| 5665 | if( PSA_ALG_IS_TLS12_PRF( alg ) || PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) ) |
| 5666 | { |
| 5667 | status = psa_key_derivation_input_bytes( derivation, |
| 5668 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 5669 | seed, seed_length ); |
| 5670 | if( status != PSA_SUCCESS ) |
| 5671 | return( status ); |
| 5672 | |
Przemek Stekiel | 51a1f36 | 2022-04-13 08:57:06 +0200 | [diff] [blame] | 5673 | if ( other_secret != NULL ) |
Przemek Stekiel | 1f02703 | 2022-04-05 17:12:11 +0200 | [diff] [blame] | 5674 | { |
| 5675 | status = psa_key_derivation_input_bytes( derivation, |
Przemek Stekiel | 51a1f36 | 2022-04-13 08:57:06 +0200 | [diff] [blame] | 5676 | PSA_KEY_DERIVATION_INPUT_OTHER_SECRET, |
| 5677 | other_secret, other_secret_length ); |
Przemek Stekiel | 1f02703 | 2022-04-05 17:12:11 +0200 | [diff] [blame] | 5678 | if( status != PSA_SUCCESS ) |
| 5679 | return( status ); |
| 5680 | } |
| 5681 | |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5682 | if( mbedtls_svc_key_id_is_null( key ) ) |
| 5683 | { |
| 5684 | status = psa_key_derivation_input_bytes( |
| 5685 | derivation, PSA_KEY_DERIVATION_INPUT_SECRET, |
Neil Armstrong | 8ecd668 | 2022-05-05 11:40:35 +0200 | [diff] [blame] | 5686 | raw_psk, raw_psk_length ); |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5687 | } |
| 5688 | else |
| 5689 | { |
| 5690 | status = psa_key_derivation_input_key( |
| 5691 | derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key ); |
| 5692 | } |
| 5693 | if( status != PSA_SUCCESS ) |
| 5694 | return( status ); |
| 5695 | |
| 5696 | status = psa_key_derivation_input_bytes( derivation, |
| 5697 | PSA_KEY_DERIVATION_INPUT_LABEL, |
| 5698 | label, label_length ); |
| 5699 | if( status != PSA_SUCCESS ) |
| 5700 | return( status ); |
| 5701 | } |
| 5702 | else |
| 5703 | { |
| 5704 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5705 | } |
| 5706 | |
| 5707 | status = psa_key_derivation_set_capacity( derivation, capacity ); |
| 5708 | if( status != PSA_SUCCESS ) |
| 5709 | return( status ); |
| 5710 | |
| 5711 | return( PSA_SUCCESS ); |
| 5712 | } |
| 5713 | |
Andrzej Kurek | 57d1063 | 2022-10-24 10:32:01 -0400 | [diff] [blame] | 5714 | #if defined(PSA_WANT_ALG_SHA_384) || \ |
| 5715 | defined(PSA_WANT_ALG_SHA_256) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 5716 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5717 | static int tls_prf_generic( mbedtls_md_type_t md_type, |
| 5718 | const unsigned char *secret, size_t slen, |
| 5719 | const char *label, |
| 5720 | const unsigned char *random, size_t rlen, |
| 5721 | unsigned char *dstbuf, size_t dlen ) |
| 5722 | { |
| 5723 | psa_status_t status; |
| 5724 | psa_algorithm_t alg; |
| 5725 | mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5726 | psa_key_derivation_operation_t derivation = |
| 5727 | PSA_KEY_DERIVATION_OPERATION_INIT; |
| 5728 | |
| 5729 | if( md_type == MBEDTLS_MD_SHA384 ) |
| 5730 | alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384); |
| 5731 | else |
| 5732 | alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256); |
| 5733 | |
| 5734 | /* Normally a "secret" should be long enough to be impossible to |
| 5735 | * find by brute force, and in particular should not be empty. But |
| 5736 | * this PRF is also used to derive an IV, in particular in EAP-TLS, |
| 5737 | * and for this use case it makes sense to have a 0-length "secret". |
| 5738 | * Since the key API doesn't allow importing a key of length 0, |
| 5739 | * keep master_key=0, which setup_psa_key_derivation() understands |
| 5740 | * to mean a 0-length "secret" input. */ |
| 5741 | if( slen != 0 ) |
| 5742 | { |
| 5743 | psa_key_attributes_t key_attributes = psa_key_attributes_init(); |
| 5744 | psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE ); |
| 5745 | psa_set_key_algorithm( &key_attributes, alg ); |
| 5746 | psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE ); |
| 5747 | |
| 5748 | status = psa_import_key( &key_attributes, secret, slen, &master_key ); |
| 5749 | if( status != PSA_SUCCESS ) |
| 5750 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 5751 | } |
| 5752 | |
| 5753 | status = setup_psa_key_derivation( &derivation, |
| 5754 | master_key, alg, |
Neil Armstrong | 8ecd668 | 2022-05-05 11:40:35 +0200 | [diff] [blame] | 5755 | NULL, 0, |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5756 | random, rlen, |
| 5757 | (unsigned char const *) label, |
| 5758 | (size_t) strlen( label ), |
Przemek Stekiel | 1f02703 | 2022-04-05 17:12:11 +0200 | [diff] [blame] | 5759 | NULL, 0, |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5760 | dlen ); |
| 5761 | if( status != PSA_SUCCESS ) |
| 5762 | { |
| 5763 | psa_key_derivation_abort( &derivation ); |
| 5764 | psa_destroy_key( master_key ); |
| 5765 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 5766 | } |
| 5767 | |
| 5768 | status = psa_key_derivation_output_bytes( &derivation, dstbuf, dlen ); |
| 5769 | if( status != PSA_SUCCESS ) |
| 5770 | { |
| 5771 | psa_key_derivation_abort( &derivation ); |
| 5772 | psa_destroy_key( master_key ); |
| 5773 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 5774 | } |
| 5775 | |
| 5776 | status = psa_key_derivation_abort( &derivation ); |
| 5777 | if( status != PSA_SUCCESS ) |
| 5778 | { |
| 5779 | psa_destroy_key( master_key ); |
| 5780 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 5781 | } |
| 5782 | |
| 5783 | if( ! mbedtls_svc_key_id_is_null( master_key ) ) |
| 5784 | status = psa_destroy_key( master_key ); |
| 5785 | if( status != PSA_SUCCESS ) |
| 5786 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 5787 | |
| 5788 | return( 0 ); |
| 5789 | } |
Andrzej Kurek | 57d1063 | 2022-10-24 10:32:01 -0400 | [diff] [blame] | 5790 | #endif /* PSA_WANT_ALG_SHA_256 || PSA_WANT_ALG_SHA_384 */ |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5791 | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
| 5792 | |
Andrzej Kurek | 57d1063 | 2022-10-24 10:32:01 -0400 | [diff] [blame] | 5793 | #if defined(MBEDTLS_MD_C) && \ |
| 5794 | ( defined(MBEDTLS_SHA256_C) || \ |
| 5795 | defined(MBEDTLS_SHA384_C) ) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 5796 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5797 | static int tls_prf_generic( mbedtls_md_type_t md_type, |
| 5798 | const unsigned char *secret, size_t slen, |
| 5799 | const char *label, |
| 5800 | const unsigned char *random, size_t rlen, |
| 5801 | unsigned char *dstbuf, size_t dlen ) |
| 5802 | { |
| 5803 | size_t nb; |
| 5804 | size_t i, j, k, md_len; |
| 5805 | unsigned char *tmp; |
| 5806 | size_t tmp_len = 0; |
| 5807 | unsigned char h_i[MBEDTLS_MD_MAX_SIZE]; |
| 5808 | const mbedtls_md_info_t *md_info; |
| 5809 | mbedtls_md_context_t md_ctx; |
| 5810 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 5811 | |
| 5812 | mbedtls_md_init( &md_ctx ); |
| 5813 | |
| 5814 | if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL ) |
| 5815 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 5816 | |
| 5817 | md_len = mbedtls_md_get_size( md_info ); |
| 5818 | |
| 5819 | tmp_len = md_len + strlen( label ) + rlen; |
| 5820 | tmp = mbedtls_calloc( 1, tmp_len ); |
| 5821 | if( tmp == NULL ) |
| 5822 | { |
| 5823 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 5824 | goto exit; |
| 5825 | } |
| 5826 | |
| 5827 | nb = strlen( label ); |
| 5828 | memcpy( tmp + md_len, label, nb ); |
| 5829 | memcpy( tmp + md_len + nb, random, rlen ); |
| 5830 | nb += rlen; |
| 5831 | |
| 5832 | /* |
| 5833 | * Compute P_<hash>(secret, label + random)[0..dlen] |
| 5834 | */ |
| 5835 | if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 ) |
| 5836 | goto exit; |
| 5837 | |
| 5838 | ret = mbedtls_md_hmac_starts( &md_ctx, secret, slen ); |
| 5839 | if( ret != 0 ) |
| 5840 | goto exit; |
| 5841 | ret = mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb ); |
| 5842 | if( ret != 0 ) |
| 5843 | goto exit; |
| 5844 | ret = mbedtls_md_hmac_finish( &md_ctx, tmp ); |
| 5845 | if( ret != 0 ) |
| 5846 | goto exit; |
| 5847 | |
| 5848 | for( i = 0; i < dlen; i += md_len ) |
| 5849 | { |
| 5850 | ret = mbedtls_md_hmac_reset ( &md_ctx ); |
| 5851 | if( ret != 0 ) |
| 5852 | goto exit; |
| 5853 | ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb ); |
| 5854 | if( ret != 0 ) |
| 5855 | goto exit; |
| 5856 | ret = mbedtls_md_hmac_finish( &md_ctx, h_i ); |
| 5857 | if( ret != 0 ) |
| 5858 | goto exit; |
| 5859 | |
| 5860 | ret = mbedtls_md_hmac_reset ( &md_ctx ); |
| 5861 | if( ret != 0 ) |
| 5862 | goto exit; |
| 5863 | ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len ); |
| 5864 | if( ret != 0 ) |
| 5865 | goto exit; |
| 5866 | ret = mbedtls_md_hmac_finish( &md_ctx, tmp ); |
| 5867 | if( ret != 0 ) |
| 5868 | goto exit; |
| 5869 | |
| 5870 | k = ( i + md_len > dlen ) ? dlen % md_len : md_len; |
| 5871 | |
| 5872 | for( j = 0; j < k; j++ ) |
| 5873 | dstbuf[i + j] = h_i[j]; |
| 5874 | } |
| 5875 | |
| 5876 | exit: |
| 5877 | mbedtls_md_free( &md_ctx ); |
| 5878 | |
| 5879 | mbedtls_platform_zeroize( tmp, tmp_len ); |
| 5880 | mbedtls_platform_zeroize( h_i, sizeof( h_i ) ); |
| 5881 | |
| 5882 | mbedtls_free( tmp ); |
| 5883 | |
| 5884 | return( ret ); |
| 5885 | } |
Andrzej Kurek | 57d1063 | 2022-10-24 10:32:01 -0400 | [diff] [blame] | 5886 | #endif /* MBEDTLS_MD_C && ( MBEDTLS_SHA256_C || MBEDTLS_SHA384_C ) */ |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5887 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 5888 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5889 | #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] | 5890 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5891 | static int tls_prf_sha256( const unsigned char *secret, size_t slen, |
| 5892 | const char *label, |
| 5893 | const unsigned char *random, size_t rlen, |
| 5894 | unsigned char *dstbuf, size_t dlen ) |
| 5895 | { |
| 5896 | return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen, |
| 5897 | label, random, rlen, dstbuf, dlen ) ); |
| 5898 | } |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 5899 | #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] | 5900 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5901 | #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] | 5902 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5903 | static int tls_prf_sha384( const unsigned char *secret, size_t slen, |
| 5904 | const char *label, |
| 5905 | const unsigned char *random, size_t rlen, |
| 5906 | unsigned char *dstbuf, size_t dlen ) |
| 5907 | { |
| 5908 | return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen, |
| 5909 | label, random, rlen, dstbuf, dlen ) ); |
| 5910 | } |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 5911 | #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] | 5912 | |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5913 | /* |
| 5914 | * Set appropriate PRF function and other SSL / TLS1.2 functions |
| 5915 | * |
| 5916 | * Inputs: |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5917 | * - hash associated with the ciphersuite (only used by TLS 1.2) |
| 5918 | * |
| 5919 | * Outputs: |
| 5920 | * - the tls_prf, calc_verify and calc_finished members of handshake structure |
| 5921 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 5922 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5923 | static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake, |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5924 | mbedtls_md_type_t hash ) |
| 5925 | { |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5926 | #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] | 5927 | if( hash == MBEDTLS_MD_SHA384 ) |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5928 | { |
| 5929 | handshake->tls_prf = tls_prf_sha384; |
| 5930 | handshake->calc_verify = ssl_calc_verify_tls_sha384; |
| 5931 | handshake->calc_finished = ssl_calc_finished_tls_sha384; |
| 5932 | } |
| 5933 | else |
| 5934 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5935 | #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] | 5936 | { |
Ronald Cron | 81591aa | 2022-03-07 09:05:51 +0100 | [diff] [blame] | 5937 | (void) hash; |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5938 | handshake->tls_prf = tls_prf_sha256; |
| 5939 | handshake->calc_verify = ssl_calc_verify_tls_sha256; |
| 5940 | handshake->calc_finished = ssl_calc_finished_tls_sha256; |
| 5941 | } |
Ronald Cron | 81591aa | 2022-03-07 09:05:51 +0100 | [diff] [blame] | 5942 | #else |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5943 | { |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5944 | (void) handshake; |
Ronald Cron | 81591aa | 2022-03-07 09:05:51 +0100 | [diff] [blame] | 5945 | (void) hash; |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5946 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 5947 | } |
Ronald Cron | 81591aa | 2022-03-07 09:05:51 +0100 | [diff] [blame] | 5948 | #endif |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5949 | |
| 5950 | return( 0 ); |
| 5951 | } |
Jerry Yu | d6ab235 | 2022-02-17 14:03:43 +0800 | [diff] [blame] | 5952 | |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5953 | /* |
| 5954 | * Compute master secret if needed |
| 5955 | * |
| 5956 | * Parameters: |
| 5957 | * [in/out] handshake |
| 5958 | * [in] resume, premaster, extended_ms, calc_verify, tls_prf |
| 5959 | * (PSA-PSK) ciphersuite_info, psk_opaque |
| 5960 | * [out] premaster (cleared) |
| 5961 | * [out] master |
| 5962 | * [in] ssl: optionally used for debugging, EMS and PSA-PSK |
| 5963 | * debug: conf->f_dbg, conf->p_dbg |
| 5964 | * EMS: passed to calc_verify (debug + session_negotiate) |
Ronald Cron | a25cf58 | 2022-03-07 11:10:36 +0100 | [diff] [blame] | 5965 | * PSA-PSA: conf |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5966 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 5967 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5968 | static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake, |
| 5969 | unsigned char *master, |
| 5970 | const mbedtls_ssl_context *ssl ) |
| 5971 | { |
| 5972 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 5973 | |
| 5974 | /* cf. RFC 5246, Section 8.1: |
| 5975 | * "The master secret is always exactly 48 bytes in length." */ |
| 5976 | size_t const master_secret_len = 48; |
| 5977 | |
| 5978 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 5979 | unsigned char session_hash[48]; |
| 5980 | #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */ |
| 5981 | |
| 5982 | /* The label for the KDF used for key expansion. |
| 5983 | * This is either "master secret" or "extended master secret" |
| 5984 | * depending on whether the Extended Master Secret extension |
| 5985 | * is used. */ |
| 5986 | char const *lbl = "master secret"; |
| 5987 | |
Przemek Stekiel | ae4ed30 | 2022-04-05 17:15:55 +0200 | [diff] [blame] | 5988 | /* The seed for the KDF used for key expansion. |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5989 | * - If the Extended Master Secret extension is not used, |
| 5990 | * this is ClientHello.Random + ServerHello.Random |
| 5991 | * (see Sect. 8.1 in RFC 5246). |
| 5992 | * - If the Extended Master Secret extension is used, |
| 5993 | * this is the transcript of the handshake so far. |
| 5994 | * (see Sect. 4 in RFC 7627). */ |
Przemek Stekiel | ae4ed30 | 2022-04-05 17:15:55 +0200 | [diff] [blame] | 5995 | unsigned char const *seed = handshake->randbytes; |
| 5996 | size_t seed_len = 64; |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5997 | |
| 5998 | #if !defined(MBEDTLS_DEBUG_C) && \ |
| 5999 | !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \ |
| 6000 | !(defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
| 6001 | defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)) |
| 6002 | ssl = NULL; /* make sure we don't use it except for those cases */ |
| 6003 | (void) ssl; |
| 6004 | #endif |
| 6005 | |
| 6006 | if( handshake->resume != 0 ) |
| 6007 | { |
| 6008 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) ); |
| 6009 | return( 0 ); |
| 6010 | } |
| 6011 | |
| 6012 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 6013 | if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED ) |
| 6014 | { |
| 6015 | lbl = "extended master secret"; |
Przemek Stekiel | ae4ed30 | 2022-04-05 17:15:55 +0200 | [diff] [blame] | 6016 | seed = session_hash; |
| 6017 | handshake->calc_verify( ssl, session_hash, &seed_len ); |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6018 | |
| 6019 | MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret", |
Przemek Stekiel | ae4ed30 | 2022-04-05 17:15:55 +0200 | [diff] [blame] | 6020 | session_hash, seed_len ); |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6021 | } |
Przemek Stekiel | 169bf0b | 2022-04-29 07:53:29 +0200 | [diff] [blame] | 6022 | #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */ |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6023 | |
Przemek Stekiel | 99114f3 | 2022-04-22 11:20:09 +0200 | [diff] [blame] | 6024 | #if defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
Przemek Stekiel | 8a4b7fd | 2022-04-28 09:22:22 +0200 | [diff] [blame] | 6025 | defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Neil Armstrong | e952a30 | 2022-05-03 10:22:14 +0200 | [diff] [blame] | 6026 | if( mbedtls_ssl_ciphersuite_uses_psk( handshake->ciphersuite_info ) == 1 ) |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6027 | { |
| 6028 | /* Perform PSK-to-MS expansion in a single step. */ |
| 6029 | psa_status_t status; |
| 6030 | psa_algorithm_t alg; |
| 6031 | mbedtls_svc_key_id_t psk; |
| 6032 | psa_key_derivation_operation_t derivation = |
| 6033 | PSA_KEY_DERIVATION_OPERATION_INIT; |
| 6034 | mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac; |
| 6035 | |
| 6036 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) ); |
| 6037 | |
| 6038 | psk = mbedtls_ssl_get_opaque_psk( ssl ); |
| 6039 | |
| 6040 | if( hash_alg == MBEDTLS_MD_SHA384 ) |
| 6041 | alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384); |
| 6042 | else |
| 6043 | alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256); |
| 6044 | |
Przemek Stekiel | 51a1f36 | 2022-04-13 08:57:06 +0200 | [diff] [blame] | 6045 | size_t other_secret_len = 0; |
| 6046 | unsigned char* other_secret = NULL; |
Przemek Stekiel | c203340 | 2022-04-05 17:19:41 +0200 | [diff] [blame] | 6047 | |
Przemek Stekiel | 19b80f8 | 2022-04-14 08:29:31 +0200 | [diff] [blame] | 6048 | switch( handshake->ciphersuite_info->key_exchange ) |
Przemek Stekiel | c203340 | 2022-04-05 17:19:41 +0200 | [diff] [blame] | 6049 | { |
Przemek Stekiel | 19b80f8 | 2022-04-14 08:29:31 +0200 | [diff] [blame] | 6050 | /* Provide other secret. |
Przemek Stekiel | 51a1f36 | 2022-04-13 08:57:06 +0200 | [diff] [blame] | 6051 | * Other secret is stored in premaster, where first 2 bytes hold the |
Przemek Stekiel | 19b80f8 | 2022-04-14 08:29:31 +0200 | [diff] [blame] | 6052 | * length of the other key. |
Przemek Stekiel | c203340 | 2022-04-05 17:19:41 +0200 | [diff] [blame] | 6053 | */ |
Przemek Stekiel | 19b80f8 | 2022-04-14 08:29:31 +0200 | [diff] [blame] | 6054 | case MBEDTLS_KEY_EXCHANGE_RSA_PSK: |
Przemek Stekiel | 8abcee9 | 2022-04-28 09:16:28 +0200 | [diff] [blame] | 6055 | /* For RSA-PSK other key length is always 48 bytes. */ |
Przemek Stekiel | 19b80f8 | 2022-04-14 08:29:31 +0200 | [diff] [blame] | 6056 | other_secret_len = 48; |
| 6057 | other_secret = handshake->premaster + 2; |
| 6058 | break; |
| 6059 | case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK: |
Przemek Stekiel | b293aaa | 2022-04-19 12:22:38 +0200 | [diff] [blame] | 6060 | case MBEDTLS_KEY_EXCHANGE_DHE_PSK: |
Przemek Stekiel | 19b80f8 | 2022-04-14 08:29:31 +0200 | [diff] [blame] | 6061 | other_secret_len = MBEDTLS_GET_UINT16_BE(handshake->premaster, 0); |
| 6062 | other_secret = handshake->premaster + 2; |
| 6063 | break; |
| 6064 | default: |
| 6065 | break; |
Przemek Stekiel | c203340 | 2022-04-05 17:19:41 +0200 | [diff] [blame] | 6066 | } |
| 6067 | |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6068 | status = setup_psa_key_derivation( &derivation, psk, alg, |
Neil Armstrong | 8ecd668 | 2022-05-05 11:40:35 +0200 | [diff] [blame] | 6069 | ssl->conf->psk, ssl->conf->psk_len, |
Przemek Stekiel | ae4ed30 | 2022-04-05 17:15:55 +0200 | [diff] [blame] | 6070 | seed, seed_len, |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6071 | (unsigned char const *) lbl, |
| 6072 | (size_t) strlen( lbl ), |
Przemek Stekiel | 51a1f36 | 2022-04-13 08:57:06 +0200 | [diff] [blame] | 6073 | other_secret, other_secret_len, |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6074 | master_secret_len ); |
| 6075 | if( status != PSA_SUCCESS ) |
| 6076 | { |
| 6077 | psa_key_derivation_abort( &derivation ); |
| 6078 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 6079 | } |
| 6080 | |
| 6081 | status = psa_key_derivation_output_bytes( &derivation, |
| 6082 | master, |
| 6083 | master_secret_len ); |
| 6084 | if( status != PSA_SUCCESS ) |
| 6085 | { |
| 6086 | psa_key_derivation_abort( &derivation ); |
| 6087 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 6088 | } |
| 6089 | |
| 6090 | status = psa_key_derivation_abort( &derivation ); |
| 6091 | if( status != PSA_SUCCESS ) |
| 6092 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 6093 | } |
| 6094 | else |
| 6095 | #endif |
| 6096 | { |
| 6097 | ret = handshake->tls_prf( handshake->premaster, handshake->pmslen, |
Przemek Stekiel | ae4ed30 | 2022-04-05 17:15:55 +0200 | [diff] [blame] | 6098 | lbl, seed, seed_len, |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6099 | master, |
| 6100 | master_secret_len ); |
| 6101 | if( ret != 0 ) |
| 6102 | { |
| 6103 | MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret ); |
| 6104 | return( ret ); |
| 6105 | } |
| 6106 | |
| 6107 | MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", |
| 6108 | handshake->premaster, |
| 6109 | handshake->pmslen ); |
| 6110 | |
| 6111 | mbedtls_platform_zeroize( handshake->premaster, |
| 6112 | sizeof(handshake->premaster) ); |
| 6113 | } |
| 6114 | |
| 6115 | return( 0 ); |
| 6116 | } |
| 6117 | |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 6118 | int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl ) |
| 6119 | { |
| 6120 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 6121 | const mbedtls_ssl_ciphersuite_t * const ciphersuite_info = |
| 6122 | ssl->handshake->ciphersuite_info; |
| 6123 | |
| 6124 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) ); |
| 6125 | |
| 6126 | /* Set PRF, calc_verify and calc_finished function pointers */ |
| 6127 | ret = ssl_set_handshake_prfs( ssl->handshake, |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 6128 | ciphersuite_info->mac ); |
| 6129 | if( ret != 0 ) |
| 6130 | { |
| 6131 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret ); |
| 6132 | return( ret ); |
| 6133 | } |
| 6134 | |
| 6135 | /* Compute master secret if needed */ |
| 6136 | ret = ssl_compute_master( ssl->handshake, |
| 6137 | ssl->session_negotiate->master, |
| 6138 | ssl ); |
| 6139 | if( ret != 0 ) |
| 6140 | { |
| 6141 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret ); |
| 6142 | return( ret ); |
| 6143 | } |
| 6144 | |
| 6145 | /* Swap the client and server random values: |
| 6146 | * - MS derivation wanted client+server (RFC 5246 8.1) |
| 6147 | * - key derivation wants server+client (RFC 5246 6.3) */ |
| 6148 | { |
| 6149 | unsigned char tmp[64]; |
| 6150 | memcpy( tmp, ssl->handshake->randbytes, 64 ); |
| 6151 | memcpy( ssl->handshake->randbytes, tmp + 32, 32 ); |
| 6152 | memcpy( ssl->handshake->randbytes + 32, tmp, 32 ); |
| 6153 | mbedtls_platform_zeroize( tmp, sizeof( tmp ) ); |
| 6154 | } |
| 6155 | |
| 6156 | /* Populate transform structure */ |
| 6157 | ret = ssl_tls12_populate_transform( ssl->transform_negotiate, |
| 6158 | ssl->session_negotiate->ciphersuite, |
| 6159 | ssl->session_negotiate->master, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 6160 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 6161 | ssl->session_negotiate->encrypt_then_mac, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 6162 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 6163 | ssl->handshake->tls_prf, |
| 6164 | ssl->handshake->randbytes, |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 6165 | ssl->tls_version, |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 6166 | ssl->conf->endpoint, |
| 6167 | ssl ); |
| 6168 | if( ret != 0 ) |
| 6169 | { |
| 6170 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls12_populate_transform", ret ); |
| 6171 | return( ret ); |
| 6172 | } |
| 6173 | |
| 6174 | /* We no longer need Server/ClientHello.random values */ |
| 6175 | mbedtls_platform_zeroize( ssl->handshake->randbytes, |
| 6176 | sizeof( ssl->handshake->randbytes ) ); |
| 6177 | |
| 6178 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) ); |
| 6179 | |
| 6180 | return( 0 ); |
| 6181 | } |
Jerry Yu | 8392e0d | 2022-02-17 14:10:24 +0800 | [diff] [blame] | 6182 | |
Ronald Cron | 4dcbca9 | 2022-03-07 10:21:40 +0100 | [diff] [blame] | 6183 | int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md ) |
| 6184 | { |
Ronald Cron | 4dcbca9 | 2022-03-07 10:21:40 +0100 | [diff] [blame] | 6185 | switch( md ) |
| 6186 | { |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 6187 | #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] | 6188 | case MBEDTLS_SSL_HASH_SHA384: |
| 6189 | ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384; |
| 6190 | break; |
| 6191 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 6192 | #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] | 6193 | case MBEDTLS_SSL_HASH_SHA256: |
| 6194 | ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256; |
| 6195 | break; |
| 6196 | #endif |
| 6197 | default: |
| 6198 | return( -1 ); |
| 6199 | } |
Andrzej Kurek | eabeb30 | 2022-10-17 07:52:51 -0400 | [diff] [blame] | 6200 | #if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \ |
| 6201 | !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
| 6202 | (void) ssl; |
| 6203 | #endif |
Ronald Cron | c2f13a0 | 2022-03-07 10:25:24 +0100 | [diff] [blame] | 6204 | return( 0 ); |
Ronald Cron | 4dcbca9 | 2022-03-07 10:21:40 +0100 | [diff] [blame] | 6205 | } |
| 6206 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 6207 | #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] | 6208 | void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl, |
| 6209 | unsigned char *hash, |
| 6210 | size_t *hlen ) |
| 6211 | { |
| 6212 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 6213 | size_t hash_size; |
| 6214 | psa_status_t status; |
| 6215 | psa_hash_operation_t sha256_psa = psa_hash_operation_init(); |
| 6216 | |
| 6217 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) ); |
| 6218 | status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa ); |
| 6219 | if( status != PSA_SUCCESS ) |
| 6220 | { |
| 6221 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); |
| 6222 | return; |
| 6223 | } |
| 6224 | |
| 6225 | status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size ); |
| 6226 | if( status != PSA_SUCCESS ) |
| 6227 | { |
| 6228 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); |
| 6229 | return; |
| 6230 | } |
| 6231 | |
| 6232 | *hlen = 32; |
| 6233 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen ); |
| 6234 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) ); |
| 6235 | #else |
| 6236 | mbedtls_sha256_context sha256; |
| 6237 | |
| 6238 | mbedtls_sha256_init( &sha256 ); |
| 6239 | |
| 6240 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) ); |
| 6241 | |
| 6242 | mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 ); |
| 6243 | mbedtls_sha256_finish( &sha256, hash ); |
| 6244 | |
| 6245 | *hlen = 32; |
| 6246 | |
| 6247 | MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen ); |
| 6248 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) ); |
| 6249 | |
| 6250 | mbedtls_sha256_free( &sha256 ); |
| 6251 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 6252 | return; |
| 6253 | } |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 6254 | #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] | 6255 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 6256 | #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] | 6257 | void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl, |
| 6258 | unsigned char *hash, |
| 6259 | size_t *hlen ) |
| 6260 | { |
| 6261 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 6262 | size_t hash_size; |
| 6263 | psa_status_t status; |
| 6264 | psa_hash_operation_t sha384_psa = psa_hash_operation_init(); |
| 6265 | |
| 6266 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) ); |
| 6267 | status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa ); |
| 6268 | if( status != PSA_SUCCESS ) |
| 6269 | { |
| 6270 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); |
| 6271 | return; |
| 6272 | } |
| 6273 | |
| 6274 | status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size ); |
| 6275 | if( status != PSA_SUCCESS ) |
| 6276 | { |
| 6277 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); |
| 6278 | return; |
| 6279 | } |
| 6280 | |
| 6281 | *hlen = 48; |
| 6282 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen ); |
| 6283 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) ); |
| 6284 | #else |
| 6285 | mbedtls_sha512_context sha512; |
| 6286 | |
| 6287 | mbedtls_sha512_init( &sha512 ); |
| 6288 | |
| 6289 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) ); |
| 6290 | |
Andrzej Kurek | a242e83 | 2022-08-11 10:03:14 -0400 | [diff] [blame] | 6291 | mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 ); |
Jerry Yu | c1cb384 | 2022-02-17 14:13:48 +0800 | [diff] [blame] | 6292 | mbedtls_sha512_finish( &sha512, hash ); |
| 6293 | |
| 6294 | *hlen = 48; |
| 6295 | |
| 6296 | MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen ); |
| 6297 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) ); |
| 6298 | |
| 6299 | mbedtls_sha512_free( &sha512 ); |
| 6300 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 6301 | return; |
| 6302 | } |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 6303 | #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] | 6304 | |
Neil Armstrong | 80f6f32 | 2022-05-03 17:56:38 +0200 | [diff] [blame] | 6305 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
| 6306 | defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6307 | int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex ) |
| 6308 | { |
| 6309 | unsigned char *p = ssl->handshake->premaster; |
| 6310 | unsigned char *end = p + sizeof( ssl->handshake->premaster ); |
| 6311 | const unsigned char *psk = NULL; |
| 6312 | size_t psk_len = 0; |
Przemek Stekiel | b293aaa | 2022-04-19 12:22:38 +0200 | [diff] [blame] | 6313 | int psk_ret = mbedtls_ssl_get_psk( ssl, &psk, &psk_len ); |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6314 | |
Przemek Stekiel | b293aaa | 2022-04-19 12:22:38 +0200 | [diff] [blame] | 6315 | if( psk_ret == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED ) |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6316 | { |
| 6317 | /* |
| 6318 | * This should never happen because the existence of a PSK is always |
Przemek Stekiel | b293aaa | 2022-04-19 12:22:38 +0200 | [diff] [blame] | 6319 | * checked before calling this function. |
| 6320 | * |
| 6321 | * The exception is opaque DHE-PSK. For DHE-PSK fill premaster with |
Przemek Stekiel | 8abcee9 | 2022-04-28 09:16:28 +0200 | [diff] [blame] | 6322 | * the shared secret without PSK. |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6323 | */ |
Przemek Stekiel | b293aaa | 2022-04-19 12:22:38 +0200 | [diff] [blame] | 6324 | if ( key_ex != MBEDTLS_KEY_EXCHANGE_DHE_PSK ) |
| 6325 | { |
| 6326 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 6327 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 6328 | } |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6329 | } |
| 6330 | |
| 6331 | /* |
| 6332 | * PMS = struct { |
| 6333 | * opaque other_secret<0..2^16-1>; |
| 6334 | * opaque psk<0..2^16-1>; |
| 6335 | * }; |
| 6336 | * with "other_secret" depending on the particular key exchange |
| 6337 | */ |
| 6338 | #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) |
| 6339 | if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK ) |
| 6340 | { |
| 6341 | if( end - p < 2 ) |
| 6342 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6343 | |
| 6344 | MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 ); |
| 6345 | p += 2; |
| 6346 | |
| 6347 | if( end < p || (size_t)( end - p ) < psk_len ) |
| 6348 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6349 | |
| 6350 | memset( p, 0, psk_len ); |
| 6351 | p += psk_len; |
| 6352 | } |
| 6353 | else |
| 6354 | #endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */ |
| 6355 | #if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) |
| 6356 | if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK ) |
| 6357 | { |
| 6358 | /* |
| 6359 | * other_secret already set by the ClientKeyExchange message, |
| 6360 | * and is 48 bytes long |
| 6361 | */ |
| 6362 | if( end - p < 2 ) |
| 6363 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6364 | |
| 6365 | *p++ = 0; |
| 6366 | *p++ = 48; |
| 6367 | p += 48; |
| 6368 | } |
| 6369 | else |
| 6370 | #endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */ |
| 6371 | #if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) |
| 6372 | if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK ) |
| 6373 | { |
| 6374 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 6375 | size_t len; |
| 6376 | |
| 6377 | /* Write length only when we know the actual value */ |
| 6378 | if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx, |
| 6379 | p + 2, end - ( p + 2 ), &len, |
| 6380 | ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 ) |
| 6381 | { |
| 6382 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret ); |
| 6383 | return( ret ); |
| 6384 | } |
| 6385 | MBEDTLS_PUT_UINT16_BE( len, p, 0 ); |
| 6386 | p += 2 + len; |
| 6387 | |
| 6388 | MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K ); |
| 6389 | } |
| 6390 | else |
| 6391 | #endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */ |
Neil Armstrong | 80f6f32 | 2022-05-03 17:56:38 +0200 | [diff] [blame] | 6392 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6393 | if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ) |
| 6394 | { |
| 6395 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 6396 | size_t zlen; |
| 6397 | |
| 6398 | if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen, |
| 6399 | p + 2, end - ( p + 2 ), |
| 6400 | ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 ) |
| 6401 | { |
| 6402 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret ); |
| 6403 | return( ret ); |
| 6404 | } |
| 6405 | |
| 6406 | MBEDTLS_PUT_UINT16_BE( zlen, p, 0 ); |
| 6407 | p += 2 + zlen; |
| 6408 | |
| 6409 | MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx, |
| 6410 | MBEDTLS_DEBUG_ECDH_Z ); |
| 6411 | } |
| 6412 | else |
Neil Armstrong | 80f6f32 | 2022-05-03 17:56:38 +0200 | [diff] [blame] | 6413 | #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */ |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6414 | { |
| 6415 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 6416 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 6417 | } |
| 6418 | |
| 6419 | /* opaque psk<0..2^16-1>; */ |
| 6420 | if( end - p < 2 ) |
| 6421 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6422 | |
| 6423 | MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 ); |
| 6424 | p += 2; |
| 6425 | |
| 6426 | if( end < p || (size_t)( end - p ) < psk_len ) |
| 6427 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6428 | |
| 6429 | memcpy( p, psk, psk_len ); |
| 6430 | p += psk_len; |
| 6431 | |
| 6432 | ssl->handshake->pmslen = p - ssl->handshake->premaster; |
| 6433 | |
| 6434 | return( 0 ); |
| 6435 | } |
Neil Armstrong | 80f6f32 | 2022-05-03 17:56:38 +0200 | [diff] [blame] | 6436 | #endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ |
Jerry Yu | c2c673d | 2022-02-17 14:20:39 +0800 | [diff] [blame] | 6437 | |
| 6438 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6439 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | c2c673d | 2022-02-17 14:20:39 +0800 | [diff] [blame] | 6440 | static int ssl_write_hello_request( mbedtls_ssl_context *ssl ); |
| 6441 | |
| 6442 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 6443 | int mbedtls_ssl_resend_hello_request( mbedtls_ssl_context *ssl ) |
| 6444 | { |
| 6445 | /* If renegotiation is not enforced, retransmit until we would reach max |
| 6446 | * timeout if we were using the usual handshake doubling scheme */ |
| 6447 | if( ssl->conf->renego_max_records < 0 ) |
| 6448 | { |
| 6449 | uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1; |
| 6450 | unsigned char doublings = 1; |
| 6451 | |
| 6452 | while( ratio != 0 ) |
| 6453 | { |
| 6454 | ++doublings; |
| 6455 | ratio >>= 1; |
| 6456 | } |
| 6457 | |
| 6458 | if( ++ssl->renego_records_seen > doublings ) |
| 6459 | { |
| 6460 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) ); |
| 6461 | return( 0 ); |
| 6462 | } |
| 6463 | } |
| 6464 | |
| 6465 | return( ssl_write_hello_request( ssl ) ); |
| 6466 | } |
| 6467 | #endif |
| 6468 | #endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */ |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6469 | |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6470 | /* |
| 6471 | * Handshake functions |
| 6472 | */ |
| 6473 | #if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
| 6474 | /* No certificate support -> dummy functions */ |
| 6475 | int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl ) |
| 6476 | { |
| 6477 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 6478 | ssl->handshake->ciphersuite_info; |
| 6479 | |
| 6480 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) ); |
| 6481 | |
| 6482 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) |
| 6483 | { |
| 6484 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) ); |
| 6485 | ssl->state++; |
| 6486 | return( 0 ); |
| 6487 | } |
| 6488 | |
| 6489 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 6490 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 6491 | } |
| 6492 | |
| 6493 | int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl ) |
| 6494 | { |
| 6495 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 6496 | ssl->handshake->ciphersuite_info; |
| 6497 | |
| 6498 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) ); |
| 6499 | |
| 6500 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) |
| 6501 | { |
| 6502 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) ); |
| 6503 | ssl->state++; |
| 6504 | return( 0 ); |
| 6505 | } |
| 6506 | |
| 6507 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 6508 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 6509 | } |
| 6510 | |
| 6511 | #else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 6512 | /* Some certificate support -> implement write and parse */ |
| 6513 | |
| 6514 | int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl ) |
| 6515 | { |
| 6516 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
| 6517 | size_t i, n; |
| 6518 | const mbedtls_x509_crt *crt; |
| 6519 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 6520 | ssl->handshake->ciphersuite_info; |
| 6521 | |
| 6522 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) ); |
| 6523 | |
| 6524 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) |
| 6525 | { |
| 6526 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) ); |
| 6527 | ssl->state++; |
| 6528 | return( 0 ); |
| 6529 | } |
| 6530 | |
| 6531 | #if defined(MBEDTLS_SSL_CLI_C) |
| 6532 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 6533 | { |
| 6534 | if( ssl->handshake->client_auth == 0 ) |
| 6535 | { |
| 6536 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) ); |
| 6537 | ssl->state++; |
| 6538 | return( 0 ); |
| 6539 | } |
| 6540 | } |
| 6541 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 6542 | #if defined(MBEDTLS_SSL_SRV_C) |
| 6543 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 6544 | { |
| 6545 | if( mbedtls_ssl_own_cert( ssl ) == NULL ) |
| 6546 | { |
| 6547 | /* Should never happen because we shouldn't have picked the |
| 6548 | * ciphersuite if we don't have a certificate. */ |
| 6549 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 6550 | } |
| 6551 | } |
| 6552 | #endif |
| 6553 | |
| 6554 | MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) ); |
| 6555 | |
| 6556 | /* |
| 6557 | * 0 . 0 handshake type |
| 6558 | * 1 . 3 handshake length |
| 6559 | * 4 . 6 length of all certs |
| 6560 | * 7 . 9 length of cert. 1 |
| 6561 | * 10 . n-1 peer certificate |
| 6562 | * n . n+2 length of cert. 2 |
| 6563 | * n+3 . ... upper level cert, etc. |
| 6564 | */ |
| 6565 | i = 7; |
| 6566 | crt = mbedtls_ssl_own_cert( ssl ); |
| 6567 | |
| 6568 | while( crt != NULL ) |
| 6569 | { |
| 6570 | n = crt->raw.len; |
| 6571 | if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i ) |
| 6572 | { |
| 6573 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %" MBEDTLS_PRINTF_SIZET |
| 6574 | " > %" MBEDTLS_PRINTF_SIZET, |
| 6575 | i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN ) ); |
| 6576 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 6577 | } |
| 6578 | |
| 6579 | ssl->out_msg[i ] = MBEDTLS_BYTE_2( n ); |
| 6580 | ssl->out_msg[i + 1] = MBEDTLS_BYTE_1( n ); |
| 6581 | ssl->out_msg[i + 2] = MBEDTLS_BYTE_0( n ); |
| 6582 | |
| 6583 | i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n ); |
| 6584 | i += n; crt = crt->next; |
| 6585 | } |
| 6586 | |
| 6587 | ssl->out_msg[4] = MBEDTLS_BYTE_2( i - 7 ); |
| 6588 | ssl->out_msg[5] = MBEDTLS_BYTE_1( i - 7 ); |
| 6589 | ssl->out_msg[6] = MBEDTLS_BYTE_0( i - 7 ); |
| 6590 | |
| 6591 | ssl->out_msglen = i; |
| 6592 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 6593 | ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE; |
| 6594 | |
| 6595 | ssl->state++; |
| 6596 | |
| 6597 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
| 6598 | { |
| 6599 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); |
| 6600 | return( ret ); |
| 6601 | } |
| 6602 | |
| 6603 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) ); |
| 6604 | |
| 6605 | return( ret ); |
| 6606 | } |
| 6607 | |
| 6608 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) |
| 6609 | |
| 6610 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6611 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6612 | static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl, |
| 6613 | unsigned char *crt_buf, |
| 6614 | size_t crt_buf_len ) |
| 6615 | { |
| 6616 | mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert; |
| 6617 | |
| 6618 | if( peer_crt == NULL ) |
| 6619 | return( -1 ); |
| 6620 | |
| 6621 | if( peer_crt->raw.len != crt_buf_len ) |
| 6622 | return( -1 ); |
| 6623 | |
| 6624 | return( memcmp( peer_crt->raw.p, crt_buf, peer_crt->raw.len ) ); |
| 6625 | } |
| 6626 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6627 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6628 | static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl, |
| 6629 | unsigned char *crt_buf, |
| 6630 | size_t crt_buf_len ) |
| 6631 | { |
| 6632 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 6633 | unsigned char const * const peer_cert_digest = |
| 6634 | ssl->session->peer_cert_digest; |
| 6635 | mbedtls_md_type_t const peer_cert_digest_type = |
| 6636 | ssl->session->peer_cert_digest_type; |
| 6637 | mbedtls_md_info_t const * const digest_info = |
| 6638 | mbedtls_md_info_from_type( peer_cert_digest_type ); |
| 6639 | unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN]; |
| 6640 | size_t digest_len; |
| 6641 | |
| 6642 | if( peer_cert_digest == NULL || digest_info == NULL ) |
| 6643 | return( -1 ); |
| 6644 | |
| 6645 | digest_len = mbedtls_md_get_size( digest_info ); |
| 6646 | if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN ) |
| 6647 | return( -1 ); |
| 6648 | |
| 6649 | ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest ); |
| 6650 | if( ret != 0 ) |
| 6651 | return( -1 ); |
| 6652 | |
| 6653 | return( memcmp( tmp_digest, peer_cert_digest, digest_len ) ); |
| 6654 | } |
| 6655 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 6656 | #endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */ |
| 6657 | |
| 6658 | /* |
| 6659 | * Once the certificate message is read, parse it into a cert chain and |
| 6660 | * perform basic checks, but leave actual verification to the caller |
| 6661 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6662 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6663 | static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl, |
| 6664 | mbedtls_x509_crt *chain ) |
| 6665 | { |
| 6666 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 6667 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) |
| 6668 | int crt_cnt=0; |
| 6669 | #endif |
| 6670 | size_t i, n; |
| 6671 | uint8_t alert; |
| 6672 | |
| 6673 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) |
| 6674 | { |
| 6675 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
| 6676 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6677 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
| 6678 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
| 6679 | } |
| 6680 | |
| 6681 | if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ) |
| 6682 | { |
| 6683 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6684 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
| 6685 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
| 6686 | } |
| 6687 | |
| 6688 | if( ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 ) |
| 6689 | { |
| 6690 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
| 6691 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6692 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
| 6693 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
| 6694 | } |
| 6695 | |
| 6696 | i = mbedtls_ssl_hs_hdr_len( ssl ); |
| 6697 | |
| 6698 | /* |
| 6699 | * Same message structure as in mbedtls_ssl_write_certificate() |
| 6700 | */ |
| 6701 | n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2]; |
| 6702 | |
| 6703 | if( ssl->in_msg[i] != 0 || |
| 6704 | ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) ) |
| 6705 | { |
| 6706 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
| 6707 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6708 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
| 6709 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
| 6710 | } |
| 6711 | |
| 6712 | /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */ |
| 6713 | i += 3; |
| 6714 | |
| 6715 | /* Iterate through and parse the CRTs in the provided chain. */ |
| 6716 | while( i < ssl->in_hslen ) |
| 6717 | { |
| 6718 | /* Check that there's room for the next CRT's length fields. */ |
| 6719 | if ( i + 3 > ssl->in_hslen ) { |
| 6720 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
| 6721 | mbedtls_ssl_send_alert_message( ssl, |
| 6722 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6723 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
| 6724 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
| 6725 | } |
| 6726 | /* In theory, the CRT can be up to 2**24 Bytes, but we don't support |
| 6727 | * anything beyond 2**16 ~ 64K. */ |
| 6728 | if( ssl->in_msg[i] != 0 ) |
| 6729 | { |
| 6730 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
| 6731 | mbedtls_ssl_send_alert_message( ssl, |
| 6732 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6733 | MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT ); |
| 6734 | return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE ); |
| 6735 | } |
| 6736 | |
| 6737 | /* Read length of the next CRT in the chain. */ |
| 6738 | n = ( (unsigned int) ssl->in_msg[i + 1] << 8 ) |
| 6739 | | (unsigned int) ssl->in_msg[i + 2]; |
| 6740 | i += 3; |
| 6741 | |
| 6742 | if( n < 128 || i + n > ssl->in_hslen ) |
| 6743 | { |
| 6744 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
| 6745 | mbedtls_ssl_send_alert_message( ssl, |
| 6746 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6747 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
| 6748 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
| 6749 | } |
| 6750 | |
| 6751 | /* Check if we're handling the first CRT in the chain. */ |
| 6752 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) |
| 6753 | if( crt_cnt++ == 0 && |
| 6754 | ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
| 6755 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
| 6756 | { |
| 6757 | /* During client-side renegotiation, check that the server's |
| 6758 | * end-CRTs hasn't changed compared to the initial handshake, |
| 6759 | * mitigating the triple handshake attack. On success, reuse |
| 6760 | * the original end-CRT instead of parsing it again. */ |
| 6761 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) ); |
| 6762 | if( ssl_check_peer_crt_unchanged( ssl, |
| 6763 | &ssl->in_msg[i], |
| 6764 | n ) != 0 ) |
| 6765 | { |
| 6766 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) ); |
| 6767 | mbedtls_ssl_send_alert_message( ssl, |
| 6768 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6769 | MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED ); |
| 6770 | return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE ); |
| 6771 | } |
| 6772 | |
| 6773 | /* Now we can safely free the original chain. */ |
| 6774 | ssl_clear_peer_cert( ssl->session ); |
| 6775 | } |
| 6776 | #endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */ |
| 6777 | |
| 6778 | /* Parse the next certificate in the chain. */ |
| 6779 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 6780 | ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n ); |
| 6781 | #else |
| 6782 | /* If we don't need to store the CRT chain permanently, parse |
| 6783 | * it in-place from the input buffer instead of making a copy. */ |
| 6784 | ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n ); |
| 6785 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 6786 | switch( ret ) |
| 6787 | { |
| 6788 | case 0: /*ok*/ |
| 6789 | case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND: |
| 6790 | /* Ignore certificate with an unknown algorithm: maybe a |
| 6791 | prior certificate was already trusted. */ |
| 6792 | break; |
| 6793 | |
| 6794 | case MBEDTLS_ERR_X509_ALLOC_FAILED: |
| 6795 | alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR; |
| 6796 | goto crt_parse_der_failed; |
| 6797 | |
| 6798 | case MBEDTLS_ERR_X509_UNKNOWN_VERSION: |
| 6799 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 6800 | goto crt_parse_der_failed; |
| 6801 | |
| 6802 | default: |
| 6803 | alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT; |
| 6804 | crt_parse_der_failed: |
| 6805 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert ); |
| 6806 | MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret ); |
| 6807 | return( ret ); |
| 6808 | } |
| 6809 | |
| 6810 | i += n; |
| 6811 | } |
| 6812 | |
| 6813 | MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain ); |
| 6814 | return( 0 ); |
| 6815 | } |
| 6816 | |
| 6817 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6818 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6819 | static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl ) |
| 6820 | { |
| 6821 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 6822 | return( -1 ); |
| 6823 | |
| 6824 | if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) && |
| 6825 | ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 6826 | ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE && |
| 6827 | memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 ) |
| 6828 | { |
Ronald Cron | 1938588 | 2022-06-15 16:26:13 +0200 | [diff] [blame] | 6829 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "peer has no certificate" ) ); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6830 | return( 0 ); |
| 6831 | } |
| 6832 | return( -1 ); |
| 6833 | } |
| 6834 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 6835 | |
| 6836 | /* Check if a certificate message is expected. |
| 6837 | * Return either |
| 6838 | * - SSL_CERTIFICATE_EXPECTED, or |
| 6839 | * - SSL_CERTIFICATE_SKIP |
| 6840 | * indicating whether a Certificate message is expected or not. |
| 6841 | */ |
| 6842 | #define SSL_CERTIFICATE_EXPECTED 0 |
| 6843 | #define SSL_CERTIFICATE_SKIP 1 |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6844 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6845 | static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl, |
| 6846 | int authmode ) |
| 6847 | { |
| 6848 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 6849 | ssl->handshake->ciphersuite_info; |
| 6850 | |
| 6851 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) |
| 6852 | return( SSL_CERTIFICATE_SKIP ); |
| 6853 | |
| 6854 | #if defined(MBEDTLS_SSL_SRV_C) |
| 6855 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 6856 | { |
| 6857 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ) |
| 6858 | return( SSL_CERTIFICATE_SKIP ); |
| 6859 | |
| 6860 | if( authmode == MBEDTLS_SSL_VERIFY_NONE ) |
| 6861 | { |
| 6862 | ssl->session_negotiate->verify_result = |
| 6863 | MBEDTLS_X509_BADCERT_SKIP_VERIFY; |
| 6864 | return( SSL_CERTIFICATE_SKIP ); |
| 6865 | } |
| 6866 | } |
| 6867 | #else |
| 6868 | ((void) authmode); |
| 6869 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 6870 | |
| 6871 | return( SSL_CERTIFICATE_EXPECTED ); |
| 6872 | } |
| 6873 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6874 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6875 | static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl, |
| 6876 | int authmode, |
| 6877 | mbedtls_x509_crt *chain, |
| 6878 | void *rs_ctx ) |
| 6879 | { |
| 6880 | int ret = 0; |
| 6881 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 6882 | ssl->handshake->ciphersuite_info; |
| 6883 | int have_ca_chain = 0; |
| 6884 | |
| 6885 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *); |
| 6886 | void *p_vrfy; |
| 6887 | |
| 6888 | if( authmode == MBEDTLS_SSL_VERIFY_NONE ) |
| 6889 | return( 0 ); |
| 6890 | |
| 6891 | if( ssl->f_vrfy != NULL ) |
| 6892 | { |
| 6893 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) ); |
| 6894 | f_vrfy = ssl->f_vrfy; |
| 6895 | p_vrfy = ssl->p_vrfy; |
| 6896 | } |
| 6897 | else |
| 6898 | { |
| 6899 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) ); |
| 6900 | f_vrfy = ssl->conf->f_vrfy; |
| 6901 | p_vrfy = ssl->conf->p_vrfy; |
| 6902 | } |
| 6903 | |
| 6904 | /* |
| 6905 | * Main check: verify certificate |
| 6906 | */ |
| 6907 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 6908 | if( ssl->conf->f_ca_cb != NULL ) |
| 6909 | { |
| 6910 | ((void) rs_ctx); |
| 6911 | have_ca_chain = 1; |
| 6912 | |
| 6913 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) ); |
| 6914 | ret = mbedtls_x509_crt_verify_with_ca_cb( |
| 6915 | chain, |
| 6916 | ssl->conf->f_ca_cb, |
| 6917 | ssl->conf->p_ca_cb, |
| 6918 | ssl->conf->cert_profile, |
| 6919 | ssl->hostname, |
| 6920 | &ssl->session_negotiate->verify_result, |
| 6921 | f_vrfy, p_vrfy ); |
| 6922 | } |
| 6923 | else |
| 6924 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
| 6925 | { |
| 6926 | mbedtls_x509_crt *ca_chain; |
| 6927 | mbedtls_x509_crl *ca_crl; |
| 6928 | |
| 6929 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 6930 | if( ssl->handshake->sni_ca_chain != NULL ) |
| 6931 | { |
| 6932 | ca_chain = ssl->handshake->sni_ca_chain; |
| 6933 | ca_crl = ssl->handshake->sni_ca_crl; |
| 6934 | } |
| 6935 | else |
| 6936 | #endif |
| 6937 | { |
| 6938 | ca_chain = ssl->conf->ca_chain; |
| 6939 | ca_crl = ssl->conf->ca_crl; |
| 6940 | } |
| 6941 | |
| 6942 | if( ca_chain != NULL ) |
| 6943 | have_ca_chain = 1; |
| 6944 | |
| 6945 | ret = mbedtls_x509_crt_verify_restartable( |
| 6946 | chain, |
| 6947 | ca_chain, ca_crl, |
| 6948 | ssl->conf->cert_profile, |
| 6949 | ssl->hostname, |
| 6950 | &ssl->session_negotiate->verify_result, |
| 6951 | f_vrfy, p_vrfy, rs_ctx ); |
| 6952 | } |
| 6953 | |
| 6954 | if( ret != 0 ) |
| 6955 | { |
| 6956 | MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret ); |
| 6957 | } |
| 6958 | |
| 6959 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
| 6960 | if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS ) |
| 6961 | return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS ); |
| 6962 | #endif |
| 6963 | |
| 6964 | /* |
| 6965 | * Secondary checks: always done, but change 'ret' only if it was 0 |
| 6966 | */ |
| 6967 | |
| 6968 | #if defined(MBEDTLS_ECP_C) |
| 6969 | { |
| 6970 | const mbedtls_pk_context *pk = &chain->pk; |
| 6971 | |
Manuel Pégourié-Gonnard | 66b0d61 | 2022-06-17 10:49:29 +0200 | [diff] [blame] | 6972 | /* If certificate uses an EC key, make sure the curve is OK. |
| 6973 | * This is a public key, so it can't be opaque, so can_do() is a good |
| 6974 | * enough check to ensure pk_ec() is safe to use here. */ |
Leonid Rozenboim | 19e5973 | 2022-08-08 16:52:38 -0700 | [diff] [blame] | 6975 | if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) ) |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6976 | { |
Leonid Rozenboim | 19e5973 | 2022-08-08 16:52:38 -0700 | [diff] [blame] | 6977 | /* and in the unlikely case the above assumption no longer holds |
| 6978 | * we are making sure that pk_ec() here does not return a NULL |
| 6979 | */ |
| 6980 | const mbedtls_ecp_keypair *ec = mbedtls_pk_ec( *pk ); |
| 6981 | if( ec == NULL ) |
| 6982 | { |
Tom Cosgrove | 20c1137 | 2022-08-24 15:06:13 +0100 | [diff] [blame] | 6983 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_pk_ec() returned NULL" ) ); |
Leonid Rozenboim | 19e5973 | 2022-08-08 16:52:38 -0700 | [diff] [blame] | 6984 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 6985 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6986 | |
Leonid Rozenboim | 19e5973 | 2022-08-08 16:52:38 -0700 | [diff] [blame] | 6987 | if( mbedtls_ssl_check_curve( ssl, ec->grp.id ) != 0 ) |
| 6988 | { |
| 6989 | ssl->session_negotiate->verify_result |= |
| 6990 | MBEDTLS_X509_BADCERT_BAD_KEY; |
| 6991 | |
| 6992 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) ); |
| 6993 | if( ret == 0 ) |
| 6994 | ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE; |
| 6995 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6996 | } |
| 6997 | } |
| 6998 | #endif /* MBEDTLS_ECP_C */ |
| 6999 | |
| 7000 | if( mbedtls_ssl_check_cert_usage( chain, |
| 7001 | ciphersuite_info, |
| 7002 | ! ssl->conf->endpoint, |
| 7003 | &ssl->session_negotiate->verify_result ) != 0 ) |
| 7004 | { |
| 7005 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) ); |
| 7006 | if( ret == 0 ) |
| 7007 | ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE; |
| 7008 | } |
| 7009 | |
| 7010 | /* mbedtls_x509_crt_verify_with_profile is supposed to report a |
| 7011 | * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED, |
| 7012 | * with details encoded in the verification flags. All other kinds |
| 7013 | * of error codes, including those from the user provided f_vrfy |
| 7014 | * functions, are treated as fatal and lead to a failure of |
| 7015 | * ssl_parse_certificate even if verification was optional. */ |
| 7016 | if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL && |
| 7017 | ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED || |
| 7018 | ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE ) ) |
| 7019 | { |
| 7020 | ret = 0; |
| 7021 | } |
| 7022 | |
| 7023 | if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED ) |
| 7024 | { |
| 7025 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) ); |
| 7026 | ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED; |
| 7027 | } |
| 7028 | |
| 7029 | if( ret != 0 ) |
| 7030 | { |
| 7031 | uint8_t alert; |
| 7032 | |
| 7033 | /* The certificate may have been rejected for several reasons. |
| 7034 | Pick one and send the corresponding alert. Which alert to send |
| 7035 | may be a subject of debate in some cases. */ |
| 7036 | if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER ) |
| 7037 | alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED; |
| 7038 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH ) |
| 7039 | alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT; |
| 7040 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE ) |
| 7041 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 7042 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE ) |
| 7043 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 7044 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE ) |
| 7045 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 7046 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK ) |
| 7047 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 7048 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY ) |
| 7049 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 7050 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED ) |
| 7051 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED; |
| 7052 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED ) |
| 7053 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED; |
| 7054 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED ) |
| 7055 | alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA; |
| 7056 | else |
| 7057 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN; |
| 7058 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7059 | alert ); |
| 7060 | } |
| 7061 | |
| 7062 | #if defined(MBEDTLS_DEBUG_C) |
| 7063 | if( ssl->session_negotiate->verify_result != 0 ) |
| 7064 | { |
| 7065 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %08x", |
| 7066 | (unsigned int) ssl->session_negotiate->verify_result ) ); |
| 7067 | } |
| 7068 | else |
| 7069 | { |
| 7070 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) ); |
| 7071 | } |
| 7072 | #endif /* MBEDTLS_DEBUG_C */ |
| 7073 | |
| 7074 | return( ret ); |
| 7075 | } |
| 7076 | |
| 7077 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 7078 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7079 | static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl, |
| 7080 | unsigned char *start, size_t len ) |
| 7081 | { |
| 7082 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 7083 | /* Remember digest of the peer's end-CRT. */ |
| 7084 | ssl->session_negotiate->peer_cert_digest = |
| 7085 | mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ); |
| 7086 | if( ssl->session_negotiate->peer_cert_digest == NULL ) |
| 7087 | { |
| 7088 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", |
| 7089 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ); |
| 7090 | mbedtls_ssl_send_alert_message( ssl, |
| 7091 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7092 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
| 7093 | |
| 7094 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 7095 | } |
| 7096 | |
| 7097 | ret = mbedtls_md( mbedtls_md_info_from_type( |
| 7098 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ), |
| 7099 | start, len, |
| 7100 | ssl->session_negotiate->peer_cert_digest ); |
| 7101 | |
| 7102 | ssl->session_negotiate->peer_cert_digest_type = |
| 7103 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE; |
| 7104 | ssl->session_negotiate->peer_cert_digest_len = |
| 7105 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN; |
| 7106 | |
| 7107 | return( ret ); |
| 7108 | } |
| 7109 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 7110 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7111 | static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl, |
| 7112 | unsigned char *start, size_t len ) |
| 7113 | { |
| 7114 | unsigned char *end = start + len; |
| 7115 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 7116 | |
| 7117 | /* Make a copy of the peer's raw public key. */ |
| 7118 | mbedtls_pk_init( &ssl->handshake->peer_pubkey ); |
| 7119 | ret = mbedtls_pk_parse_subpubkey( &start, end, |
| 7120 | &ssl->handshake->peer_pubkey ); |
| 7121 | if( ret != 0 ) |
| 7122 | { |
| 7123 | /* We should have parsed the public key before. */ |
| 7124 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 7125 | } |
| 7126 | |
| 7127 | return( 0 ); |
| 7128 | } |
| 7129 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 7130 | |
| 7131 | int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl ) |
| 7132 | { |
| 7133 | int ret = 0; |
| 7134 | int crt_expected; |
| 7135 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 7136 | const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET |
| 7137 | ? ssl->handshake->sni_authmode |
| 7138 | : ssl->conf->authmode; |
| 7139 | #else |
| 7140 | const int authmode = ssl->conf->authmode; |
| 7141 | #endif |
| 7142 | void *rs_ctx = NULL; |
| 7143 | mbedtls_x509_crt *chain = NULL; |
| 7144 | |
| 7145 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) ); |
| 7146 | |
| 7147 | crt_expected = ssl_parse_certificate_coordinate( ssl, authmode ); |
| 7148 | if( crt_expected == SSL_CERTIFICATE_SKIP ) |
| 7149 | { |
| 7150 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) ); |
| 7151 | goto exit; |
| 7152 | } |
| 7153 | |
| 7154 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
| 7155 | if( ssl->handshake->ecrs_enabled && |
| 7156 | ssl->handshake->ecrs_state == ssl_ecrs_crt_verify ) |
| 7157 | { |
| 7158 | chain = ssl->handshake->ecrs_peer_cert; |
| 7159 | ssl->handshake->ecrs_peer_cert = NULL; |
| 7160 | goto crt_verify; |
| 7161 | } |
| 7162 | #endif |
| 7163 | |
| 7164 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) |
| 7165 | { |
| 7166 | /* mbedtls_ssl_read_record may have sent an alert already. We |
| 7167 | let it decide whether to alert. */ |
| 7168 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
| 7169 | goto exit; |
| 7170 | } |
| 7171 | |
| 7172 | #if defined(MBEDTLS_SSL_SRV_C) |
| 7173 | if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 ) |
| 7174 | { |
| 7175 | ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING; |
| 7176 | |
| 7177 | if( authmode != MBEDTLS_SSL_VERIFY_OPTIONAL ) |
| 7178 | ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE; |
| 7179 | |
| 7180 | goto exit; |
| 7181 | } |
| 7182 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 7183 | |
| 7184 | /* Clear existing peer CRT structure in case we tried to |
| 7185 | * reuse a session but it failed, and allocate a new one. */ |
| 7186 | ssl_clear_peer_cert( ssl->session_negotiate ); |
| 7187 | |
| 7188 | chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ); |
| 7189 | if( chain == NULL ) |
| 7190 | { |
| 7191 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", |
| 7192 | sizeof( mbedtls_x509_crt ) ) ); |
| 7193 | mbedtls_ssl_send_alert_message( ssl, |
| 7194 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7195 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
| 7196 | |
| 7197 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 7198 | goto exit; |
| 7199 | } |
| 7200 | mbedtls_x509_crt_init( chain ); |
| 7201 | |
| 7202 | ret = ssl_parse_certificate_chain( ssl, chain ); |
| 7203 | if( ret != 0 ) |
| 7204 | goto exit; |
| 7205 | |
| 7206 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
| 7207 | if( ssl->handshake->ecrs_enabled) |
| 7208 | ssl->handshake->ecrs_state = ssl_ecrs_crt_verify; |
| 7209 | |
| 7210 | crt_verify: |
| 7211 | if( ssl->handshake->ecrs_enabled) |
| 7212 | rs_ctx = &ssl->handshake->ecrs_ctx; |
| 7213 | #endif |
| 7214 | |
| 7215 | ret = ssl_parse_certificate_verify( ssl, authmode, |
| 7216 | chain, rs_ctx ); |
| 7217 | if( ret != 0 ) |
| 7218 | goto exit; |
| 7219 | |
| 7220 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 7221 | { |
| 7222 | unsigned char *crt_start, *pk_start; |
| 7223 | size_t crt_len, pk_len; |
| 7224 | |
| 7225 | /* We parse the CRT chain without copying, so |
| 7226 | * these pointers point into the input buffer, |
| 7227 | * and are hence still valid after freeing the |
| 7228 | * CRT chain. */ |
| 7229 | |
| 7230 | crt_start = chain->raw.p; |
| 7231 | crt_len = chain->raw.len; |
| 7232 | |
| 7233 | pk_start = chain->pk_raw.p; |
| 7234 | pk_len = chain->pk_raw.len; |
| 7235 | |
| 7236 | /* Free the CRT structures before computing |
| 7237 | * digest and copying the peer's public key. */ |
| 7238 | mbedtls_x509_crt_free( chain ); |
| 7239 | mbedtls_free( chain ); |
| 7240 | chain = NULL; |
| 7241 | |
| 7242 | ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len ); |
| 7243 | if( ret != 0 ) |
| 7244 | goto exit; |
| 7245 | |
| 7246 | ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len ); |
| 7247 | if( ret != 0 ) |
| 7248 | goto exit; |
| 7249 | } |
| 7250 | #else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 7251 | /* Pass ownership to session structure. */ |
| 7252 | ssl->session_negotiate->peer_cert = chain; |
| 7253 | chain = NULL; |
| 7254 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 7255 | |
| 7256 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) ); |
| 7257 | |
| 7258 | exit: |
| 7259 | |
| 7260 | if( ret == 0 ) |
| 7261 | ssl->state++; |
| 7262 | |
| 7263 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
| 7264 | if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS ) |
| 7265 | { |
| 7266 | ssl->handshake->ecrs_peer_cert = chain; |
| 7267 | chain = NULL; |
| 7268 | } |
| 7269 | #endif |
| 7270 | |
| 7271 | if( chain != NULL ) |
| 7272 | { |
| 7273 | mbedtls_x509_crt_free( chain ); |
| 7274 | mbedtls_free( chain ); |
| 7275 | } |
| 7276 | |
| 7277 | return( ret ); |
| 7278 | } |
| 7279 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 7280 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 7281 | #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] | 7282 | static void ssl_calc_finished_tls_sha256( |
| 7283 | mbedtls_ssl_context *ssl, unsigned char *buf, int from ) |
| 7284 | { |
| 7285 | int len = 12; |
| 7286 | const char *sender; |
| 7287 | unsigned char padbuf[32]; |
| 7288 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7289 | size_t hash_size; |
| 7290 | psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT; |
| 7291 | psa_status_t status; |
| 7292 | #else |
| 7293 | mbedtls_sha256_context sha256; |
| 7294 | #endif |
| 7295 | |
| 7296 | mbedtls_ssl_session *session = ssl->session_negotiate; |
| 7297 | if( !session ) |
| 7298 | session = ssl->session; |
| 7299 | |
| 7300 | sender = ( from == MBEDTLS_SSL_IS_CLIENT ) |
| 7301 | ? "client finished" |
| 7302 | : "server finished"; |
| 7303 | |
| 7304 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7305 | sha256_psa = psa_hash_operation_init(); |
| 7306 | |
| 7307 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) ); |
| 7308 | |
| 7309 | status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa ); |
| 7310 | if( status != PSA_SUCCESS ) |
| 7311 | { |
| 7312 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); |
| 7313 | return; |
| 7314 | } |
| 7315 | |
| 7316 | status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size ); |
| 7317 | if( status != PSA_SUCCESS ) |
| 7318 | { |
| 7319 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); |
| 7320 | return; |
| 7321 | } |
| 7322 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 ); |
| 7323 | #else |
| 7324 | |
| 7325 | mbedtls_sha256_init( &sha256 ); |
| 7326 | |
| 7327 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) ); |
| 7328 | |
| 7329 | mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 ); |
| 7330 | |
| 7331 | /* |
| 7332 | * TLSv1.2: |
| 7333 | * hash = PRF( master, finished_label, |
| 7334 | * Hash( handshake ) )[0.11] |
| 7335 | */ |
| 7336 | |
| 7337 | #if !defined(MBEDTLS_SHA256_ALT) |
| 7338 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *) |
| 7339 | sha256.state, sizeof( sha256.state ) ); |
| 7340 | #endif |
| 7341 | |
| 7342 | mbedtls_sha256_finish( &sha256, padbuf ); |
| 7343 | mbedtls_sha256_free( &sha256 ); |
| 7344 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 7345 | |
| 7346 | ssl->handshake->tls_prf( session->master, 48, sender, |
| 7347 | padbuf, 32, buf, len ); |
| 7348 | |
| 7349 | MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len ); |
| 7350 | |
| 7351 | mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) ); |
| 7352 | |
| 7353 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) ); |
| 7354 | } |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 7355 | #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] | 7356 | |
Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 7357 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 7358 | #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] | 7359 | static void ssl_calc_finished_tls_sha384( |
| 7360 | mbedtls_ssl_context *ssl, unsigned char *buf, int from ) |
| 7361 | { |
| 7362 | int len = 12; |
| 7363 | const char *sender; |
| 7364 | unsigned char padbuf[48]; |
| 7365 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7366 | size_t hash_size; |
| 7367 | psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT; |
| 7368 | psa_status_t status; |
| 7369 | #else |
| 7370 | mbedtls_sha512_context sha512; |
| 7371 | #endif |
| 7372 | |
| 7373 | mbedtls_ssl_session *session = ssl->session_negotiate; |
| 7374 | if( !session ) |
| 7375 | session = ssl->session; |
| 7376 | |
| 7377 | sender = ( from == MBEDTLS_SSL_IS_CLIENT ) |
| 7378 | ? "client finished" |
| 7379 | : "server finished"; |
| 7380 | |
| 7381 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7382 | sha384_psa = psa_hash_operation_init(); |
| 7383 | |
| 7384 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) ); |
| 7385 | |
| 7386 | status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa ); |
| 7387 | if( status != PSA_SUCCESS ) |
| 7388 | { |
| 7389 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); |
| 7390 | return; |
| 7391 | } |
| 7392 | |
| 7393 | status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size ); |
| 7394 | if( status != PSA_SUCCESS ) |
| 7395 | { |
| 7396 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); |
| 7397 | return; |
| 7398 | } |
| 7399 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 ); |
| 7400 | #else |
| 7401 | mbedtls_sha512_init( &sha512 ); |
| 7402 | |
| 7403 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) ); |
| 7404 | |
Andrzej Kurek | a242e83 | 2022-08-11 10:03:14 -0400 | [diff] [blame] | 7405 | mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 ); |
Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 7406 | |
| 7407 | /* |
| 7408 | * TLSv1.2: |
| 7409 | * hash = PRF( master, finished_label, |
| 7410 | * Hash( handshake ) )[0.11] |
| 7411 | */ |
| 7412 | |
| 7413 | #if !defined(MBEDTLS_SHA512_ALT) |
| 7414 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *) |
| 7415 | sha512.state, sizeof( sha512.state ) ); |
| 7416 | #endif |
| 7417 | mbedtls_sha512_finish( &sha512, padbuf ); |
| 7418 | |
| 7419 | mbedtls_sha512_free( &sha512 ); |
| 7420 | #endif |
| 7421 | |
| 7422 | ssl->handshake->tls_prf( session->master, 48, sender, |
| 7423 | padbuf, 48, buf, len ); |
| 7424 | |
| 7425 | MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len ); |
| 7426 | |
| 7427 | mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) ); |
| 7428 | |
| 7429 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) ); |
| 7430 | } |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 7431 | #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] | 7432 | |
Jerry Yu | aef0015 | 2022-02-17 14:27:31 +0800 | [diff] [blame] | 7433 | void mbedtls_ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl ) |
| 7434 | { |
| 7435 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) ); |
| 7436 | |
| 7437 | /* |
| 7438 | * Free our handshake params |
| 7439 | */ |
| 7440 | mbedtls_ssl_handshake_free( ssl ); |
| 7441 | mbedtls_free( ssl->handshake ); |
| 7442 | ssl->handshake = NULL; |
| 7443 | |
| 7444 | /* |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 7445 | * Free the previous transform and switch in the current one |
Jerry Yu | aef0015 | 2022-02-17 14:27:31 +0800 | [diff] [blame] | 7446 | */ |
| 7447 | if( ssl->transform ) |
| 7448 | { |
| 7449 | mbedtls_ssl_transform_free( ssl->transform ); |
| 7450 | mbedtls_free( ssl->transform ); |
| 7451 | } |
| 7452 | ssl->transform = ssl->transform_negotiate; |
| 7453 | ssl->transform_negotiate = NULL; |
| 7454 | |
| 7455 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) ); |
| 7456 | } |
| 7457 | |
Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 7458 | void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl ) |
| 7459 | { |
| 7460 | int resume = ssl->handshake->resume; |
| 7461 | |
| 7462 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) ); |
| 7463 | |
| 7464 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 7465 | if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
| 7466 | { |
| 7467 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE; |
| 7468 | ssl->renego_records_seen = 0; |
| 7469 | } |
| 7470 | #endif |
| 7471 | |
| 7472 | /* |
| 7473 | * Free the previous session and switch in the current one |
| 7474 | */ |
| 7475 | if( ssl->session ) |
| 7476 | { |
| 7477 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 7478 | /* RFC 7366 3.1: keep the EtM state */ |
| 7479 | ssl->session_negotiate->encrypt_then_mac = |
| 7480 | ssl->session->encrypt_then_mac; |
| 7481 | #endif |
| 7482 | |
| 7483 | mbedtls_ssl_session_free( ssl->session ); |
| 7484 | mbedtls_free( ssl->session ); |
| 7485 | } |
| 7486 | ssl->session = ssl->session_negotiate; |
| 7487 | ssl->session_negotiate = NULL; |
| 7488 | |
| 7489 | /* |
| 7490 | * Add cache entry |
| 7491 | */ |
| 7492 | if( ssl->conf->f_set_cache != NULL && |
| 7493 | ssl->session->id_len != 0 && |
| 7494 | resume == 0 ) |
| 7495 | { |
| 7496 | if( ssl->conf->f_set_cache( ssl->conf->p_cache, |
| 7497 | ssl->session->id, |
| 7498 | ssl->session->id_len, |
| 7499 | ssl->session ) != 0 ) |
| 7500 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) ); |
| 7501 | } |
| 7502 | |
| 7503 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 7504 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 7505 | ssl->handshake->flight != NULL ) |
| 7506 | { |
| 7507 | /* Cancel handshake timer */ |
| 7508 | mbedtls_ssl_set_timer( ssl, 0 ); |
| 7509 | |
| 7510 | /* Keep last flight around in case we need to resend it: |
| 7511 | * we need the handshake and transform structures for that */ |
| 7512 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) ); |
| 7513 | } |
| 7514 | else |
| 7515 | #endif |
| 7516 | mbedtls_ssl_handshake_wrapup_free_hs_transform( ssl ); |
| 7517 | |
| 7518 | ssl->state++; |
| 7519 | |
| 7520 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) ); |
| 7521 | } |
| 7522 | |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 7523 | int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl ) |
| 7524 | { |
| 7525 | int ret, hash_len; |
| 7526 | |
| 7527 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) ); |
| 7528 | |
| 7529 | mbedtls_ssl_update_out_pointers( ssl, ssl->transform_negotiate ); |
| 7530 | |
| 7531 | ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint ); |
| 7532 | |
| 7533 | /* |
| 7534 | * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites |
| 7535 | * may define some other value. Currently (early 2016), no defined |
| 7536 | * ciphersuite does this (and this is unlikely to change as activity has |
| 7537 | * moved to TLS 1.3 now) so we can keep the hardcoded 12 here. |
| 7538 | */ |
| 7539 | hash_len = 12; |
| 7540 | |
| 7541 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 7542 | ssl->verify_data_len = hash_len; |
| 7543 | memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len ); |
| 7544 | #endif |
| 7545 | |
| 7546 | ssl->out_msglen = 4 + hash_len; |
| 7547 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 7548 | ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED; |
| 7549 | |
| 7550 | /* |
| 7551 | * In case of session resuming, invert the client and server |
| 7552 | * ChangeCipherSpec messages order. |
| 7553 | */ |
| 7554 | if( ssl->handshake->resume != 0 ) |
| 7555 | { |
| 7556 | #if defined(MBEDTLS_SSL_CLI_C) |
| 7557 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 7558 | ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP; |
| 7559 | #endif |
| 7560 | #if defined(MBEDTLS_SSL_SRV_C) |
| 7561 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 7562 | ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC; |
| 7563 | #endif |
| 7564 | } |
| 7565 | else |
| 7566 | ssl->state++; |
| 7567 | |
| 7568 | /* |
| 7569 | * Switch to our negotiated transform and session parameters for outbound |
| 7570 | * data. |
| 7571 | */ |
| 7572 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) ); |
| 7573 | |
| 7574 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 7575 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 7576 | { |
| 7577 | unsigned char i; |
| 7578 | |
| 7579 | /* Remember current epoch settings for resending */ |
| 7580 | ssl->handshake->alt_transform_out = ssl->transform_out; |
| 7581 | memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, |
| 7582 | sizeof( ssl->handshake->alt_out_ctr ) ); |
| 7583 | |
| 7584 | /* Set sequence_number to zero */ |
| 7585 | memset( &ssl->cur_out_ctr[2], 0, sizeof( ssl->cur_out_ctr ) - 2 ); |
| 7586 | |
| 7587 | |
| 7588 | /* Increment epoch */ |
| 7589 | for( i = 2; i > 0; i-- ) |
| 7590 | if( ++ssl->cur_out_ctr[i - 1] != 0 ) |
| 7591 | break; |
| 7592 | |
| 7593 | /* The loop goes to its end iff the counter is wrapping */ |
| 7594 | if( i == 0 ) |
| 7595 | { |
| 7596 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) ); |
| 7597 | return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING ); |
| 7598 | } |
| 7599 | } |
| 7600 | else |
| 7601 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 7602 | memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) ); |
| 7603 | |
| 7604 | ssl->transform_out = ssl->transform_negotiate; |
| 7605 | ssl->session_out = ssl->session_negotiate; |
| 7606 | |
| 7607 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 7608 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 7609 | mbedtls_ssl_send_flight_completed( ssl ); |
| 7610 | #endif |
| 7611 | |
| 7612 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
| 7613 | { |
| 7614 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); |
| 7615 | return( ret ); |
| 7616 | } |
| 7617 | |
| 7618 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 7619 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 7620 | ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 ) |
| 7621 | { |
| 7622 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret ); |
| 7623 | return( ret ); |
| 7624 | } |
| 7625 | #endif |
| 7626 | |
| 7627 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) ); |
| 7628 | |
| 7629 | return( 0 ); |
| 7630 | } |
| 7631 | |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 7632 | #define SSL_MAX_HASH_LEN 12 |
| 7633 | |
| 7634 | int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl ) |
| 7635 | { |
| 7636 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 7637 | unsigned int hash_len = 12; |
| 7638 | unsigned char buf[SSL_MAX_HASH_LEN]; |
| 7639 | |
| 7640 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) ); |
| 7641 | |
| 7642 | ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 ); |
| 7643 | |
| 7644 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) |
| 7645 | { |
| 7646 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
| 7647 | goto exit; |
| 7648 | } |
| 7649 | |
| 7650 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) |
| 7651 | { |
| 7652 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) ); |
| 7653 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7654 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
| 7655 | ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; |
| 7656 | goto exit; |
| 7657 | } |
| 7658 | |
| 7659 | if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ) |
| 7660 | { |
| 7661 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7662 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
| 7663 | ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; |
| 7664 | goto exit; |
| 7665 | } |
| 7666 | |
| 7667 | if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len ) |
| 7668 | { |
| 7669 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) ); |
| 7670 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7671 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
| 7672 | ret = MBEDTLS_ERR_SSL_DECODE_ERROR; |
| 7673 | goto exit; |
| 7674 | } |
| 7675 | |
| 7676 | if( mbedtls_ct_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), |
| 7677 | buf, hash_len ) != 0 ) |
| 7678 | { |
| 7679 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) ); |
| 7680 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7681 | MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR ); |
| 7682 | ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE; |
| 7683 | goto exit; |
| 7684 | } |
| 7685 | |
| 7686 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 7687 | ssl->verify_data_len = hash_len; |
| 7688 | memcpy( ssl->peer_verify_data, buf, hash_len ); |
| 7689 | #endif |
| 7690 | |
| 7691 | if( ssl->handshake->resume != 0 ) |
| 7692 | { |
| 7693 | #if defined(MBEDTLS_SSL_CLI_C) |
| 7694 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 7695 | ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC; |
| 7696 | #endif |
| 7697 | #if defined(MBEDTLS_SSL_SRV_C) |
| 7698 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 7699 | ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP; |
| 7700 | #endif |
| 7701 | } |
| 7702 | else |
| 7703 | ssl->state++; |
| 7704 | |
| 7705 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 7706 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 7707 | mbedtls_ssl_recv_flight_completed( ssl ); |
| 7708 | #endif |
| 7709 | |
| 7710 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) ); |
| 7711 | |
| 7712 | exit: |
| 7713 | mbedtls_platform_zeroize( buf, hash_len ); |
| 7714 | return( ret ); |
| 7715 | } |
| 7716 | |
Jerry Yu | 392112c | 2022-02-17 14:34:10 +0800 | [diff] [blame] | 7717 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
| 7718 | /* |
| 7719 | * Helper to get TLS 1.2 PRF from ciphersuite |
| 7720 | * (Duplicates bits of logic from ssl_set_handshake_prfs().) |
| 7721 | */ |
| 7722 | static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id ) |
| 7723 | { |
Jerry Yu | 392112c | 2022-02-17 14:34:10 +0800 | [diff] [blame] | 7724 | const mbedtls_ssl_ciphersuite_t * const ciphersuite_info = |
Andrzej Kurek | 6832774 | 2022-10-03 06:18:18 -0400 | [diff] [blame] | 7725 | mbedtls_ssl_ciphersuite_from_id( ciphersuite_id ); |
| 7726 | #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] | 7727 | if( ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA384 ) |
Jerry Yu | 392112c | 2022-02-17 14:34:10 +0800 | [diff] [blame] | 7728 | return( tls_prf_sha384 ); |
Andrzej Kurek | 894edde | 2022-09-29 06:31:14 -0400 | [diff] [blame] | 7729 | else |
Jerry Yu | 392112c | 2022-02-17 14:34:10 +0800 | [diff] [blame] | 7730 | #endif |
Andrzej Kurek | 894edde | 2022-09-29 06:31:14 -0400 | [diff] [blame] | 7731 | #if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
| 7732 | { |
| 7733 | if( ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA256 ) |
| 7734 | return( tls_prf_sha256 ); |
| 7735 | } |
| 7736 | #endif |
| 7737 | #if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \ |
| 7738 | !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
| 7739 | (void) ciphersuite_info; |
| 7740 | #endif |
Andrzej Kurek | eabeb30 | 2022-10-17 07:52:51 -0400 | [diff] [blame] | 7741 | |
Andrzej Kurek | 894edde | 2022-09-29 06:31:14 -0400 | [diff] [blame] | 7742 | return( NULL ); |
Jerry Yu | 392112c | 2022-02-17 14:34:10 +0800 | [diff] [blame] | 7743 | } |
| 7744 | #endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */ |
Jerry Yu | e93ffcd | 2022-02-17 14:37:06 +0800 | [diff] [blame] | 7745 | |
| 7746 | static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf ) |
| 7747 | { |
| 7748 | ((void) tls_prf); |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 7749 | #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] | 7750 | if( tls_prf == tls_prf_sha384 ) |
| 7751 | { |
| 7752 | return( MBEDTLS_SSL_TLS_PRF_SHA384 ); |
| 7753 | } |
| 7754 | else |
| 7755 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 7756 | #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] | 7757 | if( tls_prf == tls_prf_sha256 ) |
| 7758 | { |
| 7759 | return( MBEDTLS_SSL_TLS_PRF_SHA256 ); |
| 7760 | } |
| 7761 | else |
| 7762 | #endif |
| 7763 | return( MBEDTLS_SSL_TLS_PRF_NONE ); |
| 7764 | } |
| 7765 | |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7766 | /* |
| 7767 | * Populate a transform structure with session keys and all the other |
| 7768 | * necessary information. |
| 7769 | * |
| 7770 | * Parameters: |
| 7771 | * - [in/out]: transform: structure to populate |
| 7772 | * [in] must be just initialised with mbedtls_ssl_transform_init() |
| 7773 | * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf() |
| 7774 | * - [in] ciphersuite |
| 7775 | * - [in] master |
| 7776 | * - [in] encrypt_then_mac |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7777 | * - [in] tls_prf: pointer to PRF to use for key derivation |
| 7778 | * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random |
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 7779 | * - [in] tls_version: TLS version |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7780 | * - [in] endpoint: client or server |
| 7781 | * - [in] ssl: used for: |
| 7782 | * - ssl->conf->{f,p}_export_keys |
| 7783 | * [in] optionally used for: |
| 7784 | * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg |
| 7785 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 7786 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7787 | static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform, |
| 7788 | int ciphersuite, |
| 7789 | const unsigned char master[48], |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 7790 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7791 | int encrypt_then_mac, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 7792 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7793 | ssl_tls_prf_t tls_prf, |
| 7794 | const unsigned char randbytes[64], |
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 7795 | mbedtls_ssl_protocol_version tls_version, |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7796 | unsigned endpoint, |
| 7797 | const mbedtls_ssl_context *ssl ) |
| 7798 | { |
| 7799 | int ret = 0; |
| 7800 | unsigned char keyblk[256]; |
| 7801 | unsigned char *key1; |
| 7802 | unsigned char *key2; |
| 7803 | unsigned char *mac_enc; |
| 7804 | unsigned char *mac_dec; |
| 7805 | size_t mac_key_len = 0; |
| 7806 | size_t iv_copy_len; |
| 7807 | size_t keylen; |
| 7808 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info; |
Neil Armstrong | 7fea33e | 2022-04-01 15:40:25 +0200 | [diff] [blame] | 7809 | mbedtls_ssl_mode_t ssl_mode; |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7810 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7811 | const mbedtls_cipher_info_t *cipher_info; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7812 | const mbedtls_md_info_t *md_info; |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7813 | #endif /* !MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7814 | |
| 7815 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7816 | psa_key_type_t key_type; |
| 7817 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7818 | psa_algorithm_t alg; |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7819 | psa_algorithm_t mac_alg = 0; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7820 | size_t key_bits; |
| 7821 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 7822 | #endif |
| 7823 | |
| 7824 | #if !defined(MBEDTLS_DEBUG_C) && \ |
| 7825 | !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 7826 | if( ssl->f_export_keys == NULL ) |
| 7827 | { |
| 7828 | ssl = NULL; /* make sure we don't use it except for these cases */ |
| 7829 | (void) ssl; |
| 7830 | } |
| 7831 | #endif |
| 7832 | |
| 7833 | /* |
| 7834 | * Some data just needs copying into the structure |
| 7835 | */ |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 7836 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7837 | transform->encrypt_then_mac = encrypt_then_mac; |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 7838 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 7839 | transform->tls_version = tls_version; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7840 | |
| 7841 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
| 7842 | memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) ); |
| 7843 | #endif |
| 7844 | |
| 7845 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 7846 | if( tls_version == MBEDTLS_SSL_VERSION_TLS1_3 ) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7847 | { |
| 7848 | /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform |
| 7849 | * generation separate. This should never happen. */ |
| 7850 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 7851 | } |
| 7852 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 7853 | |
| 7854 | /* |
| 7855 | * Get various info structures |
| 7856 | */ |
| 7857 | ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite ); |
| 7858 | if( ciphersuite_info == NULL ) |
| 7859 | { |
| 7860 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found", |
| 7861 | ciphersuite ) ); |
| 7862 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 7863 | } |
| 7864 | |
Neil Armstrong | ab555e0 | 2022-04-04 11:07:59 +0200 | [diff] [blame] | 7865 | ssl_mode = mbedtls_ssl_get_mode_from_ciphersuite( |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 7866 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Neil Armstrong | 7fea33e | 2022-04-01 15:40:25 +0200 | [diff] [blame] | 7867 | encrypt_then_mac, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 7868 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Neil Armstrong | 7fea33e | 2022-04-01 15:40:25 +0200 | [diff] [blame] | 7869 | ciphersuite_info ); |
| 7870 | |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7871 | if( ssl_mode == MBEDTLS_SSL_MODE_AEAD ) |
| 7872 | transform->taglen = |
| 7873 | ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16; |
| 7874 | |
| 7875 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7876 | if( ( status = mbedtls_ssl_cipher_to_psa( ciphersuite_info->cipher, |
| 7877 | transform->taglen, |
| 7878 | &alg, |
| 7879 | &key_type, |
| 7880 | &key_bits ) ) != PSA_SUCCESS ) |
| 7881 | { |
| 7882 | ret = psa_ssl_status_to_mbedtls( status ); |
| 7883 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_cipher_to_psa", ret ); |
| 7884 | goto end; |
| 7885 | } |
| 7886 | #else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7887 | cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher ); |
| 7888 | if( cipher_info == NULL ) |
| 7889 | { |
| 7890 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %u not found", |
| 7891 | ciphersuite_info->cipher ) ); |
| 7892 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 7893 | } |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7894 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7895 | |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7896 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Manuel Pégourié-Gonnard | abac037 | 2022-07-18 13:41:11 +0200 | [diff] [blame] | 7897 | mac_alg = mbedtls_hash_info_psa_from_md( ciphersuite_info->mac ); |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7898 | if( mac_alg == 0 ) |
| 7899 | { |
Manuel Pégourié-Gonnard | abac037 | 2022-07-18 13:41:11 +0200 | [diff] [blame] | 7900 | 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] | 7901 | (unsigned) ciphersuite_info->mac ) ); |
| 7902 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 7903 | } |
| 7904 | #else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7905 | md_info = mbedtls_md_info_from_type( ciphersuite_info->mac ); |
| 7906 | if( md_info == NULL ) |
| 7907 | { |
| 7908 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %u not found", |
| 7909 | (unsigned) ciphersuite_info->mac ) ); |
| 7910 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 7911 | } |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7912 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7913 | |
| 7914 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 7915 | /* Copy own and peer's CID if the use of the CID |
| 7916 | * extension has been negotiated. */ |
| 7917 | if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED ) |
| 7918 | { |
| 7919 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) ); |
| 7920 | |
| 7921 | transform->in_cid_len = ssl->own_cid_len; |
| 7922 | memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len ); |
| 7923 | MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid, |
| 7924 | transform->in_cid_len ); |
| 7925 | |
| 7926 | transform->out_cid_len = ssl->handshake->peer_cid_len; |
| 7927 | memcpy( transform->out_cid, ssl->handshake->peer_cid, |
| 7928 | ssl->handshake->peer_cid_len ); |
| 7929 | MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid, |
| 7930 | transform->out_cid_len ); |
| 7931 | } |
| 7932 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 7933 | |
| 7934 | /* |
| 7935 | * Compute key block using the PRF |
| 7936 | */ |
| 7937 | ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 ); |
| 7938 | if( ret != 0 ) |
| 7939 | { |
| 7940 | MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret ); |
| 7941 | return( ret ); |
| 7942 | } |
| 7943 | |
| 7944 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s", |
| 7945 | mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) ); |
| 7946 | MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 ); |
| 7947 | MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 ); |
| 7948 | MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 ); |
| 7949 | |
| 7950 | /* |
| 7951 | * Determine the appropriate key, IV and MAC length. |
| 7952 | */ |
| 7953 | |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7954 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7955 | keylen = PSA_BITS_TO_BYTES(key_bits); |
| 7956 | #else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7957 | keylen = mbedtls_cipher_info_get_key_bitlen( cipher_info ) / 8; |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7958 | #endif |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7959 | |
| 7960 | #if defined(MBEDTLS_GCM_C) || \ |
| 7961 | defined(MBEDTLS_CCM_C) || \ |
| 7962 | defined(MBEDTLS_CHACHAPOLY_C) |
Neil Armstrong | 7fea33e | 2022-04-01 15:40:25 +0200 | [diff] [blame] | 7963 | if( ssl_mode == MBEDTLS_SSL_MODE_AEAD ) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7964 | { |
| 7965 | size_t explicit_ivlen; |
| 7966 | |
| 7967 | transform->maclen = 0; |
| 7968 | mac_key_len = 0; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7969 | |
| 7970 | /* All modes haves 96-bit IVs, but the length of the static parts vary |
| 7971 | * with mode and version: |
| 7972 | * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes |
| 7973 | * (to be concatenated with a dynamically chosen IV of 8 Bytes) |
| 7974 | * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's |
| 7975 | * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record |
| 7976 | * sequence number). |
| 7977 | */ |
| 7978 | transform->ivlen = 12; |
David Horstmann | 3b2276a | 2022-10-06 14:49:08 +0100 | [diff] [blame] | 7979 | |
| 7980 | int is_chachapoly = 0; |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7981 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
David Horstmann | 3b2276a | 2022-10-06 14:49:08 +0100 | [diff] [blame] | 7982 | is_chachapoly = ( key_type == PSA_KEY_TYPE_CHACHA20 ); |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7983 | #else |
David Horstmann | 3b2276a | 2022-10-06 14:49:08 +0100 | [diff] [blame] | 7984 | is_chachapoly = ( mbedtls_cipher_info_get_mode( cipher_info ) |
| 7985 | == MBEDTLS_MODE_CHACHAPOLY ); |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7986 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
David Horstmann | 3b2276a | 2022-10-06 14:49:08 +0100 | [diff] [blame] | 7987 | |
| 7988 | if( is_chachapoly ) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7989 | transform->fixed_ivlen = 12; |
| 7990 | else |
| 7991 | transform->fixed_ivlen = 4; |
| 7992 | |
| 7993 | /* Minimum length of encrypted record */ |
| 7994 | explicit_ivlen = transform->ivlen - transform->fixed_ivlen; |
| 7995 | transform->minlen = explicit_ivlen + transform->taglen; |
| 7996 | } |
| 7997 | else |
| 7998 | #endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */ |
| 7999 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
Neil Armstrong | 7fea33e | 2022-04-01 15:40:25 +0200 | [diff] [blame] | 8000 | if( ssl_mode == MBEDTLS_SSL_MODE_STREAM || |
| 8001 | ssl_mode == MBEDTLS_SSL_MODE_CBC || |
| 8002 | ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM ) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8003 | { |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 8004 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Neil Armstrong | d1be767 | 2022-04-04 11:21:41 +0200 | [diff] [blame] | 8005 | size_t block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ); |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8006 | #else |
| 8007 | size_t block_size = cipher_info->block_size; |
| 8008 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 8009 | |
| 8010 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 8011 | /* Get MAC length */ |
| 8012 | mac_key_len = PSA_HASH_LENGTH(mac_alg); |
| 8013 | #else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8014 | /* Initialize HMAC contexts */ |
| 8015 | if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 || |
| 8016 | ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 ) |
| 8017 | { |
| 8018 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret ); |
| 8019 | goto end; |
| 8020 | } |
| 8021 | |
| 8022 | /* Get MAC length */ |
| 8023 | mac_key_len = mbedtls_md_get_size( md_info ); |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 8024 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8025 | transform->maclen = mac_key_len; |
| 8026 | |
| 8027 | /* IV length */ |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8028 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Neil Armstrong | 2230e6c | 2022-04-27 10:36:14 +0200 | [diff] [blame] | 8029 | transform->ivlen = PSA_CIPHER_IV_LENGTH( key_type, alg ); |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8030 | #else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8031 | transform->ivlen = cipher_info->iv_size; |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8032 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8033 | |
| 8034 | /* Minimum length */ |
Neil Armstrong | 7fea33e | 2022-04-01 15:40:25 +0200 | [diff] [blame] | 8035 | if( ssl_mode == MBEDTLS_SSL_MODE_STREAM ) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8036 | transform->minlen = transform->maclen; |
| 8037 | else |
| 8038 | { |
| 8039 | /* |
| 8040 | * GenericBlockCipher: |
| 8041 | * 1. if EtM is in use: one block plus MAC |
| 8042 | * otherwise: * first multiple of blocklen greater than maclen |
| 8043 | * 2. IV |
| 8044 | */ |
| 8045 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Neil Armstrong | 7fea33e | 2022-04-01 15:40:25 +0200 | [diff] [blame] | 8046 | if( ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM ) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8047 | { |
| 8048 | transform->minlen = transform->maclen |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8049 | + block_size; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8050 | } |
| 8051 | else |
| 8052 | #endif |
| 8053 | { |
| 8054 | transform->minlen = transform->maclen |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8055 | + block_size |
| 8056 | - transform->maclen % block_size; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8057 | } |
| 8058 | |
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 8059 | if( tls_version == MBEDTLS_SSL_VERSION_TLS1_2 ) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8060 | { |
| 8061 | transform->minlen += transform->ivlen; |
| 8062 | } |
| 8063 | else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8064 | { |
| 8065 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 8066 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 8067 | goto end; |
| 8068 | } |
| 8069 | } |
| 8070 | } |
| 8071 | else |
| 8072 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
| 8073 | { |
| 8074 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 8075 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 8076 | } |
| 8077 | |
| 8078 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u", |
| 8079 | (unsigned) keylen, |
| 8080 | (unsigned) transform->minlen, |
| 8081 | (unsigned) transform->ivlen, |
| 8082 | (unsigned) transform->maclen ) ); |
| 8083 | |
| 8084 | /* |
| 8085 | * Finally setup the cipher contexts, IVs and MAC secrets. |
| 8086 | */ |
| 8087 | #if defined(MBEDTLS_SSL_CLI_C) |
| 8088 | if( endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 8089 | { |
| 8090 | key1 = keyblk + mac_key_len * 2; |
| 8091 | key2 = keyblk + mac_key_len * 2 + keylen; |
| 8092 | |
| 8093 | mac_enc = keyblk; |
| 8094 | mac_dec = keyblk + mac_key_len; |
| 8095 | |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8096 | iv_copy_len = ( transform->fixed_ivlen ) ? |
| 8097 | transform->fixed_ivlen : transform->ivlen; |
| 8098 | memcpy( transform->iv_enc, key2 + keylen, iv_copy_len ); |
| 8099 | memcpy( transform->iv_dec, key2 + keylen + iv_copy_len, |
| 8100 | iv_copy_len ); |
| 8101 | } |
| 8102 | else |
| 8103 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 8104 | #if defined(MBEDTLS_SSL_SRV_C) |
| 8105 | if( endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 8106 | { |
| 8107 | key1 = keyblk + mac_key_len * 2 + keylen; |
| 8108 | key2 = keyblk + mac_key_len * 2; |
| 8109 | |
| 8110 | mac_enc = keyblk + mac_key_len; |
| 8111 | mac_dec = keyblk; |
| 8112 | |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8113 | iv_copy_len = ( transform->fixed_ivlen ) ? |
| 8114 | transform->fixed_ivlen : transform->ivlen; |
| 8115 | memcpy( transform->iv_dec, key1 + keylen, iv_copy_len ); |
| 8116 | memcpy( transform->iv_enc, key1 + keylen + iv_copy_len, |
| 8117 | iv_copy_len ); |
| 8118 | } |
| 8119 | else |
| 8120 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 8121 | { |
| 8122 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 8123 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 8124 | goto end; |
| 8125 | } |
| 8126 | |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8127 | if( ssl != NULL && ssl->f_export_keys != NULL ) |
| 8128 | { |
| 8129 | ssl->f_export_keys( ssl->p_export_keys, |
| 8130 | MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET, |
| 8131 | master, 48, |
| 8132 | randbytes + 32, |
| 8133 | randbytes, |
| 8134 | tls_prf_get_type( tls_prf ) ); |
| 8135 | } |
| 8136 | |
| 8137 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8138 | transform->psa_alg = alg; |
| 8139 | |
| 8140 | if ( alg != MBEDTLS_SSL_NULL_CIPHER ) |
| 8141 | { |
| 8142 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 8143 | psa_set_key_algorithm( &attributes, alg ); |
| 8144 | psa_set_key_type( &attributes, key_type ); |
| 8145 | |
| 8146 | if( ( status = psa_import_key( &attributes, |
| 8147 | key1, |
| 8148 | PSA_BITS_TO_BYTES( key_bits ), |
| 8149 | &transform->psa_key_enc ) ) != PSA_SUCCESS ) |
| 8150 | { |
| 8151 | MBEDTLS_SSL_DEBUG_RET( 3, "psa_import_key", (int)status ); |
| 8152 | ret = psa_ssl_status_to_mbedtls( status ); |
| 8153 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret ); |
| 8154 | goto end; |
| 8155 | } |
| 8156 | |
| 8157 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 8158 | |
| 8159 | if( ( status = psa_import_key( &attributes, |
| 8160 | key2, |
| 8161 | PSA_BITS_TO_BYTES( key_bits ), |
| 8162 | &transform->psa_key_dec ) ) != PSA_SUCCESS ) |
| 8163 | { |
| 8164 | ret = psa_ssl_status_to_mbedtls( status ); |
| 8165 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret ); |
| 8166 | goto end; |
| 8167 | } |
| 8168 | } |
| 8169 | #else |
| 8170 | if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc, |
| 8171 | cipher_info ) ) != 0 ) |
| 8172 | { |
| 8173 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret ); |
| 8174 | goto end; |
| 8175 | } |
| 8176 | |
| 8177 | if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec, |
| 8178 | cipher_info ) ) != 0 ) |
| 8179 | { |
| 8180 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret ); |
| 8181 | goto end; |
| 8182 | } |
| 8183 | |
| 8184 | if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1, |
| 8185 | (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ), |
| 8186 | MBEDTLS_ENCRYPT ) ) != 0 ) |
| 8187 | { |
| 8188 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret ); |
| 8189 | goto end; |
| 8190 | } |
| 8191 | |
| 8192 | if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2, |
| 8193 | (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ), |
| 8194 | MBEDTLS_DECRYPT ) ) != 0 ) |
| 8195 | { |
| 8196 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret ); |
| 8197 | goto end; |
| 8198 | } |
| 8199 | |
| 8200 | #if defined(MBEDTLS_CIPHER_MODE_CBC) |
| 8201 | if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CBC ) |
| 8202 | { |
| 8203 | if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc, |
| 8204 | MBEDTLS_PADDING_NONE ) ) != 0 ) |
| 8205 | { |
| 8206 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret ); |
| 8207 | goto end; |
| 8208 | } |
| 8209 | |
| 8210 | if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec, |
| 8211 | MBEDTLS_PADDING_NONE ) ) != 0 ) |
| 8212 | { |
| 8213 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret ); |
| 8214 | goto end; |
| 8215 | } |
| 8216 | } |
| 8217 | #endif /* MBEDTLS_CIPHER_MODE_CBC */ |
| 8218 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 8219 | |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 8220 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
| 8221 | /* For HMAC-based ciphersuites, initialize the HMAC transforms. |
| 8222 | For AEAD-based ciphersuites, there is nothing to do here. */ |
| 8223 | if( mac_key_len != 0 ) |
| 8224 | { |
| 8225 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 8226 | transform->psa_mac_alg = PSA_ALG_HMAC( mac_alg ); |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 8227 | |
| 8228 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE ); |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 8229 | psa_set_key_algorithm( &attributes, PSA_ALG_HMAC( mac_alg ) ); |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 8230 | psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC ); |
| 8231 | |
| 8232 | if( ( status = psa_import_key( &attributes, |
| 8233 | mac_enc, mac_key_len, |
| 8234 | &transform->psa_mac_enc ) ) != PSA_SUCCESS ) |
| 8235 | { |
| 8236 | ret = psa_ssl_status_to_mbedtls( status ); |
| 8237 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret ); |
| 8238 | goto end; |
| 8239 | } |
| 8240 | |
Ronald Cron | fb39f15 | 2022-03-25 14:36:28 +0100 | [diff] [blame] | 8241 | if( ( transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER ) || |
Andrzej Kurek | 8c95ac4 | 2022-08-17 16:17:00 -0400 | [diff] [blame] | 8242 | ( ( transform->psa_alg == PSA_ALG_CBC_NO_PADDING ) |
| 8243 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
| 8244 | && ( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED ) |
| 8245 | #endif |
| 8246 | ) ) |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 8247 | /* mbedtls_ct_hmac() requires the key to be exportable */ |
| 8248 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT | |
| 8249 | PSA_KEY_USAGE_VERIFY_HASH ); |
| 8250 | else |
| 8251 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH ); |
| 8252 | |
| 8253 | if( ( status = psa_import_key( &attributes, |
| 8254 | mac_dec, mac_key_len, |
| 8255 | &transform->psa_mac_dec ) ) != PSA_SUCCESS ) |
| 8256 | { |
| 8257 | ret = psa_ssl_status_to_mbedtls( status ); |
| 8258 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret ); |
| 8259 | goto end; |
| 8260 | } |
| 8261 | #else |
| 8262 | ret = mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len ); |
| 8263 | if( ret != 0 ) |
| 8264 | goto end; |
| 8265 | ret = mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len ); |
| 8266 | if( ret != 0 ) |
| 8267 | goto end; |
| 8268 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 8269 | } |
| 8270 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
| 8271 | |
| 8272 | ((void) mac_dec); |
| 8273 | ((void) mac_enc); |
| 8274 | |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8275 | end: |
| 8276 | mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) ); |
| 8277 | return( ret ); |
| 8278 | } |
| 8279 | |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 8280 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 8281 | int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl, |
| 8282 | unsigned char *hash, size_t *hashlen, |
| 8283 | unsigned char *data, size_t data_len, |
| 8284 | mbedtls_md_type_t md_alg ) |
| 8285 | { |
| 8286 | psa_status_t status; |
| 8287 | psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT; |
Manuel Pégourié-Gonnard | abac037 | 2022-07-18 13:41:11 +0200 | [diff] [blame] | 8288 | 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] | 8289 | |
| 8290 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) ); |
| 8291 | |
| 8292 | if( ( status = psa_hash_setup( &hash_operation, |
| 8293 | hash_alg ) ) != PSA_SUCCESS ) |
| 8294 | { |
| 8295 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status ); |
| 8296 | goto exit; |
| 8297 | } |
| 8298 | |
| 8299 | if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes, |
| 8300 | 64 ) ) != PSA_SUCCESS ) |
| 8301 | { |
| 8302 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status ); |
| 8303 | goto exit; |
| 8304 | } |
| 8305 | |
| 8306 | if( ( status = psa_hash_update( &hash_operation, |
| 8307 | data, data_len ) ) != PSA_SUCCESS ) |
| 8308 | { |
| 8309 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status ); |
| 8310 | goto exit; |
| 8311 | } |
| 8312 | |
| 8313 | if( ( status = psa_hash_finish( &hash_operation, hash, PSA_HASH_MAX_SIZE, |
| 8314 | hashlen ) ) != PSA_SUCCESS ) |
| 8315 | { |
| 8316 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status ); |
| 8317 | goto exit; |
| 8318 | } |
| 8319 | |
| 8320 | exit: |
| 8321 | if( status != PSA_SUCCESS ) |
| 8322 | { |
| 8323 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 8324 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
| 8325 | switch( status ) |
| 8326 | { |
| 8327 | case PSA_ERROR_NOT_SUPPORTED: |
| 8328 | return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE ); |
| 8329 | case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */ |
| 8330 | case PSA_ERROR_BUFFER_TOO_SMALL: |
| 8331 | return( MBEDTLS_ERR_MD_BAD_INPUT_DATA ); |
| 8332 | case PSA_ERROR_INSUFFICIENT_MEMORY: |
| 8333 | return( MBEDTLS_ERR_MD_ALLOC_FAILED ); |
| 8334 | default: |
| 8335 | return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED ); |
| 8336 | } |
| 8337 | } |
| 8338 | return( 0 ); |
| 8339 | } |
| 8340 | |
| 8341 | #else |
| 8342 | |
| 8343 | int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl, |
| 8344 | unsigned char *hash, size_t *hashlen, |
| 8345 | unsigned char *data, size_t data_len, |
| 8346 | mbedtls_md_type_t md_alg ) |
| 8347 | { |
| 8348 | int ret = 0; |
| 8349 | mbedtls_md_context_t ctx; |
| 8350 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg ); |
| 8351 | *hashlen = mbedtls_md_get_size( md_info ); |
| 8352 | |
| 8353 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) ); |
| 8354 | |
| 8355 | mbedtls_md_init( &ctx ); |
| 8356 | |
| 8357 | /* |
| 8358 | * digitally-signed struct { |
| 8359 | * opaque client_random[32]; |
| 8360 | * opaque server_random[32]; |
| 8361 | * ServerDHParams params; |
| 8362 | * }; |
| 8363 | */ |
| 8364 | if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 ) |
| 8365 | { |
| 8366 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret ); |
| 8367 | goto exit; |
| 8368 | } |
| 8369 | if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 ) |
| 8370 | { |
| 8371 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret ); |
| 8372 | goto exit; |
| 8373 | } |
| 8374 | if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 ) |
| 8375 | { |
| 8376 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret ); |
| 8377 | goto exit; |
| 8378 | } |
| 8379 | if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 ) |
| 8380 | { |
| 8381 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret ); |
| 8382 | goto exit; |
| 8383 | } |
| 8384 | if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 ) |
| 8385 | { |
| 8386 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret ); |
| 8387 | goto exit; |
| 8388 | } |
| 8389 | |
| 8390 | exit: |
| 8391 | mbedtls_md_free( &ctx ); |
| 8392 | |
| 8393 | if( ret != 0 ) |
| 8394 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 8395 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
| 8396 | |
| 8397 | return( ret ); |
| 8398 | } |
| 8399 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 8400 | |
Jerry Yu | d9d91da | 2022-02-17 14:57:06 +0800 | [diff] [blame] | 8401 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
| 8402 | |
Gabor Mezei | a3d016c | 2022-05-10 12:44:09 +0200 | [diff] [blame] | 8403 | /* Find the preferred hash for a given signature algorithm. */ |
| 8404 | unsigned int mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg( |
| 8405 | mbedtls_ssl_context *ssl, |
| 8406 | unsigned int sig_alg ) |
Jerry Yu | d9d91da | 2022-02-17 14:57:06 +0800 | [diff] [blame] | 8407 | { |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 8408 | unsigned int i; |
Gabor Mezei | a3d016c | 2022-05-10 12:44:09 +0200 | [diff] [blame] | 8409 | uint16_t *received_sig_algs = ssl->handshake->received_sig_algs; |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 8410 | |
| 8411 | if( sig_alg == MBEDTLS_SSL_SIG_ANON ) |
Gabor Mezei | a3d016c | 2022-05-10 12:44:09 +0200 | [diff] [blame] | 8412 | return( MBEDTLS_SSL_HASH_NONE ); |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 8413 | |
Gabor Mezei | a3d016c | 2022-05-10 12:44:09 +0200 | [diff] [blame] | 8414 | for( i = 0; received_sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ ) |
Jerry Yu | d9d91da | 2022-02-17 14:57:06 +0800 | [diff] [blame] | 8415 | { |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 8416 | unsigned int hash_alg_received = |
| 8417 | MBEDTLS_SSL_TLS12_HASH_ALG_FROM_SIG_AND_HASH_ALG( |
| 8418 | received_sig_algs[i] ); |
| 8419 | unsigned int sig_alg_received = |
| 8420 | MBEDTLS_SSL_TLS12_SIG_ALG_FROM_SIG_AND_HASH_ALG( |
| 8421 | received_sig_algs[i] ); |
| 8422 | |
| 8423 | if( sig_alg == sig_alg_received ) |
| 8424 | { |
| 8425 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 8426 | if( ssl->handshake->key_cert && ssl->handshake->key_cert->key ) |
| 8427 | { |
Neil Armstrong | 96eceb8 | 2022-06-30 18:05:05 +0200 | [diff] [blame] | 8428 | psa_algorithm_t psa_hash_alg = |
Manuel Pégourié-Gonnard | abac037 | 2022-07-18 13:41:11 +0200 | [diff] [blame] | 8429 | mbedtls_hash_info_psa_from_md( hash_alg_received ); |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 8430 | |
Neil Armstrong | 96eceb8 | 2022-06-30 18:05:05 +0200 | [diff] [blame] | 8431 | if( sig_alg_received == MBEDTLS_SSL_SIG_ECDSA && |
| 8432 | ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key, |
| 8433 | PSA_ALG_ECDSA( psa_hash_alg ), |
| 8434 | PSA_KEY_USAGE_SIGN_HASH ) ) |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 8435 | continue; |
| 8436 | |
Neil Armstrong | 96eceb8 | 2022-06-30 18:05:05 +0200 | [diff] [blame] | 8437 | if( sig_alg_received == MBEDTLS_SSL_SIG_RSA && |
Neil Armstrong | 971f30d | 2022-07-01 16:23:50 +0200 | [diff] [blame] | 8438 | ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key, |
| 8439 | PSA_ALG_RSA_PKCS1V15_SIGN( |
| 8440 | psa_hash_alg ), |
| 8441 | PSA_KEY_USAGE_SIGN_HASH ) ) |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 8442 | continue; |
| 8443 | } |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 8444 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Neil Armstrong | 96eceb8 | 2022-06-30 18:05:05 +0200 | [diff] [blame] | 8445 | |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 8446 | return( hash_alg_received ); |
| 8447 | } |
Jerry Yu | d9d91da | 2022-02-17 14:57:06 +0800 | [diff] [blame] | 8448 | } |
Jerry Yu | d9d91da | 2022-02-17 14:57:06 +0800 | [diff] [blame] | 8449 | |
Gabor Mezei | a3d016c | 2022-05-10 12:44:09 +0200 | [diff] [blame] | 8450 | return( MBEDTLS_SSL_HASH_NONE ); |
Jerry Yu | d9d91da | 2022-02-17 14:57:06 +0800 | [diff] [blame] | 8451 | } |
| 8452 | |
| 8453 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 8454 | |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8455 | /* Serialization of TLS 1.2 sessions: |
| 8456 | * |
| 8457 | * struct { |
| 8458 | * uint64 start_time; |
| 8459 | * uint8 ciphersuite[2]; // defined by the standard |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8460 | * uint8 session_id_len; // at most 32 |
| 8461 | * opaque session_id[32]; |
| 8462 | * opaque master[48]; // fixed length in the standard |
| 8463 | * uint32 verify_result; |
| 8464 | * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert |
| 8465 | * opaque ticket<0..2^24-1>; // length 0 means no ticket |
| 8466 | * uint32 ticket_lifetime; |
| 8467 | * uint8 mfl_code; // up to 255 according to standard |
| 8468 | * uint8 encrypt_then_mac; // 0 or 1 |
| 8469 | * } serialized_session_tls12; |
| 8470 | * |
| 8471 | */ |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 8472 | static size_t ssl_tls12_session_save( const mbedtls_ssl_session *session, |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8473 | unsigned char *buf, |
| 8474 | size_t buf_len ) |
| 8475 | { |
| 8476 | unsigned char *p = buf; |
| 8477 | size_t used = 0; |
| 8478 | |
| 8479 | #if defined(MBEDTLS_HAVE_TIME) |
| 8480 | uint64_t start; |
| 8481 | #endif |
| 8482 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 8483 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 8484 | size_t cert_len; |
| 8485 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 8486 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 8487 | |
| 8488 | /* |
| 8489 | * Time |
| 8490 | */ |
| 8491 | #if defined(MBEDTLS_HAVE_TIME) |
| 8492 | used += 8; |
| 8493 | |
| 8494 | if( used <= buf_len ) |
| 8495 | { |
| 8496 | start = (uint64_t) session->start; |
| 8497 | |
| 8498 | MBEDTLS_PUT_UINT64_BE( start, p, 0 ); |
| 8499 | p += 8; |
| 8500 | } |
| 8501 | #endif /* MBEDTLS_HAVE_TIME */ |
| 8502 | |
| 8503 | /* |
| 8504 | * Basic mandatory fields |
| 8505 | */ |
| 8506 | used += 2 /* ciphersuite */ |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8507 | + 1 /* id_len */ |
| 8508 | + sizeof( session->id ) |
| 8509 | + sizeof( session->master ) |
| 8510 | + 4; /* verify_result */ |
| 8511 | |
| 8512 | if( used <= buf_len ) |
| 8513 | { |
| 8514 | MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 0 ); |
| 8515 | p += 2; |
| 8516 | |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8517 | *p++ = MBEDTLS_BYTE_0( session->id_len ); |
| 8518 | memcpy( p, session->id, 32 ); |
| 8519 | p += 32; |
| 8520 | |
| 8521 | memcpy( p, session->master, 48 ); |
| 8522 | p += 48; |
| 8523 | |
| 8524 | MBEDTLS_PUT_UINT32_BE( session->verify_result, p, 0 ); |
| 8525 | p += 4; |
| 8526 | } |
| 8527 | |
| 8528 | /* |
| 8529 | * Peer's end-entity certificate |
| 8530 | */ |
| 8531 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 8532 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 8533 | if( session->peer_cert == NULL ) |
| 8534 | cert_len = 0; |
| 8535 | else |
| 8536 | cert_len = session->peer_cert->raw.len; |
| 8537 | |
| 8538 | used += 3 + cert_len; |
| 8539 | |
| 8540 | if( used <= buf_len ) |
| 8541 | { |
| 8542 | *p++ = MBEDTLS_BYTE_2( cert_len ); |
| 8543 | *p++ = MBEDTLS_BYTE_1( cert_len ); |
| 8544 | *p++ = MBEDTLS_BYTE_0( cert_len ); |
| 8545 | |
| 8546 | if( session->peer_cert != NULL ) |
| 8547 | { |
| 8548 | memcpy( p, session->peer_cert->raw.p, cert_len ); |
| 8549 | p += cert_len; |
| 8550 | } |
| 8551 | } |
| 8552 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 8553 | if( session->peer_cert_digest != NULL ) |
| 8554 | { |
| 8555 | used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len; |
| 8556 | if( used <= buf_len ) |
| 8557 | { |
| 8558 | *p++ = (unsigned char) session->peer_cert_digest_type; |
| 8559 | *p++ = (unsigned char) session->peer_cert_digest_len; |
| 8560 | memcpy( p, session->peer_cert_digest, |
| 8561 | session->peer_cert_digest_len ); |
| 8562 | p += session->peer_cert_digest_len; |
| 8563 | } |
| 8564 | } |
| 8565 | else |
| 8566 | { |
| 8567 | used += 2; |
| 8568 | if( used <= buf_len ) |
| 8569 | { |
| 8570 | *p++ = (unsigned char) MBEDTLS_MD_NONE; |
| 8571 | *p++ = 0; |
| 8572 | } |
| 8573 | } |
| 8574 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 8575 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 8576 | |
| 8577 | /* |
| 8578 | * Session ticket if any, plus associated data |
| 8579 | */ |
| 8580 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
| 8581 | used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */ |
| 8582 | |
| 8583 | if( used <= buf_len ) |
| 8584 | { |
| 8585 | *p++ = MBEDTLS_BYTE_2( session->ticket_len ); |
| 8586 | *p++ = MBEDTLS_BYTE_1( session->ticket_len ); |
| 8587 | *p++ = MBEDTLS_BYTE_0( session->ticket_len ); |
| 8588 | |
| 8589 | if( session->ticket != NULL ) |
| 8590 | { |
| 8591 | memcpy( p, session->ticket, session->ticket_len ); |
| 8592 | p += session->ticket_len; |
| 8593 | } |
| 8594 | |
| 8595 | MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 ); |
| 8596 | p += 4; |
| 8597 | } |
| 8598 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
| 8599 | |
| 8600 | /* |
| 8601 | * Misc extension-related info |
| 8602 | */ |
| 8603 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 8604 | used += 1; |
| 8605 | |
| 8606 | if( used <= buf_len ) |
| 8607 | *p++ = session->mfl_code; |
| 8608 | #endif |
| 8609 | |
| 8610 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 8611 | used += 1; |
| 8612 | |
| 8613 | if( used <= buf_len ) |
| 8614 | *p++ = MBEDTLS_BYTE_0( session->encrypt_then_mac ); |
| 8615 | #endif |
| 8616 | |
| 8617 | return( used ); |
| 8618 | } |
| 8619 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 8620 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 8621 | static int ssl_tls12_session_load( mbedtls_ssl_session *session, |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8622 | const unsigned char *buf, |
| 8623 | size_t len ) |
| 8624 | { |
| 8625 | #if defined(MBEDTLS_HAVE_TIME) |
| 8626 | uint64_t start; |
| 8627 | #endif |
| 8628 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 8629 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 8630 | size_t cert_len; |
| 8631 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 8632 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 8633 | |
| 8634 | const unsigned char *p = buf; |
| 8635 | const unsigned char * const end = buf + len; |
| 8636 | |
| 8637 | /* |
| 8638 | * Time |
| 8639 | */ |
| 8640 | #if defined(MBEDTLS_HAVE_TIME) |
| 8641 | if( 8 > (size_t)( end - p ) ) |
| 8642 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8643 | |
| 8644 | start = ( (uint64_t) p[0] << 56 ) | |
| 8645 | ( (uint64_t) p[1] << 48 ) | |
| 8646 | ( (uint64_t) p[2] << 40 ) | |
| 8647 | ( (uint64_t) p[3] << 32 ) | |
| 8648 | ( (uint64_t) p[4] << 24 ) | |
| 8649 | ( (uint64_t) p[5] << 16 ) | |
| 8650 | ( (uint64_t) p[6] << 8 ) | |
| 8651 | ( (uint64_t) p[7] ); |
| 8652 | p += 8; |
| 8653 | |
| 8654 | session->start = (time_t) start; |
| 8655 | #endif /* MBEDTLS_HAVE_TIME */ |
| 8656 | |
| 8657 | /* |
| 8658 | * Basic mandatory fields |
| 8659 | */ |
Thomas Daubney | 20f89a9 | 2022-06-20 15:12:19 +0100 | [diff] [blame] | 8660 | if( 2 + 1 + 32 + 48 + 4 > (size_t)( end - p ) ) |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8661 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8662 | |
| 8663 | session->ciphersuite = ( p[0] << 8 ) | p[1]; |
| 8664 | p += 2; |
| 8665 | |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8666 | session->id_len = *p++; |
| 8667 | memcpy( session->id, p, 32 ); |
| 8668 | p += 32; |
| 8669 | |
| 8670 | memcpy( session->master, p, 48 ); |
| 8671 | p += 48; |
| 8672 | |
| 8673 | session->verify_result = ( (uint32_t) p[0] << 24 ) | |
| 8674 | ( (uint32_t) p[1] << 16 ) | |
| 8675 | ( (uint32_t) p[2] << 8 ) | |
| 8676 | ( (uint32_t) p[3] ); |
| 8677 | p += 4; |
| 8678 | |
| 8679 | /* Immediately clear invalid pointer values that have been read, in case |
| 8680 | * we exit early before we replaced them with valid ones. */ |
| 8681 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 8682 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 8683 | session->peer_cert = NULL; |
| 8684 | #else |
| 8685 | session->peer_cert_digest = NULL; |
| 8686 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 8687 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 8688 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
| 8689 | session->ticket = NULL; |
| 8690 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
| 8691 | |
| 8692 | /* |
| 8693 | * Peer certificate |
| 8694 | */ |
| 8695 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 8696 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 8697 | /* Deserialize CRT from the end of the ticket. */ |
| 8698 | if( 3 > (size_t)( end - p ) ) |
| 8699 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8700 | |
| 8701 | cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2]; |
| 8702 | p += 3; |
| 8703 | |
| 8704 | if( cert_len != 0 ) |
| 8705 | { |
| 8706 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 8707 | |
| 8708 | if( cert_len > (size_t)( end - p ) ) |
| 8709 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8710 | |
| 8711 | session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ); |
| 8712 | |
| 8713 | if( session->peer_cert == NULL ) |
| 8714 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 8715 | |
| 8716 | mbedtls_x509_crt_init( session->peer_cert ); |
| 8717 | |
| 8718 | if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert, |
| 8719 | p, cert_len ) ) != 0 ) |
| 8720 | { |
| 8721 | mbedtls_x509_crt_free( session->peer_cert ); |
| 8722 | mbedtls_free( session->peer_cert ); |
| 8723 | session->peer_cert = NULL; |
| 8724 | return( ret ); |
| 8725 | } |
| 8726 | |
| 8727 | p += cert_len; |
| 8728 | } |
| 8729 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 8730 | /* Deserialize CRT digest from the end of the ticket. */ |
| 8731 | if( 2 > (size_t)( end - p ) ) |
| 8732 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8733 | |
| 8734 | session->peer_cert_digest_type = (mbedtls_md_type_t) *p++; |
| 8735 | session->peer_cert_digest_len = (size_t) *p++; |
| 8736 | |
| 8737 | if( session->peer_cert_digest_len != 0 ) |
| 8738 | { |
| 8739 | const mbedtls_md_info_t *md_info = |
| 8740 | mbedtls_md_info_from_type( session->peer_cert_digest_type ); |
| 8741 | if( md_info == NULL ) |
| 8742 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8743 | if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) ) |
| 8744 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8745 | |
| 8746 | if( session->peer_cert_digest_len > (size_t)( end - p ) ) |
| 8747 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8748 | |
| 8749 | session->peer_cert_digest = |
| 8750 | mbedtls_calloc( 1, session->peer_cert_digest_len ); |
| 8751 | if( session->peer_cert_digest == NULL ) |
| 8752 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 8753 | |
| 8754 | memcpy( session->peer_cert_digest, p, |
| 8755 | session->peer_cert_digest_len ); |
| 8756 | p += session->peer_cert_digest_len; |
| 8757 | } |
| 8758 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 8759 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 8760 | |
| 8761 | /* |
| 8762 | * Session ticket and associated data |
| 8763 | */ |
| 8764 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
| 8765 | if( 3 > (size_t)( end - p ) ) |
| 8766 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8767 | |
| 8768 | session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2]; |
| 8769 | p += 3; |
| 8770 | |
| 8771 | if( session->ticket_len != 0 ) |
| 8772 | { |
| 8773 | if( session->ticket_len > (size_t)( end - p ) ) |
| 8774 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8775 | |
| 8776 | session->ticket = mbedtls_calloc( 1, session->ticket_len ); |
| 8777 | if( session->ticket == NULL ) |
| 8778 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 8779 | |
| 8780 | memcpy( session->ticket, p, session->ticket_len ); |
| 8781 | p += session->ticket_len; |
| 8782 | } |
| 8783 | |
| 8784 | if( 4 > (size_t)( end - p ) ) |
| 8785 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8786 | |
| 8787 | session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) | |
| 8788 | ( (uint32_t) p[1] << 16 ) | |
| 8789 | ( (uint32_t) p[2] << 8 ) | |
| 8790 | ( (uint32_t) p[3] ); |
| 8791 | p += 4; |
| 8792 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
| 8793 | |
| 8794 | /* |
| 8795 | * Misc extension-related info |
| 8796 | */ |
| 8797 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 8798 | if( 1 > (size_t)( end - p ) ) |
| 8799 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8800 | |
| 8801 | session->mfl_code = *p++; |
| 8802 | #endif |
| 8803 | |
| 8804 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 8805 | if( 1 > (size_t)( end - p ) ) |
| 8806 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8807 | |
| 8808 | session->encrypt_then_mac = *p++; |
| 8809 | #endif |
| 8810 | |
| 8811 | /* Done, should have consumed entire buffer */ |
| 8812 | if( p != end ) |
| 8813 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8814 | |
| 8815 | return( 0 ); |
| 8816 | } |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 8817 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 8818 | |
XiaokangQian | 75d40ef | 2022-04-20 11:05:24 +0000 | [diff] [blame] | 8819 | int mbedtls_ssl_validate_ciphersuite( |
| 8820 | const mbedtls_ssl_context *ssl, |
| 8821 | const mbedtls_ssl_ciphersuite_t *suite_info, |
| 8822 | mbedtls_ssl_protocol_version min_tls_version, |
| 8823 | mbedtls_ssl_protocol_version max_tls_version ) |
| 8824 | { |
| 8825 | (void) ssl; |
| 8826 | |
| 8827 | if( suite_info == NULL ) |
| 8828 | return( -1 ); |
| 8829 | |
| 8830 | if( ( suite_info->min_tls_version > max_tls_version ) || |
| 8831 | ( suite_info->max_tls_version < min_tls_version ) ) |
| 8832 | { |
| 8833 | return( -1 ); |
| 8834 | } |
| 8835 | |
XiaokangQian | 060d867 | 2022-04-21 09:24:56 +0000 | [diff] [blame] | 8836 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_CLI_C) |
XiaokangQian | 75d40ef | 2022-04-20 11:05:24 +0000 | [diff] [blame] | 8837 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
| 8838 | if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE && |
| 8839 | mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 ) |
| 8840 | { |
| 8841 | return( -1 ); |
| 8842 | } |
| 8843 | #endif |
| 8844 | |
| 8845 | /* Don't suggest PSK-based ciphersuite if no PSK is available. */ |
| 8846 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
| 8847 | if( mbedtls_ssl_ciphersuite_uses_psk( suite_info ) && |
| 8848 | mbedtls_ssl_conf_has_static_psk( ssl->conf ) == 0 ) |
| 8849 | { |
| 8850 | return( -1 ); |
| 8851 | } |
| 8852 | #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ |
| 8853 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 8854 | |
| 8855 | return( 0 ); |
| 8856 | } |
| 8857 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 8858 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 8859 | /* |
| 8860 | * Function for writing a signature algorithm extension. |
| 8861 | * |
| 8862 | * The `extension_data` field of signature algorithm contains a `SignatureSchemeList` |
| 8863 | * value (TLS 1.3 RFC8446): |
| 8864 | * enum { |
| 8865 | * .... |
| 8866 | * ecdsa_secp256r1_sha256( 0x0403 ), |
| 8867 | * ecdsa_secp384r1_sha384( 0x0503 ), |
| 8868 | * ecdsa_secp521r1_sha512( 0x0603 ), |
| 8869 | * .... |
| 8870 | * } SignatureScheme; |
| 8871 | * |
| 8872 | * struct { |
| 8873 | * SignatureScheme supported_signature_algorithms<2..2^16-2>; |
| 8874 | * } SignatureSchemeList; |
| 8875 | * |
| 8876 | * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm` |
| 8877 | * value (TLS 1.2 RFC5246): |
| 8878 | * enum { |
| 8879 | * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5), |
| 8880 | * sha512(6), (255) |
| 8881 | * } HashAlgorithm; |
| 8882 | * |
| 8883 | * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) } |
| 8884 | * SignatureAlgorithm; |
| 8885 | * |
| 8886 | * struct { |
| 8887 | * HashAlgorithm hash; |
| 8888 | * SignatureAlgorithm signature; |
| 8889 | * } SignatureAndHashAlgorithm; |
| 8890 | * |
| 8891 | * SignatureAndHashAlgorithm |
| 8892 | * supported_signature_algorithms<2..2^16-2>; |
| 8893 | * |
| 8894 | * The TLS 1.3 signature algorithm extension was defined to be a compatible |
| 8895 | * generalization of the TLS 1.2 signature algorithm extension. |
| 8896 | * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by |
| 8897 | * `SignatureScheme` field of TLS 1.3 |
| 8898 | * |
| 8899 | */ |
| 8900 | int mbedtls_ssl_write_sig_alg_ext( mbedtls_ssl_context *ssl, unsigned char *buf, |
| 8901 | const unsigned char *end, size_t *out_len ) |
| 8902 | { |
| 8903 | unsigned char *p = buf; |
| 8904 | unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */ |
| 8905 | size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */ |
| 8906 | |
| 8907 | *out_len = 0; |
| 8908 | |
| 8909 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding signature_algorithms extension" ) ); |
| 8910 | |
| 8911 | /* Check if we have space for header and length field: |
| 8912 | * - extension_type (2 bytes) |
| 8913 | * - extension_data_length (2 bytes) |
| 8914 | * - supported_signature_algorithms_length (2 bytes) |
| 8915 | */ |
| 8916 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 ); |
| 8917 | p += 6; |
| 8918 | |
| 8919 | /* |
| 8920 | * Write supported_signature_algorithms |
| 8921 | */ |
| 8922 | supported_sig_alg = p; |
| 8923 | const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs( ssl ); |
| 8924 | if( sig_alg == NULL ) |
| 8925 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); |
| 8926 | |
| 8927 | for( ; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++ ) |
| 8928 | { |
Jerry Yu | 53f5c15 | 2022-06-22 20:24:38 +0800 | [diff] [blame] | 8929 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "got signature scheme [%x] %s", |
| 8930 | *sig_alg, |
| 8931 | mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) ); |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 8932 | if( ! mbedtls_ssl_sig_alg_is_supported( ssl, *sig_alg ) ) |
| 8933 | continue; |
| 8934 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 ); |
| 8935 | MBEDTLS_PUT_UINT16_BE( *sig_alg, p, 0 ); |
| 8936 | p += 2; |
Jerry Yu | 80dd5db | 2022-06-22 19:30:32 +0800 | [diff] [blame] | 8937 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "sent signature scheme [%x] %s", |
Jerry Yu | f3b46b5 | 2022-06-19 16:52:27 +0800 | [diff] [blame] | 8938 | *sig_alg, |
| 8939 | mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) ); |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 8940 | } |
| 8941 | |
| 8942 | /* Length of supported_signature_algorithms */ |
| 8943 | supported_sig_alg_len = p - supported_sig_alg; |
| 8944 | if( supported_sig_alg_len == 0 ) |
| 8945 | { |
| 8946 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "No signature algorithms defined." ) ); |
| 8947 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 8948 | } |
| 8949 | |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 8950 | MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SIG_ALG, buf, 0 ); |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 8951 | MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len + 2, buf, 2 ); |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 8952 | MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len, buf, 4 ); |
| 8953 | |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 8954 | *out_len = p - buf; |
| 8955 | |
| 8956 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | c4bf5d6 | 2022-10-29 09:08:47 +0800 | [diff] [blame] | 8957 | mbedtls_ssl_tls13_set_hs_sent_ext_mask( ssl, MBEDTLS_TLS_EXT_SIG_ALG ); |
Jerry Yu | 0c354a2 | 2022-08-29 15:25:36 +0800 | [diff] [blame] | 8958 | MBEDTLS_SSL_DEBUG_MSG( |
| 8959 | 4, ( "sent %s extension", |
| 8960 | mbedtls_tls13_get_extension_name( |
| 8961 | MBEDTLS_TLS_EXT_SIG_ALG ) ) ); |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 8962 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Jerry Yu | 0c354a2 | 2022-08-29 15:25:36 +0800 | [diff] [blame] | 8963 | |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 8964 | return( 0 ); |
| 8965 | } |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 8966 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 8967 | |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 8968 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 8969 | /* |
| 8970 | * mbedtls_ssl_parse_server_name_ext |
| 8971 | * |
| 8972 | * Structure of server_name extension: |
| 8973 | * |
| 8974 | * enum { |
| 8975 | * host_name(0), (255) |
| 8976 | * } NameType; |
| 8977 | * opaque HostName<1..2^16-1>; |
| 8978 | * |
| 8979 | * struct { |
| 8980 | * NameType name_type; |
| 8981 | * select (name_type) { |
| 8982 | * case host_name: HostName; |
| 8983 | * } name; |
| 8984 | * } ServerName; |
| 8985 | * struct { |
| 8986 | * ServerName server_name_list<1..2^16-1> |
| 8987 | * } ServerNameList; |
| 8988 | */ |
Ronald Cron | ce7d76e | 2022-07-08 18:56:49 +0200 | [diff] [blame] | 8989 | MBEDTLS_CHECK_RETURN_CRITICAL |
XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 8990 | int mbedtls_ssl_parse_server_name_ext( mbedtls_ssl_context *ssl, |
| 8991 | const unsigned char *buf, |
| 8992 | const unsigned char *end ) |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 8993 | { |
| 8994 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 8995 | const unsigned char *p = buf; |
XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 8996 | size_t server_name_list_len, hostname_len; |
| 8997 | const unsigned char *server_name_list_end; |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 8998 | |
XiaokangQian | f2a9420 | 2022-05-20 06:44:24 +0000 | [diff] [blame] | 8999 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "parse ServerName extension" ) ); |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 9000 | |
| 9001 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 ); |
XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 9002 | server_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 ); |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 9003 | p += 2; |
| 9004 | |
XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 9005 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, server_name_list_len ); |
| 9006 | server_name_list_end = p + server_name_list_len; |
XiaokangQian | 75fe8c7 | 2022-06-15 09:42:45 +0000 | [diff] [blame] | 9007 | while( p < server_name_list_end ) |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 9008 | { |
XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 9009 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end, 3 ); |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 9010 | hostname_len = MBEDTLS_GET_UINT16_BE( p, 1 ); |
XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 9011 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end, |
| 9012 | hostname_len + 3 ); |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 9013 | |
| 9014 | if( p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME ) |
| 9015 | { |
XiaokangQian | 75fe8c7 | 2022-06-15 09:42:45 +0000 | [diff] [blame] | 9016 | /* sni_name is intended to be used only during the parsing of the |
| 9017 | * ClientHello message (it is reset to NULL before the end of |
| 9018 | * the message parsing). Thus it is ok to just point to the |
| 9019 | * reception buffer and not make a copy of it. |
| 9020 | */ |
XiaokangQian | f2a9420 | 2022-05-20 06:44:24 +0000 | [diff] [blame] | 9021 | ssl->handshake->sni_name = p + 3; |
| 9022 | ssl->handshake->sni_name_len = hostname_len; |
| 9023 | if( ssl->conf->f_sni == NULL ) |
| 9024 | return( 0 ); |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 9025 | ret = ssl->conf->f_sni( ssl->conf->p_sni, |
XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 9026 | ssl, p + 3, hostname_len ); |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 9027 | if( ret != 0 ) |
| 9028 | { |
XiaokangQian | f2a9420 | 2022-05-20 06:44:24 +0000 | [diff] [blame] | 9029 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_sni_wrapper", ret ); |
XiaokangQian | 129aeb9 | 2022-06-02 09:29:18 +0000 | [diff] [blame] | 9030 | MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME, |
| 9031 | MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME ); |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 9032 | return( MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME ); |
| 9033 | } |
| 9034 | return( 0 ); |
| 9035 | } |
| 9036 | |
| 9037 | p += hostname_len + 3; |
| 9038 | } |
| 9039 | |
| 9040 | return( 0 ); |
| 9041 | } |
| 9042 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
| 9043 | |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9044 | #if defined(MBEDTLS_SSL_ALPN) |
Ronald Cron | ce7d76e | 2022-07-08 18:56:49 +0200 | [diff] [blame] | 9045 | MBEDTLS_CHECK_RETURN_CRITICAL |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9046 | int mbedtls_ssl_parse_alpn_ext( mbedtls_ssl_context *ssl, |
| 9047 | const unsigned char *buf, |
| 9048 | const unsigned char *end ) |
| 9049 | { |
| 9050 | const unsigned char *p = buf; |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 9051 | size_t protocol_name_list_len; |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9052 | const unsigned char *protocol_name_list; |
| 9053 | const unsigned char *protocol_name_list_end; |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 9054 | size_t protocol_name_len; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9055 | |
| 9056 | /* If ALPN not configured, just ignore the extension */ |
| 9057 | if( ssl->conf->alpn_list == NULL ) |
| 9058 | return( 0 ); |
| 9059 | |
| 9060 | /* |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 9061 | * RFC7301, section 3.1 |
| 9062 | * opaque ProtocolName<1..2^8-1>; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9063 | * |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 9064 | * struct { |
| 9065 | * ProtocolName protocol_name_list<2..2^16-1> |
| 9066 | * } ProtocolNameList; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9067 | */ |
| 9068 | |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 9069 | /* |
XiaokangQian | 0b776e2 | 2022-06-24 09:04:59 +0000 | [diff] [blame] | 9070 | * protocol_name_list_len 2 bytes |
| 9071 | * protocol_name_len 1 bytes |
| 9072 | * protocol_name >=1 byte |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 9073 | */ |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9074 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 ); |
| 9075 | |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 9076 | protocol_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 ); |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9077 | p += 2; |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 9078 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, protocol_name_list_len ); |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9079 | protocol_name_list = p; |
| 9080 | protocol_name_list_end = p + protocol_name_list_len; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9081 | |
| 9082 | /* Validate peer's list (lengths) */ |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9083 | while( p < protocol_name_list_end ) |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9084 | { |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9085 | protocol_name_len = *p++; |
| 9086 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, protocol_name_list_end, |
| 9087 | protocol_name_len ); |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 9088 | if( protocol_name_len == 0 ) |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9089 | { |
| 9090 | MBEDTLS_SSL_PEND_FATAL_ALERT( |
| 9091 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER, |
| 9092 | MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER ); |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9093 | return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER ); |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9094 | } |
| 9095 | |
| 9096 | p += protocol_name_len; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9097 | } |
| 9098 | |
| 9099 | /* Use our order of preference */ |
| 9100 | for( const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++ ) |
| 9101 | { |
| 9102 | size_t const alpn_len = strlen( *alpn ); |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9103 | p = protocol_name_list; |
| 9104 | while( p < protocol_name_list_end ) |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9105 | { |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9106 | protocol_name_len = *p++; |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 9107 | if( protocol_name_len == alpn_len && |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9108 | memcmp( p, *alpn, alpn_len ) == 0 ) |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9109 | { |
| 9110 | ssl->alpn_chosen = *alpn; |
| 9111 | return( 0 ); |
| 9112 | } |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9113 | |
| 9114 | p += protocol_name_len; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9115 | } |
| 9116 | } |
| 9117 | |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9118 | /* If we get here, no match was found */ |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9119 | MBEDTLS_SSL_PEND_FATAL_ALERT( |
| 9120 | MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL, |
| 9121 | MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL ); |
| 9122 | return( MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL ); |
| 9123 | } |
| 9124 | |
| 9125 | int mbedtls_ssl_write_alpn_ext( mbedtls_ssl_context *ssl, |
| 9126 | unsigned char *buf, |
| 9127 | unsigned char *end, |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 9128 | size_t *out_len ) |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9129 | { |
| 9130 | unsigned char *p = buf; |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9131 | size_t protocol_name_len; |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 9132 | *out_len = 0; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9133 | |
| 9134 | if( ssl->alpn_chosen == NULL ) |
| 9135 | { |
| 9136 | return( 0 ); |
| 9137 | } |
| 9138 | |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9139 | protocol_name_len = strlen( ssl->alpn_chosen ); |
| 9140 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 7 + protocol_name_len ); |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9141 | |
| 9142 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "server side, adding alpn extension" ) ); |
| 9143 | /* |
| 9144 | * 0 . 1 ext identifier |
| 9145 | * 2 . 3 ext length |
| 9146 | * 4 . 5 protocol list length |
| 9147 | * 6 . 6 protocol name length |
| 9148 | * 7 . 7+n protocol name |
| 9149 | */ |
| 9150 | MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ALPN, p, 0 ); |
| 9151 | |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9152 | *out_len = 7 + protocol_name_len; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9153 | |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9154 | MBEDTLS_PUT_UINT16_BE( protocol_name_len + 3, p, 2 ); |
| 9155 | MBEDTLS_PUT_UINT16_BE( protocol_name_len + 1, p, 4 ); |
XiaokangQian | 0b776e2 | 2022-06-24 09:04:59 +0000 | [diff] [blame] | 9156 | /* Note: the length of the chosen protocol has been checked to be less |
| 9157 | * than 255 bytes in `mbedtls_ssl_conf_alpn_protocols`. |
| 9158 | */ |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9159 | p[6] = MBEDTLS_BYTE_0( protocol_name_len ); |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9160 | |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9161 | memcpy( p + 7, ssl->alpn_chosen, protocol_name_len ); |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9162 | return ( 0 ); |
| 9163 | } |
| 9164 | #endif /* MBEDTLS_SSL_ALPN */ |
| 9165 | |
Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 9166 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \ |
Xiaokang Qian | 0340929 | 2022-10-12 02:49:52 +0000 | [diff] [blame] | 9167 | defined(MBEDTLS_SSL_SESSION_TICKETS) && \ |
Xiaokang Qian | ed0620c | 2022-10-12 06:58:13 +0000 | [diff] [blame] | 9168 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \ |
Xiaokang Qian | ed3afcd | 2022-10-12 08:31:11 +0000 | [diff] [blame] | 9169 | defined(MBEDTLS_SSL_CLI_C) |
Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 9170 | int mbedtls_ssl_session_set_hostname( mbedtls_ssl_session *session, |
| 9171 | const char *hostname ) |
| 9172 | { |
| 9173 | /* Initialize to suppress unnecessary compiler warning */ |
| 9174 | size_t hostname_len = 0; |
| 9175 | |
| 9176 | /* Check if new hostname is valid before |
| 9177 | * making any change to current one */ |
| 9178 | if( hostname != NULL ) |
| 9179 | { |
| 9180 | hostname_len = strlen( hostname ); |
| 9181 | |
| 9182 | if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN ) |
| 9183 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 9184 | } |
| 9185 | |
| 9186 | /* Now it's clear that we will overwrite the old hostname, |
| 9187 | * so we can free it safely */ |
| 9188 | if( session->hostname != NULL ) |
| 9189 | { |
| 9190 | mbedtls_platform_zeroize( session->hostname, |
| 9191 | strlen( session->hostname ) ); |
| 9192 | mbedtls_free( session->hostname ); |
| 9193 | } |
| 9194 | |
| 9195 | /* Passing NULL as hostname shall clear the old one */ |
| 9196 | if( hostname == NULL ) |
| 9197 | { |
| 9198 | session->hostname = NULL; |
| 9199 | } |
| 9200 | else |
| 9201 | { |
| 9202 | session->hostname = mbedtls_calloc( 1, hostname_len + 1 ); |
| 9203 | if( session->hostname == NULL ) |
| 9204 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 9205 | |
| 9206 | memcpy( session->hostname, hostname, hostname_len ); |
| 9207 | } |
| 9208 | |
| 9209 | return( 0 ); |
| 9210 | } |
Xiaokang Qian | 0340929 | 2022-10-12 02:49:52 +0000 | [diff] [blame] | 9211 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 && |
| 9212 | MBEDTLS_SSL_SESSION_TICKETS && |
Xiaokang Qian | ed0620c | 2022-10-12 06:58:13 +0000 | [diff] [blame] | 9213 | MBEDTLS_SSL_SERVER_NAME_INDICATION && |
Xiaokang Qian | ed3afcd | 2022-10-12 08:31:11 +0000 | [diff] [blame] | 9214 | MBEDTLS_SSL_CLI_C */ |
Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 9215 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9216 | #endif /* MBEDTLS_SSL_TLS_C */ |