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) |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 671 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 672 | handshake->psa_pake_ctx = psa_pake_operation_init(); |
| 673 | handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT; |
| 674 | #else |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 675 | mbedtls_ecjpake_init( &handshake->ecjpake_ctx ); |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 676 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | 77c0646 | 2015-09-17 13:59:49 +0200 | [diff] [blame] | 677 | #if defined(MBEDTLS_SSL_CLI_C) |
| 678 | handshake->ecjpake_cache = NULL; |
| 679 | handshake->ecjpake_cache_len = 0; |
| 680 | #endif |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 681 | #endif |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 682 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 683 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Manuel Pégourié-Gonnard | 6b7301c | 2017-08-15 12:08:45 +0200 | [diff] [blame] | 684 | mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx ); |
Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 685 | #endif |
| 686 | |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 687 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 688 | handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET; |
| 689 | #endif |
Hanno Becker | 7517312 | 2019-02-06 16:18:31 +0000 | [diff] [blame] | 690 | |
| 691 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 692 | !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 693 | mbedtls_pk_init( &handshake->peer_pubkey ); |
| 694 | #endif |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 695 | } |
| 696 | |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 697 | void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform ) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 698 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 699 | memset( transform, 0, sizeof(mbedtls_ssl_transform) ); |
Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 700 | |
Przemyslaw Stekiel | 8f80fb9 | 2022-01-11 08:28:13 +0100 | [diff] [blame] | 701 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 702 | transform->psa_key_enc = MBEDTLS_SVC_KEY_ID_INIT; |
| 703 | transform->psa_key_dec = MBEDTLS_SVC_KEY_ID_INIT; |
Przemyslaw Stekiel | 6be9cf5 | 2022-01-19 16:00:22 +0100 | [diff] [blame] | 704 | #else |
| 705 | mbedtls_cipher_init( &transform->cipher_ctx_enc ); |
| 706 | mbedtls_cipher_init( &transform->cipher_ctx_dec ); |
Przemyslaw Stekiel | 8f80fb9 | 2022-01-11 08:28:13 +0100 | [diff] [blame] | 707 | #endif |
| 708 | |
Hanno Becker | fd86ca8 | 2020-11-30 08:54:23 +0000 | [diff] [blame] | 709 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
Neil Armstrong | 39b8e7d | 2022-02-23 09:24:45 +0100 | [diff] [blame] | 710 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 711 | transform->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT; |
| 712 | transform->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT; |
Neil Armstrong | cf8841a | 2022-02-24 11:17:45 +0100 | [diff] [blame] | 713 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 714 | mbedtls_md_init( &transform->md_ctx_enc ); |
| 715 | mbedtls_md_init( &transform->md_ctx_dec ); |
Hanno Becker | d56ed24 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 716 | #endif |
Neil Armstrong | cf8841a | 2022-02-24 11:17:45 +0100 | [diff] [blame] | 717 | #endif |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 718 | } |
| 719 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 720 | void mbedtls_ssl_session_init( mbedtls_ssl_session *session ) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 721 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 722 | memset( session, 0, sizeof(mbedtls_ssl_session) ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 723 | } |
| 724 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 725 | MBEDTLS_CHECK_RETURN_CRITICAL |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 726 | static int ssl_handshake_init( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 727 | { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 728 | /* Clear old handshake information if present */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 729 | if( ssl->transform_negotiate ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 730 | mbedtls_ssl_transform_free( ssl->transform_negotiate ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 731 | if( ssl->session_negotiate ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 732 | mbedtls_ssl_session_free( ssl->session_negotiate ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 733 | if( ssl->handshake ) |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 734 | mbedtls_ssl_handshake_free( ssl ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 735 | |
| 736 | /* |
| 737 | * Either the pointers are now NULL or cleared properly and can be freed. |
| 738 | * Now allocate missing structures. |
| 739 | */ |
| 740 | if( ssl->transform_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->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) ); |
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 | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 745 | if( ssl->session_negotiate == 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->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 748 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 749 | |
Paul Bakker | 82788fb | 2014-10-20 13:59:19 +0200 | [diff] [blame] | 750 | if( ssl->handshake == NULL ) |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 751 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 752 | ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 753 | } |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 754 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 755 | /* If the buffers are too small - reallocate */ |
Andrzej Kurek | 8ea6872 | 2020-04-03 06:40:47 -0400 | [diff] [blame] | 756 | |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 757 | handle_buffer_resizing( ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN, |
| 758 | MBEDTLS_SSL_OUT_BUFFER_LEN ); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 759 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 760 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 761 | /* All pointers should exist and can be directly freed without issue */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 762 | if( ssl->handshake == NULL || |
| 763 | ssl->transform_negotiate == NULL || |
| 764 | ssl->session_negotiate == NULL ) |
| 765 | { |
Manuel Pégourié-Gonnard | b2a18a2 | 2015-05-27 16:29:56 +0200 | [diff] [blame] | 766 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 767 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 768 | mbedtls_free( ssl->handshake ); |
| 769 | mbedtls_free( ssl->transform_negotiate ); |
| 770 | mbedtls_free( ssl->session_negotiate ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 771 | |
| 772 | ssl->handshake = NULL; |
| 773 | ssl->transform_negotiate = NULL; |
| 774 | ssl->session_negotiate = NULL; |
| 775 | |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 776 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 777 | } |
| 778 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 779 | /* Initialize structures */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 780 | mbedtls_ssl_session_init( ssl->session_negotiate ); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 781 | mbedtls_ssl_transform_init( ssl->transform_negotiate ); |
Paul Bakker | 968afaa | 2014-07-09 11:09:24 +0200 | [diff] [blame] | 782 | ssl_handshake_params_init( ssl->handshake ); |
| 783 | |
Jerry Yu | d0766ec | 2022-09-22 10:46:57 +0800 | [diff] [blame] | 784 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \ |
| 785 | defined(MBEDTLS_SSL_SRV_C) && \ |
| 786 | defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 787 | ssl->handshake->new_session_tickets_count = |
| 788 | ssl->conf->new_session_tickets_count ; |
| 789 | #endif |
| 790 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 791 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 792 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 793 | { |
| 794 | ssl->handshake->alt_transform_out = ssl->transform_out; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 795 | |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 796 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 797 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING; |
| 798 | else |
| 799 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING; |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 800 | |
Hanno Becker | 0f57a65 | 2020-02-05 10:37:26 +0000 | [diff] [blame] | 801 | mbedtls_ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 802 | } |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 803 | #endif |
| 804 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 805 | /* |
| 806 | * curve_list is translated to IANA TLS group identifiers here because |
| 807 | * mbedtls_ssl_conf_curves returns void and so can't return |
| 808 | * any error codes. |
| 809 | */ |
| 810 | #if defined(MBEDTLS_ECP_C) |
| 811 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 812 | /* Heap allocate and translate curve_list from internal to IANA group ids */ |
| 813 | if ( ssl->conf->curve_list != NULL ) |
| 814 | { |
| 815 | size_t length; |
| 816 | const mbedtls_ecp_group_id *curve_list = ssl->conf->curve_list; |
| 817 | |
| 818 | for( length = 0; ( curve_list[length] != MBEDTLS_ECP_DP_NONE ) && |
| 819 | ( length < MBEDTLS_ECP_DP_MAX ); length++ ) {} |
| 820 | |
| 821 | /* Leave room for zero termination */ |
| 822 | uint16_t *group_list = mbedtls_calloc( length + 1, sizeof(uint16_t) ); |
| 823 | if ( group_list == NULL ) |
| 824 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 825 | |
| 826 | for( size_t i = 0; i < length; i++ ) |
| 827 | { |
| 828 | const mbedtls_ecp_curve_info *info = |
| 829 | mbedtls_ecp_curve_info_from_grp_id( curve_list[i] ); |
| 830 | if ( info == NULL ) |
| 831 | { |
| 832 | mbedtls_free( group_list ); |
| 833 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); |
| 834 | } |
| 835 | group_list[i] = info->tls_id; |
| 836 | } |
| 837 | |
| 838 | group_list[length] = 0; |
| 839 | |
| 840 | ssl->handshake->group_list = group_list; |
| 841 | ssl->handshake->group_list_heap_allocated = 1; |
| 842 | } |
| 843 | else |
| 844 | { |
| 845 | ssl->handshake->group_list = ssl->conf->group_list; |
| 846 | ssl->handshake->group_list_heap_allocated = 0; |
| 847 | } |
| 848 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
| 849 | #endif /* MBEDTLS_ECP_C */ |
| 850 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 851 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 852 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Jerry Yu | a69269a | 2022-01-17 21:06:01 +0800 | [diff] [blame] | 853 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Jerry Yu | 713013f | 2022-01-17 18:16:35 +0800 | [diff] [blame] | 854 | /* Heap allocate and translate sig_hashes from internal hash identifiers to |
| 855 | signature algorithms IANA identifiers. */ |
| 856 | if ( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) && |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 857 | ssl->conf->sig_hashes != NULL ) |
| 858 | { |
| 859 | const int *md; |
| 860 | const int *sig_hashes = ssl->conf->sig_hashes; |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 861 | size_t sig_algs_len = 0; |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 862 | uint16_t *p; |
| 863 | |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 864 | #if defined(static_assert) |
| 865 | static_assert( MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN |
| 866 | <= ( SIZE_MAX - ( 2 * sizeof(uint16_t) ) ), |
| 867 | "MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN too big" ); |
Jerry Yu | a68dca2 | 2022-01-20 16:28:27 +0800 | [diff] [blame] | 868 | #endif |
| 869 | |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 870 | for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ ) |
| 871 | { |
| 872 | if( mbedtls_ssl_hash_from_md_alg( *md ) == MBEDTLS_SSL_HASH_NONE ) |
| 873 | continue; |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 874 | #if defined(MBEDTLS_ECDSA_C) |
| 875 | sig_algs_len += sizeof( uint16_t ); |
| 876 | #endif |
Jerry Yu | a68dca2 | 2022-01-20 16:28:27 +0800 | [diff] [blame] | 877 | |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 878 | #if defined(MBEDTLS_RSA_C) |
| 879 | sig_algs_len += sizeof( uint16_t ); |
| 880 | #endif |
| 881 | if( sig_algs_len > MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN ) |
| 882 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 883 | } |
| 884 | |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 885 | if( sig_algs_len < MBEDTLS_SSL_MIN_SIG_ALG_LIST_LEN ) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 886 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); |
| 887 | |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 888 | ssl->handshake->sig_algs = mbedtls_calloc( 1, sig_algs_len + |
| 889 | sizeof( uint16_t )); |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 890 | if( ssl->handshake->sig_algs == NULL ) |
| 891 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 892 | |
| 893 | p = (uint16_t *)ssl->handshake->sig_algs; |
| 894 | for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ ) |
| 895 | { |
| 896 | unsigned char hash = mbedtls_ssl_hash_from_md_alg( *md ); |
| 897 | if( hash == MBEDTLS_SSL_HASH_NONE ) |
| 898 | continue; |
Jerry Yu | 6106fdc | 2022-01-12 16:36:14 +0800 | [diff] [blame] | 899 | #if defined(MBEDTLS_ECDSA_C) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 900 | *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_ECDSA); |
| 901 | p++; |
Jerry Yu | 6106fdc | 2022-01-12 16:36:14 +0800 | [diff] [blame] | 902 | #endif |
| 903 | #if defined(MBEDTLS_RSA_C) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 904 | *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_RSA); |
| 905 | p++; |
Jerry Yu | 6106fdc | 2022-01-12 16:36:14 +0800 | [diff] [blame] | 906 | #endif |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 907 | } |
Gabor Mezei | 15b95a6 | 2022-05-09 16:37:58 +0200 | [diff] [blame] | 908 | *p = MBEDTLS_TLS_SIG_NONE; |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 909 | ssl->handshake->sig_algs_heap_allocated = 1; |
| 910 | } |
| 911 | else |
Jerry Yu | a69269a | 2022-01-17 21:06:01 +0800 | [diff] [blame] | 912 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 913 | { |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 914 | ssl->handshake->sig_algs_heap_allocated = 0; |
| 915 | } |
Jerry Yu | cc53910 | 2022-06-27 16:27:35 +0800 | [diff] [blame] | 916 | #endif /* !MBEDTLS_DEPRECATED_REMOVED */ |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 917 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 918 | return( 0 ); |
| 919 | } |
| 920 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 921 | #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] | 922 | /* Dummy cookie callbacks for defaults */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 923 | MBEDTLS_CHECK_RETURN_CRITICAL |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 924 | static int ssl_cookie_write_dummy( void *ctx, |
| 925 | unsigned char **p, unsigned char *end, |
| 926 | const unsigned char *cli_id, size_t cli_id_len ) |
| 927 | { |
| 928 | ((void) ctx); |
| 929 | ((void) p); |
| 930 | ((void) end); |
| 931 | ((void) cli_id); |
| 932 | ((void) cli_id_len); |
| 933 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 934 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 935 | } |
| 936 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 937 | MBEDTLS_CHECK_RETURN_CRITICAL |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 938 | static int ssl_cookie_check_dummy( void *ctx, |
| 939 | const unsigned char *cookie, size_t cookie_len, |
| 940 | const unsigned char *cli_id, size_t cli_id_len ) |
| 941 | { |
| 942 | ((void) ctx); |
| 943 | ((void) cookie); |
| 944 | ((void) cookie_len); |
| 945 | ((void) cli_id); |
| 946 | ((void) cli_id_len); |
| 947 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 948 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 949 | } |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 950 | #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 951 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 952 | /* |
| 953 | * Initialize an SSL context |
| 954 | */ |
Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 955 | void mbedtls_ssl_init( mbedtls_ssl_context *ssl ) |
| 956 | { |
| 957 | memset( ssl, 0, sizeof( mbedtls_ssl_context ) ); |
| 958 | } |
| 959 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 960 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 961 | static int ssl_conf_version_check( const mbedtls_ssl_context *ssl ) |
| 962 | { |
Ronald Cron | 086ee0b | 2022-03-15 15:18:51 +0100 | [diff] [blame] | 963 | const mbedtls_ssl_config *conf = ssl->conf; |
| 964 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 965 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Ronald Cron | 086ee0b | 2022-03-15 15:18:51 +0100 | [diff] [blame] | 966 | if( mbedtls_ssl_conf_is_tls13_only( conf ) ) |
| 967 | { |
XiaokangQian | ed582dd | 2022-04-13 08:21:05 +0000 | [diff] [blame] | 968 | if( conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 969 | { |
| 970 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS 1.3 is not yet supported." ) ); |
| 971 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 972 | } |
| 973 | |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 974 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls13 only." ) ); |
| 975 | return( 0 ); |
| 976 | } |
| 977 | #endif |
| 978 | |
| 979 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Ronald Cron | 086ee0b | 2022-03-15 15:18:51 +0100 | [diff] [blame] | 980 | if( mbedtls_ssl_conf_is_tls12_only( conf ) ) |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 981 | { |
| 982 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls12 only." ) ); |
| 983 | return( 0 ); |
| 984 | } |
| 985 | #endif |
| 986 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 987 | #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] | 988 | if( mbedtls_ssl_conf_is_hybrid_tls12_tls13( conf ) ) |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 989 | { |
XiaokangQian | ed582dd | 2022-04-13 08:21:05 +0000 | [diff] [blame] | 990 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 991 | { |
| 992 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS not yet supported in Hybrid TLS 1.3 + TLS 1.2" ) ); |
| 993 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 994 | } |
| 995 | |
XiaokangQian | 8f9dfe4 | 2022-04-15 02:52:39 +0000 | [diff] [blame] | 996 | if( conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 997 | { |
| 998 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS 1.3 server is not supported yet." ) ); |
| 999 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 1000 | } |
| 1001 | |
| 1002 | |
Ronald Cron | e1d3f06 | 2022-02-10 14:50:54 +0100 | [diff] [blame] | 1003 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is TLS 1.3 or TLS 1.2." ) ); |
| 1004 | return( 0 ); |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1005 | } |
| 1006 | #endif |
| 1007 | |
| 1008 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "The SSL configuration is invalid." ) ); |
| 1009 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); |
| 1010 | } |
| 1011 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 1012 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1013 | static int ssl_conf_check(const mbedtls_ssl_context *ssl) |
| 1014 | { |
| 1015 | int ret; |
| 1016 | ret = ssl_conf_version_check( ssl ); |
| 1017 | if( ret != 0 ) |
| 1018 | return( ret ); |
| 1019 | |
Jerry Yu | def7ae4 | 2022-10-30 14:13:19 +0800 | [diff] [blame] | 1020 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 1021 | /* RFC 8446 section 4.4.3 |
| 1022 | * |
| 1023 | * If the verification fails, the receiver MUST terminate the handshake with |
| 1024 | * a "decrypt_error" alert. |
| 1025 | * |
| 1026 | * If the client is configured as TLS 1.3 only with optional verify, return |
| 1027 | * bad config. |
| 1028 | * |
| 1029 | */ |
| 1030 | if( mbedtls_ssl_conf_tls13_ephemeral_enabled( |
| 1031 | (mbedtls_ssl_context *)ssl ) && |
| 1032 | ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
| 1033 | ssl->conf->max_tls_version == MBEDTLS_SSL_VERSION_TLS1_3 && |
| 1034 | ssl->conf->min_tls_version == MBEDTLS_SSL_VERSION_TLS1_3 && |
| 1035 | ssl->conf->authmode == MBEDTLS_SSL_VERIFY_OPTIONAL ) |
| 1036 | { |
| 1037 | MBEDTLS_SSL_DEBUG_MSG( |
Dave Rodgman | e8734d8 | 2022-10-31 14:30:24 +0000 | [diff] [blame] | 1038 | 1, ( "Optional verify auth mode " |
Jerry Yu | def7ae4 | 2022-10-30 14:13:19 +0800 | [diff] [blame] | 1039 | "is not available for TLS 1.3 client" ) ); |
| 1040 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); |
| 1041 | } |
| 1042 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 1043 | |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1044 | /* Space for further checks */ |
| 1045 | |
| 1046 | return( 0 ); |
| 1047 | } |
| 1048 | |
Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 1049 | /* |
| 1050 | * Setup an SSL context |
| 1051 | */ |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 1052 | |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 1053 | int mbedtls_ssl_setup( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 1897af9 | 2015-05-10 23:27:38 +0200 | [diff] [blame] | 1054 | const mbedtls_ssl_config *conf ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1055 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1056 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1057 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; |
| 1058 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1059 | |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 1060 | ssl->conf = conf; |
Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 1061 | |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1062 | if( ( ret = ssl_conf_check( ssl ) ) != 0 ) |
| 1063 | return( ret ); |
| 1064 | |
Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 1065 | /* |
Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 1066 | * Prepare base structures |
Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 1067 | */ |
k-stachowiak | c9a5f02 | 2018-07-24 13:53:31 +0200 | [diff] [blame] | 1068 | |
| 1069 | /* Set to NULL in case of an error condition */ |
| 1070 | ssl->out_buf = NULL; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1071 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1072 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 1073 | ssl->in_buf_len = in_buf_len; |
| 1074 | #endif |
| 1075 | ssl->in_buf = mbedtls_calloc( 1, in_buf_len ); |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 1076 | if( ssl->in_buf == NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1077 | { |
Paul Elliott | d48d5c6 | 2021-01-07 14:47:05 +0000 | [diff] [blame] | 1078 | 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] | 1079 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1080 | goto error; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 1081 | } |
| 1082 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1083 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 1084 | ssl->out_buf_len = out_buf_len; |
| 1085 | #endif |
| 1086 | ssl->out_buf = mbedtls_calloc( 1, out_buf_len ); |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 1087 | if( ssl->out_buf == NULL ) |
| 1088 | { |
Paul Elliott | d48d5c6 | 2021-01-07 14:47:05 +0000 | [diff] [blame] | 1089 | 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] | 1090 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1091 | goto error; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1092 | } |
| 1093 | |
Hanno Becker | 3e6f8ab | 2020-02-05 10:40:57 +0000 | [diff] [blame] | 1094 | mbedtls_ssl_reset_in_out_pointers( ssl ); |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 1095 | |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 1096 | #if defined(MBEDTLS_SSL_DTLS_SRTP) |
Ron Eldor | 3adb992 | 2017-12-21 10:15:08 +0200 | [diff] [blame] | 1097 | memset( &ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info) ); |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 1098 | #endif |
| 1099 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1100 | if( ( ret = ssl_handshake_init( ssl ) ) != 0 ) |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1101 | goto error; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1102 | |
| 1103 | return( 0 ); |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1104 | |
| 1105 | error: |
| 1106 | mbedtls_free( ssl->in_buf ); |
| 1107 | mbedtls_free( ssl->out_buf ); |
| 1108 | |
| 1109 | ssl->conf = NULL; |
| 1110 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1111 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 1112 | ssl->in_buf_len = 0; |
| 1113 | ssl->out_buf_len = 0; |
| 1114 | #endif |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1115 | ssl->in_buf = NULL; |
| 1116 | ssl->out_buf = NULL; |
| 1117 | |
| 1118 | ssl->in_hdr = NULL; |
| 1119 | ssl->in_ctr = NULL; |
| 1120 | ssl->in_len = NULL; |
| 1121 | ssl->in_iv = NULL; |
| 1122 | ssl->in_msg = NULL; |
| 1123 | |
| 1124 | ssl->out_hdr = NULL; |
| 1125 | ssl->out_ctr = NULL; |
| 1126 | ssl->out_len = NULL; |
| 1127 | ssl->out_iv = NULL; |
| 1128 | ssl->out_msg = NULL; |
| 1129 | |
k-stachowiak | 9f7798e | 2018-07-31 16:52:32 +0200 | [diff] [blame] | 1130 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1131 | } |
| 1132 | |
| 1133 | /* |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 1134 | * Reset an initialized and used SSL context for re-use while retaining |
| 1135 | * all application-set variables, function pointers and data. |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 1136 | * |
| 1137 | * If partial is non-zero, keep data in the input buffer and client ID. |
| 1138 | * (Use when a DTLS client reconnects from the same port.) |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 1139 | */ |
XiaokangQian | 78b1fa7 | 2022-01-19 06:56:30 +0000 | [diff] [blame] | 1140 | void mbedtls_ssl_session_reset_msg_layer( mbedtls_ssl_context *ssl, |
| 1141 | int partial ) |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 1142 | { |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1143 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 1144 | size_t in_buf_len = ssl->in_buf_len; |
| 1145 | size_t out_buf_len = ssl->out_buf_len; |
| 1146 | #else |
| 1147 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; |
| 1148 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; |
| 1149 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1150 | |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1151 | #if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || !defined(MBEDTLS_SSL_SRV_C) |
| 1152 | partial = 0; |
Hanno Becker | 7e77213 | 2018-08-10 12:38:21 +0100 | [diff] [blame] | 1153 | #endif |
| 1154 | |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 1155 | /* Cancel any possibly running timer */ |
Hanno Becker | 0f57a65 | 2020-02-05 10:37:26 +0000 | [diff] [blame] | 1156 | mbedtls_ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 1157 | |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1158 | mbedtls_ssl_reset_in_out_pointers( ssl ); |
| 1159 | |
| 1160 | /* Reset incoming message parsing */ |
| 1161 | ssl->in_offt = NULL; |
| 1162 | ssl->nb_zero = 0; |
| 1163 | ssl->in_msgtype = 0; |
| 1164 | ssl->in_msglen = 0; |
| 1165 | ssl->in_hslen = 0; |
| 1166 | ssl->keep_current_message = 0; |
| 1167 | ssl->transform_in = NULL; |
| 1168 | |
| 1169 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 1170 | ssl->next_record_offset = 0; |
| 1171 | ssl->in_epoch = 0; |
| 1172 | #endif |
| 1173 | |
| 1174 | /* Keep current datagram if partial == 1 */ |
| 1175 | if( partial == 0 ) |
| 1176 | { |
| 1177 | ssl->in_left = 0; |
| 1178 | memset( ssl->in_buf, 0, in_buf_len ); |
| 1179 | } |
| 1180 | |
Ronald Cron | ad8c17b | 2022-06-10 17:18:09 +0200 | [diff] [blame] | 1181 | ssl->send_alert = 0; |
| 1182 | |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1183 | /* Reset outgoing message writing */ |
| 1184 | ssl->out_msgtype = 0; |
| 1185 | ssl->out_msglen = 0; |
| 1186 | ssl->out_left = 0; |
| 1187 | memset( ssl->out_buf, 0, out_buf_len ); |
| 1188 | memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) ); |
| 1189 | ssl->transform_out = NULL; |
| 1190 | |
| 1191 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 1192 | mbedtls_ssl_dtls_replay_reset( ssl ); |
| 1193 | #endif |
| 1194 | |
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1195 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1196 | if( ssl->transform ) |
| 1197 | { |
| 1198 | mbedtls_ssl_transform_free( ssl->transform ); |
| 1199 | mbedtls_free( ssl->transform ); |
| 1200 | ssl->transform = NULL; |
| 1201 | } |
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1202 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 1203 | |
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1204 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 1205 | mbedtls_ssl_transform_free( ssl->transform_application ); |
| 1206 | mbedtls_free( ssl->transform_application ); |
| 1207 | ssl->transform_application = NULL; |
| 1208 | |
| 1209 | if( ssl->handshake != NULL ) |
| 1210 | { |
| 1211 | mbedtls_ssl_transform_free( ssl->handshake->transform_earlydata ); |
| 1212 | mbedtls_free( ssl->handshake->transform_earlydata ); |
| 1213 | ssl->handshake->transform_earlydata = NULL; |
| 1214 | |
| 1215 | mbedtls_ssl_transform_free( ssl->handshake->transform_handshake ); |
| 1216 | mbedtls_free( ssl->handshake->transform_handshake ); |
| 1217 | ssl->handshake->transform_handshake = NULL; |
| 1218 | } |
| 1219 | |
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1220 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1221 | } |
| 1222 | |
| 1223 | int mbedtls_ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial ) |
| 1224 | { |
| 1225 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 1226 | |
| 1227 | ssl->state = MBEDTLS_SSL_HELLO_REQUEST; |
| 1228 | |
XiaokangQian | 78b1fa7 | 2022-01-19 06:56:30 +0000 | [diff] [blame] | 1229 | mbedtls_ssl_session_reset_msg_layer( ssl, partial ); |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1230 | |
| 1231 | /* Reset renegotiation state */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1232 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 1233 | ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE; |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 1234 | ssl->renego_records_seen = 0; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1235 | |
| 1236 | ssl->verify_data_len = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1237 | memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN ); |
| 1238 | 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] | 1239 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1240 | ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1241 | |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1242 | ssl->session_in = NULL; |
Hanno Becker | 7864090 | 2018-08-13 16:35:15 +0100 | [diff] [blame] | 1243 | ssl->session_out = NULL; |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 1244 | if( ssl->session ) |
| 1245 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1246 | mbedtls_ssl_session_free( ssl->session ); |
| 1247 | mbedtls_free( ssl->session ); |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 1248 | ssl->session = NULL; |
| 1249 | } |
| 1250 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1251 | #if defined(MBEDTLS_SSL_ALPN) |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 1252 | ssl->alpn_chosen = NULL; |
| 1253 | #endif |
| 1254 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 1255 | #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C) |
David Horstmann | 3b2276a | 2022-10-06 14:49:08 +0100 | [diff] [blame] | 1256 | int free_cli_id = 1; |
Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 1257 | #if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) |
David Horstmann | 3b2276a | 2022-10-06 14:49:08 +0100 | [diff] [blame] | 1258 | free_cli_id = ( partial == 0 ); |
Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 1259 | #endif |
David Horstmann | 3b2276a | 2022-10-06 14:49:08 +0100 | [diff] [blame] | 1260 | if( free_cli_id ) |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 1261 | { |
| 1262 | mbedtls_free( ssl->cli_id ); |
| 1263 | ssl->cli_id = NULL; |
| 1264 | ssl->cli_id_len = 0; |
| 1265 | } |
Manuel Pégourié-Gonnard | 43c0218 | 2014-07-22 17:32:01 +0200 | [diff] [blame] | 1266 | #endif |
| 1267 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1268 | if( ( ret = ssl_handshake_init( ssl ) ) != 0 ) |
| 1269 | return( ret ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1270 | |
| 1271 | return( 0 ); |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 1272 | } |
| 1273 | |
Manuel Pégourié-Gonnard | 779e429 | 2013-08-03 13:50:48 +0200 | [diff] [blame] | 1274 | /* |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 1275 | * Reset an initialized and used SSL context for re-use while retaining |
| 1276 | * all application-set variables, function pointers and data. |
| 1277 | */ |
| 1278 | int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl ) |
| 1279 | { |
Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 1280 | return( mbedtls_ssl_session_reset_int( ssl, 0 ) ); |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 1281 | } |
| 1282 | |
| 1283 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1284 | * SSL set accessors |
| 1285 | */ |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1286 | void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1287 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1288 | conf->endpoint = endpoint; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1289 | } |
| 1290 | |
Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 1291 | 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] | 1292 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1293 | conf->transport = transport; |
Manuel Pégourié-Gonnard | 0b1ff29 | 2014-02-06 13:04:16 +0100 | [diff] [blame] | 1294 | } |
| 1295 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1296 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1297 | 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] | 1298 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1299 | conf->anti_replay = mode; |
Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 1300 | } |
| 1301 | #endif |
| 1302 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1303 | 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] | 1304 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1305 | conf->badmac_limit = limit; |
Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 1306 | } |
Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 1307 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1308 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 1309 | |
Hanno Becker | 1841b0a | 2018-08-24 11:13:57 +0100 | [diff] [blame] | 1310 | void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl, |
| 1311 | unsigned allow_packing ) |
Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 1312 | { |
| 1313 | ssl->disable_datagram_packing = !allow_packing; |
| 1314 | } |
| 1315 | |
| 1316 | void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf, |
| 1317 | uint32_t min, uint32_t max ) |
Manuel Pégourié-Gonnard | 905dd24 | 2014-10-01 12:03:55 +0200 | [diff] [blame] | 1318 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1319 | conf->hs_timeout_min = min; |
| 1320 | conf->hs_timeout_max = max; |
Manuel Pégourié-Gonnard | 905dd24 | 2014-10-01 12:03:55 +0200 | [diff] [blame] | 1321 | } |
| 1322 | #endif |
| 1323 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1324 | void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1325 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1326 | conf->authmode = authmode; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1327 | } |
| 1328 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1329 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1330 | void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 1331 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 1332 | void *p_vrfy ) |
| 1333 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1334 | conf->f_vrfy = f_vrfy; |
| 1335 | conf->p_vrfy = p_vrfy; |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 1336 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1337 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 1338 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1339 | void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf, |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 1340 | int (*f_rng)(void *, unsigned char *, size_t), |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1341 | void *p_rng ) |
| 1342 | { |
Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 1343 | conf->f_rng = f_rng; |
| 1344 | conf->p_rng = p_rng; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1345 | } |
| 1346 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1347 | void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | fd47423 | 2015-06-23 16:34:24 +0200 | [diff] [blame] | 1348 | void (*f_dbg)(void *, int, const char *, int, const char *), |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1349 | void *p_dbg ) |
| 1350 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1351 | conf->f_dbg = f_dbg; |
| 1352 | conf->p_dbg = p_dbg; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1353 | } |
| 1354 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1355 | void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 1356 | void *p_bio, |
Simon Butcher | e846b51 | 2016-03-01 17:31:49 +0000 | [diff] [blame] | 1357 | mbedtls_ssl_send_t *f_send, |
| 1358 | mbedtls_ssl_recv_t *f_recv, |
| 1359 | mbedtls_ssl_recv_timeout_t *f_recv_timeout ) |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 1360 | { |
| 1361 | ssl->p_bio = p_bio; |
| 1362 | ssl->f_send = f_send; |
| 1363 | ssl->f_recv = f_recv; |
| 1364 | ssl->f_recv_timeout = f_recv_timeout; |
Manuel Pégourié-Gonnard | 97fd52c | 2015-05-06 15:38:52 +0100 | [diff] [blame] | 1365 | } |
| 1366 | |
Manuel Pégourié-Gonnard | 6e7aaca | 2018-08-20 10:37:23 +0200 | [diff] [blame] | 1367 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 1368 | void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu ) |
| 1369 | { |
| 1370 | ssl->mtu = mtu; |
| 1371 | } |
| 1372 | #endif |
| 1373 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1374 | 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] | 1375 | { |
| 1376 | conf->read_timeout = timeout; |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 1377 | } |
| 1378 | |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 1379 | void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl, |
| 1380 | void *p_timer, |
Simon Butcher | e846b51 | 2016-03-01 17:31:49 +0000 | [diff] [blame] | 1381 | mbedtls_ssl_set_timer_t *f_set_timer, |
| 1382 | mbedtls_ssl_get_timer_t *f_get_timer ) |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 1383 | { |
| 1384 | ssl->p_timer = p_timer; |
| 1385 | ssl->f_set_timer = f_set_timer; |
| 1386 | ssl->f_get_timer = f_get_timer; |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 1387 | |
| 1388 | /* Make sure we start with no timer running */ |
Hanno Becker | 0f57a65 | 2020-02-05 10:37:26 +0000 | [diff] [blame] | 1389 | mbedtls_ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 1390 | } |
| 1391 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1392 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1393 | void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf, |
Hanno Becker | a637ff6 | 2021-04-15 08:42:48 +0100 | [diff] [blame] | 1394 | void *p_cache, |
| 1395 | mbedtls_ssl_cache_get_t *f_get_cache, |
| 1396 | mbedtls_ssl_cache_set_t *f_set_cache ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1397 | { |
Manuel Pégourié-Gonnard | 5cb3308 | 2015-05-06 18:06:26 +0100 | [diff] [blame] | 1398 | conf->p_cache = p_cache; |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1399 | conf->f_get_cache = f_get_cache; |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1400 | conf->f_set_cache = f_set_cache; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1401 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1402 | #endif /* MBEDTLS_SSL_SRV_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1403 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1404 | #if defined(MBEDTLS_SSL_CLI_C) |
| 1405 | 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] | 1406 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1407 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1408 | |
| 1409 | if( ssl == NULL || |
| 1410 | session == NULL || |
| 1411 | ssl->session_negotiate == NULL || |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1412 | ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT ) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1413 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1414 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1415 | } |
| 1416 | |
Hanno Becker | e810bbc | 2021-05-14 16:01:05 +0100 | [diff] [blame] | 1417 | if( ssl->handshake->resume == 1 ) |
| 1418 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 1419 | |
Jerry Yu | 2109206 | 2022-10-10 21:21:31 +0800 | [diff] [blame] | 1420 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 1421 | if( session->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 ) |
Jerry Yu | 40afab6 | 2022-10-08 10:42:13 +0800 | [diff] [blame] | 1422 | { |
Jerry Yu | 2109206 | 2022-10-10 21:21:31 +0800 | [diff] [blame] | 1423 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 1424 | mbedtls_ssl_ciphersuite_from_id( session->ciphersuite ); |
| 1425 | |
| 1426 | if( mbedtls_ssl_validate_ciphersuite( |
| 1427 | ssl, ciphersuite_info, MBEDTLS_SSL_VERSION_TLS1_3, |
| 1428 | MBEDTLS_SSL_VERSION_TLS1_3 ) != 0 ) |
| 1429 | { |
| 1430 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "%d is not a valid TLS 1.3 ciphersuite.", |
| 1431 | session->ciphersuite ) ); |
| 1432 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1433 | } |
Jerry Yu | 40afab6 | 2022-10-08 10:42:13 +0800 | [diff] [blame] | 1434 | } |
Jerry Yu | 2109206 | 2022-10-10 21:21:31 +0800 | [diff] [blame] | 1435 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Jerry Yu | 40afab6 | 2022-10-08 10:42:13 +0800 | [diff] [blame] | 1436 | |
Hanno Becker | 52055ae | 2019-02-06 14:30:46 +0000 | [diff] [blame] | 1437 | if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate, |
| 1438 | session ) ) != 0 ) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1439 | return( ret ); |
| 1440 | |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 1441 | ssl->handshake->resume = 1; |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1442 | |
| 1443 | return( 0 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1444 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1445 | #endif /* MBEDTLS_SSL_CLI_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1446 | |
Mateusz Starzyk | 06b07fb | 2021-02-18 13:55:21 +0100 | [diff] [blame] | 1447 | void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf, |
| 1448 | const int *ciphersuites ) |
| 1449 | { |
Hanno Becker | d60b6c6 | 2021-04-29 12:04:11 +0100 | [diff] [blame] | 1450 | conf->ciphersuite_list = ciphersuites; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1451 | } |
| 1452 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 1453 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | cadebe5 | 2021-08-24 10:36:45 +0800 | [diff] [blame] | 1454 | void mbedtls_ssl_conf_tls13_key_exchange_modes( mbedtls_ssl_config *conf, |
Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 1455 | const int kex_modes ) |
| 1456 | { |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 1457 | 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] | 1458 | } |
Xiaokang Qian | 72de95d | 2022-10-25 02:54:33 +0000 | [diff] [blame] | 1459 | |
| 1460 | #if defined(MBEDTLS_SSL_EARLY_DATA) |
| 1461 | void mbedtls_ssl_tls13_conf_early_data( mbedtls_ssl_config *conf, |
Xiaokang Qian | 72dbfef | 2022-10-26 06:33:57 +0000 | [diff] [blame] | 1462 | int early_data_enabled ) |
Xiaokang Qian | 72de95d | 2022-10-25 02:54:33 +0000 | [diff] [blame] | 1463 | { |
| 1464 | conf->early_data_enabled = early_data_enabled; |
| 1465 | } |
| 1466 | #endif /* MBEDTLS_SSL_EARLY_DATA */ |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 1467 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 1468 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1469 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 6e3ee3a | 2015-06-17 10:58:20 +0200 | [diff] [blame] | 1470 | void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf, |
Nicholas Wilson | 2088e2e | 2015-09-08 16:53:18 +0100 | [diff] [blame] | 1471 | const mbedtls_x509_crt_profile *profile ) |
Manuel Pégourié-Gonnard | 6e3ee3a | 2015-06-17 10:58:20 +0200 | [diff] [blame] | 1472 | { |
| 1473 | conf->cert_profile = profile; |
| 1474 | } |
| 1475 | |
Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 1476 | static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert ) |
| 1477 | { |
| 1478 | mbedtls_ssl_key_cert *cur = key_cert, *next; |
| 1479 | |
| 1480 | while( cur != NULL ) |
| 1481 | { |
| 1482 | next = cur->next; |
| 1483 | mbedtls_free( cur ); |
| 1484 | cur = next; |
| 1485 | } |
| 1486 | } |
| 1487 | |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1488 | /* Append a new keycert entry to a (possibly empty) list */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 1489 | MBEDTLS_CHECK_RETURN_CRITICAL |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1490 | static int ssl_append_key_cert( mbedtls_ssl_key_cert **head, |
| 1491 | mbedtls_x509_crt *cert, |
| 1492 | mbedtls_pk_context *key ) |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1493 | { |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1494 | mbedtls_ssl_key_cert *new_cert; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1495 | |
Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 1496 | if( cert == NULL ) |
| 1497 | { |
| 1498 | /* Free list if cert is null */ |
| 1499 | ssl_key_cert_free( *head ); |
| 1500 | *head = NULL; |
| 1501 | return( 0 ); |
| 1502 | } |
| 1503 | |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1504 | new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) ); |
| 1505 | if( new_cert == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 1506 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1507 | |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1508 | new_cert->cert = cert; |
| 1509 | new_cert->key = key; |
| 1510 | new_cert->next = NULL; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1511 | |
Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 1512 | /* 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] | 1513 | if( *head == NULL ) |
Paul Bakker | 0333b97 | 2013-11-04 17:08:28 +0100 | [diff] [blame] | 1514 | { |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1515 | *head = new_cert; |
Paul Bakker | 0333b97 | 2013-11-04 17:08:28 +0100 | [diff] [blame] | 1516 | } |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1517 | else |
| 1518 | { |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1519 | mbedtls_ssl_key_cert *cur = *head; |
| 1520 | while( cur->next != NULL ) |
| 1521 | cur = cur->next; |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1522 | cur->next = new_cert; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1523 | } |
| 1524 | |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1525 | return( 0 ); |
| 1526 | } |
| 1527 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1528 | int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1529 | mbedtls_x509_crt *own_cert, |
| 1530 | mbedtls_pk_context *pk_key ) |
| 1531 | { |
Manuel Pégourié-Gonnard | 17a40cd | 2015-05-10 23:17:17 +0200 | [diff] [blame] | 1532 | 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] | 1533 | } |
| 1534 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1535 | void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 1536 | mbedtls_x509_crt *ca_chain, |
| 1537 | mbedtls_x509_crl *ca_crl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1538 | { |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 1539 | conf->ca_chain = ca_chain; |
| 1540 | conf->ca_crl = ca_crl; |
Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 1541 | |
| 1542 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 1543 | /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb() |
| 1544 | * cannot be used together. */ |
| 1545 | conf->f_ca_cb = NULL; |
| 1546 | conf->p_ca_cb = NULL; |
| 1547 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1548 | } |
Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 1549 | |
| 1550 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 1551 | void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf, |
Hanno Becker | afd0b0a | 2019-03-27 16:55:01 +0000 | [diff] [blame] | 1552 | mbedtls_x509_crt_ca_cb_t f_ca_cb, |
Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 1553 | void *p_ca_cb ) |
| 1554 | { |
| 1555 | conf->f_ca_cb = f_ca_cb; |
| 1556 | conf->p_ca_cb = p_ca_cb; |
| 1557 | |
| 1558 | /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb() |
| 1559 | * cannot be used together. */ |
| 1560 | conf->ca_chain = NULL; |
| 1561 | conf->ca_crl = NULL; |
| 1562 | } |
| 1563 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1564 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | eb2c658 | 2012-09-27 19:15:01 +0000 | [diff] [blame] | 1565 | |
Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 1566 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Glenn Strauss | 6989407 | 2022-01-24 12:58:00 -0500 | [diff] [blame] | 1567 | const unsigned char *mbedtls_ssl_get_hs_sni( mbedtls_ssl_context *ssl, |
| 1568 | size_t *name_len ) |
| 1569 | { |
| 1570 | *name_len = ssl->handshake->sni_name_len; |
| 1571 | return( ssl->handshake->sni_name ); |
| 1572 | } |
| 1573 | |
Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 1574 | int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl, |
| 1575 | mbedtls_x509_crt *own_cert, |
| 1576 | mbedtls_pk_context *pk_key ) |
| 1577 | { |
| 1578 | return( ssl_append_key_cert( &ssl->handshake->sni_key_cert, |
| 1579 | own_cert, pk_key ) ); |
| 1580 | } |
Manuel Pégourié-Gonnard | 22bfa4b | 2015-05-11 08:46:37 +0200 | [diff] [blame] | 1581 | |
| 1582 | void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl, |
| 1583 | mbedtls_x509_crt *ca_chain, |
| 1584 | mbedtls_x509_crl *ca_crl ) |
| 1585 | { |
| 1586 | ssl->handshake->sni_ca_chain = ca_chain; |
| 1587 | ssl->handshake->sni_ca_crl = ca_crl; |
| 1588 | } |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 1589 | |
Glenn Strauss | 999ef70 | 2022-03-11 01:37:23 -0500 | [diff] [blame] | 1590 | #if defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED) |
| 1591 | void mbedtls_ssl_set_hs_dn_hints( mbedtls_ssl_context *ssl, |
| 1592 | const mbedtls_x509_crt *crt) |
| 1593 | { |
| 1594 | ssl->handshake->dn_hints = crt; |
| 1595 | } |
| 1596 | #endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */ |
| 1597 | |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 1598 | void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl, |
| 1599 | int authmode ) |
| 1600 | { |
| 1601 | ssl->handshake->sni_authmode = authmode; |
| 1602 | } |
Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 1603 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
| 1604 | |
Hanno Becker | 8927c83 | 2019-04-03 12:52:50 +0100 | [diff] [blame] | 1605 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 1606 | void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl, |
| 1607 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), |
| 1608 | void *p_vrfy ) |
| 1609 | { |
| 1610 | ssl->f_vrfy = f_vrfy; |
| 1611 | ssl->p_vrfy = p_vrfy; |
| 1612 | } |
| 1613 | #endif |
| 1614 | |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 1615 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 1616 | /* |
| 1617 | * Set EC J-PAKE password for current handshake |
| 1618 | */ |
Valerio Setti | 02c25b5 | 2022-11-15 14:08:42 +0100 | [diff] [blame] | 1619 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 1620 | int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl, |
| 1621 | const unsigned char *pw, |
| 1622 | size_t pw_len ) |
| 1623 | { |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 1624 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 1625 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1626 | psa_pake_role_t psa_role; |
| 1627 | psa_status_t status; |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 1628 | |
Janos Follath | 8eb6413 | 2016-06-03 15:40:57 +0100 | [diff] [blame] | 1629 | if( ssl->handshake == NULL || ssl->conf == NULL ) |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 1630 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1631 | |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 1632 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 1633 | psa_role = PSA_PAKE_ROLE_SERVER; |
| 1634 | else |
| 1635 | psa_role = PSA_PAKE_ROLE_CLIENT; |
| 1636 | |
| 1637 | |
| 1638 | if( pw_len > 0 ) |
| 1639 | { |
| 1640 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 1641 | psa_set_key_algorithm( &attributes, PSA_ALG_JPAKE ); |
| 1642 | psa_set_key_type( &attributes, PSA_KEY_TYPE_PASSWORD ); |
| 1643 | |
| 1644 | status = psa_import_key( &attributes, pw, pw_len, |
| 1645 | &ssl->handshake->psa_pake_password ); |
| 1646 | if( status != PSA_SUCCESS ) |
| 1647 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 1648 | } |
| 1649 | |
| 1650 | psa_pake_cs_set_algorithm( &cipher_suite, PSA_ALG_JPAKE ); |
| 1651 | psa_pake_cs_set_primitive( &cipher_suite, |
| 1652 | PSA_PAKE_PRIMITIVE( PSA_PAKE_PRIMITIVE_TYPE_ECC, |
| 1653 | PSA_ECC_FAMILY_SECP_R1, |
| 1654 | 256) ); |
| 1655 | psa_pake_cs_set_hash( &cipher_suite, PSA_ALG_SHA_256 ); |
| 1656 | |
| 1657 | status = psa_pake_setup( &ssl->handshake->psa_pake_ctx, &cipher_suite ); |
| 1658 | if( status != PSA_SUCCESS ) |
| 1659 | { |
| 1660 | psa_destroy_key( ssl->handshake->psa_pake_password ); |
| 1661 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 1662 | } |
| 1663 | |
| 1664 | status = psa_pake_set_role( &ssl->handshake->psa_pake_ctx, psa_role ); |
| 1665 | if( status != PSA_SUCCESS ) |
| 1666 | { |
| 1667 | psa_destroy_key( ssl->handshake->psa_pake_password ); |
| 1668 | psa_pake_abort( &ssl->handshake->psa_pake_ctx ); |
| 1669 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 1670 | } |
| 1671 | |
| 1672 | if( pw_len > 0 ) |
| 1673 | { |
| 1674 | psa_pake_set_password_key( &ssl->handshake->psa_pake_ctx, |
| 1675 | ssl->handshake->psa_pake_password ); |
| 1676 | if( status != PSA_SUCCESS ) |
| 1677 | { |
| 1678 | psa_destroy_key( ssl->handshake->psa_pake_password ); |
| 1679 | psa_pake_abort( &ssl->handshake->psa_pake_ctx ); |
| 1680 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 1681 | } |
| 1682 | } |
| 1683 | |
| 1684 | ssl->handshake->psa_pake_ctx_is_ok = 1; |
| 1685 | |
| 1686 | return( 0 ); |
Valerio Setti | 02c25b5 | 2022-11-15 14:08:42 +0100 | [diff] [blame] | 1687 | } |
| 1688 | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
| 1689 | int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl, |
| 1690 | const unsigned char *pw, |
| 1691 | size_t pw_len ) |
| 1692 | { |
| 1693 | mbedtls_ecjpake_role role; |
| 1694 | |
| 1695 | if( ssl->handshake == NULL || ssl->conf == NULL ) |
| 1696 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1697 | |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 1698 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 1699 | role = MBEDTLS_ECJPAKE_SERVER; |
| 1700 | else |
| 1701 | role = MBEDTLS_ECJPAKE_CLIENT; |
| 1702 | |
| 1703 | return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx, |
| 1704 | role, |
| 1705 | MBEDTLS_MD_SHA256, |
| 1706 | MBEDTLS_ECP_DP_SECP256R1, |
| 1707 | pw, pw_len ) ); |
| 1708 | } |
Valerio Setti | 02c25b5 | 2022-11-15 14:08:42 +0100 | [diff] [blame] | 1709 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 1710 | #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 1711 | |
Ronald Cron | 73fe8df | 2022-10-05 14:31:43 +0200 | [diff] [blame] | 1712 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED) |
Ronald Cron | d29e13e | 2022-10-19 10:33:48 +0200 | [diff] [blame] | 1713 | int mbedtls_ssl_conf_has_static_psk( mbedtls_ssl_config const *conf ) |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 1714 | { |
Ronald Cron | d29e13e | 2022-10-19 10:33:48 +0200 | [diff] [blame] | 1715 | if( conf->psk_identity == NULL || |
| 1716 | conf->psk_identity_len == 0 ) |
| 1717 | { |
| 1718 | return( 0 ); |
| 1719 | } |
| 1720 | |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 1721 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Ronald Cron | d29e13e | 2022-10-19 10:33:48 +0200 | [diff] [blame] | 1722 | if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) ) |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 1723 | return( 1 ); |
| 1724 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Ronald Cron | d29e13e | 2022-10-19 10:33:48 +0200 | [diff] [blame] | 1725 | |
| 1726 | if( conf->psk != NULL && conf->psk_len != 0 ) |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 1727 | return( 1 ); |
| 1728 | |
| 1729 | return( 0 ); |
| 1730 | } |
| 1731 | |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1732 | static void ssl_conf_remove_psk( mbedtls_ssl_config *conf ) |
| 1733 | { |
| 1734 | /* Remove reference to existing PSK, if any. */ |
| 1735 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1736 | if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) ) |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1737 | { |
| 1738 | /* The maintenance of the PSK key slot is the |
| 1739 | * user's responsibility. */ |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1740 | conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT; |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1741 | } |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1742 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 1743 | if( conf->psk != NULL ) |
| 1744 | { |
| 1745 | mbedtls_platform_zeroize( conf->psk, conf->psk_len ); |
| 1746 | |
| 1747 | mbedtls_free( conf->psk ); |
| 1748 | conf->psk = NULL; |
| 1749 | conf->psk_len = 0; |
| 1750 | } |
| 1751 | |
| 1752 | /* Remove reference to PSK identity, if any. */ |
| 1753 | if( conf->psk_identity != NULL ) |
| 1754 | { |
| 1755 | mbedtls_free( conf->psk_identity ); |
| 1756 | conf->psk_identity = NULL; |
| 1757 | conf->psk_identity_len = 0; |
| 1758 | } |
| 1759 | } |
| 1760 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1761 | /* This function assumes that PSK identity in the SSL config is unset. |
| 1762 | * It checks that the provided identity is well-formed and attempts |
| 1763 | * to make a copy of it in the SSL config. |
| 1764 | * On failure, the PSK identity in the config remains unset. */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 1765 | MBEDTLS_CHECK_RETURN_CRITICAL |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1766 | static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf, |
| 1767 | unsigned char const *psk_identity, |
| 1768 | size_t psk_identity_len ) |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 1769 | { |
Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 1770 | /* Identity len will be encoded on two bytes */ |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1771 | if( psk_identity == NULL || |
Ronald Cron | 2a87e9b | 2022-10-19 10:55:26 +0200 | [diff] [blame] | 1772 | psk_identity_len == 0 || |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1773 | ( psk_identity_len >> 16 ) != 0 || |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 1774 | psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN ) |
Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 1775 | { |
| 1776 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1777 | } |
| 1778 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1779 | conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ); |
| 1780 | if( conf->psk_identity == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 1781 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 1782 | |
Manuel Pégourié-Gonnard | 120fdbd | 2015-05-07 17:07:50 +0100 | [diff] [blame] | 1783 | conf->psk_identity_len = psk_identity_len; |
Manuel Pégourié-Gonnard | 120fdbd | 2015-05-07 17:07:50 +0100 | [diff] [blame] | 1784 | memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len ); |
Paul Bakker | 5ad403f | 2013-09-18 21:21:30 +0200 | [diff] [blame] | 1785 | |
| 1786 | return( 0 ); |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 1787 | } |
| 1788 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1789 | int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf, |
| 1790 | const unsigned char *psk, size_t psk_len, |
| 1791 | const unsigned char *psk_identity, size_t psk_identity_len ) |
| 1792 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1793 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 1794 | |
| 1795 | /* We currently only support one PSK, raw or opaque. */ |
Ronald Cron | d29e13e | 2022-10-19 10:33:48 +0200 | [diff] [blame] | 1796 | if( mbedtls_ssl_conf_has_static_psk( conf ) ) |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 1797 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1798 | |
| 1799 | /* Check and set raw PSK */ |
Piotr Nowicki | 9926eaf | 2019-11-20 14:54:36 +0100 | [diff] [blame] | 1800 | if( psk == NULL ) |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1801 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Piotr Nowicki | 9926eaf | 2019-11-20 14:54:36 +0100 | [diff] [blame] | 1802 | if( psk_len == 0 ) |
| 1803 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1804 | if( psk_len > MBEDTLS_PSK_MAX_LEN ) |
| 1805 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1806 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1807 | if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ) |
| 1808 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 1809 | conf->psk_len = psk_len; |
| 1810 | memcpy( conf->psk, psk, conf->psk_len ); |
| 1811 | |
| 1812 | /* Check and set PSK Identity */ |
| 1813 | ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len ); |
| 1814 | if( ret != 0 ) |
| 1815 | ssl_conf_remove_psk( conf ); |
| 1816 | |
| 1817 | return( ret ); |
| 1818 | } |
| 1819 | |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1820 | static void ssl_remove_psk( mbedtls_ssl_context *ssl ) |
| 1821 | { |
| 1822 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1823 | if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) ) |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1824 | { |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 1825 | /* The maintenance of the external PSK key slot is the |
| 1826 | * user's responsibility. */ |
| 1827 | if( ssl->handshake->psk_opaque_is_internal ) |
| 1828 | { |
| 1829 | psa_destroy_key( ssl->handshake->psk_opaque ); |
| 1830 | ssl->handshake->psk_opaque_is_internal = 0; |
| 1831 | } |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1832 | ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT; |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1833 | } |
Neil Armstrong | e952a30 | 2022-05-03 10:22:14 +0200 | [diff] [blame] | 1834 | #else |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1835 | if( ssl->handshake->psk != NULL ) |
| 1836 | { |
| 1837 | mbedtls_platform_zeroize( ssl->handshake->psk, |
| 1838 | ssl->handshake->psk_len ); |
| 1839 | mbedtls_free( ssl->handshake->psk ); |
| 1840 | ssl->handshake->psk_len = 0; |
| 1841 | } |
Neil Armstrong | e952a30 | 2022-05-03 10:22:14 +0200 | [diff] [blame] | 1842 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1843 | } |
| 1844 | |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1845 | int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl, |
| 1846 | const unsigned char *psk, size_t psk_len ) |
| 1847 | { |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 1848 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1849 | psa_key_attributes_t key_attributes = psa_key_attributes_init(); |
Jerry Yu | 5d01c05 | 2022-08-17 10:18:10 +0800 | [diff] [blame] | 1850 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Jerry Yu | 24b8c81 | 2022-08-20 19:06:56 +0800 | [diff] [blame] | 1851 | psa_algorithm_t alg = PSA_ALG_NONE; |
Jerry Yu | 5d01c05 | 2022-08-17 10:18:10 +0800 | [diff] [blame] | 1852 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 1853 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 1854 | |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1855 | if( psk == NULL || ssl->handshake == NULL ) |
| 1856 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1857 | |
| 1858 | if( psk_len > MBEDTLS_PSK_MAX_LEN ) |
| 1859 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1860 | |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1861 | ssl_remove_psk( ssl ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1862 | |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 1863 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Jerry Yu | ccc68a4 | 2022-07-26 16:39:20 +0800 | [diff] [blame] | 1864 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 1865 | if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 ) |
| 1866 | { |
Jerry Yu | 6cf6b47 | 2022-08-16 14:50:28 +0800 | [diff] [blame] | 1867 | if( ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384 ) |
Jerry Yu | ccc68a4 | 2022-07-26 16:39:20 +0800 | [diff] [blame] | 1868 | alg = PSA_ALG_TLS12_PSK_TO_MS( PSA_ALG_SHA_384 ); |
| 1869 | else |
| 1870 | alg = PSA_ALG_TLS12_PSK_TO_MS( PSA_ALG_SHA_256 ); |
| 1871 | psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE ); |
| 1872 | } |
| 1873 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 1874 | |
Jerry Yu | 568ec25 | 2022-07-22 21:27:34 +0800 | [diff] [blame] | 1875 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | ccc68a4 | 2022-07-26 16:39:20 +0800 | [diff] [blame] | 1876 | if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 ) |
| 1877 | { |
| 1878 | alg = PSA_ALG_HKDF_EXTRACT( PSA_ALG_ANY_HASH ); |
| 1879 | psa_set_key_usage_flags( &key_attributes, |
| 1880 | PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT ); |
| 1881 | } |
| 1882 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 1883 | |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 1884 | psa_set_key_algorithm( &key_attributes, alg ); |
| 1885 | psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE ); |
| 1886 | |
| 1887 | status = psa_import_key( &key_attributes, psk, psk_len, &key ); |
| 1888 | if( status != PSA_SUCCESS ) |
| 1889 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 1890 | |
| 1891 | /* Allow calling psa_destroy_key() on psk remove */ |
| 1892 | ssl->handshake->psk_opaque_is_internal = 1; |
| 1893 | return mbedtls_ssl_set_hs_psk_opaque( ssl, key ); |
| 1894 | #else |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 1895 | if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 1896 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1897 | |
| 1898 | ssl->handshake->psk_len = psk_len; |
| 1899 | memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len ); |
| 1900 | |
| 1901 | return( 0 ); |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 1902 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1903 | } |
| 1904 | |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1905 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1906 | int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf, |
Andrzej Kurek | 03e0146 | 2022-01-03 12:53:24 +0100 | [diff] [blame] | 1907 | mbedtls_svc_key_id_t psk, |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1908 | const unsigned char *psk_identity, |
| 1909 | size_t psk_identity_len ) |
| 1910 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1911 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 1912 | |
| 1913 | /* We currently only support one PSK, raw or opaque. */ |
Ronald Cron | d29e13e | 2022-10-19 10:33:48 +0200 | [diff] [blame] | 1914 | if( mbedtls_ssl_conf_has_static_psk( conf ) ) |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 1915 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1916 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1917 | /* Check and set opaque PSK */ |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1918 | if( mbedtls_svc_key_id_is_null( psk ) ) |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1919 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1920 | conf->psk_opaque = psk; |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1921 | |
| 1922 | /* Check and set PSK Identity */ |
| 1923 | ret = ssl_conf_set_psk_identity( conf, psk_identity, |
| 1924 | psk_identity_len ); |
| 1925 | if( ret != 0 ) |
| 1926 | ssl_conf_remove_psk( conf ); |
| 1927 | |
| 1928 | return( ret ); |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1929 | } |
| 1930 | |
Przemyslaw Stekiel | 6928a51 | 2022-02-03 13:50:35 +0100 | [diff] [blame] | 1931 | int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl, |
| 1932 | mbedtls_svc_key_id_t psk ) |
| 1933 | { |
| 1934 | if( ( mbedtls_svc_key_id_is_null( psk ) ) || |
| 1935 | ( ssl->handshake == NULL ) ) |
| 1936 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1937 | |
| 1938 | ssl_remove_psk( ssl ); |
| 1939 | ssl->handshake->psk_opaque = psk; |
| 1940 | return( 0 ); |
| 1941 | } |
| 1942 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 1943 | |
Jerry Yu | 8897c07 | 2022-08-12 13:56:53 +0800 | [diff] [blame] | 1944 | #if defined(MBEDTLS_SSL_SRV_C) |
Przemyslaw Stekiel | 6928a51 | 2022-02-03 13:50:35 +0100 | [diff] [blame] | 1945 | void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf, |
| 1946 | int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *, |
| 1947 | size_t), |
| 1948 | void *p_psk ) |
| 1949 | { |
| 1950 | conf->f_psk = f_psk; |
| 1951 | conf->p_psk = p_psk; |
| 1952 | } |
Jerry Yu | 8897c07 | 2022-08-12 13:56:53 +0800 | [diff] [blame] | 1953 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 1954 | |
Ronald Cron | 73fe8df | 2022-10-05 14:31:43 +0200 | [diff] [blame] | 1955 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */ |
Przemyslaw Stekiel | 6928a51 | 2022-02-03 13:50:35 +0100 | [diff] [blame] | 1956 | |
| 1957 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 301711e | 2022-04-26 16:57:05 +0200 | [diff] [blame] | 1958 | static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode( |
| 1959 | psa_algorithm_t alg ) |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 1960 | { |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 1961 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1962 | if( alg == PSA_ALG_CBC_NO_PADDING ) |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 1963 | return( MBEDTLS_SSL_MODE_CBC ); |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 1964 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1965 | if( PSA_ALG_IS_AEAD( alg ) ) |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 1966 | return( MBEDTLS_SSL_MODE_AEAD ); |
Gilles Peskine | 301711e | 2022-04-26 16:57:05 +0200 | [diff] [blame] | 1967 | return( MBEDTLS_SSL_MODE_STREAM ); |
| 1968 | } |
| 1969 | |
| 1970 | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
| 1971 | |
| 1972 | static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode( |
| 1973 | mbedtls_cipher_mode_t mode ) |
| 1974 | { |
| 1975 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
| 1976 | if( mode == MBEDTLS_MODE_CBC ) |
| 1977 | return( MBEDTLS_SSL_MODE_CBC ); |
| 1978 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
| 1979 | |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 1980 | #if defined(MBEDTLS_GCM_C) || \ |
| 1981 | defined(MBEDTLS_CCM_C) || \ |
| 1982 | defined(MBEDTLS_CHACHAPOLY_C) |
| 1983 | if( mode == MBEDTLS_MODE_GCM || |
| 1984 | mode == MBEDTLS_MODE_CCM || |
| 1985 | mode == MBEDTLS_MODE_CHACHAPOLY ) |
| 1986 | return( MBEDTLS_SSL_MODE_AEAD ); |
| 1987 | #endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */ |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 1988 | |
| 1989 | return( MBEDTLS_SSL_MODE_STREAM ); |
| 1990 | } |
Gilles Peskine | 301711e | 2022-04-26 16:57:05 +0200 | [diff] [blame] | 1991 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 1992 | |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 1993 | static mbedtls_ssl_mode_t mbedtls_ssl_get_actual_mode( |
| 1994 | mbedtls_ssl_mode_t base_mode, |
| 1995 | int encrypt_then_mac ) |
| 1996 | { |
| 1997 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
| 1998 | if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED && |
| 1999 | base_mode == MBEDTLS_SSL_MODE_CBC ) |
| 2000 | { |
| 2001 | return( MBEDTLS_SSL_MODE_CBC_ETM ); |
| 2002 | } |
| 2003 | #else |
| 2004 | (void) encrypt_then_mac; |
| 2005 | #endif |
| 2006 | return( base_mode ); |
| 2007 | } |
| 2008 | |
Neil Armstrong | ab555e0 | 2022-04-04 11:07:59 +0200 | [diff] [blame] | 2009 | mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_transform( |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2010 | const mbedtls_ssl_transform *transform ) |
| 2011 | { |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2012 | mbedtls_ssl_mode_t base_mode = mbedtls_ssl_get_base_mode( |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2013 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2014 | transform->psa_alg |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2015 | #else |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2016 | mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) |
| 2017 | #endif |
| 2018 | ); |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2019 | |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2020 | int encrypt_then_mac = 0; |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 2021 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2022 | encrypt_then_mac = transform->encrypt_then_mac; |
| 2023 | #endif |
| 2024 | return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) ); |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2025 | } |
| 2026 | |
Neil Armstrong | ab555e0 | 2022-04-04 11:07:59 +0200 | [diff] [blame] | 2027 | mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_ciphersuite( |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 2028 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2029 | int encrypt_then_mac, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 2030 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2031 | const mbedtls_ssl_ciphersuite_t *suite ) |
| 2032 | { |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2033 | mbedtls_ssl_mode_t base_mode = MBEDTLS_SSL_MODE_STREAM; |
| 2034 | |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2035 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 2036 | psa_status_t status; |
| 2037 | psa_algorithm_t alg; |
| 2038 | psa_key_type_t type; |
| 2039 | size_t size; |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2040 | status = mbedtls_ssl_cipher_to_psa( suite->cipher, 0, &alg, &type, &size ); |
| 2041 | if( status == PSA_SUCCESS ) |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2042 | base_mode = mbedtls_ssl_get_base_mode( alg ); |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2043 | #else |
| 2044 | const mbedtls_cipher_info_t *cipher = |
| 2045 | mbedtls_cipher_info_from_type( suite->cipher ); |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2046 | if( cipher != NULL ) |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2047 | { |
| 2048 | base_mode = |
| 2049 | mbedtls_ssl_get_base_mode( |
| 2050 | mbedtls_cipher_info_get_mode( cipher ) ); |
| 2051 | } |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2052 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 2053 | |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2054 | #if !defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
| 2055 | int encrypt_then_mac = 0; |
| 2056 | #endif |
| 2057 | return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) ); |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2058 | } |
| 2059 | |
Neil Armstrong | 8395d7a | 2022-05-18 11:44:56 +0200 | [diff] [blame] | 2060 | #if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 2061 | |
| 2062 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2063 | /* Serialization of TLS 1.3 sessions: |
| 2064 | * |
| 2065 | * struct { |
Xiaokang Qian | 126bf8e | 2022-10-13 02:22:40 +0000 | [diff] [blame] | 2066 | * opaque hostname<0..2^16-1>; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2067 | * uint64 ticket_received; |
| 2068 | * uint32 ticket_lifetime; |
Jerry Yu | 3419107 | 2022-08-18 10:32:09 +0800 | [diff] [blame] | 2069 | * opaque ticket<1..2^16-1>; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2070 | * } ClientOnlyData; |
| 2071 | * |
| 2072 | * struct { |
| 2073 | * uint8 endpoint; |
| 2074 | * uint8 ciphersuite[2]; |
| 2075 | * uint32 ticket_age_add; |
| 2076 | * uint8 ticket_flags; |
| 2077 | * opaque resumption_key<0..255>; |
| 2078 | * select ( endpoint ) { |
| 2079 | * case client: ClientOnlyData; |
| 2080 | * case server: uint64 start_time; |
| 2081 | * }; |
| 2082 | * } serialized_session_tls13; |
| 2083 | * |
| 2084 | */ |
| 2085 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 2086 | MBEDTLS_CHECK_RETURN_CRITICAL |
| 2087 | static int ssl_tls13_session_save( const mbedtls_ssl_session *session, |
| 2088 | unsigned char *buf, |
| 2089 | size_t buf_len, |
| 2090 | size_t *olen ) |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2091 | { |
| 2092 | unsigned char *p = buf; |
Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 2093 | #if defined(MBEDTLS_SSL_CLI_C) && \ |
| 2094 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 2095 | size_t hostname_len = ( session->hostname == NULL ) ? |
Xiaokang Qian | ed0620c | 2022-10-12 06:58:13 +0000 | [diff] [blame] | 2096 | 0 : strlen( session->hostname ) + 1; |
Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 2097 | #endif |
Jerry Yu | bc7c1a4 | 2022-07-21 22:57:37 +0800 | [diff] [blame] | 2098 | size_t needed = 1 /* endpoint */ |
| 2099 | + 2 /* ciphersuite */ |
| 2100 | + 4 /* ticket_age_add */ |
Jerry Yu | 3419107 | 2022-08-18 10:32:09 +0800 | [diff] [blame] | 2101 | + 1 /* ticket_flags */ |
| 2102 | + 1; /* resumption_key length */ |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 2103 | *olen = 0; |
Jerry Yu | 3419107 | 2022-08-18 10:32:09 +0800 | [diff] [blame] | 2104 | |
| 2105 | if( session->resumption_key_len > MBEDTLS_SSL_TLS1_3_TICKET_RESUMPTION_KEY_LEN ) |
| 2106 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2107 | needed += session->resumption_key_len; /* resumption_key */ |
| 2108 | |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2109 | #if defined(MBEDTLS_HAVE_TIME) |
| 2110 | needed += 8; /* start_time or ticket_received */ |
| 2111 | #endif |
| 2112 | |
| 2113 | #if defined(MBEDTLS_SSL_CLI_C) |
| 2114 | if( session->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 2115 | { |
Xiaokang Qian | 126bf8e | 2022-10-13 02:22:40 +0000 | [diff] [blame] | 2116 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Xiaokang Qian | bc663a0 | 2022-10-09 11:14:39 +0000 | [diff] [blame] | 2117 | needed += 2 /* hostname_len */ |
Xiaokang Qian | 2f9efd3 | 2022-10-10 11:24:08 +0000 | [diff] [blame] | 2118 | + hostname_len; /* hostname */ |
Xiaokang Qian | 281fd1b | 2022-09-20 11:35:41 +0000 | [diff] [blame] | 2119 | #endif |
| 2120 | |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2121 | needed += 4 /* ticket_lifetime */ |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 2122 | + 2; /* ticket_len */ |
Jerry Yu | 3419107 | 2022-08-18 10:32:09 +0800 | [diff] [blame] | 2123 | |
| 2124 | /* Check size_t overflow */ |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 2125 | if( session->ticket_len > SIZE_MAX - needed ) |
| 2126 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jerry Yu | 3419107 | 2022-08-18 10:32:09 +0800 | [diff] [blame] | 2127 | |
Jerry Yu | e28d974 | 2022-08-18 15:44:03 +0800 | [diff] [blame] | 2128 | needed += session->ticket_len; /* ticket */ |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2129 | } |
| 2130 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 2131 | |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 2132 | *olen = needed; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2133 | if( needed > buf_len ) |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 2134 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2135 | |
| 2136 | p[0] = session->endpoint; |
| 2137 | MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 1 ); |
| 2138 | MBEDTLS_PUT_UINT32_BE( session->ticket_age_add, p, 3 ); |
| 2139 | p[7] = session->ticket_flags; |
| 2140 | |
| 2141 | /* save resumption_key */ |
Jerry Yu | bc7c1a4 | 2022-07-21 22:57:37 +0800 | [diff] [blame] | 2142 | p[8] = session->resumption_key_len; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2143 | p += 9; |
Jerry Yu | bc7c1a4 | 2022-07-21 22:57:37 +0800 | [diff] [blame] | 2144 | memcpy( p, session->resumption_key, session->resumption_key_len ); |
| 2145 | p += session->resumption_key_len; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2146 | |
| 2147 | #if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C) |
| 2148 | if( session->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 2149 | { |
| 2150 | MBEDTLS_PUT_UINT64_BE( (uint64_t) session->start, p, 0 ); |
| 2151 | p += 8; |
| 2152 | } |
| 2153 | #endif /* MBEDTLS_HAVE_TIME */ |
| 2154 | |
| 2155 | #if defined(MBEDTLS_SSL_CLI_C) |
| 2156 | if( session->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 2157 | { |
Xiaokang Qian | ed0620c | 2022-10-12 06:58:13 +0000 | [diff] [blame] | 2158 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 2159 | MBEDTLS_PUT_UINT16_BE( hostname_len, p, 0 ); |
| 2160 | p += 2; |
Xiaokang Qian | 126bf8e | 2022-10-13 02:22:40 +0000 | [diff] [blame] | 2161 | if( hostname_len > 0 ) |
Xiaokang Qian | ed0620c | 2022-10-12 06:58:13 +0000 | [diff] [blame] | 2162 | { |
| 2163 | /* save host name */ |
| 2164 | memcpy( p, session->hostname, hostname_len ); |
| 2165 | p += hostname_len; |
| 2166 | } |
| 2167 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
| 2168 | |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2169 | #if defined(MBEDTLS_HAVE_TIME) |
| 2170 | MBEDTLS_PUT_UINT64_BE( (uint64_t) session->ticket_received, p, 0 ); |
| 2171 | p += 8; |
| 2172 | #endif |
| 2173 | MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 ); |
| 2174 | p += 4; |
| 2175 | |
| 2176 | MBEDTLS_PUT_UINT16_BE( session->ticket_len, p, 0 ); |
| 2177 | p += 2; |
Jerry Yu | 3419107 | 2022-08-18 10:32:09 +0800 | [diff] [blame] | 2178 | |
| 2179 | if( session->ticket != NULL && session->ticket_len > 0 ) |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2180 | { |
| 2181 | memcpy( p, session->ticket, session->ticket_len ); |
| 2182 | p += session->ticket_len; |
| 2183 | } |
| 2184 | } |
| 2185 | #endif /* MBEDTLS_SSL_CLI_C */ |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 2186 | return( 0 ); |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2187 | } |
| 2188 | |
| 2189 | MBEDTLS_CHECK_RETURN_CRITICAL |
| 2190 | static int ssl_tls13_session_load( mbedtls_ssl_session *session, |
| 2191 | const unsigned char *buf, |
| 2192 | size_t len ) |
| 2193 | { |
| 2194 | const unsigned char *p = buf; |
| 2195 | const unsigned char *end = buf + len; |
| 2196 | |
| 2197 | if( end - p < 9 ) |
| 2198 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2199 | session->endpoint = p[0]; |
| 2200 | session->ciphersuite = MBEDTLS_GET_UINT16_BE( p, 1 ); |
| 2201 | session->ticket_age_add = MBEDTLS_GET_UINT32_BE( p, 3 ); |
| 2202 | session->ticket_flags = p[7]; |
| 2203 | |
| 2204 | /* load resumption_key */ |
Jerry Yu | bc7c1a4 | 2022-07-21 22:57:37 +0800 | [diff] [blame] | 2205 | session->resumption_key_len = p[8]; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2206 | p += 9; |
| 2207 | |
Jerry Yu | bc7c1a4 | 2022-07-21 22:57:37 +0800 | [diff] [blame] | 2208 | if( end - p < session->resumption_key_len ) |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2209 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2210 | |
Jerry Yu | bc7c1a4 | 2022-07-21 22:57:37 +0800 | [diff] [blame] | 2211 | if( sizeof( session->resumption_key ) < session->resumption_key_len ) |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2212 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jerry Yu | bc7c1a4 | 2022-07-21 22:57:37 +0800 | [diff] [blame] | 2213 | memcpy( session->resumption_key, p, session->resumption_key_len ); |
| 2214 | p += session->resumption_key_len; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2215 | |
| 2216 | #if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C) |
| 2217 | if( session->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 2218 | { |
| 2219 | if( end - p < 8 ) |
| 2220 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2221 | session->start = MBEDTLS_GET_UINT64_BE( p, 0 ); |
| 2222 | p += 8; |
| 2223 | } |
| 2224 | #endif /* MBEDTLS_HAVE_TIME */ |
| 2225 | |
| 2226 | #if defined(MBEDTLS_SSL_CLI_C) |
| 2227 | if( session->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 2228 | { |
Xiaokang Qian | ed0620c | 2022-10-12 06:58:13 +0000 | [diff] [blame] | 2229 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \ |
| 2230 | defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 2231 | size_t hostname_len; |
| 2232 | /* load host name */ |
| 2233 | if( end - p < 2 ) |
| 2234 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2235 | hostname_len = MBEDTLS_GET_UINT16_BE( p, 0 ); |
| 2236 | p += 2; |
| 2237 | |
| 2238 | if( end - p < ( long int )hostname_len ) |
| 2239 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2240 | if( hostname_len > 0 ) |
| 2241 | { |
| 2242 | session->hostname = mbedtls_calloc( 1, hostname_len ); |
| 2243 | if( session->hostname == NULL ) |
| 2244 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 2245 | memcpy( session->hostname, p, hostname_len ); |
| 2246 | p += hostname_len; |
| 2247 | } |
| 2248 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION && |
| 2249 | MBEDTLS_SSL_SESSION_TICKETS */ |
| 2250 | |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2251 | #if defined(MBEDTLS_HAVE_TIME) |
| 2252 | if( end - p < 8 ) |
| 2253 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2254 | session->ticket_received = MBEDTLS_GET_UINT64_BE( p, 0 ); |
| 2255 | p += 8; |
| 2256 | #endif |
| 2257 | if( end - p < 4 ) |
| 2258 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2259 | session->ticket_lifetime = MBEDTLS_GET_UINT32_BE( p, 0 ); |
| 2260 | p += 4; |
| 2261 | |
| 2262 | if( end - p < 2 ) |
| 2263 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2264 | session->ticket_len = MBEDTLS_GET_UINT16_BE( p, 0 ); |
| 2265 | p += 2; |
| 2266 | |
| 2267 | if( end - p < ( long int )session->ticket_len ) |
| 2268 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2269 | if( session->ticket_len > 0 ) |
| 2270 | { |
| 2271 | session->ticket = mbedtls_calloc( 1, session->ticket_len ); |
| 2272 | if( session->ticket == NULL ) |
| 2273 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 2274 | memcpy( session->ticket, p, session->ticket_len ); |
| 2275 | p += session->ticket_len; |
| 2276 | } |
| 2277 | } |
| 2278 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 2279 | |
| 2280 | return( 0 ); |
| 2281 | |
| 2282 | } |
| 2283 | #else /* MBEDTLS_SSL_SESSION_TICKETS */ |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 2284 | MBEDTLS_CHECK_RETURN_CRITICAL |
| 2285 | static int ssl_tls13_session_save( const mbedtls_ssl_session *session, |
| 2286 | unsigned char *buf, |
| 2287 | size_t buf_len, |
| 2288 | size_t *olen ) |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 2289 | { |
| 2290 | ((void) session); |
| 2291 | ((void) buf); |
| 2292 | ((void) buf_len); |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 2293 | *olen = 0; |
| 2294 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 2295 | } |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2296 | |
| 2297 | static int ssl_tls13_session_load( const mbedtls_ssl_session *session, |
| 2298 | unsigned char *buf, |
| 2299 | size_t buf_len ) |
| 2300 | { |
| 2301 | ((void) session); |
| 2302 | ((void) buf); |
| 2303 | ((void) buf_len); |
| 2304 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 2305 | } |
| 2306 | #endif /* !MBEDTLS_SSL_SESSION_TICKETS */ |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 2307 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 2308 | |
Przemyslaw Stekiel | f57b456 | 2022-01-25 00:04:18 +0100 | [diff] [blame] | 2309 | 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] | 2310 | size_t taglen, |
| 2311 | psa_algorithm_t *alg, |
| 2312 | psa_key_type_t *key_type, |
| 2313 | size_t *key_size ) |
| 2314 | { |
| 2315 | switch ( mbedtls_cipher_type ) |
| 2316 | { |
| 2317 | case MBEDTLS_CIPHER_AES_128_CBC: |
| 2318 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 2319 | *key_type = PSA_KEY_TYPE_AES; |
| 2320 | *key_size = 128; |
| 2321 | break; |
| 2322 | case MBEDTLS_CIPHER_AES_128_CCM: |
| 2323 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 2324 | *key_type = PSA_KEY_TYPE_AES; |
| 2325 | *key_size = 128; |
| 2326 | break; |
| 2327 | case MBEDTLS_CIPHER_AES_128_GCM: |
| 2328 | *alg = PSA_ALG_GCM; |
| 2329 | *key_type = PSA_KEY_TYPE_AES; |
| 2330 | *key_size = 128; |
| 2331 | break; |
| 2332 | case MBEDTLS_CIPHER_AES_192_CCM: |
| 2333 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 2334 | *key_type = PSA_KEY_TYPE_AES; |
| 2335 | *key_size = 192; |
| 2336 | break; |
| 2337 | case MBEDTLS_CIPHER_AES_192_GCM: |
| 2338 | *alg = PSA_ALG_GCM; |
| 2339 | *key_type = PSA_KEY_TYPE_AES; |
| 2340 | *key_size = 192; |
| 2341 | break; |
| 2342 | case MBEDTLS_CIPHER_AES_256_CBC: |
| 2343 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 2344 | *key_type = PSA_KEY_TYPE_AES; |
| 2345 | *key_size = 256; |
| 2346 | break; |
| 2347 | case MBEDTLS_CIPHER_AES_256_CCM: |
| 2348 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 2349 | *key_type = PSA_KEY_TYPE_AES; |
| 2350 | *key_size = 256; |
| 2351 | break; |
| 2352 | case MBEDTLS_CIPHER_AES_256_GCM: |
| 2353 | *alg = PSA_ALG_GCM; |
| 2354 | *key_type = PSA_KEY_TYPE_AES; |
| 2355 | *key_size = 256; |
| 2356 | break; |
| 2357 | case MBEDTLS_CIPHER_ARIA_128_CBC: |
| 2358 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 2359 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2360 | *key_size = 128; |
| 2361 | break; |
| 2362 | case MBEDTLS_CIPHER_ARIA_128_CCM: |
| 2363 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 2364 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2365 | *key_size = 128; |
| 2366 | break; |
| 2367 | case MBEDTLS_CIPHER_ARIA_128_GCM: |
| 2368 | *alg = PSA_ALG_GCM; |
| 2369 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2370 | *key_size = 128; |
| 2371 | break; |
| 2372 | case MBEDTLS_CIPHER_ARIA_192_CCM: |
| 2373 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 2374 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2375 | *key_size = 192; |
| 2376 | break; |
| 2377 | case MBEDTLS_CIPHER_ARIA_192_GCM: |
| 2378 | *alg = PSA_ALG_GCM; |
| 2379 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2380 | *key_size = 192; |
| 2381 | break; |
| 2382 | case MBEDTLS_CIPHER_ARIA_256_CBC: |
| 2383 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 2384 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2385 | *key_size = 256; |
| 2386 | break; |
| 2387 | case MBEDTLS_CIPHER_ARIA_256_CCM: |
| 2388 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 2389 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2390 | *key_size = 256; |
| 2391 | break; |
| 2392 | case MBEDTLS_CIPHER_ARIA_256_GCM: |
| 2393 | *alg = PSA_ALG_GCM; |
| 2394 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2395 | *key_size = 256; |
| 2396 | break; |
| 2397 | case MBEDTLS_CIPHER_CAMELLIA_128_CBC: |
| 2398 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 2399 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2400 | *key_size = 128; |
| 2401 | break; |
| 2402 | case MBEDTLS_CIPHER_CAMELLIA_128_CCM: |
| 2403 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 2404 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2405 | *key_size = 128; |
| 2406 | break; |
| 2407 | case MBEDTLS_CIPHER_CAMELLIA_128_GCM: |
| 2408 | *alg = PSA_ALG_GCM; |
| 2409 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2410 | *key_size = 128; |
| 2411 | break; |
| 2412 | case MBEDTLS_CIPHER_CAMELLIA_192_CCM: |
| 2413 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 2414 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2415 | *key_size = 192; |
| 2416 | break; |
| 2417 | case MBEDTLS_CIPHER_CAMELLIA_192_GCM: |
| 2418 | *alg = PSA_ALG_GCM; |
| 2419 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2420 | *key_size = 192; |
| 2421 | break; |
| 2422 | case MBEDTLS_CIPHER_CAMELLIA_256_CBC: |
| 2423 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 2424 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2425 | *key_size = 256; |
| 2426 | break; |
| 2427 | case MBEDTLS_CIPHER_CAMELLIA_256_CCM: |
| 2428 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 2429 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2430 | *key_size = 256; |
| 2431 | break; |
| 2432 | case MBEDTLS_CIPHER_CAMELLIA_256_GCM: |
| 2433 | *alg = PSA_ALG_GCM; |
| 2434 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2435 | *key_size = 256; |
| 2436 | break; |
| 2437 | case MBEDTLS_CIPHER_CHACHA20_POLY1305: |
| 2438 | *alg = PSA_ALG_CHACHA20_POLY1305; |
| 2439 | *key_type = PSA_KEY_TYPE_CHACHA20; |
| 2440 | *key_size = 256; |
| 2441 | break; |
| 2442 | case MBEDTLS_CIPHER_NULL: |
| 2443 | *alg = MBEDTLS_SSL_NULL_CIPHER; |
| 2444 | *key_type = 0; |
| 2445 | *key_size = 0; |
| 2446 | break; |
| 2447 | default: |
| 2448 | return PSA_ERROR_NOT_SUPPORTED; |
| 2449 | } |
| 2450 | |
| 2451 | return PSA_SUCCESS; |
| 2452 | } |
Neil Armstrong | 8395d7a | 2022-05-18 11:44:56 +0200 | [diff] [blame] | 2453 | #endif /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2454 | |
Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 2455 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 2456 | int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf, |
| 2457 | const unsigned char *dhm_P, size_t P_len, |
| 2458 | const unsigned char *dhm_G, size_t G_len ) |
| 2459 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2460 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 2461 | |
Glenn Strauss | cee1129 | 2021-12-20 01:43:17 -0500 | [diff] [blame] | 2462 | mbedtls_mpi_free( &conf->dhm_P ); |
| 2463 | mbedtls_mpi_free( &conf->dhm_G ); |
| 2464 | |
Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 2465 | if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 || |
| 2466 | ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 ) |
| 2467 | { |
| 2468 | mbedtls_mpi_free( &conf->dhm_P ); |
| 2469 | mbedtls_mpi_free( &conf->dhm_G ); |
| 2470 | return( ret ); |
| 2471 | } |
| 2472 | |
| 2473 | return( 0 ); |
| 2474 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2475 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2476 | 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] | 2477 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2478 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 2479 | |
Glenn Strauss | cee1129 | 2021-12-20 01:43:17 -0500 | [diff] [blame] | 2480 | mbedtls_mpi_free( &conf->dhm_P ); |
| 2481 | mbedtls_mpi_free( &conf->dhm_G ); |
| 2482 | |
Gilles Peskine | e570248 | 2021-06-11 21:59:08 +0200 | [diff] [blame] | 2483 | if( ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_P, |
| 2484 | &conf->dhm_P ) ) != 0 || |
| 2485 | ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_G, |
| 2486 | &conf->dhm_G ) ) != 0 ) |
Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 2487 | { |
| 2488 | mbedtls_mpi_free( &conf->dhm_P ); |
| 2489 | mbedtls_mpi_free( &conf->dhm_G ); |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 2490 | return( ret ); |
Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 2491 | } |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 2492 | |
| 2493 | return( 0 ); |
| 2494 | } |
Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 2495 | #endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */ |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 2496 | |
Manuel Pégourié-Gonnard | bd990d6 | 2015-06-11 14:49:42 +0200 | [diff] [blame] | 2497 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C) |
| 2498 | /* |
| 2499 | * Set the minimum length for Diffie-Hellman parameters |
| 2500 | */ |
| 2501 | void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf, |
| 2502 | unsigned int bitlen ) |
| 2503 | { |
| 2504 | conf->dhm_min_bitlen = bitlen; |
| 2505 | } |
| 2506 | #endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */ |
| 2507 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 2508 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Jerry Yu | 7ddc38c | 2022-01-19 11:08:05 +0800 | [diff] [blame] | 2509 | #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] | 2510 | /* |
| 2511 | * Set allowed/preferred hashes for handshake signatures |
| 2512 | */ |
| 2513 | void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf, |
| 2514 | const int *hashes ) |
| 2515 | { |
| 2516 | conf->sig_hashes = hashes; |
| 2517 | } |
Jerry Yu | 7ddc38c | 2022-01-19 11:08:05 +0800 | [diff] [blame] | 2518 | #endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */ |
Hanno Becker | 1cd6e00 | 2021-08-10 13:27:10 +0100 | [diff] [blame] | 2519 | |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 2520 | /* Configure allowed signature algorithms for handshake */ |
Hanno Becker | 1cd6e00 | 2021-08-10 13:27:10 +0100 | [diff] [blame] | 2521 | void mbedtls_ssl_conf_sig_algs( mbedtls_ssl_config *conf, |
| 2522 | const uint16_t* sig_algs ) |
| 2523 | { |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 2524 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 2525 | conf->sig_hashes = NULL; |
| 2526 | #endif /* !MBEDTLS_DEPRECATED_REMOVED */ |
| 2527 | conf->sig_algs = sig_algs; |
Hanno Becker | 1cd6e00 | 2021-08-10 13:27:10 +0100 | [diff] [blame] | 2528 | } |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 2529 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Manuel Pégourié-Gonnard | 36a8b57 | 2015-06-17 12:43:26 +0200 | [diff] [blame] | 2530 | |
Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 2531 | #if defined(MBEDTLS_ECP_C) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 2532 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 2533 | /* |
| 2534 | * Set the allowed elliptic curves |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 2535 | * |
| 2536 | * mbedtls_ssl_setup() takes the provided list |
| 2537 | * and translates it to a list of IANA TLS group identifiers, |
| 2538 | * stored in ssl->handshake->group_list. |
| 2539 | * |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 2540 | */ |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2541 | void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2542 | const mbedtls_ecp_group_id *curve_list ) |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 2543 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2544 | conf->curve_list = curve_list; |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 2545 | conf->group_list = NULL; |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 2546 | } |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 2547 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2548 | #endif /* MBEDTLS_ECP_C */ |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 2549 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 2550 | /* |
| 2551 | * Set the allowed groups |
| 2552 | */ |
| 2553 | void mbedtls_ssl_conf_groups( mbedtls_ssl_config *conf, |
| 2554 | const uint16_t *group_list ) |
| 2555 | { |
| 2556 | #if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 2557 | conf->curve_list = NULL; |
| 2558 | #endif |
| 2559 | conf->group_list = group_list; |
| 2560 | } |
| 2561 | |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 2562 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2563 | int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2564 | { |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2565 | /* Initialize to suppress unnecessary compiler warning */ |
| 2566 | size_t hostname_len = 0; |
| 2567 | |
| 2568 | /* Check if new hostname is valid before |
| 2569 | * making any change to current one */ |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2570 | if( hostname != NULL ) |
| 2571 | { |
| 2572 | hostname_len = strlen( hostname ); |
| 2573 | |
| 2574 | if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN ) |
| 2575 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2576 | } |
| 2577 | |
| 2578 | /* Now it's clear that we will overwrite the old hostname, |
| 2579 | * so we can free it safely */ |
| 2580 | |
| 2581 | if( ssl->hostname != NULL ) |
| 2582 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 2583 | mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) ); |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2584 | mbedtls_free( ssl->hostname ); |
| 2585 | } |
| 2586 | |
| 2587 | /* Passing NULL as hostname shall clear the old one */ |
Manuel Pégourié-Gonnard | ba26c24 | 2015-05-06 10:47:06 +0100 | [diff] [blame] | 2588 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2589 | if( hostname == NULL ) |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2590 | { |
| 2591 | ssl->hostname = NULL; |
| 2592 | } |
| 2593 | else |
| 2594 | { |
| 2595 | ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 ); |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2596 | if( ssl->hostname == NULL ) |
| 2597 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Paul Bakker | 75c1a6f | 2013-08-19 14:25:29 +0200 | [diff] [blame] | 2598 | |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2599 | memcpy( ssl->hostname, hostname, hostname_len ); |
Paul Bakker | 75c1a6f | 2013-08-19 14:25:29 +0200 | [diff] [blame] | 2600 | |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2601 | ssl->hostname[hostname_len] = '\0'; |
| 2602 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2603 | |
| 2604 | return( 0 ); |
| 2605 | } |
Hanno Becker | 1a9a51c | 2017-04-07 13:02:16 +0100 | [diff] [blame] | 2606 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2607 | |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 2608 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2609 | void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2610 | int (*f_sni)(void *, mbedtls_ssl_context *, |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 2611 | const unsigned char *, size_t), |
| 2612 | void *p_sni ) |
| 2613 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2614 | conf->f_sni = f_sni; |
| 2615 | conf->p_sni = p_sni; |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 2616 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2617 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 2618 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2619 | #if defined(MBEDTLS_SSL_ALPN) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2620 | 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] | 2621 | { |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 2622 | size_t cur_len, tot_len; |
| 2623 | const char **p; |
| 2624 | |
| 2625 | /* |
Brian J Murray | 1903fb3 | 2016-11-06 04:45:15 -0800 | [diff] [blame] | 2626 | * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings |
| 2627 | * MUST NOT be truncated." |
| 2628 | * We check lengths now rather than later. |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 2629 | */ |
| 2630 | tot_len = 0; |
| 2631 | for( p = protos; *p != NULL; p++ ) |
| 2632 | { |
| 2633 | cur_len = strlen( *p ); |
| 2634 | tot_len += cur_len; |
| 2635 | |
Ronald Cron | 8216dd3 | 2020-04-23 16:41:44 +0200 | [diff] [blame] | 2636 | if( ( cur_len == 0 ) || |
| 2637 | ( cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN ) || |
| 2638 | ( tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN ) ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2639 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 2640 | } |
| 2641 | |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2642 | conf->alpn_list = protos; |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 2643 | |
| 2644 | return( 0 ); |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 2645 | } |
| 2646 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2647 | 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] | 2648 | { |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 2649 | return( ssl->alpn_chosen ); |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 2650 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2651 | #endif /* MBEDTLS_SSL_ALPN */ |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 2652 | |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2653 | #if defined(MBEDTLS_SSL_DTLS_SRTP) |
Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 2654 | void mbedtls_ssl_conf_srtp_mki_value_supported( mbedtls_ssl_config *conf, |
| 2655 | int support_mki_value ) |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 2656 | { |
| 2657 | conf->dtls_srtp_mki_support = support_mki_value; |
| 2658 | } |
| 2659 | |
Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 2660 | int mbedtls_ssl_dtls_srtp_set_mki_value( mbedtls_ssl_context *ssl, |
| 2661 | unsigned char *mki_value, |
Johan Pascal | f6417ec | 2020-09-22 15:15:19 +0200 | [diff] [blame] | 2662 | uint16_t mki_len ) |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 2663 | { |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 2664 | if( mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH ) |
Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 2665 | { |
Johan Pascal | d576fdb | 2020-09-22 10:39:53 +0200 | [diff] [blame] | 2666 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 2667 | } |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 2668 | |
| 2669 | 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] | 2670 | { |
Johan Pascal | d576fdb | 2020-09-22 10:39:53 +0200 | [diff] [blame] | 2671 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 2672 | } |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 2673 | |
| 2674 | memcpy( ssl->dtls_srtp_info.mki_value, mki_value, mki_len ); |
| 2675 | ssl->dtls_srtp_info.mki_len = mki_len; |
Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 2676 | return( 0 ); |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 2677 | } |
| 2678 | |
Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 2679 | int mbedtls_ssl_conf_dtls_srtp_protection_profiles( mbedtls_ssl_config *conf, |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 2680 | const mbedtls_ssl_srtp_profile *profiles ) |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2681 | { |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 2682 | const mbedtls_ssl_srtp_profile *p; |
| 2683 | size_t list_size = 0; |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2684 | |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 2685 | /* check the profiles list: all entry must be valid, |
| 2686 | * 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] | 2687 | for( p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET && |
| 2688 | list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH; |
| 2689 | p++ ) |
Johan Pascal | d576fdb | 2020-09-22 10:39:53 +0200 | [diff] [blame] | 2690 | { |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 2691 | if( mbedtls_ssl_check_srtp_profile_value( *p ) != MBEDTLS_TLS_SRTP_UNSET ) |
Johan Pascal | d576fdb | 2020-09-22 10:39:53 +0200 | [diff] [blame] | 2692 | { |
Johan Pascal | 76fdf1d | 2020-10-22 23:31:00 +0200 | [diff] [blame] | 2693 | list_size++; |
| 2694 | } |
| 2695 | else |
| 2696 | { |
| 2697 | /* unsupported value, stop parsing and set the size to an error value */ |
| 2698 | list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1; |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2699 | } |
| 2700 | } |
| 2701 | |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 2702 | if( list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH ) |
Johan Pascal | d387aa0 | 2020-09-23 18:47:56 +0200 | [diff] [blame] | 2703 | { |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 2704 | conf->dtls_srtp_profile_list = NULL; |
| 2705 | conf->dtls_srtp_profile_list_len = 0; |
| 2706 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2707 | } |
| 2708 | |
Johan Pascal | 9bc97ca | 2020-09-21 23:44:45 +0200 | [diff] [blame] | 2709 | conf->dtls_srtp_profile_list = profiles; |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 2710 | conf->dtls_srtp_profile_list_len = list_size; |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2711 | |
| 2712 | return( 0 ); |
| 2713 | } |
| 2714 | |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 2715 | void mbedtls_ssl_get_dtls_srtp_negotiation_result( const mbedtls_ssl_context *ssl, |
| 2716 | mbedtls_dtls_srtp_info *dtls_srtp_info ) |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2717 | { |
Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 2718 | dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile; |
| 2719 | /* do not copy the mki value if there is no chosen profile */ |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 2720 | if( dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET ) |
Johan Pascal | 0dbcd1d | 2020-10-28 11:03:07 +0100 | [diff] [blame] | 2721 | { |
Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 2722 | dtls_srtp_info->mki_len = 0; |
Johan Pascal | 0dbcd1d | 2020-10-28 11:03:07 +0100 | [diff] [blame] | 2723 | } |
Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 2724 | else |
| 2725 | { |
| 2726 | dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len; |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 2727 | memcpy( dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value, |
| 2728 | ssl->dtls_srtp_info.mki_len ); |
Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 2729 | } |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2730 | } |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2731 | #endif /* MBEDTLS_SSL_DTLS_SRTP */ |
| 2732 | |
Aditya Patwardhan | 3096f33 | 2022-07-26 14:31:46 +0530 | [diff] [blame] | 2733 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 2734 | 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] | 2735 | { |
Glenn Strauss | 2dfcea2 | 2022-03-14 17:26:42 -0400 | [diff] [blame] | 2736 | conf->max_tls_version = (major << 8) | minor; |
Paul Bakker | 490ecc8 | 2011-10-06 13:04:09 +0000 | [diff] [blame] | 2737 | } |
| 2738 | |
Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 2739 | 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] | 2740 | { |
Glenn Strauss | 2dfcea2 | 2022-03-14 17:26:42 -0400 | [diff] [blame] | 2741 | conf->min_tls_version = (major << 8) | minor; |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 2742 | } |
Aditya Patwardhan | 3096f33 | 2022-07-26 14:31:46 +0530 | [diff] [blame] | 2743 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 2744 | |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 2745 | #if defined(MBEDTLS_SSL_SRV_C) |
| 2746 | void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf, |
| 2747 | char cert_req_ca_list ) |
| 2748 | { |
| 2749 | conf->cert_req_ca_list = cert_req_ca_list; |
| 2750 | } |
| 2751 | #endif |
| 2752 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2753 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2754 | 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] | 2755 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2756 | conf->encrypt_then_mac = etm; |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 2757 | } |
| 2758 | #endif |
| 2759 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2760 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2761 | 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] | 2762 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2763 | conf->extended_ms = ems; |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 2764 | } |
| 2765 | #endif |
| 2766 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2767 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2768 | 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] | 2769 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2770 | if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID || |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 2771 | 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] | 2772 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2773 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 2774 | } |
| 2775 | |
Manuel Pégourié-Gonnard | 6bf89d6 | 2015-05-05 17:01:57 +0100 | [diff] [blame] | 2776 | conf->mfl_code = mfl_code; |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 2777 | |
| 2778 | return( 0 ); |
| 2779 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2780 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 2781 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2782 | 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] | 2783 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2784 | conf->allow_legacy_renegotiation = allow_legacy; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2785 | } |
| 2786 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2787 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2788 | 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] | 2789 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2790 | conf->disable_renegotiation = renegotiation; |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 2791 | } |
| 2792 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2793 | 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] | 2794 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2795 | conf->renego_max_records = max_records; |
Manuel Pégourié-Gonnard | a9964db | 2014-07-03 19:29:16 +0200 | [diff] [blame] | 2796 | } |
| 2797 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2798 | void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 837f0fe | 2014-11-05 13:58:53 +0100 | [diff] [blame] | 2799 | const unsigned char period[8] ) |
| 2800 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2801 | memcpy( conf->renego_period, period, 8 ); |
Manuel Pégourié-Gonnard | 837f0fe | 2014-11-05 13:58:53 +0100 | [diff] [blame] | 2802 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2803 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2804 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2805 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 2806 | #if defined(MBEDTLS_SSL_CLI_C) |
| 2807 | 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] | 2808 | { |
Manuel Pégourié-Gonnard | 2b49445 | 2015-05-06 10:05:11 +0100 | [diff] [blame] | 2809 | conf->session_tickets = use_tickets; |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 2810 | } |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 2811 | #endif |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 2812 | |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 2813 | #if defined(MBEDTLS_SSL_SRV_C) |
Jerry Yu | 1ad7ace | 2022-08-09 13:28:39 +0800 | [diff] [blame] | 2814 | |
Jerry Yu | d0766ec | 2022-09-22 10:46:57 +0800 | [diff] [blame] | 2815 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS) |
Jerry Yu | 1ad7ace | 2022-08-09 13:28:39 +0800 | [diff] [blame] | 2816 | void mbedtls_ssl_conf_new_session_tickets( mbedtls_ssl_config *conf, |
| 2817 | uint16_t num_tickets ) |
| 2818 | { |
Jerry Yu | d0766ec | 2022-09-22 10:46:57 +0800 | [diff] [blame] | 2819 | conf->new_session_tickets_count = num_tickets; |
Jerry Yu | 1ad7ace | 2022-08-09 13:28:39 +0800 | [diff] [blame] | 2820 | } |
| 2821 | #endif |
| 2822 | |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 2823 | void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf, |
| 2824 | mbedtls_ssl_ticket_write_t *f_ticket_write, |
| 2825 | mbedtls_ssl_ticket_parse_t *f_ticket_parse, |
| 2826 | void *p_ticket ) |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 2827 | { |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 2828 | conf->f_ticket_write = f_ticket_write; |
| 2829 | conf->f_ticket_parse = f_ticket_parse; |
| 2830 | conf->p_ticket = p_ticket; |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 2831 | } |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 2832 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2833 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 2834 | |
Hanno Becker | 7e6c178 | 2021-06-08 09:24:55 +0100 | [diff] [blame] | 2835 | void mbedtls_ssl_set_export_keys_cb( mbedtls_ssl_context *ssl, |
| 2836 | mbedtls_ssl_export_keys_t *f_export_keys, |
| 2837 | void *p_export_keys ) |
Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 2838 | { |
Hanno Becker | 7e6c178 | 2021-06-08 09:24:55 +0100 | [diff] [blame] | 2839 | ssl->f_export_keys = f_export_keys; |
| 2840 | ssl->p_export_keys = p_export_keys; |
Ron Eldor | f5cc10d | 2019-05-07 18:33:40 +0300 | [diff] [blame] | 2841 | } |
Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 2842 | |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 2843 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 2844 | void mbedtls_ssl_conf_async_private_cb( |
| 2845 | mbedtls_ssl_config *conf, |
| 2846 | mbedtls_ssl_async_sign_t *f_async_sign, |
| 2847 | mbedtls_ssl_async_decrypt_t *f_async_decrypt, |
| 2848 | mbedtls_ssl_async_resume_t *f_async_resume, |
| 2849 | mbedtls_ssl_async_cancel_t *f_async_cancel, |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 2850 | void *async_config_data ) |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 2851 | { |
| 2852 | conf->f_async_sign_start = f_async_sign; |
| 2853 | conf->f_async_decrypt_start = f_async_decrypt; |
| 2854 | conf->f_async_resume = f_async_resume; |
| 2855 | conf->f_async_cancel = f_async_cancel; |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 2856 | conf->p_async_config_data = async_config_data; |
| 2857 | } |
| 2858 | |
Gilles Peskine | 8f97af7 | 2018-04-26 11:46:10 +0200 | [diff] [blame] | 2859 | void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf ) |
| 2860 | { |
| 2861 | return( conf->p_async_config_data ); |
| 2862 | } |
| 2863 | |
Gilles Peskine | 1febfef | 2018-04-30 11:54:39 +0200 | [diff] [blame] | 2864 | void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl ) |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 2865 | { |
| 2866 | if( ssl->handshake == NULL ) |
| 2867 | return( NULL ); |
| 2868 | else |
| 2869 | return( ssl->handshake->user_async_ctx ); |
| 2870 | } |
| 2871 | |
Gilles Peskine | 1febfef | 2018-04-30 11:54:39 +0200 | [diff] [blame] | 2872 | void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl, |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 2873 | void *ctx ) |
| 2874 | { |
| 2875 | if( ssl->handshake != NULL ) |
| 2876 | ssl->handshake->user_async_ctx = ctx; |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 2877 | } |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 2878 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 2879 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2880 | /* |
| 2881 | * SSL get accessors |
| 2882 | */ |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 2883 | uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2884 | { |
Manuel Pégourié-Gonnard | e89163c | 2015-01-23 14:30:57 +0000 | [diff] [blame] | 2885 | if( ssl->session != NULL ) |
| 2886 | return( ssl->session->verify_result ); |
| 2887 | |
| 2888 | if( ssl->session_negotiate != NULL ) |
| 2889 | return( ssl->session_negotiate->verify_result ); |
| 2890 | |
Manuel Pégourié-Gonnard | 6ab9b00 | 2015-05-14 11:25:04 +0200 | [diff] [blame] | 2891 | return( 0xFFFFFFFF ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2892 | } |
| 2893 | |
Glenn Strauss | 8f52690 | 2022-01-13 00:04:49 -0500 | [diff] [blame] | 2894 | int mbedtls_ssl_get_ciphersuite_id_from_ssl( const mbedtls_ssl_context *ssl ) |
| 2895 | { |
| 2896 | if( ssl == NULL || ssl->session == NULL ) |
| 2897 | return( 0 ); |
| 2898 | |
| 2899 | return( ssl->session->ciphersuite ); |
| 2900 | } |
| 2901 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2902 | const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl ) |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 2903 | { |
Paul Bakker | 926c8e4 | 2013-03-06 10:23:34 +0100 | [diff] [blame] | 2904 | if( ssl == NULL || ssl->session == NULL ) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 2905 | return( NULL ); |
Paul Bakker | 926c8e4 | 2013-03-06 10:23:34 +0100 | [diff] [blame] | 2906 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2907 | return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite ); |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 2908 | } |
| 2909 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2910 | const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl ) |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 2911 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2912 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2913 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 2914 | { |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 2915 | switch( ssl->tls_version ) |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 2916 | { |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 2917 | case MBEDTLS_SSL_VERSION_TLS1_2: |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 2918 | return( "DTLSv1.2" ); |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 2919 | default: |
| 2920 | return( "unknown (DTLS)" ); |
| 2921 | } |
| 2922 | } |
| 2923 | #endif |
| 2924 | |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 2925 | switch( ssl->tls_version ) |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 2926 | { |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 2927 | case MBEDTLS_SSL_VERSION_TLS1_2: |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2928 | return( "TLSv1.2" ); |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 2929 | case MBEDTLS_SSL_VERSION_TLS1_3: |
Gilles Peskine | c63a1e0 | 2022-01-13 01:10:24 +0100 | [diff] [blame] | 2930 | return( "TLSv1.3" ); |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 2931 | default: |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 2932 | return( "unknown" ); |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 2933 | } |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 2934 | } |
| 2935 | |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 2936 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 2937 | size_t mbedtls_ssl_get_input_max_frag_len( const mbedtls_ssl_context *ssl ) |
| 2938 | { |
David Horstmann | 95d516f | 2021-05-04 18:36:56 +0100 | [diff] [blame] | 2939 | size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN; |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 2940 | size_t read_mfl; |
| 2941 | |
| 2942 | /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */ |
| 2943 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
| 2944 | ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE ) |
| 2945 | { |
| 2946 | return ssl_mfl_code_to_length( ssl->conf->mfl_code ); |
| 2947 | } |
| 2948 | |
| 2949 | /* Check if a smaller max length was negotiated */ |
| 2950 | if( ssl->session_out != NULL ) |
| 2951 | { |
| 2952 | read_mfl = ssl_mfl_code_to_length( ssl->session_out->mfl_code ); |
| 2953 | if( read_mfl < max_len ) |
| 2954 | { |
| 2955 | max_len = read_mfl; |
| 2956 | } |
| 2957 | } |
| 2958 | |
| 2959 | // During a handshake, use the value being negotiated |
| 2960 | if( ssl->session_negotiate != NULL ) |
| 2961 | { |
| 2962 | read_mfl = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ); |
| 2963 | if( read_mfl < max_len ) |
| 2964 | { |
| 2965 | max_len = read_mfl; |
| 2966 | } |
| 2967 | } |
| 2968 | |
| 2969 | return( max_len ); |
| 2970 | } |
| 2971 | |
| 2972 | 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] | 2973 | { |
| 2974 | size_t max_len; |
| 2975 | |
| 2976 | /* |
| 2977 | * Assume mfl_code is correct since it was checked when set |
| 2978 | */ |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 2979 | 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] | 2980 | |
Manuel Pégourié-Gonnard | 2cb17e2 | 2017-09-19 13:00:47 +0200 | [diff] [blame] | 2981 | /* Check if a smaller max length was negotiated */ |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 2982 | if( ssl->session_out != NULL && |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 2983 | 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] | 2984 | { |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 2985 | 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] | 2986 | } |
| 2987 | |
Manuel Pégourié-Gonnard | 2cb17e2 | 2017-09-19 13:00:47 +0200 | [diff] [blame] | 2988 | /* During a handshake, use the value being negotiated */ |
| 2989 | if( ssl->session_negotiate != NULL && |
| 2990 | ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len ) |
| 2991 | { |
| 2992 | max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ); |
| 2993 | } |
| 2994 | |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 2995 | return( max_len ); |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 2996 | } |
| 2997 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
| 2998 | |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 2999 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 8949071 | 2020-02-05 10:50:12 +0000 | [diff] [blame] | 3000 | 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] | 3001 | { |
Andrzej Kurek | ef43ce6 | 2018-10-09 08:24:12 -0400 | [diff] [blame] | 3002 | /* Return unlimited mtu for client hello messages to avoid fragmentation. */ |
| 3003 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
| 3004 | ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO || |
| 3005 | ssl->state == MBEDTLS_SSL_SERVER_HELLO ) ) |
| 3006 | return ( 0 ); |
| 3007 | |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 3008 | if( ssl->handshake == NULL || ssl->handshake->mtu == 0 ) |
| 3009 | return( ssl->mtu ); |
| 3010 | |
| 3011 | if( ssl->mtu == 0 ) |
| 3012 | return( ssl->handshake->mtu ); |
| 3013 | |
| 3014 | return( ssl->mtu < ssl->handshake->mtu ? |
| 3015 | ssl->mtu : ssl->handshake->mtu ); |
| 3016 | } |
| 3017 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 3018 | |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3019 | int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl ) |
| 3020 | { |
| 3021 | size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN; |
| 3022 | |
Manuel Pégourié-Gonnard | 000281e | 2018-08-21 11:20:58 +0200 | [diff] [blame] | 3023 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \ |
| 3024 | !defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3025 | (void) ssl; |
| 3026 | #endif |
| 3027 | |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3028 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 3029 | 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] | 3030 | |
| 3031 | if( max_len > mfl ) |
| 3032 | max_len = mfl; |
| 3033 | #endif |
| 3034 | |
| 3035 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 8949071 | 2020-02-05 10:50:12 +0000 | [diff] [blame] | 3036 | if( mbedtls_ssl_get_current_mtu( ssl ) != 0 ) |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3037 | { |
Hanno Becker | 8949071 | 2020-02-05 10:50:12 +0000 | [diff] [blame] | 3038 | const size_t mtu = mbedtls_ssl_get_current_mtu( ssl ); |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3039 | const int ret = mbedtls_ssl_get_record_expansion( ssl ); |
| 3040 | const size_t overhead = (size_t) ret; |
| 3041 | |
| 3042 | if( ret < 0 ) |
| 3043 | return( ret ); |
| 3044 | |
| 3045 | if( mtu <= overhead ) |
| 3046 | { |
| 3047 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) ); |
| 3048 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 3049 | } |
| 3050 | |
| 3051 | if( max_len > mtu - overhead ) |
| 3052 | max_len = mtu - overhead; |
| 3053 | } |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 3054 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3055 | |
Hanno Becker | 0defedb | 2018-08-10 12:35:02 +0100 | [diff] [blame] | 3056 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \ |
| 3057 | !defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3058 | ((void) ssl); |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3059 | #endif |
| 3060 | |
| 3061 | return( (int) max_len ); |
| 3062 | } |
| 3063 | |
Hanno Becker | 2d8e99b | 2021-04-21 06:19:50 +0100 | [diff] [blame] | 3064 | int mbedtls_ssl_get_max_in_record_payload( const mbedtls_ssl_context *ssl ) |
| 3065 | { |
| 3066 | size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN; |
| 3067 | |
| 3068 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 3069 | (void) ssl; |
| 3070 | #endif |
| 3071 | |
| 3072 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 3073 | const size_t mfl = mbedtls_ssl_get_input_max_frag_len( ssl ); |
| 3074 | |
| 3075 | if( max_len > mfl ) |
| 3076 | max_len = mfl; |
| 3077 | #endif |
| 3078 | |
| 3079 | return( (int) max_len ); |
| 3080 | } |
| 3081 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3082 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 3083 | 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] | 3084 | { |
| 3085 | if( ssl == NULL || ssl->session == NULL ) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 3086 | return( NULL ); |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 3087 | |
Hanno Becker | e682457 | 2019-02-07 13:18:46 +0000 | [diff] [blame] | 3088 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 3089 | return( ssl->session->peer_cert ); |
Hanno Becker | e682457 | 2019-02-07 13:18:46 +0000 | [diff] [blame] | 3090 | #else |
| 3091 | return( NULL ); |
| 3092 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 3093 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3094 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 3095 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3096 | #if defined(MBEDTLS_SSL_CLI_C) |
Hanno Becker | f852b1c | 2019-02-05 11:42:30 +0000 | [diff] [blame] | 3097 | int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, |
| 3098 | mbedtls_ssl_session *dst ) |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 3099 | { |
Hanno Becker | e810bbc | 2021-05-14 16:01:05 +0100 | [diff] [blame] | 3100 | int ret; |
| 3101 | |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 3102 | if( ssl == NULL || |
| 3103 | dst == NULL || |
| 3104 | ssl->session == NULL || |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3105 | ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT ) |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 3106 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3107 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 3108 | } |
| 3109 | |
Hanno Becker | e810bbc | 2021-05-14 16:01:05 +0100 | [diff] [blame] | 3110 | /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer |
| 3111 | * idempotent: Each session can only be exported once. |
| 3112 | * |
| 3113 | * (This is in preparation for TLS 1.3 support where we will |
| 3114 | * need the ability to export multiple sessions (aka tickets), |
| 3115 | * which will be achieved by calling mbedtls_ssl_get_session() |
| 3116 | * multiple times until it fails.) |
| 3117 | * |
| 3118 | * Check whether we have already exported the current session, |
| 3119 | * and fail if so. |
| 3120 | */ |
| 3121 | if( ssl->session->exported == 1 ) |
| 3122 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 3123 | |
| 3124 | ret = mbedtls_ssl_session_copy( dst, ssl->session ); |
| 3125 | if( ret != 0 ) |
| 3126 | return( ret ); |
| 3127 | |
| 3128 | /* Remember that we've exported the session. */ |
| 3129 | ssl->session->exported = 1; |
| 3130 | return( 0 ); |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 3131 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3132 | #endif /* MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 3133 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3134 | /* |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 3135 | * Define ticket header determining Mbed TLS version |
| 3136 | * and structure of the ticket. |
| 3137 | */ |
| 3138 | |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3139 | /* |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 3140 | * Define bitflag determining compile-time settings influencing |
| 3141 | * structure of serialized SSL sessions. |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3142 | */ |
| 3143 | |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 3144 | #if defined(MBEDTLS_HAVE_TIME) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3145 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME 1 |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 3146 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3147 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3148 | #endif /* MBEDTLS_HAVE_TIME */ |
| 3149 | |
| 3150 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3151 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3152 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3153 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3154 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 3155 | |
| 3156 | #if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3157 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3158 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3159 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3160 | #endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */ |
| 3161 | |
| 3162 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3163 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3164 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3165 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3166 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
| 3167 | |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3168 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3169 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3170 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3171 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3172 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ |
| 3173 | |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3174 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 3175 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1 |
| 3176 | #else |
| 3177 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0 |
| 3178 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
| 3179 | |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3180 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0 |
| 3181 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1 |
| 3182 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2 |
| 3183 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3 |
Hanno Becker | 37bdbe6 | 2021-08-01 05:38:58 +0100 | [diff] [blame] | 3184 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4 |
| 3185 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5 |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3186 | |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 3187 | #define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \ |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3188 | ( (uint16_t) ( \ |
| 3189 | ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \ |
| 3190 | ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \ |
| 3191 | ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \ |
| 3192 | ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \ |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3193 | ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \ |
Hanno Becker | be34e8e | 2019-06-04 09:43:16 +0100 | [diff] [blame] | 3194 | ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) ) ) |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3195 | |
Hanno Becker | f878707 | 2019-05-16 12:41:07 +0100 | [diff] [blame] | 3196 | static unsigned char ssl_serialized_session_header[] = { |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3197 | MBEDTLS_VERSION_MAJOR, |
| 3198 | MBEDTLS_VERSION_MINOR, |
| 3199 | MBEDTLS_VERSION_PATCH, |
Joe Subbiani | 2194dc4 | 2021-07-14 12:31:31 +0100 | [diff] [blame] | 3200 | MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ), |
| 3201 | MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ), |
Hanno Becker | f878707 | 2019-05-16 12:41:07 +0100 | [diff] [blame] | 3202 | }; |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 3203 | |
| 3204 | /* |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 3205 | * Serialize a session in the following format: |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 3206 | * (in the presentation language of TLS, RFC 8446 section 3) |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 3207 | * |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3208 | * struct { |
Hanno Becker | dc28b6c | 2019-05-29 11:08:00 +0100 | [diff] [blame] | 3209 | * |
Hanno Becker | dce5097 | 2021-08-01 05:39:23 +0100 | [diff] [blame] | 3210 | * opaque mbedtls_version[3]; // library version: major, minor, patch |
| 3211 | * opaque session_format[2]; // library-version specific 16-bit field |
| 3212 | * // determining the format of the remaining |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3213 | * // serialized data. |
Hanno Becker | dc28b6c | 2019-05-29 11:08:00 +0100 | [diff] [blame] | 3214 | * |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3215 | * Note: When updating the format, remember to keep |
| 3216 | * these version+format bytes. |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 3217 | * |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3218 | * // In this version, `session_format` determines |
| 3219 | * // the setting of those compile-time |
| 3220 | * // configuration options which influence |
| 3221 | * // the structure of mbedtls_ssl_session. |
| 3222 | * |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 3223 | * uint8_t minor_ver; // Protocol minor version. Possible values: |
| 3224 | * // - TLS 1.2 (3) |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3225 | * |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 3226 | * select (serialized_session.tls_version) { |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3227 | * |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 3228 | * case MBEDTLS_SSL_VERSION_TLS1_2: |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3229 | * serialized_session_tls12 data; |
| 3230 | * |
| 3231 | * }; |
| 3232 | * |
| 3233 | * } serialized_session; |
| 3234 | * |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 3235 | */ |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3236 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 3237 | MBEDTLS_CHECK_RETURN_CRITICAL |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3238 | static int ssl_session_save( const mbedtls_ssl_session *session, |
| 3239 | unsigned char omit_header, |
| 3240 | unsigned char *buf, |
| 3241 | size_t buf_len, |
| 3242 | size_t *olen ) |
| 3243 | { |
| 3244 | unsigned char *p = buf; |
| 3245 | size_t used = 0; |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 3246 | size_t remaining_len; |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 3247 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 3248 | size_t out_len; |
| 3249 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 3250 | #endif |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 3251 | if( session == NULL ) |
| 3252 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 3253 | |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3254 | if( !omit_header ) |
| 3255 | { |
| 3256 | /* |
| 3257 | * Add Mbed TLS version identifier |
| 3258 | */ |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3259 | used += sizeof( ssl_serialized_session_header ); |
| 3260 | |
| 3261 | if( used <= buf_len ) |
| 3262 | { |
| 3263 | memcpy( p, ssl_serialized_session_header, |
| 3264 | sizeof( ssl_serialized_session_header ) ); |
| 3265 | p += sizeof( ssl_serialized_session_header ); |
| 3266 | } |
| 3267 | } |
| 3268 | |
| 3269 | /* |
| 3270 | * TLS version identifier |
| 3271 | */ |
| 3272 | used += 1; |
| 3273 | if( used <= buf_len ) |
| 3274 | { |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 3275 | *p++ = MBEDTLS_BYTE_0( session->tls_version ); |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3276 | } |
| 3277 | |
| 3278 | /* Forward to version-specific serialization routine. */ |
Jerry Yu | fca4d57 | 2022-07-21 10:37:48 +0800 | [diff] [blame] | 3279 | remaining_len = (buf_len >= used) ? buf_len - used : 0; |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 3280 | switch( session->tls_version ) |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3281 | { |
| 3282 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 3283 | case MBEDTLS_SSL_VERSION_TLS1_2: |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 3284 | used += ssl_tls12_session_save( session, p, remaining_len ); |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3285 | break; |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3286 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 3287 | |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 3288 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 3289 | case MBEDTLS_SSL_VERSION_TLS1_3: |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 3290 | ret = ssl_tls13_session_save( session, p, remaining_len, &out_len ); |
| 3291 | if( ret != 0 && ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ) |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 3292 | return( ret ); |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 3293 | used += out_len; |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 3294 | break; |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 3295 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 3296 | |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3297 | default: |
| 3298 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 3299 | } |
| 3300 | |
| 3301 | *olen = used; |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 3302 | if( used > buf_len ) |
| 3303 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 3304 | |
| 3305 | return( 0 ); |
| 3306 | } |
| 3307 | |
| 3308 | /* |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 3309 | * Public wrapper for ssl_session_save() |
| 3310 | */ |
| 3311 | int mbedtls_ssl_session_save( const mbedtls_ssl_session *session, |
| 3312 | unsigned char *buf, |
| 3313 | size_t buf_len, |
| 3314 | size_t *olen ) |
| 3315 | { |
| 3316 | return( ssl_session_save( session, 0, buf, buf_len, olen ) ); |
| 3317 | } |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 3318 | |
Jerry Yu | f1b23ca | 2022-02-18 11:48:47 +0800 | [diff] [blame] | 3319 | /* |
| 3320 | * Deserialize session, see mbedtls_ssl_session_save() for format. |
| 3321 | * |
| 3322 | * This internal version is wrapped by a public function that cleans up in |
| 3323 | * case of error, and has an extra option omit_header. |
| 3324 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 3325 | MBEDTLS_CHECK_RETURN_CRITICAL |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3326 | static int ssl_session_load( mbedtls_ssl_session *session, |
| 3327 | unsigned char omit_header, |
| 3328 | const unsigned char *buf, |
| 3329 | size_t len ) |
| 3330 | { |
| 3331 | const unsigned char *p = buf; |
| 3332 | const unsigned char * const end = buf + len; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 3333 | size_t remaining_len; |
| 3334 | |
| 3335 | |
| 3336 | if( session == NULL ) |
| 3337 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3338 | |
| 3339 | if( !omit_header ) |
| 3340 | { |
| 3341 | /* |
| 3342 | * Check Mbed TLS version identifier |
| 3343 | */ |
| 3344 | |
| 3345 | if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) ) |
| 3346 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3347 | |
| 3348 | if( memcmp( p, ssl_serialized_session_header, |
| 3349 | sizeof( ssl_serialized_session_header ) ) != 0 ) |
| 3350 | { |
| 3351 | return( MBEDTLS_ERR_SSL_VERSION_MISMATCH ); |
| 3352 | } |
| 3353 | p += sizeof( ssl_serialized_session_header ); |
| 3354 | } |
| 3355 | |
| 3356 | /* |
| 3357 | * TLS version identifier |
| 3358 | */ |
| 3359 | if( 1 > (size_t)( end - p ) ) |
| 3360 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 3361 | session->tls_version = 0x0300 | *p++; |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3362 | |
| 3363 | /* Dispatch according to TLS version. */ |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 3364 | remaining_len = ( end - p ); |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 3365 | switch( session->tls_version ) |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3366 | { |
| 3367 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 3368 | case MBEDTLS_SSL_VERSION_TLS1_2: |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 3369 | return( ssl_tls12_session_load( session, p, remaining_len ) ); |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3370 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 3371 | |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 3372 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 3373 | case MBEDTLS_SSL_VERSION_TLS1_3: |
| 3374 | return( ssl_tls13_session_load( session, p, remaining_len ) ); |
| 3375 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 3376 | |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3377 | default: |
| 3378 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3379 | } |
| 3380 | } |
| 3381 | |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 3382 | /* |
Manuel Pégourié-Gonnard | b9dfc9f | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 3383 | * Deserialize session: public wrapper for error cleaning |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 3384 | */ |
| 3385 | int mbedtls_ssl_session_load( mbedtls_ssl_session *session, |
| 3386 | const unsigned char *buf, |
| 3387 | size_t len ) |
| 3388 | { |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 3389 | int ret = ssl_session_load( session, 0, buf, len ); |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 3390 | |
| 3391 | if( ret != 0 ) |
| 3392 | mbedtls_ssl_session_free( session ); |
| 3393 | |
| 3394 | return( ret ); |
| 3395 | } |
| 3396 | |
| 3397 | /* |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3398 | * Perform a single step of the SSL handshake |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3399 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 3400 | MBEDTLS_CHECK_RETURN_CRITICAL |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 3401 | static int ssl_prepare_handshake_step( mbedtls_ssl_context *ssl ) |
| 3402 | { |
| 3403 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 3404 | |
Ronald Cron | 66dbf91 | 2022-02-02 15:33:46 +0100 | [diff] [blame] | 3405 | /* |
| 3406 | * 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] | 3407 | * that were written into the output buffer by the previous handshake step, |
| 3408 | * if the write to the network callback returned with the |
Ronald Cron | 66dbf91 | 2022-02-02 15:33:46 +0100 | [diff] [blame] | 3409 | * #MBEDTLS_ERR_SSL_WANT_WRITE error code. |
| 3410 | * We proceed to the next handshake step only when all data from the |
| 3411 | * previous one have been sent to the peer, thus we make sure that this is |
| 3412 | * the case here by calling `mbedtls_ssl_flush_output()`. The function may |
| 3413 | * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case |
| 3414 | * we have to wait before to go ahead. |
| 3415 | * In the case of TLS 1.3, handshake step handlers do not send data to the |
| 3416 | * peer. Data are only sent here and through |
| 3417 | * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an |
Andrzej Kurek | 5c65c57 | 2022-04-13 14:28:52 -0400 | [diff] [blame] | 3418 | * alert occurred. |
Ronald Cron | 66dbf91 | 2022-02-02 15:33:46 +0100 | [diff] [blame] | 3419 | */ |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 3420 | if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) |
| 3421 | return( ret ); |
| 3422 | |
| 3423 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3424 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 3425 | ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING ) |
| 3426 | { |
| 3427 | if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 ) |
| 3428 | return( ret ); |
| 3429 | } |
| 3430 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 3431 | |
| 3432 | return( ret ); |
| 3433 | } |
| 3434 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3435 | int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3436 | { |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 3437 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3438 | |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 3439 | if( ssl == NULL || |
| 3440 | ssl->conf == NULL || |
| 3441 | ssl->handshake == NULL || |
Paul Elliott | 27b0d94 | 2022-03-18 21:55:32 +0000 | [diff] [blame] | 3442 | mbedtls_ssl_is_handshake_over( ssl ) == 1 ) |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 3443 | { |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 3444 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 3445 | } |
| 3446 | |
| 3447 | ret = ssl_prepare_handshake_step( ssl ); |
| 3448 | if( ret != 0 ) |
| 3449 | return( ret ); |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 3450 | |
Jerry Yu | e704781 | 2021-09-13 19:26:39 +0800 | [diff] [blame] | 3451 | ret = mbedtls_ssl_handle_pending_alert( ssl ); |
| 3452 | if( ret != 0 ) |
| 3453 | goto cleanup; |
| 3454 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3455 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3456 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 3457 | { |
Ronald Cron | 27c85e7 | 2022-03-08 11:37:55 +0100 | [diff] [blame] | 3458 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "client state: %s", |
| 3459 | mbedtls_ssl_states_str( ssl->state ) ) ); |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 3460 | |
Ronald Cron | 9f0fba3 | 2022-02-10 16:45:15 +0100 | [diff] [blame] | 3461 | switch( ssl->state ) |
| 3462 | { |
| 3463 | case MBEDTLS_SSL_HELLO_REQUEST: |
| 3464 | ssl->state = MBEDTLS_SSL_CLIENT_HELLO; |
| 3465 | break; |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 3466 | |
Ronald Cron | 9f0fba3 | 2022-02-10 16:45:15 +0100 | [diff] [blame] | 3467 | case MBEDTLS_SSL_CLIENT_HELLO: |
| 3468 | ret = mbedtls_ssl_write_client_hello( ssl ); |
| 3469 | break; |
| 3470 | |
| 3471 | default: |
| 3472 | #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] | 3473 | if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 ) |
Ronald Cron | 9f0fba3 | 2022-02-10 16:45:15 +0100 | [diff] [blame] | 3474 | ret = mbedtls_ssl_tls13_handshake_client_step( ssl ); |
| 3475 | else |
| 3476 | ret = mbedtls_ssl_handshake_client_step( ssl ); |
| 3477 | #elif defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 3478 | ret = mbedtls_ssl_handshake_client_step( ssl ); |
| 3479 | #else |
| 3480 | ret = mbedtls_ssl_tls13_handshake_client_step( ssl ); |
| 3481 | #endif |
| 3482 | } |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 3483 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3484 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3485 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3486 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 3487 | { |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3488 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 3489 | if( mbedtls_ssl_conf_is_tls13_only( ssl->conf ) ) |
Jerry Yu | 2756193 | 2021-08-27 17:07:38 +0800 | [diff] [blame] | 3490 | ret = mbedtls_ssl_tls13_handshake_server_step( ssl ); |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3491 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 3492 | |
| 3493 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 3494 | if( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) ) |
| 3495 | ret = mbedtls_ssl_handshake_server_step( ssl ); |
| 3496 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 3497 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3498 | #endif |
| 3499 | |
Jerry Yu | e704781 | 2021-09-13 19:26:39 +0800 | [diff] [blame] | 3500 | if( ret != 0 ) |
| 3501 | { |
Jerry Yu | bbd5a3f | 2021-09-18 20:50:22 +0800 | [diff] [blame] | 3502 | /* handshake_step return error. And it is same |
| 3503 | * with alert_reason. |
| 3504 | */ |
Jerry Yu | 3bf1f97 | 2021-09-22 21:37:18 +0800 | [diff] [blame] | 3505 | if( ssl->send_alert ) |
Jerry Yu | e704781 | 2021-09-13 19:26:39 +0800 | [diff] [blame] | 3506 | { |
Jerry Yu | 3bf1f97 | 2021-09-22 21:37:18 +0800 | [diff] [blame] | 3507 | ret = mbedtls_ssl_handle_pending_alert( ssl ); |
Jerry Yu | e704781 | 2021-09-13 19:26:39 +0800 | [diff] [blame] | 3508 | goto cleanup; |
| 3509 | } |
| 3510 | } |
| 3511 | |
| 3512 | cleanup: |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3513 | return( ret ); |
| 3514 | } |
| 3515 | |
| 3516 | /* |
| 3517 | * Perform the SSL handshake |
| 3518 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3519 | int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl ) |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3520 | { |
| 3521 | int ret = 0; |
| 3522 | |
Hanno Becker | a817ea4 | 2020-10-20 15:20:23 +0100 | [diff] [blame] | 3523 | /* Sanity checks */ |
| 3524 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 3525 | if( ssl == NULL || ssl->conf == NULL ) |
| 3526 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3527 | |
Hanno Becker | a817ea4 | 2020-10-20 15:20:23 +0100 | [diff] [blame] | 3528 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3529 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 3530 | ( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL ) ) |
| 3531 | { |
| 3532 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use " |
| 3533 | "mbedtls_ssl_set_timer_cb() for DTLS" ) ); |
| 3534 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3535 | } |
| 3536 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 3537 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3538 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3539 | |
Hanno Becker | a817ea4 | 2020-10-20 15:20:23 +0100 | [diff] [blame] | 3540 | /* Main handshake loop */ |
Paul Elliott | 27b0d94 | 2022-03-18 21:55:32 +0000 | [diff] [blame] | 3541 | while( mbedtls_ssl_is_handshake_over( ssl ) == 0 ) |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3542 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3543 | ret = mbedtls_ssl_handshake_step( ssl ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3544 | |
| 3545 | if( ret != 0 ) |
| 3546 | break; |
| 3547 | } |
| 3548 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3549 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3550 | |
| 3551 | return( ret ); |
| 3552 | } |
| 3553 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3554 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 3555 | #if defined(MBEDTLS_SSL_SRV_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3556 | /* |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3557 | * Write HelloRequest to request renegotiation on server |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3558 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 3559 | MBEDTLS_CHECK_RETURN_CRITICAL |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3560 | static int ssl_write_hello_request( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3561 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3562 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3563 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3564 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) ); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3565 | |
| 3566 | ssl->out_msglen = 4; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3567 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 3568 | ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST; |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3569 | |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3570 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3571 | { |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3572 | 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] | 3573 | return( ret ); |
| 3574 | } |
| 3575 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3576 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) ); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3577 | |
| 3578 | return( 0 ); |
| 3579 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3580 | #endif /* MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3581 | |
| 3582 | /* |
| 3583 | * Actually renegotiate current connection, triggered by either: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3584 | * - any side: calling mbedtls_ssl_renegotiate(), |
| 3585 | * - client: receiving a HelloRequest during mbedtls_ssl_read(), |
| 3586 | * - 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] | 3587 | * the initial handshake is completed. |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3588 | * 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] | 3589 | * 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] | 3590 | */ |
Hanno Becker | 40cdaa1 | 2020-02-05 10:48:27 +0000 | [diff] [blame] | 3591 | int mbedtls_ssl_start_renegotiation( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3592 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3593 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3594 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3595 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3596 | |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3597 | if( ( ret = ssl_handshake_init( ssl ) ) != 0 ) |
| 3598 | return( ret ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3599 | |
Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 3600 | /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and |
| 3601 | * the ServerHello will have message_seq = 1" */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3602 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3603 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3604 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ) |
Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 3605 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3606 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 3607 | ssl->handshake->out_msg_seq = 1; |
| 3608 | else |
| 3609 | ssl->handshake->in_msg_seq = 1; |
Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 3610 | } |
| 3611 | #endif |
| 3612 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3613 | ssl->state = MBEDTLS_SSL_HELLO_REQUEST; |
| 3614 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3615 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3616 | if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3617 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3618 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3619 | return( ret ); |
| 3620 | } |
| 3621 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3622 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3623 | |
| 3624 | return( 0 ); |
| 3625 | } |
| 3626 | |
| 3627 | /* |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3628 | * Renegotiate current connection on client, |
| 3629 | * or request renegotiation on server |
| 3630 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3631 | int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3632 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3633 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3634 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 3635 | if( ssl == NULL || ssl->conf == NULL ) |
| 3636 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3637 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3638 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3639 | /* On server, just send the request */ |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3640 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3641 | { |
Paul Elliott | 27b0d94 | 2022-03-18 21:55:32 +0000 | [diff] [blame] | 3642 | if( mbedtls_ssl_is_handshake_over( ssl ) == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3643 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3644 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3645 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING; |
Manuel Pégourié-Gonnard | f07f421 | 2014-08-15 19:04:47 +0200 | [diff] [blame] | 3646 | |
| 3647 | /* Did we already try/start sending HelloRequest? */ |
| 3648 | if( ssl->out_left != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3649 | return( mbedtls_ssl_flush_output( ssl ) ); |
Manuel Pégourié-Gonnard | f07f421 | 2014-08-15 19:04:47 +0200 | [diff] [blame] | 3650 | |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3651 | return( ssl_write_hello_request( ssl ) ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3652 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3653 | #endif /* MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3654 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3655 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3656 | /* |
| 3657 | * On client, either start the renegotiation process or, |
| 3658 | * if already in progress, continue the handshake |
| 3659 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3660 | if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3661 | { |
Paul Elliott | 27b0d94 | 2022-03-18 21:55:32 +0000 | [diff] [blame] | 3662 | if( mbedtls_ssl_is_handshake_over( ssl ) == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3663 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3664 | |
Hanno Becker | 40cdaa1 | 2020-02-05 10:48:27 +0000 | [diff] [blame] | 3665 | if( ( ret = mbedtls_ssl_start_renegotiation( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3666 | { |
Hanno Becker | 40cdaa1 | 2020-02-05 10:48:27 +0000 | [diff] [blame] | 3667 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_start_renegotiation", ret ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3668 | return( ret ); |
| 3669 | } |
| 3670 | } |
| 3671 | else |
| 3672 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3673 | if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3674 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3675 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3676 | return( ret ); |
| 3677 | } |
| 3678 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3679 | #endif /* MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3680 | |
Paul Bakker | 37ce0ff | 2013-10-31 14:32:04 +0100 | [diff] [blame] | 3681 | return( ret ); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3682 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3683 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3684 | |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 3685 | void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3686 | { |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 3687 | mbedtls_ssl_handshake_params *handshake = ssl->handshake; |
| 3688 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3689 | if( handshake == NULL ) |
| 3690 | return; |
| 3691 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 3692 | #if defined(MBEDTLS_ECP_C) |
| 3693 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 3694 | if ( ssl->handshake->group_list_heap_allocated ) |
| 3695 | mbedtls_free( (void*) handshake->group_list ); |
| 3696 | handshake->group_list = NULL; |
| 3697 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
| 3698 | #endif /* MBEDTLS_ECP_C */ |
| 3699 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 3700 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 3701 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 3702 | if ( ssl->handshake->sig_algs_heap_allocated ) |
| 3703 | mbedtls_free( (void*) handshake->sig_algs ); |
| 3704 | handshake->sig_algs = NULL; |
| 3705 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
Xiaofei Bai | c234ecf | 2022-02-08 09:59:23 +0000 | [diff] [blame] | 3706 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 3707 | if( ssl->handshake->certificate_request_context ) |
| 3708 | { |
| 3709 | mbedtls_free( (void*) handshake->certificate_request_context ); |
| 3710 | } |
| 3711 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 3712 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 3713 | |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3714 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
| 3715 | if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 ) |
| 3716 | { |
Gilles Peskine | 8f97af7 | 2018-04-26 11:46:10 +0200 | [diff] [blame] | 3717 | ssl->conf->f_async_cancel( ssl ); |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3718 | handshake->async_in_progress = 0; |
| 3719 | } |
| 3720 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
| 3721 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 3722 | #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] | 3723 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 3724 | psa_hash_abort( &handshake->fin_sha256_psa ); |
| 3725 | #else |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 3726 | mbedtls_sha256_free( &handshake->fin_sha256 ); |
| 3727 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3728 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 3729 | #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] | 3730 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 3731 | psa_hash_abort( &handshake->fin_sha384_psa ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3732 | #else |
Andrzej Kurek | a242e83 | 2022-08-11 10:03:14 -0400 | [diff] [blame] | 3733 | mbedtls_sha512_free( &handshake->fin_sha384 ); |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 3734 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3735 | #endif |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 3736 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3737 | #if defined(MBEDTLS_DHM_C) |
| 3738 | mbedtls_dhm_free( &handshake->dhm_ctx ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3739 | #endif |
Neil Armstrong | f3f4641 | 2022-04-12 14:43:39 +0200 | [diff] [blame] | 3740 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3741 | mbedtls_ecdh_free( &handshake->ecdh_ctx ); |
Paul Bakker | 61d113b | 2013-07-04 11:51:43 +0200 | [diff] [blame] | 3742 | #endif |
Valerio Setti | 02c25b5 | 2022-11-15 14:08:42 +0100 | [diff] [blame] | 3743 | |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 3744 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 3745 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 3746 | psa_pake_abort( &handshake->psa_pake_ctx ); |
| 3747 | psa_destroy_key( handshake->psa_pake_password ); |
| 3748 | handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT; |
| 3749 | #else |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 3750 | mbedtls_ecjpake_free( &handshake->ecjpake_ctx ); |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 3751 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | 77c0646 | 2015-09-17 13:59:49 +0200 | [diff] [blame] | 3752 | #if defined(MBEDTLS_SSL_CLI_C) |
| 3753 | mbedtls_free( handshake->ecjpake_cache ); |
| 3754 | handshake->ecjpake_cache = NULL; |
| 3755 | handshake->ecjpake_cache_len = 0; |
| 3756 | #endif |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 3757 | #endif |
Paul Bakker | 61d113b | 2013-07-04 11:51:43 +0200 | [diff] [blame] | 3758 | |
Janos Follath | 4ae5c29 | 2016-02-10 11:27:43 +0000 | [diff] [blame] | 3759 | #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ |
| 3760 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 3761 | /* explicit void pointer cast for buggy MS compiler */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3762 | mbedtls_free( (void *) handshake->curves ); |
Manuel Pégourié-Gonnard | d09453c | 2013-09-23 19:11:32 +0200 | [diff] [blame] | 3763 | #endif |
| 3764 | |
Ronald Cron | 73fe8df | 2022-10-05 14:31:43 +0200 | [diff] [blame] | 3765 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED) |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 3766 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 3767 | if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) ) |
| 3768 | { |
| 3769 | /* The maintenance of the external PSK key slot is the |
| 3770 | * user's responsibility. */ |
| 3771 | if( ssl->handshake->psk_opaque_is_internal ) |
| 3772 | { |
| 3773 | psa_destroy_key( ssl->handshake->psk_opaque ); |
| 3774 | ssl->handshake->psk_opaque_is_internal = 0; |
| 3775 | } |
| 3776 | ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT; |
| 3777 | } |
Neil Armstrong | e952a30 | 2022-05-03 10:22:14 +0200 | [diff] [blame] | 3778 | #else |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 3779 | if( handshake->psk != NULL ) |
| 3780 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 3781 | mbedtls_platform_zeroize( handshake->psk, handshake->psk_len ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 3782 | mbedtls_free( handshake->psk ); |
| 3783 | } |
Neil Armstrong | e952a30 | 2022-05-03 10:22:14 +0200 | [diff] [blame] | 3784 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Ronald Cron | 73fe8df | 2022-10-05 14:31:43 +0200 | [diff] [blame] | 3785 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */ |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 3786 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3787 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 3788 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Manuel Pégourié-Gonnard | 8372454 | 2013-09-24 22:30:56 +0200 | [diff] [blame] | 3789 | /* |
| 3790 | * Free only the linked list wrapper, not the keys themselves |
| 3791 | * since the belong to the SNI callback |
| 3792 | */ |
Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 3793 | ssl_key_cert_free( handshake->sni_key_cert ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3794 | #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] | 3795 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3796 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Manuel Pégourié-Gonnard | 6b7301c | 2017-08-15 12:08:45 +0200 | [diff] [blame] | 3797 | mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx ); |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 3798 | if( handshake->ecrs_peer_cert != NULL ) |
| 3799 | { |
| 3800 | mbedtls_x509_crt_free( handshake->ecrs_peer_cert ); |
| 3801 | mbedtls_free( handshake->ecrs_peer_cert ); |
| 3802 | } |
Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 3803 | #endif |
| 3804 | |
Hanno Becker | 7517312 | 2019-02-06 16:18:31 +0000 | [diff] [blame] | 3805 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 3806 | !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 3807 | mbedtls_pk_free( &handshake->peer_pubkey ); |
| 3808 | #endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 3809 | |
XiaokangQian | 9b93c0d | 2022-02-09 06:02:25 +0000 | [diff] [blame] | 3810 | #if defined(MBEDTLS_SSL_CLI_C) && \ |
| 3811 | ( defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3) ) |
| 3812 | mbedtls_free( handshake->cookie ); |
| 3813 | #endif /* MBEDTLS_SSL_CLI_C && |
| 3814 | ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */ |
XiaokangQian | 8499b6c | 2022-01-27 09:00:11 +0000 | [diff] [blame] | 3815 | |
| 3816 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 533ab5f | 2020-02-05 10:49:13 +0000 | [diff] [blame] | 3817 | mbedtls_ssl_flight_free( handshake->flight ); |
| 3818 | mbedtls_ssl_buffering_free( ssl ); |
XiaokangQian | 8499b6c | 2022-01-27 09:00:11 +0000 | [diff] [blame] | 3819 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 3820 | |
Ronald Cron | f12b81d | 2022-03-15 10:42:41 +0100 | [diff] [blame] | 3821 | #if defined(MBEDTLS_ECDH_C) && \ |
| 3822 | ( defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3) ) |
Neil Armstrong | f716a70 | 2022-04-04 11:23:46 +0200 | [diff] [blame] | 3823 | if( handshake->ecdh_psa_privkey_is_external == 0 ) |
Neil Armstrong | 8113d25 | 2022-03-23 10:57:04 +0100 | [diff] [blame] | 3824 | psa_destroy_key( handshake->ecdh_psa_privkey ); |
Hanno Becker | 4a63ed4 | 2019-01-08 11:39:35 +0000 | [diff] [blame] | 3825 | #endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */ |
| 3826 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3827 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | a1a568c | 2021-11-09 10:17:21 +0800 | [diff] [blame] | 3828 | mbedtls_ssl_transform_free( handshake->transform_handshake ); |
| 3829 | mbedtls_ssl_transform_free( handshake->transform_earlydata ); |
Jerry Yu | ba9c727 | 2021-10-30 11:54:10 +0800 | [diff] [blame] | 3830 | mbedtls_free( handshake->transform_earlydata ); |
| 3831 | mbedtls_free( handshake->transform_handshake ); |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3832 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Jerry Yu | ba9c727 | 2021-10-30 11:54:10 +0800 | [diff] [blame] | 3833 | |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 3834 | |
| 3835 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 3836 | /* If the buffers are too big - reallocate. Because of the way Mbed TLS |
| 3837 | * processes datagrams and the fact that a datagram is allowed to have |
| 3838 | * several records in it, it is possible that the I/O buffers are not |
| 3839 | * empty at this stage */ |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 3840 | handle_buffer_resizing( ssl, 1, mbedtls_ssl_get_input_buflen( ssl ), |
| 3841 | mbedtls_ssl_get_output_buflen( ssl ) ); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 3842 | #endif |
Hanno Becker | 3aa186f | 2021-08-10 09:24:19 +0100 | [diff] [blame] | 3843 | |
Jerry Yu | ba9c727 | 2021-10-30 11:54:10 +0800 | [diff] [blame] | 3844 | /* mbedtls_platform_zeroize MUST be last one in this function */ |
| 3845 | mbedtls_platform_zeroize( handshake, |
| 3846 | sizeof( mbedtls_ssl_handshake_params ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3847 | } |
| 3848 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3849 | void mbedtls_ssl_session_free( mbedtls_ssl_session *session ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3850 | { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3851 | if( session == NULL ) |
| 3852 | return; |
| 3853 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3854 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Hanno Becker | 1294a0b | 2019-02-05 12:38:15 +0000 | [diff] [blame] | 3855 | ssl_clear_peer_cert( session ); |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 3856 | #endif |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 3857 | |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 3858 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
Xiaokang Qian | bc663a0 | 2022-10-09 11:14:39 +0000 | [diff] [blame] | 3859 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \ |
| 3860 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Xiaokang Qian | 281fd1b | 2022-09-20 11:35:41 +0000 | [diff] [blame] | 3861 | mbedtls_free( session->hostname ); |
| 3862 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3863 | mbedtls_free( session->ticket ); |
Paul Bakker | a503a63 | 2013-08-14 13:48:06 +0200 | [diff] [blame] | 3864 | #endif |
Manuel Pégourié-Gonnard | 75d4401 | 2013-08-02 14:44:04 +0200 | [diff] [blame] | 3865 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 3866 | mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3867 | } |
| 3868 | |
Manuel Pégourié-Gonnard | 5c0e377 | 2019-07-23 16:13:17 +0200 | [diff] [blame] | 3869 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 3870 | |
| 3871 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 3872 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u |
| 3873 | #else |
| 3874 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u |
| 3875 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 3876 | |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 3877 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 3878 | |
| 3879 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 3880 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u |
| 3881 | #else |
| 3882 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u |
| 3883 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
| 3884 | |
| 3885 | #if defined(MBEDTLS_SSL_ALPN) |
| 3886 | #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u |
| 3887 | #else |
| 3888 | #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u |
| 3889 | #endif /* MBEDTLS_SSL_ALPN */ |
| 3890 | |
| 3891 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0 |
| 3892 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1 |
| 3893 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2 |
| 3894 | #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3 |
| 3895 | |
| 3896 | #define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \ |
| 3897 | ( (uint32_t) ( \ |
| 3898 | ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \ |
| 3899 | ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \ |
| 3900 | ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \ |
| 3901 | ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \ |
| 3902 | 0u ) ) |
| 3903 | |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3904 | static unsigned char ssl_serialized_context_header[] = { |
| 3905 | MBEDTLS_VERSION_MAJOR, |
| 3906 | MBEDTLS_VERSION_MINOR, |
| 3907 | MBEDTLS_VERSION_PATCH, |
Joe Subbiani | 2194dc4 | 2021-07-14 12:31:31 +0100 | [diff] [blame] | 3908 | MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ), |
| 3909 | MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ), |
| 3910 | MBEDTLS_BYTE_2( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ), |
| 3911 | MBEDTLS_BYTE_1( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ), |
| 3912 | MBEDTLS_BYTE_0( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ), |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3913 | }; |
| 3914 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3915 | /* |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 3916 | * Serialize a full SSL context |
Manuel Pégourié-Gonnard | 00400c2 | 2019-07-10 14:58:45 +0200 | [diff] [blame] | 3917 | * |
| 3918 | * The format of the serialized data is: |
| 3919 | * (in the presentation language of TLS, RFC 8446 section 3) |
| 3920 | * |
| 3921 | * // header |
| 3922 | * opaque mbedtls_version[3]; // major, minor, patch |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3923 | * opaque context_format[5]; // version-specific field determining |
Manuel Pégourié-Gonnard | 00400c2 | 2019-07-10 14:58:45 +0200 | [diff] [blame] | 3924 | * // the format of the remaining |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3925 | * // serialized data. |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 3926 | * Note: When updating the format, remember to keep these |
| 3927 | * 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] | 3928 | * |
| 3929 | * // session sub-structure |
| 3930 | * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save() |
| 3931 | * // transform sub-structure |
| 3932 | * uint8 random[64]; // ServerHello.random+ClientHello.random |
| 3933 | * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value |
| 3934 | * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use |
| 3935 | * // fields from ssl_context |
| 3936 | * uint32 badmac_seen; // DTLS: number of records with failing MAC |
| 3937 | * uint64 in_window_top; // DTLS: last validated record seq_num |
| 3938 | * uint64 in_window; // DTLS: bitmask for replay protection |
| 3939 | * uint8 disable_datagram_packing; // DTLS: only one record per datagram |
| 3940 | * uint64 cur_out_ctr; // Record layer: outgoing sequence number |
| 3941 | * uint16 mtu; // DTLS: path mtu (max outgoing fragment size) |
| 3942 | * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol |
| 3943 | * |
| 3944 | * Note that many fields of the ssl_context or sub-structures are not |
| 3945 | * serialized, as they fall in one of the following categories: |
| 3946 | * |
| 3947 | * 1. forced value (eg in_left must be 0) |
| 3948 | * 2. pointer to dynamically-allocated memory (eg session, transform) |
| 3949 | * 3. value can be re-derived from other data (eg session keys from MS) |
| 3950 | * 4. value was temporary (eg content of input buffer) |
| 3951 | * 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] | 3952 | */ |
| 3953 | int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl, |
| 3954 | unsigned char *buf, |
| 3955 | size_t buf_len, |
| 3956 | size_t *olen ) |
| 3957 | { |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3958 | unsigned char *p = buf; |
| 3959 | size_t used = 0; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3960 | size_t session_len; |
| 3961 | int ret = 0; |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3962 | |
Manuel Pégourié-Gonnard | 1aaf669 | 2019-07-10 14:14:05 +0200 | [diff] [blame] | 3963 | /* |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3964 | * Enforce usage restrictions, see "return BAD_INPUT_DATA" in |
| 3965 | * this function's documentation. |
| 3966 | * |
| 3967 | * These are due to assumptions/limitations in the implementation. Some of |
| 3968 | * them are likely to stay (no handshake in progress) some might go away |
| 3969 | * (only DTLS) but are currently used to simplify the implementation. |
Manuel Pégourié-Gonnard | 1aaf669 | 2019-07-10 14:14:05 +0200 | [diff] [blame] | 3970 | */ |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3971 | /* The initial handshake must be over */ |
Paul Elliott | 27b0d94 | 2022-03-18 21:55:32 +0000 | [diff] [blame] | 3972 | if( mbedtls_ssl_is_handshake_over( ssl ) == 0 ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3973 | { |
| 3974 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Initial handshake isn't over" ) ); |
Manuel Pégourié-Gonnard | 1aaf669 | 2019-07-10 14:14:05 +0200 | [diff] [blame] | 3975 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3976 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3977 | if( ssl->handshake != NULL ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3978 | { |
| 3979 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Handshake isn't completed" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3980 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3981 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3982 | /* Double-check that sub-structures are indeed ready */ |
| 3983 | if( ssl->transform == NULL || ssl->session == NULL ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3984 | { |
| 3985 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Serialised structures aren't ready" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3986 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3987 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3988 | /* There must be no pending incoming or outgoing data */ |
| 3989 | if( mbedtls_ssl_check_pending( ssl ) != 0 ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3990 | { |
| 3991 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending incoming data" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3992 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3993 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3994 | if( ssl->out_left != 0 ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3995 | { |
| 3996 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending outgoing data" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3997 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3998 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3999 | /* Protocol must be DLTS, not TLS */ |
| 4000 | if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 4001 | { |
| 4002 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only DTLS is supported" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 4003 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 4004 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 4005 | /* Version must be 1.2 */ |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 4006 | if( ssl->tls_version != MBEDTLS_SSL_VERSION_TLS1_2 ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 4007 | { |
| 4008 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only version 1.2 supported" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 4009 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 4010 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 4011 | /* We must be using an AEAD ciphersuite */ |
| 4012 | if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 4013 | { |
| 4014 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only AEAD ciphersuites supported" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 4015 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 4016 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 4017 | /* Renegotiation must not be enabled */ |
| 4018 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 4019 | if( ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 4020 | { |
| 4021 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Renegotiation must not be enabled" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 4022 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 4023 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 4024 | #endif |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 4025 | |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4026 | /* |
| 4027 | * Version and format identifier |
| 4028 | */ |
| 4029 | used += sizeof( ssl_serialized_context_header ); |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 4030 | |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4031 | if( used <= buf_len ) |
| 4032 | { |
| 4033 | memcpy( p, ssl_serialized_context_header, |
| 4034 | sizeof( ssl_serialized_context_header ) ); |
| 4035 | p += sizeof( ssl_serialized_context_header ); |
| 4036 | } |
| 4037 | |
| 4038 | /* |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4039 | * Session (length + data) |
| 4040 | */ |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 4041 | 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] | 4042 | if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ) |
| 4043 | return( ret ); |
| 4044 | |
| 4045 | used += 4 + session_len; |
| 4046 | if( used <= buf_len ) |
| 4047 | { |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 4048 | MBEDTLS_PUT_UINT32_BE( session_len, p, 0 ); |
| 4049 | p += 4; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4050 | |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 4051 | ret = ssl_session_save( ssl->session, 1, |
| 4052 | p, session_len, &session_len ); |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4053 | if( ret != 0 ) |
| 4054 | return( ret ); |
| 4055 | |
| 4056 | p += session_len; |
| 4057 | } |
| 4058 | |
| 4059 | /* |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4060 | * Transform |
| 4061 | */ |
| 4062 | used += sizeof( ssl->transform->randbytes ); |
| 4063 | if( used <= buf_len ) |
| 4064 | { |
| 4065 | memcpy( p, ssl->transform->randbytes, |
| 4066 | sizeof( ssl->transform->randbytes ) ); |
| 4067 | p += sizeof( ssl->transform->randbytes ); |
| 4068 | } |
| 4069 | |
| 4070 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 4071 | used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len; |
| 4072 | if( used <= buf_len ) |
| 4073 | { |
| 4074 | *p++ = ssl->transform->in_cid_len; |
| 4075 | memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len ); |
| 4076 | p += ssl->transform->in_cid_len; |
| 4077 | |
| 4078 | *p++ = ssl->transform->out_cid_len; |
| 4079 | memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len ); |
| 4080 | p += ssl->transform->out_cid_len; |
| 4081 | } |
| 4082 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 4083 | |
| 4084 | /* |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4085 | * Saved fields from top-level ssl_context structure |
| 4086 | */ |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4087 | used += 4; |
| 4088 | if( used <= buf_len ) |
| 4089 | { |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 4090 | MBEDTLS_PUT_UINT32_BE( ssl->badmac_seen, p, 0 ); |
| 4091 | p += 4; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4092 | } |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4093 | |
| 4094 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 4095 | used += 16; |
| 4096 | if( used <= buf_len ) |
| 4097 | { |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 4098 | MBEDTLS_PUT_UINT64_BE( ssl->in_window_top, p, 0 ); |
| 4099 | p += 8; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4100 | |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 4101 | MBEDTLS_PUT_UINT64_BE( ssl->in_window, p, 0 ); |
| 4102 | p += 8; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4103 | } |
| 4104 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
| 4105 | |
| 4106 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4107 | used += 1; |
| 4108 | if( used <= buf_len ) |
| 4109 | { |
| 4110 | *p++ = ssl->disable_datagram_packing; |
| 4111 | } |
| 4112 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 4113 | |
Jerry Yu | ae0b2e2 | 2021-10-08 15:21:19 +0800 | [diff] [blame] | 4114 | used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4115 | if( used <= buf_len ) |
| 4116 | { |
Jerry Yu | ae0b2e2 | 2021-10-08 15:21:19 +0800 | [diff] [blame] | 4117 | memcpy( p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN ); |
| 4118 | p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4119 | } |
| 4120 | |
| 4121 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4122 | used += 2; |
| 4123 | if( used <= buf_len ) |
| 4124 | { |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 4125 | MBEDTLS_PUT_UINT16_BE( ssl->mtu, p, 0 ); |
| 4126 | p += 2; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4127 | } |
| 4128 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 4129 | |
| 4130 | #if defined(MBEDTLS_SSL_ALPN) |
| 4131 | { |
| 4132 | const uint8_t alpn_len = ssl->alpn_chosen |
Manuel Pégourié-Gonnard | f041f4e | 2019-07-24 00:58:27 +0200 | [diff] [blame] | 4133 | ? (uint8_t) strlen( ssl->alpn_chosen ) |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4134 | : 0; |
| 4135 | |
| 4136 | used += 1 + alpn_len; |
| 4137 | if( used <= buf_len ) |
| 4138 | { |
| 4139 | *p++ = alpn_len; |
| 4140 | |
| 4141 | if( ssl->alpn_chosen != NULL ) |
| 4142 | { |
| 4143 | memcpy( p, ssl->alpn_chosen, alpn_len ); |
| 4144 | p += alpn_len; |
| 4145 | } |
| 4146 | } |
| 4147 | } |
| 4148 | #endif /* MBEDTLS_SSL_ALPN */ |
| 4149 | |
| 4150 | /* |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4151 | * Done |
| 4152 | */ |
| 4153 | *olen = used; |
| 4154 | |
| 4155 | if( used > buf_len ) |
| 4156 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 4157 | |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4158 | MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used ); |
| 4159 | |
Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 4160 | return( mbedtls_ssl_session_reset_int( ssl, 0 ) ); |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 4161 | } |
| 4162 | |
| 4163 | /* |
Manuel Pégourié-Gonnard | b9dfc9f | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 4164 | * Deserialize context, see mbedtls_ssl_context_save() for format. |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4165 | * |
| 4166 | * This internal version is wrapped by a public function that cleans up in |
| 4167 | * case of error. |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 4168 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 4169 | MBEDTLS_CHECK_RETURN_CRITICAL |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4170 | static int ssl_context_load( mbedtls_ssl_context *ssl, |
| 4171 | const unsigned char *buf, |
| 4172 | size_t len ) |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 4173 | { |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4174 | const unsigned char *p = buf; |
| 4175 | const unsigned char * const end = buf + len; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4176 | size_t session_len; |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 4177 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Andrzej Kurek | 2d59dbc | 2022-10-13 08:34:38 -0400 | [diff] [blame] | 4178 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Andrzej Kurek | 894edde | 2022-09-29 06:31:14 -0400 | [diff] [blame] | 4179 | tls_prf_fn prf_func = NULL; |
Andrzej Kurek | 2d59dbc | 2022-10-13 08:34:38 -0400 | [diff] [blame] | 4180 | #endif |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4181 | |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 4182 | /* |
| 4183 | * The context should have been freshly setup or reset. |
| 4184 | * Give the user an error in case of obvious misuse. |
Manuel Pégourié-Gonnard | 4ca930f | 2019-07-26 16:31:53 +0200 | [diff] [blame] | 4185 | * (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] | 4186 | * renegotiating, or if the user mistakenly loaded a session first.) |
| 4187 | */ |
| 4188 | if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST || |
| 4189 | ssl->session != NULL ) |
| 4190 | { |
| 4191 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4192 | } |
| 4193 | |
| 4194 | /* |
| 4195 | * We can't check that the config matches the initial one, but we can at |
| 4196 | * least check it matches the requirements for serializing. |
| 4197 | */ |
| 4198 | if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM || |
Glenn Strauss | 2dfcea2 | 2022-03-14 17:26:42 -0400 | [diff] [blame] | 4199 | ssl->conf->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2 || |
| 4200 | ssl->conf->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 || |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 4201 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | 9a96fd7 | 2019-07-23 17:11:24 +0200 | [diff] [blame] | 4202 | ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED || |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 4203 | #endif |
Manuel Pégourié-Gonnard | 9a96fd7 | 2019-07-23 17:11:24 +0200 | [diff] [blame] | 4204 | 0 ) |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 4205 | { |
| 4206 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4207 | } |
| 4208 | |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4209 | MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len ); |
| 4210 | |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4211 | /* |
| 4212 | * Check version identifier |
| 4213 | */ |
| 4214 | if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) ) |
| 4215 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4216 | |
| 4217 | if( memcmp( p, ssl_serialized_context_header, |
| 4218 | sizeof( ssl_serialized_context_header ) ) != 0 ) |
| 4219 | { |
| 4220 | return( MBEDTLS_ERR_SSL_VERSION_MISMATCH ); |
| 4221 | } |
| 4222 | p += sizeof( ssl_serialized_context_header ); |
| 4223 | |
| 4224 | /* |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4225 | * Session |
| 4226 | */ |
| 4227 | if( (size_t)( end - p ) < 4 ) |
| 4228 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4229 | |
| 4230 | session_len = ( (size_t) p[0] << 24 ) | |
| 4231 | ( (size_t) p[1] << 16 ) | |
| 4232 | ( (size_t) p[2] << 8 ) | |
| 4233 | ( (size_t) p[3] ); |
| 4234 | p += 4; |
| 4235 | |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 4236 | /* This has been allocated by ssl_handshake_init(), called by |
Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 4237 | * 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] | 4238 | ssl->session = ssl->session_negotiate; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4239 | ssl->session_in = ssl->session; |
| 4240 | ssl->session_out = ssl->session; |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 4241 | ssl->session_negotiate = NULL; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4242 | |
| 4243 | if( (size_t)( end - p ) < session_len ) |
| 4244 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4245 | |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 4246 | ret = ssl_session_load( ssl->session, 1, p, session_len ); |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4247 | if( ret != 0 ) |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 4248 | { |
| 4249 | mbedtls_ssl_session_free( ssl->session ); |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4250 | return( ret ); |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 4251 | } |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4252 | |
| 4253 | p += session_len; |
| 4254 | |
| 4255 | /* |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4256 | * Transform |
| 4257 | */ |
| 4258 | |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 4259 | /* This has been allocated by ssl_handshake_init(), called by |
Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 4260 | * 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] | 4261 | ssl->transform = ssl->transform_negotiate; |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4262 | ssl->transform_in = ssl->transform; |
| 4263 | ssl->transform_out = ssl->transform; |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 4264 | ssl->transform_negotiate = NULL; |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4265 | |
Andrzej Kurek | 2d59dbc | 2022-10-13 08:34:38 -0400 | [diff] [blame] | 4266 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Andrzej Kurek | 894edde | 2022-09-29 06:31:14 -0400 | [diff] [blame] | 4267 | prf_func = ssl_tls12prf_from_cs( ssl->session->ciphersuite ); |
| 4268 | if( prf_func == NULL ) |
| 4269 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4270 | |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4271 | /* Read random bytes and populate structure */ |
| 4272 | if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) ) |
| 4273 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Andrzej Kurek | 2d59dbc | 2022-10-13 08:34:38 -0400 | [diff] [blame] | 4274 | |
Hanno Becker | bd25755 | 2021-03-22 06:59:27 +0000 | [diff] [blame] | 4275 | ret = ssl_tls12_populate_transform( ssl->transform, |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4276 | ssl->session->ciphersuite, |
| 4277 | ssl->session->master, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 4278 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4279 | ssl->session->encrypt_then_mac, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 4280 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Andrzej Kurek | 894edde | 2022-09-29 06:31:14 -0400 | [diff] [blame] | 4281 | prf_func, |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4282 | p, /* currently pointing to randbytes */ |
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 4283 | 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] | 4284 | ssl->conf->endpoint, |
| 4285 | ssl ); |
| 4286 | if( ret != 0 ) |
| 4287 | return( ret ); |
Jerry Yu | 840fbb2 | 2022-02-17 14:59:29 +0800 | [diff] [blame] | 4288 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4289 | p += sizeof( ssl->transform->randbytes ); |
| 4290 | |
| 4291 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 4292 | /* Read connection IDs and store them */ |
| 4293 | if( (size_t)( end - p ) < 1 ) |
| 4294 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4295 | |
| 4296 | ssl->transform->in_cid_len = *p++; |
| 4297 | |
Manuel Pégourié-Gonnard | 5ea13b8 | 2019-07-23 15:02:54 +0200 | [diff] [blame] | 4298 | 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] | 4299 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4300 | |
| 4301 | memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len ); |
| 4302 | p += ssl->transform->in_cid_len; |
| 4303 | |
| 4304 | ssl->transform->out_cid_len = *p++; |
| 4305 | |
| 4306 | if( (size_t)( end - p ) < ssl->transform->out_cid_len ) |
| 4307 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4308 | |
| 4309 | memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len ); |
| 4310 | p += ssl->transform->out_cid_len; |
| 4311 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 4312 | |
| 4313 | /* |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4314 | * Saved fields from top-level ssl_context structure |
| 4315 | */ |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4316 | if( (size_t)( end - p ) < 4 ) |
| 4317 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4318 | |
| 4319 | ssl->badmac_seen = ( (uint32_t) p[0] << 24 ) | |
| 4320 | ( (uint32_t) p[1] << 16 ) | |
| 4321 | ( (uint32_t) p[2] << 8 ) | |
| 4322 | ( (uint32_t) p[3] ); |
| 4323 | p += 4; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4324 | |
| 4325 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 4326 | if( (size_t)( end - p ) < 16 ) |
| 4327 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4328 | |
| 4329 | ssl->in_window_top = ( (uint64_t) p[0] << 56 ) | |
| 4330 | ( (uint64_t) p[1] << 48 ) | |
| 4331 | ( (uint64_t) p[2] << 40 ) | |
| 4332 | ( (uint64_t) p[3] << 32 ) | |
| 4333 | ( (uint64_t) p[4] << 24 ) | |
| 4334 | ( (uint64_t) p[5] << 16 ) | |
| 4335 | ( (uint64_t) p[6] << 8 ) | |
| 4336 | ( (uint64_t) p[7] ); |
| 4337 | p += 8; |
| 4338 | |
| 4339 | ssl->in_window = ( (uint64_t) p[0] << 56 ) | |
| 4340 | ( (uint64_t) p[1] << 48 ) | |
| 4341 | ( (uint64_t) p[2] << 40 ) | |
| 4342 | ( (uint64_t) p[3] << 32 ) | |
| 4343 | ( (uint64_t) p[4] << 24 ) | |
| 4344 | ( (uint64_t) p[5] << 16 ) | |
| 4345 | ( (uint64_t) p[6] << 8 ) | |
| 4346 | ( (uint64_t) p[7] ); |
| 4347 | p += 8; |
| 4348 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
| 4349 | |
| 4350 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4351 | if( (size_t)( end - p ) < 1 ) |
| 4352 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4353 | |
| 4354 | ssl->disable_datagram_packing = *p++; |
| 4355 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 4356 | |
Jerry Yu | d9a94fe | 2021-09-28 18:58:59 +0800 | [diff] [blame] | 4357 | if( (size_t)( end - p ) < sizeof( ssl->cur_out_ctr ) ) |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4358 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jerry Yu | d9a94fe | 2021-09-28 18:58:59 +0800 | [diff] [blame] | 4359 | memcpy( ssl->cur_out_ctr, p, sizeof( ssl->cur_out_ctr ) ); |
| 4360 | p += sizeof( ssl->cur_out_ctr ); |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4361 | |
| 4362 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4363 | if( (size_t)( end - p ) < 2 ) |
| 4364 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4365 | |
| 4366 | ssl->mtu = ( p[0] << 8 ) | p[1]; |
| 4367 | p += 2; |
| 4368 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 4369 | |
| 4370 | #if defined(MBEDTLS_SSL_ALPN) |
| 4371 | { |
| 4372 | uint8_t alpn_len; |
| 4373 | const char **cur; |
| 4374 | |
| 4375 | if( (size_t)( end - p ) < 1 ) |
| 4376 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4377 | |
| 4378 | alpn_len = *p++; |
| 4379 | |
| 4380 | if( alpn_len != 0 && ssl->conf->alpn_list != NULL ) |
| 4381 | { |
| 4382 | /* alpn_chosen should point to an item in the configured list */ |
| 4383 | for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ ) |
| 4384 | { |
| 4385 | if( strlen( *cur ) == alpn_len && |
| 4386 | memcmp( p, cur, alpn_len ) == 0 ) |
| 4387 | { |
| 4388 | ssl->alpn_chosen = *cur; |
| 4389 | break; |
| 4390 | } |
| 4391 | } |
| 4392 | } |
| 4393 | |
| 4394 | /* can only happen on conf mismatch */ |
| 4395 | if( alpn_len != 0 && ssl->alpn_chosen == NULL ) |
| 4396 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4397 | |
| 4398 | p += alpn_len; |
| 4399 | } |
| 4400 | #endif /* MBEDTLS_SSL_ALPN */ |
| 4401 | |
| 4402 | /* |
Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 4403 | * Forced fields from top-level ssl_context structure |
| 4404 | * |
| 4405 | * Most of them already set to the correct value by mbedtls_ssl_init() and |
| 4406 | * mbedtls_ssl_reset(), so we only need to set the remaining ones. |
| 4407 | */ |
| 4408 | ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER; |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 4409 | ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 4410 | |
Hanno Becker | 361b10d | 2019-08-30 10:42:49 +0100 | [diff] [blame] | 4411 | /* Adjust pointers for header fields of outgoing records to |
| 4412 | * the given transform, accounting for explicit IV and CID. */ |
Hanno Becker | 3e6f8ab | 2020-02-05 10:40:57 +0000 | [diff] [blame] | 4413 | mbedtls_ssl_update_out_pointers( ssl, ssl->transform ); |
Hanno Becker | 361b10d | 2019-08-30 10:42:49 +0100 | [diff] [blame] | 4414 | |
Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 4415 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4416 | ssl->in_epoch = 1; |
| 4417 | #endif |
| 4418 | |
| 4419 | /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated, |
| 4420 | * 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] | 4421 | * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform() |
| 4422 | * inappropriately. */ |
Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 4423 | if( ssl->handshake != NULL ) |
| 4424 | { |
| 4425 | mbedtls_ssl_handshake_free( ssl ); |
| 4426 | mbedtls_free( ssl->handshake ); |
| 4427 | ssl->handshake = NULL; |
| 4428 | } |
| 4429 | |
| 4430 | /* |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4431 | * Done - should have consumed entire buffer |
| 4432 | */ |
| 4433 | if( p != end ) |
| 4434 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 4435 | |
| 4436 | return( 0 ); |
| 4437 | } |
| 4438 | |
| 4439 | /* |
Manuel Pégourié-Gonnard | b9dfc9f | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 4440 | * Deserialize context: public wrapper for error cleaning |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4441 | */ |
| 4442 | int mbedtls_ssl_context_load( mbedtls_ssl_context *context, |
| 4443 | const unsigned char *buf, |
| 4444 | size_t len ) |
| 4445 | { |
| 4446 | int ret = ssl_context_load( context, buf, len ); |
| 4447 | |
| 4448 | if( ret != 0 ) |
| 4449 | mbedtls_ssl_free( context ); |
| 4450 | |
| 4451 | return( ret ); |
| 4452 | } |
Manuel Pégourié-Gonnard | 5c0e377 | 2019-07-23 16:13:17 +0200 | [diff] [blame] | 4453 | #endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */ |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4454 | |
| 4455 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4456 | * Free an SSL context |
| 4457 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4458 | void mbedtls_ssl_free( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4459 | { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 4460 | if( ssl == NULL ) |
| 4461 | return; |
| 4462 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4463 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4464 | |
Manuel Pégourié-Gonnard | 7ee6f0e | 2014-02-13 10:54:07 +0100 | [diff] [blame] | 4465 | if( ssl->out_buf != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4466 | { |
sander-visser | b8aa207 | 2020-05-06 22:05:13 +0200 | [diff] [blame] | 4467 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 4468 | size_t out_buf_len = ssl->out_buf_len; |
| 4469 | #else |
| 4470 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; |
| 4471 | #endif |
| 4472 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 4473 | mbedtls_platform_zeroize( ssl->out_buf, out_buf_len ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4474 | mbedtls_free( ssl->out_buf ); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 4475 | ssl->out_buf = NULL; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4476 | } |
| 4477 | |
Manuel Pégourié-Gonnard | 7ee6f0e | 2014-02-13 10:54:07 +0100 | [diff] [blame] | 4478 | if( ssl->in_buf != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4479 | { |
sander-visser | b8aa207 | 2020-05-06 22:05:13 +0200 | [diff] [blame] | 4480 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 4481 | size_t in_buf_len = ssl->in_buf_len; |
| 4482 | #else |
| 4483 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; |
| 4484 | #endif |
| 4485 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 4486 | mbedtls_platform_zeroize( ssl->in_buf, in_buf_len ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4487 | mbedtls_free( ssl->in_buf ); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 4488 | ssl->in_buf = NULL; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4489 | } |
| 4490 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4491 | if( ssl->transform ) |
| 4492 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4493 | mbedtls_ssl_transform_free( ssl->transform ); |
| 4494 | mbedtls_free( ssl->transform ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4495 | } |
| 4496 | |
| 4497 | if( ssl->handshake ) |
| 4498 | { |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 4499 | mbedtls_ssl_handshake_free( ssl ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4500 | mbedtls_ssl_transform_free( ssl->transform_negotiate ); |
| 4501 | mbedtls_ssl_session_free( ssl->session_negotiate ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4502 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4503 | mbedtls_free( ssl->handshake ); |
| 4504 | mbedtls_free( ssl->transform_negotiate ); |
| 4505 | mbedtls_free( ssl->session_negotiate ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4506 | } |
| 4507 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4508 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Hanno Becker | 3aa186f | 2021-08-10 09:24:19 +0100 | [diff] [blame] | 4509 | mbedtls_ssl_transform_free( ssl->transform_application ); |
| 4510 | mbedtls_free( ssl->transform_application ); |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4511 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | 3aa186f | 2021-08-10 09:24:19 +0100 | [diff] [blame] | 4512 | |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 4513 | if( ssl->session ) |
| 4514 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4515 | mbedtls_ssl_session_free( ssl->session ); |
| 4516 | mbedtls_free( ssl->session ); |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 4517 | } |
| 4518 | |
Manuel Pégourié-Gonnard | 55fab2d | 2015-05-11 16:15:19 +0200 | [diff] [blame] | 4519 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 4520 | if( ssl->hostname != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4521 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 4522 | mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4523 | mbedtls_free( ssl->hostname ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4524 | } |
Paul Bakker | 0be444a | 2013-08-27 21:55:01 +0200 | [diff] [blame] | 4525 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4526 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 4527 | #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] | 4528 | mbedtls_free( ssl->cli_id ); |
Manuel Pégourié-Gonnard | 43c0218 | 2014-07-22 17:32:01 +0200 | [diff] [blame] | 4529 | #endif |
| 4530 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4531 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) ); |
Paul Bakker | 2da561c | 2009-02-05 18:00:28 +0000 | [diff] [blame] | 4532 | |
Paul Bakker | 86f04f4 | 2013-02-14 11:20:09 +0100 | [diff] [blame] | 4533 | /* Actually clear after last debug message */ |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 4534 | mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4535 | } |
| 4536 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4537 | /* |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 4538 | * Initialize mbedtls_ssl_config |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4539 | */ |
| 4540 | void mbedtls_ssl_config_init( mbedtls_ssl_config *conf ) |
| 4541 | { |
| 4542 | memset( conf, 0, sizeof( mbedtls_ssl_config ) ); |
| 4543 | } |
| 4544 | |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4545 | /* The selection should be the same as mbedtls_x509_crt_profile_default in |
| 4546 | * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters: |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4547 | * curves with a lower resource usage come first. |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4548 | * See the documentation of mbedtls_ssl_conf_curves() for what we promise |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4549 | * about this list. |
| 4550 | */ |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4551 | static uint16_t ssl_preset_default_groups[] = { |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4552 | #if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4553 | MBEDTLS_SSL_IANA_TLS_GROUP_X25519, |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4554 | #endif |
| 4555 | #if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4556 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1, |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4557 | #endif |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4558 | #if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4559 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4560 | #endif |
| 4561 | #if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4562 | MBEDTLS_SSL_IANA_TLS_GROUP_X448, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4563 | #endif |
| 4564 | #if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4565 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4566 | #endif |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4567 | #if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4568 | MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1, |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4569 | #endif |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4570 | #if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4571 | MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4572 | #endif |
| 4573 | #if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4574 | MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4575 | #endif |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4576 | MBEDTLS_SSL_IANA_TLS_GROUP_NONE |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4577 | }; |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4578 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4579 | static int ssl_preset_suiteb_ciphersuites[] = { |
| 4580 | MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, |
| 4581 | MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, |
| 4582 | 0 |
| 4583 | }; |
| 4584 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 4585 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Hanno Becker | 9c6aa7b | 2021-08-10 13:50:43 +0100 | [diff] [blame] | 4586 | |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4587 | /* NOTICE: |
Jerry Yu | 0b994b8 | 2022-01-25 17:22:12 +0800 | [diff] [blame] | 4588 | * 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] | 4589 | * rules SHOULD be upheld. |
| 4590 | * - No duplicate entries. |
| 4591 | * - 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] | 4592 | * - ssl_tls12_preset* is for TLS 1.2 use only. |
Jerry Yu | 370e146 | 2022-01-25 10:36:53 +0800 | [diff] [blame] | 4593 | * - 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] | 4594 | */ |
Hanno Becker | 9c6aa7b | 2021-08-10 13:50:43 +0100 | [diff] [blame] | 4595 | static uint16_t ssl_preset_default_sig_algs[] = { |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4596 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4597 | #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] | 4598 | defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) |
| 4599 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256, |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4600 | #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] | 4601 | MBEDTLS_ECP_DP_SECP256R1_ENABLED */ |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4602 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4603 | #if defined(MBEDTLS_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] | 4604 | defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) |
| 4605 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384, |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4606 | #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] | 4607 | MBEDTLS_ECP_DP_SECP384R1_ENABLED */ |
| 4608 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4609 | #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] | 4610 | defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) |
| 4611 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512, |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4612 | #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] | 4613 | MBEDTLS_ECP_DP_SECP521R1_ENABLED */ |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4614 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4615 | #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] | 4616 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512, |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4617 | #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] | 4618 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4619 | #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] | 4620 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384, |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4621 | #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] | 4622 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4623 | #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] | 4624 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256, |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4625 | #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] | 4626 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4627 | #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] | 4628 | MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512, |
| 4629 | #endif /* MBEDTLS_RSA_C && MBEDTLS_SHA512_C */ |
| 4630 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4631 | #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] | 4632 | MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384, |
| 4633 | #endif /* MBEDTLS_RSA_C && MBEDTLS_SHA384_C */ |
| 4634 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4635 | #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] | 4636 | MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256, |
| 4637 | #endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */ |
| 4638 | |
Gabor Mezei | 15b95a6 | 2022-05-09 16:37:58 +0200 | [diff] [blame] | 4639 | MBEDTLS_TLS_SIG_NONE |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4640 | }; |
| 4641 | |
| 4642 | /* NOTICE: see above */ |
| 4643 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 4644 | static uint16_t ssl_tls12_preset_default_sig_algs[] = { |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4645 | #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] | 4646 | #if defined(MBEDTLS_ECDSA_C) |
| 4647 | 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] | 4648 | #endif |
Jerry Yu | 09a99fc | 2022-07-28 14:22:17 +0800 | [diff] [blame] | 4649 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) |
| 4650 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512, |
| 4651 | #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */ |
Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 4652 | #if defined(MBEDTLS_RSA_C) |
| 4653 | MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA512 ), |
| 4654 | #endif |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4655 | #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] | 4656 | #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] | 4657 | #if defined(MBEDTLS_ECDSA_C) |
| 4658 | 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] | 4659 | #endif |
Jerry Yu | 09a99fc | 2022-07-28 14:22:17 +0800 | [diff] [blame] | 4660 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) |
| 4661 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384, |
| 4662 | #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */ |
Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 4663 | #if defined(MBEDTLS_RSA_C) |
| 4664 | MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ), |
| 4665 | #endif |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4666 | #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] | 4667 | #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] | 4668 | #if defined(MBEDTLS_ECDSA_C) |
| 4669 | 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] | 4670 | #endif |
Jerry Yu | 09a99fc | 2022-07-28 14:22:17 +0800 | [diff] [blame] | 4671 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) |
| 4672 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256, |
| 4673 | #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */ |
Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 4674 | #if defined(MBEDTLS_RSA_C) |
| 4675 | MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ), |
| 4676 | #endif |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4677 | #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] | 4678 | MBEDTLS_TLS_SIG_NONE |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4679 | }; |
| 4680 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 4681 | /* NOTICE: see above */ |
| 4682 | static uint16_t ssl_preset_suiteb_sig_algs[] = { |
| 4683 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4684 | #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] | 4685 | defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) |
| 4686 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256, |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4687 | #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] | 4688 | MBEDTLS_ECP_DP_SECP256R1_ENABLED */ |
| 4689 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4690 | #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] | 4691 | defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) |
| 4692 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384, |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4693 | #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] | 4694 | MBEDTLS_ECP_DP_SECP384R1_ENABLED */ |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4695 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4696 | #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] | 4697 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256, |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4698 | #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] | 4699 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4700 | #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] | 4701 | MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256, |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4702 | #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] | 4703 | |
Gabor Mezei | 15b95a6 | 2022-05-09 16:37:58 +0200 | [diff] [blame] | 4704 | MBEDTLS_TLS_SIG_NONE |
Jerry Yu | 713013f | 2022-01-17 18:16:35 +0800 | [diff] [blame] | 4705 | }; |
Jerry Yu | 6106fdc | 2022-01-12 16:36:14 +0800 | [diff] [blame] | 4706 | |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4707 | /* NOTICE: see above */ |
| 4708 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 4709 | static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = { |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4710 | #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] | 4711 | #if defined(MBEDTLS_ECDSA_C) |
| 4712 | 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] | 4713 | #endif |
Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 4714 | #if defined(MBEDTLS_RSA_C) |
| 4715 | MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ), |
| 4716 | #endif |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4717 | #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] | 4718 | #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] | 4719 | #if defined(MBEDTLS_ECDSA_C) |
| 4720 | 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] | 4721 | #endif |
Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 4722 | #if defined(MBEDTLS_RSA_C) |
| 4723 | MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ), |
| 4724 | #endif |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4725 | #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] | 4726 | MBEDTLS_TLS_SIG_NONE |
Hanno Becker | 9c6aa7b | 2021-08-10 13:50:43 +0100 | [diff] [blame] | 4727 | }; |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4728 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 4729 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 4730 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4731 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4732 | static uint16_t ssl_preset_suiteb_groups[] = { |
Jaeden Amero | d431104 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 4733 | #if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4734 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1, |
Jaeden Amero | d431104 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 4735 | #endif |
| 4736 | #if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4737 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1, |
Jaeden Amero | d431104 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 4738 | #endif |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4739 | MBEDTLS_SSL_IANA_TLS_GROUP_NONE |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4740 | }; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4741 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 4742 | #if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4743 | /* 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] | 4744 | * to make sure there are no duplicated signature algorithm entries. */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 4745 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 4746 | static int ssl_check_no_sig_alg_duplication( uint16_t * sig_algs ) |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4747 | { |
| 4748 | size_t i, j; |
| 4749 | int ret = 0; |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4750 | |
Gabor Mezei | 15b95a6 | 2022-05-09 16:37:58 +0200 | [diff] [blame] | 4751 | for( i = 0; sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ ) |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4752 | { |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 4753 | for( j = 0; j < i; j++ ) |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4754 | { |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 4755 | if( sig_algs[i] != sig_algs[j] ) |
| 4756 | continue; |
| 4757 | mbedtls_printf( " entry(%04x,%" MBEDTLS_PRINTF_SIZET |
| 4758 | ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n", |
| 4759 | sig_algs[i], j, i ); |
| 4760 | ret = -1; |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4761 | } |
| 4762 | } |
| 4763 | return( ret ); |
| 4764 | } |
| 4765 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 4766 | #endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4767 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4768 | /* |
Tillmann Karras | 588ad50 | 2015-09-25 04:27:22 +0200 | [diff] [blame] | 4769 | * Load default in mbedtls_ssl_config |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4770 | */ |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 4771 | int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4772 | int endpoint, int transport, int preset ) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4773 | { |
Manuel Pégourié-Gonnard | 8b431fb | 2015-05-11 12:54:52 +0200 | [diff] [blame] | 4774 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 4775 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 8b431fb | 2015-05-11 12:54:52 +0200 | [diff] [blame] | 4776 | #endif |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4777 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 4778 | #if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 4779 | if( ssl_check_no_sig_alg_duplication( ssl_preset_suiteb_sig_algs ) ) |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4780 | { |
| 4781 | mbedtls_printf( "ssl_preset_suiteb_sig_algs has duplicated entries\n" ); |
| 4782 | return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED ); |
| 4783 | } |
| 4784 | |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 4785 | if( ssl_check_no_sig_alg_duplication( ssl_preset_default_sig_algs ) ) |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4786 | { |
| 4787 | mbedtls_printf( "ssl_preset_default_sig_algs has duplicated entries\n" ); |
| 4788 | return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED ); |
| 4789 | } |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4790 | |
| 4791 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 4792 | 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] | 4793 | { |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4794 | mbedtls_printf( "ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n" ); |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4795 | return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED ); |
| 4796 | } |
| 4797 | |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 4798 | 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] | 4799 | { |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4800 | mbedtls_printf( "ssl_tls12_preset_default_sig_algs has duplicated entries\n" ); |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4801 | return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED ); |
| 4802 | } |
| 4803 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 4804 | #endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4805 | |
Manuel Pégourié-Gonnard | 0de074f | 2015-05-14 12:58:01 +0200 | [diff] [blame] | 4806 | /* Use the functions here so that they are covered in tests, |
| 4807 | * but otherwise access member directly for efficiency */ |
| 4808 | mbedtls_ssl_conf_endpoint( conf, endpoint ); |
| 4809 | mbedtls_ssl_conf_transport( conf, transport ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4810 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4811 | /* |
| 4812 | * Things that are common to all presets |
| 4813 | */ |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 4814 | #if defined(MBEDTLS_SSL_CLI_C) |
| 4815 | if( endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 4816 | { |
| 4817 | conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED; |
| 4818 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 4819 | conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED; |
| 4820 | #endif |
| 4821 | } |
| 4822 | #endif |
| 4823 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4824 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 4825 | conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED; |
| 4826 | #endif |
| 4827 | |
| 4828 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 4829 | conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED; |
| 4830 | #endif |
| 4831 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 4832 | #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] | 4833 | conf->f_cookie_write = ssl_cookie_write_dummy; |
| 4834 | conf->f_cookie_check = ssl_cookie_check_dummy; |
| 4835 | #endif |
| 4836 | |
| 4837 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 4838 | conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED; |
| 4839 | #endif |
| 4840 | |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 4841 | #if defined(MBEDTLS_SSL_SRV_C) |
| 4842 | conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED; |
TRodziewicz | 3946f79 | 2021-06-14 12:11:18 +0200 | [diff] [blame] | 4843 | conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER; |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 4844 | #endif |
| 4845 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4846 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4847 | conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN; |
| 4848 | conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX; |
| 4849 | #endif |
| 4850 | |
| 4851 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 4852 | conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT; |
Andres AG | 2196c7f | 2016-12-15 17:01:16 +0000 | [diff] [blame] | 4853 | memset( conf->renego_period, 0x00, 2 ); |
| 4854 | memset( conf->renego_period + 2, 0xFF, 6 ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4855 | #endif |
| 4856 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4857 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
Hanno Becker | e2defad | 2021-07-24 05:59:17 +0100 | [diff] [blame] | 4858 | if( endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 4859 | { |
| 4860 | const unsigned char dhm_p[] = |
| 4861 | MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN; |
| 4862 | const unsigned char dhm_g[] = |
| 4863 | MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN; |
Hanno Becker | 00d0a68 | 2017-10-04 13:14:29 +0100 | [diff] [blame] | 4864 | |
Hanno Becker | e2defad | 2021-07-24 05:59:17 +0100 | [diff] [blame] | 4865 | if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf, |
| 4866 | dhm_p, sizeof( dhm_p ), |
| 4867 | dhm_g, sizeof( dhm_g ) ) ) != 0 ) |
| 4868 | { |
| 4869 | return( ret ); |
| 4870 | } |
| 4871 | } |
Manuel Pégourié-Gonnard | bd990d6 | 2015-06-11 14:49:42 +0200 | [diff] [blame] | 4872 | #endif |
| 4873 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4874 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | d0766ec | 2022-09-22 10:46:57 +0800 | [diff] [blame] | 4875 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SESSION_TICKETS) |
Jerry Yu | 1ad7ace | 2022-08-09 13:28:39 +0800 | [diff] [blame] | 4876 | mbedtls_ssl_conf_new_session_tickets( |
| 4877 | conf, MBEDTLS_SSL_TLS1_3_DEFAULT_NEW_SESSION_TICKETS ); |
| 4878 | #endif |
Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 4879 | /* |
| 4880 | * Allow all TLS 1.3 key exchange modes by default. |
| 4881 | */ |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 4882 | conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL; |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4883 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 4884 | |
XiaokangQian | 4d3a604 | 2022-04-21 13:46:17 +0000 | [diff] [blame] | 4885 | if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 4886 | { |
Glenn Strauss | 2dfcea2 | 2022-03-14 17:26:42 -0400 | [diff] [blame] | 4887 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
XiaokangQian | 4d3a604 | 2022-04-21 13:46:17 +0000 | [diff] [blame] | 4888 | conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
XiaokangQian | 060d867 | 2022-04-21 09:24:56 +0000 | [diff] [blame] | 4889 | conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
XiaokangQian | 4d3a604 | 2022-04-21 13:46:17 +0000 | [diff] [blame] | 4890 | #else |
XiaokangQian | 060d867 | 2022-04-21 09:24:56 +0000 | [diff] [blame] | 4891 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
XiaokangQian | 060d867 | 2022-04-21 09:24:56 +0000 | [diff] [blame] | 4892 | #endif |
XiaokangQian | 4d3a604 | 2022-04-21 13:46:17 +0000 | [diff] [blame] | 4893 | } |
| 4894 | else |
| 4895 | { |
| 4896 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 4897 | if( endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 4898 | { |
| 4899 | conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 4900 | conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3; |
| 4901 | } |
| 4902 | else |
| 4903 | /* Hybrid TLS 1.2 / 1.3 is not supported on server side yet */ |
| 4904 | { |
| 4905 | conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 4906 | conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 4907 | } |
| 4908 | #elif defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 4909 | conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_3; |
| 4910 | conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3; |
| 4911 | #elif defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 4912 | conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 4913 | conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 4914 | #else |
| 4915 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 4916 | #endif |
| 4917 | } |
Glenn Strauss | 2dfcea2 | 2022-03-14 17:26:42 -0400 | [diff] [blame] | 4918 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4919 | /* |
| 4920 | * Preset-specific defaults |
| 4921 | */ |
| 4922 | switch( preset ) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4923 | { |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4924 | /* |
| 4925 | * NSA Suite B |
| 4926 | */ |
| 4927 | case MBEDTLS_SSL_PRESET_SUITEB: |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4928 | |
Hanno Becker | d60b6c6 | 2021-04-29 12:04:11 +0100 | [diff] [blame] | 4929 | conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4930 | |
| 4931 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 4932 | conf->cert_profile = &mbedtls_x509_crt_profile_suiteb; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4933 | #endif |
| 4934 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 4935 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4936 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 4937 | if( mbedtls_ssl_conf_is_tls12_only( conf ) ) |
| 4938 | conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs; |
| 4939 | else |
| 4940 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 4941 | conf->sig_algs = ssl_preset_suiteb_sig_algs; |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 4942 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4943 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4944 | #if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 4945 | conf->curve_list = NULL; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4946 | #endif |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4947 | conf->group_list = ssl_preset_suiteb_groups; |
Manuel Pégourié-Gonnard | c98204e | 2015-08-11 04:21:01 +0200 | [diff] [blame] | 4948 | break; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4949 | |
| 4950 | /* |
| 4951 | * Default |
| 4952 | */ |
| 4953 | default: |
Ronald Cron | f660655 | 2022-03-15 11:23:25 +0100 | [diff] [blame] | 4954 | |
Hanno Becker | d60b6c6 | 2021-04-29 12:04:11 +0100 | [diff] [blame] | 4955 | conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites(); |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4956 | |
| 4957 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 4958 | conf->cert_profile = &mbedtls_x509_crt_profile_default; |
| 4959 | #endif |
| 4960 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 4961 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4962 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 4963 | if( mbedtls_ssl_conf_is_tls12_only( conf ) ) |
| 4964 | conf->sig_algs = ssl_tls12_preset_default_sig_algs; |
| 4965 | else |
| 4966 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 4967 | conf->sig_algs = ssl_preset_default_sig_algs; |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 4968 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4969 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4970 | #if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 4971 | conf->curve_list = NULL; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4972 | #endif |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4973 | conf->group_list = ssl_preset_default_groups; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4974 | |
| 4975 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C) |
| 4976 | conf->dhm_min_bitlen = 1024; |
| 4977 | #endif |
| 4978 | } |
| 4979 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4980 | return( 0 ); |
| 4981 | } |
| 4982 | |
| 4983 | /* |
| 4984 | * Free mbedtls_ssl_config |
| 4985 | */ |
| 4986 | void mbedtls_ssl_config_free( mbedtls_ssl_config *conf ) |
| 4987 | { |
| 4988 | #if defined(MBEDTLS_DHM_C) |
| 4989 | mbedtls_mpi_free( &conf->dhm_P ); |
| 4990 | mbedtls_mpi_free( &conf->dhm_G ); |
| 4991 | #endif |
| 4992 | |
Ronald Cron | 73fe8df | 2022-10-05 14:31:43 +0200 | [diff] [blame] | 4993 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED) |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 4994 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 4995 | if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) ) |
| 4996 | { |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 4997 | conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT; |
| 4998 | } |
Neil Armstrong | 8ecd668 | 2022-05-05 11:40:35 +0200 | [diff] [blame] | 4999 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5000 | if( conf->psk != NULL ) |
| 5001 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 5002 | mbedtls_platform_zeroize( conf->psk, conf->psk_len ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5003 | mbedtls_free( conf->psk ); |
Azim Khan | 27e8a12 | 2018-03-21 14:24:11 +0000 | [diff] [blame] | 5004 | conf->psk = NULL; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5005 | conf->psk_len = 0; |
junyeonLEE | 316b162 | 2017-12-20 16:29:30 +0900 | [diff] [blame] | 5006 | } |
| 5007 | |
| 5008 | if( conf->psk_identity != NULL ) |
| 5009 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 5010 | mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len ); |
junyeonLEE | 316b162 | 2017-12-20 16:29:30 +0900 | [diff] [blame] | 5011 | mbedtls_free( conf->psk_identity ); |
Azim Khan | 27e8a12 | 2018-03-21 14:24:11 +0000 | [diff] [blame] | 5012 | conf->psk_identity = NULL; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5013 | conf->psk_identity_len = 0; |
| 5014 | } |
Ronald Cron | 73fe8df | 2022-10-05 14:31:43 +0200 | [diff] [blame] | 5015 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */ |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5016 | |
| 5017 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 5018 | ssl_key_cert_free( conf->key_cert ); |
| 5019 | #endif |
| 5020 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 5021 | mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5022 | } |
| 5023 | |
Manuel Pégourié-Gonnard | 5674a97 | 2015-10-19 15:14:03 +0200 | [diff] [blame] | 5024 | #if defined(MBEDTLS_PK_C) && \ |
| 5025 | ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) ) |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 5026 | /* |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5027 | * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 5028 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5029 | 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] | 5030 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5031 | #if defined(MBEDTLS_RSA_C) |
| 5032 | if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) ) |
| 5033 | return( MBEDTLS_SSL_SIG_RSA ); |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 5034 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5035 | #if defined(MBEDTLS_ECDSA_C) |
| 5036 | if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) ) |
| 5037 | return( MBEDTLS_SSL_SIG_ECDSA ); |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 5038 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5039 | return( MBEDTLS_SSL_SIG_ANON ); |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 5040 | } |
| 5041 | |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 5042 | unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type ) |
| 5043 | { |
| 5044 | switch( type ) { |
| 5045 | case MBEDTLS_PK_RSA: |
| 5046 | return( MBEDTLS_SSL_SIG_RSA ); |
| 5047 | case MBEDTLS_PK_ECDSA: |
| 5048 | case MBEDTLS_PK_ECKEY: |
| 5049 | return( MBEDTLS_SSL_SIG_ECDSA ); |
| 5050 | default: |
| 5051 | return( MBEDTLS_SSL_SIG_ANON ); |
| 5052 | } |
| 5053 | } |
| 5054 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5055 | 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] | 5056 | { |
| 5057 | switch( sig ) |
| 5058 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5059 | #if defined(MBEDTLS_RSA_C) |
| 5060 | case MBEDTLS_SSL_SIG_RSA: |
| 5061 | return( MBEDTLS_PK_RSA ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 5062 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5063 | #if defined(MBEDTLS_ECDSA_C) |
| 5064 | case MBEDTLS_SSL_SIG_ECDSA: |
| 5065 | return( MBEDTLS_PK_ECDSA ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 5066 | #endif |
| 5067 | default: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5068 | return( MBEDTLS_PK_NONE ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 5069 | } |
| 5070 | } |
Manuel Pégourié-Gonnard | 5674a97 | 2015-10-19 15:14:03 +0200 | [diff] [blame] | 5071 | #endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */ |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 5072 | |
Manuel Pégourié-Gonnard | 1a48383 | 2013-09-20 12:29:15 +0200 | [diff] [blame] | 5073 | /* |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 5074 | * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX |
Manuel Pégourié-Gonnard | 1a48383 | 2013-09-20 12:29:15 +0200 | [diff] [blame] | 5075 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5076 | 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] | 5077 | { |
| 5078 | switch( hash ) |
| 5079 | { |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5080 | #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] | 5081 | case MBEDTLS_SSL_HASH_MD5: |
| 5082 | return( MBEDTLS_MD_MD5 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 5083 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5084 | #if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5085 | case MBEDTLS_SSL_HASH_SHA1: |
| 5086 | return( MBEDTLS_MD_SHA1 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 5087 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5088 | #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] | 5089 | case MBEDTLS_SSL_HASH_SHA224: |
| 5090 | return( MBEDTLS_MD_SHA224 ); |
Mateusz Starzyk | e3c48b4 | 2021-04-19 16:46:28 +0200 | [diff] [blame] | 5091 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5092 | #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] | 5093 | case MBEDTLS_SSL_HASH_SHA256: |
| 5094 | return( MBEDTLS_MD_SHA256 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 5095 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5096 | #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] | 5097 | case MBEDTLS_SSL_HASH_SHA384: |
| 5098 | return( MBEDTLS_MD_SHA384 ); |
Mateusz Starzyk | 3352a53 | 2021-04-06 14:28:22 +0200 | [diff] [blame] | 5099 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5100 | #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] | 5101 | case MBEDTLS_SSL_HASH_SHA512: |
| 5102 | return( MBEDTLS_MD_SHA512 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 5103 | #endif |
| 5104 | default: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5105 | return( MBEDTLS_MD_NONE ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 5106 | } |
| 5107 | } |
| 5108 | |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 5109 | /* |
| 5110 | * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX |
| 5111 | */ |
| 5112 | unsigned char mbedtls_ssl_hash_from_md_alg( int md ) |
| 5113 | { |
| 5114 | switch( md ) |
| 5115 | { |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5116 | #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] | 5117 | case MBEDTLS_MD_MD5: |
| 5118 | return( MBEDTLS_SSL_HASH_MD5 ); |
| 5119 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5120 | #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] | 5121 | case MBEDTLS_MD_SHA1: |
| 5122 | return( MBEDTLS_SSL_HASH_SHA1 ); |
| 5123 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5124 | #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] | 5125 | case MBEDTLS_MD_SHA224: |
| 5126 | return( MBEDTLS_SSL_HASH_SHA224 ); |
Mateusz Starzyk | e3c48b4 | 2021-04-19 16:46:28 +0200 | [diff] [blame] | 5127 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5128 | #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] | 5129 | case MBEDTLS_MD_SHA256: |
| 5130 | return( MBEDTLS_SSL_HASH_SHA256 ); |
| 5131 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5132 | #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] | 5133 | case MBEDTLS_MD_SHA384: |
| 5134 | return( MBEDTLS_SSL_HASH_SHA384 ); |
Mateusz Starzyk | 3352a53 | 2021-04-06 14:28:22 +0200 | [diff] [blame] | 5135 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5136 | #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] | 5137 | case MBEDTLS_MD_SHA512: |
| 5138 | return( MBEDTLS_SSL_HASH_SHA512 ); |
| 5139 | #endif |
| 5140 | default: |
| 5141 | return( MBEDTLS_SSL_HASH_NONE ); |
| 5142 | } |
| 5143 | } |
| 5144 | |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 5145 | /* |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 5146 | * 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] | 5147 | * 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] | 5148 | */ |
Manuel Pégourié-Gonnard | 0d63b84 | 2022-01-18 13:10:56 +0100 | [diff] [blame] | 5149 | 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] | 5150 | { |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5151 | const uint16_t *group_list = mbedtls_ssl_get_groups( ssl ); |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 5152 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5153 | if( group_list == NULL ) |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 5154 | return( -1 ); |
| 5155 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5156 | for( ; *group_list != 0; group_list++ ) |
| 5157 | { |
| 5158 | if( *group_list == tls_id ) |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 5159 | return( 0 ); |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5160 | } |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 5161 | |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 5162 | return( -1 ); |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 5163 | } |
Manuel Pégourié-Gonnard | 0d63b84 | 2022-01-18 13:10:56 +0100 | [diff] [blame] | 5164 | |
| 5165 | #if defined(MBEDTLS_ECP_C) |
| 5166 | /* |
| 5167 | * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id. |
| 5168 | */ |
| 5169 | int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id ) |
| 5170 | { |
Leonid Rozenboim | 19e5973 | 2022-08-08 16:52:38 -0700 | [diff] [blame] | 5171 | const mbedtls_ecp_curve_info *grp_info = |
Tom Cosgrove | bcc13c9 | 2022-08-24 15:08:16 +0100 | [diff] [blame] | 5172 | mbedtls_ecp_curve_info_from_grp_id( grp_id ); |
Leonid Rozenboim | 19e5973 | 2022-08-08 16:52:38 -0700 | [diff] [blame] | 5173 | |
Tom Cosgrove | bcc13c9 | 2022-08-24 15:08:16 +0100 | [diff] [blame] | 5174 | if ( grp_info == NULL ) |
Leonid Rozenboim | 19e5973 | 2022-08-08 16:52:38 -0700 | [diff] [blame] | 5175 | return -1; |
| 5176 | |
| 5177 | uint16_t tls_id = grp_info->tls_id; |
| 5178 | |
Manuel Pégourié-Gonnard | 0d63b84 | 2022-01-18 13:10:56 +0100 | [diff] [blame] | 5179 | return mbedtls_ssl_check_curve_tls_id( ssl, tls_id ); |
| 5180 | } |
Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 5181 | #endif /* MBEDTLS_ECP_C */ |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5182 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5183 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 5184 | int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert, |
| 5185 | const mbedtls_ssl_ciphersuite_t *ciphersuite, |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 5186 | int cert_endpoint, |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 5187 | uint32_t *flags ) |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5188 | { |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 5189 | int ret = 0; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5190 | int usage = 0; |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 5191 | const char *ext_oid; |
| 5192 | size_t ext_len; |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 5193 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5194 | if( cert_endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5195 | { |
| 5196 | /* Server part of the key exchange */ |
| 5197 | switch( ciphersuite->key_exchange ) |
| 5198 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5199 | case MBEDTLS_KEY_EXCHANGE_RSA: |
| 5200 | case MBEDTLS_KEY_EXCHANGE_RSA_PSK: |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 5201 | usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5202 | break; |
| 5203 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5204 | case MBEDTLS_KEY_EXCHANGE_DHE_RSA: |
| 5205 | case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA: |
| 5206 | case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA: |
| 5207 | usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5208 | break; |
| 5209 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5210 | case MBEDTLS_KEY_EXCHANGE_ECDH_RSA: |
| 5211 | case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA: |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 5212 | usage = MBEDTLS_X509_KU_KEY_AGREEMENT; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5213 | break; |
| 5214 | |
| 5215 | /* 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] | 5216 | case MBEDTLS_KEY_EXCHANGE_NONE: |
| 5217 | case MBEDTLS_KEY_EXCHANGE_PSK: |
| 5218 | case MBEDTLS_KEY_EXCHANGE_DHE_PSK: |
| 5219 | case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK: |
Manuel Pégourié-Gonnard | 557535d | 2015-09-15 17:53:32 +0200 | [diff] [blame] | 5220 | case MBEDTLS_KEY_EXCHANGE_ECJPAKE: |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5221 | usage = 0; |
| 5222 | } |
| 5223 | } |
| 5224 | else |
| 5225 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5226 | /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */ |
| 5227 | usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5228 | } |
| 5229 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5230 | if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 ) |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 5231 | { |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 5232 | *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE; |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 5233 | ret = -1; |
| 5234 | } |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5235 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5236 | if( cert_endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 5237 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5238 | ext_oid = MBEDTLS_OID_SERVER_AUTH; |
| 5239 | ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH ); |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 5240 | } |
| 5241 | else |
| 5242 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5243 | ext_oid = MBEDTLS_OID_CLIENT_AUTH; |
| 5244 | ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH ); |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 5245 | } |
| 5246 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5247 | 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] | 5248 | { |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 5249 | *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE; |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 5250 | ret = -1; |
| 5251 | } |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 5252 | |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 5253 | return( ret ); |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5254 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5255 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Manuel Pégourié-Gonnard | 3a306b9 | 2014-04-29 15:11:17 +0200 | [diff] [blame] | 5256 | |
Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 5257 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 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 ) |
| 5263 | { |
Ronald Cron | f6893e1 | 2022-01-07 22:09:01 +0100 | [diff] [blame] | 5264 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 5265 | psa_hash_operation_t *hash_operation_to_clone; |
| 5266 | psa_hash_operation_t hash_operation = psa_hash_operation_init(); |
| 5267 | |
Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 5268 | *olen = 0; |
Ronald Cron | f6893e1 | 2022-01-07 22:09:01 +0100 | [diff] [blame] | 5269 | |
| 5270 | switch( md ) |
| 5271 | { |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5272 | #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] | 5273 | case MBEDTLS_MD_SHA384: |
| 5274 | hash_operation_to_clone = &ssl->handshake->fin_sha384_psa; |
| 5275 | break; |
| 5276 | #endif |
| 5277 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5278 | #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] | 5279 | case MBEDTLS_MD_SHA256: |
| 5280 | hash_operation_to_clone = &ssl->handshake->fin_sha256_psa; |
| 5281 | break; |
| 5282 | #endif |
| 5283 | |
| 5284 | default: |
| 5285 | goto exit; |
| 5286 | } |
| 5287 | |
| 5288 | status = psa_hash_clone( hash_operation_to_clone, &hash_operation ); |
| 5289 | if( status != PSA_SUCCESS ) |
| 5290 | goto exit; |
| 5291 | |
| 5292 | status = psa_hash_finish( &hash_operation, dst, dst_len, olen ); |
| 5293 | if( status != PSA_SUCCESS ) |
| 5294 | goto exit; |
| 5295 | |
| 5296 | exit: |
Andrzej Kurek | eabeb30 | 2022-10-17 07:52:51 -0400 | [diff] [blame] | 5297 | #if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \ |
| 5298 | !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
| 5299 | (void) ssl; |
| 5300 | #endif |
Ronald Cron | b788c04 | 2022-02-15 09:14:15 +0100 | [diff] [blame] | 5301 | return( psa_ssl_status_to_mbedtls( status ) ); |
Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 5302 | } |
| 5303 | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
| 5304 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5305 | #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] | 5306 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | 000f976 | 2021-09-14 11:12:51 +0800 | [diff] [blame] | 5307 | static int ssl_get_handshake_transcript_sha384( mbedtls_ssl_context *ssl, |
| 5308 | unsigned char *dst, |
| 5309 | size_t dst_len, |
| 5310 | size_t *olen ) |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5311 | { |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5312 | int ret; |
| 5313 | mbedtls_sha512_context sha512; |
| 5314 | |
| 5315 | if( dst_len < 48 ) |
| 5316 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 5317 | |
| 5318 | mbedtls_sha512_init( &sha512 ); |
Andrzej Kurek | a242e83 | 2022-08-11 10:03:14 -0400 | [diff] [blame] | 5319 | mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 ); |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5320 | |
| 5321 | if( ( ret = mbedtls_sha512_finish( &sha512, dst ) ) != 0 ) |
| 5322 | { |
| 5323 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha512_finish", ret ); |
| 5324 | goto exit; |
| 5325 | } |
| 5326 | |
| 5327 | *olen = 48; |
| 5328 | |
| 5329 | exit: |
| 5330 | |
| 5331 | mbedtls_sha512_free( &sha512 ); |
| 5332 | return( ret ); |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5333 | } |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5334 | #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] | 5335 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5336 | #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] | 5337 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | 000f976 | 2021-09-14 11:12:51 +0800 | [diff] [blame] | 5338 | static int ssl_get_handshake_transcript_sha256( mbedtls_ssl_context *ssl, |
| 5339 | unsigned char *dst, |
| 5340 | size_t dst_len, |
| 5341 | size_t *olen ) |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5342 | { |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5343 | int ret; |
| 5344 | mbedtls_sha256_context sha256; |
| 5345 | |
| 5346 | if( dst_len < 32 ) |
| 5347 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 5348 | |
| 5349 | mbedtls_sha256_init( &sha256 ); |
| 5350 | mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 ); |
Jerry Yu | c5aef88 | 2021-12-23 20:15:02 +0800 | [diff] [blame] | 5351 | |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5352 | if( ( ret = mbedtls_sha256_finish( &sha256, dst ) ) != 0 ) |
| 5353 | { |
| 5354 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha256_finish", ret ); |
| 5355 | goto exit; |
| 5356 | } |
| 5357 | |
| 5358 | *olen = 32; |
| 5359 | |
| 5360 | exit: |
| 5361 | |
| 5362 | mbedtls_sha256_free( &sha256 ); |
| 5363 | return( ret ); |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5364 | } |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5365 | #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] | 5366 | |
Jerry Yu | 000f976 | 2021-09-14 11:12:51 +0800 | [diff] [blame] | 5367 | int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl, |
| 5368 | const mbedtls_md_type_t md, |
| 5369 | unsigned char *dst, |
| 5370 | size_t dst_len, |
| 5371 | size_t *olen ) |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5372 | { |
Jerry Yu | c1ddeef | 2021-10-08 15:14:45 +0800 | [diff] [blame] | 5373 | switch( md ) |
| 5374 | { |
| 5375 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5376 | #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] | 5377 | case MBEDTLS_MD_SHA384: |
Jerry Yu | c5aef88 | 2021-12-23 20:15:02 +0800 | [diff] [blame] | 5378 | return( ssl_get_handshake_transcript_sha384( ssl, dst, dst_len, olen ) ); |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 5379 | #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] | 5380 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5381 | #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] | 5382 | case MBEDTLS_MD_SHA256: |
Jerry Yu | c5aef88 | 2021-12-23 20:15:02 +0800 | [diff] [blame] | 5383 | return( ssl_get_handshake_transcript_sha256( ssl, dst, dst_len, olen ) ); |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 5384 | #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] | 5385 | |
| 5386 | default: |
Andrzej Kurek | 409248a | 2022-10-24 10:33:21 -0400 | [diff] [blame] | 5387 | #if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \ |
| 5388 | !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
| 5389 | (void) ssl; |
| 5390 | (void) dst; |
| 5391 | (void) dst_len; |
| 5392 | (void) olen; |
| 5393 | #endif |
Jerry Yu | c1ddeef | 2021-10-08 15:14:45 +0800 | [diff] [blame] | 5394 | break; |
| 5395 | } |
Jerry Yu | c5aef88 | 2021-12-23 20:15:02 +0800 | [diff] [blame] | 5396 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5397 | } |
XiaokangQian | 647719a | 2021-12-07 09:16:29 +0000 | [diff] [blame] | 5398 | |
Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 5399 | #endif /* !MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5400 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5401 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 5402 | /* mbedtls_ssl_parse_sig_alg_ext() |
| 5403 | * |
| 5404 | * The `extension_data` field of signature algorithm contains a `SignatureSchemeList` |
| 5405 | * value (TLS 1.3 RFC8446): |
| 5406 | * enum { |
| 5407 | * .... |
| 5408 | * ecdsa_secp256r1_sha256( 0x0403 ), |
| 5409 | * ecdsa_secp384r1_sha384( 0x0503 ), |
| 5410 | * ecdsa_secp521r1_sha512( 0x0603 ), |
| 5411 | * .... |
| 5412 | * } SignatureScheme; |
| 5413 | * |
| 5414 | * struct { |
| 5415 | * SignatureScheme supported_signature_algorithms<2..2^16-2>; |
| 5416 | * } SignatureSchemeList; |
| 5417 | * |
| 5418 | * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm` |
| 5419 | * value (TLS 1.2 RFC5246): |
| 5420 | * enum { |
| 5421 | * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5), |
| 5422 | * sha512(6), (255) |
| 5423 | * } HashAlgorithm; |
| 5424 | * |
| 5425 | * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) } |
| 5426 | * SignatureAlgorithm; |
| 5427 | * |
| 5428 | * struct { |
| 5429 | * HashAlgorithm hash; |
| 5430 | * SignatureAlgorithm signature; |
| 5431 | * } SignatureAndHashAlgorithm; |
| 5432 | * |
| 5433 | * SignatureAndHashAlgorithm |
| 5434 | * supported_signature_algorithms<2..2^16-2>; |
| 5435 | * |
| 5436 | * The TLS 1.3 signature algorithm extension was defined to be a compatible |
| 5437 | * generalization of the TLS 1.2 signature algorithm extension. |
| 5438 | * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by |
| 5439 | * `SignatureScheme` field of TLS 1.3 |
| 5440 | * |
| 5441 | */ |
| 5442 | int mbedtls_ssl_parse_sig_alg_ext( mbedtls_ssl_context *ssl, |
| 5443 | const unsigned char *buf, |
| 5444 | const unsigned char *end ) |
| 5445 | { |
| 5446 | const unsigned char *p = buf; |
| 5447 | size_t supported_sig_algs_len = 0; |
| 5448 | const unsigned char *supported_sig_algs_end; |
| 5449 | uint16_t sig_alg; |
| 5450 | uint32_t common_idx = 0; |
| 5451 | |
| 5452 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 ); |
| 5453 | supported_sig_algs_len = MBEDTLS_GET_UINT16_BE( p, 0 ); |
| 5454 | p += 2; |
| 5455 | |
| 5456 | memset( ssl->handshake->received_sig_algs, 0, |
| 5457 | sizeof(ssl->handshake->received_sig_algs) ); |
| 5458 | |
| 5459 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, supported_sig_algs_len ); |
| 5460 | supported_sig_algs_end = p + supported_sig_algs_len; |
| 5461 | while( p < supported_sig_algs_end ) |
| 5462 | { |
| 5463 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, supported_sig_algs_end, 2 ); |
| 5464 | sig_alg = MBEDTLS_GET_UINT16_BE( p, 0 ); |
| 5465 | p += 2; |
Jerry Yu | f3b46b5 | 2022-06-19 16:52:27 +0800 | [diff] [blame] | 5466 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "received signature algorithm: 0x%x %s", |
| 5467 | sig_alg, |
| 5468 | mbedtls_ssl_sig_alg_to_str( sig_alg ) ) ); |
Jerry Yu | 2fe6c63 | 2022-06-29 10:02:38 +0800 | [diff] [blame] | 5469 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Jerry Yu | 52b7d92 | 2022-07-01 18:03:31 +0800 | [diff] [blame] | 5470 | if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 && |
Jerry Yu | 2fe6c63 | 2022-06-29 10:02:38 +0800 | [diff] [blame] | 5471 | ( ! ( mbedtls_ssl_sig_alg_is_supported( ssl, sig_alg ) && |
| 5472 | mbedtls_ssl_sig_alg_is_offered( ssl, sig_alg ) ) ) ) |
| 5473 | { |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 5474 | continue; |
Jerry Yu | 2fe6c63 | 2022-06-29 10:02:38 +0800 | [diff] [blame] | 5475 | } |
| 5476 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 5477 | |
Jerry Yu | f3b46b5 | 2022-06-19 16:52:27 +0800 | [diff] [blame] | 5478 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "valid signature algorithm: %s", |
| 5479 | mbedtls_ssl_sig_alg_to_str( sig_alg ) ) ); |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 5480 | |
| 5481 | if( common_idx + 1 < MBEDTLS_RECEIVED_SIG_ALGS_SIZE ) |
| 5482 | { |
| 5483 | ssl->handshake->received_sig_algs[common_idx] = sig_alg; |
| 5484 | common_idx += 1; |
| 5485 | } |
| 5486 | } |
| 5487 | /* Check that we consumed all the message. */ |
| 5488 | if( p != end ) |
| 5489 | { |
| 5490 | MBEDTLS_SSL_DEBUG_MSG( 1, |
| 5491 | ( "Signature algorithms extension length misaligned" ) ); |
| 5492 | MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR, |
| 5493 | MBEDTLS_ERR_SSL_DECODE_ERROR ); |
| 5494 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
| 5495 | } |
| 5496 | |
| 5497 | if( common_idx == 0 ) |
| 5498 | { |
| 5499 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "no signature algorithm in common" ) ); |
| 5500 | MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE, |
| 5501 | MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE ); |
| 5502 | return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE ); |
| 5503 | } |
| 5504 | |
Gabor Mezei | 15b95a6 | 2022-05-09 16:37:58 +0200 | [diff] [blame] | 5505 | ssl->handshake->received_sig_algs[common_idx] = MBEDTLS_TLS_SIG_NONE; |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 5506 | return( 0 ); |
| 5507 | } |
| 5508 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5509 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 5510 | |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5511 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 5512 | |
| 5513 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 5514 | |
| 5515 | static psa_status_t setup_psa_key_derivation( psa_key_derivation_operation_t* derivation, |
| 5516 | mbedtls_svc_key_id_t key, |
| 5517 | psa_algorithm_t alg, |
Neil Armstrong | 8ecd668 | 2022-05-05 11:40:35 +0200 | [diff] [blame] | 5518 | const unsigned char* raw_psk, size_t raw_psk_length, |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5519 | const unsigned char* seed, size_t seed_length, |
| 5520 | const unsigned char* label, size_t label_length, |
Przemek Stekiel | 51a1f36 | 2022-04-13 08:57:06 +0200 | [diff] [blame] | 5521 | const unsigned char* other_secret, |
| 5522 | size_t other_secret_length, |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5523 | size_t capacity ) |
| 5524 | { |
| 5525 | psa_status_t status; |
| 5526 | |
| 5527 | status = psa_key_derivation_setup( derivation, alg ); |
| 5528 | if( status != PSA_SUCCESS ) |
| 5529 | return( status ); |
| 5530 | |
| 5531 | if( PSA_ALG_IS_TLS12_PRF( alg ) || PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) ) |
| 5532 | { |
| 5533 | status = psa_key_derivation_input_bytes( derivation, |
| 5534 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 5535 | seed, seed_length ); |
| 5536 | if( status != PSA_SUCCESS ) |
| 5537 | return( status ); |
| 5538 | |
Przemek Stekiel | 51a1f36 | 2022-04-13 08:57:06 +0200 | [diff] [blame] | 5539 | if ( other_secret != NULL ) |
Przemek Stekiel | 1f02703 | 2022-04-05 17:12:11 +0200 | [diff] [blame] | 5540 | { |
| 5541 | status = psa_key_derivation_input_bytes( derivation, |
Przemek Stekiel | 51a1f36 | 2022-04-13 08:57:06 +0200 | [diff] [blame] | 5542 | PSA_KEY_DERIVATION_INPUT_OTHER_SECRET, |
| 5543 | other_secret, other_secret_length ); |
Przemek Stekiel | 1f02703 | 2022-04-05 17:12:11 +0200 | [diff] [blame] | 5544 | if( status != PSA_SUCCESS ) |
| 5545 | return( status ); |
| 5546 | } |
| 5547 | |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5548 | if( mbedtls_svc_key_id_is_null( key ) ) |
| 5549 | { |
| 5550 | status = psa_key_derivation_input_bytes( |
| 5551 | derivation, PSA_KEY_DERIVATION_INPUT_SECRET, |
Neil Armstrong | 8ecd668 | 2022-05-05 11:40:35 +0200 | [diff] [blame] | 5552 | raw_psk, raw_psk_length ); |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5553 | } |
| 5554 | else |
| 5555 | { |
| 5556 | status = psa_key_derivation_input_key( |
| 5557 | derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key ); |
| 5558 | } |
| 5559 | if( status != PSA_SUCCESS ) |
| 5560 | return( status ); |
| 5561 | |
| 5562 | status = psa_key_derivation_input_bytes( derivation, |
| 5563 | PSA_KEY_DERIVATION_INPUT_LABEL, |
| 5564 | label, label_length ); |
| 5565 | if( status != PSA_SUCCESS ) |
| 5566 | return( status ); |
| 5567 | } |
| 5568 | else |
| 5569 | { |
| 5570 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5571 | } |
| 5572 | |
| 5573 | status = psa_key_derivation_set_capacity( derivation, capacity ); |
| 5574 | if( status != PSA_SUCCESS ) |
| 5575 | return( status ); |
| 5576 | |
| 5577 | return( PSA_SUCCESS ); |
| 5578 | } |
| 5579 | |
Andrzej Kurek | 57d1063 | 2022-10-24 10:32:01 -0400 | [diff] [blame] | 5580 | #if defined(PSA_WANT_ALG_SHA_384) || \ |
| 5581 | defined(PSA_WANT_ALG_SHA_256) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 5582 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5583 | static int tls_prf_generic( mbedtls_md_type_t md_type, |
| 5584 | const unsigned char *secret, size_t slen, |
| 5585 | const char *label, |
| 5586 | const unsigned char *random, size_t rlen, |
| 5587 | unsigned char *dstbuf, size_t dlen ) |
| 5588 | { |
| 5589 | psa_status_t status; |
| 5590 | psa_algorithm_t alg; |
| 5591 | mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5592 | psa_key_derivation_operation_t derivation = |
| 5593 | PSA_KEY_DERIVATION_OPERATION_INIT; |
| 5594 | |
| 5595 | if( md_type == MBEDTLS_MD_SHA384 ) |
| 5596 | alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384); |
| 5597 | else |
| 5598 | alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256); |
| 5599 | |
| 5600 | /* Normally a "secret" should be long enough to be impossible to |
| 5601 | * find by brute force, and in particular should not be empty. But |
| 5602 | * this PRF is also used to derive an IV, in particular in EAP-TLS, |
| 5603 | * and for this use case it makes sense to have a 0-length "secret". |
| 5604 | * Since the key API doesn't allow importing a key of length 0, |
| 5605 | * keep master_key=0, which setup_psa_key_derivation() understands |
| 5606 | * to mean a 0-length "secret" input. */ |
| 5607 | if( slen != 0 ) |
| 5608 | { |
| 5609 | psa_key_attributes_t key_attributes = psa_key_attributes_init(); |
| 5610 | psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE ); |
| 5611 | psa_set_key_algorithm( &key_attributes, alg ); |
| 5612 | psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE ); |
| 5613 | |
| 5614 | status = psa_import_key( &key_attributes, secret, slen, &master_key ); |
| 5615 | if( status != PSA_SUCCESS ) |
| 5616 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 5617 | } |
| 5618 | |
| 5619 | status = setup_psa_key_derivation( &derivation, |
| 5620 | master_key, alg, |
Neil Armstrong | 8ecd668 | 2022-05-05 11:40:35 +0200 | [diff] [blame] | 5621 | NULL, 0, |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5622 | random, rlen, |
| 5623 | (unsigned char const *) label, |
| 5624 | (size_t) strlen( label ), |
Przemek Stekiel | 1f02703 | 2022-04-05 17:12:11 +0200 | [diff] [blame] | 5625 | NULL, 0, |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5626 | dlen ); |
| 5627 | if( status != PSA_SUCCESS ) |
| 5628 | { |
| 5629 | psa_key_derivation_abort( &derivation ); |
| 5630 | psa_destroy_key( master_key ); |
| 5631 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 5632 | } |
| 5633 | |
| 5634 | status = psa_key_derivation_output_bytes( &derivation, dstbuf, dlen ); |
| 5635 | if( status != PSA_SUCCESS ) |
| 5636 | { |
| 5637 | psa_key_derivation_abort( &derivation ); |
| 5638 | psa_destroy_key( master_key ); |
| 5639 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 5640 | } |
| 5641 | |
| 5642 | status = psa_key_derivation_abort( &derivation ); |
| 5643 | if( status != PSA_SUCCESS ) |
| 5644 | { |
| 5645 | psa_destroy_key( master_key ); |
| 5646 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 5647 | } |
| 5648 | |
| 5649 | if( ! mbedtls_svc_key_id_is_null( master_key ) ) |
| 5650 | status = psa_destroy_key( master_key ); |
| 5651 | if( status != PSA_SUCCESS ) |
| 5652 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 5653 | |
| 5654 | return( 0 ); |
| 5655 | } |
Andrzej Kurek | 57d1063 | 2022-10-24 10:32:01 -0400 | [diff] [blame] | 5656 | #endif /* PSA_WANT_ALG_SHA_256 || PSA_WANT_ALG_SHA_384 */ |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5657 | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
| 5658 | |
Andrzej Kurek | 57d1063 | 2022-10-24 10:32:01 -0400 | [diff] [blame] | 5659 | #if defined(MBEDTLS_MD_C) && \ |
| 5660 | ( defined(MBEDTLS_SHA256_C) || \ |
| 5661 | defined(MBEDTLS_SHA384_C) ) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 5662 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5663 | static int tls_prf_generic( mbedtls_md_type_t md_type, |
| 5664 | const unsigned char *secret, size_t slen, |
| 5665 | const char *label, |
| 5666 | const unsigned char *random, size_t rlen, |
| 5667 | unsigned char *dstbuf, size_t dlen ) |
| 5668 | { |
| 5669 | size_t nb; |
| 5670 | size_t i, j, k, md_len; |
| 5671 | unsigned char *tmp; |
| 5672 | size_t tmp_len = 0; |
| 5673 | unsigned char h_i[MBEDTLS_MD_MAX_SIZE]; |
| 5674 | const mbedtls_md_info_t *md_info; |
| 5675 | mbedtls_md_context_t md_ctx; |
| 5676 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 5677 | |
| 5678 | mbedtls_md_init( &md_ctx ); |
| 5679 | |
| 5680 | if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL ) |
| 5681 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 5682 | |
| 5683 | md_len = mbedtls_md_get_size( md_info ); |
| 5684 | |
| 5685 | tmp_len = md_len + strlen( label ) + rlen; |
| 5686 | tmp = mbedtls_calloc( 1, tmp_len ); |
| 5687 | if( tmp == NULL ) |
| 5688 | { |
| 5689 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 5690 | goto exit; |
| 5691 | } |
| 5692 | |
| 5693 | nb = strlen( label ); |
| 5694 | memcpy( tmp + md_len, label, nb ); |
| 5695 | memcpy( tmp + md_len + nb, random, rlen ); |
| 5696 | nb += rlen; |
| 5697 | |
| 5698 | /* |
| 5699 | * Compute P_<hash>(secret, label + random)[0..dlen] |
| 5700 | */ |
| 5701 | if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 ) |
| 5702 | goto exit; |
| 5703 | |
| 5704 | ret = mbedtls_md_hmac_starts( &md_ctx, secret, slen ); |
| 5705 | if( ret != 0 ) |
| 5706 | goto exit; |
| 5707 | ret = mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb ); |
| 5708 | if( ret != 0 ) |
| 5709 | goto exit; |
| 5710 | ret = mbedtls_md_hmac_finish( &md_ctx, tmp ); |
| 5711 | if( ret != 0 ) |
| 5712 | goto exit; |
| 5713 | |
| 5714 | for( i = 0; i < dlen; i += md_len ) |
| 5715 | { |
| 5716 | ret = mbedtls_md_hmac_reset ( &md_ctx ); |
| 5717 | if( ret != 0 ) |
| 5718 | goto exit; |
| 5719 | ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb ); |
| 5720 | if( ret != 0 ) |
| 5721 | goto exit; |
| 5722 | ret = mbedtls_md_hmac_finish( &md_ctx, h_i ); |
| 5723 | if( ret != 0 ) |
| 5724 | goto exit; |
| 5725 | |
| 5726 | ret = mbedtls_md_hmac_reset ( &md_ctx ); |
| 5727 | if( ret != 0 ) |
| 5728 | goto exit; |
| 5729 | ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len ); |
| 5730 | if( ret != 0 ) |
| 5731 | goto exit; |
| 5732 | ret = mbedtls_md_hmac_finish( &md_ctx, tmp ); |
| 5733 | if( ret != 0 ) |
| 5734 | goto exit; |
| 5735 | |
| 5736 | k = ( i + md_len > dlen ) ? dlen % md_len : md_len; |
| 5737 | |
| 5738 | for( j = 0; j < k; j++ ) |
| 5739 | dstbuf[i + j] = h_i[j]; |
| 5740 | } |
| 5741 | |
| 5742 | exit: |
| 5743 | mbedtls_md_free( &md_ctx ); |
| 5744 | |
Dave Rodgman | 29b9b2b | 2022-11-01 16:08:14 +0000 | [diff] [blame] | 5745 | if ( tmp != NULL ) |
| 5746 | mbedtls_platform_zeroize( tmp, tmp_len ); |
| 5747 | |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5748 | mbedtls_platform_zeroize( h_i, sizeof( h_i ) ); |
| 5749 | |
| 5750 | mbedtls_free( tmp ); |
| 5751 | |
| 5752 | return( ret ); |
| 5753 | } |
Andrzej Kurek | 57d1063 | 2022-10-24 10:32:01 -0400 | [diff] [blame] | 5754 | #endif /* MBEDTLS_MD_C && ( MBEDTLS_SHA256_C || MBEDTLS_SHA384_C ) */ |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5755 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 5756 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5757 | #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] | 5758 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5759 | static int tls_prf_sha256( const unsigned char *secret, size_t slen, |
| 5760 | const char *label, |
| 5761 | const unsigned char *random, size_t rlen, |
| 5762 | unsigned char *dstbuf, size_t dlen ) |
| 5763 | { |
| 5764 | return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen, |
| 5765 | label, random, rlen, dstbuf, dlen ) ); |
| 5766 | } |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 5767 | #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] | 5768 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5769 | #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] | 5770 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5771 | static int tls_prf_sha384( const unsigned char *secret, size_t slen, |
| 5772 | const char *label, |
| 5773 | const unsigned char *random, size_t rlen, |
| 5774 | unsigned char *dstbuf, size_t dlen ) |
| 5775 | { |
| 5776 | return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen, |
| 5777 | label, random, rlen, dstbuf, dlen ) ); |
| 5778 | } |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 5779 | #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] | 5780 | |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5781 | /* |
| 5782 | * Set appropriate PRF function and other SSL / TLS1.2 functions |
| 5783 | * |
| 5784 | * Inputs: |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5785 | * - hash associated with the ciphersuite (only used by TLS 1.2) |
| 5786 | * |
| 5787 | * Outputs: |
| 5788 | * - the tls_prf, calc_verify and calc_finished members of handshake structure |
| 5789 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 5790 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5791 | static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake, |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5792 | mbedtls_md_type_t hash ) |
| 5793 | { |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5794 | #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] | 5795 | if( hash == MBEDTLS_MD_SHA384 ) |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5796 | { |
| 5797 | handshake->tls_prf = tls_prf_sha384; |
| 5798 | handshake->calc_verify = ssl_calc_verify_tls_sha384; |
| 5799 | handshake->calc_finished = ssl_calc_finished_tls_sha384; |
| 5800 | } |
| 5801 | else |
| 5802 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5803 | #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] | 5804 | { |
Ronald Cron | 81591aa | 2022-03-07 09:05:51 +0100 | [diff] [blame] | 5805 | (void) hash; |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5806 | handshake->tls_prf = tls_prf_sha256; |
| 5807 | handshake->calc_verify = ssl_calc_verify_tls_sha256; |
| 5808 | handshake->calc_finished = ssl_calc_finished_tls_sha256; |
| 5809 | } |
Ronald Cron | 81591aa | 2022-03-07 09:05:51 +0100 | [diff] [blame] | 5810 | #else |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5811 | { |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5812 | (void) handshake; |
Ronald Cron | 81591aa | 2022-03-07 09:05:51 +0100 | [diff] [blame] | 5813 | (void) hash; |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5814 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 5815 | } |
Ronald Cron | 81591aa | 2022-03-07 09:05:51 +0100 | [diff] [blame] | 5816 | #endif |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5817 | |
| 5818 | return( 0 ); |
| 5819 | } |
Jerry Yu | d6ab235 | 2022-02-17 14:03:43 +0800 | [diff] [blame] | 5820 | |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5821 | /* |
| 5822 | * Compute master secret if needed |
| 5823 | * |
| 5824 | * Parameters: |
| 5825 | * [in/out] handshake |
| 5826 | * [in] resume, premaster, extended_ms, calc_verify, tls_prf |
| 5827 | * (PSA-PSK) ciphersuite_info, psk_opaque |
| 5828 | * [out] premaster (cleared) |
| 5829 | * [out] master |
| 5830 | * [in] ssl: optionally used for debugging, EMS and PSA-PSK |
| 5831 | * debug: conf->f_dbg, conf->p_dbg |
| 5832 | * EMS: passed to calc_verify (debug + session_negotiate) |
Ronald Cron | a25cf58 | 2022-03-07 11:10:36 +0100 | [diff] [blame] | 5833 | * PSA-PSA: conf |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5834 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 5835 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5836 | static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake, |
| 5837 | unsigned char *master, |
| 5838 | const mbedtls_ssl_context *ssl ) |
| 5839 | { |
| 5840 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 5841 | |
| 5842 | /* cf. RFC 5246, Section 8.1: |
| 5843 | * "The master secret is always exactly 48 bytes in length." */ |
| 5844 | size_t const master_secret_len = 48; |
| 5845 | |
| 5846 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 5847 | unsigned char session_hash[48]; |
| 5848 | #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */ |
| 5849 | |
| 5850 | /* The label for the KDF used for key expansion. |
| 5851 | * This is either "master secret" or "extended master secret" |
| 5852 | * depending on whether the Extended Master Secret extension |
| 5853 | * is used. */ |
| 5854 | char const *lbl = "master secret"; |
| 5855 | |
Przemek Stekiel | ae4ed30 | 2022-04-05 17:15:55 +0200 | [diff] [blame] | 5856 | /* The seed for the KDF used for key expansion. |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5857 | * - If the Extended Master Secret extension is not used, |
| 5858 | * this is ClientHello.Random + ServerHello.Random |
| 5859 | * (see Sect. 8.1 in RFC 5246). |
| 5860 | * - If the Extended Master Secret extension is used, |
| 5861 | * this is the transcript of the handshake so far. |
| 5862 | * (see Sect. 4 in RFC 7627). */ |
Przemek Stekiel | ae4ed30 | 2022-04-05 17:15:55 +0200 | [diff] [blame] | 5863 | unsigned char const *seed = handshake->randbytes; |
| 5864 | size_t seed_len = 64; |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5865 | |
| 5866 | #if !defined(MBEDTLS_DEBUG_C) && \ |
| 5867 | !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \ |
| 5868 | !(defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
| 5869 | defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)) |
| 5870 | ssl = NULL; /* make sure we don't use it except for those cases */ |
| 5871 | (void) ssl; |
| 5872 | #endif |
| 5873 | |
| 5874 | if( handshake->resume != 0 ) |
| 5875 | { |
| 5876 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) ); |
| 5877 | return( 0 ); |
| 5878 | } |
| 5879 | |
| 5880 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 5881 | if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED ) |
| 5882 | { |
| 5883 | lbl = "extended master secret"; |
Przemek Stekiel | ae4ed30 | 2022-04-05 17:15:55 +0200 | [diff] [blame] | 5884 | seed = session_hash; |
| 5885 | handshake->calc_verify( ssl, session_hash, &seed_len ); |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5886 | |
| 5887 | MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret", |
Przemek Stekiel | ae4ed30 | 2022-04-05 17:15:55 +0200 | [diff] [blame] | 5888 | session_hash, seed_len ); |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5889 | } |
Przemek Stekiel | 169bf0b | 2022-04-29 07:53:29 +0200 | [diff] [blame] | 5890 | #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */ |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5891 | |
Przemek Stekiel | 99114f3 | 2022-04-22 11:20:09 +0200 | [diff] [blame] | 5892 | #if defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
Przemek Stekiel | 8a4b7fd | 2022-04-28 09:22:22 +0200 | [diff] [blame] | 5893 | defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Neil Armstrong | e952a30 | 2022-05-03 10:22:14 +0200 | [diff] [blame] | 5894 | if( mbedtls_ssl_ciphersuite_uses_psk( handshake->ciphersuite_info ) == 1 ) |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5895 | { |
| 5896 | /* Perform PSK-to-MS expansion in a single step. */ |
| 5897 | psa_status_t status; |
| 5898 | psa_algorithm_t alg; |
| 5899 | mbedtls_svc_key_id_t psk; |
| 5900 | psa_key_derivation_operation_t derivation = |
| 5901 | PSA_KEY_DERIVATION_OPERATION_INIT; |
| 5902 | mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac; |
| 5903 | |
| 5904 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) ); |
| 5905 | |
| 5906 | psk = mbedtls_ssl_get_opaque_psk( ssl ); |
| 5907 | |
| 5908 | if( hash_alg == MBEDTLS_MD_SHA384 ) |
| 5909 | alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384); |
| 5910 | else |
| 5911 | alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256); |
| 5912 | |
Przemek Stekiel | 51a1f36 | 2022-04-13 08:57:06 +0200 | [diff] [blame] | 5913 | size_t other_secret_len = 0; |
| 5914 | unsigned char* other_secret = NULL; |
Przemek Stekiel | c203340 | 2022-04-05 17:19:41 +0200 | [diff] [blame] | 5915 | |
Przemek Stekiel | 19b80f8 | 2022-04-14 08:29:31 +0200 | [diff] [blame] | 5916 | switch( handshake->ciphersuite_info->key_exchange ) |
Przemek Stekiel | c203340 | 2022-04-05 17:19:41 +0200 | [diff] [blame] | 5917 | { |
Przemek Stekiel | 19b80f8 | 2022-04-14 08:29:31 +0200 | [diff] [blame] | 5918 | /* Provide other secret. |
Przemek Stekiel | 51a1f36 | 2022-04-13 08:57:06 +0200 | [diff] [blame] | 5919 | * Other secret is stored in premaster, where first 2 bytes hold the |
Przemek Stekiel | 19b80f8 | 2022-04-14 08:29:31 +0200 | [diff] [blame] | 5920 | * length of the other key. |
Przemek Stekiel | c203340 | 2022-04-05 17:19:41 +0200 | [diff] [blame] | 5921 | */ |
Przemek Stekiel | 19b80f8 | 2022-04-14 08:29:31 +0200 | [diff] [blame] | 5922 | case MBEDTLS_KEY_EXCHANGE_RSA_PSK: |
Przemek Stekiel | 8abcee9 | 2022-04-28 09:16:28 +0200 | [diff] [blame] | 5923 | /* For RSA-PSK other key length is always 48 bytes. */ |
Przemek Stekiel | 19b80f8 | 2022-04-14 08:29:31 +0200 | [diff] [blame] | 5924 | other_secret_len = 48; |
| 5925 | other_secret = handshake->premaster + 2; |
| 5926 | break; |
| 5927 | case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK: |
Przemek Stekiel | b293aaa | 2022-04-19 12:22:38 +0200 | [diff] [blame] | 5928 | case MBEDTLS_KEY_EXCHANGE_DHE_PSK: |
Przemek Stekiel | 19b80f8 | 2022-04-14 08:29:31 +0200 | [diff] [blame] | 5929 | other_secret_len = MBEDTLS_GET_UINT16_BE(handshake->premaster, 0); |
| 5930 | other_secret = handshake->premaster + 2; |
| 5931 | break; |
| 5932 | default: |
| 5933 | break; |
Przemek Stekiel | c203340 | 2022-04-05 17:19:41 +0200 | [diff] [blame] | 5934 | } |
| 5935 | |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5936 | status = setup_psa_key_derivation( &derivation, psk, alg, |
Neil Armstrong | 8ecd668 | 2022-05-05 11:40:35 +0200 | [diff] [blame] | 5937 | ssl->conf->psk, ssl->conf->psk_len, |
Przemek Stekiel | ae4ed30 | 2022-04-05 17:15:55 +0200 | [diff] [blame] | 5938 | seed, seed_len, |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5939 | (unsigned char const *) lbl, |
| 5940 | (size_t) strlen( lbl ), |
Przemek Stekiel | 51a1f36 | 2022-04-13 08:57:06 +0200 | [diff] [blame] | 5941 | other_secret, other_secret_len, |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5942 | master_secret_len ); |
| 5943 | if( status != PSA_SUCCESS ) |
| 5944 | { |
| 5945 | psa_key_derivation_abort( &derivation ); |
| 5946 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 5947 | } |
| 5948 | |
| 5949 | status = psa_key_derivation_output_bytes( &derivation, |
| 5950 | master, |
| 5951 | master_secret_len ); |
| 5952 | if( status != PSA_SUCCESS ) |
| 5953 | { |
| 5954 | psa_key_derivation_abort( &derivation ); |
| 5955 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 5956 | } |
| 5957 | |
| 5958 | status = psa_key_derivation_abort( &derivation ); |
| 5959 | if( status != PSA_SUCCESS ) |
| 5960 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 5961 | } |
| 5962 | else |
| 5963 | #endif |
| 5964 | { |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 5965 | #if defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
| 5966 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
| 5967 | if( handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE ) |
| 5968 | { |
| 5969 | psa_status_t status; |
| 5970 | psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS; |
| 5971 | psa_key_derivation_operation_t derivation = |
| 5972 | PSA_KEY_DERIVATION_OPERATION_INIT; |
| 5973 | |
| 5974 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PMS KDF for ECJPAKE" ) ); |
| 5975 | |
| 5976 | handshake->pmslen = PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE; |
| 5977 | |
| 5978 | status = psa_key_derivation_setup( &derivation, alg ); |
| 5979 | if( status != PSA_SUCCESS ) |
| 5980 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 5981 | |
| 5982 | status = psa_key_derivation_set_capacity( &derivation, |
| 5983 | PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE ); |
| 5984 | if( status != PSA_SUCCESS ) |
| 5985 | { |
| 5986 | psa_key_derivation_abort( &derivation ); |
| 5987 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 5988 | } |
| 5989 | |
| 5990 | status = psa_pake_get_implicit_key( &handshake->psa_pake_ctx, |
| 5991 | &derivation ); |
| 5992 | if( status != PSA_SUCCESS ) |
| 5993 | { |
| 5994 | psa_key_derivation_abort( &derivation ); |
| 5995 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 5996 | } |
| 5997 | |
| 5998 | status = psa_key_derivation_output_bytes( &derivation, |
| 5999 | handshake->premaster, |
| 6000 | handshake->pmslen ); |
| 6001 | if( status != PSA_SUCCESS ) |
| 6002 | { |
| 6003 | psa_key_derivation_abort( &derivation ); |
| 6004 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 6005 | } |
| 6006 | |
| 6007 | status = psa_key_derivation_abort( &derivation ); |
| 6008 | if( status != PSA_SUCCESS ) |
| 6009 | { |
| 6010 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 6011 | } |
| 6012 | } |
| 6013 | #endif |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6014 | ret = handshake->tls_prf( handshake->premaster, handshake->pmslen, |
Przemek Stekiel | ae4ed30 | 2022-04-05 17:15:55 +0200 | [diff] [blame] | 6015 | lbl, seed, seed_len, |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6016 | master, |
| 6017 | master_secret_len ); |
| 6018 | if( ret != 0 ) |
| 6019 | { |
| 6020 | MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret ); |
| 6021 | return( ret ); |
| 6022 | } |
| 6023 | |
| 6024 | MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", |
| 6025 | handshake->premaster, |
| 6026 | handshake->pmslen ); |
| 6027 | |
| 6028 | mbedtls_platform_zeroize( handshake->premaster, |
| 6029 | sizeof(handshake->premaster) ); |
| 6030 | } |
| 6031 | |
| 6032 | return( 0 ); |
| 6033 | } |
| 6034 | |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 6035 | int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl ) |
| 6036 | { |
| 6037 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 6038 | const mbedtls_ssl_ciphersuite_t * const ciphersuite_info = |
| 6039 | ssl->handshake->ciphersuite_info; |
| 6040 | |
| 6041 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) ); |
| 6042 | |
| 6043 | /* Set PRF, calc_verify and calc_finished function pointers */ |
| 6044 | ret = ssl_set_handshake_prfs( ssl->handshake, |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 6045 | ciphersuite_info->mac ); |
| 6046 | if( ret != 0 ) |
| 6047 | { |
| 6048 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret ); |
| 6049 | return( ret ); |
| 6050 | } |
| 6051 | |
| 6052 | /* Compute master secret if needed */ |
| 6053 | ret = ssl_compute_master( ssl->handshake, |
| 6054 | ssl->session_negotiate->master, |
| 6055 | ssl ); |
| 6056 | if( ret != 0 ) |
| 6057 | { |
| 6058 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret ); |
| 6059 | return( ret ); |
| 6060 | } |
| 6061 | |
| 6062 | /* Swap the client and server random values: |
| 6063 | * - MS derivation wanted client+server (RFC 5246 8.1) |
| 6064 | * - key derivation wants server+client (RFC 5246 6.3) */ |
| 6065 | { |
| 6066 | unsigned char tmp[64]; |
| 6067 | memcpy( tmp, ssl->handshake->randbytes, 64 ); |
| 6068 | memcpy( ssl->handshake->randbytes, tmp + 32, 32 ); |
| 6069 | memcpy( ssl->handshake->randbytes + 32, tmp, 32 ); |
| 6070 | mbedtls_platform_zeroize( tmp, sizeof( tmp ) ); |
| 6071 | } |
| 6072 | |
| 6073 | /* Populate transform structure */ |
| 6074 | ret = ssl_tls12_populate_transform( ssl->transform_negotiate, |
| 6075 | ssl->session_negotiate->ciphersuite, |
| 6076 | ssl->session_negotiate->master, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 6077 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 6078 | ssl->session_negotiate->encrypt_then_mac, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 6079 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 6080 | ssl->handshake->tls_prf, |
| 6081 | ssl->handshake->randbytes, |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 6082 | ssl->tls_version, |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 6083 | ssl->conf->endpoint, |
| 6084 | ssl ); |
| 6085 | if( ret != 0 ) |
| 6086 | { |
| 6087 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls12_populate_transform", ret ); |
| 6088 | return( ret ); |
| 6089 | } |
| 6090 | |
| 6091 | /* We no longer need Server/ClientHello.random values */ |
| 6092 | mbedtls_platform_zeroize( ssl->handshake->randbytes, |
| 6093 | sizeof( ssl->handshake->randbytes ) ); |
| 6094 | |
| 6095 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) ); |
| 6096 | |
| 6097 | return( 0 ); |
| 6098 | } |
Jerry Yu | 8392e0d | 2022-02-17 14:10:24 +0800 | [diff] [blame] | 6099 | |
Ronald Cron | 4dcbca9 | 2022-03-07 10:21:40 +0100 | [diff] [blame] | 6100 | int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md ) |
| 6101 | { |
Ronald Cron | 4dcbca9 | 2022-03-07 10:21:40 +0100 | [diff] [blame] | 6102 | switch( md ) |
| 6103 | { |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 6104 | #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] | 6105 | case MBEDTLS_SSL_HASH_SHA384: |
| 6106 | ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384; |
| 6107 | break; |
| 6108 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 6109 | #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] | 6110 | case MBEDTLS_SSL_HASH_SHA256: |
| 6111 | ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256; |
| 6112 | break; |
| 6113 | #endif |
| 6114 | default: |
| 6115 | return( -1 ); |
| 6116 | } |
Andrzej Kurek | eabeb30 | 2022-10-17 07:52:51 -0400 | [diff] [blame] | 6117 | #if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \ |
| 6118 | !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
| 6119 | (void) ssl; |
| 6120 | #endif |
Ronald Cron | c2f13a0 | 2022-03-07 10:25:24 +0100 | [diff] [blame] | 6121 | return( 0 ); |
Ronald Cron | 4dcbca9 | 2022-03-07 10:21:40 +0100 | [diff] [blame] | 6122 | } |
| 6123 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 6124 | #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] | 6125 | void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl, |
| 6126 | unsigned char *hash, |
| 6127 | size_t *hlen ) |
| 6128 | { |
| 6129 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 6130 | size_t hash_size; |
| 6131 | psa_status_t status; |
| 6132 | psa_hash_operation_t sha256_psa = psa_hash_operation_init(); |
| 6133 | |
| 6134 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) ); |
| 6135 | status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa ); |
| 6136 | if( status != PSA_SUCCESS ) |
| 6137 | { |
| 6138 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); |
| 6139 | return; |
| 6140 | } |
| 6141 | |
| 6142 | status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size ); |
| 6143 | if( status != PSA_SUCCESS ) |
| 6144 | { |
| 6145 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); |
| 6146 | return; |
| 6147 | } |
| 6148 | |
| 6149 | *hlen = 32; |
| 6150 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen ); |
| 6151 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) ); |
| 6152 | #else |
| 6153 | mbedtls_sha256_context sha256; |
| 6154 | |
| 6155 | mbedtls_sha256_init( &sha256 ); |
| 6156 | |
| 6157 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) ); |
| 6158 | |
| 6159 | mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 ); |
| 6160 | mbedtls_sha256_finish( &sha256, hash ); |
| 6161 | |
| 6162 | *hlen = 32; |
| 6163 | |
| 6164 | MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen ); |
| 6165 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) ); |
| 6166 | |
| 6167 | mbedtls_sha256_free( &sha256 ); |
| 6168 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 6169 | return; |
| 6170 | } |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 6171 | #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] | 6172 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 6173 | #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] | 6174 | void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl, |
| 6175 | unsigned char *hash, |
| 6176 | size_t *hlen ) |
| 6177 | { |
| 6178 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 6179 | size_t hash_size; |
| 6180 | psa_status_t status; |
| 6181 | psa_hash_operation_t sha384_psa = psa_hash_operation_init(); |
| 6182 | |
| 6183 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) ); |
| 6184 | status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa ); |
| 6185 | if( status != PSA_SUCCESS ) |
| 6186 | { |
| 6187 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); |
| 6188 | return; |
| 6189 | } |
| 6190 | |
| 6191 | status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size ); |
| 6192 | if( status != PSA_SUCCESS ) |
| 6193 | { |
| 6194 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); |
| 6195 | return; |
| 6196 | } |
| 6197 | |
| 6198 | *hlen = 48; |
| 6199 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen ); |
| 6200 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) ); |
| 6201 | #else |
| 6202 | mbedtls_sha512_context sha512; |
| 6203 | |
| 6204 | mbedtls_sha512_init( &sha512 ); |
| 6205 | |
| 6206 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) ); |
| 6207 | |
Andrzej Kurek | a242e83 | 2022-08-11 10:03:14 -0400 | [diff] [blame] | 6208 | mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 ); |
Jerry Yu | c1cb384 | 2022-02-17 14:13:48 +0800 | [diff] [blame] | 6209 | mbedtls_sha512_finish( &sha512, hash ); |
| 6210 | |
| 6211 | *hlen = 48; |
| 6212 | |
| 6213 | MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen ); |
| 6214 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) ); |
| 6215 | |
| 6216 | mbedtls_sha512_free( &sha512 ); |
| 6217 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 6218 | return; |
| 6219 | } |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 6220 | #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] | 6221 | |
Neil Armstrong | 80f6f32 | 2022-05-03 17:56:38 +0200 | [diff] [blame] | 6222 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
| 6223 | defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6224 | int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex ) |
| 6225 | { |
| 6226 | unsigned char *p = ssl->handshake->premaster; |
| 6227 | unsigned char *end = p + sizeof( ssl->handshake->premaster ); |
| 6228 | const unsigned char *psk = NULL; |
| 6229 | size_t psk_len = 0; |
Przemek Stekiel | b293aaa | 2022-04-19 12:22:38 +0200 | [diff] [blame] | 6230 | int psk_ret = mbedtls_ssl_get_psk( ssl, &psk, &psk_len ); |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6231 | |
Przemek Stekiel | b293aaa | 2022-04-19 12:22:38 +0200 | [diff] [blame] | 6232 | if( psk_ret == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED ) |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6233 | { |
| 6234 | /* |
| 6235 | * This should never happen because the existence of a PSK is always |
Przemek Stekiel | b293aaa | 2022-04-19 12:22:38 +0200 | [diff] [blame] | 6236 | * checked before calling this function. |
| 6237 | * |
| 6238 | * The exception is opaque DHE-PSK. For DHE-PSK fill premaster with |
Przemek Stekiel | 8abcee9 | 2022-04-28 09:16:28 +0200 | [diff] [blame] | 6239 | * the shared secret without PSK. |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6240 | */ |
Przemek Stekiel | b293aaa | 2022-04-19 12:22:38 +0200 | [diff] [blame] | 6241 | if ( key_ex != MBEDTLS_KEY_EXCHANGE_DHE_PSK ) |
| 6242 | { |
| 6243 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 6244 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 6245 | } |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6246 | } |
| 6247 | |
| 6248 | /* |
| 6249 | * PMS = struct { |
| 6250 | * opaque other_secret<0..2^16-1>; |
| 6251 | * opaque psk<0..2^16-1>; |
| 6252 | * }; |
| 6253 | * with "other_secret" depending on the particular key exchange |
| 6254 | */ |
| 6255 | #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) |
| 6256 | if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK ) |
| 6257 | { |
| 6258 | if( end - p < 2 ) |
| 6259 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6260 | |
| 6261 | MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 ); |
| 6262 | p += 2; |
| 6263 | |
| 6264 | if( end < p || (size_t)( end - p ) < psk_len ) |
| 6265 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6266 | |
| 6267 | memset( p, 0, psk_len ); |
| 6268 | p += psk_len; |
| 6269 | } |
| 6270 | else |
| 6271 | #endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */ |
| 6272 | #if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) |
| 6273 | if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK ) |
| 6274 | { |
| 6275 | /* |
| 6276 | * other_secret already set by the ClientKeyExchange message, |
| 6277 | * and is 48 bytes long |
| 6278 | */ |
| 6279 | if( end - p < 2 ) |
| 6280 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6281 | |
| 6282 | *p++ = 0; |
| 6283 | *p++ = 48; |
| 6284 | p += 48; |
| 6285 | } |
| 6286 | else |
| 6287 | #endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */ |
| 6288 | #if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) |
| 6289 | if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK ) |
| 6290 | { |
| 6291 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 6292 | size_t len; |
| 6293 | |
| 6294 | /* Write length only when we know the actual value */ |
| 6295 | if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx, |
| 6296 | p + 2, end - ( p + 2 ), &len, |
| 6297 | ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 ) |
| 6298 | { |
| 6299 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret ); |
| 6300 | return( ret ); |
| 6301 | } |
| 6302 | MBEDTLS_PUT_UINT16_BE( len, p, 0 ); |
| 6303 | p += 2 + len; |
| 6304 | |
| 6305 | MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K ); |
| 6306 | } |
| 6307 | else |
| 6308 | #endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */ |
Neil Armstrong | 80f6f32 | 2022-05-03 17:56:38 +0200 | [diff] [blame] | 6309 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6310 | if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ) |
| 6311 | { |
| 6312 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 6313 | size_t zlen; |
| 6314 | |
| 6315 | if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen, |
| 6316 | p + 2, end - ( p + 2 ), |
| 6317 | ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 ) |
| 6318 | { |
| 6319 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret ); |
| 6320 | return( ret ); |
| 6321 | } |
| 6322 | |
| 6323 | MBEDTLS_PUT_UINT16_BE( zlen, p, 0 ); |
| 6324 | p += 2 + zlen; |
| 6325 | |
| 6326 | MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx, |
| 6327 | MBEDTLS_DEBUG_ECDH_Z ); |
| 6328 | } |
| 6329 | else |
Neil Armstrong | 80f6f32 | 2022-05-03 17:56:38 +0200 | [diff] [blame] | 6330 | #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */ |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6331 | { |
| 6332 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 6333 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 6334 | } |
| 6335 | |
| 6336 | /* opaque psk<0..2^16-1>; */ |
| 6337 | if( end - p < 2 ) |
| 6338 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6339 | |
| 6340 | MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 ); |
| 6341 | p += 2; |
| 6342 | |
| 6343 | if( end < p || (size_t)( end - p ) < psk_len ) |
| 6344 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6345 | |
| 6346 | memcpy( p, psk, psk_len ); |
| 6347 | p += psk_len; |
| 6348 | |
| 6349 | ssl->handshake->pmslen = p - ssl->handshake->premaster; |
| 6350 | |
| 6351 | return( 0 ); |
| 6352 | } |
Neil Armstrong | 80f6f32 | 2022-05-03 17:56:38 +0200 | [diff] [blame] | 6353 | #endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ |
Jerry Yu | c2c673d | 2022-02-17 14:20:39 +0800 | [diff] [blame] | 6354 | |
| 6355 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6356 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | c2c673d | 2022-02-17 14:20:39 +0800 | [diff] [blame] | 6357 | static int ssl_write_hello_request( mbedtls_ssl_context *ssl ); |
| 6358 | |
| 6359 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 6360 | int mbedtls_ssl_resend_hello_request( mbedtls_ssl_context *ssl ) |
| 6361 | { |
| 6362 | /* If renegotiation is not enforced, retransmit until we would reach max |
| 6363 | * timeout if we were using the usual handshake doubling scheme */ |
| 6364 | if( ssl->conf->renego_max_records < 0 ) |
| 6365 | { |
| 6366 | uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1; |
| 6367 | unsigned char doublings = 1; |
| 6368 | |
| 6369 | while( ratio != 0 ) |
| 6370 | { |
| 6371 | ++doublings; |
| 6372 | ratio >>= 1; |
| 6373 | } |
| 6374 | |
| 6375 | if( ++ssl->renego_records_seen > doublings ) |
| 6376 | { |
| 6377 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) ); |
| 6378 | return( 0 ); |
| 6379 | } |
| 6380 | } |
| 6381 | |
| 6382 | return( ssl_write_hello_request( ssl ) ); |
| 6383 | } |
| 6384 | #endif |
| 6385 | #endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */ |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6386 | |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6387 | /* |
| 6388 | * Handshake functions |
| 6389 | */ |
| 6390 | #if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
| 6391 | /* No certificate support -> dummy functions */ |
| 6392 | int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl ) |
| 6393 | { |
| 6394 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 6395 | ssl->handshake->ciphersuite_info; |
| 6396 | |
| 6397 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) ); |
| 6398 | |
| 6399 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) |
| 6400 | { |
| 6401 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) ); |
| 6402 | ssl->state++; |
| 6403 | return( 0 ); |
| 6404 | } |
| 6405 | |
| 6406 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 6407 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 6408 | } |
| 6409 | |
| 6410 | int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl ) |
| 6411 | { |
| 6412 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 6413 | ssl->handshake->ciphersuite_info; |
| 6414 | |
| 6415 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) ); |
| 6416 | |
| 6417 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) |
| 6418 | { |
| 6419 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) ); |
| 6420 | ssl->state++; |
| 6421 | return( 0 ); |
| 6422 | } |
| 6423 | |
| 6424 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 6425 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 6426 | } |
| 6427 | |
| 6428 | #else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 6429 | /* Some certificate support -> implement write and parse */ |
| 6430 | |
| 6431 | int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl ) |
| 6432 | { |
| 6433 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
| 6434 | size_t i, n; |
| 6435 | const mbedtls_x509_crt *crt; |
| 6436 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 6437 | ssl->handshake->ciphersuite_info; |
| 6438 | |
| 6439 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) ); |
| 6440 | |
| 6441 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) |
| 6442 | { |
| 6443 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) ); |
| 6444 | ssl->state++; |
| 6445 | return( 0 ); |
| 6446 | } |
| 6447 | |
| 6448 | #if defined(MBEDTLS_SSL_CLI_C) |
| 6449 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 6450 | { |
| 6451 | if( ssl->handshake->client_auth == 0 ) |
| 6452 | { |
| 6453 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) ); |
| 6454 | ssl->state++; |
| 6455 | return( 0 ); |
| 6456 | } |
| 6457 | } |
| 6458 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 6459 | #if defined(MBEDTLS_SSL_SRV_C) |
| 6460 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 6461 | { |
| 6462 | if( mbedtls_ssl_own_cert( ssl ) == NULL ) |
| 6463 | { |
| 6464 | /* Should never happen because we shouldn't have picked the |
| 6465 | * ciphersuite if we don't have a certificate. */ |
| 6466 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 6467 | } |
| 6468 | } |
| 6469 | #endif |
| 6470 | |
| 6471 | MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) ); |
| 6472 | |
| 6473 | /* |
| 6474 | * 0 . 0 handshake type |
| 6475 | * 1 . 3 handshake length |
| 6476 | * 4 . 6 length of all certs |
| 6477 | * 7 . 9 length of cert. 1 |
| 6478 | * 10 . n-1 peer certificate |
| 6479 | * n . n+2 length of cert. 2 |
| 6480 | * n+3 . ... upper level cert, etc. |
| 6481 | */ |
| 6482 | i = 7; |
| 6483 | crt = mbedtls_ssl_own_cert( ssl ); |
| 6484 | |
| 6485 | while( crt != NULL ) |
| 6486 | { |
| 6487 | n = crt->raw.len; |
| 6488 | if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i ) |
| 6489 | { |
| 6490 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %" MBEDTLS_PRINTF_SIZET |
| 6491 | " > %" MBEDTLS_PRINTF_SIZET, |
| 6492 | i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN ) ); |
| 6493 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 6494 | } |
| 6495 | |
| 6496 | ssl->out_msg[i ] = MBEDTLS_BYTE_2( n ); |
| 6497 | ssl->out_msg[i + 1] = MBEDTLS_BYTE_1( n ); |
| 6498 | ssl->out_msg[i + 2] = MBEDTLS_BYTE_0( n ); |
| 6499 | |
| 6500 | i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n ); |
| 6501 | i += n; crt = crt->next; |
| 6502 | } |
| 6503 | |
| 6504 | ssl->out_msg[4] = MBEDTLS_BYTE_2( i - 7 ); |
| 6505 | ssl->out_msg[5] = MBEDTLS_BYTE_1( i - 7 ); |
| 6506 | ssl->out_msg[6] = MBEDTLS_BYTE_0( i - 7 ); |
| 6507 | |
| 6508 | ssl->out_msglen = i; |
| 6509 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 6510 | ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE; |
| 6511 | |
| 6512 | ssl->state++; |
| 6513 | |
| 6514 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
| 6515 | { |
| 6516 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); |
| 6517 | return( ret ); |
| 6518 | } |
| 6519 | |
| 6520 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) ); |
| 6521 | |
| 6522 | return( ret ); |
| 6523 | } |
| 6524 | |
| 6525 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) |
| 6526 | |
| 6527 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6528 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6529 | static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl, |
| 6530 | unsigned char *crt_buf, |
| 6531 | size_t crt_buf_len ) |
| 6532 | { |
| 6533 | mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert; |
| 6534 | |
| 6535 | if( peer_crt == NULL ) |
| 6536 | return( -1 ); |
| 6537 | |
| 6538 | if( peer_crt->raw.len != crt_buf_len ) |
| 6539 | return( -1 ); |
| 6540 | |
| 6541 | return( memcmp( peer_crt->raw.p, crt_buf, peer_crt->raw.len ) ); |
| 6542 | } |
| 6543 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6544 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6545 | static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl, |
| 6546 | unsigned char *crt_buf, |
| 6547 | size_t crt_buf_len ) |
| 6548 | { |
| 6549 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 6550 | unsigned char const * const peer_cert_digest = |
| 6551 | ssl->session->peer_cert_digest; |
| 6552 | mbedtls_md_type_t const peer_cert_digest_type = |
| 6553 | ssl->session->peer_cert_digest_type; |
| 6554 | mbedtls_md_info_t const * const digest_info = |
| 6555 | mbedtls_md_info_from_type( peer_cert_digest_type ); |
| 6556 | unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN]; |
| 6557 | size_t digest_len; |
| 6558 | |
| 6559 | if( peer_cert_digest == NULL || digest_info == NULL ) |
| 6560 | return( -1 ); |
| 6561 | |
| 6562 | digest_len = mbedtls_md_get_size( digest_info ); |
| 6563 | if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN ) |
| 6564 | return( -1 ); |
| 6565 | |
| 6566 | ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest ); |
| 6567 | if( ret != 0 ) |
| 6568 | return( -1 ); |
| 6569 | |
| 6570 | return( memcmp( tmp_digest, peer_cert_digest, digest_len ) ); |
| 6571 | } |
| 6572 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 6573 | #endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */ |
| 6574 | |
| 6575 | /* |
| 6576 | * Once the certificate message is read, parse it into a cert chain and |
| 6577 | * perform basic checks, but leave actual verification to the caller |
| 6578 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6579 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6580 | static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl, |
| 6581 | mbedtls_x509_crt *chain ) |
| 6582 | { |
| 6583 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 6584 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) |
| 6585 | int crt_cnt=0; |
| 6586 | #endif |
| 6587 | size_t i, n; |
| 6588 | uint8_t alert; |
| 6589 | |
| 6590 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) |
| 6591 | { |
| 6592 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
| 6593 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6594 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
| 6595 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
| 6596 | } |
| 6597 | |
| 6598 | if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ) |
| 6599 | { |
| 6600 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6601 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
| 6602 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
| 6603 | } |
| 6604 | |
| 6605 | if( ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 ) |
| 6606 | { |
| 6607 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
| 6608 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6609 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
| 6610 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
| 6611 | } |
| 6612 | |
| 6613 | i = mbedtls_ssl_hs_hdr_len( ssl ); |
| 6614 | |
| 6615 | /* |
| 6616 | * Same message structure as in mbedtls_ssl_write_certificate() |
| 6617 | */ |
| 6618 | n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2]; |
| 6619 | |
| 6620 | if( ssl->in_msg[i] != 0 || |
| 6621 | ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) ) |
| 6622 | { |
| 6623 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
| 6624 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6625 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
| 6626 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
| 6627 | } |
| 6628 | |
| 6629 | /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */ |
| 6630 | i += 3; |
| 6631 | |
| 6632 | /* Iterate through and parse the CRTs in the provided chain. */ |
| 6633 | while( i < ssl->in_hslen ) |
| 6634 | { |
| 6635 | /* Check that there's room for the next CRT's length fields. */ |
| 6636 | if ( i + 3 > ssl->in_hslen ) { |
| 6637 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
| 6638 | mbedtls_ssl_send_alert_message( ssl, |
| 6639 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6640 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
| 6641 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
| 6642 | } |
| 6643 | /* In theory, the CRT can be up to 2**24 Bytes, but we don't support |
| 6644 | * anything beyond 2**16 ~ 64K. */ |
| 6645 | if( ssl->in_msg[i] != 0 ) |
| 6646 | { |
| 6647 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
| 6648 | mbedtls_ssl_send_alert_message( ssl, |
| 6649 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6650 | MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT ); |
| 6651 | return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE ); |
| 6652 | } |
| 6653 | |
| 6654 | /* Read length of the next CRT in the chain. */ |
| 6655 | n = ( (unsigned int) ssl->in_msg[i + 1] << 8 ) |
| 6656 | | (unsigned int) ssl->in_msg[i + 2]; |
| 6657 | i += 3; |
| 6658 | |
| 6659 | if( n < 128 || i + n > ssl->in_hslen ) |
| 6660 | { |
| 6661 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
| 6662 | mbedtls_ssl_send_alert_message( ssl, |
| 6663 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6664 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
| 6665 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
| 6666 | } |
| 6667 | |
| 6668 | /* Check if we're handling the first CRT in the chain. */ |
| 6669 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) |
| 6670 | if( crt_cnt++ == 0 && |
| 6671 | ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
| 6672 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
| 6673 | { |
| 6674 | /* During client-side renegotiation, check that the server's |
| 6675 | * end-CRTs hasn't changed compared to the initial handshake, |
| 6676 | * mitigating the triple handshake attack. On success, reuse |
| 6677 | * the original end-CRT instead of parsing it again. */ |
| 6678 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) ); |
| 6679 | if( ssl_check_peer_crt_unchanged( ssl, |
| 6680 | &ssl->in_msg[i], |
| 6681 | n ) != 0 ) |
| 6682 | { |
| 6683 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) ); |
| 6684 | mbedtls_ssl_send_alert_message( ssl, |
| 6685 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6686 | MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED ); |
| 6687 | return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE ); |
| 6688 | } |
| 6689 | |
| 6690 | /* Now we can safely free the original chain. */ |
| 6691 | ssl_clear_peer_cert( ssl->session ); |
| 6692 | } |
| 6693 | #endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */ |
| 6694 | |
| 6695 | /* Parse the next certificate in the chain. */ |
| 6696 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 6697 | ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n ); |
| 6698 | #else |
| 6699 | /* If we don't need to store the CRT chain permanently, parse |
| 6700 | * it in-place from the input buffer instead of making a copy. */ |
| 6701 | ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n ); |
| 6702 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 6703 | switch( ret ) |
| 6704 | { |
| 6705 | case 0: /*ok*/ |
| 6706 | case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND: |
| 6707 | /* Ignore certificate with an unknown algorithm: maybe a |
| 6708 | prior certificate was already trusted. */ |
| 6709 | break; |
| 6710 | |
| 6711 | case MBEDTLS_ERR_X509_ALLOC_FAILED: |
| 6712 | alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR; |
| 6713 | goto crt_parse_der_failed; |
| 6714 | |
| 6715 | case MBEDTLS_ERR_X509_UNKNOWN_VERSION: |
| 6716 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 6717 | goto crt_parse_der_failed; |
| 6718 | |
| 6719 | default: |
| 6720 | alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT; |
| 6721 | crt_parse_der_failed: |
| 6722 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert ); |
| 6723 | MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret ); |
| 6724 | return( ret ); |
| 6725 | } |
| 6726 | |
| 6727 | i += n; |
| 6728 | } |
| 6729 | |
| 6730 | MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain ); |
| 6731 | return( 0 ); |
| 6732 | } |
| 6733 | |
| 6734 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6735 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6736 | static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl ) |
| 6737 | { |
| 6738 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 6739 | return( -1 ); |
| 6740 | |
| 6741 | if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) && |
| 6742 | ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 6743 | ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE && |
| 6744 | memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 ) |
| 6745 | { |
Ronald Cron | 1938588 | 2022-06-15 16:26:13 +0200 | [diff] [blame] | 6746 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "peer has no certificate" ) ); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6747 | return( 0 ); |
| 6748 | } |
| 6749 | return( -1 ); |
| 6750 | } |
| 6751 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 6752 | |
| 6753 | /* Check if a certificate message is expected. |
| 6754 | * Return either |
| 6755 | * - SSL_CERTIFICATE_EXPECTED, or |
| 6756 | * - SSL_CERTIFICATE_SKIP |
| 6757 | * indicating whether a Certificate message is expected or not. |
| 6758 | */ |
| 6759 | #define SSL_CERTIFICATE_EXPECTED 0 |
| 6760 | #define SSL_CERTIFICATE_SKIP 1 |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6761 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6762 | static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl, |
| 6763 | int authmode ) |
| 6764 | { |
| 6765 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 6766 | ssl->handshake->ciphersuite_info; |
| 6767 | |
| 6768 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) |
| 6769 | return( SSL_CERTIFICATE_SKIP ); |
| 6770 | |
| 6771 | #if defined(MBEDTLS_SSL_SRV_C) |
| 6772 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 6773 | { |
| 6774 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ) |
| 6775 | return( SSL_CERTIFICATE_SKIP ); |
| 6776 | |
| 6777 | if( authmode == MBEDTLS_SSL_VERIFY_NONE ) |
| 6778 | { |
| 6779 | ssl->session_negotiate->verify_result = |
| 6780 | MBEDTLS_X509_BADCERT_SKIP_VERIFY; |
| 6781 | return( SSL_CERTIFICATE_SKIP ); |
| 6782 | } |
| 6783 | } |
| 6784 | #else |
| 6785 | ((void) authmode); |
| 6786 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 6787 | |
| 6788 | return( SSL_CERTIFICATE_EXPECTED ); |
| 6789 | } |
| 6790 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6791 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6792 | static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl, |
| 6793 | int authmode, |
| 6794 | mbedtls_x509_crt *chain, |
| 6795 | void *rs_ctx ) |
| 6796 | { |
| 6797 | int ret = 0; |
| 6798 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 6799 | ssl->handshake->ciphersuite_info; |
| 6800 | int have_ca_chain = 0; |
| 6801 | |
| 6802 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *); |
| 6803 | void *p_vrfy; |
| 6804 | |
| 6805 | if( authmode == MBEDTLS_SSL_VERIFY_NONE ) |
| 6806 | return( 0 ); |
| 6807 | |
| 6808 | if( ssl->f_vrfy != NULL ) |
| 6809 | { |
| 6810 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) ); |
| 6811 | f_vrfy = ssl->f_vrfy; |
| 6812 | p_vrfy = ssl->p_vrfy; |
| 6813 | } |
| 6814 | else |
| 6815 | { |
| 6816 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) ); |
| 6817 | f_vrfy = ssl->conf->f_vrfy; |
| 6818 | p_vrfy = ssl->conf->p_vrfy; |
| 6819 | } |
| 6820 | |
| 6821 | /* |
| 6822 | * Main check: verify certificate |
| 6823 | */ |
| 6824 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 6825 | if( ssl->conf->f_ca_cb != NULL ) |
| 6826 | { |
| 6827 | ((void) rs_ctx); |
| 6828 | have_ca_chain = 1; |
| 6829 | |
| 6830 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) ); |
| 6831 | ret = mbedtls_x509_crt_verify_with_ca_cb( |
| 6832 | chain, |
| 6833 | ssl->conf->f_ca_cb, |
| 6834 | ssl->conf->p_ca_cb, |
| 6835 | ssl->conf->cert_profile, |
| 6836 | ssl->hostname, |
| 6837 | &ssl->session_negotiate->verify_result, |
| 6838 | f_vrfy, p_vrfy ); |
| 6839 | } |
| 6840 | else |
| 6841 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
| 6842 | { |
| 6843 | mbedtls_x509_crt *ca_chain; |
| 6844 | mbedtls_x509_crl *ca_crl; |
| 6845 | |
| 6846 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 6847 | if( ssl->handshake->sni_ca_chain != NULL ) |
| 6848 | { |
| 6849 | ca_chain = ssl->handshake->sni_ca_chain; |
| 6850 | ca_crl = ssl->handshake->sni_ca_crl; |
| 6851 | } |
| 6852 | else |
| 6853 | #endif |
| 6854 | { |
| 6855 | ca_chain = ssl->conf->ca_chain; |
| 6856 | ca_crl = ssl->conf->ca_crl; |
| 6857 | } |
| 6858 | |
| 6859 | if( ca_chain != NULL ) |
| 6860 | have_ca_chain = 1; |
| 6861 | |
| 6862 | ret = mbedtls_x509_crt_verify_restartable( |
| 6863 | chain, |
| 6864 | ca_chain, ca_crl, |
| 6865 | ssl->conf->cert_profile, |
| 6866 | ssl->hostname, |
| 6867 | &ssl->session_negotiate->verify_result, |
| 6868 | f_vrfy, p_vrfy, rs_ctx ); |
| 6869 | } |
| 6870 | |
| 6871 | if( ret != 0 ) |
| 6872 | { |
| 6873 | MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret ); |
| 6874 | } |
| 6875 | |
| 6876 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
| 6877 | if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS ) |
| 6878 | return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS ); |
| 6879 | #endif |
| 6880 | |
| 6881 | /* |
| 6882 | * Secondary checks: always done, but change 'ret' only if it was 0 |
| 6883 | */ |
| 6884 | |
| 6885 | #if defined(MBEDTLS_ECP_C) |
| 6886 | { |
| 6887 | const mbedtls_pk_context *pk = &chain->pk; |
| 6888 | |
Manuel Pégourié-Gonnard | 66b0d61 | 2022-06-17 10:49:29 +0200 | [diff] [blame] | 6889 | /* If certificate uses an EC key, make sure the curve is OK. |
| 6890 | * This is a public key, so it can't be opaque, so can_do() is a good |
| 6891 | * enough check to ensure pk_ec() is safe to use here. */ |
Leonid Rozenboim | 19e5973 | 2022-08-08 16:52:38 -0700 | [diff] [blame] | 6892 | if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) ) |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6893 | { |
Leonid Rozenboim | 19e5973 | 2022-08-08 16:52:38 -0700 | [diff] [blame] | 6894 | /* and in the unlikely case the above assumption no longer holds |
| 6895 | * we are making sure that pk_ec() here does not return a NULL |
| 6896 | */ |
| 6897 | const mbedtls_ecp_keypair *ec = mbedtls_pk_ec( *pk ); |
| 6898 | if( ec == NULL ) |
| 6899 | { |
Tom Cosgrove | 20c1137 | 2022-08-24 15:06:13 +0100 | [diff] [blame] | 6900 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_pk_ec() returned NULL" ) ); |
Leonid Rozenboim | 19e5973 | 2022-08-08 16:52:38 -0700 | [diff] [blame] | 6901 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 6902 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6903 | |
Leonid Rozenboim | 19e5973 | 2022-08-08 16:52:38 -0700 | [diff] [blame] | 6904 | if( mbedtls_ssl_check_curve( ssl, ec->grp.id ) != 0 ) |
| 6905 | { |
| 6906 | ssl->session_negotiate->verify_result |= |
| 6907 | MBEDTLS_X509_BADCERT_BAD_KEY; |
| 6908 | |
| 6909 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) ); |
| 6910 | if( ret == 0 ) |
| 6911 | ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE; |
| 6912 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6913 | } |
| 6914 | } |
| 6915 | #endif /* MBEDTLS_ECP_C */ |
| 6916 | |
| 6917 | if( mbedtls_ssl_check_cert_usage( chain, |
| 6918 | ciphersuite_info, |
| 6919 | ! ssl->conf->endpoint, |
| 6920 | &ssl->session_negotiate->verify_result ) != 0 ) |
| 6921 | { |
| 6922 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) ); |
| 6923 | if( ret == 0 ) |
| 6924 | ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE; |
| 6925 | } |
| 6926 | |
| 6927 | /* mbedtls_x509_crt_verify_with_profile is supposed to report a |
| 6928 | * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED, |
| 6929 | * with details encoded in the verification flags. All other kinds |
| 6930 | * of error codes, including those from the user provided f_vrfy |
| 6931 | * functions, are treated as fatal and lead to a failure of |
| 6932 | * ssl_parse_certificate even if verification was optional. */ |
| 6933 | if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL && |
| 6934 | ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED || |
| 6935 | ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE ) ) |
| 6936 | { |
| 6937 | ret = 0; |
| 6938 | } |
| 6939 | |
| 6940 | if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED ) |
| 6941 | { |
| 6942 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) ); |
| 6943 | ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED; |
| 6944 | } |
| 6945 | |
| 6946 | if( ret != 0 ) |
| 6947 | { |
| 6948 | uint8_t alert; |
| 6949 | |
| 6950 | /* The certificate may have been rejected for several reasons. |
| 6951 | Pick one and send the corresponding alert. Which alert to send |
| 6952 | may be a subject of debate in some cases. */ |
| 6953 | if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER ) |
| 6954 | alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED; |
| 6955 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH ) |
| 6956 | alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT; |
| 6957 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE ) |
| 6958 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 6959 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE ) |
| 6960 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 6961 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE ) |
| 6962 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 6963 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK ) |
| 6964 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 6965 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY ) |
| 6966 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 6967 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED ) |
| 6968 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED; |
| 6969 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED ) |
| 6970 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED; |
| 6971 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED ) |
| 6972 | alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA; |
| 6973 | else |
| 6974 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN; |
| 6975 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6976 | alert ); |
| 6977 | } |
| 6978 | |
| 6979 | #if defined(MBEDTLS_DEBUG_C) |
| 6980 | if( ssl->session_negotiate->verify_result != 0 ) |
| 6981 | { |
| 6982 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %08x", |
| 6983 | (unsigned int) ssl->session_negotiate->verify_result ) ); |
| 6984 | } |
| 6985 | else |
| 6986 | { |
| 6987 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) ); |
| 6988 | } |
| 6989 | #endif /* MBEDTLS_DEBUG_C */ |
| 6990 | |
| 6991 | return( ret ); |
| 6992 | } |
| 6993 | |
| 6994 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6995 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6996 | static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl, |
| 6997 | unsigned char *start, size_t len ) |
| 6998 | { |
| 6999 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 7000 | /* Remember digest of the peer's end-CRT. */ |
| 7001 | ssl->session_negotiate->peer_cert_digest = |
| 7002 | mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ); |
| 7003 | if( ssl->session_negotiate->peer_cert_digest == NULL ) |
| 7004 | { |
| 7005 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", |
| 7006 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ); |
| 7007 | mbedtls_ssl_send_alert_message( ssl, |
| 7008 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7009 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
| 7010 | |
| 7011 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 7012 | } |
| 7013 | |
| 7014 | ret = mbedtls_md( mbedtls_md_info_from_type( |
| 7015 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ), |
| 7016 | start, len, |
| 7017 | ssl->session_negotiate->peer_cert_digest ); |
| 7018 | |
| 7019 | ssl->session_negotiate->peer_cert_digest_type = |
| 7020 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE; |
| 7021 | ssl->session_negotiate->peer_cert_digest_len = |
| 7022 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN; |
| 7023 | |
| 7024 | return( ret ); |
| 7025 | } |
| 7026 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 7027 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7028 | static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl, |
| 7029 | unsigned char *start, size_t len ) |
| 7030 | { |
| 7031 | unsigned char *end = start + len; |
| 7032 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 7033 | |
| 7034 | /* Make a copy of the peer's raw public key. */ |
| 7035 | mbedtls_pk_init( &ssl->handshake->peer_pubkey ); |
| 7036 | ret = mbedtls_pk_parse_subpubkey( &start, end, |
| 7037 | &ssl->handshake->peer_pubkey ); |
| 7038 | if( ret != 0 ) |
| 7039 | { |
| 7040 | /* We should have parsed the public key before. */ |
| 7041 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 7042 | } |
| 7043 | |
| 7044 | return( 0 ); |
| 7045 | } |
| 7046 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 7047 | |
| 7048 | int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl ) |
| 7049 | { |
| 7050 | int ret = 0; |
| 7051 | int crt_expected; |
| 7052 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 7053 | const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET |
| 7054 | ? ssl->handshake->sni_authmode |
| 7055 | : ssl->conf->authmode; |
| 7056 | #else |
| 7057 | const int authmode = ssl->conf->authmode; |
| 7058 | #endif |
| 7059 | void *rs_ctx = NULL; |
| 7060 | mbedtls_x509_crt *chain = NULL; |
| 7061 | |
| 7062 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) ); |
| 7063 | |
| 7064 | crt_expected = ssl_parse_certificate_coordinate( ssl, authmode ); |
| 7065 | if( crt_expected == SSL_CERTIFICATE_SKIP ) |
| 7066 | { |
| 7067 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) ); |
| 7068 | goto exit; |
| 7069 | } |
| 7070 | |
| 7071 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
| 7072 | if( ssl->handshake->ecrs_enabled && |
| 7073 | ssl->handshake->ecrs_state == ssl_ecrs_crt_verify ) |
| 7074 | { |
| 7075 | chain = ssl->handshake->ecrs_peer_cert; |
| 7076 | ssl->handshake->ecrs_peer_cert = NULL; |
| 7077 | goto crt_verify; |
| 7078 | } |
| 7079 | #endif |
| 7080 | |
| 7081 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) |
| 7082 | { |
| 7083 | /* mbedtls_ssl_read_record may have sent an alert already. We |
| 7084 | let it decide whether to alert. */ |
| 7085 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
| 7086 | goto exit; |
| 7087 | } |
| 7088 | |
| 7089 | #if defined(MBEDTLS_SSL_SRV_C) |
| 7090 | if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 ) |
| 7091 | { |
| 7092 | ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING; |
| 7093 | |
| 7094 | if( authmode != MBEDTLS_SSL_VERIFY_OPTIONAL ) |
| 7095 | ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE; |
| 7096 | |
| 7097 | goto exit; |
| 7098 | } |
| 7099 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 7100 | |
| 7101 | /* Clear existing peer CRT structure in case we tried to |
| 7102 | * reuse a session but it failed, and allocate a new one. */ |
| 7103 | ssl_clear_peer_cert( ssl->session_negotiate ); |
| 7104 | |
| 7105 | chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ); |
| 7106 | if( chain == NULL ) |
| 7107 | { |
| 7108 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", |
| 7109 | sizeof( mbedtls_x509_crt ) ) ); |
| 7110 | mbedtls_ssl_send_alert_message( ssl, |
| 7111 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7112 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
| 7113 | |
| 7114 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 7115 | goto exit; |
| 7116 | } |
| 7117 | mbedtls_x509_crt_init( chain ); |
| 7118 | |
| 7119 | ret = ssl_parse_certificate_chain( ssl, chain ); |
| 7120 | if( ret != 0 ) |
| 7121 | goto exit; |
| 7122 | |
| 7123 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
| 7124 | if( ssl->handshake->ecrs_enabled) |
| 7125 | ssl->handshake->ecrs_state = ssl_ecrs_crt_verify; |
| 7126 | |
| 7127 | crt_verify: |
| 7128 | if( ssl->handshake->ecrs_enabled) |
| 7129 | rs_ctx = &ssl->handshake->ecrs_ctx; |
| 7130 | #endif |
| 7131 | |
| 7132 | ret = ssl_parse_certificate_verify( ssl, authmode, |
| 7133 | chain, rs_ctx ); |
| 7134 | if( ret != 0 ) |
| 7135 | goto exit; |
| 7136 | |
| 7137 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 7138 | { |
| 7139 | unsigned char *crt_start, *pk_start; |
| 7140 | size_t crt_len, pk_len; |
| 7141 | |
| 7142 | /* We parse the CRT chain without copying, so |
| 7143 | * these pointers point into the input buffer, |
| 7144 | * and are hence still valid after freeing the |
| 7145 | * CRT chain. */ |
| 7146 | |
| 7147 | crt_start = chain->raw.p; |
| 7148 | crt_len = chain->raw.len; |
| 7149 | |
| 7150 | pk_start = chain->pk_raw.p; |
| 7151 | pk_len = chain->pk_raw.len; |
| 7152 | |
| 7153 | /* Free the CRT structures before computing |
| 7154 | * digest and copying the peer's public key. */ |
| 7155 | mbedtls_x509_crt_free( chain ); |
| 7156 | mbedtls_free( chain ); |
| 7157 | chain = NULL; |
| 7158 | |
| 7159 | ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len ); |
| 7160 | if( ret != 0 ) |
| 7161 | goto exit; |
| 7162 | |
| 7163 | ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len ); |
| 7164 | if( ret != 0 ) |
| 7165 | goto exit; |
| 7166 | } |
| 7167 | #else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 7168 | /* Pass ownership to session structure. */ |
| 7169 | ssl->session_negotiate->peer_cert = chain; |
| 7170 | chain = NULL; |
| 7171 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 7172 | |
| 7173 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) ); |
| 7174 | |
| 7175 | exit: |
| 7176 | |
| 7177 | if( ret == 0 ) |
| 7178 | ssl->state++; |
| 7179 | |
| 7180 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
| 7181 | if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS ) |
| 7182 | { |
| 7183 | ssl->handshake->ecrs_peer_cert = chain; |
| 7184 | chain = NULL; |
| 7185 | } |
| 7186 | #endif |
| 7187 | |
| 7188 | if( chain != NULL ) |
| 7189 | { |
| 7190 | mbedtls_x509_crt_free( chain ); |
| 7191 | mbedtls_free( chain ); |
| 7192 | } |
| 7193 | |
| 7194 | return( ret ); |
| 7195 | } |
| 7196 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 7197 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 7198 | #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] | 7199 | static void ssl_calc_finished_tls_sha256( |
| 7200 | mbedtls_ssl_context *ssl, unsigned char *buf, int from ) |
| 7201 | { |
| 7202 | int len = 12; |
| 7203 | const char *sender; |
| 7204 | unsigned char padbuf[32]; |
| 7205 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7206 | size_t hash_size; |
| 7207 | psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT; |
| 7208 | psa_status_t status; |
| 7209 | #else |
| 7210 | mbedtls_sha256_context sha256; |
| 7211 | #endif |
| 7212 | |
| 7213 | mbedtls_ssl_session *session = ssl->session_negotiate; |
| 7214 | if( !session ) |
| 7215 | session = ssl->session; |
| 7216 | |
| 7217 | sender = ( from == MBEDTLS_SSL_IS_CLIENT ) |
| 7218 | ? "client finished" |
| 7219 | : "server finished"; |
| 7220 | |
| 7221 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7222 | sha256_psa = psa_hash_operation_init(); |
| 7223 | |
| 7224 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) ); |
| 7225 | |
| 7226 | status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa ); |
| 7227 | if( status != PSA_SUCCESS ) |
| 7228 | { |
| 7229 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); |
| 7230 | return; |
| 7231 | } |
| 7232 | |
| 7233 | status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size ); |
| 7234 | if( status != PSA_SUCCESS ) |
| 7235 | { |
| 7236 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); |
| 7237 | return; |
| 7238 | } |
| 7239 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 ); |
| 7240 | #else |
| 7241 | |
| 7242 | mbedtls_sha256_init( &sha256 ); |
| 7243 | |
| 7244 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) ); |
| 7245 | |
| 7246 | mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 ); |
| 7247 | |
| 7248 | /* |
| 7249 | * TLSv1.2: |
| 7250 | * hash = PRF( master, finished_label, |
| 7251 | * Hash( handshake ) )[0.11] |
| 7252 | */ |
| 7253 | |
| 7254 | #if !defined(MBEDTLS_SHA256_ALT) |
| 7255 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *) |
| 7256 | sha256.state, sizeof( sha256.state ) ); |
| 7257 | #endif |
| 7258 | |
| 7259 | mbedtls_sha256_finish( &sha256, padbuf ); |
| 7260 | mbedtls_sha256_free( &sha256 ); |
| 7261 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 7262 | |
| 7263 | ssl->handshake->tls_prf( session->master, 48, sender, |
| 7264 | padbuf, 32, buf, len ); |
| 7265 | |
| 7266 | MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len ); |
| 7267 | |
| 7268 | mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) ); |
| 7269 | |
| 7270 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) ); |
| 7271 | } |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 7272 | #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] | 7273 | |
Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 7274 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 7275 | #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] | 7276 | static void ssl_calc_finished_tls_sha384( |
| 7277 | mbedtls_ssl_context *ssl, unsigned char *buf, int from ) |
| 7278 | { |
| 7279 | int len = 12; |
| 7280 | const char *sender; |
| 7281 | unsigned char padbuf[48]; |
| 7282 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7283 | size_t hash_size; |
| 7284 | psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT; |
| 7285 | psa_status_t status; |
| 7286 | #else |
| 7287 | mbedtls_sha512_context sha512; |
| 7288 | #endif |
| 7289 | |
| 7290 | mbedtls_ssl_session *session = ssl->session_negotiate; |
| 7291 | if( !session ) |
| 7292 | session = ssl->session; |
| 7293 | |
| 7294 | sender = ( from == MBEDTLS_SSL_IS_CLIENT ) |
| 7295 | ? "client finished" |
| 7296 | : "server finished"; |
| 7297 | |
| 7298 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7299 | sha384_psa = psa_hash_operation_init(); |
| 7300 | |
| 7301 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) ); |
| 7302 | |
| 7303 | status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa ); |
| 7304 | if( status != PSA_SUCCESS ) |
| 7305 | { |
| 7306 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); |
| 7307 | return; |
| 7308 | } |
| 7309 | |
| 7310 | status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size ); |
| 7311 | if( status != PSA_SUCCESS ) |
| 7312 | { |
| 7313 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); |
| 7314 | return; |
| 7315 | } |
| 7316 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 ); |
| 7317 | #else |
| 7318 | mbedtls_sha512_init( &sha512 ); |
| 7319 | |
| 7320 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) ); |
| 7321 | |
Andrzej Kurek | a242e83 | 2022-08-11 10:03:14 -0400 | [diff] [blame] | 7322 | mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 ); |
Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 7323 | |
| 7324 | /* |
| 7325 | * TLSv1.2: |
| 7326 | * hash = PRF( master, finished_label, |
| 7327 | * Hash( handshake ) )[0.11] |
| 7328 | */ |
| 7329 | |
| 7330 | #if !defined(MBEDTLS_SHA512_ALT) |
| 7331 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *) |
| 7332 | sha512.state, sizeof( sha512.state ) ); |
| 7333 | #endif |
| 7334 | mbedtls_sha512_finish( &sha512, padbuf ); |
| 7335 | |
| 7336 | mbedtls_sha512_free( &sha512 ); |
| 7337 | #endif |
| 7338 | |
| 7339 | ssl->handshake->tls_prf( session->master, 48, sender, |
| 7340 | padbuf, 48, buf, len ); |
| 7341 | |
| 7342 | MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len ); |
| 7343 | |
| 7344 | mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) ); |
| 7345 | |
| 7346 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) ); |
| 7347 | } |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 7348 | #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] | 7349 | |
Jerry Yu | aef0015 | 2022-02-17 14:27:31 +0800 | [diff] [blame] | 7350 | void mbedtls_ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl ) |
| 7351 | { |
| 7352 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) ); |
| 7353 | |
| 7354 | /* |
| 7355 | * Free our handshake params |
| 7356 | */ |
| 7357 | mbedtls_ssl_handshake_free( ssl ); |
| 7358 | mbedtls_free( ssl->handshake ); |
| 7359 | ssl->handshake = NULL; |
| 7360 | |
| 7361 | /* |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 7362 | * Free the previous transform and switch in the current one |
Jerry Yu | aef0015 | 2022-02-17 14:27:31 +0800 | [diff] [blame] | 7363 | */ |
| 7364 | if( ssl->transform ) |
| 7365 | { |
| 7366 | mbedtls_ssl_transform_free( ssl->transform ); |
| 7367 | mbedtls_free( ssl->transform ); |
| 7368 | } |
| 7369 | ssl->transform = ssl->transform_negotiate; |
| 7370 | ssl->transform_negotiate = NULL; |
| 7371 | |
| 7372 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) ); |
| 7373 | } |
| 7374 | |
Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 7375 | void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl ) |
| 7376 | { |
| 7377 | int resume = ssl->handshake->resume; |
| 7378 | |
| 7379 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) ); |
| 7380 | |
| 7381 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 7382 | if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
| 7383 | { |
| 7384 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE; |
| 7385 | ssl->renego_records_seen = 0; |
| 7386 | } |
| 7387 | #endif |
| 7388 | |
| 7389 | /* |
| 7390 | * Free the previous session and switch in the current one |
| 7391 | */ |
| 7392 | if( ssl->session ) |
| 7393 | { |
| 7394 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 7395 | /* RFC 7366 3.1: keep the EtM state */ |
| 7396 | ssl->session_negotiate->encrypt_then_mac = |
| 7397 | ssl->session->encrypt_then_mac; |
| 7398 | #endif |
| 7399 | |
| 7400 | mbedtls_ssl_session_free( ssl->session ); |
| 7401 | mbedtls_free( ssl->session ); |
| 7402 | } |
| 7403 | ssl->session = ssl->session_negotiate; |
| 7404 | ssl->session_negotiate = NULL; |
| 7405 | |
| 7406 | /* |
| 7407 | * Add cache entry |
| 7408 | */ |
| 7409 | if( ssl->conf->f_set_cache != NULL && |
| 7410 | ssl->session->id_len != 0 && |
| 7411 | resume == 0 ) |
| 7412 | { |
| 7413 | if( ssl->conf->f_set_cache( ssl->conf->p_cache, |
| 7414 | ssl->session->id, |
| 7415 | ssl->session->id_len, |
| 7416 | ssl->session ) != 0 ) |
| 7417 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) ); |
| 7418 | } |
| 7419 | |
| 7420 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 7421 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 7422 | ssl->handshake->flight != NULL ) |
| 7423 | { |
| 7424 | /* Cancel handshake timer */ |
| 7425 | mbedtls_ssl_set_timer( ssl, 0 ); |
| 7426 | |
| 7427 | /* Keep last flight around in case we need to resend it: |
| 7428 | * we need the handshake and transform structures for that */ |
| 7429 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) ); |
| 7430 | } |
| 7431 | else |
| 7432 | #endif |
| 7433 | mbedtls_ssl_handshake_wrapup_free_hs_transform( ssl ); |
| 7434 | |
| 7435 | ssl->state++; |
| 7436 | |
| 7437 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) ); |
| 7438 | } |
| 7439 | |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 7440 | int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl ) |
| 7441 | { |
| 7442 | int ret, hash_len; |
| 7443 | |
| 7444 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) ); |
| 7445 | |
| 7446 | mbedtls_ssl_update_out_pointers( ssl, ssl->transform_negotiate ); |
| 7447 | |
| 7448 | ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint ); |
| 7449 | |
| 7450 | /* |
| 7451 | * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites |
| 7452 | * may define some other value. Currently (early 2016), no defined |
| 7453 | * ciphersuite does this (and this is unlikely to change as activity has |
| 7454 | * moved to TLS 1.3 now) so we can keep the hardcoded 12 here. |
| 7455 | */ |
| 7456 | hash_len = 12; |
| 7457 | |
| 7458 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 7459 | ssl->verify_data_len = hash_len; |
| 7460 | memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len ); |
| 7461 | #endif |
| 7462 | |
| 7463 | ssl->out_msglen = 4 + hash_len; |
| 7464 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 7465 | ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED; |
| 7466 | |
| 7467 | /* |
| 7468 | * In case of session resuming, invert the client and server |
| 7469 | * ChangeCipherSpec messages order. |
| 7470 | */ |
| 7471 | if( ssl->handshake->resume != 0 ) |
| 7472 | { |
| 7473 | #if defined(MBEDTLS_SSL_CLI_C) |
| 7474 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 7475 | ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP; |
| 7476 | #endif |
| 7477 | #if defined(MBEDTLS_SSL_SRV_C) |
| 7478 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 7479 | ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC; |
| 7480 | #endif |
| 7481 | } |
| 7482 | else |
| 7483 | ssl->state++; |
| 7484 | |
| 7485 | /* |
| 7486 | * Switch to our negotiated transform and session parameters for outbound |
| 7487 | * data. |
| 7488 | */ |
| 7489 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) ); |
| 7490 | |
| 7491 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 7492 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 7493 | { |
| 7494 | unsigned char i; |
| 7495 | |
| 7496 | /* Remember current epoch settings for resending */ |
| 7497 | ssl->handshake->alt_transform_out = ssl->transform_out; |
| 7498 | memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, |
| 7499 | sizeof( ssl->handshake->alt_out_ctr ) ); |
| 7500 | |
| 7501 | /* Set sequence_number to zero */ |
| 7502 | memset( &ssl->cur_out_ctr[2], 0, sizeof( ssl->cur_out_ctr ) - 2 ); |
| 7503 | |
| 7504 | |
| 7505 | /* Increment epoch */ |
| 7506 | for( i = 2; i > 0; i-- ) |
| 7507 | if( ++ssl->cur_out_ctr[i - 1] != 0 ) |
| 7508 | break; |
| 7509 | |
| 7510 | /* The loop goes to its end iff the counter is wrapping */ |
| 7511 | if( i == 0 ) |
| 7512 | { |
| 7513 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) ); |
| 7514 | return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING ); |
| 7515 | } |
| 7516 | } |
| 7517 | else |
| 7518 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 7519 | memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) ); |
| 7520 | |
| 7521 | ssl->transform_out = ssl->transform_negotiate; |
| 7522 | ssl->session_out = ssl->session_negotiate; |
| 7523 | |
| 7524 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 7525 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 7526 | mbedtls_ssl_send_flight_completed( ssl ); |
| 7527 | #endif |
| 7528 | |
| 7529 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
| 7530 | { |
| 7531 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); |
| 7532 | return( ret ); |
| 7533 | } |
| 7534 | |
| 7535 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 7536 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 7537 | ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 ) |
| 7538 | { |
| 7539 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret ); |
| 7540 | return( ret ); |
| 7541 | } |
| 7542 | #endif |
| 7543 | |
| 7544 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) ); |
| 7545 | |
| 7546 | return( 0 ); |
| 7547 | } |
| 7548 | |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 7549 | #define SSL_MAX_HASH_LEN 12 |
| 7550 | |
| 7551 | int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl ) |
| 7552 | { |
| 7553 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 7554 | unsigned int hash_len = 12; |
| 7555 | unsigned char buf[SSL_MAX_HASH_LEN]; |
| 7556 | |
| 7557 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) ); |
| 7558 | |
| 7559 | ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 ); |
| 7560 | |
| 7561 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) |
| 7562 | { |
| 7563 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
| 7564 | goto exit; |
| 7565 | } |
| 7566 | |
| 7567 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) |
| 7568 | { |
| 7569 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) ); |
| 7570 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7571 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
| 7572 | ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; |
| 7573 | goto exit; |
| 7574 | } |
| 7575 | |
| 7576 | if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ) |
| 7577 | { |
| 7578 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7579 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
| 7580 | ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; |
| 7581 | goto exit; |
| 7582 | } |
| 7583 | |
| 7584 | if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len ) |
| 7585 | { |
| 7586 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) ); |
| 7587 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7588 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
| 7589 | ret = MBEDTLS_ERR_SSL_DECODE_ERROR; |
| 7590 | goto exit; |
| 7591 | } |
| 7592 | |
| 7593 | if( mbedtls_ct_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), |
| 7594 | buf, hash_len ) != 0 ) |
| 7595 | { |
| 7596 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) ); |
| 7597 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7598 | MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR ); |
| 7599 | ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE; |
| 7600 | goto exit; |
| 7601 | } |
| 7602 | |
| 7603 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 7604 | ssl->verify_data_len = hash_len; |
| 7605 | memcpy( ssl->peer_verify_data, buf, hash_len ); |
| 7606 | #endif |
| 7607 | |
| 7608 | if( ssl->handshake->resume != 0 ) |
| 7609 | { |
| 7610 | #if defined(MBEDTLS_SSL_CLI_C) |
| 7611 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 7612 | ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC; |
| 7613 | #endif |
| 7614 | #if defined(MBEDTLS_SSL_SRV_C) |
| 7615 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 7616 | ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP; |
| 7617 | #endif |
| 7618 | } |
| 7619 | else |
| 7620 | ssl->state++; |
| 7621 | |
| 7622 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 7623 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 7624 | mbedtls_ssl_recv_flight_completed( ssl ); |
| 7625 | #endif |
| 7626 | |
| 7627 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) ); |
| 7628 | |
| 7629 | exit: |
| 7630 | mbedtls_platform_zeroize( buf, hash_len ); |
| 7631 | return( ret ); |
| 7632 | } |
| 7633 | |
Jerry Yu | 392112c | 2022-02-17 14:34:10 +0800 | [diff] [blame] | 7634 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
| 7635 | /* |
| 7636 | * Helper to get TLS 1.2 PRF from ciphersuite |
| 7637 | * (Duplicates bits of logic from ssl_set_handshake_prfs().) |
| 7638 | */ |
| 7639 | static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id ) |
| 7640 | { |
Jerry Yu | 392112c | 2022-02-17 14:34:10 +0800 | [diff] [blame] | 7641 | const mbedtls_ssl_ciphersuite_t * const ciphersuite_info = |
Andrzej Kurek | 6832774 | 2022-10-03 06:18:18 -0400 | [diff] [blame] | 7642 | mbedtls_ssl_ciphersuite_from_id( ciphersuite_id ); |
| 7643 | #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] | 7644 | if( ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA384 ) |
Jerry Yu | 392112c | 2022-02-17 14:34:10 +0800 | [diff] [blame] | 7645 | return( tls_prf_sha384 ); |
Andrzej Kurek | 894edde | 2022-09-29 06:31:14 -0400 | [diff] [blame] | 7646 | else |
Jerry Yu | 392112c | 2022-02-17 14:34:10 +0800 | [diff] [blame] | 7647 | #endif |
Andrzej Kurek | 894edde | 2022-09-29 06:31:14 -0400 | [diff] [blame] | 7648 | #if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
| 7649 | { |
| 7650 | if( ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA256 ) |
| 7651 | return( tls_prf_sha256 ); |
| 7652 | } |
| 7653 | #endif |
| 7654 | #if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \ |
| 7655 | !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
| 7656 | (void) ciphersuite_info; |
| 7657 | #endif |
Andrzej Kurek | eabeb30 | 2022-10-17 07:52:51 -0400 | [diff] [blame] | 7658 | |
Andrzej Kurek | 894edde | 2022-09-29 06:31:14 -0400 | [diff] [blame] | 7659 | return( NULL ); |
Jerry Yu | 392112c | 2022-02-17 14:34:10 +0800 | [diff] [blame] | 7660 | } |
| 7661 | #endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */ |
Jerry Yu | e93ffcd | 2022-02-17 14:37:06 +0800 | [diff] [blame] | 7662 | |
| 7663 | static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf ) |
| 7664 | { |
| 7665 | ((void) tls_prf); |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 7666 | #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] | 7667 | if( tls_prf == tls_prf_sha384 ) |
| 7668 | { |
| 7669 | return( MBEDTLS_SSL_TLS_PRF_SHA384 ); |
| 7670 | } |
| 7671 | else |
| 7672 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 7673 | #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] | 7674 | if( tls_prf == tls_prf_sha256 ) |
| 7675 | { |
| 7676 | return( MBEDTLS_SSL_TLS_PRF_SHA256 ); |
| 7677 | } |
| 7678 | else |
| 7679 | #endif |
| 7680 | return( MBEDTLS_SSL_TLS_PRF_NONE ); |
| 7681 | } |
| 7682 | |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7683 | /* |
| 7684 | * Populate a transform structure with session keys and all the other |
| 7685 | * necessary information. |
| 7686 | * |
| 7687 | * Parameters: |
| 7688 | * - [in/out]: transform: structure to populate |
| 7689 | * [in] must be just initialised with mbedtls_ssl_transform_init() |
| 7690 | * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf() |
| 7691 | * - [in] ciphersuite |
| 7692 | * - [in] master |
| 7693 | * - [in] encrypt_then_mac |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7694 | * - [in] tls_prf: pointer to PRF to use for key derivation |
| 7695 | * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random |
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 7696 | * - [in] tls_version: TLS version |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7697 | * - [in] endpoint: client or server |
| 7698 | * - [in] ssl: used for: |
| 7699 | * - ssl->conf->{f,p}_export_keys |
| 7700 | * [in] optionally used for: |
| 7701 | * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg |
| 7702 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 7703 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7704 | static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform, |
| 7705 | int ciphersuite, |
| 7706 | const unsigned char master[48], |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 7707 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7708 | int encrypt_then_mac, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 7709 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7710 | ssl_tls_prf_t tls_prf, |
| 7711 | const unsigned char randbytes[64], |
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 7712 | mbedtls_ssl_protocol_version tls_version, |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7713 | unsigned endpoint, |
| 7714 | const mbedtls_ssl_context *ssl ) |
| 7715 | { |
| 7716 | int ret = 0; |
| 7717 | unsigned char keyblk[256]; |
| 7718 | unsigned char *key1; |
| 7719 | unsigned char *key2; |
| 7720 | unsigned char *mac_enc; |
| 7721 | unsigned char *mac_dec; |
| 7722 | size_t mac_key_len = 0; |
| 7723 | size_t iv_copy_len; |
| 7724 | size_t keylen; |
| 7725 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info; |
Neil Armstrong | 7fea33e | 2022-04-01 15:40:25 +0200 | [diff] [blame] | 7726 | mbedtls_ssl_mode_t ssl_mode; |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7727 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7728 | const mbedtls_cipher_info_t *cipher_info; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7729 | const mbedtls_md_info_t *md_info; |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7730 | #endif /* !MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7731 | |
| 7732 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7733 | psa_key_type_t key_type; |
| 7734 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7735 | psa_algorithm_t alg; |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7736 | psa_algorithm_t mac_alg = 0; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7737 | size_t key_bits; |
| 7738 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 7739 | #endif |
| 7740 | |
| 7741 | #if !defined(MBEDTLS_DEBUG_C) && \ |
| 7742 | !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 7743 | if( ssl->f_export_keys == NULL ) |
| 7744 | { |
| 7745 | ssl = NULL; /* make sure we don't use it except for these cases */ |
| 7746 | (void) ssl; |
| 7747 | } |
| 7748 | #endif |
| 7749 | |
| 7750 | /* |
| 7751 | * Some data just needs copying into the structure |
| 7752 | */ |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 7753 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7754 | transform->encrypt_then_mac = encrypt_then_mac; |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 7755 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 7756 | transform->tls_version = tls_version; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7757 | |
| 7758 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
| 7759 | memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) ); |
| 7760 | #endif |
| 7761 | |
| 7762 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 7763 | if( tls_version == MBEDTLS_SSL_VERSION_TLS1_3 ) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7764 | { |
| 7765 | /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform |
| 7766 | * generation separate. This should never happen. */ |
| 7767 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 7768 | } |
| 7769 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 7770 | |
| 7771 | /* |
| 7772 | * Get various info structures |
| 7773 | */ |
| 7774 | ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite ); |
| 7775 | if( ciphersuite_info == NULL ) |
| 7776 | { |
| 7777 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found", |
| 7778 | ciphersuite ) ); |
| 7779 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 7780 | } |
| 7781 | |
Neil Armstrong | ab555e0 | 2022-04-04 11:07:59 +0200 | [diff] [blame] | 7782 | ssl_mode = mbedtls_ssl_get_mode_from_ciphersuite( |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 7783 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Neil Armstrong | 7fea33e | 2022-04-01 15:40:25 +0200 | [diff] [blame] | 7784 | encrypt_then_mac, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 7785 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Neil Armstrong | 7fea33e | 2022-04-01 15:40:25 +0200 | [diff] [blame] | 7786 | ciphersuite_info ); |
| 7787 | |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7788 | if( ssl_mode == MBEDTLS_SSL_MODE_AEAD ) |
| 7789 | transform->taglen = |
| 7790 | ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16; |
| 7791 | |
| 7792 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7793 | if( ( status = mbedtls_ssl_cipher_to_psa( ciphersuite_info->cipher, |
| 7794 | transform->taglen, |
| 7795 | &alg, |
| 7796 | &key_type, |
| 7797 | &key_bits ) ) != PSA_SUCCESS ) |
| 7798 | { |
| 7799 | ret = psa_ssl_status_to_mbedtls( status ); |
| 7800 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_cipher_to_psa", ret ); |
| 7801 | goto end; |
| 7802 | } |
| 7803 | #else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7804 | cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher ); |
| 7805 | if( cipher_info == NULL ) |
| 7806 | { |
| 7807 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %u not found", |
| 7808 | ciphersuite_info->cipher ) ); |
| 7809 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 7810 | } |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7811 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7812 | |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7813 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Manuel Pégourié-Gonnard | abac037 | 2022-07-18 13:41:11 +0200 | [diff] [blame] | 7814 | mac_alg = mbedtls_hash_info_psa_from_md( ciphersuite_info->mac ); |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7815 | if( mac_alg == 0 ) |
| 7816 | { |
Manuel Pégourié-Gonnard | abac037 | 2022-07-18 13:41:11 +0200 | [diff] [blame] | 7817 | 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] | 7818 | (unsigned) ciphersuite_info->mac ) ); |
| 7819 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 7820 | } |
| 7821 | #else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7822 | md_info = mbedtls_md_info_from_type( ciphersuite_info->mac ); |
| 7823 | if( md_info == NULL ) |
| 7824 | { |
| 7825 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %u not found", |
| 7826 | (unsigned) ciphersuite_info->mac ) ); |
| 7827 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 7828 | } |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7829 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7830 | |
| 7831 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 7832 | /* Copy own and peer's CID if the use of the CID |
| 7833 | * extension has been negotiated. */ |
| 7834 | if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED ) |
| 7835 | { |
| 7836 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) ); |
| 7837 | |
| 7838 | transform->in_cid_len = ssl->own_cid_len; |
| 7839 | memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len ); |
| 7840 | MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid, |
| 7841 | transform->in_cid_len ); |
| 7842 | |
| 7843 | transform->out_cid_len = ssl->handshake->peer_cid_len; |
| 7844 | memcpy( transform->out_cid, ssl->handshake->peer_cid, |
| 7845 | ssl->handshake->peer_cid_len ); |
| 7846 | MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid, |
| 7847 | transform->out_cid_len ); |
| 7848 | } |
| 7849 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 7850 | |
| 7851 | /* |
| 7852 | * Compute key block using the PRF |
| 7853 | */ |
| 7854 | ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 ); |
| 7855 | if( ret != 0 ) |
| 7856 | { |
| 7857 | MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret ); |
| 7858 | return( ret ); |
| 7859 | } |
| 7860 | |
| 7861 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s", |
| 7862 | mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) ); |
| 7863 | MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 ); |
| 7864 | MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 ); |
| 7865 | MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 ); |
| 7866 | |
| 7867 | /* |
| 7868 | * Determine the appropriate key, IV and MAC length. |
| 7869 | */ |
| 7870 | |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7871 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7872 | keylen = PSA_BITS_TO_BYTES(key_bits); |
| 7873 | #else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7874 | keylen = mbedtls_cipher_info_get_key_bitlen( cipher_info ) / 8; |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7875 | #endif |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7876 | |
| 7877 | #if defined(MBEDTLS_GCM_C) || \ |
| 7878 | defined(MBEDTLS_CCM_C) || \ |
| 7879 | defined(MBEDTLS_CHACHAPOLY_C) |
Neil Armstrong | 7fea33e | 2022-04-01 15:40:25 +0200 | [diff] [blame] | 7880 | if( ssl_mode == MBEDTLS_SSL_MODE_AEAD ) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7881 | { |
| 7882 | size_t explicit_ivlen; |
| 7883 | |
| 7884 | transform->maclen = 0; |
| 7885 | mac_key_len = 0; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7886 | |
| 7887 | /* All modes haves 96-bit IVs, but the length of the static parts vary |
| 7888 | * with mode and version: |
| 7889 | * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes |
| 7890 | * (to be concatenated with a dynamically chosen IV of 8 Bytes) |
| 7891 | * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's |
| 7892 | * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record |
| 7893 | * sequence number). |
| 7894 | */ |
| 7895 | transform->ivlen = 12; |
David Horstmann | 3b2276a | 2022-10-06 14:49:08 +0100 | [diff] [blame] | 7896 | |
| 7897 | int is_chachapoly = 0; |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7898 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
David Horstmann | 3b2276a | 2022-10-06 14:49:08 +0100 | [diff] [blame] | 7899 | is_chachapoly = ( key_type == PSA_KEY_TYPE_CHACHA20 ); |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7900 | #else |
David Horstmann | 3b2276a | 2022-10-06 14:49:08 +0100 | [diff] [blame] | 7901 | is_chachapoly = ( mbedtls_cipher_info_get_mode( cipher_info ) |
| 7902 | == MBEDTLS_MODE_CHACHAPOLY ); |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7903 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
David Horstmann | 3b2276a | 2022-10-06 14:49:08 +0100 | [diff] [blame] | 7904 | |
| 7905 | if( is_chachapoly ) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7906 | transform->fixed_ivlen = 12; |
| 7907 | else |
| 7908 | transform->fixed_ivlen = 4; |
| 7909 | |
| 7910 | /* Minimum length of encrypted record */ |
| 7911 | explicit_ivlen = transform->ivlen - transform->fixed_ivlen; |
| 7912 | transform->minlen = explicit_ivlen + transform->taglen; |
| 7913 | } |
| 7914 | else |
| 7915 | #endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */ |
| 7916 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
Neil Armstrong | 7fea33e | 2022-04-01 15:40:25 +0200 | [diff] [blame] | 7917 | if( ssl_mode == MBEDTLS_SSL_MODE_STREAM || |
| 7918 | ssl_mode == MBEDTLS_SSL_MODE_CBC || |
| 7919 | ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM ) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7920 | { |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7921 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Neil Armstrong | d1be767 | 2022-04-04 11:21:41 +0200 | [diff] [blame] | 7922 | size_t block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ); |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7923 | #else |
| 7924 | size_t block_size = cipher_info->block_size; |
| 7925 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 7926 | |
| 7927 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7928 | /* Get MAC length */ |
| 7929 | mac_key_len = PSA_HASH_LENGTH(mac_alg); |
| 7930 | #else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7931 | /* Initialize HMAC contexts */ |
| 7932 | if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 || |
| 7933 | ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 ) |
| 7934 | { |
| 7935 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret ); |
| 7936 | goto end; |
| 7937 | } |
| 7938 | |
| 7939 | /* Get MAC length */ |
| 7940 | mac_key_len = mbedtls_md_get_size( md_info ); |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7941 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7942 | transform->maclen = mac_key_len; |
| 7943 | |
| 7944 | /* IV length */ |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7945 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Neil Armstrong | 2230e6c | 2022-04-27 10:36:14 +0200 | [diff] [blame] | 7946 | transform->ivlen = PSA_CIPHER_IV_LENGTH( key_type, alg ); |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7947 | #else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7948 | transform->ivlen = cipher_info->iv_size; |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7949 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7950 | |
| 7951 | /* Minimum length */ |
Neil Armstrong | 7fea33e | 2022-04-01 15:40:25 +0200 | [diff] [blame] | 7952 | if( ssl_mode == MBEDTLS_SSL_MODE_STREAM ) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7953 | transform->minlen = transform->maclen; |
| 7954 | else |
| 7955 | { |
| 7956 | /* |
| 7957 | * GenericBlockCipher: |
| 7958 | * 1. if EtM is in use: one block plus MAC |
| 7959 | * otherwise: * first multiple of blocklen greater than maclen |
| 7960 | * 2. IV |
| 7961 | */ |
| 7962 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Neil Armstrong | 7fea33e | 2022-04-01 15:40:25 +0200 | [diff] [blame] | 7963 | if( ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM ) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7964 | { |
| 7965 | transform->minlen = transform->maclen |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7966 | + block_size; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7967 | } |
| 7968 | else |
| 7969 | #endif |
| 7970 | { |
| 7971 | transform->minlen = transform->maclen |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7972 | + block_size |
| 7973 | - transform->maclen % block_size; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7974 | } |
| 7975 | |
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 7976 | if( tls_version == MBEDTLS_SSL_VERSION_TLS1_2 ) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7977 | { |
| 7978 | transform->minlen += transform->ivlen; |
| 7979 | } |
| 7980 | else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7981 | { |
| 7982 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 7983 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 7984 | goto end; |
| 7985 | } |
| 7986 | } |
| 7987 | } |
| 7988 | else |
| 7989 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
| 7990 | { |
| 7991 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 7992 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 7993 | } |
| 7994 | |
| 7995 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u", |
| 7996 | (unsigned) keylen, |
| 7997 | (unsigned) transform->minlen, |
| 7998 | (unsigned) transform->ivlen, |
| 7999 | (unsigned) transform->maclen ) ); |
| 8000 | |
| 8001 | /* |
| 8002 | * Finally setup the cipher contexts, IVs and MAC secrets. |
| 8003 | */ |
| 8004 | #if defined(MBEDTLS_SSL_CLI_C) |
| 8005 | if( endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 8006 | { |
| 8007 | key1 = keyblk + mac_key_len * 2; |
| 8008 | key2 = keyblk + mac_key_len * 2 + keylen; |
| 8009 | |
| 8010 | mac_enc = keyblk; |
| 8011 | mac_dec = keyblk + mac_key_len; |
| 8012 | |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8013 | iv_copy_len = ( transform->fixed_ivlen ) ? |
| 8014 | transform->fixed_ivlen : transform->ivlen; |
| 8015 | memcpy( transform->iv_enc, key2 + keylen, iv_copy_len ); |
| 8016 | memcpy( transform->iv_dec, key2 + keylen + iv_copy_len, |
| 8017 | iv_copy_len ); |
| 8018 | } |
| 8019 | else |
| 8020 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 8021 | #if defined(MBEDTLS_SSL_SRV_C) |
| 8022 | if( endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 8023 | { |
| 8024 | key1 = keyblk + mac_key_len * 2 + keylen; |
| 8025 | key2 = keyblk + mac_key_len * 2; |
| 8026 | |
| 8027 | mac_enc = keyblk + mac_key_len; |
| 8028 | mac_dec = keyblk; |
| 8029 | |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8030 | iv_copy_len = ( transform->fixed_ivlen ) ? |
| 8031 | transform->fixed_ivlen : transform->ivlen; |
| 8032 | memcpy( transform->iv_dec, key1 + keylen, iv_copy_len ); |
| 8033 | memcpy( transform->iv_enc, key1 + keylen + iv_copy_len, |
| 8034 | iv_copy_len ); |
| 8035 | } |
| 8036 | else |
| 8037 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 8038 | { |
| 8039 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 8040 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 8041 | goto end; |
| 8042 | } |
| 8043 | |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8044 | if( ssl != NULL && ssl->f_export_keys != NULL ) |
| 8045 | { |
| 8046 | ssl->f_export_keys( ssl->p_export_keys, |
| 8047 | MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET, |
| 8048 | master, 48, |
| 8049 | randbytes + 32, |
| 8050 | randbytes, |
| 8051 | tls_prf_get_type( tls_prf ) ); |
| 8052 | } |
| 8053 | |
| 8054 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8055 | transform->psa_alg = alg; |
| 8056 | |
| 8057 | if ( alg != MBEDTLS_SSL_NULL_CIPHER ) |
| 8058 | { |
| 8059 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 8060 | psa_set_key_algorithm( &attributes, alg ); |
| 8061 | psa_set_key_type( &attributes, key_type ); |
| 8062 | |
| 8063 | if( ( status = psa_import_key( &attributes, |
| 8064 | key1, |
| 8065 | PSA_BITS_TO_BYTES( key_bits ), |
| 8066 | &transform->psa_key_enc ) ) != PSA_SUCCESS ) |
| 8067 | { |
| 8068 | MBEDTLS_SSL_DEBUG_RET( 3, "psa_import_key", (int)status ); |
| 8069 | ret = psa_ssl_status_to_mbedtls( status ); |
| 8070 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret ); |
| 8071 | goto end; |
| 8072 | } |
| 8073 | |
| 8074 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 8075 | |
| 8076 | if( ( status = psa_import_key( &attributes, |
| 8077 | key2, |
| 8078 | PSA_BITS_TO_BYTES( key_bits ), |
| 8079 | &transform->psa_key_dec ) ) != PSA_SUCCESS ) |
| 8080 | { |
| 8081 | ret = psa_ssl_status_to_mbedtls( status ); |
| 8082 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret ); |
| 8083 | goto end; |
| 8084 | } |
| 8085 | } |
| 8086 | #else |
| 8087 | if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc, |
| 8088 | cipher_info ) ) != 0 ) |
| 8089 | { |
| 8090 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret ); |
| 8091 | goto end; |
| 8092 | } |
| 8093 | |
| 8094 | if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec, |
| 8095 | cipher_info ) ) != 0 ) |
| 8096 | { |
| 8097 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret ); |
| 8098 | goto end; |
| 8099 | } |
| 8100 | |
| 8101 | if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1, |
| 8102 | (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ), |
| 8103 | MBEDTLS_ENCRYPT ) ) != 0 ) |
| 8104 | { |
| 8105 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret ); |
| 8106 | goto end; |
| 8107 | } |
| 8108 | |
| 8109 | if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2, |
| 8110 | (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ), |
| 8111 | MBEDTLS_DECRYPT ) ) != 0 ) |
| 8112 | { |
| 8113 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret ); |
| 8114 | goto end; |
| 8115 | } |
| 8116 | |
| 8117 | #if defined(MBEDTLS_CIPHER_MODE_CBC) |
| 8118 | if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CBC ) |
| 8119 | { |
| 8120 | if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc, |
| 8121 | MBEDTLS_PADDING_NONE ) ) != 0 ) |
| 8122 | { |
| 8123 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret ); |
| 8124 | goto end; |
| 8125 | } |
| 8126 | |
| 8127 | if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec, |
| 8128 | MBEDTLS_PADDING_NONE ) ) != 0 ) |
| 8129 | { |
| 8130 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret ); |
| 8131 | goto end; |
| 8132 | } |
| 8133 | } |
| 8134 | #endif /* MBEDTLS_CIPHER_MODE_CBC */ |
| 8135 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 8136 | |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 8137 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
| 8138 | /* For HMAC-based ciphersuites, initialize the HMAC transforms. |
| 8139 | For AEAD-based ciphersuites, there is nothing to do here. */ |
| 8140 | if( mac_key_len != 0 ) |
| 8141 | { |
| 8142 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 8143 | transform->psa_mac_alg = PSA_ALG_HMAC( mac_alg ); |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 8144 | |
| 8145 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE ); |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 8146 | psa_set_key_algorithm( &attributes, PSA_ALG_HMAC( mac_alg ) ); |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 8147 | psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC ); |
| 8148 | |
| 8149 | if( ( status = psa_import_key( &attributes, |
| 8150 | mac_enc, mac_key_len, |
| 8151 | &transform->psa_mac_enc ) ) != PSA_SUCCESS ) |
| 8152 | { |
| 8153 | ret = psa_ssl_status_to_mbedtls( status ); |
| 8154 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret ); |
| 8155 | goto end; |
| 8156 | } |
| 8157 | |
Ronald Cron | fb39f15 | 2022-03-25 14:36:28 +0100 | [diff] [blame] | 8158 | if( ( transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER ) || |
Andrzej Kurek | 8c95ac4 | 2022-08-17 16:17:00 -0400 | [diff] [blame] | 8159 | ( ( transform->psa_alg == PSA_ALG_CBC_NO_PADDING ) |
| 8160 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
| 8161 | && ( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED ) |
| 8162 | #endif |
| 8163 | ) ) |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 8164 | /* mbedtls_ct_hmac() requires the key to be exportable */ |
| 8165 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT | |
| 8166 | PSA_KEY_USAGE_VERIFY_HASH ); |
| 8167 | else |
| 8168 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH ); |
| 8169 | |
| 8170 | if( ( status = psa_import_key( &attributes, |
| 8171 | mac_dec, mac_key_len, |
| 8172 | &transform->psa_mac_dec ) ) != PSA_SUCCESS ) |
| 8173 | { |
| 8174 | ret = psa_ssl_status_to_mbedtls( status ); |
| 8175 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret ); |
| 8176 | goto end; |
| 8177 | } |
| 8178 | #else |
| 8179 | ret = mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len ); |
| 8180 | if( ret != 0 ) |
| 8181 | goto end; |
| 8182 | ret = mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len ); |
| 8183 | if( ret != 0 ) |
| 8184 | goto end; |
| 8185 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 8186 | } |
| 8187 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
| 8188 | |
| 8189 | ((void) mac_dec); |
| 8190 | ((void) mac_enc); |
| 8191 | |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8192 | end: |
| 8193 | mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) ); |
| 8194 | return( ret ); |
| 8195 | } |
| 8196 | |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 8197 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 8198 | int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl, |
| 8199 | unsigned char *hash, size_t *hashlen, |
| 8200 | unsigned char *data, size_t data_len, |
| 8201 | mbedtls_md_type_t md_alg ) |
| 8202 | { |
| 8203 | psa_status_t status; |
| 8204 | psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT; |
Manuel Pégourié-Gonnard | abac037 | 2022-07-18 13:41:11 +0200 | [diff] [blame] | 8205 | 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] | 8206 | |
| 8207 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) ); |
| 8208 | |
| 8209 | if( ( status = psa_hash_setup( &hash_operation, |
| 8210 | hash_alg ) ) != PSA_SUCCESS ) |
| 8211 | { |
| 8212 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status ); |
| 8213 | goto exit; |
| 8214 | } |
| 8215 | |
| 8216 | if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes, |
| 8217 | 64 ) ) != PSA_SUCCESS ) |
| 8218 | { |
| 8219 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status ); |
| 8220 | goto exit; |
| 8221 | } |
| 8222 | |
| 8223 | if( ( status = psa_hash_update( &hash_operation, |
| 8224 | data, data_len ) ) != PSA_SUCCESS ) |
| 8225 | { |
| 8226 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status ); |
| 8227 | goto exit; |
| 8228 | } |
| 8229 | |
| 8230 | if( ( status = psa_hash_finish( &hash_operation, hash, PSA_HASH_MAX_SIZE, |
| 8231 | hashlen ) ) != PSA_SUCCESS ) |
| 8232 | { |
| 8233 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status ); |
| 8234 | goto exit; |
| 8235 | } |
| 8236 | |
| 8237 | exit: |
| 8238 | if( status != PSA_SUCCESS ) |
| 8239 | { |
| 8240 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 8241 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
| 8242 | switch( status ) |
| 8243 | { |
| 8244 | case PSA_ERROR_NOT_SUPPORTED: |
| 8245 | return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE ); |
| 8246 | case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */ |
| 8247 | case PSA_ERROR_BUFFER_TOO_SMALL: |
| 8248 | return( MBEDTLS_ERR_MD_BAD_INPUT_DATA ); |
| 8249 | case PSA_ERROR_INSUFFICIENT_MEMORY: |
| 8250 | return( MBEDTLS_ERR_MD_ALLOC_FAILED ); |
| 8251 | default: |
| 8252 | return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED ); |
| 8253 | } |
| 8254 | } |
| 8255 | return( 0 ); |
| 8256 | } |
| 8257 | |
| 8258 | #else |
| 8259 | |
| 8260 | int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl, |
| 8261 | unsigned char *hash, size_t *hashlen, |
| 8262 | unsigned char *data, size_t data_len, |
| 8263 | mbedtls_md_type_t md_alg ) |
| 8264 | { |
| 8265 | int ret = 0; |
| 8266 | mbedtls_md_context_t ctx; |
| 8267 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg ); |
| 8268 | *hashlen = mbedtls_md_get_size( md_info ); |
| 8269 | |
| 8270 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) ); |
| 8271 | |
| 8272 | mbedtls_md_init( &ctx ); |
| 8273 | |
| 8274 | /* |
| 8275 | * digitally-signed struct { |
| 8276 | * opaque client_random[32]; |
| 8277 | * opaque server_random[32]; |
| 8278 | * ServerDHParams params; |
| 8279 | * }; |
| 8280 | */ |
| 8281 | if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 ) |
| 8282 | { |
| 8283 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret ); |
| 8284 | goto exit; |
| 8285 | } |
| 8286 | if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 ) |
| 8287 | { |
| 8288 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret ); |
| 8289 | goto exit; |
| 8290 | } |
| 8291 | if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 ) |
| 8292 | { |
| 8293 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret ); |
| 8294 | goto exit; |
| 8295 | } |
| 8296 | if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 ) |
| 8297 | { |
| 8298 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret ); |
| 8299 | goto exit; |
| 8300 | } |
| 8301 | if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 ) |
| 8302 | { |
| 8303 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret ); |
| 8304 | goto exit; |
| 8305 | } |
| 8306 | |
| 8307 | exit: |
| 8308 | mbedtls_md_free( &ctx ); |
| 8309 | |
| 8310 | if( ret != 0 ) |
| 8311 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 8312 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
| 8313 | |
| 8314 | return( ret ); |
| 8315 | } |
| 8316 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 8317 | |
Jerry Yu | d9d91da | 2022-02-17 14:57:06 +0800 | [diff] [blame] | 8318 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
| 8319 | |
Gabor Mezei | a3d016c | 2022-05-10 12:44:09 +0200 | [diff] [blame] | 8320 | /* Find the preferred hash for a given signature algorithm. */ |
| 8321 | unsigned int mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg( |
| 8322 | mbedtls_ssl_context *ssl, |
| 8323 | unsigned int sig_alg ) |
Jerry Yu | d9d91da | 2022-02-17 14:57:06 +0800 | [diff] [blame] | 8324 | { |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 8325 | unsigned int i; |
Gabor Mezei | a3d016c | 2022-05-10 12:44:09 +0200 | [diff] [blame] | 8326 | uint16_t *received_sig_algs = ssl->handshake->received_sig_algs; |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 8327 | |
| 8328 | if( sig_alg == MBEDTLS_SSL_SIG_ANON ) |
Gabor Mezei | a3d016c | 2022-05-10 12:44:09 +0200 | [diff] [blame] | 8329 | return( MBEDTLS_SSL_HASH_NONE ); |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 8330 | |
Gabor Mezei | a3d016c | 2022-05-10 12:44:09 +0200 | [diff] [blame] | 8331 | for( i = 0; received_sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ ) |
Jerry Yu | d9d91da | 2022-02-17 14:57:06 +0800 | [diff] [blame] | 8332 | { |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 8333 | unsigned int hash_alg_received = |
| 8334 | MBEDTLS_SSL_TLS12_HASH_ALG_FROM_SIG_AND_HASH_ALG( |
| 8335 | received_sig_algs[i] ); |
| 8336 | unsigned int sig_alg_received = |
| 8337 | MBEDTLS_SSL_TLS12_SIG_ALG_FROM_SIG_AND_HASH_ALG( |
| 8338 | received_sig_algs[i] ); |
| 8339 | |
| 8340 | if( sig_alg == sig_alg_received ) |
| 8341 | { |
| 8342 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 8343 | if( ssl->handshake->key_cert && ssl->handshake->key_cert->key ) |
| 8344 | { |
Neil Armstrong | 96eceb8 | 2022-06-30 18:05:05 +0200 | [diff] [blame] | 8345 | psa_algorithm_t psa_hash_alg = |
Manuel Pégourié-Gonnard | abac037 | 2022-07-18 13:41:11 +0200 | [diff] [blame] | 8346 | mbedtls_hash_info_psa_from_md( hash_alg_received ); |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 8347 | |
Neil Armstrong | 96eceb8 | 2022-06-30 18:05:05 +0200 | [diff] [blame] | 8348 | if( sig_alg_received == MBEDTLS_SSL_SIG_ECDSA && |
| 8349 | ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key, |
| 8350 | PSA_ALG_ECDSA( psa_hash_alg ), |
| 8351 | PSA_KEY_USAGE_SIGN_HASH ) ) |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 8352 | continue; |
| 8353 | |
Neil Armstrong | 96eceb8 | 2022-06-30 18:05:05 +0200 | [diff] [blame] | 8354 | if( sig_alg_received == MBEDTLS_SSL_SIG_RSA && |
Neil Armstrong | 971f30d | 2022-07-01 16:23:50 +0200 | [diff] [blame] | 8355 | ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key, |
| 8356 | PSA_ALG_RSA_PKCS1V15_SIGN( |
| 8357 | psa_hash_alg ), |
| 8358 | PSA_KEY_USAGE_SIGN_HASH ) ) |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 8359 | continue; |
| 8360 | } |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 8361 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Neil Armstrong | 96eceb8 | 2022-06-30 18:05:05 +0200 | [diff] [blame] | 8362 | |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 8363 | return( hash_alg_received ); |
| 8364 | } |
Jerry Yu | d9d91da | 2022-02-17 14:57:06 +0800 | [diff] [blame] | 8365 | } |
Jerry Yu | d9d91da | 2022-02-17 14:57:06 +0800 | [diff] [blame] | 8366 | |
Gabor Mezei | a3d016c | 2022-05-10 12:44:09 +0200 | [diff] [blame] | 8367 | return( MBEDTLS_SSL_HASH_NONE ); |
Jerry Yu | d9d91da | 2022-02-17 14:57:06 +0800 | [diff] [blame] | 8368 | } |
| 8369 | |
| 8370 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 8371 | |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8372 | /* Serialization of TLS 1.2 sessions: |
| 8373 | * |
| 8374 | * struct { |
| 8375 | * uint64 start_time; |
| 8376 | * uint8 ciphersuite[2]; // defined by the standard |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8377 | * uint8 session_id_len; // at most 32 |
| 8378 | * opaque session_id[32]; |
| 8379 | * opaque master[48]; // fixed length in the standard |
| 8380 | * uint32 verify_result; |
| 8381 | * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert |
| 8382 | * opaque ticket<0..2^24-1>; // length 0 means no ticket |
| 8383 | * uint32 ticket_lifetime; |
| 8384 | * uint8 mfl_code; // up to 255 according to standard |
| 8385 | * uint8 encrypt_then_mac; // 0 or 1 |
| 8386 | * } serialized_session_tls12; |
| 8387 | * |
| 8388 | */ |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 8389 | static size_t ssl_tls12_session_save( const mbedtls_ssl_session *session, |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8390 | unsigned char *buf, |
| 8391 | size_t buf_len ) |
| 8392 | { |
| 8393 | unsigned char *p = buf; |
| 8394 | size_t used = 0; |
| 8395 | |
| 8396 | #if defined(MBEDTLS_HAVE_TIME) |
| 8397 | uint64_t start; |
| 8398 | #endif |
| 8399 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 8400 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 8401 | size_t cert_len; |
| 8402 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 8403 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 8404 | |
| 8405 | /* |
| 8406 | * Time |
| 8407 | */ |
| 8408 | #if defined(MBEDTLS_HAVE_TIME) |
| 8409 | used += 8; |
| 8410 | |
| 8411 | if( used <= buf_len ) |
| 8412 | { |
| 8413 | start = (uint64_t) session->start; |
| 8414 | |
| 8415 | MBEDTLS_PUT_UINT64_BE( start, p, 0 ); |
| 8416 | p += 8; |
| 8417 | } |
| 8418 | #endif /* MBEDTLS_HAVE_TIME */ |
| 8419 | |
| 8420 | /* |
| 8421 | * Basic mandatory fields |
| 8422 | */ |
| 8423 | used += 2 /* ciphersuite */ |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8424 | + 1 /* id_len */ |
| 8425 | + sizeof( session->id ) |
| 8426 | + sizeof( session->master ) |
| 8427 | + 4; /* verify_result */ |
| 8428 | |
| 8429 | if( used <= buf_len ) |
| 8430 | { |
| 8431 | MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 0 ); |
| 8432 | p += 2; |
| 8433 | |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8434 | *p++ = MBEDTLS_BYTE_0( session->id_len ); |
| 8435 | memcpy( p, session->id, 32 ); |
| 8436 | p += 32; |
| 8437 | |
| 8438 | memcpy( p, session->master, 48 ); |
| 8439 | p += 48; |
| 8440 | |
| 8441 | MBEDTLS_PUT_UINT32_BE( session->verify_result, p, 0 ); |
| 8442 | p += 4; |
| 8443 | } |
| 8444 | |
| 8445 | /* |
| 8446 | * Peer's end-entity certificate |
| 8447 | */ |
| 8448 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 8449 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 8450 | if( session->peer_cert == NULL ) |
| 8451 | cert_len = 0; |
| 8452 | else |
| 8453 | cert_len = session->peer_cert->raw.len; |
| 8454 | |
| 8455 | used += 3 + cert_len; |
| 8456 | |
| 8457 | if( used <= buf_len ) |
| 8458 | { |
| 8459 | *p++ = MBEDTLS_BYTE_2( cert_len ); |
| 8460 | *p++ = MBEDTLS_BYTE_1( cert_len ); |
| 8461 | *p++ = MBEDTLS_BYTE_0( cert_len ); |
| 8462 | |
| 8463 | if( session->peer_cert != NULL ) |
| 8464 | { |
| 8465 | memcpy( p, session->peer_cert->raw.p, cert_len ); |
| 8466 | p += cert_len; |
| 8467 | } |
| 8468 | } |
| 8469 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 8470 | if( session->peer_cert_digest != NULL ) |
| 8471 | { |
| 8472 | used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len; |
| 8473 | if( used <= buf_len ) |
| 8474 | { |
| 8475 | *p++ = (unsigned char) session->peer_cert_digest_type; |
| 8476 | *p++ = (unsigned char) session->peer_cert_digest_len; |
| 8477 | memcpy( p, session->peer_cert_digest, |
| 8478 | session->peer_cert_digest_len ); |
| 8479 | p += session->peer_cert_digest_len; |
| 8480 | } |
| 8481 | } |
| 8482 | else |
| 8483 | { |
| 8484 | used += 2; |
| 8485 | if( used <= buf_len ) |
| 8486 | { |
| 8487 | *p++ = (unsigned char) MBEDTLS_MD_NONE; |
| 8488 | *p++ = 0; |
| 8489 | } |
| 8490 | } |
| 8491 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 8492 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 8493 | |
| 8494 | /* |
| 8495 | * Session ticket if any, plus associated data |
| 8496 | */ |
| 8497 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
| 8498 | used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */ |
| 8499 | |
| 8500 | if( used <= buf_len ) |
| 8501 | { |
| 8502 | *p++ = MBEDTLS_BYTE_2( session->ticket_len ); |
| 8503 | *p++ = MBEDTLS_BYTE_1( session->ticket_len ); |
| 8504 | *p++ = MBEDTLS_BYTE_0( session->ticket_len ); |
| 8505 | |
| 8506 | if( session->ticket != NULL ) |
| 8507 | { |
| 8508 | memcpy( p, session->ticket, session->ticket_len ); |
| 8509 | p += session->ticket_len; |
| 8510 | } |
| 8511 | |
| 8512 | MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 ); |
| 8513 | p += 4; |
| 8514 | } |
| 8515 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
| 8516 | |
| 8517 | /* |
| 8518 | * Misc extension-related info |
| 8519 | */ |
| 8520 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 8521 | used += 1; |
| 8522 | |
| 8523 | if( used <= buf_len ) |
| 8524 | *p++ = session->mfl_code; |
| 8525 | #endif |
| 8526 | |
| 8527 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 8528 | used += 1; |
| 8529 | |
| 8530 | if( used <= buf_len ) |
| 8531 | *p++ = MBEDTLS_BYTE_0( session->encrypt_then_mac ); |
| 8532 | #endif |
| 8533 | |
| 8534 | return( used ); |
| 8535 | } |
| 8536 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 8537 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 8538 | static int ssl_tls12_session_load( mbedtls_ssl_session *session, |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8539 | const unsigned char *buf, |
| 8540 | size_t len ) |
| 8541 | { |
| 8542 | #if defined(MBEDTLS_HAVE_TIME) |
| 8543 | uint64_t start; |
| 8544 | #endif |
| 8545 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 8546 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 8547 | size_t cert_len; |
| 8548 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 8549 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 8550 | |
| 8551 | const unsigned char *p = buf; |
| 8552 | const unsigned char * const end = buf + len; |
| 8553 | |
| 8554 | /* |
| 8555 | * Time |
| 8556 | */ |
| 8557 | #if defined(MBEDTLS_HAVE_TIME) |
| 8558 | if( 8 > (size_t)( end - p ) ) |
| 8559 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8560 | |
| 8561 | start = ( (uint64_t) p[0] << 56 ) | |
| 8562 | ( (uint64_t) p[1] << 48 ) | |
| 8563 | ( (uint64_t) p[2] << 40 ) | |
| 8564 | ( (uint64_t) p[3] << 32 ) | |
| 8565 | ( (uint64_t) p[4] << 24 ) | |
| 8566 | ( (uint64_t) p[5] << 16 ) | |
| 8567 | ( (uint64_t) p[6] << 8 ) | |
| 8568 | ( (uint64_t) p[7] ); |
| 8569 | p += 8; |
| 8570 | |
| 8571 | session->start = (time_t) start; |
| 8572 | #endif /* MBEDTLS_HAVE_TIME */ |
| 8573 | |
| 8574 | /* |
| 8575 | * Basic mandatory fields |
| 8576 | */ |
Thomas Daubney | 20f89a9 | 2022-06-20 15:12:19 +0100 | [diff] [blame] | 8577 | if( 2 + 1 + 32 + 48 + 4 > (size_t)( end - p ) ) |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8578 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8579 | |
| 8580 | session->ciphersuite = ( p[0] << 8 ) | p[1]; |
| 8581 | p += 2; |
| 8582 | |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8583 | session->id_len = *p++; |
| 8584 | memcpy( session->id, p, 32 ); |
| 8585 | p += 32; |
| 8586 | |
| 8587 | memcpy( session->master, p, 48 ); |
| 8588 | p += 48; |
| 8589 | |
| 8590 | session->verify_result = ( (uint32_t) p[0] << 24 ) | |
| 8591 | ( (uint32_t) p[1] << 16 ) | |
| 8592 | ( (uint32_t) p[2] << 8 ) | |
| 8593 | ( (uint32_t) p[3] ); |
| 8594 | p += 4; |
| 8595 | |
| 8596 | /* Immediately clear invalid pointer values that have been read, in case |
| 8597 | * we exit early before we replaced them with valid ones. */ |
| 8598 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 8599 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 8600 | session->peer_cert = NULL; |
| 8601 | #else |
| 8602 | session->peer_cert_digest = NULL; |
| 8603 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 8604 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 8605 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
| 8606 | session->ticket = NULL; |
| 8607 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
| 8608 | |
| 8609 | /* |
| 8610 | * Peer certificate |
| 8611 | */ |
| 8612 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 8613 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 8614 | /* Deserialize CRT from the end of the ticket. */ |
| 8615 | if( 3 > (size_t)( end - p ) ) |
| 8616 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8617 | |
| 8618 | cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2]; |
| 8619 | p += 3; |
| 8620 | |
| 8621 | if( cert_len != 0 ) |
| 8622 | { |
| 8623 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 8624 | |
| 8625 | if( cert_len > (size_t)( end - p ) ) |
| 8626 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8627 | |
| 8628 | session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ); |
| 8629 | |
| 8630 | if( session->peer_cert == NULL ) |
| 8631 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 8632 | |
| 8633 | mbedtls_x509_crt_init( session->peer_cert ); |
| 8634 | |
| 8635 | if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert, |
| 8636 | p, cert_len ) ) != 0 ) |
| 8637 | { |
| 8638 | mbedtls_x509_crt_free( session->peer_cert ); |
| 8639 | mbedtls_free( session->peer_cert ); |
| 8640 | session->peer_cert = NULL; |
| 8641 | return( ret ); |
| 8642 | } |
| 8643 | |
| 8644 | p += cert_len; |
| 8645 | } |
| 8646 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 8647 | /* Deserialize CRT digest from the end of the ticket. */ |
| 8648 | if( 2 > (size_t)( end - p ) ) |
| 8649 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8650 | |
| 8651 | session->peer_cert_digest_type = (mbedtls_md_type_t) *p++; |
| 8652 | session->peer_cert_digest_len = (size_t) *p++; |
| 8653 | |
| 8654 | if( session->peer_cert_digest_len != 0 ) |
| 8655 | { |
| 8656 | const mbedtls_md_info_t *md_info = |
| 8657 | mbedtls_md_info_from_type( session->peer_cert_digest_type ); |
| 8658 | if( md_info == NULL ) |
| 8659 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8660 | if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) ) |
| 8661 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8662 | |
| 8663 | if( session->peer_cert_digest_len > (size_t)( end - p ) ) |
| 8664 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8665 | |
| 8666 | session->peer_cert_digest = |
| 8667 | mbedtls_calloc( 1, session->peer_cert_digest_len ); |
| 8668 | if( session->peer_cert_digest == NULL ) |
| 8669 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 8670 | |
| 8671 | memcpy( session->peer_cert_digest, p, |
| 8672 | session->peer_cert_digest_len ); |
| 8673 | p += session->peer_cert_digest_len; |
| 8674 | } |
| 8675 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 8676 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 8677 | |
| 8678 | /* |
| 8679 | * Session ticket and associated data |
| 8680 | */ |
| 8681 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
| 8682 | if( 3 > (size_t)( end - p ) ) |
| 8683 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8684 | |
| 8685 | session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2]; |
| 8686 | p += 3; |
| 8687 | |
| 8688 | if( session->ticket_len != 0 ) |
| 8689 | { |
| 8690 | if( session->ticket_len > (size_t)( end - p ) ) |
| 8691 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8692 | |
| 8693 | session->ticket = mbedtls_calloc( 1, session->ticket_len ); |
| 8694 | if( session->ticket == NULL ) |
| 8695 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 8696 | |
| 8697 | memcpy( session->ticket, p, session->ticket_len ); |
| 8698 | p += session->ticket_len; |
| 8699 | } |
| 8700 | |
| 8701 | if( 4 > (size_t)( end - p ) ) |
| 8702 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8703 | |
| 8704 | session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) | |
| 8705 | ( (uint32_t) p[1] << 16 ) | |
| 8706 | ( (uint32_t) p[2] << 8 ) | |
| 8707 | ( (uint32_t) p[3] ); |
| 8708 | p += 4; |
| 8709 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
| 8710 | |
| 8711 | /* |
| 8712 | * Misc extension-related info |
| 8713 | */ |
| 8714 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 8715 | if( 1 > (size_t)( end - p ) ) |
| 8716 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8717 | |
| 8718 | session->mfl_code = *p++; |
| 8719 | #endif |
| 8720 | |
| 8721 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 8722 | if( 1 > (size_t)( end - p ) ) |
| 8723 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8724 | |
| 8725 | session->encrypt_then_mac = *p++; |
| 8726 | #endif |
| 8727 | |
| 8728 | /* Done, should have consumed entire buffer */ |
| 8729 | if( p != end ) |
| 8730 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8731 | |
| 8732 | return( 0 ); |
| 8733 | } |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 8734 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 8735 | |
XiaokangQian | 75d40ef | 2022-04-20 11:05:24 +0000 | [diff] [blame] | 8736 | int mbedtls_ssl_validate_ciphersuite( |
| 8737 | const mbedtls_ssl_context *ssl, |
| 8738 | const mbedtls_ssl_ciphersuite_t *suite_info, |
| 8739 | mbedtls_ssl_protocol_version min_tls_version, |
| 8740 | mbedtls_ssl_protocol_version max_tls_version ) |
| 8741 | { |
| 8742 | (void) ssl; |
| 8743 | |
| 8744 | if( suite_info == NULL ) |
| 8745 | return( -1 ); |
| 8746 | |
| 8747 | if( ( suite_info->min_tls_version > max_tls_version ) || |
| 8748 | ( suite_info->max_tls_version < min_tls_version ) ) |
| 8749 | { |
| 8750 | return( -1 ); |
| 8751 | } |
| 8752 | |
XiaokangQian | 060d867 | 2022-04-21 09:24:56 +0000 | [diff] [blame] | 8753 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_CLI_C) |
XiaokangQian | 75d40ef | 2022-04-20 11:05:24 +0000 | [diff] [blame] | 8754 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 8755 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 8756 | if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE && |
| 8757 | ssl->handshake->psa_pake_ctx_is_ok != 1 ) |
| 8758 | #else |
XiaokangQian | 75d40ef | 2022-04-20 11:05:24 +0000 | [diff] [blame] | 8759 | if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE && |
| 8760 | mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 ) |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 8761 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
XiaokangQian | 75d40ef | 2022-04-20 11:05:24 +0000 | [diff] [blame] | 8762 | { |
| 8763 | return( -1 ); |
| 8764 | } |
| 8765 | #endif |
| 8766 | |
| 8767 | /* Don't suggest PSK-based ciphersuite if no PSK is available. */ |
| 8768 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
| 8769 | if( mbedtls_ssl_ciphersuite_uses_psk( suite_info ) && |
| 8770 | mbedtls_ssl_conf_has_static_psk( ssl->conf ) == 0 ) |
| 8771 | { |
| 8772 | return( -1 ); |
| 8773 | } |
| 8774 | #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ |
| 8775 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 8776 | |
| 8777 | return( 0 ); |
| 8778 | } |
| 8779 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 8780 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 8781 | /* |
| 8782 | * Function for writing a signature algorithm extension. |
| 8783 | * |
| 8784 | * The `extension_data` field of signature algorithm contains a `SignatureSchemeList` |
| 8785 | * value (TLS 1.3 RFC8446): |
| 8786 | * enum { |
| 8787 | * .... |
| 8788 | * ecdsa_secp256r1_sha256( 0x0403 ), |
| 8789 | * ecdsa_secp384r1_sha384( 0x0503 ), |
| 8790 | * ecdsa_secp521r1_sha512( 0x0603 ), |
| 8791 | * .... |
| 8792 | * } SignatureScheme; |
| 8793 | * |
| 8794 | * struct { |
| 8795 | * SignatureScheme supported_signature_algorithms<2..2^16-2>; |
| 8796 | * } SignatureSchemeList; |
| 8797 | * |
| 8798 | * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm` |
| 8799 | * value (TLS 1.2 RFC5246): |
| 8800 | * enum { |
| 8801 | * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5), |
| 8802 | * sha512(6), (255) |
| 8803 | * } HashAlgorithm; |
| 8804 | * |
| 8805 | * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) } |
| 8806 | * SignatureAlgorithm; |
| 8807 | * |
| 8808 | * struct { |
| 8809 | * HashAlgorithm hash; |
| 8810 | * SignatureAlgorithm signature; |
| 8811 | * } SignatureAndHashAlgorithm; |
| 8812 | * |
| 8813 | * SignatureAndHashAlgorithm |
| 8814 | * supported_signature_algorithms<2..2^16-2>; |
| 8815 | * |
| 8816 | * The TLS 1.3 signature algorithm extension was defined to be a compatible |
| 8817 | * generalization of the TLS 1.2 signature algorithm extension. |
| 8818 | * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by |
| 8819 | * `SignatureScheme` field of TLS 1.3 |
| 8820 | * |
| 8821 | */ |
| 8822 | int mbedtls_ssl_write_sig_alg_ext( mbedtls_ssl_context *ssl, unsigned char *buf, |
| 8823 | const unsigned char *end, size_t *out_len ) |
| 8824 | { |
| 8825 | unsigned char *p = buf; |
| 8826 | unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */ |
| 8827 | size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */ |
| 8828 | |
| 8829 | *out_len = 0; |
| 8830 | |
| 8831 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding signature_algorithms extension" ) ); |
| 8832 | |
| 8833 | /* Check if we have space for header and length field: |
| 8834 | * - extension_type (2 bytes) |
| 8835 | * - extension_data_length (2 bytes) |
| 8836 | * - supported_signature_algorithms_length (2 bytes) |
| 8837 | */ |
| 8838 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 ); |
| 8839 | p += 6; |
| 8840 | |
| 8841 | /* |
| 8842 | * Write supported_signature_algorithms |
| 8843 | */ |
| 8844 | supported_sig_alg = p; |
| 8845 | const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs( ssl ); |
| 8846 | if( sig_alg == NULL ) |
| 8847 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); |
| 8848 | |
| 8849 | for( ; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++ ) |
| 8850 | { |
Jerry Yu | 53f5c15 | 2022-06-22 20:24:38 +0800 | [diff] [blame] | 8851 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "got signature scheme [%x] %s", |
| 8852 | *sig_alg, |
| 8853 | mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) ); |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 8854 | if( ! mbedtls_ssl_sig_alg_is_supported( ssl, *sig_alg ) ) |
| 8855 | continue; |
| 8856 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 ); |
| 8857 | MBEDTLS_PUT_UINT16_BE( *sig_alg, p, 0 ); |
| 8858 | p += 2; |
Jerry Yu | 80dd5db | 2022-06-22 19:30:32 +0800 | [diff] [blame] | 8859 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "sent signature scheme [%x] %s", |
Jerry Yu | f3b46b5 | 2022-06-19 16:52:27 +0800 | [diff] [blame] | 8860 | *sig_alg, |
| 8861 | mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) ); |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 8862 | } |
| 8863 | |
| 8864 | /* Length of supported_signature_algorithms */ |
| 8865 | supported_sig_alg_len = p - supported_sig_alg; |
| 8866 | if( supported_sig_alg_len == 0 ) |
| 8867 | { |
| 8868 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "No signature algorithms defined." ) ); |
| 8869 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 8870 | } |
| 8871 | |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 8872 | MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SIG_ALG, buf, 0 ); |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 8873 | MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len + 2, buf, 2 ); |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 8874 | MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len, buf, 4 ); |
| 8875 | |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 8876 | *out_len = p - buf; |
| 8877 | |
| 8878 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 8879 | ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SIG_ALG; |
| 8880 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 8881 | return( 0 ); |
| 8882 | } |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 8883 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 8884 | |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 8885 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 8886 | /* |
| 8887 | * mbedtls_ssl_parse_server_name_ext |
| 8888 | * |
| 8889 | * Structure of server_name extension: |
| 8890 | * |
| 8891 | * enum { |
| 8892 | * host_name(0), (255) |
| 8893 | * } NameType; |
| 8894 | * opaque HostName<1..2^16-1>; |
| 8895 | * |
| 8896 | * struct { |
| 8897 | * NameType name_type; |
| 8898 | * select (name_type) { |
| 8899 | * case host_name: HostName; |
| 8900 | * } name; |
| 8901 | * } ServerName; |
| 8902 | * struct { |
| 8903 | * ServerName server_name_list<1..2^16-1> |
| 8904 | * } ServerNameList; |
| 8905 | */ |
Ronald Cron | ce7d76e | 2022-07-08 18:56:49 +0200 | [diff] [blame] | 8906 | MBEDTLS_CHECK_RETURN_CRITICAL |
XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 8907 | int mbedtls_ssl_parse_server_name_ext( mbedtls_ssl_context *ssl, |
| 8908 | const unsigned char *buf, |
| 8909 | const unsigned char *end ) |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 8910 | { |
| 8911 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 8912 | const unsigned char *p = buf; |
XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 8913 | size_t server_name_list_len, hostname_len; |
| 8914 | const unsigned char *server_name_list_end; |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 8915 | |
XiaokangQian | f2a9420 | 2022-05-20 06:44:24 +0000 | [diff] [blame] | 8916 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "parse ServerName extension" ) ); |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 8917 | |
| 8918 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 ); |
XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 8919 | server_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 ); |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 8920 | p += 2; |
| 8921 | |
XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 8922 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, server_name_list_len ); |
| 8923 | server_name_list_end = p + server_name_list_len; |
XiaokangQian | 75fe8c7 | 2022-06-15 09:42:45 +0000 | [diff] [blame] | 8924 | while( p < server_name_list_end ) |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 8925 | { |
XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 8926 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end, 3 ); |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 8927 | hostname_len = MBEDTLS_GET_UINT16_BE( p, 1 ); |
XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 8928 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end, |
| 8929 | hostname_len + 3 ); |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 8930 | |
| 8931 | if( p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME ) |
| 8932 | { |
XiaokangQian | 75fe8c7 | 2022-06-15 09:42:45 +0000 | [diff] [blame] | 8933 | /* sni_name is intended to be used only during the parsing of the |
| 8934 | * ClientHello message (it is reset to NULL before the end of |
| 8935 | * the message parsing). Thus it is ok to just point to the |
| 8936 | * reception buffer and not make a copy of it. |
| 8937 | */ |
XiaokangQian | f2a9420 | 2022-05-20 06:44:24 +0000 | [diff] [blame] | 8938 | ssl->handshake->sni_name = p + 3; |
| 8939 | ssl->handshake->sni_name_len = hostname_len; |
| 8940 | if( ssl->conf->f_sni == NULL ) |
| 8941 | return( 0 ); |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 8942 | ret = ssl->conf->f_sni( ssl->conf->p_sni, |
XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 8943 | ssl, p + 3, hostname_len ); |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 8944 | if( ret != 0 ) |
| 8945 | { |
XiaokangQian | f2a9420 | 2022-05-20 06:44:24 +0000 | [diff] [blame] | 8946 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_sni_wrapper", ret ); |
XiaokangQian | 129aeb9 | 2022-06-02 09:29:18 +0000 | [diff] [blame] | 8947 | MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME, |
| 8948 | MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME ); |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 8949 | return( MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME ); |
| 8950 | } |
| 8951 | return( 0 ); |
| 8952 | } |
| 8953 | |
| 8954 | p += hostname_len + 3; |
| 8955 | } |
| 8956 | |
| 8957 | return( 0 ); |
| 8958 | } |
| 8959 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
| 8960 | |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8961 | #if defined(MBEDTLS_SSL_ALPN) |
Ronald Cron | ce7d76e | 2022-07-08 18:56:49 +0200 | [diff] [blame] | 8962 | MBEDTLS_CHECK_RETURN_CRITICAL |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8963 | int mbedtls_ssl_parse_alpn_ext( mbedtls_ssl_context *ssl, |
| 8964 | const unsigned char *buf, |
| 8965 | const unsigned char *end ) |
| 8966 | { |
| 8967 | const unsigned char *p = buf; |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 8968 | size_t protocol_name_list_len; |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 8969 | const unsigned char *protocol_name_list; |
| 8970 | const unsigned char *protocol_name_list_end; |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 8971 | size_t protocol_name_len; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8972 | |
| 8973 | /* If ALPN not configured, just ignore the extension */ |
| 8974 | if( ssl->conf->alpn_list == NULL ) |
| 8975 | return( 0 ); |
| 8976 | |
| 8977 | /* |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 8978 | * RFC7301, section 3.1 |
| 8979 | * opaque ProtocolName<1..2^8-1>; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8980 | * |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 8981 | * struct { |
| 8982 | * ProtocolName protocol_name_list<2..2^16-1> |
| 8983 | * } ProtocolNameList; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8984 | */ |
| 8985 | |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 8986 | /* |
XiaokangQian | 0b776e2 | 2022-06-24 09:04:59 +0000 | [diff] [blame] | 8987 | * protocol_name_list_len 2 bytes |
| 8988 | * protocol_name_len 1 bytes |
| 8989 | * protocol_name >=1 byte |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 8990 | */ |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8991 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 ); |
| 8992 | |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 8993 | protocol_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 ); |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8994 | p += 2; |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 8995 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, protocol_name_list_len ); |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 8996 | protocol_name_list = p; |
| 8997 | protocol_name_list_end = p + protocol_name_list_len; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8998 | |
| 8999 | /* Validate peer's list (lengths) */ |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9000 | while( p < protocol_name_list_end ) |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9001 | { |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9002 | protocol_name_len = *p++; |
| 9003 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, protocol_name_list_end, |
| 9004 | protocol_name_len ); |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 9005 | if( protocol_name_len == 0 ) |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9006 | { |
| 9007 | MBEDTLS_SSL_PEND_FATAL_ALERT( |
| 9008 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER, |
| 9009 | MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER ); |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9010 | return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER ); |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9011 | } |
| 9012 | |
| 9013 | p += protocol_name_len; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9014 | } |
| 9015 | |
| 9016 | /* Use our order of preference */ |
| 9017 | for( const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++ ) |
| 9018 | { |
| 9019 | size_t const alpn_len = strlen( *alpn ); |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9020 | p = protocol_name_list; |
| 9021 | while( p < protocol_name_list_end ) |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9022 | { |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9023 | protocol_name_len = *p++; |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 9024 | if( protocol_name_len == alpn_len && |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9025 | memcmp( p, *alpn, alpn_len ) == 0 ) |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9026 | { |
| 9027 | ssl->alpn_chosen = *alpn; |
| 9028 | return( 0 ); |
| 9029 | } |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9030 | |
| 9031 | p += protocol_name_len; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9032 | } |
| 9033 | } |
| 9034 | |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9035 | /* If we get here, no match was found */ |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9036 | MBEDTLS_SSL_PEND_FATAL_ALERT( |
| 9037 | MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL, |
| 9038 | MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL ); |
| 9039 | return( MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL ); |
| 9040 | } |
| 9041 | |
| 9042 | int mbedtls_ssl_write_alpn_ext( mbedtls_ssl_context *ssl, |
| 9043 | unsigned char *buf, |
| 9044 | unsigned char *end, |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 9045 | size_t *out_len ) |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9046 | { |
| 9047 | unsigned char *p = buf; |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9048 | size_t protocol_name_len; |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 9049 | *out_len = 0; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9050 | |
| 9051 | if( ssl->alpn_chosen == NULL ) |
| 9052 | { |
| 9053 | return( 0 ); |
| 9054 | } |
| 9055 | |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9056 | protocol_name_len = strlen( ssl->alpn_chosen ); |
| 9057 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 7 + protocol_name_len ); |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9058 | |
| 9059 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "server side, adding alpn extension" ) ); |
| 9060 | /* |
| 9061 | * 0 . 1 ext identifier |
| 9062 | * 2 . 3 ext length |
| 9063 | * 4 . 5 protocol list length |
| 9064 | * 6 . 6 protocol name length |
| 9065 | * 7 . 7+n protocol name |
| 9066 | */ |
| 9067 | MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ALPN, p, 0 ); |
| 9068 | |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9069 | *out_len = 7 + protocol_name_len; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9070 | |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9071 | MBEDTLS_PUT_UINT16_BE( protocol_name_len + 3, p, 2 ); |
| 9072 | MBEDTLS_PUT_UINT16_BE( protocol_name_len + 1, p, 4 ); |
XiaokangQian | 0b776e2 | 2022-06-24 09:04:59 +0000 | [diff] [blame] | 9073 | /* Note: the length of the chosen protocol has been checked to be less |
| 9074 | * than 255 bytes in `mbedtls_ssl_conf_alpn_protocols`. |
| 9075 | */ |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9076 | p[6] = MBEDTLS_BYTE_0( protocol_name_len ); |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9077 | |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9078 | memcpy( p + 7, ssl->alpn_chosen, protocol_name_len ); |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9079 | return ( 0 ); |
| 9080 | } |
| 9081 | #endif /* MBEDTLS_SSL_ALPN */ |
| 9082 | |
Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 9083 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \ |
Xiaokang Qian | 0340929 | 2022-10-12 02:49:52 +0000 | [diff] [blame] | 9084 | defined(MBEDTLS_SSL_SESSION_TICKETS) && \ |
Xiaokang Qian | ed0620c | 2022-10-12 06:58:13 +0000 | [diff] [blame] | 9085 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \ |
Xiaokang Qian | ed3afcd | 2022-10-12 08:31:11 +0000 | [diff] [blame] | 9086 | defined(MBEDTLS_SSL_CLI_C) |
Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 9087 | int mbedtls_ssl_session_set_hostname( mbedtls_ssl_session *session, |
| 9088 | const char *hostname ) |
| 9089 | { |
| 9090 | /* Initialize to suppress unnecessary compiler warning */ |
| 9091 | size_t hostname_len = 0; |
| 9092 | |
| 9093 | /* Check if new hostname is valid before |
| 9094 | * making any change to current one */ |
| 9095 | if( hostname != NULL ) |
| 9096 | { |
| 9097 | hostname_len = strlen( hostname ); |
| 9098 | |
| 9099 | if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN ) |
| 9100 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 9101 | } |
| 9102 | |
| 9103 | /* Now it's clear that we will overwrite the old hostname, |
| 9104 | * so we can free it safely */ |
| 9105 | if( session->hostname != NULL ) |
| 9106 | { |
| 9107 | mbedtls_platform_zeroize( session->hostname, |
| 9108 | strlen( session->hostname ) ); |
| 9109 | mbedtls_free( session->hostname ); |
| 9110 | } |
| 9111 | |
| 9112 | /* Passing NULL as hostname shall clear the old one */ |
| 9113 | if( hostname == NULL ) |
| 9114 | { |
| 9115 | session->hostname = NULL; |
| 9116 | } |
| 9117 | else |
| 9118 | { |
| 9119 | session->hostname = mbedtls_calloc( 1, hostname_len + 1 ); |
| 9120 | if( session->hostname == NULL ) |
| 9121 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 9122 | |
| 9123 | memcpy( session->hostname, hostname, hostname_len ); |
| 9124 | } |
| 9125 | |
| 9126 | return( 0 ); |
| 9127 | } |
Xiaokang Qian | 0340929 | 2022-10-12 02:49:52 +0000 | [diff] [blame] | 9128 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 && |
| 9129 | MBEDTLS_SSL_SESSION_TICKETS && |
Xiaokang Qian | ed0620c | 2022-10-12 06:58:13 +0000 | [diff] [blame] | 9130 | MBEDTLS_SSL_SERVER_NAME_INDICATION && |
Xiaokang Qian | ed3afcd | 2022-10-12 08:31:11 +0000 | [diff] [blame] | 9131 | MBEDTLS_SSL_CLI_C */ |
Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 9132 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9133 | #endif /* MBEDTLS_SSL_TLS_C */ |