Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /* |
Mateusz Starzyk | 06b07fb | 2021-02-18 13:55:21 +0100 | [diff] [blame] | 2 | * TLS shared functions |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3 | * |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 4 | * Copyright The Mbed TLS Contributors |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: Apache-2.0 |
| 6 | * |
| 7 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 8 | * not use this file except in compliance with the License. |
| 9 | * You may obtain a copy of the License at |
| 10 | * |
| 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | * |
| 13 | * Unless required by applicable law or agreed to in writing, software |
| 14 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | * See the License for the specific language governing permissions and |
| 17 | * limitations under the License. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 18 | */ |
| 19 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 20 | * http://www.ietf.org/rfc/rfc2246.txt |
| 21 | * http://www.ietf.org/rfc/rfc4346.txt |
| 22 | */ |
| 23 | |
Gilles Peskine | db09ef6 | 2020-06-03 01:43:33 +0200 | [diff] [blame] | 24 | #include "common.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 25 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 26 | #if defined(MBEDTLS_SSL_TLS_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 27 | |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 28 | #include <assert.h> |
| 29 | |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 30 | #include "mbedtls/platform.h" |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 31 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 32 | #include "mbedtls/ssl.h" |
Ronald Cron | 9f0fba3 | 2022-02-10 16:45:15 +0100 | [diff] [blame] | 33 | #include "ssl_client.h" |
Ronald Cron | 27c85e7 | 2022-03-08 11:37:55 +0100 | [diff] [blame] | 34 | #include "ssl_debug_helpers.h" |
Chris Jones | 84a773f | 2021-03-05 18:38:47 +0000 | [diff] [blame] | 35 | #include "ssl_misc.h" |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 36 | |
Janos Follath | 73c616b | 2019-12-18 15:07:04 +0000 | [diff] [blame] | 37 | #include "mbedtls/debug.h" |
| 38 | #include "mbedtls/error.h" |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 39 | #include "mbedtls/platform_util.h" |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 40 | #include "mbedtls/version.h" |
Gabor Mezei | 765862c | 2021-10-19 12:22:25 +0200 | [diff] [blame] | 41 | #include "mbedtls/constant_time.h" |
Paul Bakker | 0be444a | 2013-08-27 21:55:01 +0200 | [diff] [blame] | 42 | |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 43 | #include <string.h> |
| 44 | |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 45 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 46 | #include "mbedtls/psa_util.h" |
| 47 | #include "psa/crypto.h" |
| 48 | #endif |
Manuel Pégourié-Gonnard | 07018f9 | 2022-09-15 11:29:35 +0200 | [diff] [blame] | 49 | #include "mbedtls/legacy_or_psa.h" |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 50 | |
Janos Follath | 23bdca0 | 2016-10-07 14:47:14 +0100 | [diff] [blame] | 51 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 52 | #include "mbedtls/oid.h" |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 53 | #endif |
| 54 | |
Ronald Cron | ad8c17b | 2022-06-10 17:18:09 +0200 | [diff] [blame] | 55 | #if defined(MBEDTLS_TEST_HOOKS) |
| 56 | static mbedtls_ssl_chk_buf_ptr_args chk_buf_ptr_fail_args; |
| 57 | |
| 58 | void mbedtls_ssl_set_chk_buf_ptr_fail_args( |
| 59 | const uint8_t *cur, const uint8_t *end, size_t need ) |
| 60 | { |
| 61 | chk_buf_ptr_fail_args.cur = cur; |
| 62 | chk_buf_ptr_fail_args.end = end; |
| 63 | chk_buf_ptr_fail_args.need = need; |
| 64 | } |
| 65 | |
| 66 | void mbedtls_ssl_reset_chk_buf_ptr_fail_args( void ) |
| 67 | { |
| 68 | memset( &chk_buf_ptr_fail_args, 0, sizeof( chk_buf_ptr_fail_args ) ); |
| 69 | } |
| 70 | |
| 71 | int mbedtls_ssl_cmp_chk_buf_ptr_fail_args( mbedtls_ssl_chk_buf_ptr_args *args ) |
| 72 | { |
| 73 | return( ( chk_buf_ptr_fail_args.cur != args->cur ) || |
| 74 | ( chk_buf_ptr_fail_args.end != args->end ) || |
| 75 | ( chk_buf_ptr_fail_args.need != args->need ) ); |
| 76 | } |
| 77 | #endif /* MBEDTLS_TEST_HOOKS */ |
| 78 | |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 79 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 80 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 81 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 82 | /* Top-level Connection ID API */ |
| 83 | |
Hanno Becker | 8367ccc | 2019-05-14 11:30:10 +0100 | [diff] [blame] | 84 | int mbedtls_ssl_conf_cid( mbedtls_ssl_config *conf, |
| 85 | size_t len, |
| 86 | int ignore_other_cid ) |
Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 87 | { |
| 88 | if( len > MBEDTLS_SSL_CID_IN_LEN_MAX ) |
| 89 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 90 | |
Hanno Becker | 611ac77 | 2019-05-14 11:45:26 +0100 | [diff] [blame] | 91 | if( ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL && |
| 92 | ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE ) |
| 93 | { |
| 94 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 95 | } |
| 96 | |
| 97 | conf->ignore_unexpected_cid = ignore_other_cid; |
Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 98 | conf->cid_len = len; |
| 99 | return( 0 ); |
| 100 | } |
| 101 | |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 102 | int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl, |
| 103 | int enable, |
| 104 | unsigned char const *own_cid, |
| 105 | size_t own_cid_len ) |
| 106 | { |
Hanno Becker | 76a79ab | 2019-05-03 14:38:32 +0100 | [diff] [blame] | 107 | if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 108 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 109 | |
Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 110 | ssl->negotiate_cid = enable; |
| 111 | if( enable == MBEDTLS_SSL_CID_DISABLED ) |
| 112 | { |
| 113 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) ); |
| 114 | return( 0 ); |
| 115 | } |
| 116 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) ); |
Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 117 | MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len ); |
Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 118 | |
Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 119 | if( own_cid_len != ssl->conf->cid_len ) |
Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 120 | { |
Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 121 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID length %u does not match CID length %u in config", |
| 122 | (unsigned) own_cid_len, |
| 123 | (unsigned) ssl->conf->cid_len ) ); |
Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 124 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 125 | } |
| 126 | |
| 127 | memcpy( ssl->own_cid, own_cid, own_cid_len ); |
Hanno Becker | b7ee0cf | 2019-04-30 14:07:31 +0100 | [diff] [blame] | 128 | /* Truncation is not an issue here because |
| 129 | * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */ |
| 130 | ssl->own_cid_len = (uint8_t) own_cid_len; |
Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 131 | |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 132 | return( 0 ); |
| 133 | } |
| 134 | |
Paul Elliott | 0113cf1 | 2022-03-11 20:26:47 +0000 | [diff] [blame] | 135 | int mbedtls_ssl_get_own_cid( mbedtls_ssl_context *ssl, |
| 136 | int *enabled, |
| 137 | unsigned char own_cid[MBEDTLS_SSL_CID_OUT_LEN_MAX], |
| 138 | size_t *own_cid_len ) |
| 139 | { |
| 140 | *enabled = MBEDTLS_SSL_CID_DISABLED; |
| 141 | |
| 142 | if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 143 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 144 | |
| 145 | /* We report MBEDTLS_SSL_CID_DISABLED in case the CID length is |
| 146 | * zero as this is indistinguishable from not requesting to use |
| 147 | * the CID extension. */ |
| 148 | if( ssl->own_cid_len == 0 || ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED ) |
| 149 | return( 0 ); |
| 150 | |
| 151 | if( own_cid_len != NULL ) |
| 152 | { |
| 153 | *own_cid_len = ssl->own_cid_len; |
| 154 | if( own_cid != NULL ) |
| 155 | memcpy( own_cid, ssl->own_cid, ssl->own_cid_len ); |
| 156 | } |
| 157 | |
| 158 | *enabled = MBEDTLS_SSL_CID_ENABLED; |
| 159 | |
| 160 | return( 0 ); |
| 161 | } |
| 162 | |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 163 | int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl, |
| 164 | int *enabled, |
| 165 | unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ], |
| 166 | size_t *peer_cid_len ) |
| 167 | { |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 168 | *enabled = MBEDTLS_SSL_CID_DISABLED; |
Hanno Becker | b1f89cd | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 169 | |
Hanno Becker | 76a79ab | 2019-05-03 14:38:32 +0100 | [diff] [blame] | 170 | if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM || |
Paul Elliott | 27b0d94 | 2022-03-18 21:55:32 +0000 | [diff] [blame] | 171 | mbedtls_ssl_is_handshake_over( ssl ) == 0 ) |
Hanno Becker | 76a79ab | 2019-05-03 14:38:32 +0100 | [diff] [blame] | 172 | { |
Hanno Becker | b1f89cd | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 173 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Hanno Becker | 76a79ab | 2019-05-03 14:38:32 +0100 | [diff] [blame] | 174 | } |
Hanno Becker | b1f89cd | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 175 | |
Hanno Becker | c5f2422 | 2019-05-03 12:54:52 +0100 | [diff] [blame] | 176 | /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions |
| 177 | * were used, but client and server requested the empty CID. |
| 178 | * This is indistinguishable from not using the CID extension |
| 179 | * in the first place. */ |
Hanno Becker | b1f89cd | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 180 | if( ssl->transform_in->in_cid_len == 0 && |
| 181 | ssl->transform_in->out_cid_len == 0 ) |
| 182 | { |
| 183 | return( 0 ); |
| 184 | } |
| 185 | |
Hanno Becker | 615ef17 | 2019-05-22 16:50:35 +0100 | [diff] [blame] | 186 | if( peer_cid_len != NULL ) |
| 187 | { |
| 188 | *peer_cid_len = ssl->transform_in->out_cid_len; |
| 189 | if( peer_cid != NULL ) |
| 190 | { |
| 191 | memcpy( peer_cid, ssl->transform_in->out_cid, |
| 192 | ssl->transform_in->out_cid_len ); |
| 193 | } |
| 194 | } |
Hanno Becker | b1f89cd | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 195 | |
| 196 | *enabled = MBEDTLS_SSL_CID_ENABLED; |
| 197 | |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 198 | return( 0 ); |
| 199 | } |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 200 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 201 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 202 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 203 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 204 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Manuel Pégourié-Gonnard | 581e6b6 | 2013-07-18 12:32:27 +0200 | [diff] [blame] | 205 | /* |
| 206 | * Convert max_fragment_length codes to length. |
| 207 | * RFC 6066 says: |
| 208 | * enum{ |
| 209 | * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255) |
| 210 | * } MaxFragmentLength; |
| 211 | * and we add 0 -> extension unused |
| 212 | */ |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 213 | static unsigned int ssl_mfl_code_to_length( int mfl ) |
Manuel Pégourié-Gonnard | 581e6b6 | 2013-07-18 12:32:27 +0200 | [diff] [blame] | 214 | { |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 215 | switch( mfl ) |
| 216 | { |
| 217 | case MBEDTLS_SSL_MAX_FRAG_LEN_NONE: |
| 218 | return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN ); |
| 219 | case MBEDTLS_SSL_MAX_FRAG_LEN_512: |
| 220 | return 512; |
| 221 | case MBEDTLS_SSL_MAX_FRAG_LEN_1024: |
| 222 | return 1024; |
| 223 | case MBEDTLS_SSL_MAX_FRAG_LEN_2048: |
| 224 | return 2048; |
| 225 | case MBEDTLS_SSL_MAX_FRAG_LEN_4096: |
| 226 | return 4096; |
| 227 | default: |
| 228 | return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN ); |
| 229 | } |
| 230 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 231 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
Manuel Pégourié-Gonnard | 581e6b6 | 2013-07-18 12:32:27 +0200 | [diff] [blame] | 232 | |
Hanno Becker | 52055ae | 2019-02-06 14:30:46 +0000 | [diff] [blame] | 233 | int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst, |
| 234 | const mbedtls_ssl_session *src ) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 235 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 236 | mbedtls_ssl_session_free( dst ); |
| 237 | memcpy( dst, src, sizeof( mbedtls_ssl_session ) ); |
吴敬辉 | 0b71611 | 2021-11-29 10:46:35 +0800 | [diff] [blame] | 238 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
| 239 | dst->ticket = NULL; |
Xiaokang Qian | 8730644 | 2022-10-12 09:47:38 +0000 | [diff] [blame] | 240 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \ |
| 241 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Xiaokang Qian | 126bf8e | 2022-10-13 02:22:40 +0000 | [diff] [blame] | 242 | dst->hostname = NULL; |
吴敬辉 | 0b71611 | 2021-11-29 10:46:35 +0800 | [diff] [blame] | 243 | #endif |
Xiaokang Qian | 8730644 | 2022-10-12 09:47:38 +0000 | [diff] [blame] | 244 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
吴敬辉 | 0b71611 | 2021-11-29 10:46:35 +0800 | [diff] [blame] | 245 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 246 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Hanno Becker | 6d1986e | 2019-02-07 12:27:42 +0000 | [diff] [blame] | 247 | |
| 248 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 249 | if( src->peer_cert != NULL ) |
| 250 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 251 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 2292d1f | 2013-09-15 17:06:49 +0200 | [diff] [blame] | 252 | |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 253 | dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 254 | if( dst->peer_cert == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 255 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 256 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 257 | mbedtls_x509_crt_init( dst->peer_cert ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 258 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 259 | if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p, |
Manuel Pégourié-Gonnard | 4d2a8eb | 2014-06-13 20:33:27 +0200 | [diff] [blame] | 260 | src->peer_cert->raw.len ) ) != 0 ) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 261 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 262 | mbedtls_free( dst->peer_cert ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 263 | dst->peer_cert = NULL; |
| 264 | return( ret ); |
| 265 | } |
| 266 | } |
Hanno Becker | 6d1986e | 2019-02-07 12:27:42 +0000 | [diff] [blame] | 267 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Hanno Becker | 9198ad1 | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 268 | if( src->peer_cert_digest != NULL ) |
| 269 | { |
Hanno Becker | 9198ad1 | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 270 | dst->peer_cert_digest = |
Hanno Becker | accc599 | 2019-02-25 10:06:59 +0000 | [diff] [blame] | 271 | mbedtls_calloc( 1, src->peer_cert_digest_len ); |
Hanno Becker | 9198ad1 | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 272 | if( dst->peer_cert_digest == NULL ) |
| 273 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 274 | |
| 275 | memcpy( dst->peer_cert_digest, src->peer_cert_digest, |
| 276 | src->peer_cert_digest_len ); |
| 277 | dst->peer_cert_digest_type = src->peer_cert_digest_type; |
Hanno Becker | accc599 | 2019-02-25 10:06:59 +0000 | [diff] [blame] | 278 | dst->peer_cert_digest_len = src->peer_cert_digest_len; |
Hanno Becker | 9198ad1 | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 279 | } |
| 280 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 281 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 282 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 283 | |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 284 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 285 | if( src->ticket != NULL ) |
| 286 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 287 | dst->ticket = mbedtls_calloc( 1, src->ticket_len ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 288 | if( dst->ticket == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 289 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 290 | |
| 291 | memcpy( dst->ticket, src->ticket, src->ticket_len ); |
| 292 | } |
Xiaokang Qian | 126bf8e | 2022-10-13 02:22:40 +0000 | [diff] [blame] | 293 | |
| 294 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \ |
| 295 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 296 | if( src->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 297 | { |
| 298 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 299 | ret = mbedtls_ssl_session_set_hostname( dst, src->hostname ); |
| 300 | if( ret != 0 ) |
| 301 | return ( ret ); |
| 302 | } |
| 303 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 && |
| 304 | MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 305 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 306 | |
| 307 | return( 0 ); |
| 308 | } |
| 309 | |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 310 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 311 | MBEDTLS_CHECK_RETURN_CRITICAL |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 312 | static int resize_buffer( unsigned char **buffer, size_t len_new, size_t *len_old ) |
| 313 | { |
| 314 | unsigned char* resized_buffer = mbedtls_calloc( 1, len_new ); |
| 315 | if( resized_buffer == NULL ) |
| 316 | return -1; |
| 317 | |
| 318 | /* We want to copy len_new bytes when downsizing the buffer, and |
| 319 | * len_old bytes when upsizing, so we choose the smaller of two sizes, |
| 320 | * to fit one buffer into another. Size checks, ensuring that no data is |
| 321 | * lost, are done outside of this function. */ |
| 322 | memcpy( resized_buffer, *buffer, |
| 323 | ( len_new < *len_old ) ? len_new : *len_old ); |
| 324 | mbedtls_platform_zeroize( *buffer, *len_old ); |
| 325 | mbedtls_free( *buffer ); |
| 326 | |
| 327 | *buffer = resized_buffer; |
| 328 | *len_old = len_new; |
| 329 | |
| 330 | return 0; |
| 331 | } |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 332 | |
| 333 | static void handle_buffer_resizing( mbedtls_ssl_context *ssl, int downsizing, |
Andrzej Kurek | 069fa96 | 2021-01-07 08:02:15 -0500 | [diff] [blame] | 334 | size_t in_buf_new_len, |
| 335 | size_t out_buf_new_len ) |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 336 | { |
| 337 | int modified = 0; |
| 338 | size_t written_in = 0, iv_offset_in = 0, len_offset_in = 0; |
| 339 | size_t written_out = 0, iv_offset_out = 0, len_offset_out = 0; |
| 340 | if( ssl->in_buf != NULL ) |
| 341 | { |
| 342 | written_in = ssl->in_msg - ssl->in_buf; |
| 343 | iv_offset_in = ssl->in_iv - ssl->in_buf; |
| 344 | len_offset_in = ssl->in_len - ssl->in_buf; |
| 345 | if( downsizing ? |
| 346 | ssl->in_buf_len > in_buf_new_len && ssl->in_left < in_buf_new_len : |
| 347 | ssl->in_buf_len < in_buf_new_len ) |
| 348 | { |
| 349 | if( resize_buffer( &ssl->in_buf, in_buf_new_len, &ssl->in_buf_len ) != 0 ) |
| 350 | { |
| 351 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "input buffer resizing failed - out of memory" ) ); |
| 352 | } |
| 353 | else |
| 354 | { |
Paul Elliott | b744990 | 2021-03-10 18:14:58 +0000 | [diff] [blame] | 355 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reallocating in_buf to %" MBEDTLS_PRINTF_SIZET, |
| 356 | in_buf_new_len ) ); |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 357 | modified = 1; |
| 358 | } |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | if( ssl->out_buf != NULL ) |
| 363 | { |
| 364 | written_out = ssl->out_msg - ssl->out_buf; |
| 365 | iv_offset_out = ssl->out_iv - ssl->out_buf; |
| 366 | len_offset_out = ssl->out_len - ssl->out_buf; |
| 367 | if( downsizing ? |
| 368 | ssl->out_buf_len > out_buf_new_len && ssl->out_left < out_buf_new_len : |
| 369 | ssl->out_buf_len < out_buf_new_len ) |
| 370 | { |
| 371 | if( resize_buffer( &ssl->out_buf, out_buf_new_len, &ssl->out_buf_len ) != 0 ) |
| 372 | { |
| 373 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "output buffer resizing failed - out of memory" ) ); |
| 374 | } |
| 375 | else |
| 376 | { |
Paul Elliott | b744990 | 2021-03-10 18:14:58 +0000 | [diff] [blame] | 377 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reallocating out_buf to %" MBEDTLS_PRINTF_SIZET, |
| 378 | out_buf_new_len ) ); |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 379 | modified = 1; |
| 380 | } |
| 381 | } |
| 382 | } |
| 383 | if( modified ) |
| 384 | { |
| 385 | /* Update pointers here to avoid doing it twice. */ |
| 386 | mbedtls_ssl_reset_in_out_pointers( ssl ); |
| 387 | /* Fields below might not be properly updated with record |
| 388 | * splitting or with CID, so they are manually updated here. */ |
| 389 | ssl->out_msg = ssl->out_buf + written_out; |
| 390 | ssl->out_len = ssl->out_buf + len_offset_out; |
| 391 | ssl->out_iv = ssl->out_buf + iv_offset_out; |
| 392 | |
| 393 | ssl->in_msg = ssl->in_buf + written_in; |
| 394 | ssl->in_len = ssl->in_buf + len_offset_in; |
| 395 | ssl->in_iv = ssl->in_buf + iv_offset_in; |
| 396 | } |
| 397 | } |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 398 | #endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */ |
| 399 | |
Jerry Yu | db8c48a | 2022-01-27 14:54:54 +0800 | [diff] [blame] | 400 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Jerry Yu | ed14c93 | 2022-02-17 13:40:45 +0800 | [diff] [blame] | 401 | |
| 402 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
| 403 | typedef int (*tls_prf_fn)( const unsigned char *secret, size_t slen, |
| 404 | const char *label, |
| 405 | const unsigned char *random, size_t rlen, |
| 406 | unsigned char *dstbuf, size_t dlen ); |
| 407 | |
| 408 | static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id ); |
| 409 | |
| 410 | #endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */ |
| 411 | |
| 412 | /* Type for the TLS PRF */ |
| 413 | typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *, |
| 414 | const unsigned char *, size_t, |
| 415 | unsigned char *, size_t); |
| 416 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 417 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | ed14c93 | 2022-02-17 13:40:45 +0800 | [diff] [blame] | 418 | static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform, |
| 419 | int ciphersuite, |
| 420 | const unsigned char master[48], |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 421 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Jerry Yu | ed14c93 | 2022-02-17 13:40:45 +0800 | [diff] [blame] | 422 | int encrypt_then_mac, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 423 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Jerry Yu | ed14c93 | 2022-02-17 13:40:45 +0800 | [diff] [blame] | 424 | ssl_tls_prf_t tls_prf, |
| 425 | const unsigned char randbytes[64], |
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 426 | mbedtls_ssl_protocol_version tls_version, |
Jerry Yu | ed14c93 | 2022-02-17 13:40:45 +0800 | [diff] [blame] | 427 | unsigned endpoint, |
| 428 | const mbedtls_ssl_context *ssl ); |
| 429 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 430 | #if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 431 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | ed14c93 | 2022-02-17 13:40:45 +0800 | [diff] [blame] | 432 | static int tls_prf_sha256( const unsigned char *secret, size_t slen, |
| 433 | const char *label, |
| 434 | const unsigned char *random, size_t rlen, |
| 435 | unsigned char *dstbuf, size_t dlen ); |
| 436 | static void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *,unsigned char*, size_t * ); |
| 437 | static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int ); |
| 438 | |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 439 | #endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/ |
Jerry Yu | ed14c93 | 2022-02-17 13:40:45 +0800 | [diff] [blame] | 440 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 441 | #if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 442 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | ed14c93 | 2022-02-17 13:40:45 +0800 | [diff] [blame] | 443 | static int tls_prf_sha384( const unsigned char *secret, size_t slen, |
| 444 | const char *label, |
| 445 | const unsigned char *random, size_t rlen, |
| 446 | unsigned char *dstbuf, size_t dlen ); |
| 447 | |
| 448 | static void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *, unsigned char*, size_t * ); |
| 449 | static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int ); |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 450 | #endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/ |
Jerry Yu | ed14c93 | 2022-02-17 13:40:45 +0800 | [diff] [blame] | 451 | |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 452 | static size_t ssl_tls12_session_save( const mbedtls_ssl_session *session, |
Jerry Yu | ed14c93 | 2022-02-17 13:40:45 +0800 | [diff] [blame] | 453 | unsigned char *buf, |
| 454 | size_t buf_len ); |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 455 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 456 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 457 | static int ssl_tls12_session_load( mbedtls_ssl_session *session, |
Jerry Yu | ed14c93 | 2022-02-17 13:40:45 +0800 | [diff] [blame] | 458 | const unsigned char *buf, |
| 459 | size_t len ); |
| 460 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 461 | |
Jerry Yu | 53d23e2 | 2022-02-09 16:25:09 +0800 | [diff] [blame] | 462 | static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t ); |
| 463 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 464 | #if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Jerry Yu | db8c48a | 2022-01-27 14:54:54 +0800 | [diff] [blame] | 465 | static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t ); |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 466 | #endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/ |
Jerry Yu | db8c48a | 2022-01-27 14:54:54 +0800 | [diff] [blame] | 467 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 468 | #if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Jerry Yu | db8c48a | 2022-01-27 14:54:54 +0800 | [diff] [blame] | 469 | static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t ); |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 470 | #endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/ |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 471 | |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 472 | int mbedtls_ssl_tls_prf( const mbedtls_tls_prf_types prf, |
| 473 | const unsigned char *secret, size_t slen, |
| 474 | const char *label, |
| 475 | const unsigned char *random, size_t rlen, |
| 476 | unsigned char *dstbuf, size_t dlen ) |
| 477 | { |
| 478 | mbedtls_ssl_tls_prf_cb *tls_prf = NULL; |
| 479 | |
| 480 | switch( prf ) |
| 481 | { |
Ron Eldor | d2f25f7 | 2019-05-15 14:54:22 +0300 | [diff] [blame] | 482 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 483 | #if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 484 | case MBEDTLS_SSL_TLS_PRF_SHA384: |
| 485 | tls_prf = tls_prf_sha384; |
| 486 | break; |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 487 | #endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/ |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 488 | #if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 489 | case MBEDTLS_SSL_TLS_PRF_SHA256: |
| 490 | tls_prf = tls_prf_sha256; |
| 491 | break; |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 492 | #endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/ |
Ron Eldor | d2f25f7 | 2019-05-15 14:54:22 +0300 | [diff] [blame] | 493 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 494 | default: |
| 495 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 496 | } |
| 497 | |
| 498 | return( tls_prf( secret, slen, label, random, rlen, dstbuf, dlen ) ); |
| 499 | } |
| 500 | |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 501 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 502 | static void ssl_clear_peer_cert( mbedtls_ssl_session *session ) |
| 503 | { |
| 504 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 505 | if( session->peer_cert != NULL ) |
| 506 | { |
| 507 | mbedtls_x509_crt_free( session->peer_cert ); |
| 508 | mbedtls_free( session->peer_cert ); |
| 509 | session->peer_cert = NULL; |
| 510 | } |
| 511 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 512 | if( session->peer_cert_digest != NULL ) |
| 513 | { |
| 514 | /* Zeroization is not necessary. */ |
| 515 | mbedtls_free( session->peer_cert_digest ); |
| 516 | session->peer_cert_digest = NULL; |
| 517 | session->peer_cert_digest_type = MBEDTLS_MD_NONE; |
| 518 | session->peer_cert_digest_len = 0; |
| 519 | } |
| 520 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 521 | } |
| 522 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 523 | |
| 524 | void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl, |
| 525 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info ) |
| 526 | { |
| 527 | ((void) ciphersuite_info); |
| 528 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 529 | #if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 530 | if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 ) |
| 531 | ssl->handshake->update_checksum = ssl_update_checksum_sha384; |
| 532 | else |
| 533 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 534 | #if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 535 | if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 ) |
| 536 | ssl->handshake->update_checksum = ssl_update_checksum_sha256; |
| 537 | else |
| 538 | #endif |
| 539 | { |
| 540 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 541 | return; |
| 542 | } |
| 543 | } |
| 544 | |
XiaokangQian | adab9a6 | 2022-07-18 07:41:26 +0000 | [diff] [blame] | 545 | void mbedtls_ssl_add_hs_hdr_to_checksum( mbedtls_ssl_context *ssl, |
| 546 | unsigned hs_type, |
| 547 | size_t total_hs_len ) |
Ronald Cron | 8f6d39a | 2022-03-10 18:56:50 +0100 | [diff] [blame] | 548 | { |
| 549 | unsigned char hs_hdr[4]; |
| 550 | |
| 551 | /* Build HS header for checksum update. */ |
| 552 | hs_hdr[0] = MBEDTLS_BYTE_0( hs_type ); |
| 553 | hs_hdr[1] = MBEDTLS_BYTE_2( total_hs_len ); |
| 554 | hs_hdr[2] = MBEDTLS_BYTE_1( total_hs_len ); |
| 555 | hs_hdr[3] = MBEDTLS_BYTE_0( total_hs_len ); |
| 556 | |
| 557 | ssl->handshake->update_checksum( ssl, hs_hdr, sizeof( hs_hdr ) ); |
| 558 | } |
| 559 | |
| 560 | void mbedtls_ssl_add_hs_msg_to_checksum( mbedtls_ssl_context *ssl, |
| 561 | unsigned hs_type, |
| 562 | unsigned char const *msg, |
| 563 | size_t msg_len ) |
| 564 | { |
| 565 | mbedtls_ssl_add_hs_hdr_to_checksum( ssl, hs_type, msg_len ); |
| 566 | ssl->handshake->update_checksum( ssl, msg, msg_len ); |
| 567 | } |
| 568 | |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 569 | void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl ) |
| 570 | { |
| 571 | ((void) ssl); |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 572 | #if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 573 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 574 | psa_hash_abort( &ssl->handshake->fin_sha256_psa ); |
| 575 | psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 ); |
| 576 | #else |
| 577 | mbedtls_sha256_starts( &ssl->handshake->fin_sha256, 0 ); |
| 578 | #endif |
| 579 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 580 | #if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 581 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 582 | psa_hash_abort( &ssl->handshake->fin_sha384_psa ); |
| 583 | psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 ); |
| 584 | #else |
Andrzej Kurek | a242e83 | 2022-08-11 10:03:14 -0400 | [diff] [blame] | 585 | mbedtls_sha512_starts( &ssl->handshake->fin_sha384, 1 ); |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 586 | #endif |
| 587 | #endif |
| 588 | } |
| 589 | |
| 590 | static void ssl_update_checksum_start( mbedtls_ssl_context *ssl, |
| 591 | const unsigned char *buf, size_t len ) |
| 592 | { |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 593 | #if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 594 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 595 | psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len ); |
| 596 | #else |
| 597 | mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len ); |
| 598 | #endif |
| 599 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 600 | #if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 601 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 602 | psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len ); |
| 603 | #else |
Andrzej Kurek | a242e83 | 2022-08-11 10:03:14 -0400 | [diff] [blame] | 604 | mbedtls_sha512_update( &ssl->handshake->fin_sha384, buf, len ); |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 605 | #endif |
| 606 | #endif |
Andrzej Kurek | eabeb30 | 2022-10-17 07:52:51 -0400 | [diff] [blame] | 607 | #if !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \ |
| 608 | !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
| 609 | (void) ssl; |
| 610 | (void) buf; |
| 611 | (void) len; |
| 612 | #endif |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 613 | } |
| 614 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 615 | #if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 616 | static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl, |
| 617 | const unsigned char *buf, size_t len ) |
| 618 | { |
| 619 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 620 | psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len ); |
| 621 | #else |
| 622 | mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len ); |
| 623 | #endif |
| 624 | } |
| 625 | #endif |
| 626 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 627 | #if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 628 | static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl, |
| 629 | const unsigned char *buf, size_t len ) |
| 630 | { |
| 631 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 632 | psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len ); |
| 633 | #else |
Andrzej Kurek | a242e83 | 2022-08-11 10:03:14 -0400 | [diff] [blame] | 634 | mbedtls_sha512_update( &ssl->handshake->fin_sha384, buf, len ); |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 635 | #endif |
| 636 | } |
| 637 | #endif |
| 638 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 639 | static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake ) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 640 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 641 | memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 642 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 643 | #if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 644 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 645 | handshake->fin_sha256_psa = psa_hash_operation_init(); |
| 646 | psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 ); |
| 647 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 648 | mbedtls_sha256_init( &handshake->fin_sha256 ); |
TRodziewicz | 26371e4 | 2021-06-08 16:45:41 +0200 | [diff] [blame] | 649 | mbedtls_sha256_starts( &handshake->fin_sha256, 0 ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 650 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 651 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 652 | #if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 653 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 654 | handshake->fin_sha384_psa = psa_hash_operation_init(); |
| 655 | psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 656 | #else |
Andrzej Kurek | a242e83 | 2022-08-11 10:03:14 -0400 | [diff] [blame] | 657 | mbedtls_sha512_init( &handshake->fin_sha384 ); |
| 658 | mbedtls_sha512_starts( &handshake->fin_sha384, 1 ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 659 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 660 | #endif |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 661 | |
| 662 | handshake->update_checksum = ssl_update_checksum_start; |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 663 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 664 | #if defined(MBEDTLS_DHM_C) |
| 665 | mbedtls_dhm_init( &handshake->dhm_ctx ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 666 | #endif |
Neil Armstrong | f3f4641 | 2022-04-12 14:43:39 +0200 | [diff] [blame] | 667 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 668 | mbedtls_ecdh_init( &handshake->ecdh_ctx ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 669 | #endif |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 670 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 671 | mbedtls_ecjpake_init( &handshake->ecjpake_ctx ); |
Manuel Pégourié-Gonnard | 77c0646 | 2015-09-17 13:59:49 +0200 | [diff] [blame] | 672 | #if defined(MBEDTLS_SSL_CLI_C) |
| 673 | handshake->ecjpake_cache = NULL; |
| 674 | handshake->ecjpake_cache_len = 0; |
| 675 | #endif |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 676 | #endif |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 677 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 678 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Manuel Pégourié-Gonnard | 6b7301c | 2017-08-15 12:08:45 +0200 | [diff] [blame] | 679 | mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx ); |
Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 680 | #endif |
| 681 | |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 682 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 683 | handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET; |
| 684 | #endif |
Hanno Becker | 7517312 | 2019-02-06 16:18:31 +0000 | [diff] [blame] | 685 | |
| 686 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 687 | !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 688 | mbedtls_pk_init( &handshake->peer_pubkey ); |
| 689 | #endif |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 690 | } |
| 691 | |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 692 | void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform ) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 693 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 694 | memset( transform, 0, sizeof(mbedtls_ssl_transform) ); |
Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 695 | |
Przemyslaw Stekiel | 8f80fb9 | 2022-01-11 08:28:13 +0100 | [diff] [blame] | 696 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 697 | transform->psa_key_enc = MBEDTLS_SVC_KEY_ID_INIT; |
| 698 | transform->psa_key_dec = MBEDTLS_SVC_KEY_ID_INIT; |
Przemyslaw Stekiel | 6be9cf5 | 2022-01-19 16:00:22 +0100 | [diff] [blame] | 699 | #else |
| 700 | mbedtls_cipher_init( &transform->cipher_ctx_enc ); |
| 701 | mbedtls_cipher_init( &transform->cipher_ctx_dec ); |
Przemyslaw Stekiel | 8f80fb9 | 2022-01-11 08:28:13 +0100 | [diff] [blame] | 702 | #endif |
| 703 | |
Hanno Becker | fd86ca8 | 2020-11-30 08:54:23 +0000 | [diff] [blame] | 704 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
Neil Armstrong | 39b8e7d | 2022-02-23 09:24:45 +0100 | [diff] [blame] | 705 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 706 | transform->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT; |
| 707 | transform->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT; |
Neil Armstrong | cf8841a | 2022-02-24 11:17:45 +0100 | [diff] [blame] | 708 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 709 | mbedtls_md_init( &transform->md_ctx_enc ); |
| 710 | mbedtls_md_init( &transform->md_ctx_dec ); |
Hanno Becker | d56ed24 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 711 | #endif |
Neil Armstrong | cf8841a | 2022-02-24 11:17:45 +0100 | [diff] [blame] | 712 | #endif |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 713 | } |
| 714 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 715 | void mbedtls_ssl_session_init( mbedtls_ssl_session *session ) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 716 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 717 | memset( session, 0, sizeof(mbedtls_ssl_session) ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 718 | } |
| 719 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 720 | MBEDTLS_CHECK_RETURN_CRITICAL |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 721 | static int ssl_handshake_init( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 722 | { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 723 | /* Clear old handshake information if present */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 724 | if( ssl->transform_negotiate ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 725 | mbedtls_ssl_transform_free( ssl->transform_negotiate ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 726 | if( ssl->session_negotiate ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 727 | mbedtls_ssl_session_free( ssl->session_negotiate ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 728 | if( ssl->handshake ) |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 729 | mbedtls_ssl_handshake_free( ssl ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 730 | |
| 731 | /* |
| 732 | * Either the pointers are now NULL or cleared properly and can be freed. |
| 733 | * Now allocate missing structures. |
| 734 | */ |
| 735 | if( ssl->transform_negotiate == NULL ) |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 736 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 737 | ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 738 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 739 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 740 | if( ssl->session_negotiate == NULL ) |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 741 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 742 | ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 743 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 744 | |
Paul Bakker | 82788fb | 2014-10-20 13:59:19 +0200 | [diff] [blame] | 745 | if( ssl->handshake == NULL ) |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 746 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 747 | ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 748 | } |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 749 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 750 | /* If the buffers are too small - reallocate */ |
Andrzej Kurek | 8ea6872 | 2020-04-03 06:40:47 -0400 | [diff] [blame] | 751 | |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 752 | handle_buffer_resizing( ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN, |
| 753 | MBEDTLS_SSL_OUT_BUFFER_LEN ); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 754 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 755 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 756 | /* All pointers should exist and can be directly freed without issue */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 757 | if( ssl->handshake == NULL || |
| 758 | ssl->transform_negotiate == NULL || |
| 759 | ssl->session_negotiate == NULL ) |
| 760 | { |
Manuel Pégourié-Gonnard | b2a18a2 | 2015-05-27 16:29:56 +0200 | [diff] [blame] | 761 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 762 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 763 | mbedtls_free( ssl->handshake ); |
| 764 | mbedtls_free( ssl->transform_negotiate ); |
| 765 | mbedtls_free( ssl->session_negotiate ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 766 | |
| 767 | ssl->handshake = NULL; |
| 768 | ssl->transform_negotiate = NULL; |
| 769 | ssl->session_negotiate = NULL; |
| 770 | |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 771 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 772 | } |
| 773 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 774 | /* Initialize structures */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 775 | mbedtls_ssl_session_init( ssl->session_negotiate ); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 776 | mbedtls_ssl_transform_init( ssl->transform_negotiate ); |
Paul Bakker | 968afaa | 2014-07-09 11:09:24 +0200 | [diff] [blame] | 777 | ssl_handshake_params_init( ssl->handshake ); |
| 778 | |
Jerry Yu | d0766ec | 2022-09-22 10:46:57 +0800 | [diff] [blame] | 779 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \ |
| 780 | defined(MBEDTLS_SSL_SRV_C) && \ |
| 781 | defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 782 | ssl->handshake->new_session_tickets_count = |
| 783 | ssl->conf->new_session_tickets_count ; |
| 784 | #endif |
| 785 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 786 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 787 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 788 | { |
| 789 | ssl->handshake->alt_transform_out = ssl->transform_out; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 790 | |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 791 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 792 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING; |
| 793 | else |
| 794 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING; |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 795 | |
Hanno Becker | 0f57a65 | 2020-02-05 10:37:26 +0000 | [diff] [blame] | 796 | mbedtls_ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 797 | } |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 798 | #endif |
| 799 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 800 | /* |
| 801 | * curve_list is translated to IANA TLS group identifiers here because |
| 802 | * mbedtls_ssl_conf_curves returns void and so can't return |
| 803 | * any error codes. |
| 804 | */ |
| 805 | #if defined(MBEDTLS_ECP_C) |
| 806 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 807 | /* Heap allocate and translate curve_list from internal to IANA group ids */ |
| 808 | if ( ssl->conf->curve_list != NULL ) |
| 809 | { |
| 810 | size_t length; |
| 811 | const mbedtls_ecp_group_id *curve_list = ssl->conf->curve_list; |
| 812 | |
| 813 | for( length = 0; ( curve_list[length] != MBEDTLS_ECP_DP_NONE ) && |
| 814 | ( length < MBEDTLS_ECP_DP_MAX ); length++ ) {} |
| 815 | |
| 816 | /* Leave room for zero termination */ |
| 817 | uint16_t *group_list = mbedtls_calloc( length + 1, sizeof(uint16_t) ); |
| 818 | if ( group_list == NULL ) |
| 819 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 820 | |
| 821 | for( size_t i = 0; i < length; i++ ) |
| 822 | { |
| 823 | const mbedtls_ecp_curve_info *info = |
| 824 | mbedtls_ecp_curve_info_from_grp_id( curve_list[i] ); |
| 825 | if ( info == NULL ) |
| 826 | { |
| 827 | mbedtls_free( group_list ); |
| 828 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); |
| 829 | } |
| 830 | group_list[i] = info->tls_id; |
| 831 | } |
| 832 | |
| 833 | group_list[length] = 0; |
| 834 | |
| 835 | ssl->handshake->group_list = group_list; |
| 836 | ssl->handshake->group_list_heap_allocated = 1; |
| 837 | } |
| 838 | else |
| 839 | { |
| 840 | ssl->handshake->group_list = ssl->conf->group_list; |
| 841 | ssl->handshake->group_list_heap_allocated = 0; |
| 842 | } |
| 843 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
| 844 | #endif /* MBEDTLS_ECP_C */ |
| 845 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 846 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 847 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Jerry Yu | a69269a | 2022-01-17 21:06:01 +0800 | [diff] [blame] | 848 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Jerry Yu | 713013f | 2022-01-17 18:16:35 +0800 | [diff] [blame] | 849 | /* Heap allocate and translate sig_hashes from internal hash identifiers to |
| 850 | signature algorithms IANA identifiers. */ |
| 851 | if ( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) && |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 852 | ssl->conf->sig_hashes != NULL ) |
| 853 | { |
| 854 | const int *md; |
| 855 | const int *sig_hashes = ssl->conf->sig_hashes; |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 856 | size_t sig_algs_len = 0; |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 857 | uint16_t *p; |
| 858 | |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 859 | #if defined(static_assert) |
| 860 | static_assert( MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN |
| 861 | <= ( SIZE_MAX - ( 2 * sizeof(uint16_t) ) ), |
| 862 | "MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN too big" ); |
Jerry Yu | a68dca2 | 2022-01-20 16:28:27 +0800 | [diff] [blame] | 863 | #endif |
| 864 | |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 865 | for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ ) |
| 866 | { |
| 867 | if( mbedtls_ssl_hash_from_md_alg( *md ) == MBEDTLS_SSL_HASH_NONE ) |
| 868 | continue; |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 869 | #if defined(MBEDTLS_ECDSA_C) |
| 870 | sig_algs_len += sizeof( uint16_t ); |
| 871 | #endif |
Jerry Yu | a68dca2 | 2022-01-20 16:28:27 +0800 | [diff] [blame] | 872 | |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 873 | #if defined(MBEDTLS_RSA_C) |
| 874 | sig_algs_len += sizeof( uint16_t ); |
| 875 | #endif |
| 876 | if( sig_algs_len > MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN ) |
| 877 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 878 | } |
| 879 | |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 880 | if( sig_algs_len < MBEDTLS_SSL_MIN_SIG_ALG_LIST_LEN ) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 881 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); |
| 882 | |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 883 | ssl->handshake->sig_algs = mbedtls_calloc( 1, sig_algs_len + |
| 884 | sizeof( uint16_t )); |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 885 | if( ssl->handshake->sig_algs == NULL ) |
| 886 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 887 | |
| 888 | p = (uint16_t *)ssl->handshake->sig_algs; |
| 889 | for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ ) |
| 890 | { |
| 891 | unsigned char hash = mbedtls_ssl_hash_from_md_alg( *md ); |
| 892 | if( hash == MBEDTLS_SSL_HASH_NONE ) |
| 893 | continue; |
Jerry Yu | 6106fdc | 2022-01-12 16:36:14 +0800 | [diff] [blame] | 894 | #if defined(MBEDTLS_ECDSA_C) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 895 | *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_ECDSA); |
| 896 | p++; |
Jerry Yu | 6106fdc | 2022-01-12 16:36:14 +0800 | [diff] [blame] | 897 | #endif |
| 898 | #if defined(MBEDTLS_RSA_C) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 899 | *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_RSA); |
| 900 | p++; |
Jerry Yu | 6106fdc | 2022-01-12 16:36:14 +0800 | [diff] [blame] | 901 | #endif |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 902 | } |
Gabor Mezei | 15b95a6 | 2022-05-09 16:37:58 +0200 | [diff] [blame] | 903 | *p = MBEDTLS_TLS_SIG_NONE; |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 904 | ssl->handshake->sig_algs_heap_allocated = 1; |
| 905 | } |
| 906 | else |
Jerry Yu | a69269a | 2022-01-17 21:06:01 +0800 | [diff] [blame] | 907 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 908 | { |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 909 | ssl->handshake->sig_algs_heap_allocated = 0; |
| 910 | } |
Jerry Yu | cc53910 | 2022-06-27 16:27:35 +0800 | [diff] [blame] | 911 | #endif /* !MBEDTLS_DEPRECATED_REMOVED */ |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 912 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 913 | return( 0 ); |
| 914 | } |
| 915 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 916 | #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 917 | /* Dummy cookie callbacks for defaults */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 918 | MBEDTLS_CHECK_RETURN_CRITICAL |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 919 | static int ssl_cookie_write_dummy( void *ctx, |
| 920 | unsigned char **p, unsigned char *end, |
| 921 | const unsigned char *cli_id, size_t cli_id_len ) |
| 922 | { |
| 923 | ((void) ctx); |
| 924 | ((void) p); |
| 925 | ((void) end); |
| 926 | ((void) cli_id); |
| 927 | ((void) cli_id_len); |
| 928 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 929 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 930 | } |
| 931 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 932 | MBEDTLS_CHECK_RETURN_CRITICAL |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 933 | static int ssl_cookie_check_dummy( void *ctx, |
| 934 | const unsigned char *cookie, size_t cookie_len, |
| 935 | const unsigned char *cli_id, size_t cli_id_len ) |
| 936 | { |
| 937 | ((void) ctx); |
| 938 | ((void) cookie); |
| 939 | ((void) cookie_len); |
| 940 | ((void) cli_id); |
| 941 | ((void) cli_id_len); |
| 942 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 943 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 944 | } |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 945 | #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 946 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 947 | /* |
| 948 | * Initialize an SSL context |
| 949 | */ |
Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 950 | void mbedtls_ssl_init( mbedtls_ssl_context *ssl ) |
| 951 | { |
| 952 | memset( ssl, 0, sizeof( mbedtls_ssl_context ) ); |
| 953 | } |
| 954 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 955 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 956 | static int ssl_conf_version_check( const mbedtls_ssl_context *ssl ) |
| 957 | { |
Ronald Cron | 086ee0b | 2022-03-15 15:18:51 +0100 | [diff] [blame] | 958 | const mbedtls_ssl_config *conf = ssl->conf; |
| 959 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 960 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Ronald Cron | 086ee0b | 2022-03-15 15:18:51 +0100 | [diff] [blame] | 961 | if( mbedtls_ssl_conf_is_tls13_only( conf ) ) |
| 962 | { |
XiaokangQian | ed582dd | 2022-04-13 08:21:05 +0000 | [diff] [blame] | 963 | if( conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 964 | { |
| 965 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS 1.3 is not yet supported." ) ); |
| 966 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 967 | } |
| 968 | |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 969 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls13 only." ) ); |
| 970 | return( 0 ); |
| 971 | } |
| 972 | #endif |
| 973 | |
| 974 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Ronald Cron | 086ee0b | 2022-03-15 15:18:51 +0100 | [diff] [blame] | 975 | if( mbedtls_ssl_conf_is_tls12_only( conf ) ) |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 976 | { |
| 977 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls12 only." ) ); |
| 978 | return( 0 ); |
| 979 | } |
| 980 | #endif |
| 981 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 982 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Ronald Cron | 086ee0b | 2022-03-15 15:18:51 +0100 | [diff] [blame] | 983 | if( mbedtls_ssl_conf_is_hybrid_tls12_tls13( conf ) ) |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 984 | { |
XiaokangQian | ed582dd | 2022-04-13 08:21:05 +0000 | [diff] [blame] | 985 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 986 | { |
| 987 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS not yet supported in Hybrid TLS 1.3 + TLS 1.2" ) ); |
| 988 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 989 | } |
| 990 | |
XiaokangQian | 8f9dfe4 | 2022-04-15 02:52:39 +0000 | [diff] [blame] | 991 | if( conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 992 | { |
| 993 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS 1.3 server is not supported yet." ) ); |
| 994 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 995 | } |
| 996 | |
| 997 | |
Ronald Cron | e1d3f06 | 2022-02-10 14:50:54 +0100 | [diff] [blame] | 998 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is TLS 1.3 or TLS 1.2." ) ); |
| 999 | return( 0 ); |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1000 | } |
| 1001 | #endif |
| 1002 | |
| 1003 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "The SSL configuration is invalid." ) ); |
| 1004 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); |
| 1005 | } |
| 1006 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 1007 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1008 | static int ssl_conf_check(const mbedtls_ssl_context *ssl) |
| 1009 | { |
| 1010 | int ret; |
| 1011 | ret = ssl_conf_version_check( ssl ); |
| 1012 | if( ret != 0 ) |
| 1013 | return( ret ); |
| 1014 | |
| 1015 | /* Space for further checks */ |
| 1016 | |
| 1017 | return( 0 ); |
| 1018 | } |
| 1019 | |
Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 1020 | /* |
| 1021 | * Setup an SSL context |
| 1022 | */ |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 1023 | |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 1024 | int mbedtls_ssl_setup( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 1897af9 | 2015-05-10 23:27:38 +0200 | [diff] [blame] | 1025 | const mbedtls_ssl_config *conf ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1026 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1027 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1028 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; |
| 1029 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1030 | |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 1031 | ssl->conf = conf; |
Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 1032 | |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1033 | if( ( ret = ssl_conf_check( ssl ) ) != 0 ) |
| 1034 | return( ret ); |
| 1035 | |
Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 1036 | /* |
Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 1037 | * Prepare base structures |
Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 1038 | */ |
k-stachowiak | c9a5f02 | 2018-07-24 13:53:31 +0200 | [diff] [blame] | 1039 | |
| 1040 | /* Set to NULL in case of an error condition */ |
| 1041 | ssl->out_buf = NULL; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1042 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1043 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 1044 | ssl->in_buf_len = in_buf_len; |
| 1045 | #endif |
| 1046 | ssl->in_buf = mbedtls_calloc( 1, in_buf_len ); |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 1047 | if( ssl->in_buf == NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1048 | { |
Paul Elliott | d48d5c6 | 2021-01-07 14:47:05 +0000 | [diff] [blame] | 1049 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", in_buf_len ) ); |
k-stachowiak | 9f7798e | 2018-07-31 16:52:32 +0200 | [diff] [blame] | 1050 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1051 | goto error; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 1052 | } |
| 1053 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1054 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 1055 | ssl->out_buf_len = out_buf_len; |
| 1056 | #endif |
| 1057 | ssl->out_buf = mbedtls_calloc( 1, out_buf_len ); |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 1058 | if( ssl->out_buf == NULL ) |
| 1059 | { |
Paul Elliott | d48d5c6 | 2021-01-07 14:47:05 +0000 | [diff] [blame] | 1060 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", out_buf_len ) ); |
k-stachowiak | 9f7798e | 2018-07-31 16:52:32 +0200 | [diff] [blame] | 1061 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1062 | goto error; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1063 | } |
| 1064 | |
Hanno Becker | 3e6f8ab | 2020-02-05 10:40:57 +0000 | [diff] [blame] | 1065 | mbedtls_ssl_reset_in_out_pointers( ssl ); |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 1066 | |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 1067 | #if defined(MBEDTLS_SSL_DTLS_SRTP) |
Ron Eldor | 3adb992 | 2017-12-21 10:15:08 +0200 | [diff] [blame] | 1068 | memset( &ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info) ); |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 1069 | #endif |
| 1070 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1071 | if( ( ret = ssl_handshake_init( ssl ) ) != 0 ) |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1072 | goto error; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1073 | |
| 1074 | return( 0 ); |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1075 | |
| 1076 | error: |
| 1077 | mbedtls_free( ssl->in_buf ); |
| 1078 | mbedtls_free( ssl->out_buf ); |
| 1079 | |
| 1080 | ssl->conf = NULL; |
| 1081 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1082 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 1083 | ssl->in_buf_len = 0; |
| 1084 | ssl->out_buf_len = 0; |
| 1085 | #endif |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1086 | ssl->in_buf = NULL; |
| 1087 | ssl->out_buf = NULL; |
| 1088 | |
| 1089 | ssl->in_hdr = NULL; |
| 1090 | ssl->in_ctr = NULL; |
| 1091 | ssl->in_len = NULL; |
| 1092 | ssl->in_iv = NULL; |
| 1093 | ssl->in_msg = NULL; |
| 1094 | |
| 1095 | ssl->out_hdr = NULL; |
| 1096 | ssl->out_ctr = NULL; |
| 1097 | ssl->out_len = NULL; |
| 1098 | ssl->out_iv = NULL; |
| 1099 | ssl->out_msg = NULL; |
| 1100 | |
k-stachowiak | 9f7798e | 2018-07-31 16:52:32 +0200 | [diff] [blame] | 1101 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1102 | } |
| 1103 | |
| 1104 | /* |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 1105 | * Reset an initialized and used SSL context for re-use while retaining |
| 1106 | * all application-set variables, function pointers and data. |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 1107 | * |
| 1108 | * If partial is non-zero, keep data in the input buffer and client ID. |
| 1109 | * (Use when a DTLS client reconnects from the same port.) |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 1110 | */ |
XiaokangQian | 78b1fa7 | 2022-01-19 06:56:30 +0000 | [diff] [blame] | 1111 | void mbedtls_ssl_session_reset_msg_layer( mbedtls_ssl_context *ssl, |
| 1112 | int partial ) |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 1113 | { |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1114 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 1115 | size_t in_buf_len = ssl->in_buf_len; |
| 1116 | size_t out_buf_len = ssl->out_buf_len; |
| 1117 | #else |
| 1118 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; |
| 1119 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; |
| 1120 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1121 | |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1122 | #if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || !defined(MBEDTLS_SSL_SRV_C) |
| 1123 | partial = 0; |
Hanno Becker | 7e77213 | 2018-08-10 12:38:21 +0100 | [diff] [blame] | 1124 | #endif |
| 1125 | |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 1126 | /* Cancel any possibly running timer */ |
Hanno Becker | 0f57a65 | 2020-02-05 10:37:26 +0000 | [diff] [blame] | 1127 | mbedtls_ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 1128 | |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1129 | mbedtls_ssl_reset_in_out_pointers( ssl ); |
| 1130 | |
| 1131 | /* Reset incoming message parsing */ |
| 1132 | ssl->in_offt = NULL; |
| 1133 | ssl->nb_zero = 0; |
| 1134 | ssl->in_msgtype = 0; |
| 1135 | ssl->in_msglen = 0; |
| 1136 | ssl->in_hslen = 0; |
| 1137 | ssl->keep_current_message = 0; |
| 1138 | ssl->transform_in = NULL; |
| 1139 | |
| 1140 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 1141 | ssl->next_record_offset = 0; |
| 1142 | ssl->in_epoch = 0; |
| 1143 | #endif |
| 1144 | |
| 1145 | /* Keep current datagram if partial == 1 */ |
| 1146 | if( partial == 0 ) |
| 1147 | { |
| 1148 | ssl->in_left = 0; |
| 1149 | memset( ssl->in_buf, 0, in_buf_len ); |
| 1150 | } |
| 1151 | |
Ronald Cron | ad8c17b | 2022-06-10 17:18:09 +0200 | [diff] [blame] | 1152 | ssl->send_alert = 0; |
| 1153 | |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1154 | /* Reset outgoing message writing */ |
| 1155 | ssl->out_msgtype = 0; |
| 1156 | ssl->out_msglen = 0; |
| 1157 | ssl->out_left = 0; |
| 1158 | memset( ssl->out_buf, 0, out_buf_len ); |
| 1159 | memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) ); |
| 1160 | ssl->transform_out = NULL; |
| 1161 | |
| 1162 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 1163 | mbedtls_ssl_dtls_replay_reset( ssl ); |
| 1164 | #endif |
| 1165 | |
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1166 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1167 | if( ssl->transform ) |
| 1168 | { |
| 1169 | mbedtls_ssl_transform_free( ssl->transform ); |
| 1170 | mbedtls_free( ssl->transform ); |
| 1171 | ssl->transform = NULL; |
| 1172 | } |
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1173 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 1174 | |
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1175 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 1176 | mbedtls_ssl_transform_free( ssl->transform_application ); |
| 1177 | mbedtls_free( ssl->transform_application ); |
| 1178 | ssl->transform_application = NULL; |
| 1179 | |
| 1180 | if( ssl->handshake != NULL ) |
| 1181 | { |
| 1182 | mbedtls_ssl_transform_free( ssl->handshake->transform_earlydata ); |
| 1183 | mbedtls_free( ssl->handshake->transform_earlydata ); |
| 1184 | ssl->handshake->transform_earlydata = NULL; |
| 1185 | |
| 1186 | mbedtls_ssl_transform_free( ssl->handshake->transform_handshake ); |
| 1187 | mbedtls_free( ssl->handshake->transform_handshake ); |
| 1188 | ssl->handshake->transform_handshake = NULL; |
| 1189 | } |
| 1190 | |
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1191 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1192 | } |
| 1193 | |
| 1194 | int mbedtls_ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial ) |
| 1195 | { |
| 1196 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 1197 | |
| 1198 | ssl->state = MBEDTLS_SSL_HELLO_REQUEST; |
| 1199 | |
XiaokangQian | 78b1fa7 | 2022-01-19 06:56:30 +0000 | [diff] [blame] | 1200 | mbedtls_ssl_session_reset_msg_layer( ssl, partial ); |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1201 | |
| 1202 | /* Reset renegotiation state */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1203 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 1204 | ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE; |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 1205 | ssl->renego_records_seen = 0; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1206 | |
| 1207 | ssl->verify_data_len = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1208 | memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN ); |
| 1209 | memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN ); |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 1210 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1211 | ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1212 | |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1213 | ssl->session_in = NULL; |
Hanno Becker | 7864090 | 2018-08-13 16:35:15 +0100 | [diff] [blame] | 1214 | ssl->session_out = NULL; |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 1215 | if( ssl->session ) |
| 1216 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1217 | mbedtls_ssl_session_free( ssl->session ); |
| 1218 | mbedtls_free( ssl->session ); |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 1219 | ssl->session = NULL; |
| 1220 | } |
| 1221 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1222 | #if defined(MBEDTLS_SSL_ALPN) |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 1223 | ssl->alpn_chosen = NULL; |
| 1224 | #endif |
| 1225 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 1226 | #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C) |
Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 1227 | #if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 1228 | if( partial == 0 ) |
Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 1229 | #endif |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 1230 | { |
| 1231 | mbedtls_free( ssl->cli_id ); |
| 1232 | ssl->cli_id = NULL; |
| 1233 | ssl->cli_id_len = 0; |
| 1234 | } |
Manuel Pégourié-Gonnard | 43c0218 | 2014-07-22 17:32:01 +0200 | [diff] [blame] | 1235 | #endif |
| 1236 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1237 | if( ( ret = ssl_handshake_init( ssl ) ) != 0 ) |
| 1238 | return( ret ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1239 | |
| 1240 | return( 0 ); |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 1241 | } |
| 1242 | |
Manuel Pégourié-Gonnard | 779e429 | 2013-08-03 13:50:48 +0200 | [diff] [blame] | 1243 | /* |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 1244 | * Reset an initialized and used SSL context for re-use while retaining |
| 1245 | * all application-set variables, function pointers and data. |
| 1246 | */ |
| 1247 | int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl ) |
| 1248 | { |
Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 1249 | return( mbedtls_ssl_session_reset_int( ssl, 0 ) ); |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 1250 | } |
| 1251 | |
| 1252 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1253 | * SSL set accessors |
| 1254 | */ |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1255 | void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1256 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1257 | conf->endpoint = endpoint; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1258 | } |
| 1259 | |
Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 1260 | 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] | 1261 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1262 | conf->transport = transport; |
Manuel Pégourié-Gonnard | 0b1ff29 | 2014-02-06 13:04:16 +0100 | [diff] [blame] | 1263 | } |
| 1264 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1265 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1266 | 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] | 1267 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1268 | conf->anti_replay = mode; |
Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 1269 | } |
| 1270 | #endif |
| 1271 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1272 | 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] | 1273 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1274 | conf->badmac_limit = limit; |
Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 1275 | } |
Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 1276 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1277 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 1278 | |
Hanno Becker | 1841b0a | 2018-08-24 11:13:57 +0100 | [diff] [blame] | 1279 | void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl, |
| 1280 | unsigned allow_packing ) |
Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 1281 | { |
| 1282 | ssl->disable_datagram_packing = !allow_packing; |
| 1283 | } |
| 1284 | |
| 1285 | void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf, |
| 1286 | uint32_t min, uint32_t max ) |
Manuel Pégourié-Gonnard | 905dd24 | 2014-10-01 12:03:55 +0200 | [diff] [blame] | 1287 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1288 | conf->hs_timeout_min = min; |
| 1289 | conf->hs_timeout_max = max; |
Manuel Pégourié-Gonnard | 905dd24 | 2014-10-01 12:03:55 +0200 | [diff] [blame] | 1290 | } |
| 1291 | #endif |
| 1292 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1293 | void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1294 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1295 | conf->authmode = authmode; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1296 | } |
| 1297 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1298 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1299 | void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 1300 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 1301 | void *p_vrfy ) |
| 1302 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1303 | conf->f_vrfy = f_vrfy; |
| 1304 | conf->p_vrfy = p_vrfy; |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 1305 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1306 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 1307 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1308 | void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf, |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 1309 | int (*f_rng)(void *, unsigned char *, size_t), |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1310 | void *p_rng ) |
| 1311 | { |
Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 1312 | conf->f_rng = f_rng; |
| 1313 | conf->p_rng = p_rng; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1314 | } |
| 1315 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1316 | void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | fd47423 | 2015-06-23 16:34:24 +0200 | [diff] [blame] | 1317 | void (*f_dbg)(void *, int, const char *, int, const char *), |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1318 | void *p_dbg ) |
| 1319 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1320 | conf->f_dbg = f_dbg; |
| 1321 | conf->p_dbg = p_dbg; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1322 | } |
| 1323 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1324 | void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 1325 | void *p_bio, |
Simon Butcher | e846b51 | 2016-03-01 17:31:49 +0000 | [diff] [blame] | 1326 | mbedtls_ssl_send_t *f_send, |
| 1327 | mbedtls_ssl_recv_t *f_recv, |
| 1328 | mbedtls_ssl_recv_timeout_t *f_recv_timeout ) |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 1329 | { |
| 1330 | ssl->p_bio = p_bio; |
| 1331 | ssl->f_send = f_send; |
| 1332 | ssl->f_recv = f_recv; |
| 1333 | ssl->f_recv_timeout = f_recv_timeout; |
Manuel Pégourié-Gonnard | 97fd52c | 2015-05-06 15:38:52 +0100 | [diff] [blame] | 1334 | } |
| 1335 | |
Manuel Pégourié-Gonnard | 6e7aaca | 2018-08-20 10:37:23 +0200 | [diff] [blame] | 1336 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 1337 | void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu ) |
| 1338 | { |
| 1339 | ssl->mtu = mtu; |
| 1340 | } |
| 1341 | #endif |
| 1342 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1343 | 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] | 1344 | { |
| 1345 | conf->read_timeout = timeout; |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 1346 | } |
| 1347 | |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 1348 | void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl, |
| 1349 | void *p_timer, |
Simon Butcher | e846b51 | 2016-03-01 17:31:49 +0000 | [diff] [blame] | 1350 | mbedtls_ssl_set_timer_t *f_set_timer, |
| 1351 | mbedtls_ssl_get_timer_t *f_get_timer ) |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 1352 | { |
| 1353 | ssl->p_timer = p_timer; |
| 1354 | ssl->f_set_timer = f_set_timer; |
| 1355 | ssl->f_get_timer = f_get_timer; |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 1356 | |
| 1357 | /* Make sure we start with no timer running */ |
Hanno Becker | 0f57a65 | 2020-02-05 10:37:26 +0000 | [diff] [blame] | 1358 | mbedtls_ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 1359 | } |
| 1360 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1361 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1362 | void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf, |
Hanno Becker | a637ff6 | 2021-04-15 08:42:48 +0100 | [diff] [blame] | 1363 | void *p_cache, |
| 1364 | mbedtls_ssl_cache_get_t *f_get_cache, |
| 1365 | mbedtls_ssl_cache_set_t *f_set_cache ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1366 | { |
Manuel Pégourié-Gonnard | 5cb3308 | 2015-05-06 18:06:26 +0100 | [diff] [blame] | 1367 | conf->p_cache = p_cache; |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1368 | conf->f_get_cache = f_get_cache; |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1369 | conf->f_set_cache = f_set_cache; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1370 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1371 | #endif /* MBEDTLS_SSL_SRV_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1372 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1373 | #if defined(MBEDTLS_SSL_CLI_C) |
| 1374 | 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] | 1375 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1376 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1377 | |
| 1378 | if( ssl == NULL || |
| 1379 | session == NULL || |
| 1380 | ssl->session_negotiate == NULL || |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1381 | ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT ) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1382 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1383 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1384 | } |
| 1385 | |
Hanno Becker | e810bbc | 2021-05-14 16:01:05 +0100 | [diff] [blame] | 1386 | if( ssl->handshake->resume == 1 ) |
| 1387 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 1388 | |
Jerry Yu | 2109206 | 2022-10-10 21:21:31 +0800 | [diff] [blame] | 1389 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 1390 | if( session->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 ) |
Jerry Yu | 40afab6 | 2022-10-08 10:42:13 +0800 | [diff] [blame] | 1391 | { |
Jerry Yu | 2109206 | 2022-10-10 21:21:31 +0800 | [diff] [blame] | 1392 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 1393 | mbedtls_ssl_ciphersuite_from_id( session->ciphersuite ); |
| 1394 | |
| 1395 | if( mbedtls_ssl_validate_ciphersuite( |
| 1396 | ssl, ciphersuite_info, MBEDTLS_SSL_VERSION_TLS1_3, |
| 1397 | MBEDTLS_SSL_VERSION_TLS1_3 ) != 0 ) |
| 1398 | { |
| 1399 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "%d is not a valid TLS 1.3 ciphersuite.", |
| 1400 | session->ciphersuite ) ); |
| 1401 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1402 | } |
Jerry Yu | 40afab6 | 2022-10-08 10:42:13 +0800 | [diff] [blame] | 1403 | } |
Jerry Yu | 2109206 | 2022-10-10 21:21:31 +0800 | [diff] [blame] | 1404 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Jerry Yu | 40afab6 | 2022-10-08 10:42:13 +0800 | [diff] [blame] | 1405 | |
Hanno Becker | 52055ae | 2019-02-06 14:30:46 +0000 | [diff] [blame] | 1406 | if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate, |
| 1407 | session ) ) != 0 ) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1408 | return( ret ); |
| 1409 | |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 1410 | ssl->handshake->resume = 1; |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1411 | |
| 1412 | return( 0 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1413 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1414 | #endif /* MBEDTLS_SSL_CLI_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1415 | |
Mateusz Starzyk | 06b07fb | 2021-02-18 13:55:21 +0100 | [diff] [blame] | 1416 | void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf, |
| 1417 | const int *ciphersuites ) |
| 1418 | { |
Hanno Becker | d60b6c6 | 2021-04-29 12:04:11 +0100 | [diff] [blame] | 1419 | conf->ciphersuite_list = ciphersuites; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1420 | } |
| 1421 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 1422 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | cadebe5 | 2021-08-24 10:36:45 +0800 | [diff] [blame] | 1423 | void mbedtls_ssl_conf_tls13_key_exchange_modes( mbedtls_ssl_config *conf, |
Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 1424 | const int kex_modes ) |
| 1425 | { |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 1426 | 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] | 1427 | } |
Xiaokang Qian | 72de95d | 2022-10-25 02:54:33 +0000 | [diff] [blame^] | 1428 | |
| 1429 | #if defined(MBEDTLS_SSL_EARLY_DATA) |
| 1430 | void mbedtls_ssl_tls13_conf_early_data( mbedtls_ssl_config *conf, |
| 1431 | int early_data_enabled ) |
| 1432 | { |
| 1433 | conf->early_data_enabled = early_data_enabled; |
| 1434 | } |
| 1435 | #endif /* MBEDTLS_SSL_EARLY_DATA */ |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 1436 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 1437 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1438 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 6e3ee3a | 2015-06-17 10:58:20 +0200 | [diff] [blame] | 1439 | void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf, |
Nicholas Wilson | 2088e2e | 2015-09-08 16:53:18 +0100 | [diff] [blame] | 1440 | const mbedtls_x509_crt_profile *profile ) |
Manuel Pégourié-Gonnard | 6e3ee3a | 2015-06-17 10:58:20 +0200 | [diff] [blame] | 1441 | { |
| 1442 | conf->cert_profile = profile; |
| 1443 | } |
| 1444 | |
Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 1445 | static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert ) |
| 1446 | { |
| 1447 | mbedtls_ssl_key_cert *cur = key_cert, *next; |
| 1448 | |
| 1449 | while( cur != NULL ) |
| 1450 | { |
| 1451 | next = cur->next; |
| 1452 | mbedtls_free( cur ); |
| 1453 | cur = next; |
| 1454 | } |
| 1455 | } |
| 1456 | |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1457 | /* Append a new keycert entry to a (possibly empty) list */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 1458 | MBEDTLS_CHECK_RETURN_CRITICAL |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1459 | static int ssl_append_key_cert( mbedtls_ssl_key_cert **head, |
| 1460 | mbedtls_x509_crt *cert, |
| 1461 | mbedtls_pk_context *key ) |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1462 | { |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1463 | mbedtls_ssl_key_cert *new_cert; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1464 | |
Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 1465 | if( cert == NULL ) |
| 1466 | { |
| 1467 | /* Free list if cert is null */ |
| 1468 | ssl_key_cert_free( *head ); |
| 1469 | *head = NULL; |
| 1470 | return( 0 ); |
| 1471 | } |
| 1472 | |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1473 | new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) ); |
| 1474 | if( new_cert == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 1475 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1476 | |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1477 | new_cert->cert = cert; |
| 1478 | new_cert->key = key; |
| 1479 | new_cert->next = NULL; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1480 | |
Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 1481 | /* 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] | 1482 | if( *head == NULL ) |
Paul Bakker | 0333b97 | 2013-11-04 17:08:28 +0100 | [diff] [blame] | 1483 | { |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1484 | *head = new_cert; |
Paul Bakker | 0333b97 | 2013-11-04 17:08:28 +0100 | [diff] [blame] | 1485 | } |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1486 | else |
| 1487 | { |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1488 | mbedtls_ssl_key_cert *cur = *head; |
| 1489 | while( cur->next != NULL ) |
| 1490 | cur = cur->next; |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1491 | cur->next = new_cert; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1492 | } |
| 1493 | |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1494 | return( 0 ); |
| 1495 | } |
| 1496 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1497 | int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1498 | mbedtls_x509_crt *own_cert, |
| 1499 | mbedtls_pk_context *pk_key ) |
| 1500 | { |
Manuel Pégourié-Gonnard | 17a40cd | 2015-05-10 23:17:17 +0200 | [diff] [blame] | 1501 | 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] | 1502 | } |
| 1503 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1504 | void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 1505 | mbedtls_x509_crt *ca_chain, |
| 1506 | mbedtls_x509_crl *ca_crl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1507 | { |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 1508 | conf->ca_chain = ca_chain; |
| 1509 | conf->ca_crl = ca_crl; |
Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 1510 | |
| 1511 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 1512 | /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb() |
| 1513 | * cannot be used together. */ |
| 1514 | conf->f_ca_cb = NULL; |
| 1515 | conf->p_ca_cb = NULL; |
| 1516 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1517 | } |
Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 1518 | |
| 1519 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 1520 | void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf, |
Hanno Becker | afd0b0a | 2019-03-27 16:55:01 +0000 | [diff] [blame] | 1521 | mbedtls_x509_crt_ca_cb_t f_ca_cb, |
Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 1522 | void *p_ca_cb ) |
| 1523 | { |
| 1524 | conf->f_ca_cb = f_ca_cb; |
| 1525 | conf->p_ca_cb = p_ca_cb; |
| 1526 | |
| 1527 | /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb() |
| 1528 | * cannot be used together. */ |
| 1529 | conf->ca_chain = NULL; |
| 1530 | conf->ca_crl = NULL; |
| 1531 | } |
| 1532 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1533 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | eb2c658 | 2012-09-27 19:15:01 +0000 | [diff] [blame] | 1534 | |
Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 1535 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Glenn Strauss | 6989407 | 2022-01-24 12:58:00 -0500 | [diff] [blame] | 1536 | const unsigned char *mbedtls_ssl_get_hs_sni( mbedtls_ssl_context *ssl, |
| 1537 | size_t *name_len ) |
| 1538 | { |
| 1539 | *name_len = ssl->handshake->sni_name_len; |
| 1540 | return( ssl->handshake->sni_name ); |
| 1541 | } |
| 1542 | |
Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 1543 | int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl, |
| 1544 | mbedtls_x509_crt *own_cert, |
| 1545 | mbedtls_pk_context *pk_key ) |
| 1546 | { |
| 1547 | return( ssl_append_key_cert( &ssl->handshake->sni_key_cert, |
| 1548 | own_cert, pk_key ) ); |
| 1549 | } |
Manuel Pégourié-Gonnard | 22bfa4b | 2015-05-11 08:46:37 +0200 | [diff] [blame] | 1550 | |
| 1551 | void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl, |
| 1552 | mbedtls_x509_crt *ca_chain, |
| 1553 | mbedtls_x509_crl *ca_crl ) |
| 1554 | { |
| 1555 | ssl->handshake->sni_ca_chain = ca_chain; |
| 1556 | ssl->handshake->sni_ca_crl = ca_crl; |
| 1557 | } |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 1558 | |
Glenn Strauss | 999ef70 | 2022-03-11 01:37:23 -0500 | [diff] [blame] | 1559 | #if defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED) |
| 1560 | void mbedtls_ssl_set_hs_dn_hints( mbedtls_ssl_context *ssl, |
| 1561 | const mbedtls_x509_crt *crt) |
| 1562 | { |
| 1563 | ssl->handshake->dn_hints = crt; |
| 1564 | } |
| 1565 | #endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */ |
| 1566 | |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 1567 | void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl, |
| 1568 | int authmode ) |
| 1569 | { |
| 1570 | ssl->handshake->sni_authmode = authmode; |
| 1571 | } |
Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 1572 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
| 1573 | |
Hanno Becker | 8927c83 | 2019-04-03 12:52:50 +0100 | [diff] [blame] | 1574 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 1575 | void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl, |
| 1576 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), |
| 1577 | void *p_vrfy ) |
| 1578 | { |
| 1579 | ssl->f_vrfy = f_vrfy; |
| 1580 | ssl->p_vrfy = p_vrfy; |
| 1581 | } |
| 1582 | #endif |
| 1583 | |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 1584 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 1585 | /* |
| 1586 | * Set EC J-PAKE password for current handshake |
| 1587 | */ |
| 1588 | int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl, |
| 1589 | const unsigned char *pw, |
| 1590 | size_t pw_len ) |
| 1591 | { |
| 1592 | mbedtls_ecjpake_role role; |
| 1593 | |
Janos Follath | 8eb6413 | 2016-06-03 15:40:57 +0100 | [diff] [blame] | 1594 | if( ssl->handshake == NULL || ssl->conf == NULL ) |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 1595 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1596 | |
| 1597 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 1598 | role = MBEDTLS_ECJPAKE_SERVER; |
| 1599 | else |
| 1600 | role = MBEDTLS_ECJPAKE_CLIENT; |
| 1601 | |
| 1602 | return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx, |
| 1603 | role, |
| 1604 | MBEDTLS_MD_SHA256, |
| 1605 | MBEDTLS_ECP_DP_SECP256R1, |
| 1606 | pw, pw_len ) ); |
| 1607 | } |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 1608 | #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 1609 | |
Ronald Cron | 73fe8df | 2022-10-05 14:31:43 +0200 | [diff] [blame] | 1610 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED) |
Ronald Cron | d29e13e | 2022-10-19 10:33:48 +0200 | [diff] [blame] | 1611 | int mbedtls_ssl_conf_has_static_psk( mbedtls_ssl_config const *conf ) |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 1612 | { |
Ronald Cron | d29e13e | 2022-10-19 10:33:48 +0200 | [diff] [blame] | 1613 | if( conf->psk_identity == NULL || |
| 1614 | conf->psk_identity_len == 0 ) |
| 1615 | { |
| 1616 | return( 0 ); |
| 1617 | } |
| 1618 | |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 1619 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Ronald Cron | d29e13e | 2022-10-19 10:33:48 +0200 | [diff] [blame] | 1620 | if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) ) |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 1621 | return( 1 ); |
| 1622 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Ronald Cron | d29e13e | 2022-10-19 10:33:48 +0200 | [diff] [blame] | 1623 | |
| 1624 | if( conf->psk != NULL && conf->psk_len != 0 ) |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 1625 | return( 1 ); |
| 1626 | |
| 1627 | return( 0 ); |
| 1628 | } |
| 1629 | |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1630 | static void ssl_conf_remove_psk( mbedtls_ssl_config *conf ) |
| 1631 | { |
| 1632 | /* Remove reference to existing PSK, if any. */ |
| 1633 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1634 | if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) ) |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1635 | { |
| 1636 | /* The maintenance of the PSK key slot is the |
| 1637 | * user's responsibility. */ |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1638 | conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT; |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1639 | } |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1640 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 1641 | if( conf->psk != NULL ) |
| 1642 | { |
| 1643 | mbedtls_platform_zeroize( conf->psk, conf->psk_len ); |
| 1644 | |
| 1645 | mbedtls_free( conf->psk ); |
| 1646 | conf->psk = NULL; |
| 1647 | conf->psk_len = 0; |
| 1648 | } |
| 1649 | |
| 1650 | /* Remove reference to PSK identity, if any. */ |
| 1651 | if( conf->psk_identity != NULL ) |
| 1652 | { |
| 1653 | mbedtls_free( conf->psk_identity ); |
| 1654 | conf->psk_identity = NULL; |
| 1655 | conf->psk_identity_len = 0; |
| 1656 | } |
| 1657 | } |
| 1658 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1659 | /* This function assumes that PSK identity in the SSL config is unset. |
| 1660 | * It checks that the provided identity is well-formed and attempts |
| 1661 | * to make a copy of it in the SSL config. |
| 1662 | * On failure, the PSK identity in the config remains unset. */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 1663 | MBEDTLS_CHECK_RETURN_CRITICAL |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1664 | static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf, |
| 1665 | unsigned char const *psk_identity, |
| 1666 | size_t psk_identity_len ) |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 1667 | { |
Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 1668 | /* Identity len will be encoded on two bytes */ |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1669 | if( psk_identity == NULL || |
Ronald Cron | 2a87e9b | 2022-10-19 10:55:26 +0200 | [diff] [blame] | 1670 | psk_identity_len == 0 || |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1671 | ( psk_identity_len >> 16 ) != 0 || |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 1672 | psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN ) |
Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 1673 | { |
| 1674 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1675 | } |
| 1676 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1677 | conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ); |
| 1678 | if( conf->psk_identity == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 1679 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 1680 | |
Manuel Pégourié-Gonnard | 120fdbd | 2015-05-07 17:07:50 +0100 | [diff] [blame] | 1681 | conf->psk_identity_len = psk_identity_len; |
Manuel Pégourié-Gonnard | 120fdbd | 2015-05-07 17:07:50 +0100 | [diff] [blame] | 1682 | memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len ); |
Paul Bakker | 5ad403f | 2013-09-18 21:21:30 +0200 | [diff] [blame] | 1683 | |
| 1684 | return( 0 ); |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 1685 | } |
| 1686 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1687 | int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf, |
| 1688 | const unsigned char *psk, size_t psk_len, |
| 1689 | const unsigned char *psk_identity, size_t psk_identity_len ) |
| 1690 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1691 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 1692 | |
| 1693 | /* We currently only support one PSK, raw or opaque. */ |
Ronald Cron | d29e13e | 2022-10-19 10:33:48 +0200 | [diff] [blame] | 1694 | if( mbedtls_ssl_conf_has_static_psk( conf ) ) |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 1695 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1696 | |
| 1697 | /* Check and set raw PSK */ |
Piotr Nowicki | 9926eaf | 2019-11-20 14:54:36 +0100 | [diff] [blame] | 1698 | if( psk == NULL ) |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1699 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Piotr Nowicki | 9926eaf | 2019-11-20 14:54:36 +0100 | [diff] [blame] | 1700 | if( psk_len == 0 ) |
| 1701 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1702 | if( psk_len > MBEDTLS_PSK_MAX_LEN ) |
| 1703 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1704 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1705 | if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ) |
| 1706 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 1707 | conf->psk_len = psk_len; |
| 1708 | memcpy( conf->psk, psk, conf->psk_len ); |
| 1709 | |
| 1710 | /* Check and set PSK Identity */ |
| 1711 | ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len ); |
| 1712 | if( ret != 0 ) |
| 1713 | ssl_conf_remove_psk( conf ); |
| 1714 | |
| 1715 | return( ret ); |
| 1716 | } |
| 1717 | |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1718 | static void ssl_remove_psk( mbedtls_ssl_context *ssl ) |
| 1719 | { |
| 1720 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1721 | if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) ) |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1722 | { |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 1723 | /* The maintenance of the external PSK key slot is the |
| 1724 | * user's responsibility. */ |
| 1725 | if( ssl->handshake->psk_opaque_is_internal ) |
| 1726 | { |
| 1727 | psa_destroy_key( ssl->handshake->psk_opaque ); |
| 1728 | ssl->handshake->psk_opaque_is_internal = 0; |
| 1729 | } |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1730 | ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT; |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1731 | } |
Neil Armstrong | e952a30 | 2022-05-03 10:22:14 +0200 | [diff] [blame] | 1732 | #else |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1733 | if( ssl->handshake->psk != NULL ) |
| 1734 | { |
| 1735 | mbedtls_platform_zeroize( ssl->handshake->psk, |
| 1736 | ssl->handshake->psk_len ); |
| 1737 | mbedtls_free( ssl->handshake->psk ); |
| 1738 | ssl->handshake->psk_len = 0; |
| 1739 | } |
Neil Armstrong | e952a30 | 2022-05-03 10:22:14 +0200 | [diff] [blame] | 1740 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1741 | } |
| 1742 | |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1743 | int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl, |
| 1744 | const unsigned char *psk, size_t psk_len ) |
| 1745 | { |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 1746 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1747 | psa_key_attributes_t key_attributes = psa_key_attributes_init(); |
Jerry Yu | 5d01c05 | 2022-08-17 10:18:10 +0800 | [diff] [blame] | 1748 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Jerry Yu | 24b8c81 | 2022-08-20 19:06:56 +0800 | [diff] [blame] | 1749 | psa_algorithm_t alg = PSA_ALG_NONE; |
Jerry Yu | 5d01c05 | 2022-08-17 10:18:10 +0800 | [diff] [blame] | 1750 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 1751 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 1752 | |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1753 | if( psk == NULL || ssl->handshake == NULL ) |
| 1754 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1755 | |
| 1756 | if( psk_len > MBEDTLS_PSK_MAX_LEN ) |
| 1757 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1758 | |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1759 | ssl_remove_psk( ssl ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1760 | |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 1761 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Jerry Yu | ccc68a4 | 2022-07-26 16:39:20 +0800 | [diff] [blame] | 1762 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 1763 | if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 ) |
| 1764 | { |
Jerry Yu | 6cf6b47 | 2022-08-16 14:50:28 +0800 | [diff] [blame] | 1765 | if( ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384 ) |
Jerry Yu | ccc68a4 | 2022-07-26 16:39:20 +0800 | [diff] [blame] | 1766 | alg = PSA_ALG_TLS12_PSK_TO_MS( PSA_ALG_SHA_384 ); |
| 1767 | else |
| 1768 | alg = PSA_ALG_TLS12_PSK_TO_MS( PSA_ALG_SHA_256 ); |
| 1769 | psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE ); |
| 1770 | } |
| 1771 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 1772 | |
Jerry Yu | 568ec25 | 2022-07-22 21:27:34 +0800 | [diff] [blame] | 1773 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | ccc68a4 | 2022-07-26 16:39:20 +0800 | [diff] [blame] | 1774 | if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 ) |
| 1775 | { |
| 1776 | alg = PSA_ALG_HKDF_EXTRACT( PSA_ALG_ANY_HASH ); |
| 1777 | psa_set_key_usage_flags( &key_attributes, |
| 1778 | PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT ); |
| 1779 | } |
| 1780 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 1781 | |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 1782 | psa_set_key_algorithm( &key_attributes, alg ); |
| 1783 | psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE ); |
| 1784 | |
| 1785 | status = psa_import_key( &key_attributes, psk, psk_len, &key ); |
| 1786 | if( status != PSA_SUCCESS ) |
| 1787 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 1788 | |
| 1789 | /* Allow calling psa_destroy_key() on psk remove */ |
| 1790 | ssl->handshake->psk_opaque_is_internal = 1; |
| 1791 | return mbedtls_ssl_set_hs_psk_opaque( ssl, key ); |
| 1792 | #else |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 1793 | if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 1794 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1795 | |
| 1796 | ssl->handshake->psk_len = psk_len; |
| 1797 | memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len ); |
| 1798 | |
| 1799 | return( 0 ); |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 1800 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1801 | } |
| 1802 | |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1803 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1804 | int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf, |
Andrzej Kurek | 03e0146 | 2022-01-03 12:53:24 +0100 | [diff] [blame] | 1805 | mbedtls_svc_key_id_t psk, |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1806 | const unsigned char *psk_identity, |
| 1807 | size_t psk_identity_len ) |
| 1808 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1809 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 1810 | |
| 1811 | /* We currently only support one PSK, raw or opaque. */ |
Ronald Cron | d29e13e | 2022-10-19 10:33:48 +0200 | [diff] [blame] | 1812 | if( mbedtls_ssl_conf_has_static_psk( conf ) ) |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 1813 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1814 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1815 | /* Check and set opaque PSK */ |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1816 | if( mbedtls_svc_key_id_is_null( psk ) ) |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1817 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1818 | conf->psk_opaque = psk; |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1819 | |
| 1820 | /* Check and set PSK Identity */ |
| 1821 | ret = ssl_conf_set_psk_identity( conf, psk_identity, |
| 1822 | psk_identity_len ); |
| 1823 | if( ret != 0 ) |
| 1824 | ssl_conf_remove_psk( conf ); |
| 1825 | |
| 1826 | return( ret ); |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1827 | } |
| 1828 | |
Przemyslaw Stekiel | 6928a51 | 2022-02-03 13:50:35 +0100 | [diff] [blame] | 1829 | int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl, |
| 1830 | mbedtls_svc_key_id_t psk ) |
| 1831 | { |
| 1832 | if( ( mbedtls_svc_key_id_is_null( psk ) ) || |
| 1833 | ( ssl->handshake == NULL ) ) |
| 1834 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1835 | |
| 1836 | ssl_remove_psk( ssl ); |
| 1837 | ssl->handshake->psk_opaque = psk; |
| 1838 | return( 0 ); |
| 1839 | } |
| 1840 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 1841 | |
Jerry Yu | 8897c07 | 2022-08-12 13:56:53 +0800 | [diff] [blame] | 1842 | #if defined(MBEDTLS_SSL_SRV_C) |
Przemyslaw Stekiel | 6928a51 | 2022-02-03 13:50:35 +0100 | [diff] [blame] | 1843 | void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf, |
| 1844 | int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *, |
| 1845 | size_t), |
| 1846 | void *p_psk ) |
| 1847 | { |
| 1848 | conf->f_psk = f_psk; |
| 1849 | conf->p_psk = p_psk; |
| 1850 | } |
Jerry Yu | 8897c07 | 2022-08-12 13:56:53 +0800 | [diff] [blame] | 1851 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 1852 | |
Ronald Cron | 73fe8df | 2022-10-05 14:31:43 +0200 | [diff] [blame] | 1853 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */ |
Przemyslaw Stekiel | 6928a51 | 2022-02-03 13:50:35 +0100 | [diff] [blame] | 1854 | |
| 1855 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 301711e | 2022-04-26 16:57:05 +0200 | [diff] [blame] | 1856 | static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode( |
| 1857 | psa_algorithm_t alg ) |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 1858 | { |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 1859 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1860 | if( alg == PSA_ALG_CBC_NO_PADDING ) |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 1861 | return( MBEDTLS_SSL_MODE_CBC ); |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 1862 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1863 | if( PSA_ALG_IS_AEAD( alg ) ) |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 1864 | return( MBEDTLS_SSL_MODE_AEAD ); |
Gilles Peskine | 301711e | 2022-04-26 16:57:05 +0200 | [diff] [blame] | 1865 | return( MBEDTLS_SSL_MODE_STREAM ); |
| 1866 | } |
| 1867 | |
| 1868 | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
| 1869 | |
| 1870 | static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode( |
| 1871 | mbedtls_cipher_mode_t mode ) |
| 1872 | { |
| 1873 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
| 1874 | if( mode == MBEDTLS_MODE_CBC ) |
| 1875 | return( MBEDTLS_SSL_MODE_CBC ); |
| 1876 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
| 1877 | |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 1878 | #if defined(MBEDTLS_GCM_C) || \ |
| 1879 | defined(MBEDTLS_CCM_C) || \ |
| 1880 | defined(MBEDTLS_CHACHAPOLY_C) |
| 1881 | if( mode == MBEDTLS_MODE_GCM || |
| 1882 | mode == MBEDTLS_MODE_CCM || |
| 1883 | mode == MBEDTLS_MODE_CHACHAPOLY ) |
| 1884 | return( MBEDTLS_SSL_MODE_AEAD ); |
| 1885 | #endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */ |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 1886 | |
| 1887 | return( MBEDTLS_SSL_MODE_STREAM ); |
| 1888 | } |
Gilles Peskine | 301711e | 2022-04-26 16:57:05 +0200 | [diff] [blame] | 1889 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 1890 | |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 1891 | static mbedtls_ssl_mode_t mbedtls_ssl_get_actual_mode( |
| 1892 | mbedtls_ssl_mode_t base_mode, |
| 1893 | int encrypt_then_mac ) |
| 1894 | { |
| 1895 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
| 1896 | if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED && |
| 1897 | base_mode == MBEDTLS_SSL_MODE_CBC ) |
| 1898 | { |
| 1899 | return( MBEDTLS_SSL_MODE_CBC_ETM ); |
| 1900 | } |
| 1901 | #else |
| 1902 | (void) encrypt_then_mac; |
| 1903 | #endif |
| 1904 | return( base_mode ); |
| 1905 | } |
| 1906 | |
Neil Armstrong | ab555e0 | 2022-04-04 11:07:59 +0200 | [diff] [blame] | 1907 | mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_transform( |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1908 | const mbedtls_ssl_transform *transform ) |
| 1909 | { |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 1910 | mbedtls_ssl_mode_t base_mode = mbedtls_ssl_get_base_mode( |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1911 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 1912 | transform->psa_alg |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1913 | #else |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 1914 | mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) |
| 1915 | #endif |
| 1916 | ); |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1917 | |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 1918 | int encrypt_then_mac = 0; |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 1919 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 1920 | encrypt_then_mac = transform->encrypt_then_mac; |
| 1921 | #endif |
| 1922 | return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) ); |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1923 | } |
| 1924 | |
Neil Armstrong | ab555e0 | 2022-04-04 11:07:59 +0200 | [diff] [blame] | 1925 | mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_ciphersuite( |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 1926 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1927 | int encrypt_then_mac, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 1928 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1929 | const mbedtls_ssl_ciphersuite_t *suite ) |
| 1930 | { |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 1931 | mbedtls_ssl_mode_t base_mode = MBEDTLS_SSL_MODE_STREAM; |
| 1932 | |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1933 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1934 | psa_status_t status; |
| 1935 | psa_algorithm_t alg; |
| 1936 | psa_key_type_t type; |
| 1937 | size_t size; |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1938 | status = mbedtls_ssl_cipher_to_psa( suite->cipher, 0, &alg, &type, &size ); |
| 1939 | if( status == PSA_SUCCESS ) |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 1940 | base_mode = mbedtls_ssl_get_base_mode( alg ); |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1941 | #else |
| 1942 | const mbedtls_cipher_info_t *cipher = |
| 1943 | mbedtls_cipher_info_from_type( suite->cipher ); |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1944 | if( cipher != NULL ) |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 1945 | { |
| 1946 | base_mode = |
| 1947 | mbedtls_ssl_get_base_mode( |
| 1948 | mbedtls_cipher_info_get_mode( cipher ) ); |
| 1949 | } |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1950 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 1951 | |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 1952 | #if !defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
| 1953 | int encrypt_then_mac = 0; |
| 1954 | #endif |
| 1955 | return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) ); |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1956 | } |
| 1957 | |
Neil Armstrong | 8395d7a | 2022-05-18 11:44:56 +0200 | [diff] [blame] | 1958 | #if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 1959 | |
| 1960 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 1961 | /* Serialization of TLS 1.3 sessions: |
| 1962 | * |
| 1963 | * struct { |
Xiaokang Qian | 126bf8e | 2022-10-13 02:22:40 +0000 | [diff] [blame] | 1964 | * opaque hostname<0..2^16-1>; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 1965 | * uint64 ticket_received; |
| 1966 | * uint32 ticket_lifetime; |
Jerry Yu | 3419107 | 2022-08-18 10:32:09 +0800 | [diff] [blame] | 1967 | * opaque ticket<1..2^16-1>; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 1968 | * } ClientOnlyData; |
| 1969 | * |
| 1970 | * struct { |
| 1971 | * uint8 endpoint; |
| 1972 | * uint8 ciphersuite[2]; |
| 1973 | * uint32 ticket_age_add; |
| 1974 | * uint8 ticket_flags; |
| 1975 | * opaque resumption_key<0..255>; |
| 1976 | * select ( endpoint ) { |
| 1977 | * case client: ClientOnlyData; |
| 1978 | * case server: uint64 start_time; |
| 1979 | * }; |
| 1980 | * } serialized_session_tls13; |
| 1981 | * |
| 1982 | */ |
| 1983 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 1984 | MBEDTLS_CHECK_RETURN_CRITICAL |
| 1985 | static int ssl_tls13_session_save( const mbedtls_ssl_session *session, |
| 1986 | unsigned char *buf, |
| 1987 | size_t buf_len, |
| 1988 | size_t *olen ) |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 1989 | { |
| 1990 | unsigned char *p = buf; |
Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 1991 | #if defined(MBEDTLS_SSL_CLI_C) && \ |
| 1992 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 1993 | size_t hostname_len = ( session->hostname == NULL ) ? |
Xiaokang Qian | ed0620c | 2022-10-12 06:58:13 +0000 | [diff] [blame] | 1994 | 0 : strlen( session->hostname ) + 1; |
Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 1995 | #endif |
Jerry Yu | bc7c1a4 | 2022-07-21 22:57:37 +0800 | [diff] [blame] | 1996 | size_t needed = 1 /* endpoint */ |
| 1997 | + 2 /* ciphersuite */ |
| 1998 | + 4 /* ticket_age_add */ |
Jerry Yu | 3419107 | 2022-08-18 10:32:09 +0800 | [diff] [blame] | 1999 | + 1 /* ticket_flags */ |
| 2000 | + 1; /* resumption_key length */ |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 2001 | *olen = 0; |
Jerry Yu | 3419107 | 2022-08-18 10:32:09 +0800 | [diff] [blame] | 2002 | |
| 2003 | if( session->resumption_key_len > MBEDTLS_SSL_TLS1_3_TICKET_RESUMPTION_KEY_LEN ) |
| 2004 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2005 | needed += session->resumption_key_len; /* resumption_key */ |
| 2006 | |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2007 | #if defined(MBEDTLS_HAVE_TIME) |
| 2008 | needed += 8; /* start_time or ticket_received */ |
| 2009 | #endif |
| 2010 | |
| 2011 | #if defined(MBEDTLS_SSL_CLI_C) |
| 2012 | if( session->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 2013 | { |
Xiaokang Qian | 126bf8e | 2022-10-13 02:22:40 +0000 | [diff] [blame] | 2014 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Xiaokang Qian | bc663a0 | 2022-10-09 11:14:39 +0000 | [diff] [blame] | 2015 | needed += 2 /* hostname_len */ |
Xiaokang Qian | 2f9efd3 | 2022-10-10 11:24:08 +0000 | [diff] [blame] | 2016 | + hostname_len; /* hostname */ |
Xiaokang Qian | 281fd1b | 2022-09-20 11:35:41 +0000 | [diff] [blame] | 2017 | #endif |
| 2018 | |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2019 | needed += 4 /* ticket_lifetime */ |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 2020 | + 2; /* ticket_len */ |
Jerry Yu | 3419107 | 2022-08-18 10:32:09 +0800 | [diff] [blame] | 2021 | |
| 2022 | /* Check size_t overflow */ |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 2023 | if( session->ticket_len > SIZE_MAX - needed ) |
| 2024 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jerry Yu | 3419107 | 2022-08-18 10:32:09 +0800 | [diff] [blame] | 2025 | |
Jerry Yu | e28d974 | 2022-08-18 15:44:03 +0800 | [diff] [blame] | 2026 | needed += session->ticket_len; /* ticket */ |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2027 | } |
| 2028 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 2029 | |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 2030 | *olen = needed; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2031 | if( needed > buf_len ) |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 2032 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2033 | |
| 2034 | p[0] = session->endpoint; |
| 2035 | MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 1 ); |
| 2036 | MBEDTLS_PUT_UINT32_BE( session->ticket_age_add, p, 3 ); |
| 2037 | p[7] = session->ticket_flags; |
| 2038 | |
| 2039 | /* save resumption_key */ |
Jerry Yu | bc7c1a4 | 2022-07-21 22:57:37 +0800 | [diff] [blame] | 2040 | p[8] = session->resumption_key_len; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2041 | p += 9; |
Jerry Yu | bc7c1a4 | 2022-07-21 22:57:37 +0800 | [diff] [blame] | 2042 | memcpy( p, session->resumption_key, session->resumption_key_len ); |
| 2043 | p += session->resumption_key_len; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2044 | |
| 2045 | #if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C) |
| 2046 | if( session->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 2047 | { |
| 2048 | MBEDTLS_PUT_UINT64_BE( (uint64_t) session->start, p, 0 ); |
| 2049 | p += 8; |
| 2050 | } |
| 2051 | #endif /* MBEDTLS_HAVE_TIME */ |
| 2052 | |
| 2053 | #if defined(MBEDTLS_SSL_CLI_C) |
| 2054 | if( session->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 2055 | { |
Xiaokang Qian | ed0620c | 2022-10-12 06:58:13 +0000 | [diff] [blame] | 2056 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 2057 | MBEDTLS_PUT_UINT16_BE( hostname_len, p, 0 ); |
| 2058 | p += 2; |
Xiaokang Qian | 126bf8e | 2022-10-13 02:22:40 +0000 | [diff] [blame] | 2059 | if( hostname_len > 0 ) |
Xiaokang Qian | ed0620c | 2022-10-12 06:58:13 +0000 | [diff] [blame] | 2060 | { |
| 2061 | /* save host name */ |
| 2062 | memcpy( p, session->hostname, hostname_len ); |
| 2063 | p += hostname_len; |
| 2064 | } |
| 2065 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
| 2066 | |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2067 | #if defined(MBEDTLS_HAVE_TIME) |
| 2068 | MBEDTLS_PUT_UINT64_BE( (uint64_t) session->ticket_received, p, 0 ); |
| 2069 | p += 8; |
| 2070 | #endif |
| 2071 | MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 ); |
| 2072 | p += 4; |
| 2073 | |
| 2074 | MBEDTLS_PUT_UINT16_BE( session->ticket_len, p, 0 ); |
| 2075 | p += 2; |
Jerry Yu | 3419107 | 2022-08-18 10:32:09 +0800 | [diff] [blame] | 2076 | |
| 2077 | if( session->ticket != NULL && session->ticket_len > 0 ) |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2078 | { |
| 2079 | memcpy( p, session->ticket, session->ticket_len ); |
| 2080 | p += session->ticket_len; |
| 2081 | } |
| 2082 | } |
| 2083 | #endif /* MBEDTLS_SSL_CLI_C */ |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 2084 | return( 0 ); |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2085 | } |
| 2086 | |
| 2087 | MBEDTLS_CHECK_RETURN_CRITICAL |
| 2088 | static int ssl_tls13_session_load( mbedtls_ssl_session *session, |
| 2089 | const unsigned char *buf, |
| 2090 | size_t len ) |
| 2091 | { |
| 2092 | const unsigned char *p = buf; |
| 2093 | const unsigned char *end = buf + len; |
| 2094 | |
| 2095 | if( end - p < 9 ) |
| 2096 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2097 | session->endpoint = p[0]; |
| 2098 | session->ciphersuite = MBEDTLS_GET_UINT16_BE( p, 1 ); |
| 2099 | session->ticket_age_add = MBEDTLS_GET_UINT32_BE( p, 3 ); |
| 2100 | session->ticket_flags = p[7]; |
| 2101 | |
| 2102 | /* load resumption_key */ |
Jerry Yu | bc7c1a4 | 2022-07-21 22:57:37 +0800 | [diff] [blame] | 2103 | session->resumption_key_len = p[8]; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2104 | p += 9; |
| 2105 | |
Jerry Yu | bc7c1a4 | 2022-07-21 22:57:37 +0800 | [diff] [blame] | 2106 | if( end - p < session->resumption_key_len ) |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2107 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2108 | |
Jerry Yu | bc7c1a4 | 2022-07-21 22:57:37 +0800 | [diff] [blame] | 2109 | if( sizeof( session->resumption_key ) < session->resumption_key_len ) |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2110 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jerry Yu | bc7c1a4 | 2022-07-21 22:57:37 +0800 | [diff] [blame] | 2111 | memcpy( session->resumption_key, p, session->resumption_key_len ); |
| 2112 | p += session->resumption_key_len; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2113 | |
| 2114 | #if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C) |
| 2115 | if( session->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 2116 | { |
| 2117 | if( end - p < 8 ) |
| 2118 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2119 | session->start = MBEDTLS_GET_UINT64_BE( p, 0 ); |
| 2120 | p += 8; |
| 2121 | } |
| 2122 | #endif /* MBEDTLS_HAVE_TIME */ |
| 2123 | |
| 2124 | #if defined(MBEDTLS_SSL_CLI_C) |
| 2125 | if( session->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 2126 | { |
Xiaokang Qian | ed0620c | 2022-10-12 06:58:13 +0000 | [diff] [blame] | 2127 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \ |
| 2128 | defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 2129 | size_t hostname_len; |
| 2130 | /* load host name */ |
| 2131 | if( end - p < 2 ) |
| 2132 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2133 | hostname_len = MBEDTLS_GET_UINT16_BE( p, 0 ); |
| 2134 | p += 2; |
| 2135 | |
| 2136 | if( end - p < ( long int )hostname_len ) |
| 2137 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2138 | if( hostname_len > 0 ) |
| 2139 | { |
| 2140 | session->hostname = mbedtls_calloc( 1, hostname_len ); |
| 2141 | if( session->hostname == NULL ) |
| 2142 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 2143 | memcpy( session->hostname, p, hostname_len ); |
| 2144 | p += hostname_len; |
| 2145 | } |
| 2146 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION && |
| 2147 | MBEDTLS_SSL_SESSION_TICKETS */ |
| 2148 | |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2149 | #if defined(MBEDTLS_HAVE_TIME) |
| 2150 | if( end - p < 8 ) |
| 2151 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2152 | session->ticket_received = MBEDTLS_GET_UINT64_BE( p, 0 ); |
| 2153 | p += 8; |
| 2154 | #endif |
| 2155 | if( end - p < 4 ) |
| 2156 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2157 | session->ticket_lifetime = MBEDTLS_GET_UINT32_BE( p, 0 ); |
| 2158 | p += 4; |
| 2159 | |
| 2160 | if( end - p < 2 ) |
| 2161 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2162 | session->ticket_len = MBEDTLS_GET_UINT16_BE( p, 0 ); |
| 2163 | p += 2; |
| 2164 | |
| 2165 | if( end - p < ( long int )session->ticket_len ) |
| 2166 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2167 | if( session->ticket_len > 0 ) |
| 2168 | { |
| 2169 | session->ticket = mbedtls_calloc( 1, session->ticket_len ); |
| 2170 | if( session->ticket == NULL ) |
| 2171 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 2172 | memcpy( session->ticket, p, session->ticket_len ); |
| 2173 | p += session->ticket_len; |
| 2174 | } |
| 2175 | } |
| 2176 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 2177 | |
| 2178 | return( 0 ); |
| 2179 | |
| 2180 | } |
| 2181 | #else /* MBEDTLS_SSL_SESSION_TICKETS */ |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 2182 | MBEDTLS_CHECK_RETURN_CRITICAL |
| 2183 | static int ssl_tls13_session_save( const mbedtls_ssl_session *session, |
| 2184 | unsigned char *buf, |
| 2185 | size_t buf_len, |
| 2186 | size_t *olen ) |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 2187 | { |
| 2188 | ((void) session); |
| 2189 | ((void) buf); |
| 2190 | ((void) buf_len); |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 2191 | *olen = 0; |
| 2192 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 2193 | } |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2194 | |
| 2195 | static int ssl_tls13_session_load( const mbedtls_ssl_session *session, |
| 2196 | unsigned char *buf, |
| 2197 | size_t buf_len ) |
| 2198 | { |
| 2199 | ((void) session); |
| 2200 | ((void) buf); |
| 2201 | ((void) buf_len); |
| 2202 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 2203 | } |
| 2204 | #endif /* !MBEDTLS_SSL_SESSION_TICKETS */ |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 2205 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 2206 | |
Przemyslaw Stekiel | f57b456 | 2022-01-25 00:04:18 +0100 | [diff] [blame] | 2207 | 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] | 2208 | size_t taglen, |
| 2209 | psa_algorithm_t *alg, |
| 2210 | psa_key_type_t *key_type, |
| 2211 | size_t *key_size ) |
| 2212 | { |
| 2213 | switch ( mbedtls_cipher_type ) |
| 2214 | { |
| 2215 | case MBEDTLS_CIPHER_AES_128_CBC: |
| 2216 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 2217 | *key_type = PSA_KEY_TYPE_AES; |
| 2218 | *key_size = 128; |
| 2219 | break; |
| 2220 | case MBEDTLS_CIPHER_AES_128_CCM: |
| 2221 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 2222 | *key_type = PSA_KEY_TYPE_AES; |
| 2223 | *key_size = 128; |
| 2224 | break; |
| 2225 | case MBEDTLS_CIPHER_AES_128_GCM: |
| 2226 | *alg = PSA_ALG_GCM; |
| 2227 | *key_type = PSA_KEY_TYPE_AES; |
| 2228 | *key_size = 128; |
| 2229 | break; |
| 2230 | case MBEDTLS_CIPHER_AES_192_CCM: |
| 2231 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 2232 | *key_type = PSA_KEY_TYPE_AES; |
| 2233 | *key_size = 192; |
| 2234 | break; |
| 2235 | case MBEDTLS_CIPHER_AES_192_GCM: |
| 2236 | *alg = PSA_ALG_GCM; |
| 2237 | *key_type = PSA_KEY_TYPE_AES; |
| 2238 | *key_size = 192; |
| 2239 | break; |
| 2240 | case MBEDTLS_CIPHER_AES_256_CBC: |
| 2241 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 2242 | *key_type = PSA_KEY_TYPE_AES; |
| 2243 | *key_size = 256; |
| 2244 | break; |
| 2245 | case MBEDTLS_CIPHER_AES_256_CCM: |
| 2246 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 2247 | *key_type = PSA_KEY_TYPE_AES; |
| 2248 | *key_size = 256; |
| 2249 | break; |
| 2250 | case MBEDTLS_CIPHER_AES_256_GCM: |
| 2251 | *alg = PSA_ALG_GCM; |
| 2252 | *key_type = PSA_KEY_TYPE_AES; |
| 2253 | *key_size = 256; |
| 2254 | break; |
| 2255 | case MBEDTLS_CIPHER_ARIA_128_CBC: |
| 2256 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 2257 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2258 | *key_size = 128; |
| 2259 | break; |
| 2260 | case MBEDTLS_CIPHER_ARIA_128_CCM: |
| 2261 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 2262 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2263 | *key_size = 128; |
| 2264 | break; |
| 2265 | case MBEDTLS_CIPHER_ARIA_128_GCM: |
| 2266 | *alg = PSA_ALG_GCM; |
| 2267 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2268 | *key_size = 128; |
| 2269 | break; |
| 2270 | case MBEDTLS_CIPHER_ARIA_192_CCM: |
| 2271 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 2272 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2273 | *key_size = 192; |
| 2274 | break; |
| 2275 | case MBEDTLS_CIPHER_ARIA_192_GCM: |
| 2276 | *alg = PSA_ALG_GCM; |
| 2277 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2278 | *key_size = 192; |
| 2279 | break; |
| 2280 | case MBEDTLS_CIPHER_ARIA_256_CBC: |
| 2281 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 2282 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2283 | *key_size = 256; |
| 2284 | break; |
| 2285 | case MBEDTLS_CIPHER_ARIA_256_CCM: |
| 2286 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 2287 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2288 | *key_size = 256; |
| 2289 | break; |
| 2290 | case MBEDTLS_CIPHER_ARIA_256_GCM: |
| 2291 | *alg = PSA_ALG_GCM; |
| 2292 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2293 | *key_size = 256; |
| 2294 | break; |
| 2295 | case MBEDTLS_CIPHER_CAMELLIA_128_CBC: |
| 2296 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 2297 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2298 | *key_size = 128; |
| 2299 | break; |
| 2300 | case MBEDTLS_CIPHER_CAMELLIA_128_CCM: |
| 2301 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 2302 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2303 | *key_size = 128; |
| 2304 | break; |
| 2305 | case MBEDTLS_CIPHER_CAMELLIA_128_GCM: |
| 2306 | *alg = PSA_ALG_GCM; |
| 2307 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2308 | *key_size = 128; |
| 2309 | break; |
| 2310 | case MBEDTLS_CIPHER_CAMELLIA_192_CCM: |
| 2311 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 2312 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2313 | *key_size = 192; |
| 2314 | break; |
| 2315 | case MBEDTLS_CIPHER_CAMELLIA_192_GCM: |
| 2316 | *alg = PSA_ALG_GCM; |
| 2317 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2318 | *key_size = 192; |
| 2319 | break; |
| 2320 | case MBEDTLS_CIPHER_CAMELLIA_256_CBC: |
| 2321 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 2322 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2323 | *key_size = 256; |
| 2324 | break; |
| 2325 | case MBEDTLS_CIPHER_CAMELLIA_256_CCM: |
| 2326 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 2327 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2328 | *key_size = 256; |
| 2329 | break; |
| 2330 | case MBEDTLS_CIPHER_CAMELLIA_256_GCM: |
| 2331 | *alg = PSA_ALG_GCM; |
| 2332 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2333 | *key_size = 256; |
| 2334 | break; |
| 2335 | case MBEDTLS_CIPHER_CHACHA20_POLY1305: |
| 2336 | *alg = PSA_ALG_CHACHA20_POLY1305; |
| 2337 | *key_type = PSA_KEY_TYPE_CHACHA20; |
| 2338 | *key_size = 256; |
| 2339 | break; |
| 2340 | case MBEDTLS_CIPHER_NULL: |
| 2341 | *alg = MBEDTLS_SSL_NULL_CIPHER; |
| 2342 | *key_type = 0; |
| 2343 | *key_size = 0; |
| 2344 | break; |
| 2345 | default: |
| 2346 | return PSA_ERROR_NOT_SUPPORTED; |
| 2347 | } |
| 2348 | |
| 2349 | return PSA_SUCCESS; |
| 2350 | } |
Neil Armstrong | 8395d7a | 2022-05-18 11:44:56 +0200 | [diff] [blame] | 2351 | #endif /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2352 | |
Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 2353 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 2354 | int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf, |
| 2355 | const unsigned char *dhm_P, size_t P_len, |
| 2356 | const unsigned char *dhm_G, size_t G_len ) |
| 2357 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2358 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 2359 | |
Glenn Strauss | cee1129 | 2021-12-20 01:43:17 -0500 | [diff] [blame] | 2360 | mbedtls_mpi_free( &conf->dhm_P ); |
| 2361 | mbedtls_mpi_free( &conf->dhm_G ); |
| 2362 | |
Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 2363 | if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 || |
| 2364 | ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 ) |
| 2365 | { |
| 2366 | mbedtls_mpi_free( &conf->dhm_P ); |
| 2367 | mbedtls_mpi_free( &conf->dhm_G ); |
| 2368 | return( ret ); |
| 2369 | } |
| 2370 | |
| 2371 | return( 0 ); |
| 2372 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2373 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2374 | 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] | 2375 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2376 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 2377 | |
Glenn Strauss | cee1129 | 2021-12-20 01:43:17 -0500 | [diff] [blame] | 2378 | mbedtls_mpi_free( &conf->dhm_P ); |
| 2379 | mbedtls_mpi_free( &conf->dhm_G ); |
| 2380 | |
Gilles Peskine | e570248 | 2021-06-11 21:59:08 +0200 | [diff] [blame] | 2381 | if( ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_P, |
| 2382 | &conf->dhm_P ) ) != 0 || |
| 2383 | ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_G, |
| 2384 | &conf->dhm_G ) ) != 0 ) |
Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 2385 | { |
| 2386 | mbedtls_mpi_free( &conf->dhm_P ); |
| 2387 | mbedtls_mpi_free( &conf->dhm_G ); |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 2388 | return( ret ); |
Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 2389 | } |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 2390 | |
| 2391 | return( 0 ); |
| 2392 | } |
Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 2393 | #endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */ |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 2394 | |
Manuel Pégourié-Gonnard | bd990d6 | 2015-06-11 14:49:42 +0200 | [diff] [blame] | 2395 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C) |
| 2396 | /* |
| 2397 | * Set the minimum length for Diffie-Hellman parameters |
| 2398 | */ |
| 2399 | void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf, |
| 2400 | unsigned int bitlen ) |
| 2401 | { |
| 2402 | conf->dhm_min_bitlen = bitlen; |
| 2403 | } |
| 2404 | #endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */ |
| 2405 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 2406 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Jerry Yu | 7ddc38c | 2022-01-19 11:08:05 +0800 | [diff] [blame] | 2407 | #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] | 2408 | /* |
| 2409 | * Set allowed/preferred hashes for handshake signatures |
| 2410 | */ |
| 2411 | void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf, |
| 2412 | const int *hashes ) |
| 2413 | { |
| 2414 | conf->sig_hashes = hashes; |
| 2415 | } |
Jerry Yu | 7ddc38c | 2022-01-19 11:08:05 +0800 | [diff] [blame] | 2416 | #endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */ |
Hanno Becker | 1cd6e00 | 2021-08-10 13:27:10 +0100 | [diff] [blame] | 2417 | |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 2418 | /* Configure allowed signature algorithms for handshake */ |
Hanno Becker | 1cd6e00 | 2021-08-10 13:27:10 +0100 | [diff] [blame] | 2419 | void mbedtls_ssl_conf_sig_algs( mbedtls_ssl_config *conf, |
| 2420 | const uint16_t* sig_algs ) |
| 2421 | { |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 2422 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 2423 | conf->sig_hashes = NULL; |
| 2424 | #endif /* !MBEDTLS_DEPRECATED_REMOVED */ |
| 2425 | conf->sig_algs = sig_algs; |
Hanno Becker | 1cd6e00 | 2021-08-10 13:27:10 +0100 | [diff] [blame] | 2426 | } |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 2427 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Manuel Pégourié-Gonnard | 36a8b57 | 2015-06-17 12:43:26 +0200 | [diff] [blame] | 2428 | |
Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 2429 | #if defined(MBEDTLS_ECP_C) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 2430 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 2431 | /* |
| 2432 | * Set the allowed elliptic curves |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 2433 | * |
| 2434 | * mbedtls_ssl_setup() takes the provided list |
| 2435 | * and translates it to a list of IANA TLS group identifiers, |
| 2436 | * stored in ssl->handshake->group_list. |
| 2437 | * |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 2438 | */ |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2439 | void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2440 | const mbedtls_ecp_group_id *curve_list ) |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 2441 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2442 | conf->curve_list = curve_list; |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 2443 | conf->group_list = NULL; |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 2444 | } |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 2445 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2446 | #endif /* MBEDTLS_ECP_C */ |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 2447 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 2448 | /* |
| 2449 | * Set the allowed groups |
| 2450 | */ |
| 2451 | void mbedtls_ssl_conf_groups( mbedtls_ssl_config *conf, |
| 2452 | const uint16_t *group_list ) |
| 2453 | { |
| 2454 | #if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 2455 | conf->curve_list = NULL; |
| 2456 | #endif |
| 2457 | conf->group_list = group_list; |
| 2458 | } |
| 2459 | |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 2460 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2461 | int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2462 | { |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2463 | /* Initialize to suppress unnecessary compiler warning */ |
| 2464 | size_t hostname_len = 0; |
| 2465 | |
| 2466 | /* Check if new hostname is valid before |
| 2467 | * making any change to current one */ |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2468 | if( hostname != NULL ) |
| 2469 | { |
| 2470 | hostname_len = strlen( hostname ); |
| 2471 | |
| 2472 | if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN ) |
| 2473 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2474 | } |
| 2475 | |
| 2476 | /* Now it's clear that we will overwrite the old hostname, |
| 2477 | * so we can free it safely */ |
| 2478 | |
| 2479 | if( ssl->hostname != NULL ) |
| 2480 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 2481 | mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) ); |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2482 | mbedtls_free( ssl->hostname ); |
| 2483 | } |
| 2484 | |
| 2485 | /* Passing NULL as hostname shall clear the old one */ |
Manuel Pégourié-Gonnard | ba26c24 | 2015-05-06 10:47:06 +0100 | [diff] [blame] | 2486 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2487 | if( hostname == NULL ) |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2488 | { |
| 2489 | ssl->hostname = NULL; |
| 2490 | } |
| 2491 | else |
| 2492 | { |
| 2493 | ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 ); |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2494 | if( ssl->hostname == NULL ) |
| 2495 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Paul Bakker | 75c1a6f | 2013-08-19 14:25:29 +0200 | [diff] [blame] | 2496 | |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2497 | memcpy( ssl->hostname, hostname, hostname_len ); |
Paul Bakker | 75c1a6f | 2013-08-19 14:25:29 +0200 | [diff] [blame] | 2498 | |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2499 | ssl->hostname[hostname_len] = '\0'; |
| 2500 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2501 | |
| 2502 | return( 0 ); |
| 2503 | } |
Hanno Becker | 1a9a51c | 2017-04-07 13:02:16 +0100 | [diff] [blame] | 2504 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2505 | |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 2506 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2507 | void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2508 | int (*f_sni)(void *, mbedtls_ssl_context *, |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 2509 | const unsigned char *, size_t), |
| 2510 | void *p_sni ) |
| 2511 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2512 | conf->f_sni = f_sni; |
| 2513 | conf->p_sni = p_sni; |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 2514 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2515 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 2516 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2517 | #if defined(MBEDTLS_SSL_ALPN) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2518 | 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] | 2519 | { |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 2520 | size_t cur_len, tot_len; |
| 2521 | const char **p; |
| 2522 | |
| 2523 | /* |
Brian J Murray | 1903fb3 | 2016-11-06 04:45:15 -0800 | [diff] [blame] | 2524 | * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings |
| 2525 | * MUST NOT be truncated." |
| 2526 | * We check lengths now rather than later. |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 2527 | */ |
| 2528 | tot_len = 0; |
| 2529 | for( p = protos; *p != NULL; p++ ) |
| 2530 | { |
| 2531 | cur_len = strlen( *p ); |
| 2532 | tot_len += cur_len; |
| 2533 | |
Ronald Cron | 8216dd3 | 2020-04-23 16:41:44 +0200 | [diff] [blame] | 2534 | if( ( cur_len == 0 ) || |
| 2535 | ( cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN ) || |
| 2536 | ( tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN ) ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2537 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 2538 | } |
| 2539 | |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2540 | conf->alpn_list = protos; |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 2541 | |
| 2542 | return( 0 ); |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 2543 | } |
| 2544 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2545 | 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] | 2546 | { |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 2547 | return( ssl->alpn_chosen ); |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 2548 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2549 | #endif /* MBEDTLS_SSL_ALPN */ |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 2550 | |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2551 | #if defined(MBEDTLS_SSL_DTLS_SRTP) |
Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 2552 | void mbedtls_ssl_conf_srtp_mki_value_supported( mbedtls_ssl_config *conf, |
| 2553 | int support_mki_value ) |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 2554 | { |
| 2555 | conf->dtls_srtp_mki_support = support_mki_value; |
| 2556 | } |
| 2557 | |
Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 2558 | int mbedtls_ssl_dtls_srtp_set_mki_value( mbedtls_ssl_context *ssl, |
| 2559 | unsigned char *mki_value, |
Johan Pascal | f6417ec | 2020-09-22 15:15:19 +0200 | [diff] [blame] | 2560 | uint16_t mki_len ) |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 2561 | { |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 2562 | if( mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH ) |
Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 2563 | { |
Johan Pascal | d576fdb | 2020-09-22 10:39:53 +0200 | [diff] [blame] | 2564 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 2565 | } |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 2566 | |
| 2567 | 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] | 2568 | { |
Johan Pascal | d576fdb | 2020-09-22 10:39:53 +0200 | [diff] [blame] | 2569 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 2570 | } |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 2571 | |
| 2572 | memcpy( ssl->dtls_srtp_info.mki_value, mki_value, mki_len ); |
| 2573 | ssl->dtls_srtp_info.mki_len = mki_len; |
Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 2574 | return( 0 ); |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 2575 | } |
| 2576 | |
Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 2577 | int mbedtls_ssl_conf_dtls_srtp_protection_profiles( mbedtls_ssl_config *conf, |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 2578 | const mbedtls_ssl_srtp_profile *profiles ) |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2579 | { |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 2580 | const mbedtls_ssl_srtp_profile *p; |
| 2581 | size_t list_size = 0; |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2582 | |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 2583 | /* check the profiles list: all entry must be valid, |
| 2584 | * 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] | 2585 | for( p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET && |
| 2586 | list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH; |
| 2587 | p++ ) |
Johan Pascal | d576fdb | 2020-09-22 10:39:53 +0200 | [diff] [blame] | 2588 | { |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 2589 | if( mbedtls_ssl_check_srtp_profile_value( *p ) != MBEDTLS_TLS_SRTP_UNSET ) |
Johan Pascal | d576fdb | 2020-09-22 10:39:53 +0200 | [diff] [blame] | 2590 | { |
Johan Pascal | 76fdf1d | 2020-10-22 23:31:00 +0200 | [diff] [blame] | 2591 | list_size++; |
| 2592 | } |
| 2593 | else |
| 2594 | { |
| 2595 | /* unsupported value, stop parsing and set the size to an error value */ |
| 2596 | list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1; |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2597 | } |
| 2598 | } |
| 2599 | |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 2600 | if( list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH ) |
Johan Pascal | d387aa0 | 2020-09-23 18:47:56 +0200 | [diff] [blame] | 2601 | { |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 2602 | conf->dtls_srtp_profile_list = NULL; |
| 2603 | conf->dtls_srtp_profile_list_len = 0; |
| 2604 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2605 | } |
| 2606 | |
Johan Pascal | 9bc97ca | 2020-09-21 23:44:45 +0200 | [diff] [blame] | 2607 | conf->dtls_srtp_profile_list = profiles; |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 2608 | conf->dtls_srtp_profile_list_len = list_size; |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2609 | |
| 2610 | return( 0 ); |
| 2611 | } |
| 2612 | |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 2613 | void mbedtls_ssl_get_dtls_srtp_negotiation_result( const mbedtls_ssl_context *ssl, |
| 2614 | mbedtls_dtls_srtp_info *dtls_srtp_info ) |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2615 | { |
Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 2616 | dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile; |
| 2617 | /* do not copy the mki value if there is no chosen profile */ |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 2618 | if( dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET ) |
Johan Pascal | 0dbcd1d | 2020-10-28 11:03:07 +0100 | [diff] [blame] | 2619 | { |
Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 2620 | dtls_srtp_info->mki_len = 0; |
Johan Pascal | 0dbcd1d | 2020-10-28 11:03:07 +0100 | [diff] [blame] | 2621 | } |
Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 2622 | else |
| 2623 | { |
| 2624 | dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len; |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 2625 | memcpy( dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value, |
| 2626 | ssl->dtls_srtp_info.mki_len ); |
Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 2627 | } |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2628 | } |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2629 | #endif /* MBEDTLS_SSL_DTLS_SRTP */ |
| 2630 | |
Aditya Patwardhan | 3096f33 | 2022-07-26 14:31:46 +0530 | [diff] [blame] | 2631 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 2632 | 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] | 2633 | { |
Glenn Strauss | 2dfcea2 | 2022-03-14 17:26:42 -0400 | [diff] [blame] | 2634 | conf->max_tls_version = (major << 8) | minor; |
Paul Bakker | 490ecc8 | 2011-10-06 13:04:09 +0000 | [diff] [blame] | 2635 | } |
| 2636 | |
Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 2637 | 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] | 2638 | { |
Glenn Strauss | 2dfcea2 | 2022-03-14 17:26:42 -0400 | [diff] [blame] | 2639 | conf->min_tls_version = (major << 8) | minor; |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 2640 | } |
Aditya Patwardhan | 3096f33 | 2022-07-26 14:31:46 +0530 | [diff] [blame] | 2641 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 2642 | |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 2643 | #if defined(MBEDTLS_SSL_SRV_C) |
| 2644 | void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf, |
| 2645 | char cert_req_ca_list ) |
| 2646 | { |
| 2647 | conf->cert_req_ca_list = cert_req_ca_list; |
| 2648 | } |
| 2649 | #endif |
| 2650 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2651 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2652 | 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] | 2653 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2654 | conf->encrypt_then_mac = etm; |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 2655 | } |
| 2656 | #endif |
| 2657 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2658 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2659 | 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] | 2660 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2661 | conf->extended_ms = ems; |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 2662 | } |
| 2663 | #endif |
| 2664 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2665 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2666 | 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] | 2667 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2668 | if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID || |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 2669 | 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] | 2670 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2671 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 2672 | } |
| 2673 | |
Manuel Pégourié-Gonnard | 6bf89d6 | 2015-05-05 17:01:57 +0100 | [diff] [blame] | 2674 | conf->mfl_code = mfl_code; |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 2675 | |
| 2676 | return( 0 ); |
| 2677 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2678 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 2679 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2680 | 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] | 2681 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2682 | conf->allow_legacy_renegotiation = allow_legacy; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2683 | } |
| 2684 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2685 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2686 | 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] | 2687 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2688 | conf->disable_renegotiation = renegotiation; |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 2689 | } |
| 2690 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2691 | 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] | 2692 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2693 | conf->renego_max_records = max_records; |
Manuel Pégourié-Gonnard | a9964db | 2014-07-03 19:29:16 +0200 | [diff] [blame] | 2694 | } |
| 2695 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2696 | void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 837f0fe | 2014-11-05 13:58:53 +0100 | [diff] [blame] | 2697 | const unsigned char period[8] ) |
| 2698 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2699 | memcpy( conf->renego_period, period, 8 ); |
Manuel Pégourié-Gonnard | 837f0fe | 2014-11-05 13:58:53 +0100 | [diff] [blame] | 2700 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2701 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2702 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2703 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 2704 | #if defined(MBEDTLS_SSL_CLI_C) |
| 2705 | 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] | 2706 | { |
Manuel Pégourié-Gonnard | 2b49445 | 2015-05-06 10:05:11 +0100 | [diff] [blame] | 2707 | conf->session_tickets = use_tickets; |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 2708 | } |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 2709 | #endif |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 2710 | |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 2711 | #if defined(MBEDTLS_SSL_SRV_C) |
Jerry Yu | 1ad7ace | 2022-08-09 13:28:39 +0800 | [diff] [blame] | 2712 | |
Jerry Yu | d0766ec | 2022-09-22 10:46:57 +0800 | [diff] [blame] | 2713 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS) |
Jerry Yu | 1ad7ace | 2022-08-09 13:28:39 +0800 | [diff] [blame] | 2714 | void mbedtls_ssl_conf_new_session_tickets( mbedtls_ssl_config *conf, |
| 2715 | uint16_t num_tickets ) |
| 2716 | { |
Jerry Yu | d0766ec | 2022-09-22 10:46:57 +0800 | [diff] [blame] | 2717 | conf->new_session_tickets_count = num_tickets; |
Jerry Yu | 1ad7ace | 2022-08-09 13:28:39 +0800 | [diff] [blame] | 2718 | } |
| 2719 | #endif |
| 2720 | |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 2721 | void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf, |
| 2722 | mbedtls_ssl_ticket_write_t *f_ticket_write, |
| 2723 | mbedtls_ssl_ticket_parse_t *f_ticket_parse, |
| 2724 | void *p_ticket ) |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 2725 | { |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 2726 | conf->f_ticket_write = f_ticket_write; |
| 2727 | conf->f_ticket_parse = f_ticket_parse; |
| 2728 | conf->p_ticket = p_ticket; |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 2729 | } |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 2730 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2731 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 2732 | |
Hanno Becker | 7e6c178 | 2021-06-08 09:24:55 +0100 | [diff] [blame] | 2733 | void mbedtls_ssl_set_export_keys_cb( mbedtls_ssl_context *ssl, |
| 2734 | mbedtls_ssl_export_keys_t *f_export_keys, |
| 2735 | void *p_export_keys ) |
Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 2736 | { |
Hanno Becker | 7e6c178 | 2021-06-08 09:24:55 +0100 | [diff] [blame] | 2737 | ssl->f_export_keys = f_export_keys; |
| 2738 | ssl->p_export_keys = p_export_keys; |
Ron Eldor | f5cc10d | 2019-05-07 18:33:40 +0300 | [diff] [blame] | 2739 | } |
Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 2740 | |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 2741 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 2742 | void mbedtls_ssl_conf_async_private_cb( |
| 2743 | mbedtls_ssl_config *conf, |
| 2744 | mbedtls_ssl_async_sign_t *f_async_sign, |
| 2745 | mbedtls_ssl_async_decrypt_t *f_async_decrypt, |
| 2746 | mbedtls_ssl_async_resume_t *f_async_resume, |
| 2747 | mbedtls_ssl_async_cancel_t *f_async_cancel, |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 2748 | void *async_config_data ) |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 2749 | { |
| 2750 | conf->f_async_sign_start = f_async_sign; |
| 2751 | conf->f_async_decrypt_start = f_async_decrypt; |
| 2752 | conf->f_async_resume = f_async_resume; |
| 2753 | conf->f_async_cancel = f_async_cancel; |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 2754 | conf->p_async_config_data = async_config_data; |
| 2755 | } |
| 2756 | |
Gilles Peskine | 8f97af7 | 2018-04-26 11:46:10 +0200 | [diff] [blame] | 2757 | void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf ) |
| 2758 | { |
| 2759 | return( conf->p_async_config_data ); |
| 2760 | } |
| 2761 | |
Gilles Peskine | 1febfef | 2018-04-30 11:54:39 +0200 | [diff] [blame] | 2762 | void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl ) |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 2763 | { |
| 2764 | if( ssl->handshake == NULL ) |
| 2765 | return( NULL ); |
| 2766 | else |
| 2767 | return( ssl->handshake->user_async_ctx ); |
| 2768 | } |
| 2769 | |
Gilles Peskine | 1febfef | 2018-04-30 11:54:39 +0200 | [diff] [blame] | 2770 | void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl, |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 2771 | void *ctx ) |
| 2772 | { |
| 2773 | if( ssl->handshake != NULL ) |
| 2774 | ssl->handshake->user_async_ctx = ctx; |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 2775 | } |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 2776 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 2777 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2778 | /* |
| 2779 | * SSL get accessors |
| 2780 | */ |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 2781 | uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2782 | { |
Manuel Pégourié-Gonnard | e89163c | 2015-01-23 14:30:57 +0000 | [diff] [blame] | 2783 | if( ssl->session != NULL ) |
| 2784 | return( ssl->session->verify_result ); |
| 2785 | |
| 2786 | if( ssl->session_negotiate != NULL ) |
| 2787 | return( ssl->session_negotiate->verify_result ); |
| 2788 | |
Manuel Pégourié-Gonnard | 6ab9b00 | 2015-05-14 11:25:04 +0200 | [diff] [blame] | 2789 | return( 0xFFFFFFFF ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2790 | } |
| 2791 | |
Glenn Strauss | 8f52690 | 2022-01-13 00:04:49 -0500 | [diff] [blame] | 2792 | int mbedtls_ssl_get_ciphersuite_id_from_ssl( const mbedtls_ssl_context *ssl ) |
| 2793 | { |
| 2794 | if( ssl == NULL || ssl->session == NULL ) |
| 2795 | return( 0 ); |
| 2796 | |
| 2797 | return( ssl->session->ciphersuite ); |
| 2798 | } |
| 2799 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2800 | const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl ) |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 2801 | { |
Paul Bakker | 926c8e4 | 2013-03-06 10:23:34 +0100 | [diff] [blame] | 2802 | if( ssl == NULL || ssl->session == NULL ) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 2803 | return( NULL ); |
Paul Bakker | 926c8e4 | 2013-03-06 10:23:34 +0100 | [diff] [blame] | 2804 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2805 | return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite ); |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 2806 | } |
| 2807 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2808 | const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl ) |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 2809 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2810 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2811 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 2812 | { |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 2813 | switch( ssl->tls_version ) |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 2814 | { |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 2815 | case MBEDTLS_SSL_VERSION_TLS1_2: |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 2816 | return( "DTLSv1.2" ); |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 2817 | default: |
| 2818 | return( "unknown (DTLS)" ); |
| 2819 | } |
| 2820 | } |
| 2821 | #endif |
| 2822 | |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 2823 | switch( ssl->tls_version ) |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 2824 | { |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 2825 | case MBEDTLS_SSL_VERSION_TLS1_2: |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2826 | return( "TLSv1.2" ); |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 2827 | case MBEDTLS_SSL_VERSION_TLS1_3: |
Gilles Peskine | c63a1e0 | 2022-01-13 01:10:24 +0100 | [diff] [blame] | 2828 | return( "TLSv1.3" ); |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 2829 | default: |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 2830 | return( "unknown" ); |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 2831 | } |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 2832 | } |
| 2833 | |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 2834 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 2835 | size_t mbedtls_ssl_get_input_max_frag_len( const mbedtls_ssl_context *ssl ) |
| 2836 | { |
David Horstmann | 95d516f | 2021-05-04 18:36:56 +0100 | [diff] [blame] | 2837 | size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN; |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 2838 | size_t read_mfl; |
| 2839 | |
| 2840 | /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */ |
| 2841 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
| 2842 | ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE ) |
| 2843 | { |
| 2844 | return ssl_mfl_code_to_length( ssl->conf->mfl_code ); |
| 2845 | } |
| 2846 | |
| 2847 | /* Check if a smaller max length was negotiated */ |
| 2848 | if( ssl->session_out != NULL ) |
| 2849 | { |
| 2850 | read_mfl = ssl_mfl_code_to_length( ssl->session_out->mfl_code ); |
| 2851 | if( read_mfl < max_len ) |
| 2852 | { |
| 2853 | max_len = read_mfl; |
| 2854 | } |
| 2855 | } |
| 2856 | |
| 2857 | // During a handshake, use the value being negotiated |
| 2858 | if( ssl->session_negotiate != NULL ) |
| 2859 | { |
| 2860 | read_mfl = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ); |
| 2861 | if( read_mfl < max_len ) |
| 2862 | { |
| 2863 | max_len = read_mfl; |
| 2864 | } |
| 2865 | } |
| 2866 | |
| 2867 | return( max_len ); |
| 2868 | } |
| 2869 | |
| 2870 | 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] | 2871 | { |
| 2872 | size_t max_len; |
| 2873 | |
| 2874 | /* |
| 2875 | * Assume mfl_code is correct since it was checked when set |
| 2876 | */ |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 2877 | 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] | 2878 | |
Manuel Pégourié-Gonnard | 2cb17e2 | 2017-09-19 13:00:47 +0200 | [diff] [blame] | 2879 | /* Check if a smaller max length was negotiated */ |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 2880 | if( ssl->session_out != NULL && |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 2881 | 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] | 2882 | { |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 2883 | 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] | 2884 | } |
| 2885 | |
Manuel Pégourié-Gonnard | 2cb17e2 | 2017-09-19 13:00:47 +0200 | [diff] [blame] | 2886 | /* During a handshake, use the value being negotiated */ |
| 2887 | if( ssl->session_negotiate != NULL && |
| 2888 | ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len ) |
| 2889 | { |
| 2890 | max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ); |
| 2891 | } |
| 2892 | |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 2893 | return( max_len ); |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 2894 | } |
| 2895 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
| 2896 | |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 2897 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 8949071 | 2020-02-05 10:50:12 +0000 | [diff] [blame] | 2898 | 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] | 2899 | { |
Andrzej Kurek | ef43ce6 | 2018-10-09 08:24:12 -0400 | [diff] [blame] | 2900 | /* Return unlimited mtu for client hello messages to avoid fragmentation. */ |
| 2901 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
| 2902 | ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO || |
| 2903 | ssl->state == MBEDTLS_SSL_SERVER_HELLO ) ) |
| 2904 | return ( 0 ); |
| 2905 | |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 2906 | if( ssl->handshake == NULL || ssl->handshake->mtu == 0 ) |
| 2907 | return( ssl->mtu ); |
| 2908 | |
| 2909 | if( ssl->mtu == 0 ) |
| 2910 | return( ssl->handshake->mtu ); |
| 2911 | |
| 2912 | return( ssl->mtu < ssl->handshake->mtu ? |
| 2913 | ssl->mtu : ssl->handshake->mtu ); |
| 2914 | } |
| 2915 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 2916 | |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 2917 | int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl ) |
| 2918 | { |
| 2919 | size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN; |
| 2920 | |
Manuel Pégourié-Gonnard | 000281e | 2018-08-21 11:20:58 +0200 | [diff] [blame] | 2921 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \ |
| 2922 | !defined(MBEDTLS_SSL_PROTO_DTLS) |
| 2923 | (void) ssl; |
| 2924 | #endif |
| 2925 | |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 2926 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 2927 | 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] | 2928 | |
| 2929 | if( max_len > mfl ) |
| 2930 | max_len = mfl; |
| 2931 | #endif |
| 2932 | |
| 2933 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 8949071 | 2020-02-05 10:50:12 +0000 | [diff] [blame] | 2934 | if( mbedtls_ssl_get_current_mtu( ssl ) != 0 ) |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 2935 | { |
Hanno Becker | 8949071 | 2020-02-05 10:50:12 +0000 | [diff] [blame] | 2936 | const size_t mtu = mbedtls_ssl_get_current_mtu( ssl ); |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 2937 | const int ret = mbedtls_ssl_get_record_expansion( ssl ); |
| 2938 | const size_t overhead = (size_t) ret; |
| 2939 | |
| 2940 | if( ret < 0 ) |
| 2941 | return( ret ); |
| 2942 | |
| 2943 | if( mtu <= overhead ) |
| 2944 | { |
| 2945 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) ); |
| 2946 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 2947 | } |
| 2948 | |
| 2949 | if( max_len > mtu - overhead ) |
| 2950 | max_len = mtu - overhead; |
| 2951 | } |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 2952 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 2953 | |
Hanno Becker | 0defedb | 2018-08-10 12:35:02 +0100 | [diff] [blame] | 2954 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \ |
| 2955 | !defined(MBEDTLS_SSL_PROTO_DTLS) |
| 2956 | ((void) ssl); |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 2957 | #endif |
| 2958 | |
| 2959 | return( (int) max_len ); |
| 2960 | } |
| 2961 | |
Hanno Becker | 2d8e99b | 2021-04-21 06:19:50 +0100 | [diff] [blame] | 2962 | int mbedtls_ssl_get_max_in_record_payload( const mbedtls_ssl_context *ssl ) |
| 2963 | { |
| 2964 | size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN; |
| 2965 | |
| 2966 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 2967 | (void) ssl; |
| 2968 | #endif |
| 2969 | |
| 2970 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 2971 | const size_t mfl = mbedtls_ssl_get_input_max_frag_len( ssl ); |
| 2972 | |
| 2973 | if( max_len > mfl ) |
| 2974 | max_len = mfl; |
| 2975 | #endif |
| 2976 | |
| 2977 | return( (int) max_len ); |
| 2978 | } |
| 2979 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2980 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 2981 | 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] | 2982 | { |
| 2983 | if( ssl == NULL || ssl->session == NULL ) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 2984 | return( NULL ); |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 2985 | |
Hanno Becker | e682457 | 2019-02-07 13:18:46 +0000 | [diff] [blame] | 2986 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 2987 | return( ssl->session->peer_cert ); |
Hanno Becker | e682457 | 2019-02-07 13:18:46 +0000 | [diff] [blame] | 2988 | #else |
| 2989 | return( NULL ); |
| 2990 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 2991 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2992 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 2993 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2994 | #if defined(MBEDTLS_SSL_CLI_C) |
Hanno Becker | f852b1c | 2019-02-05 11:42:30 +0000 | [diff] [blame] | 2995 | int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, |
| 2996 | mbedtls_ssl_session *dst ) |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 2997 | { |
Hanno Becker | e810bbc | 2021-05-14 16:01:05 +0100 | [diff] [blame] | 2998 | int ret; |
| 2999 | |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 3000 | if( ssl == NULL || |
| 3001 | dst == NULL || |
| 3002 | ssl->session == NULL || |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3003 | ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT ) |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 3004 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3005 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 3006 | } |
| 3007 | |
Hanno Becker | e810bbc | 2021-05-14 16:01:05 +0100 | [diff] [blame] | 3008 | /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer |
| 3009 | * idempotent: Each session can only be exported once. |
| 3010 | * |
| 3011 | * (This is in preparation for TLS 1.3 support where we will |
| 3012 | * need the ability to export multiple sessions (aka tickets), |
| 3013 | * which will be achieved by calling mbedtls_ssl_get_session() |
| 3014 | * multiple times until it fails.) |
| 3015 | * |
| 3016 | * Check whether we have already exported the current session, |
| 3017 | * and fail if so. |
| 3018 | */ |
| 3019 | if( ssl->session->exported == 1 ) |
| 3020 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 3021 | |
| 3022 | ret = mbedtls_ssl_session_copy( dst, ssl->session ); |
| 3023 | if( ret != 0 ) |
| 3024 | return( ret ); |
| 3025 | |
| 3026 | /* Remember that we've exported the session. */ |
| 3027 | ssl->session->exported = 1; |
| 3028 | return( 0 ); |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 3029 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3030 | #endif /* MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 3031 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3032 | /* |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 3033 | * Define ticket header determining Mbed TLS version |
| 3034 | * and structure of the ticket. |
| 3035 | */ |
| 3036 | |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3037 | /* |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 3038 | * Define bitflag determining compile-time settings influencing |
| 3039 | * structure of serialized SSL sessions. |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3040 | */ |
| 3041 | |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 3042 | #if defined(MBEDTLS_HAVE_TIME) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3043 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME 1 |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 3044 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3045 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3046 | #endif /* MBEDTLS_HAVE_TIME */ |
| 3047 | |
| 3048 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3049 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3050 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3051 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3052 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 3053 | |
| 3054 | #if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3055 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3056 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3057 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3058 | #endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */ |
| 3059 | |
| 3060 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3061 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3062 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3063 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3064 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
| 3065 | |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3066 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3067 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3068 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3069 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3070 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ |
| 3071 | |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3072 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 3073 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1 |
| 3074 | #else |
| 3075 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0 |
| 3076 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
| 3077 | |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3078 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0 |
| 3079 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1 |
| 3080 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2 |
| 3081 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3 |
Hanno Becker | 37bdbe6 | 2021-08-01 05:38:58 +0100 | [diff] [blame] | 3082 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4 |
| 3083 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5 |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3084 | |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 3085 | #define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \ |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3086 | ( (uint16_t) ( \ |
| 3087 | ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \ |
| 3088 | ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \ |
| 3089 | ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \ |
| 3090 | ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \ |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3091 | ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \ |
Hanno Becker | be34e8e | 2019-06-04 09:43:16 +0100 | [diff] [blame] | 3092 | ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) ) ) |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3093 | |
Hanno Becker | f878707 | 2019-05-16 12:41:07 +0100 | [diff] [blame] | 3094 | static unsigned char ssl_serialized_session_header[] = { |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3095 | MBEDTLS_VERSION_MAJOR, |
| 3096 | MBEDTLS_VERSION_MINOR, |
| 3097 | MBEDTLS_VERSION_PATCH, |
Joe Subbiani | 2194dc4 | 2021-07-14 12:31:31 +0100 | [diff] [blame] | 3098 | MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ), |
| 3099 | MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ), |
Hanno Becker | f878707 | 2019-05-16 12:41:07 +0100 | [diff] [blame] | 3100 | }; |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 3101 | |
| 3102 | /* |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 3103 | * Serialize a session in the following format: |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 3104 | * (in the presentation language of TLS, RFC 8446 section 3) |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 3105 | * |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3106 | * struct { |
Hanno Becker | dc28b6c | 2019-05-29 11:08:00 +0100 | [diff] [blame] | 3107 | * |
Hanno Becker | dce5097 | 2021-08-01 05:39:23 +0100 | [diff] [blame] | 3108 | * opaque mbedtls_version[3]; // library version: major, minor, patch |
| 3109 | * opaque session_format[2]; // library-version specific 16-bit field |
| 3110 | * // determining the format of the remaining |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3111 | * // serialized data. |
Hanno Becker | dc28b6c | 2019-05-29 11:08:00 +0100 | [diff] [blame] | 3112 | * |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3113 | * Note: When updating the format, remember to keep |
| 3114 | * these version+format bytes. |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 3115 | * |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3116 | * // In this version, `session_format` determines |
| 3117 | * // the setting of those compile-time |
| 3118 | * // configuration options which influence |
| 3119 | * // the structure of mbedtls_ssl_session. |
| 3120 | * |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 3121 | * uint8_t minor_ver; // Protocol minor version. Possible values: |
| 3122 | * // - TLS 1.2 (3) |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3123 | * |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 3124 | * select (serialized_session.tls_version) { |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3125 | * |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 3126 | * case MBEDTLS_SSL_VERSION_TLS1_2: |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3127 | * serialized_session_tls12 data; |
| 3128 | * |
| 3129 | * }; |
| 3130 | * |
| 3131 | * } serialized_session; |
| 3132 | * |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 3133 | */ |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3134 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 3135 | MBEDTLS_CHECK_RETURN_CRITICAL |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3136 | static int ssl_session_save( const mbedtls_ssl_session *session, |
| 3137 | unsigned char omit_header, |
| 3138 | unsigned char *buf, |
| 3139 | size_t buf_len, |
| 3140 | size_t *olen ) |
| 3141 | { |
| 3142 | unsigned char *p = buf; |
| 3143 | size_t used = 0; |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 3144 | size_t remaining_len; |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 3145 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 3146 | size_t out_len; |
| 3147 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 3148 | #endif |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 3149 | if( session == NULL ) |
| 3150 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 3151 | |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3152 | if( !omit_header ) |
| 3153 | { |
| 3154 | /* |
| 3155 | * Add Mbed TLS version identifier |
| 3156 | */ |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3157 | used += sizeof( ssl_serialized_session_header ); |
| 3158 | |
| 3159 | if( used <= buf_len ) |
| 3160 | { |
| 3161 | memcpy( p, ssl_serialized_session_header, |
| 3162 | sizeof( ssl_serialized_session_header ) ); |
| 3163 | p += sizeof( ssl_serialized_session_header ); |
| 3164 | } |
| 3165 | } |
| 3166 | |
| 3167 | /* |
| 3168 | * TLS version identifier |
| 3169 | */ |
| 3170 | used += 1; |
| 3171 | if( used <= buf_len ) |
| 3172 | { |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 3173 | *p++ = MBEDTLS_BYTE_0( session->tls_version ); |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3174 | } |
| 3175 | |
| 3176 | /* Forward to version-specific serialization routine. */ |
Jerry Yu | fca4d57 | 2022-07-21 10:37:48 +0800 | [diff] [blame] | 3177 | remaining_len = (buf_len >= used) ? buf_len - used : 0; |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 3178 | switch( session->tls_version ) |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3179 | { |
| 3180 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 3181 | case MBEDTLS_SSL_VERSION_TLS1_2: |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 3182 | used += ssl_tls12_session_save( session, p, remaining_len ); |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3183 | break; |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3184 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 3185 | |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 3186 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 3187 | case MBEDTLS_SSL_VERSION_TLS1_3: |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 3188 | ret = ssl_tls13_session_save( session, p, remaining_len, &out_len ); |
| 3189 | if( ret != 0 && ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ) |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 3190 | return( ret ); |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 3191 | used += out_len; |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 3192 | break; |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 3193 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 3194 | |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3195 | default: |
| 3196 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 3197 | } |
| 3198 | |
| 3199 | *olen = used; |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 3200 | if( used > buf_len ) |
| 3201 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 3202 | |
| 3203 | return( 0 ); |
| 3204 | } |
| 3205 | |
| 3206 | /* |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 3207 | * Public wrapper for ssl_session_save() |
| 3208 | */ |
| 3209 | int mbedtls_ssl_session_save( const mbedtls_ssl_session *session, |
| 3210 | unsigned char *buf, |
| 3211 | size_t buf_len, |
| 3212 | size_t *olen ) |
| 3213 | { |
| 3214 | return( ssl_session_save( session, 0, buf, buf_len, olen ) ); |
| 3215 | } |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 3216 | |
Jerry Yu | f1b23ca | 2022-02-18 11:48:47 +0800 | [diff] [blame] | 3217 | /* |
| 3218 | * Deserialize session, see mbedtls_ssl_session_save() for format. |
| 3219 | * |
| 3220 | * This internal version is wrapped by a public function that cleans up in |
| 3221 | * case of error, and has an extra option omit_header. |
| 3222 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 3223 | MBEDTLS_CHECK_RETURN_CRITICAL |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3224 | static int ssl_session_load( mbedtls_ssl_session *session, |
| 3225 | unsigned char omit_header, |
| 3226 | const unsigned char *buf, |
| 3227 | size_t len ) |
| 3228 | { |
| 3229 | const unsigned char *p = buf; |
| 3230 | const unsigned char * const end = buf + len; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 3231 | size_t remaining_len; |
| 3232 | |
| 3233 | |
| 3234 | if( session == NULL ) |
| 3235 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3236 | |
| 3237 | if( !omit_header ) |
| 3238 | { |
| 3239 | /* |
| 3240 | * Check Mbed TLS version identifier |
| 3241 | */ |
| 3242 | |
| 3243 | if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) ) |
| 3244 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3245 | |
| 3246 | if( memcmp( p, ssl_serialized_session_header, |
| 3247 | sizeof( ssl_serialized_session_header ) ) != 0 ) |
| 3248 | { |
| 3249 | return( MBEDTLS_ERR_SSL_VERSION_MISMATCH ); |
| 3250 | } |
| 3251 | p += sizeof( ssl_serialized_session_header ); |
| 3252 | } |
| 3253 | |
| 3254 | /* |
| 3255 | * TLS version identifier |
| 3256 | */ |
| 3257 | if( 1 > (size_t)( end - p ) ) |
| 3258 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 3259 | session->tls_version = 0x0300 | *p++; |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3260 | |
| 3261 | /* Dispatch according to TLS version. */ |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 3262 | remaining_len = ( end - p ); |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 3263 | switch( session->tls_version ) |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3264 | { |
| 3265 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 3266 | case MBEDTLS_SSL_VERSION_TLS1_2: |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 3267 | return( ssl_tls12_session_load( session, p, remaining_len ) ); |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3268 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 3269 | |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 3270 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 3271 | case MBEDTLS_SSL_VERSION_TLS1_3: |
| 3272 | return( ssl_tls13_session_load( session, p, remaining_len ) ); |
| 3273 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 3274 | |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3275 | default: |
| 3276 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3277 | } |
| 3278 | } |
| 3279 | |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 3280 | /* |
Manuel Pégourié-Gonnard | b9dfc9f | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 3281 | * Deserialize session: public wrapper for error cleaning |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 3282 | */ |
| 3283 | int mbedtls_ssl_session_load( mbedtls_ssl_session *session, |
| 3284 | const unsigned char *buf, |
| 3285 | size_t len ) |
| 3286 | { |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 3287 | int ret = ssl_session_load( session, 0, buf, len ); |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 3288 | |
| 3289 | if( ret != 0 ) |
| 3290 | mbedtls_ssl_session_free( session ); |
| 3291 | |
| 3292 | return( ret ); |
| 3293 | } |
| 3294 | |
| 3295 | /* |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3296 | * Perform a single step of the SSL handshake |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3297 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 3298 | MBEDTLS_CHECK_RETURN_CRITICAL |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 3299 | static int ssl_prepare_handshake_step( mbedtls_ssl_context *ssl ) |
| 3300 | { |
| 3301 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 3302 | |
Ronald Cron | 66dbf91 | 2022-02-02 15:33:46 +0100 | [diff] [blame] | 3303 | /* |
| 3304 | * 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] | 3305 | * that were written into the output buffer by the previous handshake step, |
| 3306 | * if the write to the network callback returned with the |
Ronald Cron | 66dbf91 | 2022-02-02 15:33:46 +0100 | [diff] [blame] | 3307 | * #MBEDTLS_ERR_SSL_WANT_WRITE error code. |
| 3308 | * We proceed to the next handshake step only when all data from the |
| 3309 | * previous one have been sent to the peer, thus we make sure that this is |
| 3310 | * the case here by calling `mbedtls_ssl_flush_output()`. The function may |
| 3311 | * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case |
| 3312 | * we have to wait before to go ahead. |
| 3313 | * In the case of TLS 1.3, handshake step handlers do not send data to the |
| 3314 | * peer. Data are only sent here and through |
| 3315 | * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an |
Andrzej Kurek | 5c65c57 | 2022-04-13 14:28:52 -0400 | [diff] [blame] | 3316 | * alert occurred. |
Ronald Cron | 66dbf91 | 2022-02-02 15:33:46 +0100 | [diff] [blame] | 3317 | */ |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 3318 | if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) |
| 3319 | return( ret ); |
| 3320 | |
| 3321 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3322 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 3323 | ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING ) |
| 3324 | { |
| 3325 | if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 ) |
| 3326 | return( ret ); |
| 3327 | } |
| 3328 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 3329 | |
| 3330 | return( ret ); |
| 3331 | } |
| 3332 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3333 | int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3334 | { |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 3335 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3336 | |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 3337 | if( ssl == NULL || |
| 3338 | ssl->conf == NULL || |
| 3339 | ssl->handshake == NULL || |
Paul Elliott | 27b0d94 | 2022-03-18 21:55:32 +0000 | [diff] [blame] | 3340 | mbedtls_ssl_is_handshake_over( ssl ) == 1 ) |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 3341 | { |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 3342 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 3343 | } |
| 3344 | |
| 3345 | ret = ssl_prepare_handshake_step( ssl ); |
| 3346 | if( ret != 0 ) |
| 3347 | return( ret ); |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 3348 | |
Jerry Yu | e704781 | 2021-09-13 19:26:39 +0800 | [diff] [blame] | 3349 | ret = mbedtls_ssl_handle_pending_alert( ssl ); |
| 3350 | if( ret != 0 ) |
| 3351 | goto cleanup; |
| 3352 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3353 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3354 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 3355 | { |
Ronald Cron | 27c85e7 | 2022-03-08 11:37:55 +0100 | [diff] [blame] | 3356 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "client state: %s", |
| 3357 | mbedtls_ssl_states_str( ssl->state ) ) ); |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 3358 | |
Ronald Cron | 9f0fba3 | 2022-02-10 16:45:15 +0100 | [diff] [blame] | 3359 | switch( ssl->state ) |
| 3360 | { |
| 3361 | case MBEDTLS_SSL_HELLO_REQUEST: |
| 3362 | ssl->state = MBEDTLS_SSL_CLIENT_HELLO; |
| 3363 | break; |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 3364 | |
Ronald Cron | 9f0fba3 | 2022-02-10 16:45:15 +0100 | [diff] [blame] | 3365 | case MBEDTLS_SSL_CLIENT_HELLO: |
| 3366 | ret = mbedtls_ssl_write_client_hello( ssl ); |
| 3367 | break; |
| 3368 | |
| 3369 | default: |
| 3370 | #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] | 3371 | if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 ) |
Ronald Cron | 9f0fba3 | 2022-02-10 16:45:15 +0100 | [diff] [blame] | 3372 | ret = mbedtls_ssl_tls13_handshake_client_step( ssl ); |
| 3373 | else |
| 3374 | ret = mbedtls_ssl_handshake_client_step( ssl ); |
| 3375 | #elif defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 3376 | ret = mbedtls_ssl_handshake_client_step( ssl ); |
| 3377 | #else |
| 3378 | ret = mbedtls_ssl_tls13_handshake_client_step( ssl ); |
| 3379 | #endif |
| 3380 | } |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 3381 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3382 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3383 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3384 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 3385 | { |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3386 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 3387 | if( mbedtls_ssl_conf_is_tls13_only( ssl->conf ) ) |
Jerry Yu | 2756193 | 2021-08-27 17:07:38 +0800 | [diff] [blame] | 3388 | ret = mbedtls_ssl_tls13_handshake_server_step( ssl ); |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3389 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 3390 | |
| 3391 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 3392 | if( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) ) |
| 3393 | ret = mbedtls_ssl_handshake_server_step( ssl ); |
| 3394 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 3395 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3396 | #endif |
| 3397 | |
Jerry Yu | e704781 | 2021-09-13 19:26:39 +0800 | [diff] [blame] | 3398 | if( ret != 0 ) |
| 3399 | { |
Jerry Yu | bbd5a3f | 2021-09-18 20:50:22 +0800 | [diff] [blame] | 3400 | /* handshake_step return error. And it is same |
| 3401 | * with alert_reason. |
| 3402 | */ |
Jerry Yu | 3bf1f97 | 2021-09-22 21:37:18 +0800 | [diff] [blame] | 3403 | if( ssl->send_alert ) |
Jerry Yu | e704781 | 2021-09-13 19:26:39 +0800 | [diff] [blame] | 3404 | { |
Jerry Yu | 3bf1f97 | 2021-09-22 21:37:18 +0800 | [diff] [blame] | 3405 | ret = mbedtls_ssl_handle_pending_alert( ssl ); |
Jerry Yu | e704781 | 2021-09-13 19:26:39 +0800 | [diff] [blame] | 3406 | goto cleanup; |
| 3407 | } |
| 3408 | } |
| 3409 | |
| 3410 | cleanup: |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3411 | return( ret ); |
| 3412 | } |
| 3413 | |
| 3414 | /* |
| 3415 | * Perform the SSL handshake |
| 3416 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3417 | int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl ) |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3418 | { |
| 3419 | int ret = 0; |
| 3420 | |
Hanno Becker | a817ea4 | 2020-10-20 15:20:23 +0100 | [diff] [blame] | 3421 | /* Sanity checks */ |
| 3422 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 3423 | if( ssl == NULL || ssl->conf == NULL ) |
| 3424 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3425 | |
Hanno Becker | a817ea4 | 2020-10-20 15:20:23 +0100 | [diff] [blame] | 3426 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3427 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 3428 | ( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL ) ) |
| 3429 | { |
| 3430 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use " |
| 3431 | "mbedtls_ssl_set_timer_cb() for DTLS" ) ); |
| 3432 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3433 | } |
| 3434 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 3435 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3436 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3437 | |
Hanno Becker | a817ea4 | 2020-10-20 15:20:23 +0100 | [diff] [blame] | 3438 | /* Main handshake loop */ |
Paul Elliott | 27b0d94 | 2022-03-18 21:55:32 +0000 | [diff] [blame] | 3439 | while( mbedtls_ssl_is_handshake_over( ssl ) == 0 ) |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3440 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3441 | ret = mbedtls_ssl_handshake_step( ssl ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3442 | |
| 3443 | if( ret != 0 ) |
| 3444 | break; |
| 3445 | } |
| 3446 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3447 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3448 | |
| 3449 | return( ret ); |
| 3450 | } |
| 3451 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3452 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 3453 | #if defined(MBEDTLS_SSL_SRV_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3454 | /* |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3455 | * Write HelloRequest to request renegotiation on server |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3456 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 3457 | MBEDTLS_CHECK_RETURN_CRITICAL |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3458 | static int ssl_write_hello_request( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3459 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3460 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3461 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3462 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) ); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3463 | |
| 3464 | ssl->out_msglen = 4; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3465 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 3466 | ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST; |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3467 | |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3468 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3469 | { |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3470 | 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] | 3471 | return( ret ); |
| 3472 | } |
| 3473 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3474 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) ); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3475 | |
| 3476 | return( 0 ); |
| 3477 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3478 | #endif /* MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3479 | |
| 3480 | /* |
| 3481 | * Actually renegotiate current connection, triggered by either: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3482 | * - any side: calling mbedtls_ssl_renegotiate(), |
| 3483 | * - client: receiving a HelloRequest during mbedtls_ssl_read(), |
| 3484 | * - 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] | 3485 | * the initial handshake is completed. |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3486 | * 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] | 3487 | * 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] | 3488 | */ |
Hanno Becker | 40cdaa1 | 2020-02-05 10:48:27 +0000 | [diff] [blame] | 3489 | int mbedtls_ssl_start_renegotiation( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3490 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3491 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3492 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3493 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3494 | |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3495 | if( ( ret = ssl_handshake_init( ssl ) ) != 0 ) |
| 3496 | return( ret ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3497 | |
Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 3498 | /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and |
| 3499 | * the ServerHello will have message_seq = 1" */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3500 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3501 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3502 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ) |
Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 3503 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3504 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 3505 | ssl->handshake->out_msg_seq = 1; |
| 3506 | else |
| 3507 | ssl->handshake->in_msg_seq = 1; |
Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 3508 | } |
| 3509 | #endif |
| 3510 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3511 | ssl->state = MBEDTLS_SSL_HELLO_REQUEST; |
| 3512 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3513 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3514 | if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3515 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3516 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3517 | return( ret ); |
| 3518 | } |
| 3519 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3520 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3521 | |
| 3522 | return( 0 ); |
| 3523 | } |
| 3524 | |
| 3525 | /* |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3526 | * Renegotiate current connection on client, |
| 3527 | * or request renegotiation on server |
| 3528 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3529 | int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3530 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3531 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3532 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 3533 | if( ssl == NULL || ssl->conf == NULL ) |
| 3534 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3535 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3536 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3537 | /* On server, just send the request */ |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3538 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3539 | { |
Paul Elliott | 27b0d94 | 2022-03-18 21:55:32 +0000 | [diff] [blame] | 3540 | if( mbedtls_ssl_is_handshake_over( ssl ) == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3541 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3542 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3543 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING; |
Manuel Pégourié-Gonnard | f07f421 | 2014-08-15 19:04:47 +0200 | [diff] [blame] | 3544 | |
| 3545 | /* Did we already try/start sending HelloRequest? */ |
| 3546 | if( ssl->out_left != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3547 | return( mbedtls_ssl_flush_output( ssl ) ); |
Manuel Pégourié-Gonnard | f07f421 | 2014-08-15 19:04:47 +0200 | [diff] [blame] | 3548 | |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3549 | return( ssl_write_hello_request( ssl ) ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3550 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3551 | #endif /* MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3552 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3553 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3554 | /* |
| 3555 | * On client, either start the renegotiation process or, |
| 3556 | * if already in progress, continue the handshake |
| 3557 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3558 | if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3559 | { |
Paul Elliott | 27b0d94 | 2022-03-18 21:55:32 +0000 | [diff] [blame] | 3560 | if( mbedtls_ssl_is_handshake_over( ssl ) == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3561 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3562 | |
Hanno Becker | 40cdaa1 | 2020-02-05 10:48:27 +0000 | [diff] [blame] | 3563 | if( ( ret = mbedtls_ssl_start_renegotiation( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3564 | { |
Hanno Becker | 40cdaa1 | 2020-02-05 10:48:27 +0000 | [diff] [blame] | 3565 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_start_renegotiation", ret ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3566 | return( ret ); |
| 3567 | } |
| 3568 | } |
| 3569 | else |
| 3570 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3571 | if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3572 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3573 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3574 | return( ret ); |
| 3575 | } |
| 3576 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3577 | #endif /* MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3578 | |
Paul Bakker | 37ce0ff | 2013-10-31 14:32:04 +0100 | [diff] [blame] | 3579 | return( ret ); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3580 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3581 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3582 | |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 3583 | void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3584 | { |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 3585 | mbedtls_ssl_handshake_params *handshake = ssl->handshake; |
| 3586 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3587 | if( handshake == NULL ) |
| 3588 | return; |
| 3589 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 3590 | #if defined(MBEDTLS_ECP_C) |
| 3591 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 3592 | if ( ssl->handshake->group_list_heap_allocated ) |
| 3593 | mbedtls_free( (void*) handshake->group_list ); |
| 3594 | handshake->group_list = NULL; |
| 3595 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
| 3596 | #endif /* MBEDTLS_ECP_C */ |
| 3597 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 3598 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 3599 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 3600 | if ( ssl->handshake->sig_algs_heap_allocated ) |
| 3601 | mbedtls_free( (void*) handshake->sig_algs ); |
| 3602 | handshake->sig_algs = NULL; |
| 3603 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
Xiaofei Bai | c234ecf | 2022-02-08 09:59:23 +0000 | [diff] [blame] | 3604 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 3605 | if( ssl->handshake->certificate_request_context ) |
| 3606 | { |
| 3607 | mbedtls_free( (void*) handshake->certificate_request_context ); |
| 3608 | } |
| 3609 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 3610 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 3611 | |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3612 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
| 3613 | if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 ) |
| 3614 | { |
Gilles Peskine | 8f97af7 | 2018-04-26 11:46:10 +0200 | [diff] [blame] | 3615 | ssl->conf->f_async_cancel( ssl ); |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3616 | handshake->async_in_progress = 0; |
| 3617 | } |
| 3618 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
| 3619 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 3620 | #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] | 3621 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 3622 | psa_hash_abort( &handshake->fin_sha256_psa ); |
| 3623 | #else |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 3624 | mbedtls_sha256_free( &handshake->fin_sha256 ); |
| 3625 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3626 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 3627 | #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] | 3628 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 3629 | psa_hash_abort( &handshake->fin_sha384_psa ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3630 | #else |
Andrzej Kurek | a242e83 | 2022-08-11 10:03:14 -0400 | [diff] [blame] | 3631 | mbedtls_sha512_free( &handshake->fin_sha384 ); |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 3632 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3633 | #endif |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 3634 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3635 | #if defined(MBEDTLS_DHM_C) |
| 3636 | mbedtls_dhm_free( &handshake->dhm_ctx ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3637 | #endif |
Neil Armstrong | f3f4641 | 2022-04-12 14:43:39 +0200 | [diff] [blame] | 3638 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3639 | mbedtls_ecdh_free( &handshake->ecdh_ctx ); |
Paul Bakker | 61d113b | 2013-07-04 11:51:43 +0200 | [diff] [blame] | 3640 | #endif |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 3641 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 3642 | mbedtls_ecjpake_free( &handshake->ecjpake_ctx ); |
Manuel Pégourié-Gonnard | 77c0646 | 2015-09-17 13:59:49 +0200 | [diff] [blame] | 3643 | #if defined(MBEDTLS_SSL_CLI_C) |
| 3644 | mbedtls_free( handshake->ecjpake_cache ); |
| 3645 | handshake->ecjpake_cache = NULL; |
| 3646 | handshake->ecjpake_cache_len = 0; |
| 3647 | #endif |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 3648 | #endif |
Paul Bakker | 61d113b | 2013-07-04 11:51:43 +0200 | [diff] [blame] | 3649 | |
Janos Follath | 4ae5c29 | 2016-02-10 11:27:43 +0000 | [diff] [blame] | 3650 | #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ |
| 3651 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 3652 | /* explicit void pointer cast for buggy MS compiler */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3653 | mbedtls_free( (void *) handshake->curves ); |
Manuel Pégourié-Gonnard | d09453c | 2013-09-23 19:11:32 +0200 | [diff] [blame] | 3654 | #endif |
| 3655 | |
Ronald Cron | 73fe8df | 2022-10-05 14:31:43 +0200 | [diff] [blame] | 3656 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED) |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 3657 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 3658 | if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) ) |
| 3659 | { |
| 3660 | /* The maintenance of the external PSK key slot is the |
| 3661 | * user's responsibility. */ |
| 3662 | if( ssl->handshake->psk_opaque_is_internal ) |
| 3663 | { |
| 3664 | psa_destroy_key( ssl->handshake->psk_opaque ); |
| 3665 | ssl->handshake->psk_opaque_is_internal = 0; |
| 3666 | } |
| 3667 | ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT; |
| 3668 | } |
Neil Armstrong | e952a30 | 2022-05-03 10:22:14 +0200 | [diff] [blame] | 3669 | #else |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 3670 | if( handshake->psk != NULL ) |
| 3671 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 3672 | mbedtls_platform_zeroize( handshake->psk, handshake->psk_len ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 3673 | mbedtls_free( handshake->psk ); |
| 3674 | } |
Neil Armstrong | e952a30 | 2022-05-03 10:22:14 +0200 | [diff] [blame] | 3675 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Ronald Cron | 73fe8df | 2022-10-05 14:31:43 +0200 | [diff] [blame] | 3676 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */ |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 3677 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3678 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 3679 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Manuel Pégourié-Gonnard | 8372454 | 2013-09-24 22:30:56 +0200 | [diff] [blame] | 3680 | /* |
| 3681 | * Free only the linked list wrapper, not the keys themselves |
| 3682 | * since the belong to the SNI callback |
| 3683 | */ |
Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 3684 | ssl_key_cert_free( handshake->sni_key_cert ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3685 | #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] | 3686 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3687 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Manuel Pégourié-Gonnard | 6b7301c | 2017-08-15 12:08:45 +0200 | [diff] [blame] | 3688 | mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx ); |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 3689 | if( handshake->ecrs_peer_cert != NULL ) |
| 3690 | { |
| 3691 | mbedtls_x509_crt_free( handshake->ecrs_peer_cert ); |
| 3692 | mbedtls_free( handshake->ecrs_peer_cert ); |
| 3693 | } |
Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 3694 | #endif |
| 3695 | |
Hanno Becker | 7517312 | 2019-02-06 16:18:31 +0000 | [diff] [blame] | 3696 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 3697 | !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 3698 | mbedtls_pk_free( &handshake->peer_pubkey ); |
| 3699 | #endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 3700 | |
XiaokangQian | 9b93c0d | 2022-02-09 06:02:25 +0000 | [diff] [blame] | 3701 | #if defined(MBEDTLS_SSL_CLI_C) && \ |
| 3702 | ( defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3) ) |
| 3703 | mbedtls_free( handshake->cookie ); |
| 3704 | #endif /* MBEDTLS_SSL_CLI_C && |
| 3705 | ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */ |
XiaokangQian | 8499b6c | 2022-01-27 09:00:11 +0000 | [diff] [blame] | 3706 | |
| 3707 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 533ab5f | 2020-02-05 10:49:13 +0000 | [diff] [blame] | 3708 | mbedtls_ssl_flight_free( handshake->flight ); |
| 3709 | mbedtls_ssl_buffering_free( ssl ); |
XiaokangQian | 8499b6c | 2022-01-27 09:00:11 +0000 | [diff] [blame] | 3710 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 3711 | |
Ronald Cron | f12b81d | 2022-03-15 10:42:41 +0100 | [diff] [blame] | 3712 | #if defined(MBEDTLS_ECDH_C) && \ |
| 3713 | ( defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3) ) |
Neil Armstrong | f716a70 | 2022-04-04 11:23:46 +0200 | [diff] [blame] | 3714 | if( handshake->ecdh_psa_privkey_is_external == 0 ) |
Neil Armstrong | 8113d25 | 2022-03-23 10:57:04 +0100 | [diff] [blame] | 3715 | psa_destroy_key( handshake->ecdh_psa_privkey ); |
Hanno Becker | 4a63ed4 | 2019-01-08 11:39:35 +0000 | [diff] [blame] | 3716 | #endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */ |
| 3717 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3718 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | a1a568c | 2021-11-09 10:17:21 +0800 | [diff] [blame] | 3719 | mbedtls_ssl_transform_free( handshake->transform_handshake ); |
| 3720 | mbedtls_ssl_transform_free( handshake->transform_earlydata ); |
Jerry Yu | ba9c727 | 2021-10-30 11:54:10 +0800 | [diff] [blame] | 3721 | mbedtls_free( handshake->transform_earlydata ); |
| 3722 | mbedtls_free( handshake->transform_handshake ); |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3723 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Jerry Yu | ba9c727 | 2021-10-30 11:54:10 +0800 | [diff] [blame] | 3724 | |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 3725 | |
| 3726 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 3727 | /* If the buffers are too big - reallocate. Because of the way Mbed TLS |
| 3728 | * processes datagrams and the fact that a datagram is allowed to have |
| 3729 | * several records in it, it is possible that the I/O buffers are not |
| 3730 | * empty at this stage */ |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 3731 | handle_buffer_resizing( ssl, 1, mbedtls_ssl_get_input_buflen( ssl ), |
| 3732 | mbedtls_ssl_get_output_buflen( ssl ) ); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 3733 | #endif |
Hanno Becker | 3aa186f | 2021-08-10 09:24:19 +0100 | [diff] [blame] | 3734 | |
Jerry Yu | ba9c727 | 2021-10-30 11:54:10 +0800 | [diff] [blame] | 3735 | /* mbedtls_platform_zeroize MUST be last one in this function */ |
| 3736 | mbedtls_platform_zeroize( handshake, |
| 3737 | sizeof( mbedtls_ssl_handshake_params ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3738 | } |
| 3739 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3740 | void mbedtls_ssl_session_free( mbedtls_ssl_session *session ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3741 | { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3742 | if( session == NULL ) |
| 3743 | return; |
| 3744 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3745 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Hanno Becker | 1294a0b | 2019-02-05 12:38:15 +0000 | [diff] [blame] | 3746 | ssl_clear_peer_cert( session ); |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 3747 | #endif |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 3748 | |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 3749 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
Xiaokang Qian | bc663a0 | 2022-10-09 11:14:39 +0000 | [diff] [blame] | 3750 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \ |
| 3751 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Xiaokang Qian | 281fd1b | 2022-09-20 11:35:41 +0000 | [diff] [blame] | 3752 | mbedtls_free( session->hostname ); |
| 3753 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3754 | mbedtls_free( session->ticket ); |
Paul Bakker | a503a63 | 2013-08-14 13:48:06 +0200 | [diff] [blame] | 3755 | #endif |
Manuel Pégourié-Gonnard | 75d4401 | 2013-08-02 14:44:04 +0200 | [diff] [blame] | 3756 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 3757 | mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3758 | } |
| 3759 | |
Manuel Pégourié-Gonnard | 5c0e377 | 2019-07-23 16:13:17 +0200 | [diff] [blame] | 3760 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 3761 | |
| 3762 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 3763 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u |
| 3764 | #else |
| 3765 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u |
| 3766 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 3767 | |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 3768 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 3769 | |
| 3770 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 3771 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u |
| 3772 | #else |
| 3773 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u |
| 3774 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
| 3775 | |
| 3776 | #if defined(MBEDTLS_SSL_ALPN) |
| 3777 | #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u |
| 3778 | #else |
| 3779 | #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u |
| 3780 | #endif /* MBEDTLS_SSL_ALPN */ |
| 3781 | |
| 3782 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0 |
| 3783 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1 |
| 3784 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2 |
| 3785 | #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3 |
| 3786 | |
| 3787 | #define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \ |
| 3788 | ( (uint32_t) ( \ |
| 3789 | ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \ |
| 3790 | ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \ |
| 3791 | ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \ |
| 3792 | ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \ |
| 3793 | 0u ) ) |
| 3794 | |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3795 | static unsigned char ssl_serialized_context_header[] = { |
| 3796 | MBEDTLS_VERSION_MAJOR, |
| 3797 | MBEDTLS_VERSION_MINOR, |
| 3798 | MBEDTLS_VERSION_PATCH, |
Joe Subbiani | 2194dc4 | 2021-07-14 12:31:31 +0100 | [diff] [blame] | 3799 | MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ), |
| 3800 | MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ), |
| 3801 | MBEDTLS_BYTE_2( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ), |
| 3802 | MBEDTLS_BYTE_1( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ), |
| 3803 | MBEDTLS_BYTE_0( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ), |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3804 | }; |
| 3805 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3806 | /* |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 3807 | * Serialize a full SSL context |
Manuel Pégourié-Gonnard | 00400c2 | 2019-07-10 14:58:45 +0200 | [diff] [blame] | 3808 | * |
| 3809 | * The format of the serialized data is: |
| 3810 | * (in the presentation language of TLS, RFC 8446 section 3) |
| 3811 | * |
| 3812 | * // header |
| 3813 | * opaque mbedtls_version[3]; // major, minor, patch |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3814 | * opaque context_format[5]; // version-specific field determining |
Manuel Pégourié-Gonnard | 00400c2 | 2019-07-10 14:58:45 +0200 | [diff] [blame] | 3815 | * // the format of the remaining |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3816 | * // serialized data. |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 3817 | * Note: When updating the format, remember to keep these |
| 3818 | * 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] | 3819 | * |
| 3820 | * // session sub-structure |
| 3821 | * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save() |
| 3822 | * // transform sub-structure |
| 3823 | * uint8 random[64]; // ServerHello.random+ClientHello.random |
| 3824 | * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value |
| 3825 | * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use |
| 3826 | * // fields from ssl_context |
| 3827 | * uint32 badmac_seen; // DTLS: number of records with failing MAC |
| 3828 | * uint64 in_window_top; // DTLS: last validated record seq_num |
| 3829 | * uint64 in_window; // DTLS: bitmask for replay protection |
| 3830 | * uint8 disable_datagram_packing; // DTLS: only one record per datagram |
| 3831 | * uint64 cur_out_ctr; // Record layer: outgoing sequence number |
| 3832 | * uint16 mtu; // DTLS: path mtu (max outgoing fragment size) |
| 3833 | * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol |
| 3834 | * |
| 3835 | * Note that many fields of the ssl_context or sub-structures are not |
| 3836 | * serialized, as they fall in one of the following categories: |
| 3837 | * |
| 3838 | * 1. forced value (eg in_left must be 0) |
| 3839 | * 2. pointer to dynamically-allocated memory (eg session, transform) |
| 3840 | * 3. value can be re-derived from other data (eg session keys from MS) |
| 3841 | * 4. value was temporary (eg content of input buffer) |
| 3842 | * 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] | 3843 | */ |
| 3844 | int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl, |
| 3845 | unsigned char *buf, |
| 3846 | size_t buf_len, |
| 3847 | size_t *olen ) |
| 3848 | { |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3849 | unsigned char *p = buf; |
| 3850 | size_t used = 0; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3851 | size_t session_len; |
| 3852 | int ret = 0; |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3853 | |
Manuel Pégourié-Gonnard | 1aaf669 | 2019-07-10 14:14:05 +0200 | [diff] [blame] | 3854 | /* |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3855 | * Enforce usage restrictions, see "return BAD_INPUT_DATA" in |
| 3856 | * this function's documentation. |
| 3857 | * |
| 3858 | * These are due to assumptions/limitations in the implementation. Some of |
| 3859 | * them are likely to stay (no handshake in progress) some might go away |
| 3860 | * (only DTLS) but are currently used to simplify the implementation. |
Manuel Pégourié-Gonnard | 1aaf669 | 2019-07-10 14:14:05 +0200 | [diff] [blame] | 3861 | */ |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3862 | /* The initial handshake must be over */ |
Paul Elliott | 27b0d94 | 2022-03-18 21:55:32 +0000 | [diff] [blame] | 3863 | if( mbedtls_ssl_is_handshake_over( ssl ) == 0 ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3864 | { |
| 3865 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Initial handshake isn't over" ) ); |
Manuel Pégourié-Gonnard | 1aaf669 | 2019-07-10 14:14:05 +0200 | [diff] [blame] | 3866 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3867 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3868 | if( ssl->handshake != NULL ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3869 | { |
| 3870 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Handshake isn't completed" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3871 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3872 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3873 | /* Double-check that sub-structures are indeed ready */ |
| 3874 | if( ssl->transform == NULL || ssl->session == NULL ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3875 | { |
| 3876 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Serialised structures aren't ready" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3877 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3878 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3879 | /* There must be no pending incoming or outgoing data */ |
| 3880 | if( mbedtls_ssl_check_pending( ssl ) != 0 ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3881 | { |
| 3882 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending incoming data" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3883 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3884 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3885 | if( ssl->out_left != 0 ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3886 | { |
| 3887 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending outgoing data" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3888 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3889 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3890 | /* Protocol must be DLTS, not TLS */ |
| 3891 | if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3892 | { |
| 3893 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only DTLS is supported" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3894 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3895 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3896 | /* Version must be 1.2 */ |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 3897 | if( ssl->tls_version != MBEDTLS_SSL_VERSION_TLS1_2 ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3898 | { |
| 3899 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only version 1.2 supported" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3900 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3901 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3902 | /* We must be using an AEAD ciphersuite */ |
| 3903 | if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3904 | { |
| 3905 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only AEAD ciphersuites supported" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3906 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3907 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3908 | /* Renegotiation must not be enabled */ |
| 3909 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 3910 | if( ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3911 | { |
| 3912 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Renegotiation must not be enabled" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3913 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3914 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3915 | #endif |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 3916 | |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3917 | /* |
| 3918 | * Version and format identifier |
| 3919 | */ |
| 3920 | used += sizeof( ssl_serialized_context_header ); |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 3921 | |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3922 | if( used <= buf_len ) |
| 3923 | { |
| 3924 | memcpy( p, ssl_serialized_context_header, |
| 3925 | sizeof( ssl_serialized_context_header ) ); |
| 3926 | p += sizeof( ssl_serialized_context_header ); |
| 3927 | } |
| 3928 | |
| 3929 | /* |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3930 | * Session (length + data) |
| 3931 | */ |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 3932 | 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] | 3933 | if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ) |
| 3934 | return( ret ); |
| 3935 | |
| 3936 | used += 4 + session_len; |
| 3937 | if( used <= buf_len ) |
| 3938 | { |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 3939 | MBEDTLS_PUT_UINT32_BE( session_len, p, 0 ); |
| 3940 | p += 4; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3941 | |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 3942 | ret = ssl_session_save( ssl->session, 1, |
| 3943 | p, session_len, &session_len ); |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3944 | if( ret != 0 ) |
| 3945 | return( ret ); |
| 3946 | |
| 3947 | p += session_len; |
| 3948 | } |
| 3949 | |
| 3950 | /* |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 3951 | * Transform |
| 3952 | */ |
| 3953 | used += sizeof( ssl->transform->randbytes ); |
| 3954 | if( used <= buf_len ) |
| 3955 | { |
| 3956 | memcpy( p, ssl->transform->randbytes, |
| 3957 | sizeof( ssl->transform->randbytes ) ); |
| 3958 | p += sizeof( ssl->transform->randbytes ); |
| 3959 | } |
| 3960 | |
| 3961 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 3962 | used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len; |
| 3963 | if( used <= buf_len ) |
| 3964 | { |
| 3965 | *p++ = ssl->transform->in_cid_len; |
| 3966 | memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len ); |
| 3967 | p += ssl->transform->in_cid_len; |
| 3968 | |
| 3969 | *p++ = ssl->transform->out_cid_len; |
| 3970 | memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len ); |
| 3971 | p += ssl->transform->out_cid_len; |
| 3972 | } |
| 3973 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 3974 | |
| 3975 | /* |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3976 | * Saved fields from top-level ssl_context structure |
| 3977 | */ |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3978 | used += 4; |
| 3979 | if( used <= buf_len ) |
| 3980 | { |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 3981 | MBEDTLS_PUT_UINT32_BE( ssl->badmac_seen, p, 0 ); |
| 3982 | p += 4; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3983 | } |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3984 | |
| 3985 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 3986 | used += 16; |
| 3987 | if( used <= buf_len ) |
| 3988 | { |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 3989 | MBEDTLS_PUT_UINT64_BE( ssl->in_window_top, p, 0 ); |
| 3990 | p += 8; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3991 | |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 3992 | MBEDTLS_PUT_UINT64_BE( ssl->in_window, p, 0 ); |
| 3993 | p += 8; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3994 | } |
| 3995 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
| 3996 | |
| 3997 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3998 | used += 1; |
| 3999 | if( used <= buf_len ) |
| 4000 | { |
| 4001 | *p++ = ssl->disable_datagram_packing; |
| 4002 | } |
| 4003 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 4004 | |
Jerry Yu | ae0b2e2 | 2021-10-08 15:21:19 +0800 | [diff] [blame] | 4005 | used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4006 | if( used <= buf_len ) |
| 4007 | { |
Jerry Yu | ae0b2e2 | 2021-10-08 15:21:19 +0800 | [diff] [blame] | 4008 | memcpy( p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN ); |
| 4009 | p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4010 | } |
| 4011 | |
| 4012 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4013 | used += 2; |
| 4014 | if( used <= buf_len ) |
| 4015 | { |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 4016 | MBEDTLS_PUT_UINT16_BE( ssl->mtu, p, 0 ); |
| 4017 | p += 2; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4018 | } |
| 4019 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 4020 | |
| 4021 | #if defined(MBEDTLS_SSL_ALPN) |
| 4022 | { |
| 4023 | const uint8_t alpn_len = ssl->alpn_chosen |
Manuel Pégourié-Gonnard | f041f4e | 2019-07-24 00:58:27 +0200 | [diff] [blame] | 4024 | ? (uint8_t) strlen( ssl->alpn_chosen ) |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4025 | : 0; |
| 4026 | |
| 4027 | used += 1 + alpn_len; |
| 4028 | if( used <= buf_len ) |
| 4029 | { |
| 4030 | *p++ = alpn_len; |
| 4031 | |
| 4032 | if( ssl->alpn_chosen != NULL ) |
| 4033 | { |
| 4034 | memcpy( p, ssl->alpn_chosen, alpn_len ); |
| 4035 | p += alpn_len; |
| 4036 | } |
| 4037 | } |
| 4038 | } |
| 4039 | #endif /* MBEDTLS_SSL_ALPN */ |
| 4040 | |
| 4041 | /* |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4042 | * Done |
| 4043 | */ |
| 4044 | *olen = used; |
| 4045 | |
| 4046 | if( used > buf_len ) |
| 4047 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 4048 | |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4049 | MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used ); |
| 4050 | |
Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 4051 | return( mbedtls_ssl_session_reset_int( ssl, 0 ) ); |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 4052 | } |
| 4053 | |
| 4054 | /* |
Manuel Pégourié-Gonnard | b9dfc9f | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 4055 | * Deserialize context, see mbedtls_ssl_context_save() for format. |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4056 | * |
| 4057 | * This internal version is wrapped by a public function that cleans up in |
| 4058 | * case of error. |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 4059 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 4060 | MBEDTLS_CHECK_RETURN_CRITICAL |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4061 | static int ssl_context_load( mbedtls_ssl_context *ssl, |
| 4062 | const unsigned char *buf, |
| 4063 | size_t len ) |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 4064 | { |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4065 | const unsigned char *p = buf; |
| 4066 | const unsigned char * const end = buf + len; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4067 | size_t session_len; |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 4068 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Andrzej Kurek | 2d59dbc | 2022-10-13 08:34:38 -0400 | [diff] [blame] | 4069 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Andrzej Kurek | 894edde | 2022-09-29 06:31:14 -0400 | [diff] [blame] | 4070 | tls_prf_fn prf_func = NULL; |
Andrzej Kurek | 2d59dbc | 2022-10-13 08:34:38 -0400 | [diff] [blame] | 4071 | #endif |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4072 | |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 4073 | /* |
| 4074 | * The context should have been freshly setup or reset. |
| 4075 | * Give the user an error in case of obvious misuse. |
Manuel Pégourié-Gonnard | 4ca930f | 2019-07-26 16:31:53 +0200 | [diff] [blame] | 4076 | * (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] | 4077 | * renegotiating, or if the user mistakenly loaded a session first.) |
| 4078 | */ |
| 4079 | if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST || |
| 4080 | ssl->session != NULL ) |
| 4081 | { |
| 4082 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4083 | } |
| 4084 | |
| 4085 | /* |
| 4086 | * We can't check that the config matches the initial one, but we can at |
| 4087 | * least check it matches the requirements for serializing. |
| 4088 | */ |
| 4089 | if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM || |
Glenn Strauss | 2dfcea2 | 2022-03-14 17:26:42 -0400 | [diff] [blame] | 4090 | ssl->conf->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2 || |
| 4091 | ssl->conf->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 || |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 4092 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | 9a96fd7 | 2019-07-23 17:11:24 +0200 | [diff] [blame] | 4093 | ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED || |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 4094 | #endif |
Manuel Pégourié-Gonnard | 9a96fd7 | 2019-07-23 17:11:24 +0200 | [diff] [blame] | 4095 | 0 ) |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 4096 | { |
| 4097 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4098 | } |
| 4099 | |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4100 | MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len ); |
| 4101 | |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4102 | /* |
| 4103 | * Check version identifier |
| 4104 | */ |
| 4105 | if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) ) |
| 4106 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4107 | |
| 4108 | if( memcmp( p, ssl_serialized_context_header, |
| 4109 | sizeof( ssl_serialized_context_header ) ) != 0 ) |
| 4110 | { |
| 4111 | return( MBEDTLS_ERR_SSL_VERSION_MISMATCH ); |
| 4112 | } |
| 4113 | p += sizeof( ssl_serialized_context_header ); |
| 4114 | |
| 4115 | /* |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4116 | * Session |
| 4117 | */ |
| 4118 | if( (size_t)( end - p ) < 4 ) |
| 4119 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4120 | |
| 4121 | session_len = ( (size_t) p[0] << 24 ) | |
| 4122 | ( (size_t) p[1] << 16 ) | |
| 4123 | ( (size_t) p[2] << 8 ) | |
| 4124 | ( (size_t) p[3] ); |
| 4125 | p += 4; |
| 4126 | |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 4127 | /* This has been allocated by ssl_handshake_init(), called by |
Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 4128 | * 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] | 4129 | ssl->session = ssl->session_negotiate; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4130 | ssl->session_in = ssl->session; |
| 4131 | ssl->session_out = ssl->session; |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 4132 | ssl->session_negotiate = NULL; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4133 | |
| 4134 | if( (size_t)( end - p ) < session_len ) |
| 4135 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4136 | |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 4137 | ret = ssl_session_load( ssl->session, 1, p, session_len ); |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4138 | if( ret != 0 ) |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 4139 | { |
| 4140 | mbedtls_ssl_session_free( ssl->session ); |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4141 | return( ret ); |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 4142 | } |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4143 | |
| 4144 | p += session_len; |
| 4145 | |
| 4146 | /* |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4147 | * Transform |
| 4148 | */ |
| 4149 | |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 4150 | /* This has been allocated by ssl_handshake_init(), called by |
Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 4151 | * 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] | 4152 | ssl->transform = ssl->transform_negotiate; |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4153 | ssl->transform_in = ssl->transform; |
| 4154 | ssl->transform_out = ssl->transform; |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 4155 | ssl->transform_negotiate = NULL; |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4156 | |
Andrzej Kurek | 2d59dbc | 2022-10-13 08:34:38 -0400 | [diff] [blame] | 4157 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Andrzej Kurek | 894edde | 2022-09-29 06:31:14 -0400 | [diff] [blame] | 4158 | prf_func = ssl_tls12prf_from_cs( ssl->session->ciphersuite ); |
| 4159 | if( prf_func == NULL ) |
| 4160 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4161 | |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4162 | /* Read random bytes and populate structure */ |
| 4163 | if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) ) |
| 4164 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Andrzej Kurek | 2d59dbc | 2022-10-13 08:34:38 -0400 | [diff] [blame] | 4165 | |
Hanno Becker | bd25755 | 2021-03-22 06:59:27 +0000 | [diff] [blame] | 4166 | ret = ssl_tls12_populate_transform( ssl->transform, |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4167 | ssl->session->ciphersuite, |
| 4168 | ssl->session->master, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 4169 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4170 | ssl->session->encrypt_then_mac, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 4171 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Andrzej Kurek | 894edde | 2022-09-29 06:31:14 -0400 | [diff] [blame] | 4172 | prf_func, |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4173 | p, /* currently pointing to randbytes */ |
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 4174 | 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] | 4175 | ssl->conf->endpoint, |
| 4176 | ssl ); |
| 4177 | if( ret != 0 ) |
| 4178 | return( ret ); |
Jerry Yu | 840fbb2 | 2022-02-17 14:59:29 +0800 | [diff] [blame] | 4179 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4180 | p += sizeof( ssl->transform->randbytes ); |
| 4181 | |
| 4182 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 4183 | /* Read connection IDs and store them */ |
| 4184 | if( (size_t)( end - p ) < 1 ) |
| 4185 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4186 | |
| 4187 | ssl->transform->in_cid_len = *p++; |
| 4188 | |
Manuel Pégourié-Gonnard | 5ea13b8 | 2019-07-23 15:02:54 +0200 | [diff] [blame] | 4189 | 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] | 4190 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4191 | |
| 4192 | memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len ); |
| 4193 | p += ssl->transform->in_cid_len; |
| 4194 | |
| 4195 | ssl->transform->out_cid_len = *p++; |
| 4196 | |
| 4197 | if( (size_t)( end - p ) < ssl->transform->out_cid_len ) |
| 4198 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4199 | |
| 4200 | memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len ); |
| 4201 | p += ssl->transform->out_cid_len; |
| 4202 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 4203 | |
| 4204 | /* |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4205 | * Saved fields from top-level ssl_context structure |
| 4206 | */ |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4207 | if( (size_t)( end - p ) < 4 ) |
| 4208 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4209 | |
| 4210 | ssl->badmac_seen = ( (uint32_t) p[0] << 24 ) | |
| 4211 | ( (uint32_t) p[1] << 16 ) | |
| 4212 | ( (uint32_t) p[2] << 8 ) | |
| 4213 | ( (uint32_t) p[3] ); |
| 4214 | p += 4; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4215 | |
| 4216 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 4217 | if( (size_t)( end - p ) < 16 ) |
| 4218 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4219 | |
| 4220 | ssl->in_window_top = ( (uint64_t) p[0] << 56 ) | |
| 4221 | ( (uint64_t) p[1] << 48 ) | |
| 4222 | ( (uint64_t) p[2] << 40 ) | |
| 4223 | ( (uint64_t) p[3] << 32 ) | |
| 4224 | ( (uint64_t) p[4] << 24 ) | |
| 4225 | ( (uint64_t) p[5] << 16 ) | |
| 4226 | ( (uint64_t) p[6] << 8 ) | |
| 4227 | ( (uint64_t) p[7] ); |
| 4228 | p += 8; |
| 4229 | |
| 4230 | ssl->in_window = ( (uint64_t) p[0] << 56 ) | |
| 4231 | ( (uint64_t) p[1] << 48 ) | |
| 4232 | ( (uint64_t) p[2] << 40 ) | |
| 4233 | ( (uint64_t) p[3] << 32 ) | |
| 4234 | ( (uint64_t) p[4] << 24 ) | |
| 4235 | ( (uint64_t) p[5] << 16 ) | |
| 4236 | ( (uint64_t) p[6] << 8 ) | |
| 4237 | ( (uint64_t) p[7] ); |
| 4238 | p += 8; |
| 4239 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
| 4240 | |
| 4241 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4242 | if( (size_t)( end - p ) < 1 ) |
| 4243 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4244 | |
| 4245 | ssl->disable_datagram_packing = *p++; |
| 4246 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 4247 | |
Jerry Yu | d9a94fe | 2021-09-28 18:58:59 +0800 | [diff] [blame] | 4248 | if( (size_t)( end - p ) < sizeof( ssl->cur_out_ctr ) ) |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4249 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jerry Yu | d9a94fe | 2021-09-28 18:58:59 +0800 | [diff] [blame] | 4250 | memcpy( ssl->cur_out_ctr, p, sizeof( ssl->cur_out_ctr ) ); |
| 4251 | p += sizeof( ssl->cur_out_ctr ); |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4252 | |
| 4253 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4254 | if( (size_t)( end - p ) < 2 ) |
| 4255 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4256 | |
| 4257 | ssl->mtu = ( p[0] << 8 ) | p[1]; |
| 4258 | p += 2; |
| 4259 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 4260 | |
| 4261 | #if defined(MBEDTLS_SSL_ALPN) |
| 4262 | { |
| 4263 | uint8_t alpn_len; |
| 4264 | const char **cur; |
| 4265 | |
| 4266 | if( (size_t)( end - p ) < 1 ) |
| 4267 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4268 | |
| 4269 | alpn_len = *p++; |
| 4270 | |
| 4271 | if( alpn_len != 0 && ssl->conf->alpn_list != NULL ) |
| 4272 | { |
| 4273 | /* alpn_chosen should point to an item in the configured list */ |
| 4274 | for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ ) |
| 4275 | { |
| 4276 | if( strlen( *cur ) == alpn_len && |
| 4277 | memcmp( p, cur, alpn_len ) == 0 ) |
| 4278 | { |
| 4279 | ssl->alpn_chosen = *cur; |
| 4280 | break; |
| 4281 | } |
| 4282 | } |
| 4283 | } |
| 4284 | |
| 4285 | /* can only happen on conf mismatch */ |
| 4286 | if( alpn_len != 0 && ssl->alpn_chosen == NULL ) |
| 4287 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4288 | |
| 4289 | p += alpn_len; |
| 4290 | } |
| 4291 | #endif /* MBEDTLS_SSL_ALPN */ |
| 4292 | |
| 4293 | /* |
Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 4294 | * Forced fields from top-level ssl_context structure |
| 4295 | * |
| 4296 | * Most of them already set to the correct value by mbedtls_ssl_init() and |
| 4297 | * mbedtls_ssl_reset(), so we only need to set the remaining ones. |
| 4298 | */ |
| 4299 | ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER; |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 4300 | ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 4301 | |
Hanno Becker | 361b10d | 2019-08-30 10:42:49 +0100 | [diff] [blame] | 4302 | /* Adjust pointers for header fields of outgoing records to |
| 4303 | * the given transform, accounting for explicit IV and CID. */ |
Hanno Becker | 3e6f8ab | 2020-02-05 10:40:57 +0000 | [diff] [blame] | 4304 | mbedtls_ssl_update_out_pointers( ssl, ssl->transform ); |
Hanno Becker | 361b10d | 2019-08-30 10:42:49 +0100 | [diff] [blame] | 4305 | |
Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 4306 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4307 | ssl->in_epoch = 1; |
| 4308 | #endif |
| 4309 | |
| 4310 | /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated, |
| 4311 | * 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] | 4312 | * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform() |
| 4313 | * inappropriately. */ |
Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 4314 | if( ssl->handshake != NULL ) |
| 4315 | { |
| 4316 | mbedtls_ssl_handshake_free( ssl ); |
| 4317 | mbedtls_free( ssl->handshake ); |
| 4318 | ssl->handshake = NULL; |
| 4319 | } |
| 4320 | |
| 4321 | /* |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4322 | * Done - should have consumed entire buffer |
| 4323 | */ |
| 4324 | if( p != end ) |
| 4325 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 4326 | |
| 4327 | return( 0 ); |
| 4328 | } |
| 4329 | |
| 4330 | /* |
Manuel Pégourié-Gonnard | b9dfc9f | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 4331 | * Deserialize context: public wrapper for error cleaning |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4332 | */ |
| 4333 | int mbedtls_ssl_context_load( mbedtls_ssl_context *context, |
| 4334 | const unsigned char *buf, |
| 4335 | size_t len ) |
| 4336 | { |
| 4337 | int ret = ssl_context_load( context, buf, len ); |
| 4338 | |
| 4339 | if( ret != 0 ) |
| 4340 | mbedtls_ssl_free( context ); |
| 4341 | |
| 4342 | return( ret ); |
| 4343 | } |
Manuel Pégourié-Gonnard | 5c0e377 | 2019-07-23 16:13:17 +0200 | [diff] [blame] | 4344 | #endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */ |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4345 | |
| 4346 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4347 | * Free an SSL context |
| 4348 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4349 | void mbedtls_ssl_free( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4350 | { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 4351 | if( ssl == NULL ) |
| 4352 | return; |
| 4353 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4354 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4355 | |
Manuel Pégourié-Gonnard | 7ee6f0e | 2014-02-13 10:54:07 +0100 | [diff] [blame] | 4356 | if( ssl->out_buf != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4357 | { |
sander-visser | b8aa207 | 2020-05-06 22:05:13 +0200 | [diff] [blame] | 4358 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 4359 | size_t out_buf_len = ssl->out_buf_len; |
| 4360 | #else |
| 4361 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; |
| 4362 | #endif |
| 4363 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 4364 | mbedtls_platform_zeroize( ssl->out_buf, out_buf_len ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4365 | mbedtls_free( ssl->out_buf ); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 4366 | ssl->out_buf = NULL; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4367 | } |
| 4368 | |
Manuel Pégourié-Gonnard | 7ee6f0e | 2014-02-13 10:54:07 +0100 | [diff] [blame] | 4369 | if( ssl->in_buf != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4370 | { |
sander-visser | b8aa207 | 2020-05-06 22:05:13 +0200 | [diff] [blame] | 4371 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 4372 | size_t in_buf_len = ssl->in_buf_len; |
| 4373 | #else |
| 4374 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; |
| 4375 | #endif |
| 4376 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 4377 | mbedtls_platform_zeroize( ssl->in_buf, in_buf_len ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4378 | mbedtls_free( ssl->in_buf ); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 4379 | ssl->in_buf = NULL; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4380 | } |
| 4381 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4382 | if( ssl->transform ) |
| 4383 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4384 | mbedtls_ssl_transform_free( ssl->transform ); |
| 4385 | mbedtls_free( ssl->transform ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4386 | } |
| 4387 | |
| 4388 | if( ssl->handshake ) |
| 4389 | { |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 4390 | mbedtls_ssl_handshake_free( ssl ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4391 | mbedtls_ssl_transform_free( ssl->transform_negotiate ); |
| 4392 | mbedtls_ssl_session_free( ssl->session_negotiate ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4393 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4394 | mbedtls_free( ssl->handshake ); |
| 4395 | mbedtls_free( ssl->transform_negotiate ); |
| 4396 | mbedtls_free( ssl->session_negotiate ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4397 | } |
| 4398 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4399 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Hanno Becker | 3aa186f | 2021-08-10 09:24:19 +0100 | [diff] [blame] | 4400 | mbedtls_ssl_transform_free( ssl->transform_application ); |
| 4401 | mbedtls_free( ssl->transform_application ); |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4402 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | 3aa186f | 2021-08-10 09:24:19 +0100 | [diff] [blame] | 4403 | |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 4404 | if( ssl->session ) |
| 4405 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4406 | mbedtls_ssl_session_free( ssl->session ); |
| 4407 | mbedtls_free( ssl->session ); |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 4408 | } |
| 4409 | |
Manuel Pégourié-Gonnard | 55fab2d | 2015-05-11 16:15:19 +0200 | [diff] [blame] | 4410 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 4411 | if( ssl->hostname != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4412 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 4413 | mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4414 | mbedtls_free( ssl->hostname ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4415 | } |
Paul Bakker | 0be444a | 2013-08-27 21:55:01 +0200 | [diff] [blame] | 4416 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4417 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 4418 | #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] | 4419 | mbedtls_free( ssl->cli_id ); |
Manuel Pégourié-Gonnard | 43c0218 | 2014-07-22 17:32:01 +0200 | [diff] [blame] | 4420 | #endif |
| 4421 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4422 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) ); |
Paul Bakker | 2da561c | 2009-02-05 18:00:28 +0000 | [diff] [blame] | 4423 | |
Paul Bakker | 86f04f4 | 2013-02-14 11:20:09 +0100 | [diff] [blame] | 4424 | /* Actually clear after last debug message */ |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 4425 | mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4426 | } |
| 4427 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4428 | /* |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 4429 | * Initialize mbedtls_ssl_config |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4430 | */ |
| 4431 | void mbedtls_ssl_config_init( mbedtls_ssl_config *conf ) |
| 4432 | { |
| 4433 | memset( conf, 0, sizeof( mbedtls_ssl_config ) ); |
| 4434 | } |
| 4435 | |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4436 | /* The selection should be the same as mbedtls_x509_crt_profile_default in |
| 4437 | * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters: |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4438 | * curves with a lower resource usage come first. |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4439 | * See the documentation of mbedtls_ssl_conf_curves() for what we promise |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4440 | * about this list. |
| 4441 | */ |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4442 | static uint16_t ssl_preset_default_groups[] = { |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4443 | #if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4444 | MBEDTLS_SSL_IANA_TLS_GROUP_X25519, |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4445 | #endif |
| 4446 | #if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4447 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1, |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4448 | #endif |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4449 | #if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4450 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4451 | #endif |
| 4452 | #if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4453 | MBEDTLS_SSL_IANA_TLS_GROUP_X448, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4454 | #endif |
| 4455 | #if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4456 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4457 | #endif |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4458 | #if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4459 | MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1, |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4460 | #endif |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4461 | #if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4462 | MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4463 | #endif |
| 4464 | #if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4465 | MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4466 | #endif |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4467 | MBEDTLS_SSL_IANA_TLS_GROUP_NONE |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4468 | }; |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4469 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4470 | static int ssl_preset_suiteb_ciphersuites[] = { |
| 4471 | MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, |
| 4472 | MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, |
| 4473 | 0 |
| 4474 | }; |
| 4475 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 4476 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Hanno Becker | 9c6aa7b | 2021-08-10 13:50:43 +0100 | [diff] [blame] | 4477 | |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4478 | /* NOTICE: |
Jerry Yu | 0b994b8 | 2022-01-25 17:22:12 +0800 | [diff] [blame] | 4479 | * 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] | 4480 | * rules SHOULD be upheld. |
| 4481 | * - No duplicate entries. |
| 4482 | * - 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] | 4483 | * - ssl_tls12_preset* is for TLS 1.2 use only. |
Jerry Yu | 370e146 | 2022-01-25 10:36:53 +0800 | [diff] [blame] | 4484 | * - 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] | 4485 | */ |
Hanno Becker | 9c6aa7b | 2021-08-10 13:50:43 +0100 | [diff] [blame] | 4486 | static uint16_t ssl_preset_default_sig_algs[] = { |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4487 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4488 | #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] | 4489 | defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) |
| 4490 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256, |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4491 | #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] | 4492 | MBEDTLS_ECP_DP_SECP256R1_ENABLED */ |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4493 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4494 | #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] | 4495 | defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) |
| 4496 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384, |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4497 | #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] | 4498 | MBEDTLS_ECP_DP_SECP384R1_ENABLED */ |
| 4499 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4500 | #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] | 4501 | defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) |
| 4502 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512, |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4503 | #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] | 4504 | MBEDTLS_ECP_DP_SECP521R1_ENABLED */ |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4505 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4506 | #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] | 4507 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512, |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4508 | #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] | 4509 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4510 | #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] | 4511 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384, |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4512 | #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] | 4513 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4514 | #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] | 4515 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256, |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4516 | #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] | 4517 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4518 | #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] | 4519 | MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512, |
| 4520 | #endif /* MBEDTLS_RSA_C && MBEDTLS_SHA512_C */ |
| 4521 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4522 | #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] | 4523 | MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384, |
| 4524 | #endif /* MBEDTLS_RSA_C && MBEDTLS_SHA384_C */ |
| 4525 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4526 | #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] | 4527 | MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256, |
| 4528 | #endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */ |
| 4529 | |
Gabor Mezei | 15b95a6 | 2022-05-09 16:37:58 +0200 | [diff] [blame] | 4530 | MBEDTLS_TLS_SIG_NONE |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4531 | }; |
| 4532 | |
| 4533 | /* NOTICE: see above */ |
| 4534 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 4535 | static uint16_t ssl_tls12_preset_default_sig_algs[] = { |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4536 | #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] | 4537 | #if defined(MBEDTLS_ECDSA_C) |
| 4538 | 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] | 4539 | #endif |
Jerry Yu | 09a99fc | 2022-07-28 14:22:17 +0800 | [diff] [blame] | 4540 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) |
| 4541 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512, |
| 4542 | #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */ |
Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 4543 | #if defined(MBEDTLS_RSA_C) |
| 4544 | MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA512 ), |
| 4545 | #endif |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4546 | #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] | 4547 | #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] | 4548 | #if defined(MBEDTLS_ECDSA_C) |
| 4549 | 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] | 4550 | #endif |
Jerry Yu | 09a99fc | 2022-07-28 14:22:17 +0800 | [diff] [blame] | 4551 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) |
| 4552 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384, |
| 4553 | #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */ |
Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 4554 | #if defined(MBEDTLS_RSA_C) |
| 4555 | MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ), |
| 4556 | #endif |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4557 | #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] | 4558 | #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] | 4559 | #if defined(MBEDTLS_ECDSA_C) |
| 4560 | 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] | 4561 | #endif |
Jerry Yu | 09a99fc | 2022-07-28 14:22:17 +0800 | [diff] [blame] | 4562 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) |
| 4563 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256, |
| 4564 | #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */ |
Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 4565 | #if defined(MBEDTLS_RSA_C) |
| 4566 | MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ), |
| 4567 | #endif |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4568 | #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] | 4569 | MBEDTLS_TLS_SIG_NONE |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4570 | }; |
| 4571 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 4572 | /* NOTICE: see above */ |
| 4573 | static uint16_t ssl_preset_suiteb_sig_algs[] = { |
| 4574 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4575 | #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] | 4576 | defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) |
| 4577 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256, |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4578 | #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] | 4579 | MBEDTLS_ECP_DP_SECP256R1_ENABLED */ |
| 4580 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4581 | #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] | 4582 | defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) |
| 4583 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384, |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4584 | #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] | 4585 | MBEDTLS_ECP_DP_SECP384R1_ENABLED */ |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4586 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4587 | #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] | 4588 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256, |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4589 | #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] | 4590 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4591 | #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] | 4592 | MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256, |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4593 | #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] | 4594 | |
Gabor Mezei | 15b95a6 | 2022-05-09 16:37:58 +0200 | [diff] [blame] | 4595 | MBEDTLS_TLS_SIG_NONE |
Jerry Yu | 713013f | 2022-01-17 18:16:35 +0800 | [diff] [blame] | 4596 | }; |
Jerry Yu | 6106fdc | 2022-01-12 16:36:14 +0800 | [diff] [blame] | 4597 | |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4598 | /* NOTICE: see above */ |
| 4599 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 4600 | static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = { |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4601 | #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] | 4602 | #if defined(MBEDTLS_ECDSA_C) |
| 4603 | 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] | 4604 | #endif |
Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 4605 | #if defined(MBEDTLS_RSA_C) |
| 4606 | MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ), |
| 4607 | #endif |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4608 | #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] | 4609 | #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] | 4610 | #if defined(MBEDTLS_ECDSA_C) |
| 4611 | 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] | 4612 | #endif |
Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 4613 | #if defined(MBEDTLS_RSA_C) |
| 4614 | MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ), |
| 4615 | #endif |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4616 | #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] | 4617 | MBEDTLS_TLS_SIG_NONE |
Hanno Becker | 9c6aa7b | 2021-08-10 13:50:43 +0100 | [diff] [blame] | 4618 | }; |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4619 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 4620 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 4621 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4622 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4623 | static uint16_t ssl_preset_suiteb_groups[] = { |
Jaeden Amero | d431104 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 4624 | #if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4625 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1, |
Jaeden Amero | d431104 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 4626 | #endif |
| 4627 | #if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4628 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1, |
Jaeden Amero | d431104 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 4629 | #endif |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4630 | MBEDTLS_SSL_IANA_TLS_GROUP_NONE |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4631 | }; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4632 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 4633 | #if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4634 | /* 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] | 4635 | * to make sure there are no duplicated signature algorithm entries. */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 4636 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 4637 | static int ssl_check_no_sig_alg_duplication( uint16_t * sig_algs ) |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4638 | { |
| 4639 | size_t i, j; |
| 4640 | int ret = 0; |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4641 | |
Gabor Mezei | 15b95a6 | 2022-05-09 16:37:58 +0200 | [diff] [blame] | 4642 | for( i = 0; sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ ) |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4643 | { |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 4644 | for( j = 0; j < i; j++ ) |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4645 | { |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 4646 | if( sig_algs[i] != sig_algs[j] ) |
| 4647 | continue; |
| 4648 | mbedtls_printf( " entry(%04x,%" MBEDTLS_PRINTF_SIZET |
| 4649 | ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n", |
| 4650 | sig_algs[i], j, i ); |
| 4651 | ret = -1; |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4652 | } |
| 4653 | } |
| 4654 | return( ret ); |
| 4655 | } |
| 4656 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 4657 | #endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4658 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4659 | /* |
Tillmann Karras | 588ad50 | 2015-09-25 04:27:22 +0200 | [diff] [blame] | 4660 | * Load default in mbedtls_ssl_config |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4661 | */ |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 4662 | int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4663 | int endpoint, int transport, int preset ) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4664 | { |
Manuel Pégourié-Gonnard | 8b431fb | 2015-05-11 12:54:52 +0200 | [diff] [blame] | 4665 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 4666 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 8b431fb | 2015-05-11 12:54:52 +0200 | [diff] [blame] | 4667 | #endif |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4668 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 4669 | #if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 4670 | if( ssl_check_no_sig_alg_duplication( ssl_preset_suiteb_sig_algs ) ) |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4671 | { |
| 4672 | mbedtls_printf( "ssl_preset_suiteb_sig_algs has duplicated entries\n" ); |
| 4673 | return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED ); |
| 4674 | } |
| 4675 | |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 4676 | if( ssl_check_no_sig_alg_duplication( ssl_preset_default_sig_algs ) ) |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4677 | { |
| 4678 | mbedtls_printf( "ssl_preset_default_sig_algs has duplicated entries\n" ); |
| 4679 | return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED ); |
| 4680 | } |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4681 | |
| 4682 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 4683 | 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] | 4684 | { |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4685 | mbedtls_printf( "ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n" ); |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4686 | return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED ); |
| 4687 | } |
| 4688 | |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 4689 | 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] | 4690 | { |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4691 | mbedtls_printf( "ssl_tls12_preset_default_sig_algs has duplicated entries\n" ); |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4692 | return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED ); |
| 4693 | } |
| 4694 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 4695 | #endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4696 | |
Manuel Pégourié-Gonnard | 0de074f | 2015-05-14 12:58:01 +0200 | [diff] [blame] | 4697 | /* Use the functions here so that they are covered in tests, |
| 4698 | * but otherwise access member directly for efficiency */ |
| 4699 | mbedtls_ssl_conf_endpoint( conf, endpoint ); |
| 4700 | mbedtls_ssl_conf_transport( conf, transport ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4701 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4702 | /* |
| 4703 | * Things that are common to all presets |
| 4704 | */ |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 4705 | #if defined(MBEDTLS_SSL_CLI_C) |
| 4706 | if( endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 4707 | { |
| 4708 | conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED; |
| 4709 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 4710 | conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED; |
| 4711 | #endif |
| 4712 | } |
| 4713 | #endif |
| 4714 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4715 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 4716 | conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED; |
| 4717 | #endif |
| 4718 | |
| 4719 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 4720 | conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED; |
| 4721 | #endif |
| 4722 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 4723 | #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] | 4724 | conf->f_cookie_write = ssl_cookie_write_dummy; |
| 4725 | conf->f_cookie_check = ssl_cookie_check_dummy; |
| 4726 | #endif |
| 4727 | |
| 4728 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 4729 | conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED; |
| 4730 | #endif |
| 4731 | |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 4732 | #if defined(MBEDTLS_SSL_SRV_C) |
| 4733 | conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED; |
TRodziewicz | 3946f79 | 2021-06-14 12:11:18 +0200 | [diff] [blame] | 4734 | conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER; |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 4735 | #endif |
| 4736 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4737 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4738 | conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN; |
| 4739 | conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX; |
| 4740 | #endif |
| 4741 | |
| 4742 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 4743 | conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT; |
Andres AG | 2196c7f | 2016-12-15 17:01:16 +0000 | [diff] [blame] | 4744 | memset( conf->renego_period, 0x00, 2 ); |
| 4745 | memset( conf->renego_period + 2, 0xFF, 6 ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4746 | #endif |
| 4747 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4748 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
Hanno Becker | e2defad | 2021-07-24 05:59:17 +0100 | [diff] [blame] | 4749 | if( endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 4750 | { |
| 4751 | const unsigned char dhm_p[] = |
| 4752 | MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN; |
| 4753 | const unsigned char dhm_g[] = |
| 4754 | MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN; |
Hanno Becker | 00d0a68 | 2017-10-04 13:14:29 +0100 | [diff] [blame] | 4755 | |
Hanno Becker | e2defad | 2021-07-24 05:59:17 +0100 | [diff] [blame] | 4756 | if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf, |
| 4757 | dhm_p, sizeof( dhm_p ), |
| 4758 | dhm_g, sizeof( dhm_g ) ) ) != 0 ) |
| 4759 | { |
| 4760 | return( ret ); |
| 4761 | } |
| 4762 | } |
Manuel Pégourié-Gonnard | bd990d6 | 2015-06-11 14:49:42 +0200 | [diff] [blame] | 4763 | #endif |
| 4764 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4765 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | d0766ec | 2022-09-22 10:46:57 +0800 | [diff] [blame] | 4766 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SESSION_TICKETS) |
Jerry Yu | 1ad7ace | 2022-08-09 13:28:39 +0800 | [diff] [blame] | 4767 | mbedtls_ssl_conf_new_session_tickets( |
| 4768 | conf, MBEDTLS_SSL_TLS1_3_DEFAULT_NEW_SESSION_TICKETS ); |
| 4769 | #endif |
Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 4770 | /* |
| 4771 | * Allow all TLS 1.3 key exchange modes by default. |
| 4772 | */ |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 4773 | conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL; |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4774 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 4775 | |
XiaokangQian | 4d3a604 | 2022-04-21 13:46:17 +0000 | [diff] [blame] | 4776 | if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 4777 | { |
Glenn Strauss | 2dfcea2 | 2022-03-14 17:26:42 -0400 | [diff] [blame] | 4778 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
XiaokangQian | 4d3a604 | 2022-04-21 13:46:17 +0000 | [diff] [blame] | 4779 | conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
XiaokangQian | 060d867 | 2022-04-21 09:24:56 +0000 | [diff] [blame] | 4780 | conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
XiaokangQian | 4d3a604 | 2022-04-21 13:46:17 +0000 | [diff] [blame] | 4781 | #else |
XiaokangQian | 060d867 | 2022-04-21 09:24:56 +0000 | [diff] [blame] | 4782 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
XiaokangQian | 060d867 | 2022-04-21 09:24:56 +0000 | [diff] [blame] | 4783 | #endif |
XiaokangQian | 4d3a604 | 2022-04-21 13:46:17 +0000 | [diff] [blame] | 4784 | } |
| 4785 | else |
| 4786 | { |
| 4787 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 4788 | if( endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 4789 | { |
| 4790 | conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 4791 | conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3; |
| 4792 | } |
| 4793 | else |
| 4794 | /* Hybrid TLS 1.2 / 1.3 is not supported on server side yet */ |
| 4795 | { |
| 4796 | conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 4797 | conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 4798 | } |
| 4799 | #elif defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 4800 | conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_3; |
| 4801 | conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3; |
| 4802 | #elif defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 4803 | conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 4804 | conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 4805 | #else |
| 4806 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 4807 | #endif |
| 4808 | } |
Glenn Strauss | 2dfcea2 | 2022-03-14 17:26:42 -0400 | [diff] [blame] | 4809 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4810 | /* |
| 4811 | * Preset-specific defaults |
| 4812 | */ |
| 4813 | switch( preset ) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4814 | { |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4815 | /* |
| 4816 | * NSA Suite B |
| 4817 | */ |
| 4818 | case MBEDTLS_SSL_PRESET_SUITEB: |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4819 | |
Hanno Becker | d60b6c6 | 2021-04-29 12:04:11 +0100 | [diff] [blame] | 4820 | conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4821 | |
| 4822 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 4823 | conf->cert_profile = &mbedtls_x509_crt_profile_suiteb; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4824 | #endif |
| 4825 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 4826 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4827 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 4828 | if( mbedtls_ssl_conf_is_tls12_only( conf ) ) |
| 4829 | conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs; |
| 4830 | else |
| 4831 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 4832 | conf->sig_algs = ssl_preset_suiteb_sig_algs; |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 4833 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4834 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4835 | #if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 4836 | conf->curve_list = NULL; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4837 | #endif |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4838 | conf->group_list = ssl_preset_suiteb_groups; |
Manuel Pégourié-Gonnard | c98204e | 2015-08-11 04:21:01 +0200 | [diff] [blame] | 4839 | break; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4840 | |
| 4841 | /* |
| 4842 | * Default |
| 4843 | */ |
| 4844 | default: |
Ronald Cron | f660655 | 2022-03-15 11:23:25 +0100 | [diff] [blame] | 4845 | |
Hanno Becker | d60b6c6 | 2021-04-29 12:04:11 +0100 | [diff] [blame] | 4846 | conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites(); |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4847 | |
| 4848 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 4849 | conf->cert_profile = &mbedtls_x509_crt_profile_default; |
| 4850 | #endif |
| 4851 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 4852 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4853 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 4854 | if( mbedtls_ssl_conf_is_tls12_only( conf ) ) |
| 4855 | conf->sig_algs = ssl_tls12_preset_default_sig_algs; |
| 4856 | else |
| 4857 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 4858 | conf->sig_algs = ssl_preset_default_sig_algs; |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 4859 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4860 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4861 | #if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 4862 | conf->curve_list = NULL; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4863 | #endif |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4864 | conf->group_list = ssl_preset_default_groups; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4865 | |
| 4866 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C) |
| 4867 | conf->dhm_min_bitlen = 1024; |
| 4868 | #endif |
| 4869 | } |
| 4870 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4871 | return( 0 ); |
| 4872 | } |
| 4873 | |
| 4874 | /* |
| 4875 | * Free mbedtls_ssl_config |
| 4876 | */ |
| 4877 | void mbedtls_ssl_config_free( mbedtls_ssl_config *conf ) |
| 4878 | { |
| 4879 | #if defined(MBEDTLS_DHM_C) |
| 4880 | mbedtls_mpi_free( &conf->dhm_P ); |
| 4881 | mbedtls_mpi_free( &conf->dhm_G ); |
| 4882 | #endif |
| 4883 | |
Ronald Cron | 73fe8df | 2022-10-05 14:31:43 +0200 | [diff] [blame] | 4884 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED) |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 4885 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 4886 | if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) ) |
| 4887 | { |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 4888 | conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT; |
| 4889 | } |
Neil Armstrong | 8ecd668 | 2022-05-05 11:40:35 +0200 | [diff] [blame] | 4890 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4891 | if( conf->psk != NULL ) |
| 4892 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 4893 | mbedtls_platform_zeroize( conf->psk, conf->psk_len ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4894 | mbedtls_free( conf->psk ); |
Azim Khan | 27e8a12 | 2018-03-21 14:24:11 +0000 | [diff] [blame] | 4895 | conf->psk = NULL; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4896 | conf->psk_len = 0; |
junyeonLEE | 316b162 | 2017-12-20 16:29:30 +0900 | [diff] [blame] | 4897 | } |
| 4898 | |
| 4899 | if( conf->psk_identity != NULL ) |
| 4900 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 4901 | mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len ); |
junyeonLEE | 316b162 | 2017-12-20 16:29:30 +0900 | [diff] [blame] | 4902 | mbedtls_free( conf->psk_identity ); |
Azim Khan | 27e8a12 | 2018-03-21 14:24:11 +0000 | [diff] [blame] | 4903 | conf->psk_identity = NULL; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4904 | conf->psk_identity_len = 0; |
| 4905 | } |
Ronald Cron | 73fe8df | 2022-10-05 14:31:43 +0200 | [diff] [blame] | 4906 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */ |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4907 | |
| 4908 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 4909 | ssl_key_cert_free( conf->key_cert ); |
| 4910 | #endif |
| 4911 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 4912 | mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4913 | } |
| 4914 | |
Manuel Pégourié-Gonnard | 5674a97 | 2015-10-19 15:14:03 +0200 | [diff] [blame] | 4915 | #if defined(MBEDTLS_PK_C) && \ |
| 4916 | ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) ) |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 4917 | /* |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4918 | * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 4919 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4920 | 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] | 4921 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4922 | #if defined(MBEDTLS_RSA_C) |
| 4923 | if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) ) |
| 4924 | return( MBEDTLS_SSL_SIG_RSA ); |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 4925 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4926 | #if defined(MBEDTLS_ECDSA_C) |
| 4927 | if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) ) |
| 4928 | return( MBEDTLS_SSL_SIG_ECDSA ); |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 4929 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4930 | return( MBEDTLS_SSL_SIG_ANON ); |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 4931 | } |
| 4932 | |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 4933 | unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type ) |
| 4934 | { |
| 4935 | switch( type ) { |
| 4936 | case MBEDTLS_PK_RSA: |
| 4937 | return( MBEDTLS_SSL_SIG_RSA ); |
| 4938 | case MBEDTLS_PK_ECDSA: |
| 4939 | case MBEDTLS_PK_ECKEY: |
| 4940 | return( MBEDTLS_SSL_SIG_ECDSA ); |
| 4941 | default: |
| 4942 | return( MBEDTLS_SSL_SIG_ANON ); |
| 4943 | } |
| 4944 | } |
| 4945 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4946 | 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] | 4947 | { |
| 4948 | switch( sig ) |
| 4949 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4950 | #if defined(MBEDTLS_RSA_C) |
| 4951 | case MBEDTLS_SSL_SIG_RSA: |
| 4952 | return( MBEDTLS_PK_RSA ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4953 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4954 | #if defined(MBEDTLS_ECDSA_C) |
| 4955 | case MBEDTLS_SSL_SIG_ECDSA: |
| 4956 | return( MBEDTLS_PK_ECDSA ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4957 | #endif |
| 4958 | default: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4959 | return( MBEDTLS_PK_NONE ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4960 | } |
| 4961 | } |
Manuel Pégourié-Gonnard | 5674a97 | 2015-10-19 15:14:03 +0200 | [diff] [blame] | 4962 | #endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */ |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4963 | |
Manuel Pégourié-Gonnard | 1a48383 | 2013-09-20 12:29:15 +0200 | [diff] [blame] | 4964 | /* |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 4965 | * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX |
Manuel Pégourié-Gonnard | 1a48383 | 2013-09-20 12:29:15 +0200 | [diff] [blame] | 4966 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4967 | 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] | 4968 | { |
| 4969 | switch( hash ) |
| 4970 | { |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4971 | #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] | 4972 | case MBEDTLS_SSL_HASH_MD5: |
| 4973 | return( MBEDTLS_MD_MD5 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4974 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4975 | #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] | 4976 | case MBEDTLS_SSL_HASH_SHA1: |
| 4977 | return( MBEDTLS_MD_SHA1 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4978 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4979 | #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] | 4980 | case MBEDTLS_SSL_HASH_SHA224: |
| 4981 | return( MBEDTLS_MD_SHA224 ); |
Mateusz Starzyk | e3c48b4 | 2021-04-19 16:46:28 +0200 | [diff] [blame] | 4982 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4983 | #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] | 4984 | case MBEDTLS_SSL_HASH_SHA256: |
| 4985 | return( MBEDTLS_MD_SHA256 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4986 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4987 | #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] | 4988 | case MBEDTLS_SSL_HASH_SHA384: |
| 4989 | return( MBEDTLS_MD_SHA384 ); |
Mateusz Starzyk | 3352a53 | 2021-04-06 14:28:22 +0200 | [diff] [blame] | 4990 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4991 | #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] | 4992 | case MBEDTLS_SSL_HASH_SHA512: |
| 4993 | return( MBEDTLS_MD_SHA512 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4994 | #endif |
| 4995 | default: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4996 | return( MBEDTLS_MD_NONE ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4997 | } |
| 4998 | } |
| 4999 | |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 5000 | /* |
| 5001 | * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX |
| 5002 | */ |
| 5003 | unsigned char mbedtls_ssl_hash_from_md_alg( int md ) |
| 5004 | { |
| 5005 | switch( md ) |
| 5006 | { |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5007 | #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] | 5008 | case MBEDTLS_MD_MD5: |
| 5009 | return( MBEDTLS_SSL_HASH_MD5 ); |
| 5010 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5011 | #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] | 5012 | case MBEDTLS_MD_SHA1: |
| 5013 | return( MBEDTLS_SSL_HASH_SHA1 ); |
| 5014 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5015 | #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] | 5016 | case MBEDTLS_MD_SHA224: |
| 5017 | return( MBEDTLS_SSL_HASH_SHA224 ); |
Mateusz Starzyk | e3c48b4 | 2021-04-19 16:46:28 +0200 | [diff] [blame] | 5018 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5019 | #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] | 5020 | case MBEDTLS_MD_SHA256: |
| 5021 | return( MBEDTLS_SSL_HASH_SHA256 ); |
| 5022 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5023 | #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] | 5024 | case MBEDTLS_MD_SHA384: |
| 5025 | return( MBEDTLS_SSL_HASH_SHA384 ); |
Mateusz Starzyk | 3352a53 | 2021-04-06 14:28:22 +0200 | [diff] [blame] | 5026 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5027 | #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] | 5028 | case MBEDTLS_MD_SHA512: |
| 5029 | return( MBEDTLS_SSL_HASH_SHA512 ); |
| 5030 | #endif |
| 5031 | default: |
| 5032 | return( MBEDTLS_SSL_HASH_NONE ); |
| 5033 | } |
| 5034 | } |
| 5035 | |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 5036 | /* |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 5037 | * 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] | 5038 | * 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] | 5039 | */ |
Manuel Pégourié-Gonnard | 0d63b84 | 2022-01-18 13:10:56 +0100 | [diff] [blame] | 5040 | 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] | 5041 | { |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5042 | const uint16_t *group_list = mbedtls_ssl_get_groups( ssl ); |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 5043 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5044 | if( group_list == NULL ) |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 5045 | return( -1 ); |
| 5046 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5047 | for( ; *group_list != 0; group_list++ ) |
| 5048 | { |
| 5049 | if( *group_list == tls_id ) |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 5050 | return( 0 ); |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5051 | } |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 5052 | |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 5053 | return( -1 ); |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 5054 | } |
Manuel Pégourié-Gonnard | 0d63b84 | 2022-01-18 13:10:56 +0100 | [diff] [blame] | 5055 | |
| 5056 | #if defined(MBEDTLS_ECP_C) |
| 5057 | /* |
| 5058 | * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id. |
| 5059 | */ |
| 5060 | int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id ) |
| 5061 | { |
Leonid Rozenboim | 19e5973 | 2022-08-08 16:52:38 -0700 | [diff] [blame] | 5062 | const mbedtls_ecp_curve_info *grp_info = |
Tom Cosgrove | bcc13c9 | 2022-08-24 15:08:16 +0100 | [diff] [blame] | 5063 | mbedtls_ecp_curve_info_from_grp_id( grp_id ); |
Leonid Rozenboim | 19e5973 | 2022-08-08 16:52:38 -0700 | [diff] [blame] | 5064 | |
Tom Cosgrove | bcc13c9 | 2022-08-24 15:08:16 +0100 | [diff] [blame] | 5065 | if ( grp_info == NULL ) |
Leonid Rozenboim | 19e5973 | 2022-08-08 16:52:38 -0700 | [diff] [blame] | 5066 | return -1; |
| 5067 | |
| 5068 | uint16_t tls_id = grp_info->tls_id; |
| 5069 | |
Manuel Pégourié-Gonnard | 0d63b84 | 2022-01-18 13:10:56 +0100 | [diff] [blame] | 5070 | return mbedtls_ssl_check_curve_tls_id( ssl, tls_id ); |
| 5071 | } |
Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 5072 | #endif /* MBEDTLS_ECP_C */ |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5073 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5074 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 5075 | int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert, |
| 5076 | const mbedtls_ssl_ciphersuite_t *ciphersuite, |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 5077 | int cert_endpoint, |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 5078 | uint32_t *flags ) |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5079 | { |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 5080 | int ret = 0; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5081 | int usage = 0; |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 5082 | const char *ext_oid; |
| 5083 | size_t ext_len; |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 5084 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5085 | if( cert_endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5086 | { |
| 5087 | /* Server part of the key exchange */ |
| 5088 | switch( ciphersuite->key_exchange ) |
| 5089 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5090 | case MBEDTLS_KEY_EXCHANGE_RSA: |
| 5091 | case MBEDTLS_KEY_EXCHANGE_RSA_PSK: |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 5092 | usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5093 | break; |
| 5094 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5095 | case MBEDTLS_KEY_EXCHANGE_DHE_RSA: |
| 5096 | case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA: |
| 5097 | case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA: |
| 5098 | usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5099 | break; |
| 5100 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5101 | case MBEDTLS_KEY_EXCHANGE_ECDH_RSA: |
| 5102 | case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA: |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 5103 | usage = MBEDTLS_X509_KU_KEY_AGREEMENT; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5104 | break; |
| 5105 | |
| 5106 | /* 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] | 5107 | case MBEDTLS_KEY_EXCHANGE_NONE: |
| 5108 | case MBEDTLS_KEY_EXCHANGE_PSK: |
| 5109 | case MBEDTLS_KEY_EXCHANGE_DHE_PSK: |
| 5110 | case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK: |
Manuel Pégourié-Gonnard | 557535d | 2015-09-15 17:53:32 +0200 | [diff] [blame] | 5111 | case MBEDTLS_KEY_EXCHANGE_ECJPAKE: |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5112 | usage = 0; |
| 5113 | } |
| 5114 | } |
| 5115 | else |
| 5116 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5117 | /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */ |
| 5118 | usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5119 | } |
| 5120 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5121 | if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 ) |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 5122 | { |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 5123 | *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE; |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 5124 | ret = -1; |
| 5125 | } |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5126 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5127 | if( cert_endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 5128 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5129 | ext_oid = MBEDTLS_OID_SERVER_AUTH; |
| 5130 | ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH ); |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 5131 | } |
| 5132 | else |
| 5133 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5134 | ext_oid = MBEDTLS_OID_CLIENT_AUTH; |
| 5135 | ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH ); |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 5136 | } |
| 5137 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5138 | 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] | 5139 | { |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 5140 | *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE; |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 5141 | ret = -1; |
| 5142 | } |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 5143 | |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 5144 | return( ret ); |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5145 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5146 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Manuel Pégourié-Gonnard | 3a306b9 | 2014-04-29 15:11:17 +0200 | [diff] [blame] | 5147 | |
Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 5148 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 5149 | int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl, |
| 5150 | const mbedtls_md_type_t md, |
| 5151 | unsigned char *dst, |
| 5152 | size_t dst_len, |
| 5153 | size_t *olen ) |
| 5154 | { |
Ronald Cron | f6893e1 | 2022-01-07 22:09:01 +0100 | [diff] [blame] | 5155 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 5156 | psa_hash_operation_t *hash_operation_to_clone; |
| 5157 | psa_hash_operation_t hash_operation = psa_hash_operation_init(); |
| 5158 | |
Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 5159 | *olen = 0; |
Ronald Cron | f6893e1 | 2022-01-07 22:09:01 +0100 | [diff] [blame] | 5160 | |
| 5161 | switch( md ) |
| 5162 | { |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5163 | #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] | 5164 | case MBEDTLS_MD_SHA384: |
| 5165 | hash_operation_to_clone = &ssl->handshake->fin_sha384_psa; |
| 5166 | break; |
| 5167 | #endif |
| 5168 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5169 | #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] | 5170 | case MBEDTLS_MD_SHA256: |
| 5171 | hash_operation_to_clone = &ssl->handshake->fin_sha256_psa; |
| 5172 | break; |
| 5173 | #endif |
| 5174 | |
| 5175 | default: |
| 5176 | goto exit; |
| 5177 | } |
| 5178 | |
| 5179 | status = psa_hash_clone( hash_operation_to_clone, &hash_operation ); |
| 5180 | if( status != PSA_SUCCESS ) |
| 5181 | goto exit; |
| 5182 | |
| 5183 | status = psa_hash_finish( &hash_operation, dst, dst_len, olen ); |
| 5184 | if( status != PSA_SUCCESS ) |
| 5185 | goto exit; |
| 5186 | |
| 5187 | exit: |
Andrzej Kurek | eabeb30 | 2022-10-17 07:52:51 -0400 | [diff] [blame] | 5188 | #if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \ |
| 5189 | !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
| 5190 | (void) ssl; |
| 5191 | #endif |
Ronald Cron | b788c04 | 2022-02-15 09:14:15 +0100 | [diff] [blame] | 5192 | return( psa_ssl_status_to_mbedtls( status ) ); |
Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 5193 | } |
| 5194 | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
| 5195 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5196 | #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] | 5197 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | 000f976 | 2021-09-14 11:12:51 +0800 | [diff] [blame] | 5198 | static int ssl_get_handshake_transcript_sha384( mbedtls_ssl_context *ssl, |
| 5199 | unsigned char *dst, |
| 5200 | size_t dst_len, |
| 5201 | size_t *olen ) |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5202 | { |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5203 | int ret; |
| 5204 | mbedtls_sha512_context sha512; |
| 5205 | |
| 5206 | if( dst_len < 48 ) |
| 5207 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 5208 | |
| 5209 | mbedtls_sha512_init( &sha512 ); |
Andrzej Kurek | a242e83 | 2022-08-11 10:03:14 -0400 | [diff] [blame] | 5210 | mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 ); |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5211 | |
| 5212 | if( ( ret = mbedtls_sha512_finish( &sha512, dst ) ) != 0 ) |
| 5213 | { |
| 5214 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha512_finish", ret ); |
| 5215 | goto exit; |
| 5216 | } |
| 5217 | |
| 5218 | *olen = 48; |
| 5219 | |
| 5220 | exit: |
| 5221 | |
| 5222 | mbedtls_sha512_free( &sha512 ); |
| 5223 | return( ret ); |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5224 | } |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5225 | #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] | 5226 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5227 | #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] | 5228 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | 000f976 | 2021-09-14 11:12:51 +0800 | [diff] [blame] | 5229 | static int ssl_get_handshake_transcript_sha256( mbedtls_ssl_context *ssl, |
| 5230 | unsigned char *dst, |
| 5231 | size_t dst_len, |
| 5232 | size_t *olen ) |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5233 | { |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5234 | int ret; |
| 5235 | mbedtls_sha256_context sha256; |
| 5236 | |
| 5237 | if( dst_len < 32 ) |
| 5238 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 5239 | |
| 5240 | mbedtls_sha256_init( &sha256 ); |
| 5241 | mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 ); |
Jerry Yu | c5aef88 | 2021-12-23 20:15:02 +0800 | [diff] [blame] | 5242 | |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5243 | if( ( ret = mbedtls_sha256_finish( &sha256, dst ) ) != 0 ) |
| 5244 | { |
| 5245 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha256_finish", ret ); |
| 5246 | goto exit; |
| 5247 | } |
| 5248 | |
| 5249 | *olen = 32; |
| 5250 | |
| 5251 | exit: |
| 5252 | |
| 5253 | mbedtls_sha256_free( &sha256 ); |
| 5254 | return( ret ); |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5255 | } |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5256 | #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] | 5257 | |
Jerry Yu | 000f976 | 2021-09-14 11:12:51 +0800 | [diff] [blame] | 5258 | int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl, |
| 5259 | const mbedtls_md_type_t md, |
| 5260 | unsigned char *dst, |
| 5261 | size_t dst_len, |
| 5262 | size_t *olen ) |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5263 | { |
Jerry Yu | c1ddeef | 2021-10-08 15:14:45 +0800 | [diff] [blame] | 5264 | switch( md ) |
| 5265 | { |
| 5266 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5267 | #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] | 5268 | case MBEDTLS_MD_SHA384: |
Jerry Yu | c5aef88 | 2021-12-23 20:15:02 +0800 | [diff] [blame] | 5269 | return( ssl_get_handshake_transcript_sha384( ssl, dst, dst_len, olen ) ); |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 5270 | #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] | 5271 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5272 | #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] | 5273 | case MBEDTLS_MD_SHA256: |
Jerry Yu | c5aef88 | 2021-12-23 20:15:02 +0800 | [diff] [blame] | 5274 | return( ssl_get_handshake_transcript_sha256( ssl, dst, dst_len, olen ) ); |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 5275 | #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] | 5276 | |
| 5277 | default: |
| 5278 | break; |
| 5279 | } |
Jerry Yu | c5aef88 | 2021-12-23 20:15:02 +0800 | [diff] [blame] | 5280 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5281 | } |
XiaokangQian | 647719a | 2021-12-07 09:16:29 +0000 | [diff] [blame] | 5282 | |
Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 5283 | #endif /* !MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5284 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5285 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 5286 | /* mbedtls_ssl_parse_sig_alg_ext() |
| 5287 | * |
| 5288 | * The `extension_data` field of signature algorithm contains a `SignatureSchemeList` |
| 5289 | * value (TLS 1.3 RFC8446): |
| 5290 | * enum { |
| 5291 | * .... |
| 5292 | * ecdsa_secp256r1_sha256( 0x0403 ), |
| 5293 | * ecdsa_secp384r1_sha384( 0x0503 ), |
| 5294 | * ecdsa_secp521r1_sha512( 0x0603 ), |
| 5295 | * .... |
| 5296 | * } SignatureScheme; |
| 5297 | * |
| 5298 | * struct { |
| 5299 | * SignatureScheme supported_signature_algorithms<2..2^16-2>; |
| 5300 | * } SignatureSchemeList; |
| 5301 | * |
| 5302 | * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm` |
| 5303 | * value (TLS 1.2 RFC5246): |
| 5304 | * enum { |
| 5305 | * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5), |
| 5306 | * sha512(6), (255) |
| 5307 | * } HashAlgorithm; |
| 5308 | * |
| 5309 | * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) } |
| 5310 | * SignatureAlgorithm; |
| 5311 | * |
| 5312 | * struct { |
| 5313 | * HashAlgorithm hash; |
| 5314 | * SignatureAlgorithm signature; |
| 5315 | * } SignatureAndHashAlgorithm; |
| 5316 | * |
| 5317 | * SignatureAndHashAlgorithm |
| 5318 | * supported_signature_algorithms<2..2^16-2>; |
| 5319 | * |
| 5320 | * The TLS 1.3 signature algorithm extension was defined to be a compatible |
| 5321 | * generalization of the TLS 1.2 signature algorithm extension. |
| 5322 | * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by |
| 5323 | * `SignatureScheme` field of TLS 1.3 |
| 5324 | * |
| 5325 | */ |
| 5326 | int mbedtls_ssl_parse_sig_alg_ext( mbedtls_ssl_context *ssl, |
| 5327 | const unsigned char *buf, |
| 5328 | const unsigned char *end ) |
| 5329 | { |
| 5330 | const unsigned char *p = buf; |
| 5331 | size_t supported_sig_algs_len = 0; |
| 5332 | const unsigned char *supported_sig_algs_end; |
| 5333 | uint16_t sig_alg; |
| 5334 | uint32_t common_idx = 0; |
| 5335 | |
| 5336 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 ); |
| 5337 | supported_sig_algs_len = MBEDTLS_GET_UINT16_BE( p, 0 ); |
| 5338 | p += 2; |
| 5339 | |
| 5340 | memset( ssl->handshake->received_sig_algs, 0, |
| 5341 | sizeof(ssl->handshake->received_sig_algs) ); |
| 5342 | |
| 5343 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, supported_sig_algs_len ); |
| 5344 | supported_sig_algs_end = p + supported_sig_algs_len; |
| 5345 | while( p < supported_sig_algs_end ) |
| 5346 | { |
| 5347 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, supported_sig_algs_end, 2 ); |
| 5348 | sig_alg = MBEDTLS_GET_UINT16_BE( p, 0 ); |
| 5349 | p += 2; |
Jerry Yu | f3b46b5 | 2022-06-19 16:52:27 +0800 | [diff] [blame] | 5350 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "received signature algorithm: 0x%x %s", |
| 5351 | sig_alg, |
| 5352 | mbedtls_ssl_sig_alg_to_str( sig_alg ) ) ); |
Jerry Yu | 2fe6c63 | 2022-06-29 10:02:38 +0800 | [diff] [blame] | 5353 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Jerry Yu | 52b7d92 | 2022-07-01 18:03:31 +0800 | [diff] [blame] | 5354 | if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 && |
Jerry Yu | 2fe6c63 | 2022-06-29 10:02:38 +0800 | [diff] [blame] | 5355 | ( ! ( mbedtls_ssl_sig_alg_is_supported( ssl, sig_alg ) && |
| 5356 | mbedtls_ssl_sig_alg_is_offered( ssl, sig_alg ) ) ) ) |
| 5357 | { |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 5358 | continue; |
Jerry Yu | 2fe6c63 | 2022-06-29 10:02:38 +0800 | [diff] [blame] | 5359 | } |
| 5360 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 5361 | |
Jerry Yu | f3b46b5 | 2022-06-19 16:52:27 +0800 | [diff] [blame] | 5362 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "valid signature algorithm: %s", |
| 5363 | mbedtls_ssl_sig_alg_to_str( sig_alg ) ) ); |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 5364 | |
| 5365 | if( common_idx + 1 < MBEDTLS_RECEIVED_SIG_ALGS_SIZE ) |
| 5366 | { |
| 5367 | ssl->handshake->received_sig_algs[common_idx] = sig_alg; |
| 5368 | common_idx += 1; |
| 5369 | } |
| 5370 | } |
| 5371 | /* Check that we consumed all the message. */ |
| 5372 | if( p != end ) |
| 5373 | { |
| 5374 | MBEDTLS_SSL_DEBUG_MSG( 1, |
| 5375 | ( "Signature algorithms extension length misaligned" ) ); |
| 5376 | MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR, |
| 5377 | MBEDTLS_ERR_SSL_DECODE_ERROR ); |
| 5378 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
| 5379 | } |
| 5380 | |
| 5381 | if( common_idx == 0 ) |
| 5382 | { |
| 5383 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "no signature algorithm in common" ) ); |
| 5384 | MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE, |
| 5385 | MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE ); |
| 5386 | return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE ); |
| 5387 | } |
| 5388 | |
Gabor Mezei | 15b95a6 | 2022-05-09 16:37:58 +0200 | [diff] [blame] | 5389 | ssl->handshake->received_sig_algs[common_idx] = MBEDTLS_TLS_SIG_NONE; |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 5390 | return( 0 ); |
| 5391 | } |
| 5392 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5393 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 5394 | |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5395 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 5396 | |
| 5397 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 5398 | |
| 5399 | static psa_status_t setup_psa_key_derivation( psa_key_derivation_operation_t* derivation, |
| 5400 | mbedtls_svc_key_id_t key, |
| 5401 | psa_algorithm_t alg, |
Neil Armstrong | 8ecd668 | 2022-05-05 11:40:35 +0200 | [diff] [blame] | 5402 | const unsigned char* raw_psk, size_t raw_psk_length, |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5403 | const unsigned char* seed, size_t seed_length, |
| 5404 | const unsigned char* label, size_t label_length, |
Przemek Stekiel | 51a1f36 | 2022-04-13 08:57:06 +0200 | [diff] [blame] | 5405 | const unsigned char* other_secret, |
| 5406 | size_t other_secret_length, |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5407 | size_t capacity ) |
| 5408 | { |
| 5409 | psa_status_t status; |
| 5410 | |
| 5411 | status = psa_key_derivation_setup( derivation, alg ); |
| 5412 | if( status != PSA_SUCCESS ) |
| 5413 | return( status ); |
| 5414 | |
| 5415 | if( PSA_ALG_IS_TLS12_PRF( alg ) || PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) ) |
| 5416 | { |
| 5417 | status = psa_key_derivation_input_bytes( derivation, |
| 5418 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 5419 | seed, seed_length ); |
| 5420 | if( status != PSA_SUCCESS ) |
| 5421 | return( status ); |
| 5422 | |
Przemek Stekiel | 51a1f36 | 2022-04-13 08:57:06 +0200 | [diff] [blame] | 5423 | if ( other_secret != NULL ) |
Przemek Stekiel | 1f02703 | 2022-04-05 17:12:11 +0200 | [diff] [blame] | 5424 | { |
| 5425 | status = psa_key_derivation_input_bytes( derivation, |
Przemek Stekiel | 51a1f36 | 2022-04-13 08:57:06 +0200 | [diff] [blame] | 5426 | PSA_KEY_DERIVATION_INPUT_OTHER_SECRET, |
| 5427 | other_secret, other_secret_length ); |
Przemek Stekiel | 1f02703 | 2022-04-05 17:12:11 +0200 | [diff] [blame] | 5428 | if( status != PSA_SUCCESS ) |
| 5429 | return( status ); |
| 5430 | } |
| 5431 | |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5432 | if( mbedtls_svc_key_id_is_null( key ) ) |
| 5433 | { |
| 5434 | status = psa_key_derivation_input_bytes( |
| 5435 | derivation, PSA_KEY_DERIVATION_INPUT_SECRET, |
Neil Armstrong | 8ecd668 | 2022-05-05 11:40:35 +0200 | [diff] [blame] | 5436 | raw_psk, raw_psk_length ); |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5437 | } |
| 5438 | else |
| 5439 | { |
| 5440 | status = psa_key_derivation_input_key( |
| 5441 | derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key ); |
| 5442 | } |
| 5443 | if( status != PSA_SUCCESS ) |
| 5444 | return( status ); |
| 5445 | |
| 5446 | status = psa_key_derivation_input_bytes( derivation, |
| 5447 | PSA_KEY_DERIVATION_INPUT_LABEL, |
| 5448 | label, label_length ); |
| 5449 | if( status != PSA_SUCCESS ) |
| 5450 | return( status ); |
| 5451 | } |
| 5452 | else |
| 5453 | { |
| 5454 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5455 | } |
| 5456 | |
| 5457 | status = psa_key_derivation_set_capacity( derivation, capacity ); |
| 5458 | if( status != PSA_SUCCESS ) |
| 5459 | return( status ); |
| 5460 | |
| 5461 | return( PSA_SUCCESS ); |
| 5462 | } |
| 5463 | |
Andrzej Kurek | eabeb30 | 2022-10-17 07:52:51 -0400 | [diff] [blame] | 5464 | #if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) || \ |
| 5465 | 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] | 5466 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5467 | static int tls_prf_generic( mbedtls_md_type_t md_type, |
| 5468 | const unsigned char *secret, size_t slen, |
| 5469 | const char *label, |
| 5470 | const unsigned char *random, size_t rlen, |
| 5471 | unsigned char *dstbuf, size_t dlen ) |
| 5472 | { |
| 5473 | psa_status_t status; |
| 5474 | psa_algorithm_t alg; |
| 5475 | mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5476 | psa_key_derivation_operation_t derivation = |
| 5477 | PSA_KEY_DERIVATION_OPERATION_INIT; |
| 5478 | |
| 5479 | if( md_type == MBEDTLS_MD_SHA384 ) |
| 5480 | alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384); |
| 5481 | else |
| 5482 | alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256); |
| 5483 | |
| 5484 | /* Normally a "secret" should be long enough to be impossible to |
| 5485 | * find by brute force, and in particular should not be empty. But |
| 5486 | * this PRF is also used to derive an IV, in particular in EAP-TLS, |
| 5487 | * and for this use case it makes sense to have a 0-length "secret". |
| 5488 | * Since the key API doesn't allow importing a key of length 0, |
| 5489 | * keep master_key=0, which setup_psa_key_derivation() understands |
| 5490 | * to mean a 0-length "secret" input. */ |
| 5491 | if( slen != 0 ) |
| 5492 | { |
| 5493 | psa_key_attributes_t key_attributes = psa_key_attributes_init(); |
| 5494 | psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE ); |
| 5495 | psa_set_key_algorithm( &key_attributes, alg ); |
| 5496 | psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE ); |
| 5497 | |
| 5498 | status = psa_import_key( &key_attributes, secret, slen, &master_key ); |
| 5499 | if( status != PSA_SUCCESS ) |
| 5500 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 5501 | } |
| 5502 | |
| 5503 | status = setup_psa_key_derivation( &derivation, |
| 5504 | master_key, alg, |
Neil Armstrong | 8ecd668 | 2022-05-05 11:40:35 +0200 | [diff] [blame] | 5505 | NULL, 0, |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5506 | random, rlen, |
| 5507 | (unsigned char const *) label, |
| 5508 | (size_t) strlen( label ), |
Przemek Stekiel | 1f02703 | 2022-04-05 17:12:11 +0200 | [diff] [blame] | 5509 | NULL, 0, |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5510 | dlen ); |
| 5511 | if( status != PSA_SUCCESS ) |
| 5512 | { |
| 5513 | psa_key_derivation_abort( &derivation ); |
| 5514 | psa_destroy_key( master_key ); |
| 5515 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 5516 | } |
| 5517 | |
| 5518 | status = psa_key_derivation_output_bytes( &derivation, dstbuf, dlen ); |
| 5519 | if( status != PSA_SUCCESS ) |
| 5520 | { |
| 5521 | psa_key_derivation_abort( &derivation ); |
| 5522 | psa_destroy_key( master_key ); |
| 5523 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 5524 | } |
| 5525 | |
| 5526 | status = psa_key_derivation_abort( &derivation ); |
| 5527 | if( status != PSA_SUCCESS ) |
| 5528 | { |
| 5529 | psa_destroy_key( master_key ); |
| 5530 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 5531 | } |
| 5532 | |
| 5533 | if( ! mbedtls_svc_key_id_is_null( master_key ) ) |
| 5534 | status = psa_destroy_key( master_key ); |
| 5535 | if( status != PSA_SUCCESS ) |
| 5536 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 5537 | |
| 5538 | return( 0 ); |
| 5539 | } |
Andrzej Kurek | eabeb30 | 2022-10-17 07:52:51 -0400 | [diff] [blame] | 5540 | #endif |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5541 | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
| 5542 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 5543 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5544 | static int tls_prf_generic( mbedtls_md_type_t md_type, |
| 5545 | const unsigned char *secret, size_t slen, |
| 5546 | const char *label, |
| 5547 | const unsigned char *random, size_t rlen, |
| 5548 | unsigned char *dstbuf, size_t dlen ) |
| 5549 | { |
| 5550 | size_t nb; |
| 5551 | size_t i, j, k, md_len; |
| 5552 | unsigned char *tmp; |
| 5553 | size_t tmp_len = 0; |
| 5554 | unsigned char h_i[MBEDTLS_MD_MAX_SIZE]; |
| 5555 | const mbedtls_md_info_t *md_info; |
| 5556 | mbedtls_md_context_t md_ctx; |
| 5557 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 5558 | |
| 5559 | mbedtls_md_init( &md_ctx ); |
| 5560 | |
| 5561 | if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL ) |
| 5562 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 5563 | |
| 5564 | md_len = mbedtls_md_get_size( md_info ); |
| 5565 | |
| 5566 | tmp_len = md_len + strlen( label ) + rlen; |
| 5567 | tmp = mbedtls_calloc( 1, tmp_len ); |
| 5568 | if( tmp == NULL ) |
| 5569 | { |
| 5570 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 5571 | goto exit; |
| 5572 | } |
| 5573 | |
| 5574 | nb = strlen( label ); |
| 5575 | memcpy( tmp + md_len, label, nb ); |
| 5576 | memcpy( tmp + md_len + nb, random, rlen ); |
| 5577 | nb += rlen; |
| 5578 | |
| 5579 | /* |
| 5580 | * Compute P_<hash>(secret, label + random)[0..dlen] |
| 5581 | */ |
| 5582 | if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 ) |
| 5583 | goto exit; |
| 5584 | |
| 5585 | ret = mbedtls_md_hmac_starts( &md_ctx, secret, slen ); |
| 5586 | if( ret != 0 ) |
| 5587 | goto exit; |
| 5588 | ret = mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb ); |
| 5589 | if( ret != 0 ) |
| 5590 | goto exit; |
| 5591 | ret = mbedtls_md_hmac_finish( &md_ctx, tmp ); |
| 5592 | if( ret != 0 ) |
| 5593 | goto exit; |
| 5594 | |
| 5595 | for( i = 0; i < dlen; i += md_len ) |
| 5596 | { |
| 5597 | ret = mbedtls_md_hmac_reset ( &md_ctx ); |
| 5598 | if( ret != 0 ) |
| 5599 | goto exit; |
| 5600 | ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb ); |
| 5601 | if( ret != 0 ) |
| 5602 | goto exit; |
| 5603 | ret = mbedtls_md_hmac_finish( &md_ctx, h_i ); |
| 5604 | if( ret != 0 ) |
| 5605 | goto exit; |
| 5606 | |
| 5607 | ret = mbedtls_md_hmac_reset ( &md_ctx ); |
| 5608 | if( ret != 0 ) |
| 5609 | goto exit; |
| 5610 | ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len ); |
| 5611 | if( ret != 0 ) |
| 5612 | goto exit; |
| 5613 | ret = mbedtls_md_hmac_finish( &md_ctx, tmp ); |
| 5614 | if( ret != 0 ) |
| 5615 | goto exit; |
| 5616 | |
| 5617 | k = ( i + md_len > dlen ) ? dlen % md_len : md_len; |
| 5618 | |
| 5619 | for( j = 0; j < k; j++ ) |
| 5620 | dstbuf[i + j] = h_i[j]; |
| 5621 | } |
| 5622 | |
| 5623 | exit: |
| 5624 | mbedtls_md_free( &md_ctx ); |
| 5625 | |
| 5626 | mbedtls_platform_zeroize( tmp, tmp_len ); |
| 5627 | mbedtls_platform_zeroize( h_i, sizeof( h_i ) ); |
| 5628 | |
| 5629 | mbedtls_free( tmp ); |
| 5630 | |
| 5631 | return( ret ); |
| 5632 | } |
| 5633 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 5634 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5635 | #if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 5636 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5637 | static int tls_prf_sha256( const unsigned char *secret, size_t slen, |
| 5638 | const char *label, |
| 5639 | const unsigned char *random, size_t rlen, |
| 5640 | unsigned char *dstbuf, size_t dlen ) |
| 5641 | { |
| 5642 | return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen, |
| 5643 | label, random, rlen, dstbuf, dlen ) ); |
| 5644 | } |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 5645 | #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] | 5646 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5647 | #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] | 5648 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5649 | static int tls_prf_sha384( const unsigned char *secret, size_t slen, |
| 5650 | const char *label, |
| 5651 | const unsigned char *random, size_t rlen, |
| 5652 | unsigned char *dstbuf, size_t dlen ) |
| 5653 | { |
| 5654 | return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen, |
| 5655 | label, random, rlen, dstbuf, dlen ) ); |
| 5656 | } |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 5657 | #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] | 5658 | |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5659 | /* |
| 5660 | * Set appropriate PRF function and other SSL / TLS1.2 functions |
| 5661 | * |
| 5662 | * Inputs: |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5663 | * - hash associated with the ciphersuite (only used by TLS 1.2) |
| 5664 | * |
| 5665 | * Outputs: |
| 5666 | * - the tls_prf, calc_verify and calc_finished members of handshake structure |
| 5667 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 5668 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5669 | static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake, |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5670 | mbedtls_md_type_t hash ) |
| 5671 | { |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5672 | #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] | 5673 | if( hash == MBEDTLS_MD_SHA384 ) |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5674 | { |
| 5675 | handshake->tls_prf = tls_prf_sha384; |
| 5676 | handshake->calc_verify = ssl_calc_verify_tls_sha384; |
| 5677 | handshake->calc_finished = ssl_calc_finished_tls_sha384; |
| 5678 | } |
| 5679 | else |
| 5680 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5681 | #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] | 5682 | { |
Ronald Cron | 81591aa | 2022-03-07 09:05:51 +0100 | [diff] [blame] | 5683 | (void) hash; |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5684 | handshake->tls_prf = tls_prf_sha256; |
| 5685 | handshake->calc_verify = ssl_calc_verify_tls_sha256; |
| 5686 | handshake->calc_finished = ssl_calc_finished_tls_sha256; |
| 5687 | } |
Ronald Cron | 81591aa | 2022-03-07 09:05:51 +0100 | [diff] [blame] | 5688 | #else |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5689 | { |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5690 | (void) handshake; |
Ronald Cron | 81591aa | 2022-03-07 09:05:51 +0100 | [diff] [blame] | 5691 | (void) hash; |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5692 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 5693 | } |
Ronald Cron | 81591aa | 2022-03-07 09:05:51 +0100 | [diff] [blame] | 5694 | #endif |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5695 | |
| 5696 | return( 0 ); |
| 5697 | } |
Jerry Yu | d6ab235 | 2022-02-17 14:03:43 +0800 | [diff] [blame] | 5698 | |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5699 | /* |
| 5700 | * Compute master secret if needed |
| 5701 | * |
| 5702 | * Parameters: |
| 5703 | * [in/out] handshake |
| 5704 | * [in] resume, premaster, extended_ms, calc_verify, tls_prf |
| 5705 | * (PSA-PSK) ciphersuite_info, psk_opaque |
| 5706 | * [out] premaster (cleared) |
| 5707 | * [out] master |
| 5708 | * [in] ssl: optionally used for debugging, EMS and PSA-PSK |
| 5709 | * debug: conf->f_dbg, conf->p_dbg |
| 5710 | * EMS: passed to calc_verify (debug + session_negotiate) |
Ronald Cron | a25cf58 | 2022-03-07 11:10:36 +0100 | [diff] [blame] | 5711 | * PSA-PSA: conf |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5712 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 5713 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5714 | static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake, |
| 5715 | unsigned char *master, |
| 5716 | const mbedtls_ssl_context *ssl ) |
| 5717 | { |
| 5718 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 5719 | |
| 5720 | /* cf. RFC 5246, Section 8.1: |
| 5721 | * "The master secret is always exactly 48 bytes in length." */ |
| 5722 | size_t const master_secret_len = 48; |
| 5723 | |
| 5724 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 5725 | unsigned char session_hash[48]; |
| 5726 | #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */ |
| 5727 | |
| 5728 | /* The label for the KDF used for key expansion. |
| 5729 | * This is either "master secret" or "extended master secret" |
| 5730 | * depending on whether the Extended Master Secret extension |
| 5731 | * is used. */ |
| 5732 | char const *lbl = "master secret"; |
| 5733 | |
Przemek Stekiel | ae4ed30 | 2022-04-05 17:15:55 +0200 | [diff] [blame] | 5734 | /* The seed for the KDF used for key expansion. |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5735 | * - If the Extended Master Secret extension is not used, |
| 5736 | * this is ClientHello.Random + ServerHello.Random |
| 5737 | * (see Sect. 8.1 in RFC 5246). |
| 5738 | * - If the Extended Master Secret extension is used, |
| 5739 | * this is the transcript of the handshake so far. |
| 5740 | * (see Sect. 4 in RFC 7627). */ |
Przemek Stekiel | ae4ed30 | 2022-04-05 17:15:55 +0200 | [diff] [blame] | 5741 | unsigned char const *seed = handshake->randbytes; |
| 5742 | size_t seed_len = 64; |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5743 | |
| 5744 | #if !defined(MBEDTLS_DEBUG_C) && \ |
| 5745 | !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \ |
| 5746 | !(defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
| 5747 | defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)) |
| 5748 | ssl = NULL; /* make sure we don't use it except for those cases */ |
| 5749 | (void) ssl; |
| 5750 | #endif |
| 5751 | |
| 5752 | if( handshake->resume != 0 ) |
| 5753 | { |
| 5754 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) ); |
| 5755 | return( 0 ); |
| 5756 | } |
| 5757 | |
| 5758 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 5759 | if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED ) |
| 5760 | { |
| 5761 | lbl = "extended master secret"; |
Przemek Stekiel | ae4ed30 | 2022-04-05 17:15:55 +0200 | [diff] [blame] | 5762 | seed = session_hash; |
| 5763 | handshake->calc_verify( ssl, session_hash, &seed_len ); |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5764 | |
| 5765 | MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret", |
Przemek Stekiel | ae4ed30 | 2022-04-05 17:15:55 +0200 | [diff] [blame] | 5766 | session_hash, seed_len ); |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5767 | } |
Przemek Stekiel | 169bf0b | 2022-04-29 07:53:29 +0200 | [diff] [blame] | 5768 | #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */ |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5769 | |
Przemek Stekiel | 99114f3 | 2022-04-22 11:20:09 +0200 | [diff] [blame] | 5770 | #if defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
Przemek Stekiel | 8a4b7fd | 2022-04-28 09:22:22 +0200 | [diff] [blame] | 5771 | defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Neil Armstrong | e952a30 | 2022-05-03 10:22:14 +0200 | [diff] [blame] | 5772 | if( mbedtls_ssl_ciphersuite_uses_psk( handshake->ciphersuite_info ) == 1 ) |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5773 | { |
| 5774 | /* Perform PSK-to-MS expansion in a single step. */ |
| 5775 | psa_status_t status; |
| 5776 | psa_algorithm_t alg; |
| 5777 | mbedtls_svc_key_id_t psk; |
| 5778 | psa_key_derivation_operation_t derivation = |
| 5779 | PSA_KEY_DERIVATION_OPERATION_INIT; |
| 5780 | mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac; |
| 5781 | |
| 5782 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) ); |
| 5783 | |
| 5784 | psk = mbedtls_ssl_get_opaque_psk( ssl ); |
| 5785 | |
| 5786 | if( hash_alg == MBEDTLS_MD_SHA384 ) |
| 5787 | alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384); |
| 5788 | else |
| 5789 | alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256); |
| 5790 | |
Przemek Stekiel | 51a1f36 | 2022-04-13 08:57:06 +0200 | [diff] [blame] | 5791 | size_t other_secret_len = 0; |
| 5792 | unsigned char* other_secret = NULL; |
Przemek Stekiel | c203340 | 2022-04-05 17:19:41 +0200 | [diff] [blame] | 5793 | |
Przemek Stekiel | 19b80f8 | 2022-04-14 08:29:31 +0200 | [diff] [blame] | 5794 | switch( handshake->ciphersuite_info->key_exchange ) |
Przemek Stekiel | c203340 | 2022-04-05 17:19:41 +0200 | [diff] [blame] | 5795 | { |
Przemek Stekiel | 19b80f8 | 2022-04-14 08:29:31 +0200 | [diff] [blame] | 5796 | /* Provide other secret. |
Przemek Stekiel | 51a1f36 | 2022-04-13 08:57:06 +0200 | [diff] [blame] | 5797 | * Other secret is stored in premaster, where first 2 bytes hold the |
Przemek Stekiel | 19b80f8 | 2022-04-14 08:29:31 +0200 | [diff] [blame] | 5798 | * length of the other key. |
Przemek Stekiel | c203340 | 2022-04-05 17:19:41 +0200 | [diff] [blame] | 5799 | */ |
Przemek Stekiel | 19b80f8 | 2022-04-14 08:29:31 +0200 | [diff] [blame] | 5800 | case MBEDTLS_KEY_EXCHANGE_RSA_PSK: |
Przemek Stekiel | 8abcee9 | 2022-04-28 09:16:28 +0200 | [diff] [blame] | 5801 | /* For RSA-PSK other key length is always 48 bytes. */ |
Przemek Stekiel | 19b80f8 | 2022-04-14 08:29:31 +0200 | [diff] [blame] | 5802 | other_secret_len = 48; |
| 5803 | other_secret = handshake->premaster + 2; |
| 5804 | break; |
| 5805 | case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK: |
Przemek Stekiel | b293aaa | 2022-04-19 12:22:38 +0200 | [diff] [blame] | 5806 | case MBEDTLS_KEY_EXCHANGE_DHE_PSK: |
Przemek Stekiel | 19b80f8 | 2022-04-14 08:29:31 +0200 | [diff] [blame] | 5807 | other_secret_len = MBEDTLS_GET_UINT16_BE(handshake->premaster, 0); |
| 5808 | other_secret = handshake->premaster + 2; |
| 5809 | break; |
| 5810 | default: |
| 5811 | break; |
Przemek Stekiel | c203340 | 2022-04-05 17:19:41 +0200 | [diff] [blame] | 5812 | } |
| 5813 | |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5814 | status = setup_psa_key_derivation( &derivation, psk, alg, |
Neil Armstrong | 8ecd668 | 2022-05-05 11:40:35 +0200 | [diff] [blame] | 5815 | ssl->conf->psk, ssl->conf->psk_len, |
Przemek Stekiel | ae4ed30 | 2022-04-05 17:15:55 +0200 | [diff] [blame] | 5816 | seed, seed_len, |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5817 | (unsigned char const *) lbl, |
| 5818 | (size_t) strlen( lbl ), |
Przemek Stekiel | 51a1f36 | 2022-04-13 08:57:06 +0200 | [diff] [blame] | 5819 | other_secret, other_secret_len, |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5820 | master_secret_len ); |
| 5821 | if( status != PSA_SUCCESS ) |
| 5822 | { |
| 5823 | psa_key_derivation_abort( &derivation ); |
| 5824 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 5825 | } |
| 5826 | |
| 5827 | status = psa_key_derivation_output_bytes( &derivation, |
| 5828 | master, |
| 5829 | master_secret_len ); |
| 5830 | if( status != PSA_SUCCESS ) |
| 5831 | { |
| 5832 | psa_key_derivation_abort( &derivation ); |
| 5833 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 5834 | } |
| 5835 | |
| 5836 | status = psa_key_derivation_abort( &derivation ); |
| 5837 | if( status != PSA_SUCCESS ) |
| 5838 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 5839 | } |
| 5840 | else |
| 5841 | #endif |
| 5842 | { |
| 5843 | ret = handshake->tls_prf( handshake->premaster, handshake->pmslen, |
Przemek Stekiel | ae4ed30 | 2022-04-05 17:15:55 +0200 | [diff] [blame] | 5844 | lbl, seed, seed_len, |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5845 | master, |
| 5846 | master_secret_len ); |
| 5847 | if( ret != 0 ) |
| 5848 | { |
| 5849 | MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret ); |
| 5850 | return( ret ); |
| 5851 | } |
| 5852 | |
| 5853 | MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", |
| 5854 | handshake->premaster, |
| 5855 | handshake->pmslen ); |
| 5856 | |
| 5857 | mbedtls_platform_zeroize( handshake->premaster, |
| 5858 | sizeof(handshake->premaster) ); |
| 5859 | } |
| 5860 | |
| 5861 | return( 0 ); |
| 5862 | } |
| 5863 | |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 5864 | int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl ) |
| 5865 | { |
| 5866 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 5867 | const mbedtls_ssl_ciphersuite_t * const ciphersuite_info = |
| 5868 | ssl->handshake->ciphersuite_info; |
| 5869 | |
| 5870 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) ); |
| 5871 | |
| 5872 | /* Set PRF, calc_verify and calc_finished function pointers */ |
| 5873 | ret = ssl_set_handshake_prfs( ssl->handshake, |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 5874 | ciphersuite_info->mac ); |
| 5875 | if( ret != 0 ) |
| 5876 | { |
| 5877 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret ); |
| 5878 | return( ret ); |
| 5879 | } |
| 5880 | |
| 5881 | /* Compute master secret if needed */ |
| 5882 | ret = ssl_compute_master( ssl->handshake, |
| 5883 | ssl->session_negotiate->master, |
| 5884 | ssl ); |
| 5885 | if( ret != 0 ) |
| 5886 | { |
| 5887 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret ); |
| 5888 | return( ret ); |
| 5889 | } |
| 5890 | |
| 5891 | /* Swap the client and server random values: |
| 5892 | * - MS derivation wanted client+server (RFC 5246 8.1) |
| 5893 | * - key derivation wants server+client (RFC 5246 6.3) */ |
| 5894 | { |
| 5895 | unsigned char tmp[64]; |
| 5896 | memcpy( tmp, ssl->handshake->randbytes, 64 ); |
| 5897 | memcpy( ssl->handshake->randbytes, tmp + 32, 32 ); |
| 5898 | memcpy( ssl->handshake->randbytes + 32, tmp, 32 ); |
| 5899 | mbedtls_platform_zeroize( tmp, sizeof( tmp ) ); |
| 5900 | } |
| 5901 | |
| 5902 | /* Populate transform structure */ |
| 5903 | ret = ssl_tls12_populate_transform( ssl->transform_negotiate, |
| 5904 | ssl->session_negotiate->ciphersuite, |
| 5905 | ssl->session_negotiate->master, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 5906 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 5907 | ssl->session_negotiate->encrypt_then_mac, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 5908 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 5909 | ssl->handshake->tls_prf, |
| 5910 | ssl->handshake->randbytes, |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 5911 | ssl->tls_version, |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 5912 | ssl->conf->endpoint, |
| 5913 | ssl ); |
| 5914 | if( ret != 0 ) |
| 5915 | { |
| 5916 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls12_populate_transform", ret ); |
| 5917 | return( ret ); |
| 5918 | } |
| 5919 | |
| 5920 | /* We no longer need Server/ClientHello.random values */ |
| 5921 | mbedtls_platform_zeroize( ssl->handshake->randbytes, |
| 5922 | sizeof( ssl->handshake->randbytes ) ); |
| 5923 | |
| 5924 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) ); |
| 5925 | |
| 5926 | return( 0 ); |
| 5927 | } |
Jerry Yu | 8392e0d | 2022-02-17 14:10:24 +0800 | [diff] [blame] | 5928 | |
Ronald Cron | 4dcbca9 | 2022-03-07 10:21:40 +0100 | [diff] [blame] | 5929 | int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md ) |
| 5930 | { |
Ronald Cron | 4dcbca9 | 2022-03-07 10:21:40 +0100 | [diff] [blame] | 5931 | switch( md ) |
| 5932 | { |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5933 | #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] | 5934 | case MBEDTLS_SSL_HASH_SHA384: |
| 5935 | ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384; |
| 5936 | break; |
| 5937 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5938 | #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] | 5939 | case MBEDTLS_SSL_HASH_SHA256: |
| 5940 | ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256; |
| 5941 | break; |
| 5942 | #endif |
| 5943 | default: |
| 5944 | return( -1 ); |
| 5945 | } |
Andrzej Kurek | eabeb30 | 2022-10-17 07:52:51 -0400 | [diff] [blame] | 5946 | #if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \ |
| 5947 | !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
| 5948 | (void) ssl; |
| 5949 | #endif |
Ronald Cron | c2f13a0 | 2022-03-07 10:25:24 +0100 | [diff] [blame] | 5950 | return( 0 ); |
Ronald Cron | 4dcbca9 | 2022-03-07 10:21:40 +0100 | [diff] [blame] | 5951 | } |
| 5952 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5953 | #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] | 5954 | void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl, |
| 5955 | unsigned char *hash, |
| 5956 | size_t *hlen ) |
| 5957 | { |
| 5958 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 5959 | size_t hash_size; |
| 5960 | psa_status_t status; |
| 5961 | psa_hash_operation_t sha256_psa = psa_hash_operation_init(); |
| 5962 | |
| 5963 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) ); |
| 5964 | status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa ); |
| 5965 | if( status != PSA_SUCCESS ) |
| 5966 | { |
| 5967 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); |
| 5968 | return; |
| 5969 | } |
| 5970 | |
| 5971 | status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size ); |
| 5972 | if( status != PSA_SUCCESS ) |
| 5973 | { |
| 5974 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); |
| 5975 | return; |
| 5976 | } |
| 5977 | |
| 5978 | *hlen = 32; |
| 5979 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen ); |
| 5980 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) ); |
| 5981 | #else |
| 5982 | mbedtls_sha256_context sha256; |
| 5983 | |
| 5984 | mbedtls_sha256_init( &sha256 ); |
| 5985 | |
| 5986 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) ); |
| 5987 | |
| 5988 | mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 ); |
| 5989 | mbedtls_sha256_finish( &sha256, hash ); |
| 5990 | |
| 5991 | *hlen = 32; |
| 5992 | |
| 5993 | MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen ); |
| 5994 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) ); |
| 5995 | |
| 5996 | mbedtls_sha256_free( &sha256 ); |
| 5997 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 5998 | return; |
| 5999 | } |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 6000 | #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] | 6001 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 6002 | #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] | 6003 | void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl, |
| 6004 | unsigned char *hash, |
| 6005 | size_t *hlen ) |
| 6006 | { |
| 6007 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 6008 | size_t hash_size; |
| 6009 | psa_status_t status; |
| 6010 | psa_hash_operation_t sha384_psa = psa_hash_operation_init(); |
| 6011 | |
| 6012 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) ); |
| 6013 | status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa ); |
| 6014 | if( status != PSA_SUCCESS ) |
| 6015 | { |
| 6016 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); |
| 6017 | return; |
| 6018 | } |
| 6019 | |
| 6020 | status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size ); |
| 6021 | if( status != PSA_SUCCESS ) |
| 6022 | { |
| 6023 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); |
| 6024 | return; |
| 6025 | } |
| 6026 | |
| 6027 | *hlen = 48; |
| 6028 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen ); |
| 6029 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) ); |
| 6030 | #else |
| 6031 | mbedtls_sha512_context sha512; |
| 6032 | |
| 6033 | mbedtls_sha512_init( &sha512 ); |
| 6034 | |
| 6035 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) ); |
| 6036 | |
Andrzej Kurek | a242e83 | 2022-08-11 10:03:14 -0400 | [diff] [blame] | 6037 | mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 ); |
Jerry Yu | c1cb384 | 2022-02-17 14:13:48 +0800 | [diff] [blame] | 6038 | mbedtls_sha512_finish( &sha512, hash ); |
| 6039 | |
| 6040 | *hlen = 48; |
| 6041 | |
| 6042 | MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen ); |
| 6043 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) ); |
| 6044 | |
| 6045 | mbedtls_sha512_free( &sha512 ); |
| 6046 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 6047 | return; |
| 6048 | } |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 6049 | #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] | 6050 | |
Neil Armstrong | 80f6f32 | 2022-05-03 17:56:38 +0200 | [diff] [blame] | 6051 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
| 6052 | defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6053 | int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex ) |
| 6054 | { |
| 6055 | unsigned char *p = ssl->handshake->premaster; |
| 6056 | unsigned char *end = p + sizeof( ssl->handshake->premaster ); |
| 6057 | const unsigned char *psk = NULL; |
| 6058 | size_t psk_len = 0; |
Przemek Stekiel | b293aaa | 2022-04-19 12:22:38 +0200 | [diff] [blame] | 6059 | int psk_ret = mbedtls_ssl_get_psk( ssl, &psk, &psk_len ); |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6060 | |
Przemek Stekiel | b293aaa | 2022-04-19 12:22:38 +0200 | [diff] [blame] | 6061 | if( psk_ret == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED ) |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6062 | { |
| 6063 | /* |
| 6064 | * This should never happen because the existence of a PSK is always |
Przemek Stekiel | b293aaa | 2022-04-19 12:22:38 +0200 | [diff] [blame] | 6065 | * checked before calling this function. |
| 6066 | * |
| 6067 | * The exception is opaque DHE-PSK. For DHE-PSK fill premaster with |
Przemek Stekiel | 8abcee9 | 2022-04-28 09:16:28 +0200 | [diff] [blame] | 6068 | * the shared secret without PSK. |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6069 | */ |
Przemek Stekiel | b293aaa | 2022-04-19 12:22:38 +0200 | [diff] [blame] | 6070 | if ( key_ex != MBEDTLS_KEY_EXCHANGE_DHE_PSK ) |
| 6071 | { |
| 6072 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 6073 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 6074 | } |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6075 | } |
| 6076 | |
| 6077 | /* |
| 6078 | * PMS = struct { |
| 6079 | * opaque other_secret<0..2^16-1>; |
| 6080 | * opaque psk<0..2^16-1>; |
| 6081 | * }; |
| 6082 | * with "other_secret" depending on the particular key exchange |
| 6083 | */ |
| 6084 | #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) |
| 6085 | if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK ) |
| 6086 | { |
| 6087 | if( end - p < 2 ) |
| 6088 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6089 | |
| 6090 | MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 ); |
| 6091 | p += 2; |
| 6092 | |
| 6093 | if( end < p || (size_t)( end - p ) < psk_len ) |
| 6094 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6095 | |
| 6096 | memset( p, 0, psk_len ); |
| 6097 | p += psk_len; |
| 6098 | } |
| 6099 | else |
| 6100 | #endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */ |
| 6101 | #if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) |
| 6102 | if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK ) |
| 6103 | { |
| 6104 | /* |
| 6105 | * other_secret already set by the ClientKeyExchange message, |
| 6106 | * and is 48 bytes long |
| 6107 | */ |
| 6108 | if( end - p < 2 ) |
| 6109 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6110 | |
| 6111 | *p++ = 0; |
| 6112 | *p++ = 48; |
| 6113 | p += 48; |
| 6114 | } |
| 6115 | else |
| 6116 | #endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */ |
| 6117 | #if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) |
| 6118 | if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK ) |
| 6119 | { |
| 6120 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 6121 | size_t len; |
| 6122 | |
| 6123 | /* Write length only when we know the actual value */ |
| 6124 | if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx, |
| 6125 | p + 2, end - ( p + 2 ), &len, |
| 6126 | ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 ) |
| 6127 | { |
| 6128 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret ); |
| 6129 | return( ret ); |
| 6130 | } |
| 6131 | MBEDTLS_PUT_UINT16_BE( len, p, 0 ); |
| 6132 | p += 2 + len; |
| 6133 | |
| 6134 | MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K ); |
| 6135 | } |
| 6136 | else |
| 6137 | #endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */ |
Neil Armstrong | 80f6f32 | 2022-05-03 17:56:38 +0200 | [diff] [blame] | 6138 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6139 | if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ) |
| 6140 | { |
| 6141 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 6142 | size_t zlen; |
| 6143 | |
| 6144 | if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen, |
| 6145 | p + 2, end - ( p + 2 ), |
| 6146 | ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 ) |
| 6147 | { |
| 6148 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret ); |
| 6149 | return( ret ); |
| 6150 | } |
| 6151 | |
| 6152 | MBEDTLS_PUT_UINT16_BE( zlen, p, 0 ); |
| 6153 | p += 2 + zlen; |
| 6154 | |
| 6155 | MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx, |
| 6156 | MBEDTLS_DEBUG_ECDH_Z ); |
| 6157 | } |
| 6158 | else |
Neil Armstrong | 80f6f32 | 2022-05-03 17:56:38 +0200 | [diff] [blame] | 6159 | #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */ |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6160 | { |
| 6161 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 6162 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 6163 | } |
| 6164 | |
| 6165 | /* opaque psk<0..2^16-1>; */ |
| 6166 | if( end - p < 2 ) |
| 6167 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6168 | |
| 6169 | MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 ); |
| 6170 | p += 2; |
| 6171 | |
| 6172 | if( end < p || (size_t)( end - p ) < psk_len ) |
| 6173 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6174 | |
| 6175 | memcpy( p, psk, psk_len ); |
| 6176 | p += psk_len; |
| 6177 | |
| 6178 | ssl->handshake->pmslen = p - ssl->handshake->premaster; |
| 6179 | |
| 6180 | return( 0 ); |
| 6181 | } |
Neil Armstrong | 80f6f32 | 2022-05-03 17:56:38 +0200 | [diff] [blame] | 6182 | #endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ |
Jerry Yu | c2c673d | 2022-02-17 14:20:39 +0800 | [diff] [blame] | 6183 | |
| 6184 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6185 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | c2c673d | 2022-02-17 14:20:39 +0800 | [diff] [blame] | 6186 | static int ssl_write_hello_request( mbedtls_ssl_context *ssl ); |
| 6187 | |
| 6188 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 6189 | int mbedtls_ssl_resend_hello_request( mbedtls_ssl_context *ssl ) |
| 6190 | { |
| 6191 | /* If renegotiation is not enforced, retransmit until we would reach max |
| 6192 | * timeout if we were using the usual handshake doubling scheme */ |
| 6193 | if( ssl->conf->renego_max_records < 0 ) |
| 6194 | { |
| 6195 | uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1; |
| 6196 | unsigned char doublings = 1; |
| 6197 | |
| 6198 | while( ratio != 0 ) |
| 6199 | { |
| 6200 | ++doublings; |
| 6201 | ratio >>= 1; |
| 6202 | } |
| 6203 | |
| 6204 | if( ++ssl->renego_records_seen > doublings ) |
| 6205 | { |
| 6206 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) ); |
| 6207 | return( 0 ); |
| 6208 | } |
| 6209 | } |
| 6210 | |
| 6211 | return( ssl_write_hello_request( ssl ) ); |
| 6212 | } |
| 6213 | #endif |
| 6214 | #endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */ |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6215 | |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6216 | /* |
| 6217 | * Handshake functions |
| 6218 | */ |
| 6219 | #if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
| 6220 | /* No certificate support -> dummy functions */ |
| 6221 | int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl ) |
| 6222 | { |
| 6223 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 6224 | ssl->handshake->ciphersuite_info; |
| 6225 | |
| 6226 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) ); |
| 6227 | |
| 6228 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) |
| 6229 | { |
| 6230 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) ); |
| 6231 | ssl->state++; |
| 6232 | return( 0 ); |
| 6233 | } |
| 6234 | |
| 6235 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 6236 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 6237 | } |
| 6238 | |
| 6239 | int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl ) |
| 6240 | { |
| 6241 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 6242 | ssl->handshake->ciphersuite_info; |
| 6243 | |
| 6244 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) ); |
| 6245 | |
| 6246 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) |
| 6247 | { |
| 6248 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) ); |
| 6249 | ssl->state++; |
| 6250 | return( 0 ); |
| 6251 | } |
| 6252 | |
| 6253 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 6254 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 6255 | } |
| 6256 | |
| 6257 | #else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 6258 | /* Some certificate support -> implement write and parse */ |
| 6259 | |
| 6260 | int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl ) |
| 6261 | { |
| 6262 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
| 6263 | size_t i, n; |
| 6264 | const mbedtls_x509_crt *crt; |
| 6265 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 6266 | ssl->handshake->ciphersuite_info; |
| 6267 | |
| 6268 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) ); |
| 6269 | |
| 6270 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) |
| 6271 | { |
| 6272 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) ); |
| 6273 | ssl->state++; |
| 6274 | return( 0 ); |
| 6275 | } |
| 6276 | |
| 6277 | #if defined(MBEDTLS_SSL_CLI_C) |
| 6278 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 6279 | { |
| 6280 | if( ssl->handshake->client_auth == 0 ) |
| 6281 | { |
| 6282 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) ); |
| 6283 | ssl->state++; |
| 6284 | return( 0 ); |
| 6285 | } |
| 6286 | } |
| 6287 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 6288 | #if defined(MBEDTLS_SSL_SRV_C) |
| 6289 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 6290 | { |
| 6291 | if( mbedtls_ssl_own_cert( ssl ) == NULL ) |
| 6292 | { |
| 6293 | /* Should never happen because we shouldn't have picked the |
| 6294 | * ciphersuite if we don't have a certificate. */ |
| 6295 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 6296 | } |
| 6297 | } |
| 6298 | #endif |
| 6299 | |
| 6300 | MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) ); |
| 6301 | |
| 6302 | /* |
| 6303 | * 0 . 0 handshake type |
| 6304 | * 1 . 3 handshake length |
| 6305 | * 4 . 6 length of all certs |
| 6306 | * 7 . 9 length of cert. 1 |
| 6307 | * 10 . n-1 peer certificate |
| 6308 | * n . n+2 length of cert. 2 |
| 6309 | * n+3 . ... upper level cert, etc. |
| 6310 | */ |
| 6311 | i = 7; |
| 6312 | crt = mbedtls_ssl_own_cert( ssl ); |
| 6313 | |
| 6314 | while( crt != NULL ) |
| 6315 | { |
| 6316 | n = crt->raw.len; |
| 6317 | if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i ) |
| 6318 | { |
| 6319 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %" MBEDTLS_PRINTF_SIZET |
| 6320 | " > %" MBEDTLS_PRINTF_SIZET, |
| 6321 | i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN ) ); |
| 6322 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 6323 | } |
| 6324 | |
| 6325 | ssl->out_msg[i ] = MBEDTLS_BYTE_2( n ); |
| 6326 | ssl->out_msg[i + 1] = MBEDTLS_BYTE_1( n ); |
| 6327 | ssl->out_msg[i + 2] = MBEDTLS_BYTE_0( n ); |
| 6328 | |
| 6329 | i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n ); |
| 6330 | i += n; crt = crt->next; |
| 6331 | } |
| 6332 | |
| 6333 | ssl->out_msg[4] = MBEDTLS_BYTE_2( i - 7 ); |
| 6334 | ssl->out_msg[5] = MBEDTLS_BYTE_1( i - 7 ); |
| 6335 | ssl->out_msg[6] = MBEDTLS_BYTE_0( i - 7 ); |
| 6336 | |
| 6337 | ssl->out_msglen = i; |
| 6338 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 6339 | ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE; |
| 6340 | |
| 6341 | ssl->state++; |
| 6342 | |
| 6343 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
| 6344 | { |
| 6345 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); |
| 6346 | return( ret ); |
| 6347 | } |
| 6348 | |
| 6349 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) ); |
| 6350 | |
| 6351 | return( ret ); |
| 6352 | } |
| 6353 | |
| 6354 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) |
| 6355 | |
| 6356 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6357 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6358 | static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl, |
| 6359 | unsigned char *crt_buf, |
| 6360 | size_t crt_buf_len ) |
| 6361 | { |
| 6362 | mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert; |
| 6363 | |
| 6364 | if( peer_crt == NULL ) |
| 6365 | return( -1 ); |
| 6366 | |
| 6367 | if( peer_crt->raw.len != crt_buf_len ) |
| 6368 | return( -1 ); |
| 6369 | |
| 6370 | return( memcmp( peer_crt->raw.p, crt_buf, peer_crt->raw.len ) ); |
| 6371 | } |
| 6372 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6373 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6374 | static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl, |
| 6375 | unsigned char *crt_buf, |
| 6376 | size_t crt_buf_len ) |
| 6377 | { |
| 6378 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 6379 | unsigned char const * const peer_cert_digest = |
| 6380 | ssl->session->peer_cert_digest; |
| 6381 | mbedtls_md_type_t const peer_cert_digest_type = |
| 6382 | ssl->session->peer_cert_digest_type; |
| 6383 | mbedtls_md_info_t const * const digest_info = |
| 6384 | mbedtls_md_info_from_type( peer_cert_digest_type ); |
| 6385 | unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN]; |
| 6386 | size_t digest_len; |
| 6387 | |
| 6388 | if( peer_cert_digest == NULL || digest_info == NULL ) |
| 6389 | return( -1 ); |
| 6390 | |
| 6391 | digest_len = mbedtls_md_get_size( digest_info ); |
| 6392 | if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN ) |
| 6393 | return( -1 ); |
| 6394 | |
| 6395 | ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest ); |
| 6396 | if( ret != 0 ) |
| 6397 | return( -1 ); |
| 6398 | |
| 6399 | return( memcmp( tmp_digest, peer_cert_digest, digest_len ) ); |
| 6400 | } |
| 6401 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 6402 | #endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */ |
| 6403 | |
| 6404 | /* |
| 6405 | * Once the certificate message is read, parse it into a cert chain and |
| 6406 | * perform basic checks, but leave actual verification to the caller |
| 6407 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6408 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6409 | static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl, |
| 6410 | mbedtls_x509_crt *chain ) |
| 6411 | { |
| 6412 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 6413 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) |
| 6414 | int crt_cnt=0; |
| 6415 | #endif |
| 6416 | size_t i, n; |
| 6417 | uint8_t alert; |
| 6418 | |
| 6419 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) |
| 6420 | { |
| 6421 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
| 6422 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6423 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
| 6424 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
| 6425 | } |
| 6426 | |
| 6427 | if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ) |
| 6428 | { |
| 6429 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6430 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
| 6431 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
| 6432 | } |
| 6433 | |
| 6434 | if( ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 ) |
| 6435 | { |
| 6436 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
| 6437 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6438 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
| 6439 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
| 6440 | } |
| 6441 | |
| 6442 | i = mbedtls_ssl_hs_hdr_len( ssl ); |
| 6443 | |
| 6444 | /* |
| 6445 | * Same message structure as in mbedtls_ssl_write_certificate() |
| 6446 | */ |
| 6447 | n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2]; |
| 6448 | |
| 6449 | if( ssl->in_msg[i] != 0 || |
| 6450 | ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) ) |
| 6451 | { |
| 6452 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
| 6453 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6454 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
| 6455 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
| 6456 | } |
| 6457 | |
| 6458 | /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */ |
| 6459 | i += 3; |
| 6460 | |
| 6461 | /* Iterate through and parse the CRTs in the provided chain. */ |
| 6462 | while( i < ssl->in_hslen ) |
| 6463 | { |
| 6464 | /* Check that there's room for the next CRT's length fields. */ |
| 6465 | if ( i + 3 > ssl->in_hslen ) { |
| 6466 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
| 6467 | mbedtls_ssl_send_alert_message( ssl, |
| 6468 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6469 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
| 6470 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
| 6471 | } |
| 6472 | /* In theory, the CRT can be up to 2**24 Bytes, but we don't support |
| 6473 | * anything beyond 2**16 ~ 64K. */ |
| 6474 | if( ssl->in_msg[i] != 0 ) |
| 6475 | { |
| 6476 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
| 6477 | mbedtls_ssl_send_alert_message( ssl, |
| 6478 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6479 | MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT ); |
| 6480 | return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE ); |
| 6481 | } |
| 6482 | |
| 6483 | /* Read length of the next CRT in the chain. */ |
| 6484 | n = ( (unsigned int) ssl->in_msg[i + 1] << 8 ) |
| 6485 | | (unsigned int) ssl->in_msg[i + 2]; |
| 6486 | i += 3; |
| 6487 | |
| 6488 | if( n < 128 || i + n > ssl->in_hslen ) |
| 6489 | { |
| 6490 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
| 6491 | mbedtls_ssl_send_alert_message( ssl, |
| 6492 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6493 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
| 6494 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
| 6495 | } |
| 6496 | |
| 6497 | /* Check if we're handling the first CRT in the chain. */ |
| 6498 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) |
| 6499 | if( crt_cnt++ == 0 && |
| 6500 | ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
| 6501 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
| 6502 | { |
| 6503 | /* During client-side renegotiation, check that the server's |
| 6504 | * end-CRTs hasn't changed compared to the initial handshake, |
| 6505 | * mitigating the triple handshake attack. On success, reuse |
| 6506 | * the original end-CRT instead of parsing it again. */ |
| 6507 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) ); |
| 6508 | if( ssl_check_peer_crt_unchanged( ssl, |
| 6509 | &ssl->in_msg[i], |
| 6510 | n ) != 0 ) |
| 6511 | { |
| 6512 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) ); |
| 6513 | mbedtls_ssl_send_alert_message( ssl, |
| 6514 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6515 | MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED ); |
| 6516 | return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE ); |
| 6517 | } |
| 6518 | |
| 6519 | /* Now we can safely free the original chain. */ |
| 6520 | ssl_clear_peer_cert( ssl->session ); |
| 6521 | } |
| 6522 | #endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */ |
| 6523 | |
| 6524 | /* Parse the next certificate in the chain. */ |
| 6525 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 6526 | ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n ); |
| 6527 | #else |
| 6528 | /* If we don't need to store the CRT chain permanently, parse |
| 6529 | * it in-place from the input buffer instead of making a copy. */ |
| 6530 | ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n ); |
| 6531 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 6532 | switch( ret ) |
| 6533 | { |
| 6534 | case 0: /*ok*/ |
| 6535 | case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND: |
| 6536 | /* Ignore certificate with an unknown algorithm: maybe a |
| 6537 | prior certificate was already trusted. */ |
| 6538 | break; |
| 6539 | |
| 6540 | case MBEDTLS_ERR_X509_ALLOC_FAILED: |
| 6541 | alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR; |
| 6542 | goto crt_parse_der_failed; |
| 6543 | |
| 6544 | case MBEDTLS_ERR_X509_UNKNOWN_VERSION: |
| 6545 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 6546 | goto crt_parse_der_failed; |
| 6547 | |
| 6548 | default: |
| 6549 | alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT; |
| 6550 | crt_parse_der_failed: |
| 6551 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert ); |
| 6552 | MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret ); |
| 6553 | return( ret ); |
| 6554 | } |
| 6555 | |
| 6556 | i += n; |
| 6557 | } |
| 6558 | |
| 6559 | MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain ); |
| 6560 | return( 0 ); |
| 6561 | } |
| 6562 | |
| 6563 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6564 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6565 | static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl ) |
| 6566 | { |
| 6567 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 6568 | return( -1 ); |
| 6569 | |
| 6570 | if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) && |
| 6571 | ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 6572 | ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE && |
| 6573 | memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 ) |
| 6574 | { |
Ronald Cron | 1938588 | 2022-06-15 16:26:13 +0200 | [diff] [blame] | 6575 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "peer has no certificate" ) ); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6576 | return( 0 ); |
| 6577 | } |
| 6578 | return( -1 ); |
| 6579 | } |
| 6580 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 6581 | |
| 6582 | /* Check if a certificate message is expected. |
| 6583 | * Return either |
| 6584 | * - SSL_CERTIFICATE_EXPECTED, or |
| 6585 | * - SSL_CERTIFICATE_SKIP |
| 6586 | * indicating whether a Certificate message is expected or not. |
| 6587 | */ |
| 6588 | #define SSL_CERTIFICATE_EXPECTED 0 |
| 6589 | #define SSL_CERTIFICATE_SKIP 1 |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6590 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6591 | static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl, |
| 6592 | int authmode ) |
| 6593 | { |
| 6594 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 6595 | ssl->handshake->ciphersuite_info; |
| 6596 | |
| 6597 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) |
| 6598 | return( SSL_CERTIFICATE_SKIP ); |
| 6599 | |
| 6600 | #if defined(MBEDTLS_SSL_SRV_C) |
| 6601 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 6602 | { |
| 6603 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ) |
| 6604 | return( SSL_CERTIFICATE_SKIP ); |
| 6605 | |
| 6606 | if( authmode == MBEDTLS_SSL_VERIFY_NONE ) |
| 6607 | { |
| 6608 | ssl->session_negotiate->verify_result = |
| 6609 | MBEDTLS_X509_BADCERT_SKIP_VERIFY; |
| 6610 | return( SSL_CERTIFICATE_SKIP ); |
| 6611 | } |
| 6612 | } |
| 6613 | #else |
| 6614 | ((void) authmode); |
| 6615 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 6616 | |
| 6617 | return( SSL_CERTIFICATE_EXPECTED ); |
| 6618 | } |
| 6619 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6620 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6621 | static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl, |
| 6622 | int authmode, |
| 6623 | mbedtls_x509_crt *chain, |
| 6624 | void *rs_ctx ) |
| 6625 | { |
| 6626 | int ret = 0; |
| 6627 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 6628 | ssl->handshake->ciphersuite_info; |
| 6629 | int have_ca_chain = 0; |
| 6630 | |
| 6631 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *); |
| 6632 | void *p_vrfy; |
| 6633 | |
| 6634 | if( authmode == MBEDTLS_SSL_VERIFY_NONE ) |
| 6635 | return( 0 ); |
| 6636 | |
| 6637 | if( ssl->f_vrfy != NULL ) |
| 6638 | { |
| 6639 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) ); |
| 6640 | f_vrfy = ssl->f_vrfy; |
| 6641 | p_vrfy = ssl->p_vrfy; |
| 6642 | } |
| 6643 | else |
| 6644 | { |
| 6645 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) ); |
| 6646 | f_vrfy = ssl->conf->f_vrfy; |
| 6647 | p_vrfy = ssl->conf->p_vrfy; |
| 6648 | } |
| 6649 | |
| 6650 | /* |
| 6651 | * Main check: verify certificate |
| 6652 | */ |
| 6653 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 6654 | if( ssl->conf->f_ca_cb != NULL ) |
| 6655 | { |
| 6656 | ((void) rs_ctx); |
| 6657 | have_ca_chain = 1; |
| 6658 | |
| 6659 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) ); |
| 6660 | ret = mbedtls_x509_crt_verify_with_ca_cb( |
| 6661 | chain, |
| 6662 | ssl->conf->f_ca_cb, |
| 6663 | ssl->conf->p_ca_cb, |
| 6664 | ssl->conf->cert_profile, |
| 6665 | ssl->hostname, |
| 6666 | &ssl->session_negotiate->verify_result, |
| 6667 | f_vrfy, p_vrfy ); |
| 6668 | } |
| 6669 | else |
| 6670 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
| 6671 | { |
| 6672 | mbedtls_x509_crt *ca_chain; |
| 6673 | mbedtls_x509_crl *ca_crl; |
| 6674 | |
| 6675 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 6676 | if( ssl->handshake->sni_ca_chain != NULL ) |
| 6677 | { |
| 6678 | ca_chain = ssl->handshake->sni_ca_chain; |
| 6679 | ca_crl = ssl->handshake->sni_ca_crl; |
| 6680 | } |
| 6681 | else |
| 6682 | #endif |
| 6683 | { |
| 6684 | ca_chain = ssl->conf->ca_chain; |
| 6685 | ca_crl = ssl->conf->ca_crl; |
| 6686 | } |
| 6687 | |
| 6688 | if( ca_chain != NULL ) |
| 6689 | have_ca_chain = 1; |
| 6690 | |
| 6691 | ret = mbedtls_x509_crt_verify_restartable( |
| 6692 | chain, |
| 6693 | ca_chain, ca_crl, |
| 6694 | ssl->conf->cert_profile, |
| 6695 | ssl->hostname, |
| 6696 | &ssl->session_negotiate->verify_result, |
| 6697 | f_vrfy, p_vrfy, rs_ctx ); |
| 6698 | } |
| 6699 | |
| 6700 | if( ret != 0 ) |
| 6701 | { |
| 6702 | MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret ); |
| 6703 | } |
| 6704 | |
| 6705 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
| 6706 | if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS ) |
| 6707 | return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS ); |
| 6708 | #endif |
| 6709 | |
| 6710 | /* |
| 6711 | * Secondary checks: always done, but change 'ret' only if it was 0 |
| 6712 | */ |
| 6713 | |
| 6714 | #if defined(MBEDTLS_ECP_C) |
| 6715 | { |
| 6716 | const mbedtls_pk_context *pk = &chain->pk; |
| 6717 | |
Manuel Pégourié-Gonnard | 66b0d61 | 2022-06-17 10:49:29 +0200 | [diff] [blame] | 6718 | /* If certificate uses an EC key, make sure the curve is OK. |
| 6719 | * This is a public key, so it can't be opaque, so can_do() is a good |
| 6720 | * enough check to ensure pk_ec() is safe to use here. */ |
Leonid Rozenboim | 19e5973 | 2022-08-08 16:52:38 -0700 | [diff] [blame] | 6721 | if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) ) |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6722 | { |
Leonid Rozenboim | 19e5973 | 2022-08-08 16:52:38 -0700 | [diff] [blame] | 6723 | /* and in the unlikely case the above assumption no longer holds |
| 6724 | * we are making sure that pk_ec() here does not return a NULL |
| 6725 | */ |
| 6726 | const mbedtls_ecp_keypair *ec = mbedtls_pk_ec( *pk ); |
| 6727 | if( ec == NULL ) |
| 6728 | { |
Tom Cosgrove | 20c1137 | 2022-08-24 15:06:13 +0100 | [diff] [blame] | 6729 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_pk_ec() returned NULL" ) ); |
Leonid Rozenboim | 19e5973 | 2022-08-08 16:52:38 -0700 | [diff] [blame] | 6730 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 6731 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6732 | |
Leonid Rozenboim | 19e5973 | 2022-08-08 16:52:38 -0700 | [diff] [blame] | 6733 | if( mbedtls_ssl_check_curve( ssl, ec->grp.id ) != 0 ) |
| 6734 | { |
| 6735 | ssl->session_negotiate->verify_result |= |
| 6736 | MBEDTLS_X509_BADCERT_BAD_KEY; |
| 6737 | |
| 6738 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) ); |
| 6739 | if( ret == 0 ) |
| 6740 | ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE; |
| 6741 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6742 | } |
| 6743 | } |
| 6744 | #endif /* MBEDTLS_ECP_C */ |
| 6745 | |
| 6746 | if( mbedtls_ssl_check_cert_usage( chain, |
| 6747 | ciphersuite_info, |
| 6748 | ! ssl->conf->endpoint, |
| 6749 | &ssl->session_negotiate->verify_result ) != 0 ) |
| 6750 | { |
| 6751 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) ); |
| 6752 | if( ret == 0 ) |
| 6753 | ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE; |
| 6754 | } |
| 6755 | |
| 6756 | /* mbedtls_x509_crt_verify_with_profile is supposed to report a |
| 6757 | * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED, |
| 6758 | * with details encoded in the verification flags. All other kinds |
| 6759 | * of error codes, including those from the user provided f_vrfy |
| 6760 | * functions, are treated as fatal and lead to a failure of |
| 6761 | * ssl_parse_certificate even if verification was optional. */ |
| 6762 | if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL && |
| 6763 | ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED || |
| 6764 | ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE ) ) |
| 6765 | { |
| 6766 | ret = 0; |
| 6767 | } |
| 6768 | |
| 6769 | if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED ) |
| 6770 | { |
| 6771 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) ); |
| 6772 | ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED; |
| 6773 | } |
| 6774 | |
| 6775 | if( ret != 0 ) |
| 6776 | { |
| 6777 | uint8_t alert; |
| 6778 | |
| 6779 | /* The certificate may have been rejected for several reasons. |
| 6780 | Pick one and send the corresponding alert. Which alert to send |
| 6781 | may be a subject of debate in some cases. */ |
| 6782 | if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER ) |
| 6783 | alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED; |
| 6784 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH ) |
| 6785 | alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT; |
| 6786 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE ) |
| 6787 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 6788 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE ) |
| 6789 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 6790 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE ) |
| 6791 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 6792 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK ) |
| 6793 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 6794 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY ) |
| 6795 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 6796 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED ) |
| 6797 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED; |
| 6798 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED ) |
| 6799 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED; |
| 6800 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED ) |
| 6801 | alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA; |
| 6802 | else |
| 6803 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN; |
| 6804 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6805 | alert ); |
| 6806 | } |
| 6807 | |
| 6808 | #if defined(MBEDTLS_DEBUG_C) |
| 6809 | if( ssl->session_negotiate->verify_result != 0 ) |
| 6810 | { |
| 6811 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %08x", |
| 6812 | (unsigned int) ssl->session_negotiate->verify_result ) ); |
| 6813 | } |
| 6814 | else |
| 6815 | { |
| 6816 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) ); |
| 6817 | } |
| 6818 | #endif /* MBEDTLS_DEBUG_C */ |
| 6819 | |
| 6820 | return( ret ); |
| 6821 | } |
| 6822 | |
| 6823 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6824 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6825 | static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl, |
| 6826 | unsigned char *start, size_t len ) |
| 6827 | { |
| 6828 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 6829 | /* Remember digest of the peer's end-CRT. */ |
| 6830 | ssl->session_negotiate->peer_cert_digest = |
| 6831 | mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ); |
| 6832 | if( ssl->session_negotiate->peer_cert_digest == NULL ) |
| 6833 | { |
| 6834 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", |
| 6835 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ); |
| 6836 | mbedtls_ssl_send_alert_message( ssl, |
| 6837 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6838 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
| 6839 | |
| 6840 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 6841 | } |
| 6842 | |
| 6843 | ret = mbedtls_md( mbedtls_md_info_from_type( |
| 6844 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ), |
| 6845 | start, len, |
| 6846 | ssl->session_negotiate->peer_cert_digest ); |
| 6847 | |
| 6848 | ssl->session_negotiate->peer_cert_digest_type = |
| 6849 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE; |
| 6850 | ssl->session_negotiate->peer_cert_digest_len = |
| 6851 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN; |
| 6852 | |
| 6853 | return( ret ); |
| 6854 | } |
| 6855 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6856 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6857 | static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl, |
| 6858 | unsigned char *start, size_t len ) |
| 6859 | { |
| 6860 | unsigned char *end = start + len; |
| 6861 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 6862 | |
| 6863 | /* Make a copy of the peer's raw public key. */ |
| 6864 | mbedtls_pk_init( &ssl->handshake->peer_pubkey ); |
| 6865 | ret = mbedtls_pk_parse_subpubkey( &start, end, |
| 6866 | &ssl->handshake->peer_pubkey ); |
| 6867 | if( ret != 0 ) |
| 6868 | { |
| 6869 | /* We should have parsed the public key before. */ |
| 6870 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 6871 | } |
| 6872 | |
| 6873 | return( 0 ); |
| 6874 | } |
| 6875 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 6876 | |
| 6877 | int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl ) |
| 6878 | { |
| 6879 | int ret = 0; |
| 6880 | int crt_expected; |
| 6881 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 6882 | const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET |
| 6883 | ? ssl->handshake->sni_authmode |
| 6884 | : ssl->conf->authmode; |
| 6885 | #else |
| 6886 | const int authmode = ssl->conf->authmode; |
| 6887 | #endif |
| 6888 | void *rs_ctx = NULL; |
| 6889 | mbedtls_x509_crt *chain = NULL; |
| 6890 | |
| 6891 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) ); |
| 6892 | |
| 6893 | crt_expected = ssl_parse_certificate_coordinate( ssl, authmode ); |
| 6894 | if( crt_expected == SSL_CERTIFICATE_SKIP ) |
| 6895 | { |
| 6896 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) ); |
| 6897 | goto exit; |
| 6898 | } |
| 6899 | |
| 6900 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
| 6901 | if( ssl->handshake->ecrs_enabled && |
| 6902 | ssl->handshake->ecrs_state == ssl_ecrs_crt_verify ) |
| 6903 | { |
| 6904 | chain = ssl->handshake->ecrs_peer_cert; |
| 6905 | ssl->handshake->ecrs_peer_cert = NULL; |
| 6906 | goto crt_verify; |
| 6907 | } |
| 6908 | #endif |
| 6909 | |
| 6910 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) |
| 6911 | { |
| 6912 | /* mbedtls_ssl_read_record may have sent an alert already. We |
| 6913 | let it decide whether to alert. */ |
| 6914 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
| 6915 | goto exit; |
| 6916 | } |
| 6917 | |
| 6918 | #if defined(MBEDTLS_SSL_SRV_C) |
| 6919 | if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 ) |
| 6920 | { |
| 6921 | ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING; |
| 6922 | |
| 6923 | if( authmode != MBEDTLS_SSL_VERIFY_OPTIONAL ) |
| 6924 | ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE; |
| 6925 | |
| 6926 | goto exit; |
| 6927 | } |
| 6928 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 6929 | |
| 6930 | /* Clear existing peer CRT structure in case we tried to |
| 6931 | * reuse a session but it failed, and allocate a new one. */ |
| 6932 | ssl_clear_peer_cert( ssl->session_negotiate ); |
| 6933 | |
| 6934 | chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ); |
| 6935 | if( chain == NULL ) |
| 6936 | { |
| 6937 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", |
| 6938 | sizeof( mbedtls_x509_crt ) ) ); |
| 6939 | mbedtls_ssl_send_alert_message( ssl, |
| 6940 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6941 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
| 6942 | |
| 6943 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 6944 | goto exit; |
| 6945 | } |
| 6946 | mbedtls_x509_crt_init( chain ); |
| 6947 | |
| 6948 | ret = ssl_parse_certificate_chain( ssl, chain ); |
| 6949 | if( ret != 0 ) |
| 6950 | goto exit; |
| 6951 | |
| 6952 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
| 6953 | if( ssl->handshake->ecrs_enabled) |
| 6954 | ssl->handshake->ecrs_state = ssl_ecrs_crt_verify; |
| 6955 | |
| 6956 | crt_verify: |
| 6957 | if( ssl->handshake->ecrs_enabled) |
| 6958 | rs_ctx = &ssl->handshake->ecrs_ctx; |
| 6959 | #endif |
| 6960 | |
| 6961 | ret = ssl_parse_certificate_verify( ssl, authmode, |
| 6962 | chain, rs_ctx ); |
| 6963 | if( ret != 0 ) |
| 6964 | goto exit; |
| 6965 | |
| 6966 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 6967 | { |
| 6968 | unsigned char *crt_start, *pk_start; |
| 6969 | size_t crt_len, pk_len; |
| 6970 | |
| 6971 | /* We parse the CRT chain without copying, so |
| 6972 | * these pointers point into the input buffer, |
| 6973 | * and are hence still valid after freeing the |
| 6974 | * CRT chain. */ |
| 6975 | |
| 6976 | crt_start = chain->raw.p; |
| 6977 | crt_len = chain->raw.len; |
| 6978 | |
| 6979 | pk_start = chain->pk_raw.p; |
| 6980 | pk_len = chain->pk_raw.len; |
| 6981 | |
| 6982 | /* Free the CRT structures before computing |
| 6983 | * digest and copying the peer's public key. */ |
| 6984 | mbedtls_x509_crt_free( chain ); |
| 6985 | mbedtls_free( chain ); |
| 6986 | chain = NULL; |
| 6987 | |
| 6988 | ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len ); |
| 6989 | if( ret != 0 ) |
| 6990 | goto exit; |
| 6991 | |
| 6992 | ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len ); |
| 6993 | if( ret != 0 ) |
| 6994 | goto exit; |
| 6995 | } |
| 6996 | #else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 6997 | /* Pass ownership to session structure. */ |
| 6998 | ssl->session_negotiate->peer_cert = chain; |
| 6999 | chain = NULL; |
| 7000 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 7001 | |
| 7002 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) ); |
| 7003 | |
| 7004 | exit: |
| 7005 | |
| 7006 | if( ret == 0 ) |
| 7007 | ssl->state++; |
| 7008 | |
| 7009 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
| 7010 | if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS ) |
| 7011 | { |
| 7012 | ssl->handshake->ecrs_peer_cert = chain; |
| 7013 | chain = NULL; |
| 7014 | } |
| 7015 | #endif |
| 7016 | |
| 7017 | if( chain != NULL ) |
| 7018 | { |
| 7019 | mbedtls_x509_crt_free( chain ); |
| 7020 | mbedtls_free( chain ); |
| 7021 | } |
| 7022 | |
| 7023 | return( ret ); |
| 7024 | } |
| 7025 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 7026 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 7027 | #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] | 7028 | static void ssl_calc_finished_tls_sha256( |
| 7029 | mbedtls_ssl_context *ssl, unsigned char *buf, int from ) |
| 7030 | { |
| 7031 | int len = 12; |
| 7032 | const char *sender; |
| 7033 | unsigned char padbuf[32]; |
| 7034 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7035 | size_t hash_size; |
| 7036 | psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT; |
| 7037 | psa_status_t status; |
| 7038 | #else |
| 7039 | mbedtls_sha256_context sha256; |
| 7040 | #endif |
| 7041 | |
| 7042 | mbedtls_ssl_session *session = ssl->session_negotiate; |
| 7043 | if( !session ) |
| 7044 | session = ssl->session; |
| 7045 | |
| 7046 | sender = ( from == MBEDTLS_SSL_IS_CLIENT ) |
| 7047 | ? "client finished" |
| 7048 | : "server finished"; |
| 7049 | |
| 7050 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7051 | sha256_psa = psa_hash_operation_init(); |
| 7052 | |
| 7053 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) ); |
| 7054 | |
| 7055 | status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa ); |
| 7056 | if( status != PSA_SUCCESS ) |
| 7057 | { |
| 7058 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); |
| 7059 | return; |
| 7060 | } |
| 7061 | |
| 7062 | status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size ); |
| 7063 | if( status != PSA_SUCCESS ) |
| 7064 | { |
| 7065 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); |
| 7066 | return; |
| 7067 | } |
| 7068 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 ); |
| 7069 | #else |
| 7070 | |
| 7071 | mbedtls_sha256_init( &sha256 ); |
| 7072 | |
| 7073 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) ); |
| 7074 | |
| 7075 | mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 ); |
| 7076 | |
| 7077 | /* |
| 7078 | * TLSv1.2: |
| 7079 | * hash = PRF( master, finished_label, |
| 7080 | * Hash( handshake ) )[0.11] |
| 7081 | */ |
| 7082 | |
| 7083 | #if !defined(MBEDTLS_SHA256_ALT) |
| 7084 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *) |
| 7085 | sha256.state, sizeof( sha256.state ) ); |
| 7086 | #endif |
| 7087 | |
| 7088 | mbedtls_sha256_finish( &sha256, padbuf ); |
| 7089 | mbedtls_sha256_free( &sha256 ); |
| 7090 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 7091 | |
| 7092 | ssl->handshake->tls_prf( session->master, 48, sender, |
| 7093 | padbuf, 32, buf, len ); |
| 7094 | |
| 7095 | MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len ); |
| 7096 | |
| 7097 | mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) ); |
| 7098 | |
| 7099 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) ); |
| 7100 | } |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 7101 | #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] | 7102 | |
Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 7103 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 7104 | #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] | 7105 | static void ssl_calc_finished_tls_sha384( |
| 7106 | mbedtls_ssl_context *ssl, unsigned char *buf, int from ) |
| 7107 | { |
| 7108 | int len = 12; |
| 7109 | const char *sender; |
| 7110 | unsigned char padbuf[48]; |
| 7111 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7112 | size_t hash_size; |
| 7113 | psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT; |
| 7114 | psa_status_t status; |
| 7115 | #else |
| 7116 | mbedtls_sha512_context sha512; |
| 7117 | #endif |
| 7118 | |
| 7119 | mbedtls_ssl_session *session = ssl->session_negotiate; |
| 7120 | if( !session ) |
| 7121 | session = ssl->session; |
| 7122 | |
| 7123 | sender = ( from == MBEDTLS_SSL_IS_CLIENT ) |
| 7124 | ? "client finished" |
| 7125 | : "server finished"; |
| 7126 | |
| 7127 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7128 | sha384_psa = psa_hash_operation_init(); |
| 7129 | |
| 7130 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) ); |
| 7131 | |
| 7132 | status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa ); |
| 7133 | if( status != PSA_SUCCESS ) |
| 7134 | { |
| 7135 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); |
| 7136 | return; |
| 7137 | } |
| 7138 | |
| 7139 | status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size ); |
| 7140 | if( status != PSA_SUCCESS ) |
| 7141 | { |
| 7142 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); |
| 7143 | return; |
| 7144 | } |
| 7145 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 ); |
| 7146 | #else |
| 7147 | mbedtls_sha512_init( &sha512 ); |
| 7148 | |
| 7149 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) ); |
| 7150 | |
Andrzej Kurek | a242e83 | 2022-08-11 10:03:14 -0400 | [diff] [blame] | 7151 | mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 ); |
Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 7152 | |
| 7153 | /* |
| 7154 | * TLSv1.2: |
| 7155 | * hash = PRF( master, finished_label, |
| 7156 | * Hash( handshake ) )[0.11] |
| 7157 | */ |
| 7158 | |
| 7159 | #if !defined(MBEDTLS_SHA512_ALT) |
| 7160 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *) |
| 7161 | sha512.state, sizeof( sha512.state ) ); |
| 7162 | #endif |
| 7163 | mbedtls_sha512_finish( &sha512, padbuf ); |
| 7164 | |
| 7165 | mbedtls_sha512_free( &sha512 ); |
| 7166 | #endif |
| 7167 | |
| 7168 | ssl->handshake->tls_prf( session->master, 48, sender, |
| 7169 | padbuf, 48, buf, len ); |
| 7170 | |
| 7171 | MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len ); |
| 7172 | |
| 7173 | mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) ); |
| 7174 | |
| 7175 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) ); |
| 7176 | } |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 7177 | #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] | 7178 | |
Jerry Yu | aef0015 | 2022-02-17 14:27:31 +0800 | [diff] [blame] | 7179 | void mbedtls_ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl ) |
| 7180 | { |
| 7181 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) ); |
| 7182 | |
| 7183 | /* |
| 7184 | * Free our handshake params |
| 7185 | */ |
| 7186 | mbedtls_ssl_handshake_free( ssl ); |
| 7187 | mbedtls_free( ssl->handshake ); |
| 7188 | ssl->handshake = NULL; |
| 7189 | |
| 7190 | /* |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 7191 | * Free the previous transform and switch in the current one |
Jerry Yu | aef0015 | 2022-02-17 14:27:31 +0800 | [diff] [blame] | 7192 | */ |
| 7193 | if( ssl->transform ) |
| 7194 | { |
| 7195 | mbedtls_ssl_transform_free( ssl->transform ); |
| 7196 | mbedtls_free( ssl->transform ); |
| 7197 | } |
| 7198 | ssl->transform = ssl->transform_negotiate; |
| 7199 | ssl->transform_negotiate = NULL; |
| 7200 | |
| 7201 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) ); |
| 7202 | } |
| 7203 | |
Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 7204 | void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl ) |
| 7205 | { |
| 7206 | int resume = ssl->handshake->resume; |
| 7207 | |
| 7208 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) ); |
| 7209 | |
| 7210 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 7211 | if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
| 7212 | { |
| 7213 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE; |
| 7214 | ssl->renego_records_seen = 0; |
| 7215 | } |
| 7216 | #endif |
| 7217 | |
| 7218 | /* |
| 7219 | * Free the previous session and switch in the current one |
| 7220 | */ |
| 7221 | if( ssl->session ) |
| 7222 | { |
| 7223 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 7224 | /* RFC 7366 3.1: keep the EtM state */ |
| 7225 | ssl->session_negotiate->encrypt_then_mac = |
| 7226 | ssl->session->encrypt_then_mac; |
| 7227 | #endif |
| 7228 | |
| 7229 | mbedtls_ssl_session_free( ssl->session ); |
| 7230 | mbedtls_free( ssl->session ); |
| 7231 | } |
| 7232 | ssl->session = ssl->session_negotiate; |
| 7233 | ssl->session_negotiate = NULL; |
| 7234 | |
| 7235 | /* |
| 7236 | * Add cache entry |
| 7237 | */ |
| 7238 | if( ssl->conf->f_set_cache != NULL && |
| 7239 | ssl->session->id_len != 0 && |
| 7240 | resume == 0 ) |
| 7241 | { |
| 7242 | if( ssl->conf->f_set_cache( ssl->conf->p_cache, |
| 7243 | ssl->session->id, |
| 7244 | ssl->session->id_len, |
| 7245 | ssl->session ) != 0 ) |
| 7246 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) ); |
| 7247 | } |
| 7248 | |
| 7249 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 7250 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 7251 | ssl->handshake->flight != NULL ) |
| 7252 | { |
| 7253 | /* Cancel handshake timer */ |
| 7254 | mbedtls_ssl_set_timer( ssl, 0 ); |
| 7255 | |
| 7256 | /* Keep last flight around in case we need to resend it: |
| 7257 | * we need the handshake and transform structures for that */ |
| 7258 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) ); |
| 7259 | } |
| 7260 | else |
| 7261 | #endif |
| 7262 | mbedtls_ssl_handshake_wrapup_free_hs_transform( ssl ); |
| 7263 | |
| 7264 | ssl->state++; |
| 7265 | |
| 7266 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) ); |
| 7267 | } |
| 7268 | |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 7269 | int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl ) |
| 7270 | { |
| 7271 | int ret, hash_len; |
| 7272 | |
| 7273 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) ); |
| 7274 | |
| 7275 | mbedtls_ssl_update_out_pointers( ssl, ssl->transform_negotiate ); |
| 7276 | |
| 7277 | ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint ); |
| 7278 | |
| 7279 | /* |
| 7280 | * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites |
| 7281 | * may define some other value. Currently (early 2016), no defined |
| 7282 | * ciphersuite does this (and this is unlikely to change as activity has |
| 7283 | * moved to TLS 1.3 now) so we can keep the hardcoded 12 here. |
| 7284 | */ |
| 7285 | hash_len = 12; |
| 7286 | |
| 7287 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 7288 | ssl->verify_data_len = hash_len; |
| 7289 | memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len ); |
| 7290 | #endif |
| 7291 | |
| 7292 | ssl->out_msglen = 4 + hash_len; |
| 7293 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 7294 | ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED; |
| 7295 | |
| 7296 | /* |
| 7297 | * In case of session resuming, invert the client and server |
| 7298 | * ChangeCipherSpec messages order. |
| 7299 | */ |
| 7300 | if( ssl->handshake->resume != 0 ) |
| 7301 | { |
| 7302 | #if defined(MBEDTLS_SSL_CLI_C) |
| 7303 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 7304 | ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP; |
| 7305 | #endif |
| 7306 | #if defined(MBEDTLS_SSL_SRV_C) |
| 7307 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 7308 | ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC; |
| 7309 | #endif |
| 7310 | } |
| 7311 | else |
| 7312 | ssl->state++; |
| 7313 | |
| 7314 | /* |
| 7315 | * Switch to our negotiated transform and session parameters for outbound |
| 7316 | * data. |
| 7317 | */ |
| 7318 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) ); |
| 7319 | |
| 7320 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 7321 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 7322 | { |
| 7323 | unsigned char i; |
| 7324 | |
| 7325 | /* Remember current epoch settings for resending */ |
| 7326 | ssl->handshake->alt_transform_out = ssl->transform_out; |
| 7327 | memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, |
| 7328 | sizeof( ssl->handshake->alt_out_ctr ) ); |
| 7329 | |
| 7330 | /* Set sequence_number to zero */ |
| 7331 | memset( &ssl->cur_out_ctr[2], 0, sizeof( ssl->cur_out_ctr ) - 2 ); |
| 7332 | |
| 7333 | |
| 7334 | /* Increment epoch */ |
| 7335 | for( i = 2; i > 0; i-- ) |
| 7336 | if( ++ssl->cur_out_ctr[i - 1] != 0 ) |
| 7337 | break; |
| 7338 | |
| 7339 | /* The loop goes to its end iff the counter is wrapping */ |
| 7340 | if( i == 0 ) |
| 7341 | { |
| 7342 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) ); |
| 7343 | return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING ); |
| 7344 | } |
| 7345 | } |
| 7346 | else |
| 7347 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 7348 | memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) ); |
| 7349 | |
| 7350 | ssl->transform_out = ssl->transform_negotiate; |
| 7351 | ssl->session_out = ssl->session_negotiate; |
| 7352 | |
| 7353 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 7354 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 7355 | mbedtls_ssl_send_flight_completed( ssl ); |
| 7356 | #endif |
| 7357 | |
| 7358 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
| 7359 | { |
| 7360 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); |
| 7361 | return( ret ); |
| 7362 | } |
| 7363 | |
| 7364 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 7365 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 7366 | ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 ) |
| 7367 | { |
| 7368 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret ); |
| 7369 | return( ret ); |
| 7370 | } |
| 7371 | #endif |
| 7372 | |
| 7373 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) ); |
| 7374 | |
| 7375 | return( 0 ); |
| 7376 | } |
| 7377 | |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 7378 | #define SSL_MAX_HASH_LEN 12 |
| 7379 | |
| 7380 | int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl ) |
| 7381 | { |
| 7382 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 7383 | unsigned int hash_len = 12; |
| 7384 | unsigned char buf[SSL_MAX_HASH_LEN]; |
| 7385 | |
| 7386 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) ); |
| 7387 | |
| 7388 | ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 ); |
| 7389 | |
| 7390 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) |
| 7391 | { |
| 7392 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
| 7393 | goto exit; |
| 7394 | } |
| 7395 | |
| 7396 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) |
| 7397 | { |
| 7398 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) ); |
| 7399 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7400 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
| 7401 | ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; |
| 7402 | goto exit; |
| 7403 | } |
| 7404 | |
| 7405 | if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ) |
| 7406 | { |
| 7407 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7408 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
| 7409 | ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; |
| 7410 | goto exit; |
| 7411 | } |
| 7412 | |
| 7413 | if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len ) |
| 7414 | { |
| 7415 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) ); |
| 7416 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7417 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
| 7418 | ret = MBEDTLS_ERR_SSL_DECODE_ERROR; |
| 7419 | goto exit; |
| 7420 | } |
| 7421 | |
| 7422 | if( mbedtls_ct_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), |
| 7423 | buf, hash_len ) != 0 ) |
| 7424 | { |
| 7425 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) ); |
| 7426 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7427 | MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR ); |
| 7428 | ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE; |
| 7429 | goto exit; |
| 7430 | } |
| 7431 | |
| 7432 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 7433 | ssl->verify_data_len = hash_len; |
| 7434 | memcpy( ssl->peer_verify_data, buf, hash_len ); |
| 7435 | #endif |
| 7436 | |
| 7437 | if( ssl->handshake->resume != 0 ) |
| 7438 | { |
| 7439 | #if defined(MBEDTLS_SSL_CLI_C) |
| 7440 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 7441 | ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC; |
| 7442 | #endif |
| 7443 | #if defined(MBEDTLS_SSL_SRV_C) |
| 7444 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 7445 | ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP; |
| 7446 | #endif |
| 7447 | } |
| 7448 | else |
| 7449 | ssl->state++; |
| 7450 | |
| 7451 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 7452 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 7453 | mbedtls_ssl_recv_flight_completed( ssl ); |
| 7454 | #endif |
| 7455 | |
| 7456 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) ); |
| 7457 | |
| 7458 | exit: |
| 7459 | mbedtls_platform_zeroize( buf, hash_len ); |
| 7460 | return( ret ); |
| 7461 | } |
| 7462 | |
Jerry Yu | 392112c | 2022-02-17 14:34:10 +0800 | [diff] [blame] | 7463 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
| 7464 | /* |
| 7465 | * Helper to get TLS 1.2 PRF from ciphersuite |
| 7466 | * (Duplicates bits of logic from ssl_set_handshake_prfs().) |
| 7467 | */ |
| 7468 | static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id ) |
| 7469 | { |
Jerry Yu | 392112c | 2022-02-17 14:34:10 +0800 | [diff] [blame] | 7470 | const mbedtls_ssl_ciphersuite_t * const ciphersuite_info = |
Andrzej Kurek | 6832774 | 2022-10-03 06:18:18 -0400 | [diff] [blame] | 7471 | mbedtls_ssl_ciphersuite_from_id( ciphersuite_id ); |
| 7472 | #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] | 7473 | if( ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA384 ) |
Jerry Yu | 392112c | 2022-02-17 14:34:10 +0800 | [diff] [blame] | 7474 | return( tls_prf_sha384 ); |
Andrzej Kurek | 894edde | 2022-09-29 06:31:14 -0400 | [diff] [blame] | 7475 | else |
Jerry Yu | 392112c | 2022-02-17 14:34:10 +0800 | [diff] [blame] | 7476 | #endif |
Andrzej Kurek | 894edde | 2022-09-29 06:31:14 -0400 | [diff] [blame] | 7477 | #if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
| 7478 | { |
| 7479 | if( ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA256 ) |
| 7480 | return( tls_prf_sha256 ); |
| 7481 | } |
| 7482 | #endif |
| 7483 | #if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \ |
| 7484 | !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
| 7485 | (void) ciphersuite_info; |
| 7486 | #endif |
Andrzej Kurek | eabeb30 | 2022-10-17 07:52:51 -0400 | [diff] [blame] | 7487 | |
Andrzej Kurek | 894edde | 2022-09-29 06:31:14 -0400 | [diff] [blame] | 7488 | return( NULL ); |
Jerry Yu | 392112c | 2022-02-17 14:34:10 +0800 | [diff] [blame] | 7489 | } |
| 7490 | #endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */ |
Jerry Yu | e93ffcd | 2022-02-17 14:37:06 +0800 | [diff] [blame] | 7491 | |
| 7492 | static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf ) |
| 7493 | { |
| 7494 | ((void) tls_prf); |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 7495 | #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] | 7496 | if( tls_prf == tls_prf_sha384 ) |
| 7497 | { |
| 7498 | return( MBEDTLS_SSL_TLS_PRF_SHA384 ); |
| 7499 | } |
| 7500 | else |
| 7501 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 7502 | #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] | 7503 | if( tls_prf == tls_prf_sha256 ) |
| 7504 | { |
| 7505 | return( MBEDTLS_SSL_TLS_PRF_SHA256 ); |
| 7506 | } |
| 7507 | else |
| 7508 | #endif |
| 7509 | return( MBEDTLS_SSL_TLS_PRF_NONE ); |
| 7510 | } |
| 7511 | |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7512 | /* |
| 7513 | * Populate a transform structure with session keys and all the other |
| 7514 | * necessary information. |
| 7515 | * |
| 7516 | * Parameters: |
| 7517 | * - [in/out]: transform: structure to populate |
| 7518 | * [in] must be just initialised with mbedtls_ssl_transform_init() |
| 7519 | * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf() |
| 7520 | * - [in] ciphersuite |
| 7521 | * - [in] master |
| 7522 | * - [in] encrypt_then_mac |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7523 | * - [in] tls_prf: pointer to PRF to use for key derivation |
| 7524 | * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random |
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 7525 | * - [in] tls_version: TLS version |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7526 | * - [in] endpoint: client or server |
| 7527 | * - [in] ssl: used for: |
| 7528 | * - ssl->conf->{f,p}_export_keys |
| 7529 | * [in] optionally used for: |
| 7530 | * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg |
| 7531 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 7532 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7533 | static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform, |
| 7534 | int ciphersuite, |
| 7535 | const unsigned char master[48], |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 7536 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7537 | int encrypt_then_mac, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 7538 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7539 | ssl_tls_prf_t tls_prf, |
| 7540 | const unsigned char randbytes[64], |
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 7541 | mbedtls_ssl_protocol_version tls_version, |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7542 | unsigned endpoint, |
| 7543 | const mbedtls_ssl_context *ssl ) |
| 7544 | { |
| 7545 | int ret = 0; |
| 7546 | unsigned char keyblk[256]; |
| 7547 | unsigned char *key1; |
| 7548 | unsigned char *key2; |
| 7549 | unsigned char *mac_enc; |
| 7550 | unsigned char *mac_dec; |
| 7551 | size_t mac_key_len = 0; |
| 7552 | size_t iv_copy_len; |
| 7553 | size_t keylen; |
| 7554 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info; |
Neil Armstrong | 7fea33e | 2022-04-01 15:40:25 +0200 | [diff] [blame] | 7555 | mbedtls_ssl_mode_t ssl_mode; |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7556 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7557 | const mbedtls_cipher_info_t *cipher_info; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7558 | const mbedtls_md_info_t *md_info; |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7559 | #endif /* !MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7560 | |
| 7561 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7562 | psa_key_type_t key_type; |
| 7563 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7564 | psa_algorithm_t alg; |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7565 | psa_algorithm_t mac_alg = 0; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7566 | size_t key_bits; |
| 7567 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 7568 | #endif |
| 7569 | |
| 7570 | #if !defined(MBEDTLS_DEBUG_C) && \ |
| 7571 | !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 7572 | if( ssl->f_export_keys == NULL ) |
| 7573 | { |
| 7574 | ssl = NULL; /* make sure we don't use it except for these cases */ |
| 7575 | (void) ssl; |
| 7576 | } |
| 7577 | #endif |
| 7578 | |
| 7579 | /* |
| 7580 | * Some data just needs copying into the structure |
| 7581 | */ |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 7582 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7583 | transform->encrypt_then_mac = encrypt_then_mac; |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 7584 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 7585 | transform->tls_version = tls_version; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7586 | |
| 7587 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
| 7588 | memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) ); |
| 7589 | #endif |
| 7590 | |
| 7591 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 7592 | if( tls_version == MBEDTLS_SSL_VERSION_TLS1_3 ) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7593 | { |
| 7594 | /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform |
| 7595 | * generation separate. This should never happen. */ |
| 7596 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 7597 | } |
| 7598 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 7599 | |
| 7600 | /* |
| 7601 | * Get various info structures |
| 7602 | */ |
| 7603 | ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite ); |
| 7604 | if( ciphersuite_info == NULL ) |
| 7605 | { |
| 7606 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found", |
| 7607 | ciphersuite ) ); |
| 7608 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 7609 | } |
| 7610 | |
Neil Armstrong | ab555e0 | 2022-04-04 11:07:59 +0200 | [diff] [blame] | 7611 | ssl_mode = mbedtls_ssl_get_mode_from_ciphersuite( |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 7612 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Neil Armstrong | 7fea33e | 2022-04-01 15:40:25 +0200 | [diff] [blame] | 7613 | encrypt_then_mac, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 7614 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Neil Armstrong | 7fea33e | 2022-04-01 15:40:25 +0200 | [diff] [blame] | 7615 | ciphersuite_info ); |
| 7616 | |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7617 | if( ssl_mode == MBEDTLS_SSL_MODE_AEAD ) |
| 7618 | transform->taglen = |
| 7619 | ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16; |
| 7620 | |
| 7621 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7622 | if( ( status = mbedtls_ssl_cipher_to_psa( ciphersuite_info->cipher, |
| 7623 | transform->taglen, |
| 7624 | &alg, |
| 7625 | &key_type, |
| 7626 | &key_bits ) ) != PSA_SUCCESS ) |
| 7627 | { |
| 7628 | ret = psa_ssl_status_to_mbedtls( status ); |
| 7629 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_cipher_to_psa", ret ); |
| 7630 | goto end; |
| 7631 | } |
| 7632 | #else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7633 | cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher ); |
| 7634 | if( cipher_info == NULL ) |
| 7635 | { |
| 7636 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %u not found", |
| 7637 | ciphersuite_info->cipher ) ); |
| 7638 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 7639 | } |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7640 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7641 | |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7642 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Manuel Pégourié-Gonnard | abac037 | 2022-07-18 13:41:11 +0200 | [diff] [blame] | 7643 | mac_alg = mbedtls_hash_info_psa_from_md( ciphersuite_info->mac ); |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7644 | if( mac_alg == 0 ) |
| 7645 | { |
Manuel Pégourié-Gonnard | abac037 | 2022-07-18 13:41:11 +0200 | [diff] [blame] | 7646 | 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] | 7647 | (unsigned) ciphersuite_info->mac ) ); |
| 7648 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 7649 | } |
| 7650 | #else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7651 | md_info = mbedtls_md_info_from_type( ciphersuite_info->mac ); |
| 7652 | if( md_info == NULL ) |
| 7653 | { |
| 7654 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %u not found", |
| 7655 | (unsigned) ciphersuite_info->mac ) ); |
| 7656 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 7657 | } |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7658 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7659 | |
| 7660 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 7661 | /* Copy own and peer's CID if the use of the CID |
| 7662 | * extension has been negotiated. */ |
| 7663 | if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED ) |
| 7664 | { |
| 7665 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) ); |
| 7666 | |
| 7667 | transform->in_cid_len = ssl->own_cid_len; |
| 7668 | memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len ); |
| 7669 | MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid, |
| 7670 | transform->in_cid_len ); |
| 7671 | |
| 7672 | transform->out_cid_len = ssl->handshake->peer_cid_len; |
| 7673 | memcpy( transform->out_cid, ssl->handshake->peer_cid, |
| 7674 | ssl->handshake->peer_cid_len ); |
| 7675 | MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid, |
| 7676 | transform->out_cid_len ); |
| 7677 | } |
| 7678 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 7679 | |
| 7680 | /* |
| 7681 | * Compute key block using the PRF |
| 7682 | */ |
| 7683 | ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 ); |
| 7684 | if( ret != 0 ) |
| 7685 | { |
| 7686 | MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret ); |
| 7687 | return( ret ); |
| 7688 | } |
| 7689 | |
| 7690 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s", |
| 7691 | mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) ); |
| 7692 | MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 ); |
| 7693 | MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 ); |
| 7694 | MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 ); |
| 7695 | |
| 7696 | /* |
| 7697 | * Determine the appropriate key, IV and MAC length. |
| 7698 | */ |
| 7699 | |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7700 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7701 | keylen = PSA_BITS_TO_BYTES(key_bits); |
| 7702 | #else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7703 | keylen = mbedtls_cipher_info_get_key_bitlen( cipher_info ) / 8; |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7704 | #endif |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7705 | |
| 7706 | #if defined(MBEDTLS_GCM_C) || \ |
| 7707 | defined(MBEDTLS_CCM_C) || \ |
| 7708 | defined(MBEDTLS_CHACHAPOLY_C) |
Neil Armstrong | 7fea33e | 2022-04-01 15:40:25 +0200 | [diff] [blame] | 7709 | if( ssl_mode == MBEDTLS_SSL_MODE_AEAD ) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7710 | { |
| 7711 | size_t explicit_ivlen; |
| 7712 | |
| 7713 | transform->maclen = 0; |
| 7714 | mac_key_len = 0; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7715 | |
| 7716 | /* All modes haves 96-bit IVs, but the length of the static parts vary |
| 7717 | * with mode and version: |
| 7718 | * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes |
| 7719 | * (to be concatenated with a dynamically chosen IV of 8 Bytes) |
| 7720 | * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's |
| 7721 | * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record |
| 7722 | * sequence number). |
| 7723 | */ |
| 7724 | transform->ivlen = 12; |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7725 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7726 | if( key_type == PSA_KEY_TYPE_CHACHA20 ) |
| 7727 | #else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7728 | if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CHACHAPOLY ) |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7729 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7730 | transform->fixed_ivlen = 12; |
| 7731 | else |
| 7732 | transform->fixed_ivlen = 4; |
| 7733 | |
| 7734 | /* Minimum length of encrypted record */ |
| 7735 | explicit_ivlen = transform->ivlen - transform->fixed_ivlen; |
| 7736 | transform->minlen = explicit_ivlen + transform->taglen; |
| 7737 | } |
| 7738 | else |
| 7739 | #endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */ |
| 7740 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
Neil Armstrong | 7fea33e | 2022-04-01 15:40:25 +0200 | [diff] [blame] | 7741 | if( ssl_mode == MBEDTLS_SSL_MODE_STREAM || |
| 7742 | ssl_mode == MBEDTLS_SSL_MODE_CBC || |
| 7743 | ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM ) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7744 | { |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7745 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Neil Armstrong | d1be767 | 2022-04-04 11:21:41 +0200 | [diff] [blame] | 7746 | size_t block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ); |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7747 | #else |
| 7748 | size_t block_size = cipher_info->block_size; |
| 7749 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 7750 | |
| 7751 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7752 | /* Get MAC length */ |
| 7753 | mac_key_len = PSA_HASH_LENGTH(mac_alg); |
| 7754 | #else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7755 | /* Initialize HMAC contexts */ |
| 7756 | if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 || |
| 7757 | ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 ) |
| 7758 | { |
| 7759 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret ); |
| 7760 | goto end; |
| 7761 | } |
| 7762 | |
| 7763 | /* Get MAC length */ |
| 7764 | mac_key_len = mbedtls_md_get_size( md_info ); |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7765 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7766 | transform->maclen = mac_key_len; |
| 7767 | |
| 7768 | /* IV length */ |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7769 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Neil Armstrong | 2230e6c | 2022-04-27 10:36:14 +0200 | [diff] [blame] | 7770 | transform->ivlen = PSA_CIPHER_IV_LENGTH( key_type, alg ); |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7771 | #else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7772 | transform->ivlen = cipher_info->iv_size; |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7773 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7774 | |
| 7775 | /* Minimum length */ |
Neil Armstrong | 7fea33e | 2022-04-01 15:40:25 +0200 | [diff] [blame] | 7776 | if( ssl_mode == MBEDTLS_SSL_MODE_STREAM ) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7777 | transform->minlen = transform->maclen; |
| 7778 | else |
| 7779 | { |
| 7780 | /* |
| 7781 | * GenericBlockCipher: |
| 7782 | * 1. if EtM is in use: one block plus MAC |
| 7783 | * otherwise: * first multiple of blocklen greater than maclen |
| 7784 | * 2. IV |
| 7785 | */ |
| 7786 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Neil Armstrong | 7fea33e | 2022-04-01 15:40:25 +0200 | [diff] [blame] | 7787 | if( ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM ) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7788 | { |
| 7789 | transform->minlen = transform->maclen |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7790 | + block_size; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7791 | } |
| 7792 | else |
| 7793 | #endif |
| 7794 | { |
| 7795 | transform->minlen = transform->maclen |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7796 | + block_size |
| 7797 | - transform->maclen % block_size; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7798 | } |
| 7799 | |
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 7800 | if( tls_version == MBEDTLS_SSL_VERSION_TLS1_2 ) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7801 | { |
| 7802 | transform->minlen += transform->ivlen; |
| 7803 | } |
| 7804 | else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7805 | { |
| 7806 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 7807 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 7808 | goto end; |
| 7809 | } |
| 7810 | } |
| 7811 | } |
| 7812 | else |
| 7813 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
| 7814 | { |
| 7815 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 7816 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 7817 | } |
| 7818 | |
| 7819 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u", |
| 7820 | (unsigned) keylen, |
| 7821 | (unsigned) transform->minlen, |
| 7822 | (unsigned) transform->ivlen, |
| 7823 | (unsigned) transform->maclen ) ); |
| 7824 | |
| 7825 | /* |
| 7826 | * Finally setup the cipher contexts, IVs and MAC secrets. |
| 7827 | */ |
| 7828 | #if defined(MBEDTLS_SSL_CLI_C) |
| 7829 | if( endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 7830 | { |
| 7831 | key1 = keyblk + mac_key_len * 2; |
| 7832 | key2 = keyblk + mac_key_len * 2 + keylen; |
| 7833 | |
| 7834 | mac_enc = keyblk; |
| 7835 | mac_dec = keyblk + mac_key_len; |
| 7836 | |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7837 | iv_copy_len = ( transform->fixed_ivlen ) ? |
| 7838 | transform->fixed_ivlen : transform->ivlen; |
| 7839 | memcpy( transform->iv_enc, key2 + keylen, iv_copy_len ); |
| 7840 | memcpy( transform->iv_dec, key2 + keylen + iv_copy_len, |
| 7841 | iv_copy_len ); |
| 7842 | } |
| 7843 | else |
| 7844 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 7845 | #if defined(MBEDTLS_SSL_SRV_C) |
| 7846 | if( endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 7847 | { |
| 7848 | key1 = keyblk + mac_key_len * 2 + keylen; |
| 7849 | key2 = keyblk + mac_key_len * 2; |
| 7850 | |
| 7851 | mac_enc = keyblk + mac_key_len; |
| 7852 | mac_dec = keyblk; |
| 7853 | |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7854 | iv_copy_len = ( transform->fixed_ivlen ) ? |
| 7855 | transform->fixed_ivlen : transform->ivlen; |
| 7856 | memcpy( transform->iv_dec, key1 + keylen, iv_copy_len ); |
| 7857 | memcpy( transform->iv_enc, key1 + keylen + iv_copy_len, |
| 7858 | iv_copy_len ); |
| 7859 | } |
| 7860 | else |
| 7861 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 7862 | { |
| 7863 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 7864 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 7865 | goto end; |
| 7866 | } |
| 7867 | |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7868 | if( ssl != NULL && ssl->f_export_keys != NULL ) |
| 7869 | { |
| 7870 | ssl->f_export_keys( ssl->p_export_keys, |
| 7871 | MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET, |
| 7872 | master, 48, |
| 7873 | randbytes + 32, |
| 7874 | randbytes, |
| 7875 | tls_prf_get_type( tls_prf ) ); |
| 7876 | } |
| 7877 | |
| 7878 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7879 | transform->psa_alg = alg; |
| 7880 | |
| 7881 | if ( alg != MBEDTLS_SSL_NULL_CIPHER ) |
| 7882 | { |
| 7883 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 7884 | psa_set_key_algorithm( &attributes, alg ); |
| 7885 | psa_set_key_type( &attributes, key_type ); |
| 7886 | |
| 7887 | if( ( status = psa_import_key( &attributes, |
| 7888 | key1, |
| 7889 | PSA_BITS_TO_BYTES( key_bits ), |
| 7890 | &transform->psa_key_enc ) ) != PSA_SUCCESS ) |
| 7891 | { |
| 7892 | MBEDTLS_SSL_DEBUG_RET( 3, "psa_import_key", (int)status ); |
| 7893 | ret = psa_ssl_status_to_mbedtls( status ); |
| 7894 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret ); |
| 7895 | goto end; |
| 7896 | } |
| 7897 | |
| 7898 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 7899 | |
| 7900 | if( ( status = psa_import_key( &attributes, |
| 7901 | key2, |
| 7902 | PSA_BITS_TO_BYTES( key_bits ), |
| 7903 | &transform->psa_key_dec ) ) != PSA_SUCCESS ) |
| 7904 | { |
| 7905 | ret = psa_ssl_status_to_mbedtls( status ); |
| 7906 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret ); |
| 7907 | goto end; |
| 7908 | } |
| 7909 | } |
| 7910 | #else |
| 7911 | if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc, |
| 7912 | cipher_info ) ) != 0 ) |
| 7913 | { |
| 7914 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret ); |
| 7915 | goto end; |
| 7916 | } |
| 7917 | |
| 7918 | if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec, |
| 7919 | cipher_info ) ) != 0 ) |
| 7920 | { |
| 7921 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret ); |
| 7922 | goto end; |
| 7923 | } |
| 7924 | |
| 7925 | if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1, |
| 7926 | (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ), |
| 7927 | MBEDTLS_ENCRYPT ) ) != 0 ) |
| 7928 | { |
| 7929 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret ); |
| 7930 | goto end; |
| 7931 | } |
| 7932 | |
| 7933 | if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2, |
| 7934 | (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ), |
| 7935 | MBEDTLS_DECRYPT ) ) != 0 ) |
| 7936 | { |
| 7937 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret ); |
| 7938 | goto end; |
| 7939 | } |
| 7940 | |
| 7941 | #if defined(MBEDTLS_CIPHER_MODE_CBC) |
| 7942 | if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CBC ) |
| 7943 | { |
| 7944 | if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc, |
| 7945 | MBEDTLS_PADDING_NONE ) ) != 0 ) |
| 7946 | { |
| 7947 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret ); |
| 7948 | goto end; |
| 7949 | } |
| 7950 | |
| 7951 | if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec, |
| 7952 | MBEDTLS_PADDING_NONE ) ) != 0 ) |
| 7953 | { |
| 7954 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret ); |
| 7955 | goto end; |
| 7956 | } |
| 7957 | } |
| 7958 | #endif /* MBEDTLS_CIPHER_MODE_CBC */ |
| 7959 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 7960 | |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 7961 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
| 7962 | /* For HMAC-based ciphersuites, initialize the HMAC transforms. |
| 7963 | For AEAD-based ciphersuites, there is nothing to do here. */ |
| 7964 | if( mac_key_len != 0 ) |
| 7965 | { |
| 7966 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7967 | transform->psa_mac_alg = PSA_ALG_HMAC( mac_alg ); |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 7968 | |
| 7969 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE ); |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7970 | psa_set_key_algorithm( &attributes, PSA_ALG_HMAC( mac_alg ) ); |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 7971 | psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC ); |
| 7972 | |
| 7973 | if( ( status = psa_import_key( &attributes, |
| 7974 | mac_enc, mac_key_len, |
| 7975 | &transform->psa_mac_enc ) ) != PSA_SUCCESS ) |
| 7976 | { |
| 7977 | ret = psa_ssl_status_to_mbedtls( status ); |
| 7978 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret ); |
| 7979 | goto end; |
| 7980 | } |
| 7981 | |
Ronald Cron | fb39f15 | 2022-03-25 14:36:28 +0100 | [diff] [blame] | 7982 | if( ( transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER ) || |
Andrzej Kurek | 8c95ac4 | 2022-08-17 16:17:00 -0400 | [diff] [blame] | 7983 | ( ( transform->psa_alg == PSA_ALG_CBC_NO_PADDING ) |
| 7984 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
| 7985 | && ( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED ) |
| 7986 | #endif |
| 7987 | ) ) |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 7988 | /* mbedtls_ct_hmac() requires the key to be exportable */ |
| 7989 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT | |
| 7990 | PSA_KEY_USAGE_VERIFY_HASH ); |
| 7991 | else |
| 7992 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH ); |
| 7993 | |
| 7994 | if( ( status = psa_import_key( &attributes, |
| 7995 | mac_dec, mac_key_len, |
| 7996 | &transform->psa_mac_dec ) ) != PSA_SUCCESS ) |
| 7997 | { |
| 7998 | ret = psa_ssl_status_to_mbedtls( status ); |
| 7999 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret ); |
| 8000 | goto end; |
| 8001 | } |
| 8002 | #else |
| 8003 | ret = mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len ); |
| 8004 | if( ret != 0 ) |
| 8005 | goto end; |
| 8006 | ret = mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len ); |
| 8007 | if( ret != 0 ) |
| 8008 | goto end; |
| 8009 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 8010 | } |
| 8011 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
| 8012 | |
| 8013 | ((void) mac_dec); |
| 8014 | ((void) mac_enc); |
| 8015 | |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8016 | end: |
| 8017 | mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) ); |
| 8018 | return( ret ); |
| 8019 | } |
| 8020 | |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 8021 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 8022 | int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl, |
| 8023 | unsigned char *hash, size_t *hashlen, |
| 8024 | unsigned char *data, size_t data_len, |
| 8025 | mbedtls_md_type_t md_alg ) |
| 8026 | { |
| 8027 | psa_status_t status; |
| 8028 | psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT; |
Manuel Pégourié-Gonnard | abac037 | 2022-07-18 13:41:11 +0200 | [diff] [blame] | 8029 | 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] | 8030 | |
| 8031 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) ); |
| 8032 | |
| 8033 | if( ( status = psa_hash_setup( &hash_operation, |
| 8034 | hash_alg ) ) != PSA_SUCCESS ) |
| 8035 | { |
| 8036 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status ); |
| 8037 | goto exit; |
| 8038 | } |
| 8039 | |
| 8040 | if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes, |
| 8041 | 64 ) ) != PSA_SUCCESS ) |
| 8042 | { |
| 8043 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status ); |
| 8044 | goto exit; |
| 8045 | } |
| 8046 | |
| 8047 | if( ( status = psa_hash_update( &hash_operation, |
| 8048 | data, data_len ) ) != PSA_SUCCESS ) |
| 8049 | { |
| 8050 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status ); |
| 8051 | goto exit; |
| 8052 | } |
| 8053 | |
| 8054 | if( ( status = psa_hash_finish( &hash_operation, hash, PSA_HASH_MAX_SIZE, |
| 8055 | hashlen ) ) != PSA_SUCCESS ) |
| 8056 | { |
| 8057 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status ); |
| 8058 | goto exit; |
| 8059 | } |
| 8060 | |
| 8061 | exit: |
| 8062 | if( status != PSA_SUCCESS ) |
| 8063 | { |
| 8064 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 8065 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
| 8066 | switch( status ) |
| 8067 | { |
| 8068 | case PSA_ERROR_NOT_SUPPORTED: |
| 8069 | return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE ); |
| 8070 | case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */ |
| 8071 | case PSA_ERROR_BUFFER_TOO_SMALL: |
| 8072 | return( MBEDTLS_ERR_MD_BAD_INPUT_DATA ); |
| 8073 | case PSA_ERROR_INSUFFICIENT_MEMORY: |
| 8074 | return( MBEDTLS_ERR_MD_ALLOC_FAILED ); |
| 8075 | default: |
| 8076 | return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED ); |
| 8077 | } |
| 8078 | } |
| 8079 | return( 0 ); |
| 8080 | } |
| 8081 | |
| 8082 | #else |
| 8083 | |
| 8084 | int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl, |
| 8085 | unsigned char *hash, size_t *hashlen, |
| 8086 | unsigned char *data, size_t data_len, |
| 8087 | mbedtls_md_type_t md_alg ) |
| 8088 | { |
| 8089 | int ret = 0; |
| 8090 | mbedtls_md_context_t ctx; |
| 8091 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg ); |
| 8092 | *hashlen = mbedtls_md_get_size( md_info ); |
| 8093 | |
| 8094 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) ); |
| 8095 | |
| 8096 | mbedtls_md_init( &ctx ); |
| 8097 | |
| 8098 | /* |
| 8099 | * digitally-signed struct { |
| 8100 | * opaque client_random[32]; |
| 8101 | * opaque server_random[32]; |
| 8102 | * ServerDHParams params; |
| 8103 | * }; |
| 8104 | */ |
| 8105 | if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 ) |
| 8106 | { |
| 8107 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret ); |
| 8108 | goto exit; |
| 8109 | } |
| 8110 | if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 ) |
| 8111 | { |
| 8112 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret ); |
| 8113 | goto exit; |
| 8114 | } |
| 8115 | if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 ) |
| 8116 | { |
| 8117 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret ); |
| 8118 | goto exit; |
| 8119 | } |
| 8120 | if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 ) |
| 8121 | { |
| 8122 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret ); |
| 8123 | goto exit; |
| 8124 | } |
| 8125 | if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 ) |
| 8126 | { |
| 8127 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret ); |
| 8128 | goto exit; |
| 8129 | } |
| 8130 | |
| 8131 | exit: |
| 8132 | mbedtls_md_free( &ctx ); |
| 8133 | |
| 8134 | if( ret != 0 ) |
| 8135 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 8136 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
| 8137 | |
| 8138 | return( ret ); |
| 8139 | } |
| 8140 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 8141 | |
Jerry Yu | d9d91da | 2022-02-17 14:57:06 +0800 | [diff] [blame] | 8142 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
| 8143 | |
Gabor Mezei | a3d016c | 2022-05-10 12:44:09 +0200 | [diff] [blame] | 8144 | /* Find the preferred hash for a given signature algorithm. */ |
| 8145 | unsigned int mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg( |
| 8146 | mbedtls_ssl_context *ssl, |
| 8147 | unsigned int sig_alg ) |
Jerry Yu | d9d91da | 2022-02-17 14:57:06 +0800 | [diff] [blame] | 8148 | { |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 8149 | unsigned int i; |
Gabor Mezei | a3d016c | 2022-05-10 12:44:09 +0200 | [diff] [blame] | 8150 | uint16_t *received_sig_algs = ssl->handshake->received_sig_algs; |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 8151 | |
| 8152 | if( sig_alg == MBEDTLS_SSL_SIG_ANON ) |
Gabor Mezei | a3d016c | 2022-05-10 12:44:09 +0200 | [diff] [blame] | 8153 | return( MBEDTLS_SSL_HASH_NONE ); |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 8154 | |
Gabor Mezei | a3d016c | 2022-05-10 12:44:09 +0200 | [diff] [blame] | 8155 | for( i = 0; received_sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ ) |
Jerry Yu | d9d91da | 2022-02-17 14:57:06 +0800 | [diff] [blame] | 8156 | { |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 8157 | unsigned int hash_alg_received = |
| 8158 | MBEDTLS_SSL_TLS12_HASH_ALG_FROM_SIG_AND_HASH_ALG( |
| 8159 | received_sig_algs[i] ); |
| 8160 | unsigned int sig_alg_received = |
| 8161 | MBEDTLS_SSL_TLS12_SIG_ALG_FROM_SIG_AND_HASH_ALG( |
| 8162 | received_sig_algs[i] ); |
| 8163 | |
| 8164 | if( sig_alg == sig_alg_received ) |
| 8165 | { |
| 8166 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 8167 | if( ssl->handshake->key_cert && ssl->handshake->key_cert->key ) |
| 8168 | { |
Neil Armstrong | 96eceb8 | 2022-06-30 18:05:05 +0200 | [diff] [blame] | 8169 | psa_algorithm_t psa_hash_alg = |
Manuel Pégourié-Gonnard | abac037 | 2022-07-18 13:41:11 +0200 | [diff] [blame] | 8170 | mbedtls_hash_info_psa_from_md( hash_alg_received ); |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 8171 | |
Neil Armstrong | 96eceb8 | 2022-06-30 18:05:05 +0200 | [diff] [blame] | 8172 | if( sig_alg_received == MBEDTLS_SSL_SIG_ECDSA && |
| 8173 | ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key, |
| 8174 | PSA_ALG_ECDSA( psa_hash_alg ), |
| 8175 | PSA_KEY_USAGE_SIGN_HASH ) ) |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 8176 | continue; |
| 8177 | |
Neil Armstrong | 96eceb8 | 2022-06-30 18:05:05 +0200 | [diff] [blame] | 8178 | if( sig_alg_received == MBEDTLS_SSL_SIG_RSA && |
Neil Armstrong | 971f30d | 2022-07-01 16:23:50 +0200 | [diff] [blame] | 8179 | ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key, |
| 8180 | PSA_ALG_RSA_PKCS1V15_SIGN( |
| 8181 | psa_hash_alg ), |
| 8182 | PSA_KEY_USAGE_SIGN_HASH ) ) |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 8183 | continue; |
| 8184 | } |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 8185 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Neil Armstrong | 96eceb8 | 2022-06-30 18:05:05 +0200 | [diff] [blame] | 8186 | |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 8187 | return( hash_alg_received ); |
| 8188 | } |
Jerry Yu | d9d91da | 2022-02-17 14:57:06 +0800 | [diff] [blame] | 8189 | } |
Jerry Yu | d9d91da | 2022-02-17 14:57:06 +0800 | [diff] [blame] | 8190 | |
Gabor Mezei | a3d016c | 2022-05-10 12:44:09 +0200 | [diff] [blame] | 8191 | return( MBEDTLS_SSL_HASH_NONE ); |
Jerry Yu | d9d91da | 2022-02-17 14:57:06 +0800 | [diff] [blame] | 8192 | } |
| 8193 | |
| 8194 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 8195 | |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8196 | /* Serialization of TLS 1.2 sessions: |
| 8197 | * |
| 8198 | * struct { |
| 8199 | * uint64 start_time; |
| 8200 | * uint8 ciphersuite[2]; // defined by the standard |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8201 | * uint8 session_id_len; // at most 32 |
| 8202 | * opaque session_id[32]; |
| 8203 | * opaque master[48]; // fixed length in the standard |
| 8204 | * uint32 verify_result; |
| 8205 | * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert |
| 8206 | * opaque ticket<0..2^24-1>; // length 0 means no ticket |
| 8207 | * uint32 ticket_lifetime; |
| 8208 | * uint8 mfl_code; // up to 255 according to standard |
| 8209 | * uint8 encrypt_then_mac; // 0 or 1 |
| 8210 | * } serialized_session_tls12; |
| 8211 | * |
| 8212 | */ |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 8213 | static size_t ssl_tls12_session_save( const mbedtls_ssl_session *session, |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8214 | unsigned char *buf, |
| 8215 | size_t buf_len ) |
| 8216 | { |
| 8217 | unsigned char *p = buf; |
| 8218 | size_t used = 0; |
| 8219 | |
| 8220 | #if defined(MBEDTLS_HAVE_TIME) |
| 8221 | uint64_t start; |
| 8222 | #endif |
| 8223 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 8224 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 8225 | size_t cert_len; |
| 8226 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 8227 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 8228 | |
| 8229 | /* |
| 8230 | * Time |
| 8231 | */ |
| 8232 | #if defined(MBEDTLS_HAVE_TIME) |
| 8233 | used += 8; |
| 8234 | |
| 8235 | if( used <= buf_len ) |
| 8236 | { |
| 8237 | start = (uint64_t) session->start; |
| 8238 | |
| 8239 | MBEDTLS_PUT_UINT64_BE( start, p, 0 ); |
| 8240 | p += 8; |
| 8241 | } |
| 8242 | #endif /* MBEDTLS_HAVE_TIME */ |
| 8243 | |
| 8244 | /* |
| 8245 | * Basic mandatory fields |
| 8246 | */ |
| 8247 | used += 2 /* ciphersuite */ |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8248 | + 1 /* id_len */ |
| 8249 | + sizeof( session->id ) |
| 8250 | + sizeof( session->master ) |
| 8251 | + 4; /* verify_result */ |
| 8252 | |
| 8253 | if( used <= buf_len ) |
| 8254 | { |
| 8255 | MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 0 ); |
| 8256 | p += 2; |
| 8257 | |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8258 | *p++ = MBEDTLS_BYTE_0( session->id_len ); |
| 8259 | memcpy( p, session->id, 32 ); |
| 8260 | p += 32; |
| 8261 | |
| 8262 | memcpy( p, session->master, 48 ); |
| 8263 | p += 48; |
| 8264 | |
| 8265 | MBEDTLS_PUT_UINT32_BE( session->verify_result, p, 0 ); |
| 8266 | p += 4; |
| 8267 | } |
| 8268 | |
| 8269 | /* |
| 8270 | * Peer's end-entity certificate |
| 8271 | */ |
| 8272 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 8273 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 8274 | if( session->peer_cert == NULL ) |
| 8275 | cert_len = 0; |
| 8276 | else |
| 8277 | cert_len = session->peer_cert->raw.len; |
| 8278 | |
| 8279 | used += 3 + cert_len; |
| 8280 | |
| 8281 | if( used <= buf_len ) |
| 8282 | { |
| 8283 | *p++ = MBEDTLS_BYTE_2( cert_len ); |
| 8284 | *p++ = MBEDTLS_BYTE_1( cert_len ); |
| 8285 | *p++ = MBEDTLS_BYTE_0( cert_len ); |
| 8286 | |
| 8287 | if( session->peer_cert != NULL ) |
| 8288 | { |
| 8289 | memcpy( p, session->peer_cert->raw.p, cert_len ); |
| 8290 | p += cert_len; |
| 8291 | } |
| 8292 | } |
| 8293 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 8294 | if( session->peer_cert_digest != NULL ) |
| 8295 | { |
| 8296 | used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len; |
| 8297 | if( used <= buf_len ) |
| 8298 | { |
| 8299 | *p++ = (unsigned char) session->peer_cert_digest_type; |
| 8300 | *p++ = (unsigned char) session->peer_cert_digest_len; |
| 8301 | memcpy( p, session->peer_cert_digest, |
| 8302 | session->peer_cert_digest_len ); |
| 8303 | p += session->peer_cert_digest_len; |
| 8304 | } |
| 8305 | } |
| 8306 | else |
| 8307 | { |
| 8308 | used += 2; |
| 8309 | if( used <= buf_len ) |
| 8310 | { |
| 8311 | *p++ = (unsigned char) MBEDTLS_MD_NONE; |
| 8312 | *p++ = 0; |
| 8313 | } |
| 8314 | } |
| 8315 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 8316 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 8317 | |
| 8318 | /* |
| 8319 | * Session ticket if any, plus associated data |
| 8320 | */ |
| 8321 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
| 8322 | used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */ |
| 8323 | |
| 8324 | if( used <= buf_len ) |
| 8325 | { |
| 8326 | *p++ = MBEDTLS_BYTE_2( session->ticket_len ); |
| 8327 | *p++ = MBEDTLS_BYTE_1( session->ticket_len ); |
| 8328 | *p++ = MBEDTLS_BYTE_0( session->ticket_len ); |
| 8329 | |
| 8330 | if( session->ticket != NULL ) |
| 8331 | { |
| 8332 | memcpy( p, session->ticket, session->ticket_len ); |
| 8333 | p += session->ticket_len; |
| 8334 | } |
| 8335 | |
| 8336 | MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 ); |
| 8337 | p += 4; |
| 8338 | } |
| 8339 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
| 8340 | |
| 8341 | /* |
| 8342 | * Misc extension-related info |
| 8343 | */ |
| 8344 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 8345 | used += 1; |
| 8346 | |
| 8347 | if( used <= buf_len ) |
| 8348 | *p++ = session->mfl_code; |
| 8349 | #endif |
| 8350 | |
| 8351 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 8352 | used += 1; |
| 8353 | |
| 8354 | if( used <= buf_len ) |
| 8355 | *p++ = MBEDTLS_BYTE_0( session->encrypt_then_mac ); |
| 8356 | #endif |
| 8357 | |
| 8358 | return( used ); |
| 8359 | } |
| 8360 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 8361 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 8362 | static int ssl_tls12_session_load( mbedtls_ssl_session *session, |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8363 | const unsigned char *buf, |
| 8364 | size_t len ) |
| 8365 | { |
| 8366 | #if defined(MBEDTLS_HAVE_TIME) |
| 8367 | uint64_t start; |
| 8368 | #endif |
| 8369 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 8370 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 8371 | size_t cert_len; |
| 8372 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 8373 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 8374 | |
| 8375 | const unsigned char *p = buf; |
| 8376 | const unsigned char * const end = buf + len; |
| 8377 | |
| 8378 | /* |
| 8379 | * Time |
| 8380 | */ |
| 8381 | #if defined(MBEDTLS_HAVE_TIME) |
| 8382 | if( 8 > (size_t)( end - p ) ) |
| 8383 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8384 | |
| 8385 | start = ( (uint64_t) p[0] << 56 ) | |
| 8386 | ( (uint64_t) p[1] << 48 ) | |
| 8387 | ( (uint64_t) p[2] << 40 ) | |
| 8388 | ( (uint64_t) p[3] << 32 ) | |
| 8389 | ( (uint64_t) p[4] << 24 ) | |
| 8390 | ( (uint64_t) p[5] << 16 ) | |
| 8391 | ( (uint64_t) p[6] << 8 ) | |
| 8392 | ( (uint64_t) p[7] ); |
| 8393 | p += 8; |
| 8394 | |
| 8395 | session->start = (time_t) start; |
| 8396 | #endif /* MBEDTLS_HAVE_TIME */ |
| 8397 | |
| 8398 | /* |
| 8399 | * Basic mandatory fields |
| 8400 | */ |
Thomas Daubney | 20f89a9 | 2022-06-20 15:12:19 +0100 | [diff] [blame] | 8401 | if( 2 + 1 + 32 + 48 + 4 > (size_t)( end - p ) ) |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8402 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8403 | |
| 8404 | session->ciphersuite = ( p[0] << 8 ) | p[1]; |
| 8405 | p += 2; |
| 8406 | |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8407 | session->id_len = *p++; |
| 8408 | memcpy( session->id, p, 32 ); |
| 8409 | p += 32; |
| 8410 | |
| 8411 | memcpy( session->master, p, 48 ); |
| 8412 | p += 48; |
| 8413 | |
| 8414 | session->verify_result = ( (uint32_t) p[0] << 24 ) | |
| 8415 | ( (uint32_t) p[1] << 16 ) | |
| 8416 | ( (uint32_t) p[2] << 8 ) | |
| 8417 | ( (uint32_t) p[3] ); |
| 8418 | p += 4; |
| 8419 | |
| 8420 | /* Immediately clear invalid pointer values that have been read, in case |
| 8421 | * we exit early before we replaced them with valid ones. */ |
| 8422 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 8423 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 8424 | session->peer_cert = NULL; |
| 8425 | #else |
| 8426 | session->peer_cert_digest = NULL; |
| 8427 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 8428 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 8429 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
| 8430 | session->ticket = NULL; |
| 8431 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
| 8432 | |
| 8433 | /* |
| 8434 | * Peer certificate |
| 8435 | */ |
| 8436 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 8437 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 8438 | /* Deserialize CRT from the end of the ticket. */ |
| 8439 | if( 3 > (size_t)( end - p ) ) |
| 8440 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8441 | |
| 8442 | cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2]; |
| 8443 | p += 3; |
| 8444 | |
| 8445 | if( cert_len != 0 ) |
| 8446 | { |
| 8447 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 8448 | |
| 8449 | if( cert_len > (size_t)( end - p ) ) |
| 8450 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8451 | |
| 8452 | session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ); |
| 8453 | |
| 8454 | if( session->peer_cert == NULL ) |
| 8455 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 8456 | |
| 8457 | mbedtls_x509_crt_init( session->peer_cert ); |
| 8458 | |
| 8459 | if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert, |
| 8460 | p, cert_len ) ) != 0 ) |
| 8461 | { |
| 8462 | mbedtls_x509_crt_free( session->peer_cert ); |
| 8463 | mbedtls_free( session->peer_cert ); |
| 8464 | session->peer_cert = NULL; |
| 8465 | return( ret ); |
| 8466 | } |
| 8467 | |
| 8468 | p += cert_len; |
| 8469 | } |
| 8470 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 8471 | /* Deserialize CRT digest from the end of the ticket. */ |
| 8472 | if( 2 > (size_t)( end - p ) ) |
| 8473 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8474 | |
| 8475 | session->peer_cert_digest_type = (mbedtls_md_type_t) *p++; |
| 8476 | session->peer_cert_digest_len = (size_t) *p++; |
| 8477 | |
| 8478 | if( session->peer_cert_digest_len != 0 ) |
| 8479 | { |
| 8480 | const mbedtls_md_info_t *md_info = |
| 8481 | mbedtls_md_info_from_type( session->peer_cert_digest_type ); |
| 8482 | if( md_info == NULL ) |
| 8483 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8484 | if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) ) |
| 8485 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8486 | |
| 8487 | if( session->peer_cert_digest_len > (size_t)( end - p ) ) |
| 8488 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8489 | |
| 8490 | session->peer_cert_digest = |
| 8491 | mbedtls_calloc( 1, session->peer_cert_digest_len ); |
| 8492 | if( session->peer_cert_digest == NULL ) |
| 8493 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 8494 | |
| 8495 | memcpy( session->peer_cert_digest, p, |
| 8496 | session->peer_cert_digest_len ); |
| 8497 | p += session->peer_cert_digest_len; |
| 8498 | } |
| 8499 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 8500 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 8501 | |
| 8502 | /* |
| 8503 | * Session ticket and associated data |
| 8504 | */ |
| 8505 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
| 8506 | if( 3 > (size_t)( end - p ) ) |
| 8507 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8508 | |
| 8509 | session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2]; |
| 8510 | p += 3; |
| 8511 | |
| 8512 | if( session->ticket_len != 0 ) |
| 8513 | { |
| 8514 | if( session->ticket_len > (size_t)( end - p ) ) |
| 8515 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8516 | |
| 8517 | session->ticket = mbedtls_calloc( 1, session->ticket_len ); |
| 8518 | if( session->ticket == NULL ) |
| 8519 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 8520 | |
| 8521 | memcpy( session->ticket, p, session->ticket_len ); |
| 8522 | p += session->ticket_len; |
| 8523 | } |
| 8524 | |
| 8525 | if( 4 > (size_t)( end - p ) ) |
| 8526 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8527 | |
| 8528 | session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) | |
| 8529 | ( (uint32_t) p[1] << 16 ) | |
| 8530 | ( (uint32_t) p[2] << 8 ) | |
| 8531 | ( (uint32_t) p[3] ); |
| 8532 | p += 4; |
| 8533 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
| 8534 | |
| 8535 | /* |
| 8536 | * Misc extension-related info |
| 8537 | */ |
| 8538 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 8539 | if( 1 > (size_t)( end - p ) ) |
| 8540 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8541 | |
| 8542 | session->mfl_code = *p++; |
| 8543 | #endif |
| 8544 | |
| 8545 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 8546 | if( 1 > (size_t)( end - p ) ) |
| 8547 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8548 | |
| 8549 | session->encrypt_then_mac = *p++; |
| 8550 | #endif |
| 8551 | |
| 8552 | /* Done, should have consumed entire buffer */ |
| 8553 | if( p != end ) |
| 8554 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8555 | |
| 8556 | return( 0 ); |
| 8557 | } |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 8558 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 8559 | |
XiaokangQian | 75d40ef | 2022-04-20 11:05:24 +0000 | [diff] [blame] | 8560 | int mbedtls_ssl_validate_ciphersuite( |
| 8561 | const mbedtls_ssl_context *ssl, |
| 8562 | const mbedtls_ssl_ciphersuite_t *suite_info, |
| 8563 | mbedtls_ssl_protocol_version min_tls_version, |
| 8564 | mbedtls_ssl_protocol_version max_tls_version ) |
| 8565 | { |
| 8566 | (void) ssl; |
| 8567 | |
| 8568 | if( suite_info == NULL ) |
| 8569 | return( -1 ); |
| 8570 | |
| 8571 | if( ( suite_info->min_tls_version > max_tls_version ) || |
| 8572 | ( suite_info->max_tls_version < min_tls_version ) ) |
| 8573 | { |
| 8574 | return( -1 ); |
| 8575 | } |
| 8576 | |
XiaokangQian | 060d867 | 2022-04-21 09:24:56 +0000 | [diff] [blame] | 8577 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_CLI_C) |
XiaokangQian | 75d40ef | 2022-04-20 11:05:24 +0000 | [diff] [blame] | 8578 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
| 8579 | if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE && |
| 8580 | mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 ) |
| 8581 | { |
| 8582 | return( -1 ); |
| 8583 | } |
| 8584 | #endif |
| 8585 | |
| 8586 | /* Don't suggest PSK-based ciphersuite if no PSK is available. */ |
| 8587 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
| 8588 | if( mbedtls_ssl_ciphersuite_uses_psk( suite_info ) && |
| 8589 | mbedtls_ssl_conf_has_static_psk( ssl->conf ) == 0 ) |
| 8590 | { |
| 8591 | return( -1 ); |
| 8592 | } |
| 8593 | #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ |
| 8594 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 8595 | |
| 8596 | return( 0 ); |
| 8597 | } |
| 8598 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 8599 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 8600 | /* |
| 8601 | * Function for writing a signature algorithm extension. |
| 8602 | * |
| 8603 | * The `extension_data` field of signature algorithm contains a `SignatureSchemeList` |
| 8604 | * value (TLS 1.3 RFC8446): |
| 8605 | * enum { |
| 8606 | * .... |
| 8607 | * ecdsa_secp256r1_sha256( 0x0403 ), |
| 8608 | * ecdsa_secp384r1_sha384( 0x0503 ), |
| 8609 | * ecdsa_secp521r1_sha512( 0x0603 ), |
| 8610 | * .... |
| 8611 | * } SignatureScheme; |
| 8612 | * |
| 8613 | * struct { |
| 8614 | * SignatureScheme supported_signature_algorithms<2..2^16-2>; |
| 8615 | * } SignatureSchemeList; |
| 8616 | * |
| 8617 | * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm` |
| 8618 | * value (TLS 1.2 RFC5246): |
| 8619 | * enum { |
| 8620 | * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5), |
| 8621 | * sha512(6), (255) |
| 8622 | * } HashAlgorithm; |
| 8623 | * |
| 8624 | * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) } |
| 8625 | * SignatureAlgorithm; |
| 8626 | * |
| 8627 | * struct { |
| 8628 | * HashAlgorithm hash; |
| 8629 | * SignatureAlgorithm signature; |
| 8630 | * } SignatureAndHashAlgorithm; |
| 8631 | * |
| 8632 | * SignatureAndHashAlgorithm |
| 8633 | * supported_signature_algorithms<2..2^16-2>; |
| 8634 | * |
| 8635 | * The TLS 1.3 signature algorithm extension was defined to be a compatible |
| 8636 | * generalization of the TLS 1.2 signature algorithm extension. |
| 8637 | * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by |
| 8638 | * `SignatureScheme` field of TLS 1.3 |
| 8639 | * |
| 8640 | */ |
| 8641 | int mbedtls_ssl_write_sig_alg_ext( mbedtls_ssl_context *ssl, unsigned char *buf, |
| 8642 | const unsigned char *end, size_t *out_len ) |
| 8643 | { |
| 8644 | unsigned char *p = buf; |
| 8645 | unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */ |
| 8646 | size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */ |
| 8647 | |
| 8648 | *out_len = 0; |
| 8649 | |
| 8650 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding signature_algorithms extension" ) ); |
| 8651 | |
| 8652 | /* Check if we have space for header and length field: |
| 8653 | * - extension_type (2 bytes) |
| 8654 | * - extension_data_length (2 bytes) |
| 8655 | * - supported_signature_algorithms_length (2 bytes) |
| 8656 | */ |
| 8657 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 ); |
| 8658 | p += 6; |
| 8659 | |
| 8660 | /* |
| 8661 | * Write supported_signature_algorithms |
| 8662 | */ |
| 8663 | supported_sig_alg = p; |
| 8664 | const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs( ssl ); |
| 8665 | if( sig_alg == NULL ) |
| 8666 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); |
| 8667 | |
| 8668 | for( ; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++ ) |
| 8669 | { |
Jerry Yu | 53f5c15 | 2022-06-22 20:24:38 +0800 | [diff] [blame] | 8670 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "got signature scheme [%x] %s", |
| 8671 | *sig_alg, |
| 8672 | mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) ); |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 8673 | if( ! mbedtls_ssl_sig_alg_is_supported( ssl, *sig_alg ) ) |
| 8674 | continue; |
| 8675 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 ); |
| 8676 | MBEDTLS_PUT_UINT16_BE( *sig_alg, p, 0 ); |
| 8677 | p += 2; |
Jerry Yu | 80dd5db | 2022-06-22 19:30:32 +0800 | [diff] [blame] | 8678 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "sent signature scheme [%x] %s", |
Jerry Yu | f3b46b5 | 2022-06-19 16:52:27 +0800 | [diff] [blame] | 8679 | *sig_alg, |
| 8680 | mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) ); |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 8681 | } |
| 8682 | |
| 8683 | /* Length of supported_signature_algorithms */ |
| 8684 | supported_sig_alg_len = p - supported_sig_alg; |
| 8685 | if( supported_sig_alg_len == 0 ) |
| 8686 | { |
| 8687 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "No signature algorithms defined." ) ); |
| 8688 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 8689 | } |
| 8690 | |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 8691 | MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SIG_ALG, buf, 0 ); |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 8692 | MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len + 2, buf, 2 ); |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 8693 | MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len, buf, 4 ); |
| 8694 | |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 8695 | *out_len = p - buf; |
| 8696 | |
| 8697 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 8698 | ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SIG_ALG; |
| 8699 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 8700 | return( 0 ); |
| 8701 | } |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 8702 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 8703 | |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 8704 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 8705 | /* |
| 8706 | * mbedtls_ssl_parse_server_name_ext |
| 8707 | * |
| 8708 | * Structure of server_name extension: |
| 8709 | * |
| 8710 | * enum { |
| 8711 | * host_name(0), (255) |
| 8712 | * } NameType; |
| 8713 | * opaque HostName<1..2^16-1>; |
| 8714 | * |
| 8715 | * struct { |
| 8716 | * NameType name_type; |
| 8717 | * select (name_type) { |
| 8718 | * case host_name: HostName; |
| 8719 | * } name; |
| 8720 | * } ServerName; |
| 8721 | * struct { |
| 8722 | * ServerName server_name_list<1..2^16-1> |
| 8723 | * } ServerNameList; |
| 8724 | */ |
Ronald Cron | ce7d76e | 2022-07-08 18:56:49 +0200 | [diff] [blame] | 8725 | MBEDTLS_CHECK_RETURN_CRITICAL |
XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 8726 | int mbedtls_ssl_parse_server_name_ext( mbedtls_ssl_context *ssl, |
| 8727 | const unsigned char *buf, |
| 8728 | const unsigned char *end ) |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 8729 | { |
| 8730 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 8731 | const unsigned char *p = buf; |
XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 8732 | size_t server_name_list_len, hostname_len; |
| 8733 | const unsigned char *server_name_list_end; |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 8734 | |
XiaokangQian | f2a9420 | 2022-05-20 06:44:24 +0000 | [diff] [blame] | 8735 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "parse ServerName extension" ) ); |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 8736 | |
| 8737 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 ); |
XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 8738 | server_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 ); |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 8739 | p += 2; |
| 8740 | |
XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 8741 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, server_name_list_len ); |
| 8742 | server_name_list_end = p + server_name_list_len; |
XiaokangQian | 75fe8c7 | 2022-06-15 09:42:45 +0000 | [diff] [blame] | 8743 | while( p < server_name_list_end ) |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 8744 | { |
XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 8745 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end, 3 ); |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 8746 | hostname_len = MBEDTLS_GET_UINT16_BE( p, 1 ); |
XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 8747 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end, |
| 8748 | hostname_len + 3 ); |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 8749 | |
| 8750 | if( p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME ) |
| 8751 | { |
XiaokangQian | 75fe8c7 | 2022-06-15 09:42:45 +0000 | [diff] [blame] | 8752 | /* sni_name is intended to be used only during the parsing of the |
| 8753 | * ClientHello message (it is reset to NULL before the end of |
| 8754 | * the message parsing). Thus it is ok to just point to the |
| 8755 | * reception buffer and not make a copy of it. |
| 8756 | */ |
XiaokangQian | f2a9420 | 2022-05-20 06:44:24 +0000 | [diff] [blame] | 8757 | ssl->handshake->sni_name = p + 3; |
| 8758 | ssl->handshake->sni_name_len = hostname_len; |
| 8759 | if( ssl->conf->f_sni == NULL ) |
| 8760 | return( 0 ); |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 8761 | ret = ssl->conf->f_sni( ssl->conf->p_sni, |
XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 8762 | ssl, p + 3, hostname_len ); |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 8763 | if( ret != 0 ) |
| 8764 | { |
XiaokangQian | f2a9420 | 2022-05-20 06:44:24 +0000 | [diff] [blame] | 8765 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_sni_wrapper", ret ); |
XiaokangQian | 129aeb9 | 2022-06-02 09:29:18 +0000 | [diff] [blame] | 8766 | MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME, |
| 8767 | MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME ); |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 8768 | return( MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME ); |
| 8769 | } |
| 8770 | return( 0 ); |
| 8771 | } |
| 8772 | |
| 8773 | p += hostname_len + 3; |
| 8774 | } |
| 8775 | |
| 8776 | return( 0 ); |
| 8777 | } |
| 8778 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
| 8779 | |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8780 | #if defined(MBEDTLS_SSL_ALPN) |
Ronald Cron | ce7d76e | 2022-07-08 18:56:49 +0200 | [diff] [blame] | 8781 | MBEDTLS_CHECK_RETURN_CRITICAL |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8782 | int mbedtls_ssl_parse_alpn_ext( mbedtls_ssl_context *ssl, |
| 8783 | const unsigned char *buf, |
| 8784 | const unsigned char *end ) |
| 8785 | { |
| 8786 | const unsigned char *p = buf; |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 8787 | size_t protocol_name_list_len; |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 8788 | const unsigned char *protocol_name_list; |
| 8789 | const unsigned char *protocol_name_list_end; |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 8790 | size_t protocol_name_len; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8791 | |
| 8792 | /* If ALPN not configured, just ignore the extension */ |
| 8793 | if( ssl->conf->alpn_list == NULL ) |
| 8794 | return( 0 ); |
| 8795 | |
| 8796 | /* |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 8797 | * RFC7301, section 3.1 |
| 8798 | * opaque ProtocolName<1..2^8-1>; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8799 | * |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 8800 | * struct { |
| 8801 | * ProtocolName protocol_name_list<2..2^16-1> |
| 8802 | * } ProtocolNameList; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8803 | */ |
| 8804 | |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 8805 | /* |
XiaokangQian | 0b776e2 | 2022-06-24 09:04:59 +0000 | [diff] [blame] | 8806 | * protocol_name_list_len 2 bytes |
| 8807 | * protocol_name_len 1 bytes |
| 8808 | * protocol_name >=1 byte |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 8809 | */ |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8810 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 ); |
| 8811 | |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 8812 | protocol_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 ); |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8813 | p += 2; |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 8814 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, protocol_name_list_len ); |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 8815 | protocol_name_list = p; |
| 8816 | protocol_name_list_end = p + protocol_name_list_len; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8817 | |
| 8818 | /* Validate peer's list (lengths) */ |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 8819 | while( p < protocol_name_list_end ) |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8820 | { |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 8821 | protocol_name_len = *p++; |
| 8822 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, protocol_name_list_end, |
| 8823 | protocol_name_len ); |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 8824 | if( protocol_name_len == 0 ) |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 8825 | { |
| 8826 | MBEDTLS_SSL_PEND_FATAL_ALERT( |
| 8827 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER, |
| 8828 | MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER ); |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8829 | return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER ); |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 8830 | } |
| 8831 | |
| 8832 | p += protocol_name_len; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8833 | } |
| 8834 | |
| 8835 | /* Use our order of preference */ |
| 8836 | for( const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++ ) |
| 8837 | { |
| 8838 | size_t const alpn_len = strlen( *alpn ); |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 8839 | p = protocol_name_list; |
| 8840 | while( p < protocol_name_list_end ) |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8841 | { |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 8842 | protocol_name_len = *p++; |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 8843 | if( protocol_name_len == alpn_len && |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 8844 | memcmp( p, *alpn, alpn_len ) == 0 ) |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8845 | { |
| 8846 | ssl->alpn_chosen = *alpn; |
| 8847 | return( 0 ); |
| 8848 | } |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 8849 | |
| 8850 | p += protocol_name_len; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8851 | } |
| 8852 | } |
| 8853 | |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 8854 | /* If we get here, no match was found */ |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8855 | MBEDTLS_SSL_PEND_FATAL_ALERT( |
| 8856 | MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL, |
| 8857 | MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL ); |
| 8858 | return( MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL ); |
| 8859 | } |
| 8860 | |
| 8861 | int mbedtls_ssl_write_alpn_ext( mbedtls_ssl_context *ssl, |
| 8862 | unsigned char *buf, |
| 8863 | unsigned char *end, |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 8864 | size_t *out_len ) |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8865 | { |
| 8866 | unsigned char *p = buf; |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 8867 | size_t protocol_name_len; |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 8868 | *out_len = 0; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8869 | |
| 8870 | if( ssl->alpn_chosen == NULL ) |
| 8871 | { |
| 8872 | return( 0 ); |
| 8873 | } |
| 8874 | |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 8875 | protocol_name_len = strlen( ssl->alpn_chosen ); |
| 8876 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 7 + protocol_name_len ); |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8877 | |
| 8878 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "server side, adding alpn extension" ) ); |
| 8879 | /* |
| 8880 | * 0 . 1 ext identifier |
| 8881 | * 2 . 3 ext length |
| 8882 | * 4 . 5 protocol list length |
| 8883 | * 6 . 6 protocol name length |
| 8884 | * 7 . 7+n protocol name |
| 8885 | */ |
| 8886 | MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ALPN, p, 0 ); |
| 8887 | |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 8888 | *out_len = 7 + protocol_name_len; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8889 | |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 8890 | MBEDTLS_PUT_UINT16_BE( protocol_name_len + 3, p, 2 ); |
| 8891 | MBEDTLS_PUT_UINT16_BE( protocol_name_len + 1, p, 4 ); |
XiaokangQian | 0b776e2 | 2022-06-24 09:04:59 +0000 | [diff] [blame] | 8892 | /* Note: the length of the chosen protocol has been checked to be less |
| 8893 | * than 255 bytes in `mbedtls_ssl_conf_alpn_protocols`. |
| 8894 | */ |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 8895 | p[6] = MBEDTLS_BYTE_0( protocol_name_len ); |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8896 | |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 8897 | memcpy( p + 7, ssl->alpn_chosen, protocol_name_len ); |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8898 | return ( 0 ); |
| 8899 | } |
| 8900 | #endif /* MBEDTLS_SSL_ALPN */ |
| 8901 | |
Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 8902 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \ |
Xiaokang Qian | 0340929 | 2022-10-12 02:49:52 +0000 | [diff] [blame] | 8903 | defined(MBEDTLS_SSL_SESSION_TICKETS) && \ |
Xiaokang Qian | ed0620c | 2022-10-12 06:58:13 +0000 | [diff] [blame] | 8904 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \ |
Xiaokang Qian | ed3afcd | 2022-10-12 08:31:11 +0000 | [diff] [blame] | 8905 | defined(MBEDTLS_SSL_CLI_C) |
Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 8906 | int mbedtls_ssl_session_set_hostname( mbedtls_ssl_session *session, |
| 8907 | const char *hostname ) |
| 8908 | { |
| 8909 | /* Initialize to suppress unnecessary compiler warning */ |
| 8910 | size_t hostname_len = 0; |
| 8911 | |
| 8912 | /* Check if new hostname is valid before |
| 8913 | * making any change to current one */ |
| 8914 | if( hostname != NULL ) |
| 8915 | { |
| 8916 | hostname_len = strlen( hostname ); |
| 8917 | |
| 8918 | if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN ) |
| 8919 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8920 | } |
| 8921 | |
| 8922 | /* Now it's clear that we will overwrite the old hostname, |
| 8923 | * so we can free it safely */ |
| 8924 | if( session->hostname != NULL ) |
| 8925 | { |
| 8926 | mbedtls_platform_zeroize( session->hostname, |
| 8927 | strlen( session->hostname ) ); |
| 8928 | mbedtls_free( session->hostname ); |
| 8929 | } |
| 8930 | |
| 8931 | /* Passing NULL as hostname shall clear the old one */ |
| 8932 | if( hostname == NULL ) |
| 8933 | { |
| 8934 | session->hostname = NULL; |
| 8935 | } |
| 8936 | else |
| 8937 | { |
| 8938 | session->hostname = mbedtls_calloc( 1, hostname_len + 1 ); |
| 8939 | if( session->hostname == NULL ) |
| 8940 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 8941 | |
| 8942 | memcpy( session->hostname, hostname, hostname_len ); |
| 8943 | } |
| 8944 | |
| 8945 | return( 0 ); |
| 8946 | } |
Xiaokang Qian | 0340929 | 2022-10-12 02:49:52 +0000 | [diff] [blame] | 8947 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 && |
| 8948 | MBEDTLS_SSL_SESSION_TICKETS && |
Xiaokang Qian | ed0620c | 2022-10-12 06:58:13 +0000 | [diff] [blame] | 8949 | MBEDTLS_SSL_SERVER_NAME_INDICATION && |
Xiaokang Qian | ed3afcd | 2022-10-12 08:31:11 +0000 | [diff] [blame] | 8950 | MBEDTLS_SSL_CLI_C */ |
Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 8951 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8952 | #endif /* MBEDTLS_SSL_TLS_C */ |